1 ;;; url-http.el --- HTTP retrieval routines
3 ;; Copyright (C) 1999, 2001, 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; Author: Bill Perry <wmperry@gnu.org>
6 ;; Keywords: comm, data, processes
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
29 (eval-when-compile (require 'cl))
30 (defvar url-http-extra-headers)
31 (defvar url-http-target-url)
39 (autoload 'url-cache-create-filename "url-cache")
41 (defconst url-http-default-port 80 "Default HTTP port.")
42 (defconst url-http-asynchronous-p t "HTTP retrievals are asynchronous.")
43 (defalias 'url-http-expand-file-name 'url-default-expander)
45 (defvar url-http-real-basic-auth-storage nil)
46 (defvar url-http-proxy-basic-auth-storage nil)
48 (defvar url-http-open-connections (make-hash-table :test 'equal
50 "A hash table of all open network connections.")
52 (defvar url-http-version "1.1"
53 "What version of HTTP we advertise, as a string.
54 Valid values are 1.1 and 1.0.
55 This is only useful when debugging the HTTP subsystem.
57 Setting this to 1.0 will tell servers not to send chunked encoding,
58 and other HTTP/1.1 specific features.")
60 (defvar url-http-attempt-keepalives t
61 "Whether to use a single TCP connection multiple times in HTTP.
62 This is only useful when debugging the HTTP subsystem. Setting to
63 nil will explicitly close the connection to the server after every
67 ;; These are all macros so that they are hidden from external sight
68 ;; when the file is byte-compiled.
70 ;; This allows us to expose just the entry points we want.
72 ;; These routines will allow us to implement persistent HTTP
74 (defsubst url-http-debug (&rest args)
76 (let ((proc (get-buffer-process (current-buffer))))
77 ;; The user hit C-g, honor it! Some things can get in an
78 ;; incredibly tight loop (chunked encoding)
81 (set-process-sentinel proc nil)
82 (set-process-filter proc nil)))
83 (error "Transfer interrupted!")))
84 (apply 'url-debug 'http args))
86 (defun url-http-mark-connection-as-busy (host port proc)
87 (url-http-debug "Marking connection as busy: %s:%d %S" host port proc)
88 (puthash (cons host port)
89 (delq proc (gethash (cons host port) url-http-open-connections))
90 url-http-open-connections)
93 (defun url-http-mark-connection-as-free (host port proc)
94 (url-http-debug "Marking connection as free: %s:%d %S" host port proc)
95 (set-process-buffer proc nil)
96 (set-process-sentinel proc 'url-http-idle-sentinel)
97 (puthash (cons host port)
98 (cons proc (gethash (cons host port) url-http-open-connections))
99 url-http-open-connections)
102 (defun url-http-find-free-connection (host port)
103 (let ((conns (gethash (cons host port) url-http-open-connections))
105 (while (and conns (not found))
106 (if (not (memq (process-status (car conns)) '(run open)))
108 (url-http-debug "Cleaning up dead process: %s:%d %S"
109 host port (car conns))
110 (url-http-idle-sentinel (car conns) nil))
111 (setq found (car conns))
112 (url-http-debug "Found existing connection: %s:%d %S" host port found))
115 (url-http-debug "Reusing existing connection: %s:%d" host port)
116 (url-http-debug "Contacting host: %s:%d" host port))
117 (url-lazy-message "Contacting host: %s:%d" host port)
118 (url-http-mark-connection-as-busy
121 (let ((buf (generate-new-buffer " *url-http-temp*")))
122 ;; `url-open-stream' needs a buffer in which to do things
123 ;; like authentication. But we use another buffer afterwards.
125 (let ((proc (url-open-stream host buf host port)))
126 ;; Drop the temp buffer link before killing the buffer.
127 (set-process-buffer proc nil)
129 (kill-buffer buf)))))))
131 ;; Building an HTTP request
132 (defun url-http-user-agent-string ()
133 (if (or (eq url-privacy-level 'paranoid)
134 (and (listp url-privacy-level)
135 (memq 'agent url-privacy-level)))
137 (format "User-Agent: %sURL/%s%s\r\n"
139 (concat url-package-name "/" url-package-version " ")
143 ((and url-os-type url-system-type)
144 (concat " (" url-os-type "; " url-system-type ")"))
145 ((or url-os-type url-system-type)
146 (concat " (" (or url-system-type url-os-type) ")"))
149 (defun url-http-create-request (url &optional ref-url)
150 "Create an HTTP request for URL, referred to by REF-URL."
151 (declare (special proxy-object proxy-info))
152 (let* ((extra-headers)
154 (no-cache (cdr-safe (assoc "Pragma" url-request-extra-headers)))
155 (proxy-obj (and (boundp 'proxy-object) proxy-object))
156 (proxy-auth (if (or (cdr-safe (assoc "Proxy-Authorization"
157 url-request-extra-headers))
160 (let ((url-basic-auth-storage
161 'url-http-proxy-basic-auth-storage))
162 (url-get-authentication url nil 'any nil))))
163 (real-fname (concat (url-filename (or proxy-obj url))
164 (url-recreate-url-attributes (or proxy-obj url))))
165 (host (url-host (or proxy-obj url)))
166 (auth (if (cdr-safe (assoc "Authorization" url-request-extra-headers))
168 (url-get-authentication (or
169 (and (boundp 'proxy-info)
171 url) nil 'any nil))))
172 (if (equal "" real-fname)
173 (setq real-fname "/"))
174 (setq no-cache (and no-cache (string-match "no-cache" no-cache)))
176 (setq auth (concat "Authorization: " auth "\r\n")))
178 (setq proxy-auth (concat "Proxy-Authorization: " proxy-auth "\r\n")))
180 ;; Protection against stupid values in the referer
181 (if (and ref-url (stringp ref-url) (or (string= ref-url "file:nil")
182 (string= ref-url "")))
185 ;; We do not want to expose the referer if the user is paranoid.
186 (if (or (memq url-privacy-level '(low high paranoid))
187 (and (listp url-privacy-level)
188 (memq 'lastloc url-privacy-level)))
191 ;; url-request-extra-headers contains an assoc-list of
192 ;; header/value pairs that we need to put into the request.
193 (setq extra-headers (mapconcat
195 (concat (car x) ": " (cdr x)))
196 url-request-extra-headers "\r\n"))
197 (if (not (equal extra-headers ""))
198 (setq extra-headers (concat extra-headers "\r\n")))
200 ;; This was done with a call to `format'. Concatting parts has
201 ;; the advantage of keeping the parts of each header together and
202 ;; allows us to elide null lines directly, at the cost of making
203 ;; the layout less clear.
205 ;; We used to concat directly, but if one of the strings happens
206 ;; to being multibyte (even if it only contains pure ASCII) then
207 ;; every string gets converted with `string-MAKE-multibyte' which
208 ;; turns the 127-255 codes into things like latin-1 accented chars
209 ;; (it would work right if it used `string-TO-multibyte' instead).
210 ;; So to avoid the problem we force every string to be unibyte.
212 ;; FIXME: Instead of `string-AS-unibyte' we'd want
213 ;; `string-to-unibyte', so as to properly signal an error if one
214 ;; of the strings contains a multibyte char.
219 (or url-request-method "GET") " "
220 (if proxy-obj (url-recreate-url proxy-obj) real-fname)
221 " HTTP/" url-http-version "\r\n"
222 ;; Version of MIME we speak
223 "MIME-Version: 1.0\r\n"
224 ;; (maybe) Try to keep the connection open
225 "Connection: " (if (or proxy-obj
226 (not url-http-attempt-keepalives))
227 "close" "keep-alive") "\r\n"
228 ;; HTTP extensions we support
229 (if url-extensions-header
231 "Extension: %s\r\n" url-extensions-header))
232 ;; Who we want to talk to
233 (if (/= (url-port (or proxy-obj url))
234 (url-scheme-get-property
235 (url-type (or proxy-obj url)) 'default-port))
237 "Host: %s:%d\r\n" host (url-port (or proxy-obj url)))
238 (format "Host: %s\r\n" host))
240 (if url-personal-mail-address
242 "From: " url-personal-mail-address "\r\n"))
243 ;; Encodings we understand
244 (if url-mime-encoding-string
246 "Accept-encoding: " url-mime-encoding-string "\r\n"))
247 (if url-mime-charset-string
249 "Accept-charset: " url-mime-charset-string "\r\n"))
250 ;; Languages we understand
251 (if url-mime-language-string
253 "Accept-language: " url-mime-language-string "\r\n"))
254 ;; Types we understand
255 "Accept: " (or url-mime-accept-string "*/*") "\r\n"
257 (url-http-user-agent-string)
258 ;; Proxy Authorization
263 (url-cookie-generate-header-lines host real-fname
264 (equal "https" (url-type url)))
266 (if (and (not no-cache)
267 (member url-request-method '("GET" nil)))
268 (let ((tm (url-is-cached (or proxy-obj url))))
270 (concat "If-modified-since: "
271 (url-get-normalized-date tm) "\r\n"))))
274 "Referer: " ref-url "\r\n"))
279 "Content-length: " (number-to-string
280 (length url-request-data))
287 (url-http-debug "Request is: \n%s" request)
291 (defun url-http-clean-headers ()
292 "Remove trailing \r from header lines.
293 This allows us to use `mail-fetch-field', etc."
294 (declare (special url-http-end-of-headers))
295 (goto-char (point-min))
296 (while (re-search-forward "\r$" url-http-end-of-headers t)
299 (defun url-http-handle-authentication (proxy)
300 (declare (special status success url-http-method url-http-data
301 url-callback-function url-callback-arguments))
302 (url-http-debug "Handling %s authentication" (if proxy "proxy" "normal"))
303 (let ((auth (or (mail-fetch-field (if proxy "proxy-authenticate" "www-authenticate"))
306 (url (url-recreate-url url-current-object))
307 (url-basic-auth-storage 'url-http-real-basic-auth-storage)
310 ;; Cheating, but who cares? :)
312 (setq url-basic-auth-storage 'url-http-proxy-basic-auth-storage))
314 (setq auth (url-eat-trailing-space (url-strip-leading-spaces auth)))
315 (if (string-match "[ \t]" auth)
316 (setq type (downcase (substring auth 0 (match-beginning 0))))
317 (setq type (downcase auth)))
319 (if (not (url-auth-registered type))
322 (goto-char (point-max))
323 (insert "<hr>Sorry, but I do not know how to handle " type
324 " authentication. If you'd like to write it,"
325 " send it to " url-bug-address ".<hr>")
327 (let* ((args (url-parse-args (subst-char-in-string ?, ?\; auth)))
328 (auth (url-get-authentication url (cdr-safe (assoc "realm" args))
332 (push (cons (if proxy "Proxy-Authorization" "Authorization") auth)
333 url-http-extra-headers)
334 (let ((url-request-method url-http-method)
335 (url-request-data url-http-data)
336 (url-request-extra-headers url-http-extra-headers))
337 (url-retrieve url url-callback-function
338 url-callback-arguments)))))))
340 (defun url-http-parse-response ()
341 "Parse just the response code."
342 (declare (special url-http-end-of-headers url-http-response-status))
343 (if (not url-http-end-of-headers)
344 (error "Trying to parse HTTP response code in odd buffer: %s" (buffer-name)))
345 (url-http-debug "url-http-parse-response called in (%s)" (buffer-name))
346 (goto-char (point-min))
347 (skip-chars-forward " \t\n") ; Skip any blank crap
348 (skip-chars-forward "HTTP/") ; Skip HTTP Version
349 (read (current-buffer))
350 (setq url-http-response-status (read (current-buffer))))
352 (defun url-http-handle-cookies ()
353 "Handle all set-cookie / set-cookie2 headers in an HTTP response.
354 The buffer must already be narrowed to the headers, so `mail-fetch-field' will
356 (let ((cookies (mail-fetch-field "Set-Cookie" nil nil t))
357 (cookies2 (mail-fetch-field "Set-Cookie2" nil nil t))
358 (url-current-object url-http-target-url))
359 (and cookies (url-http-debug "Found %d Set-Cookie headers" (length cookies)))
360 (and cookies2 (url-http-debug "Found %d Set-Cookie2 headers" (length cookies2)))
362 (url-cookie-handle-set-cookie (pop cookies)))
364 ;;; (url-cookie-handle-set-cookie2 (pop cookies)))
368 (defun url-http-parse-headers ()
369 "Parse and handle HTTP specific headers.
370 Return t if and only if the current buffer is still active and
371 should be shown to the user."
372 ;; The comments after each status code handled are taken from RFC
374 (declare (special url-http-end-of-headers url-http-response-status
375 url-http-method url-http-data url-http-process
376 url-callback-function url-callback-arguments))
378 (url-http-mark-connection-as-free (url-host url-current-object)
379 (url-port url-current-object)
382 (if (or (not (boundp 'url-http-end-of-headers))
383 (not url-http-end-of-headers))
384 (error "Trying to parse headers in odd buffer: %s" (buffer-name)))
385 (goto-char (point-min))
386 (url-http-debug "url-http-parse-headers called in (%s)" (buffer-name))
387 (url-http-parse-response)
388 (mail-narrow-to-head)
389 ;;(narrow-to-region (point-min) url-http-end-of-headers)
390 (let ((connection (mail-fetch-field "Connection")))
392 (string= (downcase connection) "close"))
393 (delete-process url-http-process)))
396 (setq class (/ url-http-response-status 100))
397 (url-http-debug "Parsed HTTP headers: class=%d status=%d" class url-http-response-status)
398 (url-http-handle-cookies)
401 ;; Classes of response codes
403 ;; 5xx = Server Error
404 ;; 4xx = Client Error
407 ;; 1xx = Informational
408 (1 ; Information messages
409 ;; 100 = Continue with request
410 ;; 101 = Switching protocols
411 ;; 102 = Processing (Added by DAV)
412 (url-mark-buffer-as-dead (current-buffer))
413 (error "HTTP responses in class 1xx not supported (%d)" url-http-response-status))
418 ;; 203 Non-authoritative information
421 ;; 206 Partial content
422 ;; 207 Multi-status (Added by DAV)
423 (case url-http-response-status
425 ;; No new data, just stay at the same document
426 (url-mark-buffer-as-dead (current-buffer))
429 ;; Generic success for all others. Store in the cache, and
430 ;; mark it as successful.
432 (if (and url-automatic-caching (equal url-http-method "GET"))
433 (url-store-in-cache (current-buffer)))
436 ;; 300 Multiple choices
437 ;; 301 Moved permanently
442 ;; 307 Temporary redirect
443 (let ((redirect-uri (or (mail-fetch-field "Location")
444 (mail-fetch-field "URI"))))
445 (case url-http-response-status
447 ;; Quoth the spec (section 10.3.1)
448 ;; -------------------------------
449 ;; The requested resource corresponds to any one of a set of
450 ;; representations, each with its own specific location and
451 ;; agent-driven negotiation information is being provided so
452 ;; that the user can select a preferred representation and
453 ;; redirect its request to that location.
455 ;; If the server has a preferred choice of representation, it
456 ;; SHOULD include the specific URI for that representation in
457 ;; the Location field; user agents MAY use the Location field
458 ;; value for automatic redirection.
459 ;; -------------------------------
460 ;; We do not support agent-driven negotiation, so we just
461 ;; redirect to the preferred URI if one is provided.
464 ;; If the 301|302 status code is received in response to a
465 ;; request other than GET or HEAD, the user agent MUST NOT
466 ;; automatically redirect the request unless it can be
467 ;; confirmed by the user, since this might change the
468 ;; conditions under which the request was issued.
469 (if (member url-http-method '("HEAD" "GET"))
470 ;; Automatic redirection is ok
472 ;; It is just too big of a pain in the ass to get this
473 ;; prompt all the time. We will just silently lose our
474 ;; data and convert to a GET method.
475 (url-http-debug "Converting `%s' request to `GET' because of REDIRECT(%d)"
476 url-http-method url-http-response-status)
477 (setq url-http-method "GET"
480 ;; The response to the request can be found under a different
481 ;; URI and SHOULD be retrieved using a GET method on that
483 (setq url-http-method "GET"
486 ;; The 304 response MUST NOT contain a message-body.
487 (url-http-debug "Extracting document from cache... (%s)"
488 (url-cache-create-filename (url-view-url t)))
489 (url-cache-extract (url-cache-create-filename (url-view-url t)))
490 (setq redirect-uri nil
493 ;; The requested resource MUST be accessed through the
494 ;; proxy given by the Location field. The Location field
495 ;; gives the URI of the proxy. The recipient is expected
496 ;; to repeat this single request via the proxy. 305
497 ;; responses MUST only be generated by origin servers.
498 (error "Redirection thru a proxy server not supported: %s"
501 ;; Treat everything like '300'
504 ;; Clean off any whitespace and/or <...> cruft.
505 (if (string-match "\\([^ \t]+\\)[ \t]" redirect-uri)
506 (setq redirect-uri (match-string 1 redirect-uri)))
507 (if (string-match "^<\\(.*\\)>$" redirect-uri)
508 (setq redirect-uri (match-string 1 redirect-uri)))
510 ;; Some stupid sites (like sourceforge) send a
511 ;; non-fully-qualified URL (ie: /), which royally confuses
513 (if (not (string-match url-nonrelative-link redirect-uri))
514 ;; Be careful to use the real target URL, otherwise we may
515 ;; compute the redirection relative to the URL of the proxy.
517 (url-expand-file-name redirect-uri url-http-target-url)))
518 (let ((url-request-method url-http-method)
519 (url-request-data url-http-data)
520 (url-request-extra-headers url-http-extra-headers))
521 ;; Put in the current buffer a forwarding pointer to the new
522 ;; destination buffer.
523 ;; FIXME: This is a hack to fix url-retrieve-synchronously
524 ;; without changing the API. Instead url-retrieve should
525 ;; either simply not return the "destination" buffer, or it
526 ;; should take an optional `dest-buf' argument.
527 (set (make-local-variable 'url-redirect-buffer)
528 (url-retrieve redirect-uri url-callback-function
531 url-callback-arguments))))
532 (url-mark-buffer-as-dead (current-buffer))))))
536 ;; 402 Payment required
539 ;; 405 Method not allowed
540 ;; 406 Not acceptable
541 ;; 407 Proxy authentication required
542 ;; 408 Request time-out
545 ;; 411 Length required
546 ;; 412 Precondition failed
547 ;; 413 Request entity too large
548 ;; 414 Request-URI too large
549 ;; 415 Unsupported media type
550 ;; 416 Requested range not satisfiable
551 ;; 417 Expectation failed
552 ;; 422 Unprocessable Entity (Added by DAV)
554 ;; 424 Failed Dependency
555 (case url-http-response-status
557 ;; The request requires user authentication. The response
558 ;; MUST include a WWW-Authenticate header field containing a
559 ;; challenge applicable to the requested resource. The
560 ;; client MAY repeat the request with a suitable
561 ;; Authorization header field.
562 (url-http-handle-authentication nil))
564 ;; This code is reserved for future use
565 (url-mark-buffer-as-dead (current-buffer))
566 (error "Somebody wants you to give them money"))
568 ;; The server understood the request, but is refusing to
569 ;; fulfill it. Authorization will not help and the request
570 ;; SHOULD NOT be repeated.
576 ;; The method specified in the Request-Line is not allowed
577 ;; for the resource identified by the Request-URI. The
578 ;; response MUST include an Allow header containing a list of
579 ;; valid methods for the requested resource.
582 ;; The resource identified by the request is only capable of
583 ;; generating response entities which have content
584 ;; characteristics nota cceptable according to the accept
585 ;; headers sent in the request.
588 ;; This code is similar to 401 (Unauthorized), but indicates
589 ;; that the client must first authenticate itself with the
590 ;; proxy. The proxy MUST return a Proxy-Authenticate header
591 ;; field containing a challenge applicable to the proxy for
592 ;; the requested resource.
593 (url-http-handle-authentication t))
595 ;; The client did not produce a request within the time that
596 ;; the server was prepared to wait. The client MAY repeat
597 ;; the request without modifications at any later time.
600 ;; The request could not be completed due to a conflict with
601 ;; the current state of the resource. This code is only
602 ;; allowed in situations where it is expected that the user
603 ;; mioght be able to resolve the conflict and resubmit the
604 ;; request. The response body SHOULD include enough
605 ;; information for the user to recognize the source of the
609 ;; The requested resource is no longer available at the
610 ;; server and no forwarding address is known.
613 ;; The server refuses to accept the request without a defined
614 ;; Content-Length. The client MAY repeat the request if it
615 ;; adds a valid Content-Length header field containing the
616 ;; length of the message-body in the request message.
618 ;; NOTE - this will never happen because
619 ;; `url-http-create-request' automatically calculates the
623 ;; The precondition given in one or more of the
624 ;; request-header fields evaluated to false when it was
625 ;; tested on the server.
628 ;; The server is refusing to process a request because the
629 ;; request entity|URI is larger than the server is willing or
633 ;; The server is refusing to service the request because the
634 ;; entity of the request is in a format not supported by the
635 ;; requested resource for the requested method.
638 ;; A server SHOULD return a response with this status code if
639 ;; a request included a Range request-header field, and none
640 ;; of the range-specifier values in this field overlap the
641 ;; current extent of the selected resource, and the request
642 ;; did not include an If-Range request-header field.
645 ;; The expectation given in an Expect request-header field
646 ;; could not be met by this server, or, if the server is a
647 ;; proxy, the server has unambiguous evidence that the
648 ;; request could not be met by the next-hop server.
651 ;; The request could not be understood by the server due to
652 ;; malformed syntax. The client SHOULD NOT repeat the
653 ;; request without modifications.
656 ;; 500 Internal server error
657 ;; 501 Not implemented
659 ;; 503 Service unavailable
660 ;; 504 Gateway time-out
661 ;; 505 HTTP version not supported
662 ;; 507 Insufficient storage
664 (case url-http-response-status
666 ;; The server does not support the functionality required to
667 ;; fulfill the request.
670 ;; The server, while acting as a gateway or proxy, received
671 ;; an invalid response from the upstream server it accessed
672 ;; in attempting to fulfill the request.
675 ;; The server is currently unable to handle the request due
676 ;; to a temporary overloading or maintenance of the server.
677 ;; The implication is that this is a temporary condition
678 ;; which will be alleviated after some delay. If known, the
679 ;; length of the delay MAY be indicated in a Retry-After
680 ;; header. If no Retry-After is given, the client SHOULD
681 ;; handle the response as it would for a 500 response.
684 ;; The server, while acting as a gateway or proxy, did not
685 ;; receive a timely response from the upstream server
686 ;; specified by the URI (e.g. HTTP, FTP, LDAP) or some other
687 ;; auxiliary server (e.g. DNS) it needed to access in
688 ;; attempting to complete the request.
691 ;; The server does not support, or refuses to support, the
692 ;; HTTP protocol version that was used in the request
696 ;; The method could not be performed on the resource
697 ;; because the server is unable to store the representation
698 ;; needed to successfully complete the request. This
699 ;; condition is considered to be temporary. If the request
700 ;; which received this status code was the result of a user
701 ;; action, the request MUST NOT be repeated until it is
702 ;; requested by a separate user action.
705 (error "Unknown class of HTTP response code: %d (%d)"
706 class url-http-response-status)))
708 (url-mark-buffer-as-dead (current-buffer)))
709 (url-http-debug "Finished parsing HTTP headers: %S" success)
714 (defun url-http-activate-callback ()
715 "Activate callback specified when this buffer was created."
716 (declare (special url-http-process
717 url-callback-function
718 url-callback-arguments))
719 (url-http-mark-connection-as-free (url-host url-current-object)
720 (url-port url-current-object)
722 (url-http-debug "Activating callback in buffer (%s)" (buffer-name))
723 (apply url-callback-function url-callback-arguments))
727 ;; These unfortunately cannot be macros... please ignore them!
728 (defun url-http-idle-sentinel (proc why)
729 "Remove this (now defunct) process PROC from the list of open connections."
730 (maphash (lambda (key val)
732 (puthash key (delq proc val) url-http-open-connections)))
733 url-http-open-connections))
735 (defun url-http-end-of-document-sentinel (proc why)
736 ;; Sentinel used for old HTTP/0.9 or connections we know are going
737 ;; to die as the 'end of document' notifier.
738 (url-http-debug "url-http-end-of-document-sentinel in buffer (%s)"
739 (process-buffer proc))
740 (url-http-idle-sentinel proc why)
741 (with-current-buffer (process-buffer proc)
742 (goto-char (point-min))
743 (if (not (looking-at "HTTP/"))
744 ;; HTTP/0.9 just gets passed back no matter what
745 (url-http-activate-callback)
746 (if (url-http-parse-headers)
747 (url-http-activate-callback)))))
749 (defun url-http-simple-after-change-function (st nd length)
750 ;; Function used when we do NOT know how long the document is going to be
751 ;; Just _very_ simple 'downloaded %d' type of info.
752 (declare (special url-http-end-of-headers))
753 (url-lazy-message "Reading %s..." (url-pretty-length nd)))
755 (defun url-http-content-length-after-change-function (st nd length)
756 "Function used when we DO know how long the document is going to be.
757 More sophisticated percentage downloaded, etc.
758 Also does minimal parsing of HTTP headers and will actually cause
759 the callback to be triggered."
760 (declare (special url-current-object
761 url-http-end-of-headers
762 url-http-content-length
763 url-http-content-type
765 (if url-http-content-type
766 (url-display-percentage
767 "Reading [%s]... %s of %s (%d%%)"
768 (url-percentage (- nd url-http-end-of-headers)
769 url-http-content-length)
770 url-http-content-type
771 (url-pretty-length (- nd url-http-end-of-headers))
772 (url-pretty-length url-http-content-length)
773 (url-percentage (- nd url-http-end-of-headers)
774 url-http-content-length))
775 (url-display-percentage
776 "Reading... %s of %s (%d%%)"
777 (url-percentage (- nd url-http-end-of-headers)
778 url-http-content-length)
779 (url-pretty-length (- nd url-http-end-of-headers))
780 (url-pretty-length url-http-content-length)
781 (url-percentage (- nd url-http-end-of-headers)
782 url-http-content-length)))
784 (if (> (- nd url-http-end-of-headers) url-http-content-length)
786 ;; Found the end of the document! Wheee!
787 (url-display-percentage nil nil)
788 (message "Reading... done.")
789 (if (url-http-parse-headers)
790 (url-http-activate-callback)))))
792 (defun url-http-chunked-encoding-after-change-function (st nd length)
793 "Function used when dealing with 'chunked' encoding.
794 Cannot give a sophisticated percentage, but we need a different
795 function to look for the special 0-length chunk that signifies
796 the end of the document."
797 (declare (special url-current-object
798 url-http-end-of-headers
799 url-http-content-type
800 url-http-chunked-length
801 url-http-chunked-counter
802 url-http-process url-http-chunked-start))
805 (let ((read-next-chunk t)
808 (no-initial-crlf nil))
809 ;; We need to loop thru looking for more chunks even within
810 ;; one after-change-function call.
811 (while read-next-chunk
812 (setq no-initial-crlf (= 0 url-http-chunked-counter))
813 (if url-http-content-type
814 (url-display-percentage nil
815 "Reading [%s]... chunk #%d"
816 url-http-content-type url-http-chunked-counter)
817 (url-display-percentage nil
818 "Reading... chunk #%d"
819 url-http-chunked-counter))
820 (url-http-debug "Reading chunk %d (%d %d %d)"
821 url-http-chunked-counter st nd length)
822 (setq regexp (if no-initial-crlf
823 "\\([0-9a-z]+\\).*\r?\n"
824 "\r?\n\\([0-9a-z]+\\).*\r?\n"))
826 (if url-http-chunked-start
827 ;; We know how long the chunk is supposed to be, skip over
828 ;; leading crap if possible.
829 (if (> nd (+ url-http-chunked-start url-http-chunked-length))
831 (url-http-debug "Got to the end of chunk #%d!"
832 url-http-chunked-counter)
833 (goto-char (+ url-http-chunked-start
834 url-http-chunked-length)))
835 (url-http-debug "Still need %d bytes to hit end of chunk"
836 (- (+ url-http-chunked-start
837 url-http-chunked-length)
839 (setq read-next-chunk nil)))
840 (if (not read-next-chunk)
841 (url-http-debug "Still spinning for next chunk...")
842 (if no-initial-crlf (skip-chars-forward "\r\n"))
843 (if (not (looking-at regexp))
845 ;; Must not have received the entirety of the chunk header,
846 ;; need to spin some more.
847 (url-http-debug "Did not see start of chunk @ %d!" (point))
848 (setq read-next-chunk nil))
849 (add-text-properties (match-beginning 0) (match-end 0)
855 (setq url-http-chunked-length (string-to-number (buffer-substring
859 url-http-chunked-counter (1+ url-http-chunked-counter)
860 url-http-chunked-start (set-marker
861 (or url-http-chunked-start
864 ; (if (not url-http-debug)
865 (delete-region (match-beginning 0) (match-end 0));)
866 (url-http-debug "Saw start of chunk %d (length=%d, start=%d"
867 url-http-chunked-counter url-http-chunked-length
868 (marker-position url-http-chunked-start))
869 (if (= 0 url-http-chunked-length)
871 ;; Found the end of the document! Wheee!
872 (url-http-debug "Saw end of stream chunk!")
873 (setq read-next-chunk nil)
874 (url-display-percentage nil nil)
875 (goto-char (match-end 1))
876 (if (re-search-forward "^\r*$" nil t)
877 (url-http-debug "Saw end of trailers..."))
878 (if (url-http-parse-headers)
879 (url-http-activate-callback))))))))))
881 (defun url-http-wait-for-headers-change-function (st nd length)
882 ;; This will wait for the headers to arrive and then splice in the
883 ;; next appropriate after-change-function, etc.
884 (declare (special url-current-object
885 url-http-end-of-headers
886 url-http-content-type
887 url-http-content-length
888 url-http-transfer-encoding
889 url-callback-function
890 url-callback-arguments
893 url-http-after-change-function
894 url-http-response-status))
895 (url-http-debug "url-http-wait-for-headers-change-function (%s)"
898 (let ((end-of-headers nil)
900 (content-length nil))
901 (goto-char (point-min))
902 (if (not (looking-at "^HTTP/[1-9]\\.[0-9]"))
903 ;; Not HTTP/x.y data, must be 0.9
904 ;; God, I wish this could die.
905 (setq end-of-headers t
906 url-http-end-of-headers 0
908 (if (re-search-forward "^\r*$" nil t)
909 ;; Saw the end of the headers
911 (url-http-debug "Saw end of headers... (%s)" (buffer-name))
912 (setq url-http-end-of-headers (set-marker (make-marker)
915 (url-http-clean-headers))))
917 (if (not end-of-headers)
918 ;; Haven't seen the end of the headers yet, need to wait
919 ;; for more data to arrive.
922 (message "HTTP/0.9 How I hate thee!")
924 (url-http-parse-response)
925 (mail-narrow-to-head)
926 ;;(narrow-to-region (point-min) url-http-end-of-headers)
927 (setq url-http-transfer-encoding (mail-fetch-field
929 url-http-content-type (mail-fetch-field "content-type"))
930 (if (mail-fetch-field "content-length")
931 (setq url-http-content-length
932 (string-to-number (mail-fetch-field "content-length"))))
934 (if url-http-transfer-encoding
935 (setq url-http-transfer-encoding
936 (downcase url-http-transfer-encoding)))
939 ((or (= url-http-response-status 204)
940 (= url-http-response-status 205))
941 (url-http-debug "%d response must have headers only (%s)."
942 url-http-response-status (buffer-name))
943 (if (url-http-parse-headers)
944 (url-http-activate-callback)))
945 ((string= "HEAD" url-http-method)
946 ;; A HEAD request is _ALWAYS_ terminated by the header
947 ;; information, regardless of any entity headers,
948 ;; according to section 4.4 of the HTTP/1.1 draft.
949 (url-http-debug "HEAD request must have headers only (%s)."
951 (if (url-http-parse-headers)
952 (url-http-activate-callback)))
953 ((string= "CONNECT" url-http-method)
954 ;; A CONNECT request is finished, but we cannot stick this
955 ;; back on the free connectin list
956 (url-http-debug "CONNECT request must have headers only.")
957 (if (url-http-parse-headers)
958 (url-http-activate-callback)))
959 ((equal url-http-response-status 304)
960 ;; Only allowed to have a header section. We have to handle
961 ;; this here instead of in url-http-parse-headers because if
962 ;; you have a cached copy of something without a known
963 ;; content-length, and try to retrieve it from the cache, we'd
964 ;; fall into the 'being dumb' section and wait for the
965 ;; connection to terminate, which means we'd wait for 10
966 ;; seconds for the keep-alives to time out on some servers.
967 (if (url-http-parse-headers)
968 (url-http-activate-callback)))
970 ;; HTTP/0.9 always signaled end-of-connection by closing the
973 "Saw HTTP/0.9 response, connection closed means end of document.")
974 (setq url-http-after-change-function
975 'url-http-simple-after-change-function))
976 ((equal url-http-transfer-encoding "chunked")
977 (url-http-debug "Saw chunked encoding.")
978 (setq url-http-after-change-function
979 'url-http-chunked-encoding-after-change-function)
980 (if (> nd url-http-end-of-headers)
983 "Calling initial chunked-encoding for extra data at end of headers")
984 (url-http-chunked-encoding-after-change-function
985 (marker-position url-http-end-of-headers) nd
986 (- nd url-http-end-of-headers)))))
987 ((integerp url-http-content-length)
989 "Got a content-length, being smart about document end.")
990 (setq url-http-after-change-function
991 'url-http-content-length-after-change-function)
993 ((= 0 url-http-content-length)
994 ;; We got a NULL body! Activate the callback
997 "Got 0-length content-length, activating callback immediately.")
998 (if (url-http-parse-headers)
999 (url-http-activate-callback)))
1000 ((> nd url-http-end-of-headers)
1001 ;; Have some leftover data
1002 (url-http-debug "Calling initial content-length for extra data at end of headers")
1003 (url-http-content-length-after-change-function
1004 (marker-position url-http-end-of-headers)
1006 (- nd url-http-end-of-headers)))
1010 (url-http-debug "No content-length, being dumb.")
1011 (setq url-http-after-change-function
1012 'url-http-simple-after-change-function)))))
1013 ;; We are still at the beginning of the buffer... must just be
1014 ;; waiting for a response.
1015 (url-http-debug "Spinning waiting for headers..."))
1016 (goto-char (point-max)))
1019 (defun url-http (url callback cbargs)
1020 "Retrieve URL via HTTP asynchronously.
1021 URL must be a parsed URL. See `url-generic-parse-url' for details.
1022 When retrieval is completed, the function CALLBACK is executed with
1023 CBARGS as the arguments."
1024 (check-type url vector "Need a pre-parsed URL.")
1025 (declare (special url-current-object
1026 url-http-end-of-headers
1027 url-http-content-type
1028 url-http-content-length
1029 url-http-transfer-encoding
1030 url-http-after-change-function
1031 url-callback-function
1032 url-callback-arguments
1034 url-http-extra-headers
1036 url-http-chunked-length
1037 url-http-chunked-start
1038 url-http-chunked-counter
1040 (let ((connection (url-http-find-free-connection (url-host url)
1042 (buffer (generate-new-buffer (format " *http %s:%d*"
1045 (if (not connection)
1046 ;; Failed to open the connection for some reason
1048 (kill-buffer buffer)
1050 (error "Could not create connection to %s:%d" (url-host url)
1052 (with-current-buffer buffer
1053 (mm-disable-multibyte)
1054 (setq url-current-object url
1055 mode-line-format "%b [%s]")
1057 (dolist (var '(url-http-end-of-headers
1058 url-http-content-type
1059 url-http-content-length
1060 url-http-transfer-encoding
1061 url-http-after-change-function
1062 url-http-response-status
1063 url-http-chunked-length
1064 url-http-chunked-counter
1065 url-http-chunked-start
1066 url-callback-function
1067 url-callback-arguments
1070 url-http-extra-headers
1072 url-http-target-url))
1073 (set (make-local-variable var) nil))
1075 (setq url-http-method (or url-request-method "GET")
1076 url-http-extra-headers url-request-extra-headers
1077 url-http-data url-request-data
1078 url-http-process connection
1079 url-http-chunked-length nil
1080 url-http-chunked-start nil
1081 url-http-chunked-counter 0
1082 url-callback-function callback
1083 url-callback-arguments cbargs
1084 url-http-after-change-function 'url-http-wait-for-headers-change-function
1085 url-http-target-url (if (boundp 'proxy-object)
1087 url-current-object))
1089 (set-process-buffer connection buffer)
1090 (set-process-sentinel connection 'url-http-end-of-document-sentinel)
1091 (set-process-filter connection 'url-http-generic-filter)
1092 (process-send-string connection (url-http-create-request url))))
1095 ;; Since Emacs 19/20 does not allow you to change the
1096 ;; `after-change-functions' hook in the midst of running them, we fake
1097 ;; an after change by hooking into the process filter and inserting
1098 ;; the data ourselves. This is slightly less efficient, but there
1099 ;; were tons of weird ways the after-change code was biting us in the
1101 (defun url-http-generic-filter (proc data)
1102 ;; Sometimes we get a zero-length data chunk after the process has
1103 ;; been changed to 'free', which means it has no buffer associated
1104 ;; with it. Do nothing if there is no buffer, or 0 length data.
1105 (declare (special url-http-after-change-function))
1106 (and (process-buffer proc)
1107 (/= (length data) 0)
1108 (with-current-buffer (process-buffer proc)
1109 (url-http-debug "Calling after change function `%s' for `%S'" url-http-after-change-function proc)
1110 (funcall url-http-after-change-function
1113 (goto-char (point-max))
1118 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1119 ;;; file-name-handler stuff from here on out
1120 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1121 (defalias 'url-http-symbol-value-in-buffer
1122 (if (fboundp 'symbol-value-in-buffer)
1123 'symbol-value-in-buffer
1124 (lambda (symbol buffer &optional unbound-value)
1125 "Return the value of SYMBOL in BUFFER, or UNBOUND-VALUE if it is unbound."
1126 (with-current-buffer buffer
1127 (if (not (boundp symbol))
1129 (symbol-value symbol))))))
1131 (defun url-http-head (url)
1132 (let ((url-request-method "HEAD")
1133 (url-request-data nil))
1134 (url-retrieve-synchronously url)))
1137 (defun url-http-file-exists-p (url)
1140 (buffer (url-http-head url)))
1143 (setq status (url-http-symbol-value-in-buffer 'url-http-response-status
1145 exists (and (integerp status)
1146 (>= status 200) (< status 300)))
1147 (kill-buffer buffer))
1151 (defalias 'url-http-file-readable-p 'url-http-file-exists-p)
1153 (defun url-http-head-file-attributes (url &optional id-format)
1154 (let ((buffer (url-http-head url))
1157 (setq attributes (make-list 11 nil))
1158 (setf (nth 1 attributes) 1) ; Number of links to file
1159 (setf (nth 2 attributes) 0) ; file uid
1160 (setf (nth 3 attributes) 0) ; file gid
1161 (setf (nth 7 attributes) ; file size
1162 (url-http-symbol-value-in-buffer 'url-http-content-length
1164 (setf (nth 8 attributes) (eval-when-compile (make-string 10 ?-)))
1165 (kill-buffer buffer))
1169 (defun url-http-file-attributes (url &optional id-format)
1170 (if (url-dav-supported-p url)
1171 (url-dav-file-attributes url id-format)
1172 (url-http-head-file-attributes url id-format)))
1175 (defun url-http-options (url)
1176 "Return a property list describing options available for URL.
1177 This list is retrieved using the `OPTIONS' HTTP method.
1179 Property list members:
1182 A list of symbols specifying what HTTP methods the resource
1186 A list of numbers specifying what DAV protocol/schema versions are
1190 A list of supported DASL search types supported (string form)
1193 A list of the units available for use in partial document fetches.
1196 The `Platform For Privacy Protection' description for the resource.
1197 Currently this is just the raw header contents. This is likely to
1198 change once P3P is formally supported by the URL package or
1200 (let* ((url-request-method "OPTIONS")
1201 (url-request-data nil)
1202 (buffer (url-retrieve-synchronously url))
1205 (when (and buffer (= 2 (/ (url-http-symbol-value-in-buffer
1206 'url-http-response-status buffer 0) 100)))
1207 ;; Only parse the options if we got a 2xx response code!
1208 (with-current-buffer buffer
1211 (mail-narrow-to-head)
1213 ;; Figure out what methods are supported.
1214 (when (setq header (mail-fetch-field "allow"))
1215 (setq options (plist-put
1217 (mapcar 'intern (split-string header "[ ,]+")))))
1220 (when (setq header (mail-fetch-field "dav"))
1221 (setq options (plist-put
1224 (mapcar 'string-to-number
1225 (split-string header "[, ]+"))))))
1228 (when (setq header (mail-fetch-field "dasl"))
1229 (setq options (plist-put
1231 (split-string header "[, ]+"))))
1233 ;; P3P - should get more detailed here. FIXME
1234 (when (setq header (mail-fetch-field "p3p"))
1235 (setq options (plist-put options 'p3p header)))
1237 ;; Check for whether they accept byte-range requests.
1238 (when (setq header (mail-fetch-field "accept-ranges"))
1239 (setq options (plist-put
1243 (split-string header "[, ]+"))))))
1245 (if buffer (kill-buffer buffer))
1250 ;; arch-tag: ba7c59ae-c0f4-4a31-9617-d85f221732ee
1251 ;;; url-http.el ends here