(Easy Customization, Customization Groups)
[emacs.git] / lisp / url / url-http.el
blob725f4bc1c8a02b5a27f7a723804cd7478432f97f
1 ;;; url-http.el --- HTTP retrieval routines
3 ;; Copyright (C) 1999, 2001, 2004, 2005 Free Software Foundation, Inc.
5 ;; Author: Bill Perry <wmperry@gnu.org>
6 ;; Keywords: comm, data, processes
8 ;; This file is part of GNU Emacs.
9 ;;
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)
13 ;; any later version.
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.
25 ;;; Commentary:
27 ;;; Code:
29 (eval-when-compile (require 'cl))
30 (defvar url-http-extra-headers)
31 (defvar url-http-target-url)
32 (require 'url-gw)
33 (require 'url-util)
34 (require 'url-parse)
35 (require 'url-cookie)
36 (require 'mail-parse)
37 (require 'url-auth)
38 (autoload 'url-retrieve-synchronously "url")
39 (autoload 'url-retrieve "url")
40 (autoload 'url-cache-create-filename "url-cache")
41 (autoload 'url-mark-buffer-as-dead "url")
43 (defconst url-http-default-port 80 "Default HTTP port.")
44 (defconst url-http-asynchronous-p t "HTTP retrievals are asynchronous.")
45 (defalias 'url-http-expand-file-name 'url-default-expander)
47 (defvar url-http-real-basic-auth-storage nil)
48 (defvar url-http-proxy-basic-auth-storage nil)
50 (defvar url-http-open-connections (make-hash-table :test 'equal
51 :size 17)
52 "A hash table of all open network connections.")
54 (defvar url-http-version "1.1"
55 "What version of HTTP we advertise, as a string.
56 Valid values are 1.1 and 1.0.
57 This is only useful when debugging the HTTP subsystem.
59 Setting this to 1.0 will tell servers not to send chunked encoding,
60 and other HTTP/1.1 specific features.
63 (defvar url-http-attempt-keepalives t
64 "Whether to use a single TCP connection multiple times in HTTP.
65 This is only useful when debugging the HTTP subsystem. Setting to
66 `nil' will explicitly close the connection to the server after every
67 request.
70 ;(eval-when-compile
71 ;; These are all macros so that they are hidden from external sight
72 ;; when the file is byte-compiled.
74 ;; This allows us to expose just the entry points we want.
76 ;; These routines will allow us to implement persistent HTTP
77 ;; connections.
78 (defsubst url-http-debug (&rest args)
79 (if quit-flag
80 (let ((proc (get-buffer-process (current-buffer))))
81 ;; The user hit C-g, honor it! Some things can get in an
82 ;; incredibly tight loop (chunked encoding)
83 (if proc
84 (progn
85 (set-process-sentinel proc nil)
86 (set-process-filter proc nil)))
87 (error "Transfer interrupted!")))
88 (apply 'url-debug 'http args))
90 (defun url-http-mark-connection-as-busy (host port proc)
91 (url-http-debug "Marking connection as busy: %s:%d %S" host port proc)
92 (puthash (cons host port)
93 (delq proc (gethash (cons host port) url-http-open-connections))
94 url-http-open-connections)
95 proc)
97 (defun url-http-mark-connection-as-free (host port proc)
98 (url-http-debug "Marking connection as free: %s:%d %S" host port proc)
99 (set-process-buffer proc nil)
100 (set-process-sentinel proc 'url-http-idle-sentinel)
101 (puthash (cons host port)
102 (cons proc (gethash (cons host port) url-http-open-connections))
103 url-http-open-connections)
104 nil)
106 (defun url-http-find-free-connection (host port)
107 (let ((conns (gethash (cons host port) url-http-open-connections))
108 (found nil))
109 (while (and conns (not found))
110 (if (not (memq (process-status (car conns)) '(run open)))
111 (progn
112 (url-http-debug "Cleaning up dead process: %s:%d %S"
113 host port (car conns))
114 (url-http-idle-sentinel (car conns) nil))
115 (setq found (car conns))
116 (url-http-debug "Found existing connection: %s:%d %S" host port found))
117 (pop conns))
118 (if found
119 (url-http-debug "Reusing existing connection: %s:%d" host port)
120 (url-http-debug "Contacting host: %s:%d" host port))
121 (url-lazy-message "Contacting host: %s:%d" host port)
122 (url-http-mark-connection-as-busy host port
123 (or found
124 (url-open-stream host nil host
125 port)))))
127 ;; Building an HTTP request
128 (defun url-http-user-agent-string ()
129 (if (or (eq url-privacy-level 'paranoid)
130 (and (listp url-privacy-level)
131 (memq 'agent url-privacy-level)))
133 (format "User-Agent: %sURL/%s%s\r\n"
134 (if url-package-name
135 (concat url-package-name "/" url-package-version " ")
137 url-version
138 (cond
139 ((and url-os-type url-system-type)
140 (concat " (" url-os-type "; " url-system-type ")"))
141 ((or url-os-type url-system-type)
142 (concat " (" (or url-system-type url-os-type) ")"))
143 (t "")))))
145 (defun url-http-create-request (url &optional ref-url)
146 "Create an HTTP request for URL, referred to by REF-URL."
147 (declare (special proxy-object proxy-info))
148 (let* ((extra-headers)
149 (request nil)
150 (no-cache (cdr-safe (assoc "Pragma" url-request-extra-headers)))
151 (proxy-obj (and (boundp 'proxy-object) proxy-object))
152 (proxy-auth (if (or (cdr-safe (assoc "Proxy-Authorization"
153 url-request-extra-headers))
154 (not proxy-obj))
156 (let ((url-basic-auth-storage
157 'url-http-proxy-basic-auth-storage))
158 (url-get-authentication url nil 'any nil))))
159 (real-fname (url-filename (or proxy-obj url)))
160 (host (url-host (or proxy-obj url)))
161 (auth (if (cdr-safe (assoc "Authorization" url-request-extra-headers))
163 (url-get-authentication (or
164 (and (boundp 'proxy-info)
165 proxy-info)
166 url) nil 'any nil))))
167 (if (equal "" real-fname)
168 (setq real-fname "/"))
169 (setq no-cache (and no-cache (string-match "no-cache" no-cache)))
170 (if auth
171 (setq auth (concat "Authorization: " auth "\r\n")))
172 (if proxy-auth
173 (setq proxy-auth (concat "Proxy-Authorization: " proxy-auth "\r\n")))
175 ;; Protection against stupid values in the referer
176 (if (and ref-url (stringp ref-url) (or (string= ref-url "file:nil")
177 (string= ref-url "")))
178 (setq ref-url nil))
180 ;; We do not want to expose the referer if the user is paranoid.
181 (if (or (memq url-privacy-level '(low high paranoid))
182 (and (listp url-privacy-level)
183 (memq 'lastloc url-privacy-level)))
184 (setq ref-url nil))
186 ;; url-request-extra-headers contains an assoc-list of
187 ;; header/value pairs that we need to put into the request.
188 (setq extra-headers (mapconcat
189 (lambda (x)
190 (concat (car x) ": " (cdr x)))
191 url-request-extra-headers "\r\n"))
192 (if (not (equal extra-headers ""))
193 (setq extra-headers (concat extra-headers "\r\n")))
195 ;; This was done with a call to `format'. Concatting parts has
196 ;; the advantage of keeping the parts of each header together and
197 ;; allows us to elide null lines directly, at the cost of making
198 ;; the layout less clear.
199 (setq request
200 ;; We used to concat directly, but if one of the strings happens
201 ;; to being multibyte (even if it only contains pure ASCII) then
202 ;; every string gets converted with `string-MAKE-multibyte' which
203 ;; turns the 127-255 codes into things like latin-1 accented chars
204 ;; (it would work right if it used `string-TO-multibyte' instead).
205 ;; So to avoid the problem we force every string to be unibyte.
206 (mapconcat
207 ;; FIXME: Instead of `string-AS-unibyte' we'd want
208 ;; `string-to-unibyte', so as to properly signal an error if one
209 ;; of the strings contains a multibyte char.
210 'string-as-unibyte
211 (delq nil
212 (list
213 ;; The request
214 (or url-request-method "GET") " "
215 (if proxy-obj (url-recreate-url proxy-obj) real-fname)
216 " HTTP/" url-http-version "\r\n"
217 ;; Version of MIME we speak
218 "MIME-Version: 1.0\r\n"
219 ;; (maybe) Try to keep the connection open
220 "Connection: " (if (or proxy-obj
221 (not url-http-attempt-keepalives))
222 "close" "keep-alive") "\r\n"
223 ;; HTTP extensions we support
224 (if url-extensions-header
225 (format
226 "Extension: %s\r\n" url-extensions-header))
227 ;; Who we want to talk to
228 (if (/= (url-port (or proxy-obj url))
229 (url-scheme-get-property
230 (url-type (or proxy-obj url)) 'default-port))
231 (format
232 "Host: %s:%d\r\n" host (url-port (or proxy-obj url)))
233 (format "Host: %s\r\n" host))
234 ;; Who its from
235 (if url-personal-mail-address
236 (concat
237 "From: " url-personal-mail-address "\r\n"))
238 ;; Encodings we understand
239 (if url-mime-encoding-string
240 (concat
241 "Accept-encoding: " url-mime-encoding-string "\r\n"))
242 (if url-mime-charset-string
243 (concat
244 "Accept-charset: " url-mime-charset-string "\r\n"))
245 ;; Languages we understand
246 (if url-mime-language-string
247 (concat
248 "Accept-language: " url-mime-language-string "\r\n"))
249 ;; Types we understand
250 "Accept: " (or url-mime-accept-string "*/*") "\r\n"
251 ;; User agent
252 (url-http-user-agent-string)
253 ;; Proxy Authorization
254 proxy-auth
255 ;; Authorization
256 auth
257 ;; Cookies
258 (url-cookie-generate-header-lines host real-fname
259 (equal "https" (url-type url)))
260 ;; If-modified-since
261 (if (and (not no-cache)
262 (member url-request-method '("GET" nil)))
263 (let ((tm (url-is-cached (or proxy-obj url))))
264 (if tm
265 (concat "If-modified-since: "
266 (url-get-normalized-date tm) "\r\n"))))
267 ;; Whence we came
268 (if ref-url (concat
269 "Referer: " ref-url "\r\n"))
270 extra-headers
271 ;; Length of data
272 (if url-request-data
273 (concat
274 "Content-length: " (number-to-string
275 (length url-request-data))
276 "\r\n"))
277 ;; End request
278 "\r\n"
279 ;; Any data
280 url-request-data))
281 ""))
282 (url-http-debug "Request is: \n%s" request)
283 request))
285 ;; Parsing routines
286 (defun url-http-clean-headers ()
287 "Remove trailing \r from header lines.
288 This allows us to use `mail-fetch-field', etc."
289 (declare (special url-http-end-of-headers))
290 (goto-char (point-min))
291 (while (re-search-forward "\r$" url-http-end-of-headers t)
292 (replace-match "")))
294 (defun url-http-handle-authentication (proxy)
295 (declare (special status success url-http-method url-http-data
296 url-callback-function url-callback-arguments))
297 (url-http-debug "Handling %s authentication" (if proxy "proxy" "normal"))
298 (let ((auth (or (mail-fetch-field (if proxy "proxy-authenticate" "www-authenticate"))
299 "basic"))
300 (type nil)
301 (url (url-recreate-url url-current-object))
302 (url-basic-auth-storage 'url-http-real-basic-auth-storage)
305 ;; Cheating, but who cares? :)
306 (if proxy
307 (setq url-basic-auth-storage 'url-http-proxy-basic-auth-storage))
309 (setq auth (url-eat-trailing-space (url-strip-leading-spaces auth)))
310 (if (string-match "[ \t]" auth)
311 (setq type (downcase (substring auth 0 (match-beginning 0))))
312 (setq type (downcase auth)))
314 (if (not (url-auth-registered type))
315 (progn
316 (widen)
317 (goto-char (point-max))
318 (insert "<hr>Sorry, but I do not know how to handle " type
319 " authentication. If you'd like to write it,"
320 " send it to " url-bug-address ".<hr>")
321 (setq status t))
322 (let* ((args (url-parse-args (subst-char-in-string ?, ?\; auth)))
323 (auth (url-get-authentication url (cdr-safe (assoc "realm" args))
324 type t args)))
325 (if (not auth)
326 (setq success t)
327 (push (cons (if proxy "Proxy-Authorization" "Authorization") auth)
328 url-http-extra-headers)
329 (let ((url-request-method url-http-method)
330 (url-request-data url-http-data)
331 (url-request-extra-headers url-http-extra-headers))
332 (url-retrieve url url-callback-function
333 url-callback-arguments)))))))
335 (defun url-http-parse-response ()
336 "Parse just the response code."
337 (declare (special url-http-end-of-headers url-http-response-status))
338 (if (not url-http-end-of-headers)
339 (error "Trying to parse HTTP response code in odd buffer: %s" (buffer-name)))
340 (url-http-debug "url-http-parse-response called in (%s)" (buffer-name))
341 (goto-char (point-min))
342 (skip-chars-forward " \t\n") ; Skip any blank crap
343 (skip-chars-forward "HTTP/") ; Skip HTTP Version
344 (read (current-buffer))
345 (setq url-http-response-status (read (current-buffer))))
347 (defun url-http-handle-cookies ()
348 "Handle all set-cookie / set-cookie2 headers in an HTTP response.
349 The buffer must already be narrowed to the headers, so mail-fetch-field will
350 work correctly."
351 (let ((cookies (mail-fetch-field "Set-Cookie" nil nil t))
352 (cookies2 (mail-fetch-field "Set-Cookie2" nil nil t))
353 (url-current-object url-http-target-url))
354 (and cookies (url-http-debug "Found %d Set-Cookie headers" (length cookies)))
355 (and cookies2 (url-http-debug "Found %d Set-Cookie2 headers" (length cookies2)))
356 (while cookies
357 (url-cookie-handle-set-cookie (pop cookies)))
358 ;;; (while cookies2
359 ;;; (url-cookie-handle-set-cookie2 (pop cookies)))
363 (defun url-http-parse-headers ()
364 "Parse and handle HTTP specific headers.
365 Return t if and only if the current buffer is still active and
366 should be shown to the user."
367 ;; The comments after each status code handled are taken from RFC
368 ;; 2616 (HTTP/1.1)
369 (declare (special url-http-end-of-headers url-http-response-status
370 url-http-method url-http-data url-http-process
371 url-callback-function url-callback-arguments))
373 (url-http-mark-connection-as-free (url-host url-current-object)
374 (url-port url-current-object)
375 url-http-process)
377 (if (or (not (boundp 'url-http-end-of-headers))
378 (not url-http-end-of-headers))
379 (error "Trying to parse headers in odd buffer: %s" (buffer-name)))
380 (goto-char (point-min))
381 (url-http-debug "url-http-parse-headers called in (%s)" (buffer-name))
382 (url-http-parse-response)
383 (mail-narrow-to-head)
384 ;;(narrow-to-region (point-min) url-http-end-of-headers)
385 (let ((class nil)
386 (success nil))
387 (setq class (/ url-http-response-status 100))
388 (url-http-debug "Parsed HTTP headers: class=%d status=%d" class url-http-response-status)
389 (url-http-handle-cookies)
391 (case class
392 ;; Classes of response codes
394 ;; 5xx = Server Error
395 ;; 4xx = Client Error
396 ;; 3xx = Redirection
397 ;; 2xx = Successful
398 ;; 1xx = Informational
399 (1 ; Information messages
400 ;; 100 = Continue with request
401 ;; 101 = Switching protocols
402 ;; 102 = Processing (Added by DAV)
403 (url-mark-buffer-as-dead (current-buffer))
404 (error "HTTP responses in class 1xx not supported (%d)" url-http-response-status))
405 (2 ; Success
406 ;; 200 Ok
407 ;; 201 Created
408 ;; 202 Accepted
409 ;; 203 Non-authoritative information
410 ;; 204 No content
411 ;; 205 Reset content
412 ;; 206 Partial content
413 ;; 207 Multi-status (Added by DAV)
414 (case url-http-response-status
415 ((204 205)
416 ;; No new data, just stay at the same document
417 (url-mark-buffer-as-dead (current-buffer))
418 (setq success t))
419 (otherwise
420 ;; Generic success for all others. Store in the cache, and
421 ;; mark it as successful.
422 (widen)
423 (if (and url-automatic-caching (equal url-http-method "GET"))
424 (url-store-in-cache (current-buffer)))
425 (setq success t))))
426 (3 ; Redirection
427 ;; 300 Multiple choices
428 ;; 301 Moved permanently
429 ;; 302 Found
430 ;; 303 See other
431 ;; 304 Not modified
432 ;; 305 Use proxy
433 ;; 307 Temporary redirect
434 (let ((redirect-uri (or (mail-fetch-field "Location")
435 (mail-fetch-field "URI"))))
436 (case url-http-response-status
437 (300
438 ;; Quoth the spec (section 10.3.1)
439 ;; -------------------------------
440 ;; The requested resource corresponds to any one of a set of
441 ;; representations, each with its own specific location and
442 ;; agent-driven negotiation information is being provided so
443 ;; that the user can select a preferred representation and
444 ;; redirect its request to that location.
445 ;; [...]
446 ;; If the server has a preferred choice of representation, it
447 ;; SHOULD include the specific URI for that representation in
448 ;; the Location field; user agents MAY use the Location field
449 ;; value for automatic redirection.
450 ;; -------------------------------
451 ;; We do not support agent-driven negotiation, so we just
452 ;; redirect to the preferred URI if one is provided.
453 nil)
454 ((301 302 307)
455 ;; If the 301|302 status code is received in response to a
456 ;; request other than GET or HEAD, the user agent MUST NOT
457 ;; automatically redirect the request unless it can be
458 ;; confirmed by the user, since this might change the
459 ;; conditions under which the request was issued.
460 (if (member url-http-method '("HEAD" "GET"))
461 ;; Automatic redirection is ok
463 ;; It is just too big of a pain in the ass to get this
464 ;; prompt all the time. We will just silently lose our
465 ;; data and convert to a GET method.
466 (url-http-debug "Converting `%s' request to `GET' because of REDIRECT(%d)"
467 url-http-method url-http-response-status)
468 (setq url-http-method "GET"
469 url-http-data nil)))
470 (303
471 ;; The response to the request can be found under a different
472 ;; URI and SHOULD be retrieved using a GET method on that
473 ;; resource.
474 (setq url-http-method "GET"
475 url-http-data nil))
476 (304
477 ;; The 304 response MUST NOT contain a message-body.
478 (url-http-debug "Extracting document from cache... (%s)"
479 (url-cache-create-filename (url-view-url t)))
480 (url-cache-extract (url-cache-create-filename (url-view-url t)))
481 (setq redirect-uri nil
482 success t))
483 (305
484 ;; The requested resource MUST be accessed through the
485 ;; proxy given by the Location field. The Location field
486 ;; gives the URI of the proxy. The recipient is expected
487 ;; to repeat this single request via the proxy. 305
488 ;; responses MUST only be generated by origin servers.
489 (error "Redirection thru a proxy server not supported: %s"
490 redirect-uri))
491 (otherwise
492 ;; Treat everything like '300'
493 nil))
494 (when redirect-uri
495 ;; Clean off any whitespace and/or <...> cruft.
496 (if (string-match "\\([^ \t]+\\)[ \t]" redirect-uri)
497 (setq redirect-uri (match-string 1 redirect-uri)))
498 (if (string-match "^<\\(.*\\)>$" redirect-uri)
499 (setq redirect-uri (match-string 1 redirect-uri)))
501 ;; Some stupid sites (like sourceforge) send a
502 ;; non-fully-qualified URL (ie: /), which royally confuses
503 ;; the URL library.
504 (if (not (string-match url-nonrelative-link redirect-uri))
505 ;; Be careful to use the real target URL, otherwise we may
506 ;; compute the redirection relative to the URL of the proxy.
507 (setq redirect-uri
508 (url-expand-file-name redirect-uri url-http-target-url)))
509 (let ((url-request-method url-http-method)
510 (url-request-data url-http-data)
511 (url-request-extra-headers url-http-extra-headers))
512 (url-retrieve redirect-uri url-callback-function
513 (cons :redirect
514 (cons redirect-uri
515 url-callback-arguments)))
516 (url-mark-buffer-as-dead (current-buffer))))))
517 (4 ; Client error
518 ;; 400 Bad Request
519 ;; 401 Unauthorized
520 ;; 402 Payment required
521 ;; 403 Forbidden
522 ;; 404 Not found
523 ;; 405 Method not allowed
524 ;; 406 Not acceptable
525 ;; 407 Proxy authentication required
526 ;; 408 Request time-out
527 ;; 409 Conflict
528 ;; 410 Gone
529 ;; 411 Length required
530 ;; 412 Precondition failed
531 ;; 413 Request entity too large
532 ;; 414 Request-URI too large
533 ;; 415 Unsupported media type
534 ;; 416 Requested range not satisfiable
535 ;; 417 Expectation failed
536 ;; 422 Unprocessable Entity (Added by DAV)
537 ;; 423 Locked
538 ;; 424 Failed Dependency
539 (case url-http-response-status
540 (401
541 ;; The request requires user authentication. The response
542 ;; MUST include a WWW-Authenticate header field containing a
543 ;; challenge applicable to the requested resource. The
544 ;; client MAY repeat the request with a suitable
545 ;; Authorization header field.
546 (url-http-handle-authentication nil))
547 (402
548 ;; This code is reserved for future use
549 (url-mark-buffer-as-dead (current-buffer))
550 (error "Somebody wants you to give them money"))
551 (403
552 ;; The server understood the request, but is refusing to
553 ;; fulfill it. Authorization will not help and the request
554 ;; SHOULD NOT be repeated.
555 (setq success t))
556 (404
557 ;; Not found
558 (setq success t))
559 (405
560 ;; The method specified in the Request-Line is not allowed
561 ;; for the resource identified by the Request-URI. The
562 ;; response MUST include an Allow header containing a list of
563 ;; valid methods for the requested resource.
564 (setq success t))
565 (406
566 ;; The resource identified by the request is only capable of
567 ;; generating response entities which have content
568 ;; characteristics nota cceptable according to the accept
569 ;; headers sent in the request.
570 (setq success t))
571 (407
572 ;; This code is similar to 401 (Unauthorized), but indicates
573 ;; that the client must first authenticate itself with the
574 ;; proxy. The proxy MUST return a Proxy-Authenticate header
575 ;; field containing a challenge applicable to the proxy for
576 ;; the requested resource.
577 (url-http-handle-authentication t))
578 (408
579 ;; The client did not produce a request within the time that
580 ;; the server was prepared to wait. The client MAY repeat
581 ;; the request without modifications at any later time.
582 (setq success t))
583 (409
584 ;; The request could not be completed due to a conflict with
585 ;; the current state of the resource. This code is only
586 ;; allowed in situations where it is expected that the user
587 ;; mioght be able to resolve the conflict and resubmit the
588 ;; request. The response body SHOULD include enough
589 ;; information for the user to recognize the source of the
590 ;; conflict.
591 (setq success t))
592 (410
593 ;; The requested resource is no longer available at the
594 ;; server and no forwarding address is known.
595 (setq success t))
596 (411
597 ;; The server refuses to accept the request without a defined
598 ;; Content-Length. The client MAY repeat the request if it
599 ;; adds a valid Content-Length header field containing the
600 ;; length of the message-body in the request message.
602 ;; NOTE - this will never happen because
603 ;; `url-http-create-request' automatically calculates the
604 ;; content-length.
605 (setq success t))
606 (412
607 ;; The precondition given in one or more of the
608 ;; request-header fields evaluated to false when it was
609 ;; tested on the server.
610 (setq success t))
611 ((413 414)
612 ;; The server is refusing to process a request because the
613 ;; request entity|URI is larger than the server is willing or
614 ;; able to process.
615 (setq success t))
616 (415
617 ;; The server is refusing to service the request because the
618 ;; entity of the request is in a format not supported by the
619 ;; requested resource for the requested method.
620 (setq success t))
621 (416
622 ;; A server SHOULD return a response with this status code if
623 ;; a request included a Range request-header field, and none
624 ;; of the range-specifier values in this field overlap the
625 ;; current extent of the selected resource, and the request
626 ;; did not include an If-Range request-header field.
627 (setq success t))
628 (417
629 ;; The expectation given in an Expect request-header field
630 ;; could not be met by this server, or, if the server is a
631 ;; proxy, the server has unambiguous evidence that the
632 ;; request could not be met by the next-hop server.
633 (setq success t))
634 (otherwise
635 ;; The request could not be understood by the server due to
636 ;; malformed syntax. The client SHOULD NOT repeat the
637 ;; request without modifications.
638 (setq success t))))
640 ;; 500 Internal server error
641 ;; 501 Not implemented
642 ;; 502 Bad gateway
643 ;; 503 Service unavailable
644 ;; 504 Gateway time-out
645 ;; 505 HTTP version not supported
646 ;; 507 Insufficient storage
647 (setq success t)
648 (case url-http-response-status
649 (501
650 ;; The server does not support the functionality required to
651 ;; fulfill the request.
652 nil)
653 (502
654 ;; The server, while acting as a gateway or proxy, received
655 ;; an invalid response from the upstream server it accessed
656 ;; in attempting to fulfill the request.
657 nil)
658 (503
659 ;; The server is currently unable to handle the request due
660 ;; to a temporary overloading or maintenance of the server.
661 ;; The implication is that this is a temporary condition
662 ;; which will be alleviated after some delay. If known, the
663 ;; length of the delay MAY be indicated in a Retry-After
664 ;; header. If no Retry-After is given, the client SHOULD
665 ;; handle the response as it would for a 500 response.
666 nil)
667 (504
668 ;; The server, while acting as a gateway or proxy, did not
669 ;; receive a timely response from the upstream server
670 ;; specified by the URI (e.g. HTTP, FTP, LDAP) or some other
671 ;; auxiliary server (e.g. DNS) it needed to access in
672 ;; attempting to complete the request.
673 nil)
674 (505
675 ;; The server does not support, or refuses to support, the
676 ;; HTTP protocol version that was used in the request
677 ;; message.
678 nil)
679 (507 ; DAV
680 ;; The method could not be performed on the resource
681 ;; because the server is unable to store the representation
682 ;; needed to successfully complete the request. This
683 ;; condition is considered to be temporary. If the request
684 ;; which received this status code was the result of a user
685 ;; action, the request MUST NOT be repeated until it is
686 ;; requested by a separate user action.
687 nil)))
688 (otherwise
689 (error "Unknown class of HTTP response code: %d (%d)"
690 class url-http-response-status)))
691 (if (not success)
692 (url-mark-buffer-as-dead (current-buffer)))
693 (url-http-debug "Finished parsing HTTP headers: %S" success)
694 (widen)
695 success))
697 ;; Miscellaneous
698 (defun url-http-activate-callback ()
699 "Activate callback specified when this buffer was created."
700 (declare (special url-http-process
701 url-callback-function
702 url-callback-arguments))
703 (url-http-mark-connection-as-free (url-host url-current-object)
704 (url-port url-current-object)
705 url-http-process)
706 (url-http-debug "Activating callback in buffer (%s)" (buffer-name))
707 (apply url-callback-function url-callback-arguments))
709 ;; )
711 ;; These unfortunately cannot be macros... please ignore them!
712 (defun url-http-idle-sentinel (proc why)
713 "Remove this (now defunct) process PROC from the list of open connections."
714 (maphash (lambda (key val)
715 (if (memq proc val)
716 (puthash key (delq proc val) url-http-open-connections)))
717 url-http-open-connections))
719 (defun url-http-end-of-document-sentinel (proc why)
720 ;; Sentinel used for old HTTP/0.9 or connections we know are going
721 ;; to die as the 'end of document' notifier.
722 (url-http-debug "url-http-end-of-document-sentinel in buffer (%s)"
723 (process-buffer proc))
724 (url-http-idle-sentinel proc why)
725 (with-current-buffer (process-buffer proc)
726 (goto-char (point-min))
727 (if (not (looking-at "HTTP/"))
728 ;; HTTP/0.9 just gets passed back no matter what
729 (url-http-activate-callback)
730 (if (url-http-parse-headers)
731 (url-http-activate-callback)))))
733 (defun url-http-simple-after-change-function (st nd length)
734 ;; Function used when we do NOT know how long the document is going to be
735 ;; Just _very_ simple 'downloaded %d' type of info.
736 (declare (special url-http-end-of-headers))
737 (url-lazy-message "Reading %s..." (url-pretty-length nd)))
739 (defun url-http-content-length-after-change-function (st nd length)
740 "Function used when we DO know how long the document is going to be.
741 More sophisticated percentage downloaded, etc.
742 Also does minimal parsing of HTTP headers and will actually cause
743 the callback to be triggered."
744 (declare (special url-current-object
745 url-http-end-of-headers
746 url-http-content-length
747 url-http-content-type
748 url-http-process))
749 (if url-http-content-type
750 (url-display-percentage
751 "Reading [%s]... %s of %s (%d%%)"
752 (url-percentage (- nd url-http-end-of-headers)
753 url-http-content-length)
754 url-http-content-type
755 (url-pretty-length (- nd url-http-end-of-headers))
756 (url-pretty-length url-http-content-length)
757 (url-percentage (- nd url-http-end-of-headers)
758 url-http-content-length))
759 (url-display-percentage
760 "Reading... %s of %s (%d%%)"
761 (url-percentage (- nd url-http-end-of-headers)
762 url-http-content-length)
763 (url-pretty-length (- nd url-http-end-of-headers))
764 (url-pretty-length url-http-content-length)
765 (url-percentage (- nd url-http-end-of-headers)
766 url-http-content-length)))
768 (if (> (- nd url-http-end-of-headers) url-http-content-length)
769 (progn
770 ;; Found the end of the document! Wheee!
771 (url-display-percentage nil nil)
772 (message "Reading... done.")
773 (if (url-http-parse-headers)
774 (url-http-activate-callback)))))
776 (defun url-http-chunked-encoding-after-change-function (st nd length)
777 "Function used when dealing with 'chunked' encoding.
778 Cannot give a sophisticated percentage, but we need a different
779 function to look for the special 0-length chunk that signifies
780 the end of the document."
781 (declare (special url-current-object
782 url-http-end-of-headers
783 url-http-content-type
784 url-http-chunked-length
785 url-http-chunked-counter
786 url-http-process url-http-chunked-start))
787 (save-excursion
788 (goto-char st)
789 (let ((read-next-chunk t)
790 (case-fold-search t)
791 (regexp nil)
792 (no-initial-crlf nil))
793 ;; We need to loop thru looking for more chunks even within
794 ;; one after-change-function call.
795 (while read-next-chunk
796 (setq no-initial-crlf (= 0 url-http-chunked-counter))
797 (if url-http-content-type
798 (url-display-percentage nil
799 "Reading [%s]... chunk #%d"
800 url-http-content-type url-http-chunked-counter)
801 (url-display-percentage nil
802 "Reading... chunk #%d"
803 url-http-chunked-counter))
804 (url-http-debug "Reading chunk %d (%d %d %d)"
805 url-http-chunked-counter st nd length)
806 (setq regexp (if no-initial-crlf
807 "\\([0-9a-z]+\\).*\r?\n"
808 "\r?\n\\([0-9a-z]+\\).*\r?\n"))
810 (if url-http-chunked-start
811 ;; We know how long the chunk is supposed to be, skip over
812 ;; leading crap if possible.
813 (if (> nd (+ url-http-chunked-start url-http-chunked-length))
814 (progn
815 (url-http-debug "Got to the end of chunk #%d!"
816 url-http-chunked-counter)
817 (goto-char (+ url-http-chunked-start
818 url-http-chunked-length)))
819 (url-http-debug "Still need %d bytes to hit end of chunk"
820 (- (+ url-http-chunked-start
821 url-http-chunked-length)
822 nd))
823 (setq read-next-chunk nil)))
824 (if (not read-next-chunk)
825 (url-http-debug "Still spinning for next chunk...")
826 (if no-initial-crlf (skip-chars-forward "\r\n"))
827 (if (not (looking-at regexp))
828 (progn
829 ;; Must not have received the entirety of the chunk header,
830 ;; need to spin some more.
831 (url-http-debug "Did not see start of chunk @ %d!" (point))
832 (setq read-next-chunk nil))
833 (add-text-properties (match-beginning 0) (match-end 0)
834 (list 'start-open t
835 'end-open t
836 'chunked-encoding t
837 'face 'cursor
838 'invisible t))
839 (setq url-http-chunked-length (string-to-number (buffer-substring
840 (match-beginning 1)
841 (match-end 1))
843 url-http-chunked-counter (1+ url-http-chunked-counter)
844 url-http-chunked-start (set-marker
845 (or url-http-chunked-start
846 (make-marker))
847 (match-end 0)))
848 ; (if (not url-http-debug)
849 (delete-region (match-beginning 0) (match-end 0));)
850 (url-http-debug "Saw start of chunk %d (length=%d, start=%d"
851 url-http-chunked-counter url-http-chunked-length
852 (marker-position url-http-chunked-start))
853 (if (= 0 url-http-chunked-length)
854 (progn
855 ;; Found the end of the document! Wheee!
856 (url-http-debug "Saw end of stream chunk!")
857 (setq read-next-chunk nil)
858 (url-display-percentage nil nil)
859 (goto-char (match-end 1))
860 (if (re-search-forward "^\r*$" nil t)
861 (url-http-debug "Saw end of trailers..."))
862 (if (url-http-parse-headers)
863 (url-http-activate-callback))))))))))
865 (defun url-http-wait-for-headers-change-function (st nd length)
866 ;; This will wait for the headers to arrive and then splice in the
867 ;; next appropriate after-change-function, etc.
868 (declare (special url-current-object
869 url-http-end-of-headers
870 url-http-content-type
871 url-http-content-length
872 url-http-transfer-encoding
873 url-callback-function
874 url-callback-arguments
875 url-http-process
876 url-http-method
877 url-http-after-change-function
878 url-http-response-status))
879 (url-http-debug "url-http-wait-for-headers-change-function (%s)"
880 (buffer-name))
881 (if (not (bobp))
882 (let ((end-of-headers nil)
883 (old-http nil)
884 (content-length nil))
885 (goto-char (point-min))
886 (if (not (looking-at "^HTTP/[1-9]\\.[0-9]"))
887 ;; Not HTTP/x.y data, must be 0.9
888 ;; God, I wish this could die.
889 (setq end-of-headers t
890 url-http-end-of-headers 0
891 old-http t)
892 (if (re-search-forward "^\r*$" nil t)
893 ;; Saw the end of the headers
894 (progn
895 (url-http-debug "Saw end of headers... (%s)" (buffer-name))
896 (setq url-http-end-of-headers (set-marker (make-marker)
897 (point))
898 end-of-headers t)
899 (url-http-clean-headers))))
901 (if (not end-of-headers)
902 ;; Haven't seen the end of the headers yet, need to wait
903 ;; for more data to arrive.
905 (if old-http
906 (message "HTTP/0.9 How I hate thee!")
907 (progn
908 (url-http-parse-response)
909 (mail-narrow-to-head)
910 ;;(narrow-to-region (point-min) url-http-end-of-headers)
911 (setq url-http-transfer-encoding (mail-fetch-field
912 "transfer-encoding")
913 url-http-content-type (mail-fetch-field "content-type"))
914 (if (mail-fetch-field "content-length")
915 (setq url-http-content-length
916 (string-to-number (mail-fetch-field "content-length"))))
917 (widen)))
918 (if url-http-transfer-encoding
919 (setq url-http-transfer-encoding
920 (downcase url-http-transfer-encoding)))
922 (cond
923 ((or (= url-http-response-status 204)
924 (= url-http-response-status 205))
925 (url-http-debug "%d response must have headers only (%s)."
926 url-http-response-status (buffer-name))
927 (if (url-http-parse-headers)
928 (url-http-activate-callback)))
929 ((string= "HEAD" url-http-method)
930 ;; A HEAD request is _ALWAYS_ terminated by the header
931 ;; information, regardless of any entity headers,
932 ;; according to section 4.4 of the HTTP/1.1 draft.
933 (url-http-debug "HEAD request must have headers only (%s)."
934 (buffer-name))
935 (if (url-http-parse-headers)
936 (url-http-activate-callback)))
937 ((string= "CONNECT" url-http-method)
938 ;; A CONNECT request is finished, but we cannot stick this
939 ;; back on the free connectin list
940 (url-http-debug "CONNECT request must have headers only.")
941 (if (url-http-parse-headers)
942 (url-http-activate-callback)))
943 ((equal url-http-response-status 304)
944 ;; Only allowed to have a header section. We have to handle
945 ;; this here instead of in url-http-parse-headers because if
946 ;; you have a cached copy of something without a known
947 ;; content-length, and try to retrieve it from the cache, we'd
948 ;; fall into the 'being dumb' section and wait for the
949 ;; connection to terminate, which means we'd wait for 10
950 ;; seconds for the keep-alives to time out on some servers.
951 (if (url-http-parse-headers)
952 (url-http-activate-callback)))
953 (old-http
954 ;; HTTP/0.9 always signaled end-of-connection by closing the
955 ;; connection.
956 (url-http-debug
957 "Saw HTTP/0.9 response, connection closed means end of document.")
958 (setq url-http-after-change-function
959 'url-http-simple-after-change-function))
960 ((equal url-http-transfer-encoding "chunked")
961 (url-http-debug "Saw chunked encoding.")
962 (setq url-http-after-change-function
963 'url-http-chunked-encoding-after-change-function)
964 (if (> nd url-http-end-of-headers)
965 (progn
966 (url-http-debug
967 "Calling initial chunked-encoding for extra data at end of headers")
968 (url-http-chunked-encoding-after-change-function
969 (marker-position url-http-end-of-headers) nd
970 (- nd url-http-end-of-headers)))))
971 ((integerp url-http-content-length)
972 (url-http-debug
973 "Got a content-length, being smart about document end.")
974 (setq url-http-after-change-function
975 'url-http-content-length-after-change-function)
976 (cond
977 ((= 0 url-http-content-length)
978 ;; We got a NULL body! Activate the callback
979 ;; immediately!
980 (url-http-debug
981 "Got 0-length content-length, activating callback immediately.")
982 (if (url-http-parse-headers)
983 (url-http-activate-callback)))
984 ((> nd url-http-end-of-headers)
985 ;; Have some leftover data
986 (url-http-debug "Calling initial content-length for extra data at end of headers")
987 (url-http-content-length-after-change-function
988 (marker-position url-http-end-of-headers)
990 (- nd url-http-end-of-headers)))
992 nil)))
994 (url-http-debug "No content-length, being dumb.")
995 (setq url-http-after-change-function
996 'url-http-simple-after-change-function)))))
997 ;; We are still at the beginning of the buffer... must just be
998 ;; waiting for a response.
999 (url-http-debug "Spinning waiting for headers..."))
1000 (goto-char (point-max)))
1002 ;;;###autoload
1003 (defun url-http (url callback cbargs)
1004 "Retrieve URL via HTTP asynchronously.
1005 URL must be a parsed URL. See `url-generic-parse-url' for details.
1006 When retrieval is completed, the function CALLBACK is executed with
1007 CBARGS as the arguments."
1008 (check-type url vector "Need a pre-parsed URL.")
1009 (declare (special url-current-object
1010 url-http-end-of-headers
1011 url-http-content-type
1012 url-http-content-length
1013 url-http-transfer-encoding
1014 url-http-after-change-function
1015 url-callback-function
1016 url-callback-arguments
1017 url-http-method
1018 url-http-extra-headers
1019 url-http-data
1020 url-http-chunked-length
1021 url-http-chunked-start
1022 url-http-chunked-counter
1023 url-http-process))
1024 (let ((connection (url-http-find-free-connection (url-host url)
1025 (url-port url)))
1026 (buffer (generate-new-buffer (format " *http %s:%d*"
1027 (url-host url)
1028 (url-port url)))))
1029 (if (not connection)
1030 ;; Failed to open the connection for some reason
1031 (progn
1032 (kill-buffer buffer)
1033 (setq buffer nil)
1034 (error "Could not create connection to %s:%d" (url-host url)
1035 (url-port url)))
1036 (with-current-buffer buffer
1037 (mm-disable-multibyte)
1038 (setq url-current-object url
1039 mode-line-format "%b [%s]")
1041 (dolist (var '(url-http-end-of-headers
1042 url-http-content-type
1043 url-http-content-length
1044 url-http-transfer-encoding
1045 url-http-after-change-function
1046 url-http-response-status
1047 url-http-chunked-length
1048 url-http-chunked-counter
1049 url-http-chunked-start
1050 url-callback-function
1051 url-callback-arguments
1052 url-http-process
1053 url-http-method
1054 url-http-extra-headers
1055 url-http-data
1056 url-http-target-url))
1057 (set (make-local-variable var) nil))
1059 (setq url-http-method (or url-request-method "GET")
1060 url-http-extra-headers url-request-extra-headers
1061 url-http-data url-request-data
1062 url-http-process connection
1063 url-http-chunked-length nil
1064 url-http-chunked-start nil
1065 url-http-chunked-counter 0
1066 url-callback-function callback
1067 url-callback-arguments cbargs
1068 url-http-after-change-function 'url-http-wait-for-headers-change-function
1069 url-http-target-url (if (boundp 'proxy-object)
1070 proxy-object
1071 url-current-object))
1073 (set-process-buffer connection buffer)
1074 (set-process-sentinel connection 'url-http-end-of-document-sentinel)
1075 (set-process-filter connection 'url-http-generic-filter)
1076 (process-send-string connection (url-http-create-request url))))
1077 buffer))
1079 ;; Since Emacs 19/20 does not allow you to change the
1080 ;; `after-change-functions' hook in the midst of running them, we fake
1081 ;; an after change by hooking into the process filter and inserting
1082 ;; the data ourselves. This is slightly less efficient, but there
1083 ;; were tons of weird ways the after-change code was biting us in the
1084 ;; shorts.
1085 (defun url-http-generic-filter (proc data)
1086 ;; Sometimes we get a zero-length data chunk after the process has
1087 ;; been changed to 'free', which means it has no buffer associated
1088 ;; with it. Do nothing if there is no buffer, or 0 length data.
1089 (declare (special url-http-after-change-function))
1090 (and (process-buffer proc)
1091 (/= (length data) 0)
1092 (with-current-buffer (process-buffer proc)
1093 (url-http-debug "Calling after change function `%s' for `%S'" url-http-after-change-function proc)
1094 (funcall url-http-after-change-function
1095 (point-max)
1096 (progn
1097 (goto-char (point-max))
1098 (insert data)
1099 (point-max))
1100 (length data)))))
1102 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1103 ;;; file-name-handler stuff from here on out
1104 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1105 (if (not (fboundp 'symbol-value-in-buffer))
1106 (defun url-http-symbol-value-in-buffer (symbol buffer
1107 &optional unbound-value)
1108 "Return the value of SYMBOL in BUFFER, or UNBOUND-VALUE if it is unbound."
1109 (with-current-buffer buffer
1110 (if (not (boundp symbol))
1111 unbound-value
1112 (symbol-value symbol))))
1113 (defalias 'url-http-symbol-value-in-buffer 'symbol-value-in-buffer))
1115 (defun url-http-head (url)
1116 (let ((url-request-method "HEAD")
1117 (url-request-data nil))
1118 (url-retrieve-synchronously url)))
1120 ;;;###autoload
1121 (defun url-http-file-exists-p (url)
1122 (let ((status nil)
1123 (exists nil)
1124 (buffer (url-http-head url)))
1125 (if (not buffer)
1126 (setq exists nil)
1127 (setq status (url-http-symbol-value-in-buffer 'url-http-response-status
1128 buffer 500)
1129 exists (and (>= status 200) (< status 300)))
1130 (kill-buffer buffer))
1131 exists))
1133 ;;;###autoload
1134 (defalias 'url-http-file-readable-p 'url-http-file-exists-p)
1136 (defun url-http-head-file-attributes (url &optional id-format)
1137 (let ((buffer (url-http-head url))
1138 (attributes nil))
1139 (when buffer
1140 (setq attributes (make-list 11 nil))
1141 (setf (nth 1 attributes) 1) ; Number of links to file
1142 (setf (nth 2 attributes) 0) ; file uid
1143 (setf (nth 3 attributes) 0) ; file gid
1144 (setf (nth 7 attributes) ; file size
1145 (url-http-symbol-value-in-buffer 'url-http-content-length
1146 buffer -1))
1147 (setf (nth 8 attributes) (eval-when-compile (make-string 10 ?-)))
1148 (kill-buffer buffer))
1149 attributes))
1151 ;;;###autoload
1152 (defun url-http-file-attributes (url &optional id-format)
1153 (if (url-dav-supported-p url)
1154 (url-dav-file-attributes url id-format)
1155 (url-http-head-file-attributes url id-format)))
1157 ;;;###autoload
1158 (defun url-http-options (url)
1159 "Returns a property list describing options available for URL.
1160 This list is retrieved using the `OPTIONS' HTTP method.
1162 Property list members:
1164 methods
1165 A list of symbols specifying what HTTP methods the resource
1166 supports.
1169 A list of numbers specifying what DAV protocol/schema versions are
1170 supported.
1172 dasl
1173 A list of supported DASL search types supported (string form)
1175 ranges
1176 A list of the units available for use in partial document fetches.
1179 The `Platform For Privacy Protection' description for the resource.
1180 Currently this is just the raw header contents. This is likely to
1181 change once P3P is formally supported by the URL package or
1182 Emacs/W3.
1184 (let* ((url-request-method "OPTIONS")
1185 (url-request-data nil)
1186 (buffer (url-retrieve-synchronously url))
1187 (header nil)
1188 (options nil))
1189 (when (and buffer (= 2 (/ (url-http-symbol-value-in-buffer
1190 'url-http-response-status buffer 0) 100)))
1191 ;; Only parse the options if we got a 2xx response code!
1192 (with-current-buffer buffer
1193 (save-restriction
1194 (save-match-data
1195 (mail-narrow-to-head)
1197 ;; Figure out what methods are supported.
1198 (when (setq header (mail-fetch-field "allow"))
1199 (setq options (plist-put
1200 options 'methods
1201 (mapcar 'intern (split-string header "[ ,]+")))))
1203 ;; Check for DAV
1204 (when (setq header (mail-fetch-field "dav"))
1205 (setq options (plist-put
1206 options 'dav
1207 (delq 0
1208 (mapcar 'string-to-number
1209 (split-string header "[, ]+"))))))
1211 ;; Now for DASL
1212 (when (setq header (mail-fetch-field "dasl"))
1213 (setq options (plist-put
1214 options 'dasl
1215 (split-string header "[, ]+"))))
1217 ;; P3P - should get more detailed here. FIXME
1218 (when (setq header (mail-fetch-field "p3p"))
1219 (setq options (plist-put options 'p3p header)))
1221 ;; Check for whether they accept byte-range requests.
1222 (when (setq header (mail-fetch-field "accept-ranges"))
1223 (setq options (plist-put
1224 options 'ranges
1225 (delq 'none
1226 (mapcar 'intern
1227 (split-string header "[, ]+"))))))
1228 ))))
1229 (if buffer (kill-buffer buffer))
1230 options))
1232 (provide 'url-http)
1234 ;; arch-tag: ba7c59ae-c0f4-4a31-9617-d85f221732ee
1235 ;;; url-http.el ends here