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