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