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