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