1 ;;; url-http.el --- HTTP retrieval routines
3 ;; Copyright (C) 1999, 2001, 2004-2014 Free Software Foundation, Inc.
5 ;; Author: Bill Perry <wmperry@gnu.org>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: comm, data, processes
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
32 (defvar url-callback-arguments)
33 (defvar url-callback-function)
34 (defvar url-current-object)
35 (defvar url-http-after-change-function)
36 (defvar url-http-chunked-counter)
37 (defvar url-http-chunked-length)
38 (defvar url-http-chunked-start)
39 (defvar url-http-connection-opened)
40 (defvar url-http-content-length)
41 (defvar url-http-content-type)
42 (defvar url-http-data)
43 (defvar url-http-end-of-headers)
44 (defvar url-http-extra-headers)
45 (defvar url-http-noninteractive)
46 (defvar url-http-method)
47 (defvar url-http-no-retry)
48 (defvar url-http-process)
49 (defvar url-http-proxy)
50 (defvar url-http-response-status)
51 (defvar url-http-response-version)
52 (defvar url-http-target-url)
53 (defvar url-http-transfer-encoding)
54 (defvar url-show-status)
62 (autoload 'url-cache-create-filename "url-cache")
64 (defconst url-http-default-port 80 "Default HTTP port.")
65 (defconst url-http-asynchronous-p t "HTTP retrievals are asynchronous.")
66 (defalias 'url-http-expand-file-name 'url-default-expander)
68 (defvar url-http-real-basic-auth-storage nil)
69 (defvar url-http-proxy-basic-auth-storage nil)
71 (defvar url-http-open-connections (make-hash-table :test 'equal
73 "A hash table of all open network connections.")
75 (defvar url-http-version "1.1"
76 "What version of HTTP we advertise, as a string.
77 Valid values are 1.1 and 1.0.
78 This is only useful when debugging the HTTP subsystem.
80 Setting this to 1.0 will tell servers not to send chunked encoding,
81 and other HTTP/1.1 specific features.")
83 (defvar url-http-attempt-keepalives t
84 "Whether to use a single TCP connection multiple times in HTTP.
85 This is only useful when debugging the HTTP subsystem. Setting to
86 nil will explicitly close the connection to the server after every
89 (defconst url-http-codes
90 '((100 continue "Continue with request")
91 (101 switching-protocols "Switching protocols")
92 (102 processing "Processing (Added by DAV)")
94 (201 created "Created")
95 (202 accepted "Accepted")
96 (203 non-authoritative "Non-authoritative information")
97 (204 no-content "No content")
98 (205 reset-content "Reset content")
99 (206 partial-content "Partial content")
100 (207 multi-status "Multi-status (Added by DAV)")
101 (300 multiple-choices "Multiple choices")
102 (301 moved-permanently "Moved permanently")
104 (303 see-other "See other")
105 (304 not-modified "Not modified")
106 (305 use-proxy "Use proxy")
107 (307 temporary-redirect "Temporary redirect")
108 (400 bad-request "Bad Request")
109 (401 unauthorized "Unauthorized")
110 (402 payment-required "Payment required")
111 (403 forbidden "Forbidden")
112 (404 not-found "Not found")
113 (405 method-not-allowed "Method not allowed")
114 (406 not-acceptable "Not acceptable")
115 (407 proxy-authentication-required "Proxy authentication required")
116 (408 request-timeout "Request time-out")
117 (409 conflict "Conflict")
119 (411 length-required "Length required")
120 (412 precondition-failed "Precondition failed")
121 (413 request-entity-too-large "Request entity too large")
122 (414 request-uri-too-large "Request-URI too large")
123 (415 unsupported-media-type "Unsupported media type")
124 (416 requested-range-not-satisfiable "Requested range not satisfiable")
125 (417 expectation-failed "Expectation failed")
126 (422 unprocessable-entity "Unprocessable Entity (Added by DAV)")
127 (423 locked "Locked")
128 (424 failed-Dependency "Failed Dependency")
129 (500 internal-server-error "Internal server error")
130 (501 not-implemented "Not implemented")
131 (502 bad-gateway "Bad gateway")
132 (503 service-unavailable "Service unavailable")
133 (504 gateway-timeout "Gateway time-out")
134 (505 http-version-not-supported "HTTP version not supported")
135 (507 insufficient-storage "Insufficient storage"))
136 "The HTTP return codes and their text.")
138 (defcustom url-user-agent (format "User-Agent: %sURL/%s\r\n"
140 (concat url-package-name "/"
141 url-package-version " ")
143 "User Agent used by the URL package."
144 :type '(choice (string :tag "A static User-Agent string")
145 (function :tag "Call a function to get the User-Agent string"))
150 ;; These are all macros so that they are hidden from external sight
151 ;; when the file is byte-compiled.
153 ;; This allows us to expose just the entry points we want.
155 ;; These routines will allow us to implement persistent HTTP
157 (defsubst url-http-debug (&rest args)
159 (let ((proc (get-buffer-process (current-buffer))))
160 ;; The user hit C-g, honor it! Some things can get in an
161 ;; incredibly tight loop (chunked encoding)
164 (set-process-sentinel proc nil)
165 (set-process-filter proc nil)))
166 (error "Transfer interrupted!")))
167 (apply 'url-debug 'http args))
169 (defun url-http-mark-connection-as-busy (host port proc)
170 (url-http-debug "Marking connection as busy: %s:%d %S" host port proc)
171 (set-process-query-on-exit-flag proc t)
172 (puthash (cons host port)
173 (delq proc (gethash (cons host port) url-http-open-connections))
174 url-http-open-connections)
177 (defun url-http-mark-connection-as-free (host port proc)
178 (url-http-debug "Marking connection as free: %s:%d %S" host port proc)
179 (when (memq (process-status proc) '(open run connect))
180 (set-process-buffer proc nil)
181 (set-process-sentinel proc 'url-http-idle-sentinel)
182 (set-process-query-on-exit-flag proc nil)
183 (puthash (cons host port)
184 (cons proc (gethash (cons host port) url-http-open-connections))
185 url-http-open-connections))
188 (defun url-http-find-free-connection (host port &optional gateway-method)
189 (let ((conns (gethash (cons host port) url-http-open-connections))
191 (while (and conns (not connection))
192 (if (not (memq (process-status (car conns)) '(run open connect)))
194 (url-http-debug "Cleaning up dead process: %s:%d %S"
195 host port (car conns))
196 (url-http-idle-sentinel (car conns) nil))
197 (setq connection (car conns))
198 (url-http-debug "Found existing connection: %s:%d %S" host port connection))
201 (url-http-debug "Reusing existing connection: %s:%d" host port)
202 (url-http-debug "Contacting host: %s:%d" host port))
203 (url-lazy-message "Contacting host: %s:%d" host port)
206 (let ((buf (generate-new-buffer " *url-http-temp*")))
207 ;; `url-open-stream' needs a buffer in which to do things
208 ;; like authentication. But we use another buffer afterwards.
210 (let ((proc (url-open-stream host buf host port gateway-method)))
211 ;; url-open-stream might return nil.
212 (when (processp proc)
213 ;; Drop the temp buffer link before killing the buffer.
214 (set-process-buffer proc nil)
215 (setq connection proc)))
216 ;; If there was an error on connect, make sure we don't
218 (when (get-buffer-process buf)
219 (set-process-query-on-exit-flag (get-buffer-process buf) nil))
223 (url-http-mark-connection-as-busy host port connection))))
225 ;; Building an HTTP request
226 (defun url-http-user-agent-string ()
227 (if (or (eq url-privacy-level 'paranoid)
228 (and (listp url-privacy-level)
229 (memq 'agent url-privacy-level)))
231 (if (functionp url-user-agent)
232 (funcall url-user-agent)
235 (defun url-http-create-request (&optional ref-url)
236 "Create an HTTP request for `url-http-target-url', referred to by REF-URL."
237 (let* ((extra-headers)
239 (no-cache (cdr-safe (assoc "Pragma" url-http-extra-headers)))
240 (using-proxy url-http-proxy)
241 (proxy-auth (if (or (cdr-safe (assoc "Proxy-Authorization"
242 url-http-extra-headers))
245 (let ((url-basic-auth-storage
246 'url-http-proxy-basic-auth-storage))
247 (url-get-authentication url-http-proxy nil 'any nil))))
248 (real-fname (url-filename url-http-target-url))
249 (host (url-host url-http-target-url))
250 (auth (if (cdr-safe (assoc "Authorization" url-http-extra-headers))
252 (url-get-authentication (or
253 (and (boundp 'proxy-info)
255 url-http-target-url) nil 'any nil))))
256 (if (equal "" real-fname)
257 (setq real-fname "/"))
258 (setq no-cache (and no-cache (string-match "no-cache" no-cache)))
260 (setq auth (concat "Authorization: " auth "\r\n")))
262 (setq proxy-auth (concat "Proxy-Authorization: " proxy-auth "\r\n")))
264 ;; Protection against stupid values in the referrer
265 (if (and ref-url (stringp ref-url) (or (string= ref-url "file:nil")
266 (string= ref-url "")))
269 ;; We do not want to expose the referrer if the user is paranoid.
270 (if (or (memq url-privacy-level '(low high paranoid))
271 (and (listp url-privacy-level)
272 (memq 'lastloc url-privacy-level)))
275 ;; url-http-extra-headers contains an assoc-list of
276 ;; header/value pairs that we need to put into the request.
277 (setq extra-headers (mapconcat
279 (concat (car x) ": " (cdr x)))
280 url-http-extra-headers "\r\n"))
281 (if (not (equal extra-headers ""))
282 (setq extra-headers (concat extra-headers "\r\n")))
284 ;; This was done with a call to `format'. Concatenating parts has
285 ;; the advantage of keeping the parts of each header together and
286 ;; allows us to elide null lines directly, at the cost of making
287 ;; the layout less clear.
289 ;; We used to concat directly, but if one of the strings happens
290 ;; to being multibyte (even if it only contains pure ASCII) then
291 ;; every string gets converted with `string-MAKE-multibyte' which
292 ;; turns the 127-255 codes into things like latin-1 accented chars
293 ;; (it would work right if it used `string-TO-multibyte' instead).
294 ;; So to avoid the problem we force every string to be unibyte.
296 ;; FIXME: Instead of `string-AS-unibyte' we'd want
297 ;; `string-to-unibyte', so as to properly signal an error if one
298 ;; of the strings contains a multibyte char.
303 (or url-http-method "GET") " "
304 (if using-proxy (url-recreate-url url-http-target-url) real-fname)
305 " HTTP/" url-http-version "\r\n"
306 ;; Version of MIME we speak
307 "MIME-Version: 1.0\r\n"
308 ;; (maybe) Try to keep the connection open
309 "Connection: " (if (or using-proxy
310 (not url-http-attempt-keepalives))
311 "close" "keep-alive") "\r\n"
312 ;; HTTP extensions we support
313 (if url-extensions-header
315 "Extension: %s\r\n" url-extensions-header))
316 ;; Who we want to talk to
317 (if (/= (url-port url-http-target-url)
318 (url-scheme-get-property
319 (url-type url-http-target-url) 'default-port))
321 "Host: %s:%d\r\n" host (url-port url-http-target-url))
322 (format "Host: %s\r\n" host))
324 (if url-personal-mail-address
326 "From: " url-personal-mail-address "\r\n"))
327 ;; Encodings we understand
328 (if (or url-mime-encoding-string
329 ;; MS-Windows loads zlib dynamically, so recheck
330 ;; in case they made it available since
331 ;; initialization in url-vars.el.
332 (and (eq 'system-type 'windows-nt)
333 (fboundp 'zlib-available-p)
335 (setq url-mime-encoding-string "gzip")))
337 "Accept-encoding: " url-mime-encoding-string "\r\n"))
338 (if url-mime-charset-string
340 "Accept-charset: " url-mime-charset-string "\r\n"))
341 ;; Languages we understand
342 (if url-mime-language-string
344 "Accept-language: " url-mime-language-string "\r\n"))
345 ;; Types we understand
346 "Accept: " (or url-mime-accept-string "*/*") "\r\n"
348 (url-http-user-agent-string)
349 ;; Proxy Authorization
354 (when (url-use-cookies url-http-target-url)
355 (url-cookie-generate-header-lines
357 (equal "https" (url-type url-http-target-url))))
359 (if (and (not no-cache)
360 (member url-http-method '("GET" nil)))
361 (let ((tm (url-is-cached url-http-target-url)))
363 (concat "If-modified-since: "
364 (url-get-normalized-date tm) "\r\n"))))
367 "Referer: " ref-url "\r\n"))
372 "Content-length: " (number-to-string
373 (length url-http-data))
380 (url-http-debug "Request is: \n%s" request)
384 (defun url-http-clean-headers ()
385 "Remove trailing \r from header lines.
386 This allows us to use `mail-fetch-field', etc.
387 Return the number of characters removed."
388 (let ((end (marker-position url-http-end-of-headers)))
389 (goto-char (point-min))
390 (while (re-search-forward "\r$" url-http-end-of-headers t)
392 (- end url-http-end-of-headers)))
394 (defun url-http-handle-authentication (proxy)
395 (url-http-debug "Handling %s authentication" (if proxy "proxy" "normal"))
396 (let ((auths (or (nreverse
398 (if proxy "proxy-authenticate" "www-authenticate")
402 (url (url-recreate-url url-current-object))
403 (auth-url (url-recreate-url
404 (if (and proxy (boundp 'url-http-proxy))
406 url-current-object)))
407 (url-basic-auth-storage (if proxy
408 ;; Cheating, but who cares? :)
409 'url-http-proxy-basic-auth-storage
410 'url-http-real-basic-auth-storage))
414 ;; find strongest supported auth
415 (dolist (this-auth auths)
416 (setq this-auth (url-eat-trailing-space
417 (url-strip-leading-spaces
420 (downcase (if (string-match "[ \t]" this-auth)
421 (substring this-auth 0 (match-beginning 0))
423 (registered (url-auth-registered this-type))
424 (this-strength (cddr registered)))
425 (when (and registered (> this-strength strength))
428 strength this-strength))))
430 (if (not (url-auth-registered type))
433 (goto-char (point-max))
434 (insert "<hr>Sorry, but I do not know how to handle " type
435 " authentication. If you'd like to write it,"
436 " please use M-x report-emacs-bug RET.<hr>")
437 ;; We used to set a `status' var (declared "special") but I can't
438 ;; find the corresponding let-binding, so it's probably an error.
439 ;; FIXME: Maybe it was supposed to set `success', i.e. to return t?
441 nil) ;; Not success yet.
443 (let* ((args (url-parse-args (subst-char-in-string ?, ?\; auth)))
444 (auth (url-get-authentication auth-url
445 (cdr-safe (assoc "realm" args))
449 (push (cons (if proxy "Proxy-Authorization" "Authorization") auth)
450 url-http-extra-headers)
451 (let ((url-request-method url-http-method)
452 (url-request-data url-http-data)
453 (url-request-extra-headers url-http-extra-headers))
454 (url-retrieve-internal url url-callback-function
455 url-callback-arguments))
456 nil))))) ;; Not success yet.
458 (defun url-http-parse-response ()
459 "Parse just the response code."
460 (if (not url-http-end-of-headers)
461 (error "Trying to parse HTTP response code in odd buffer: %s" (buffer-name)))
462 (url-http-debug "url-http-parse-response called in (%s)" (buffer-name))
463 (goto-char (point-min))
464 (skip-chars-forward " \t\n") ; Skip any blank crap
465 (skip-chars-forward "HTTP/") ; Skip HTTP Version
466 (setq url-http-response-version
467 (buffer-substring (point)
469 (skip-chars-forward "[0-9].")
471 (setq url-http-response-status (read (current-buffer))))
473 (defun url-http-handle-cookies ()
474 "Handle all set-cookie / set-cookie2 headers in an HTTP response.
475 The buffer must already be narrowed to the headers, so `mail-fetch-field' will
477 (let ((cookies (nreverse (mail-fetch-field "Set-Cookie" nil nil t)))
478 (cookies2 (nreverse (mail-fetch-field "Set-Cookie2" nil nil t))))
479 (and cookies (url-http-debug "Found %d Set-Cookie headers" (length cookies)))
480 (and cookies2 (url-http-debug "Found %d Set-Cookie2 headers" (length cookies2)))
482 (url-cookie-handle-set-cookie (pop cookies)))
484 ;;; (url-cookie-handle-set-cookie2 (pop cookies)))
488 (defun url-http-parse-headers ()
489 "Parse and handle HTTP specific headers.
490 Return t if and only if the current buffer is still active and
491 should be shown to the user."
492 ;; The comments after each status code handled are taken from RFC
494 (url-http-mark-connection-as-free (url-host url-current-object)
495 (url-port url-current-object)
497 ;; Pass the https certificate on to the caller.
498 (when (and (fboundp 'gnutls-available-p)
499 (gnutls-available-p))
500 (let ((status (gnutls-peer-status url-http-process)))
502 (plist-get (car url-callback-arguments) :peer))
503 (setcar url-callback-arguments
504 (plist-put (car url-callback-arguments)
506 (if (or (not (boundp 'url-http-end-of-headers))
507 (not url-http-end-of-headers))
508 (error "Trying to parse headers in odd buffer: %s" (buffer-name)))
509 (goto-char (point-min))
510 (url-http-debug "url-http-parse-headers called in (%s)" (buffer-name))
511 (url-http-parse-response)
512 (mail-narrow-to-head)
513 ;;(narrow-to-region (point-min) url-http-end-of-headers)
514 (let ((connection (mail-fetch-field "Connection")))
515 ;; In HTTP 1.0, keep the connection only if there is a
516 ;; "Connection: keep-alive" header.
517 ;; In HTTP 1.1 (and greater), keep the connection unless there is a
518 ;; "Connection: close" header
520 ((string= url-http-response-version "1.0")
521 (unless (and connection
522 (string= (downcase connection) "keep-alive"))
523 (delete-process url-http-process)))
525 (when (and connection
526 (string= (downcase connection) "close"))
527 (delete-process url-http-process)))))
528 (let* ((buffer (current-buffer))
529 (class (/ url-http-response-status 100))
531 ;; other status symbols: jewelry and luxury cars
532 (status-symbol (cadr (assq url-http-response-status url-http-codes))))
533 (url-http-debug "Parsed HTTP headers: class=%d status=%d"
534 class url-http-response-status)
535 (when (url-use-cookies url-http-target-url)
536 (url-http-handle-cookies))
539 ;; Classes of response codes
541 ;; 5xx = Server Error
542 ;; 4xx = Client Error
545 ;; 1xx = Informational
546 (1 ; Information messages
547 ;; 100 = Continue with request
548 ;; 101 = Switching protocols
549 ;; 102 = Processing (Added by DAV)
550 (url-mark-buffer-as-dead buffer)
551 (error "HTTP responses in class 1xx not supported (%d)"
552 url-http-response-status))
557 ;; 203 Non-authoritative information
560 ;; 206 Partial content
561 ;; 207 Multi-status (Added by DAV)
563 ((or `no-content `reset-content)
564 ;; No new data, just stay at the same document
565 (url-mark-buffer-as-dead buffer))
567 ;; Generic success for all others. Store in the cache, and
568 ;; mark it as successful.
570 (if (and url-automatic-caching (equal url-http-method "GET"))
571 (url-store-in-cache buffer))))
574 ;; 300 Multiple choices
575 ;; 301 Moved permanently
580 ;; 307 Temporary redirect
581 (let ((redirect-uri (or (mail-fetch-field "Location")
582 (mail-fetch-field "URI"))))
584 (`multiple-choices ; 300
585 ;; Quoth the spec (section 10.3.1)
586 ;; -------------------------------
587 ;; The requested resource corresponds to any one of a set of
588 ;; representations, each with its own specific location and
589 ;; agent-driven negotiation information is being provided so
590 ;; that the user can select a preferred representation and
591 ;; redirect its request to that location.
593 ;; If the server has a preferred choice of representation, it
594 ;; SHOULD include the specific URI for that representation in
595 ;; the Location field; user agents MAY use the Location field
596 ;; value for automatic redirection.
597 ;; -------------------------------
598 ;; We do not support agent-driven negotiation, so we just
599 ;; redirect to the preferred URI if one is provided.
601 ((or `moved-permanently `found `temporary-redirect) ; 301 302 307
602 ;; If the 301|302 status code is received in response to a
603 ;; request other than GET or HEAD, the user agent MUST NOT
604 ;; automatically redirect the request unless it can be
605 ;; confirmed by the user, since this might change the
606 ;; conditions under which the request was issued.
607 (unless (member url-http-method '("HEAD" "GET"))
608 (setq redirect-uri nil)))
610 ;; The response to the request can be found under a different
611 ;; URI and SHOULD be retrieved using a GET method on that
613 (setq url-http-method "GET"
616 ;; The 304 response MUST NOT contain a message-body.
617 (url-http-debug "Extracting document from cache... (%s)"
618 (url-cache-create-filename (url-view-url t)))
619 (url-cache-extract (url-cache-create-filename (url-view-url t)))
620 (setq redirect-uri nil
623 ;; The requested resource MUST be accessed through the
624 ;; proxy given by the Location field. The Location field
625 ;; gives the URI of the proxy. The recipient is expected
626 ;; to repeat this single request via the proxy. 305
627 ;; responses MUST only be generated by origin servers.
628 (error "Redirection thru a proxy server not supported: %s"
631 ;; Treat everything like '300'
634 ;; Clean off any whitespace and/or <...> cruft.
635 (if (string-match "\\([^ \t]+\\)[ \t]" redirect-uri)
636 (setq redirect-uri (match-string 1 redirect-uri)))
637 (if (string-match "^<\\(.*\\)>$" redirect-uri)
638 (setq redirect-uri (match-string 1 redirect-uri)))
640 ;; Some stupid sites (like sourceforge) send a
641 ;; non-fully-qualified URL (ie: /), which royally confuses
643 (if (not (string-match url-nonrelative-link redirect-uri))
644 ;; Be careful to use the real target URL, otherwise we may
645 ;; compute the redirection relative to the URL of the proxy.
647 (url-expand-file-name redirect-uri url-http-target-url)))
648 (let ((url-request-method url-http-method)
649 (url-request-data url-http-data)
650 (url-request-extra-headers url-http-extra-headers))
651 ;; Check existing number of redirects
652 (if (or (< url-max-redirections 0)
653 (and (> url-max-redirections 0)
654 (let ((events (car url-callback-arguments))
657 (if (eq (car events) :redirect)
658 (setq old-redirects (1+ old-redirects)))
659 (and (setq events (cdr events))
660 (setq events (cdr events))))
661 (< old-redirects url-max-redirections))))
662 ;; url-max-redirections hasn't been reached, so go
663 ;; ahead and redirect.
665 ;; Remember that the request was redirected.
666 (setf (car url-callback-arguments)
667 (nconc (list :redirect redirect-uri)
668 (car url-callback-arguments)))
669 ;; Put in the current buffer a forwarding pointer to the new
670 ;; destination buffer.
671 ;; FIXME: This is a hack to fix url-retrieve-synchronously
672 ;; without changing the API. Instead url-retrieve should
673 ;; either simply not return the "destination" buffer, or it
674 ;; should take an optional `dest-buf' argument.
675 (set (make-local-variable 'url-redirect-buffer)
676 (url-retrieve-internal
677 redirect-uri url-callback-function
678 url-callback-arguments
679 (url-silent url-current-object)
680 (not (url-use-cookies url-current-object))))
681 (url-mark-buffer-as-dead buffer))
682 ;; We hit url-max-redirections, so issue an error and
684 (url-http-debug "Maximum redirections reached")
685 (setf (car url-callback-arguments)
686 (nconc (list :error (list 'error 'http-redirect-limit
688 (car url-callback-arguments)))
689 (setq success t))))))
693 ;; 402 Payment required
696 ;; 405 Method not allowed
697 ;; 406 Not acceptable
698 ;; 407 Proxy authentication required
699 ;; 408 Request time-out
702 ;; 411 Length required
703 ;; 412 Precondition failed
704 ;; 413 Request entity too large
705 ;; 414 Request-URI too large
706 ;; 415 Unsupported media type
707 ;; 416 Requested range not satisfiable
708 ;; 417 Expectation failed
709 ;; 422 Unprocessable Entity (Added by DAV)
711 ;; 424 Failed Dependency
715 ;; The request requires user authentication. The response
716 ;; MUST include a WWW-Authenticate header field containing a
717 ;; challenge applicable to the requested resource. The
718 ;; client MAY repeat the request with a suitable
719 ;; Authorization header field.
720 (url-http-handle-authentication nil))
721 (`payment-required ; 402
722 ;; This code is reserved for future use
723 (url-mark-buffer-as-dead buffer)
724 (error "Somebody wants you to give them money"))
726 ;; The server understood the request, but is refusing to
727 ;; fulfill it. Authorization will not help and the request
728 ;; SHOULD NOT be repeated.
733 (`method-not-allowed ; 405
734 ;; The method specified in the Request-Line is not allowed
735 ;; for the resource identified by the Request-URI. The
736 ;; response MUST include an Allow header containing a list of
737 ;; valid methods for the requested resource.
739 (`not-acceptable ; 406
740 ;; The resource identified by the request is only capable of
741 ;; generating response entities which have content
742 ;; characteristics not acceptable according to the accept
743 ;; headers sent in the request.
745 (`proxy-authentication-required ; 407
746 ;; This code is similar to 401 (Unauthorized), but indicates
747 ;; that the client must first authenticate itself with the
748 ;; proxy. The proxy MUST return a Proxy-Authenticate header
749 ;; field containing a challenge applicable to the proxy for
750 ;; the requested resource.
751 (url-http-handle-authentication t))
752 (`request-timeout ; 408
753 ;; The client did not produce a request within the time that
754 ;; the server was prepared to wait. The client MAY repeat
755 ;; the request without modifications at any later time.
758 ;; The request could not be completed due to a conflict with
759 ;; the current state of the resource. This code is only
760 ;; allowed in situations where it is expected that the user
761 ;; might be able to resolve the conflict and resubmit the
762 ;; request. The response body SHOULD include enough
763 ;; information for the user to recognize the source of the
767 ;; The requested resource is no longer available at the
768 ;; server and no forwarding address is known.
770 (`length-required ; 411
771 ;; The server refuses to accept the request without a defined
772 ;; Content-Length. The client MAY repeat the request if it
773 ;; adds a valid Content-Length header field containing the
774 ;; length of the message-body in the request message.
776 ;; NOTE - this will never happen because
777 ;; `url-http-create-request' automatically calculates the
780 (`precondition-failed ; 412
781 ;; The precondition given in one or more of the
782 ;; request-header fields evaluated to false when it was
783 ;; tested on the server.
785 ((or `request-entity-too-large `request-uri-too-large) ; 413 414
786 ;; The server is refusing to process a request because the
787 ;; request entity|URI is larger than the server is willing or
790 (`unsupported-media-type ; 415
791 ;; The server is refusing to service the request because the
792 ;; entity of the request is in a format not supported by the
793 ;; requested resource for the requested method.
795 (`requested-range-not-satisfiable ; 416
796 ;; A server SHOULD return a response with this status code if
797 ;; a request included a Range request-header field, and none
798 ;; of the range-specifier values in this field overlap the
799 ;; current extent of the selected resource, and the request
800 ;; did not include an If-Range request-header field.
802 (`expectation-failed ; 417
803 ;; The expectation given in an Expect request-header field
804 ;; could not be met by this server, or, if the server is a
805 ;; proxy, the server has unambiguous evidence that the
806 ;; request could not be met by the next-hop server.
809 ;; The request could not be understood by the server due to
810 ;; malformed syntax. The client SHOULD NOT repeat the
811 ;; request without modifications.
813 ;; Tell the callback that an error occurred, and what the
816 (setf (car url-callback-arguments)
817 (nconc (list :error (list 'error 'http url-http-response-status))
818 (car url-callback-arguments)))))
820 ;; 500 Internal server error
821 ;; 501 Not implemented
823 ;; 503 Service unavailable
824 ;; 504 Gateway time-out
825 ;; 505 HTTP version not supported
826 ;; 507 Insufficient storage
828 (pcase url-http-response-status
829 (`not-implemented ; 501
830 ;; The server does not support the functionality required to
831 ;; fulfill the request.
834 ;; The server, while acting as a gateway or proxy, received
835 ;; an invalid response from the upstream server it accessed
836 ;; in attempting to fulfill the request.
838 (`service-unavailable ; 503
839 ;; The server is currently unable to handle the request due
840 ;; to a temporary overloading or maintenance of the server.
841 ;; The implication is that this is a temporary condition
842 ;; which will be alleviated after some delay. If known, the
843 ;; length of the delay MAY be indicated in a Retry-After
844 ;; header. If no Retry-After is given, the client SHOULD
845 ;; handle the response as it would for a 500 response.
847 (`gateway-timeout ; 504
848 ;; The server, while acting as a gateway or proxy, did not
849 ;; receive a timely response from the upstream server
850 ;; specified by the URI (e.g. HTTP, FTP, LDAP) or some other
851 ;; auxiliary server (e.g. DNS) it needed to access in
852 ;; attempting to complete the request.
854 (`http-version-not-supported ; 505
855 ;; The server does not support, or refuses to support, the
856 ;; HTTP protocol version that was used in the request
859 (`insufficient-storage ; 507 (DAV)
860 ;; The method could not be performed on the resource
861 ;; because the server is unable to store the representation
862 ;; needed to successfully complete the request. This
863 ;; condition is considered to be temporary. If the request
864 ;; which received this status code was the result of a user
865 ;; action, the request MUST NOT be repeated until it is
866 ;; requested by a separate user action.
868 ;; Tell the callback that an error occurred, and what the
871 (setf (car url-callback-arguments)
872 (nconc (list :error (list 'error 'http url-http-response-status))
873 (car url-callback-arguments)))))
875 (error "Unknown class of HTTP response code: %d (%d)"
876 class url-http-response-status)))
878 (url-mark-buffer-as-dead buffer)
879 (url-handle-content-transfer-encoding))
880 (url-http-debug "Finished parsing HTTP headers: %S" success)
882 (goto-char (point-min))
885 (declare-function zlib-decompress-region "decompress.c" (start end))
887 (defun url-handle-content-transfer-encoding ()
888 (let ((encoding (mail-fetch-field "content-encoding")))
890 (fboundp 'zlib-available-p)
892 (equal (downcase encoding) "gzip"))
895 (goto-char (point-min))
896 (when (search-forward "\n\n")
897 (zlib-decompress-region (point) (point-max)))))))
900 (defun url-http-activate-callback ()
901 "Activate callback specified when this buffer was created."
902 (url-http-mark-connection-as-free (url-host url-current-object)
903 (url-port url-current-object)
905 (url-http-debug "Activating callback in buffer (%s): %S %S"
906 (buffer-name) url-callback-function url-callback-arguments)
907 (apply url-callback-function url-callback-arguments))
911 ;; These unfortunately cannot be macros... please ignore them!
912 (defun url-http-idle-sentinel (proc why)
913 "Remove (now defunct) process PROC from the list of open connections."
914 (maphash (lambda (key val)
916 (puthash key (delq proc val) url-http-open-connections)))
917 url-http-open-connections))
919 (defun url-http-end-of-document-sentinel (proc why)
920 ;; Sentinel used to handle (i) terminated old HTTP/0.9 connections,
921 ;; and (ii) closed connection due to reusing a HTTP connection which
922 ;; we believed was still alive, but which the server closed on us.
923 ;; We handle case (ii) by calling `url-http' again.
924 (url-http-debug "url-http-end-of-document-sentinel in buffer (%s)"
925 (process-buffer proc))
926 (url-http-idle-sentinel proc why)
927 (when (buffer-name (process-buffer proc))
928 (with-current-buffer (process-buffer proc)
929 (goto-char (point-min))
930 (cond ((not (looking-at "HTTP/"))
931 (if url-http-no-retry
932 ;; HTTP/0.9 just gets passed back no matter what
933 (url-http-activate-callback)
934 ;; Call `url-http' again if our connection expired.
936 (let ((url-request-method url-http-method)
937 (url-request-extra-headers url-http-extra-headers)
938 (url-request-data url-http-data))
939 (url-http url-current-object url-callback-function
940 url-callback-arguments (current-buffer)))))
941 ((url-http-parse-headers)
942 (url-http-activate-callback))))))
944 (defun url-http-simple-after-change-function (st nd length)
945 ;; Function used when we do NOT know how long the document is going to be
946 ;; Just _very_ simple 'downloaded %d' type of info.
947 (url-lazy-message "Reading %s..." (file-size-human-readable nd)))
949 (defun url-http-content-length-after-change-function (st nd length)
950 "Function used when we DO know how long the document is going to be.
951 More sophisticated percentage downloaded, etc.
952 Also does minimal parsing of HTTP headers and will actually cause
953 the callback to be triggered."
954 (if url-http-content-type
955 (url-display-percentage
956 "Reading [%s]... %s of %s (%d%%)"
957 (url-percentage (- nd url-http-end-of-headers)
958 url-http-content-length)
959 url-http-content-type
960 (file-size-human-readable (- nd url-http-end-of-headers))
961 (file-size-human-readable url-http-content-length)
962 (url-percentage (- nd url-http-end-of-headers)
963 url-http-content-length))
964 (url-display-percentage
965 "Reading... %s of %s (%d%%)"
966 (url-percentage (- nd url-http-end-of-headers)
967 url-http-content-length)
968 (file-size-human-readable (- nd url-http-end-of-headers))
969 (file-size-human-readable url-http-content-length)
970 (url-percentage (- nd url-http-end-of-headers)
971 url-http-content-length)))
973 (if (> (- nd url-http-end-of-headers) url-http-content-length)
975 ;; Found the end of the document! Wheee!
976 (url-display-percentage nil nil)
977 (url-lazy-message "Reading... done.")
978 (if (url-http-parse-headers)
979 (url-http-activate-callback)))))
981 (defun url-http-chunked-encoding-after-change-function (st nd length)
982 "Function used when dealing with 'chunked' encoding.
983 Cannot give a sophisticated percentage, but we need a different
984 function to look for the special 0-length chunk that signifies
985 the end of the document."
988 (let ((read-next-chunk t)
991 (no-initial-crlf nil))
992 ;; We need to loop thru looking for more chunks even within
993 ;; one after-change-function call.
994 (while read-next-chunk
995 (setq no-initial-crlf (= 0 url-http-chunked-counter))
996 (if url-http-content-type
997 (url-display-percentage nil
998 "Reading [%s]... chunk #%d"
999 url-http-content-type url-http-chunked-counter)
1000 (url-display-percentage nil
1001 "Reading... chunk #%d"
1002 url-http-chunked-counter))
1003 (url-http-debug "Reading chunk %d (%d %d %d)"
1004 url-http-chunked-counter st nd length)
1005 (setq regexp (if no-initial-crlf
1006 "\\([0-9a-z]+\\).*\r?\n"
1007 "\r?\n\\([0-9a-z]+\\).*\r?\n"))
1009 (if url-http-chunked-start
1010 ;; We know how long the chunk is supposed to be, skip over
1011 ;; leading crap if possible.
1012 (if (> nd (+ url-http-chunked-start url-http-chunked-length))
1014 (url-http-debug "Got to the end of chunk #%d!"
1015 url-http-chunked-counter)
1016 (goto-char (+ url-http-chunked-start
1017 url-http-chunked-length)))
1018 (url-http-debug "Still need %d bytes to hit end of chunk"
1019 (- (+ url-http-chunked-start
1020 url-http-chunked-length)
1022 (setq read-next-chunk nil)))
1023 (if (not read-next-chunk)
1024 (url-http-debug "Still spinning for next chunk...")
1025 (if no-initial-crlf (skip-chars-forward "\r\n"))
1026 (if (not (looking-at regexp))
1028 ;; Must not have received the entirety of the chunk header,
1029 ;; need to spin some more.
1030 (url-http-debug "Did not see start of chunk @ %d!" (point))
1031 (setq read-next-chunk nil))
1032 (add-text-properties (match-beginning 0) (match-end 0)
1038 (setq url-http-chunked-length (string-to-number (buffer-substring
1042 url-http-chunked-counter (1+ url-http-chunked-counter)
1043 url-http-chunked-start (set-marker
1044 (or url-http-chunked-start
1047 ; (if (not url-http-debug)
1048 (delete-region (match-beginning 0) (match-end 0));)
1049 (url-http-debug "Saw start of chunk %d (length=%d, start=%d"
1050 url-http-chunked-counter url-http-chunked-length
1051 (marker-position url-http-chunked-start))
1052 (if (= 0 url-http-chunked-length)
1054 ;; Found the end of the document! Wheee!
1055 (url-http-debug "Saw end of stream chunk!")
1056 (setq read-next-chunk nil)
1057 (url-display-percentage nil nil)
1058 ;; Every chunk, even the last 0-length one, is
1059 ;; terminated by CRLF. Skip it.
1060 (when (looking-at "\r?\n")
1061 (url-http-debug "Removing terminator of last chunk")
1062 (delete-region (match-beginning 0) (match-end 0)))
1063 (if (re-search-forward "^\r*$" nil t)
1064 (url-http-debug "Saw end of trailers..."))
1065 (if (url-http-parse-headers)
1066 (url-http-activate-callback))))))))))
1068 (defun url-http-wait-for-headers-change-function (st nd length)
1069 ;; This will wait for the headers to arrive and then splice in the
1070 ;; next appropriate after-change-function, etc.
1071 (url-http-debug "url-http-wait-for-headers-change-function (%s)"
1073 (let ((end-of-headers nil)
1075 (process-buffer (current-buffer))
1076 (content-length nil))
1078 (goto-char (point-min))
1079 (if (and (looking-at ".*\n") ; have one line at least
1080 (not (looking-at "^HTTP/[1-9]\\.[0-9]")))
1081 ;; Not HTTP/x.y data, must be 0.9
1082 ;; God, I wish this could die.
1083 (setq end-of-headers t
1084 url-http-end-of-headers 0
1086 ;; Blank line at end of headers.
1087 (when (re-search-forward "^\r?\n" nil t)
1089 ;; Saw the end of the headers
1090 (url-http-debug "Saw end of headers... (%s)" (buffer-name))
1091 (setq url-http-end-of-headers (set-marker (make-marker)
1094 (setq nd (- nd (url-http-clean-headers)))))
1096 (if (not end-of-headers)
1097 ;; Haven't seen the end of the headers yet, need to wait
1098 ;; for more data to arrive.
1101 (url-http-parse-response)
1102 (mail-narrow-to-head)
1103 (setq url-http-transfer-encoding (mail-fetch-field
1104 "transfer-encoding")
1105 url-http-content-type (mail-fetch-field "content-type"))
1106 (if (mail-fetch-field "content-length")
1107 (setq url-http-content-length
1108 (string-to-number (mail-fetch-field "content-length"))))
1110 (when url-http-transfer-encoding
1111 (setq url-http-transfer-encoding
1112 (downcase url-http-transfer-encoding)))
1115 ((null url-http-response-status)
1116 ;; We got back a headerless malformed response from the
1118 (url-http-activate-callback))
1119 ((or (= url-http-response-status 204)
1120 (= url-http-response-status 205))
1121 (url-http-debug "%d response must have headers only (%s)."
1122 url-http-response-status (buffer-name))
1123 (when (url-http-parse-headers)
1124 (url-http-activate-callback)))
1125 ((string= "HEAD" url-http-method)
1126 ;; A HEAD request is _ALWAYS_ terminated by the header
1127 ;; information, regardless of any entity headers,
1128 ;; according to section 4.4 of the HTTP/1.1 draft.
1129 (url-http-debug "HEAD request must have headers only (%s)."
1131 (when (url-http-parse-headers)
1132 (url-http-activate-callback)))
1133 ((string= "CONNECT" url-http-method)
1134 ;; A CONNECT request is finished, but we cannot stick this
1135 ;; back on the free connection list
1136 (url-http-debug "CONNECT request must have headers only.")
1137 (when (url-http-parse-headers)
1138 (url-http-activate-callback)))
1139 ((equal url-http-response-status 304)
1140 ;; Only allowed to have a header section. We have to handle
1141 ;; this here instead of in url-http-parse-headers because if
1142 ;; you have a cached copy of something without a known
1143 ;; content-length, and try to retrieve it from the cache, we'd
1144 ;; fall into the 'being dumb' section and wait for the
1145 ;; connection to terminate, which means we'd wait for 10
1146 ;; seconds for the keep-alives to time out on some servers.
1147 (when (url-http-parse-headers)
1148 (url-http-activate-callback)))
1150 ;; HTTP/0.9 always signaled end-of-connection by closing the
1153 "Saw HTTP/0.9 response, connection closed means end of document.")
1154 (setq url-http-after-change-function
1155 'url-http-simple-after-change-function))
1156 ((equal url-http-transfer-encoding "chunked")
1157 (url-http-debug "Saw chunked encoding.")
1158 (setq url-http-after-change-function
1159 'url-http-chunked-encoding-after-change-function)
1160 (when (> nd url-http-end-of-headers)
1162 "Calling initial chunked-encoding for extra data at end of headers")
1163 (url-http-chunked-encoding-after-change-function
1164 (marker-position url-http-end-of-headers) nd
1165 (- nd url-http-end-of-headers))))
1166 ((integerp url-http-content-length)
1168 "Got a content-length, being smart about document end.")
1169 (setq url-http-after-change-function
1170 'url-http-content-length-after-change-function)
1172 ((= 0 url-http-content-length)
1173 ;; We got a NULL body! Activate the callback
1176 "Got 0-length content-length, activating callback immediately.")
1177 (when (url-http-parse-headers)
1178 (url-http-activate-callback)))
1179 ((> nd url-http-end-of-headers)
1180 ;; Have some leftover data
1181 (url-http-debug "Calling initial content-length for extra data at end of headers")
1182 (url-http-content-length-after-change-function
1183 (marker-position url-http-end-of-headers)
1185 (- nd url-http-end-of-headers)))
1189 (url-http-debug "No content-length, being dumb.")
1190 (setq url-http-after-change-function
1191 'url-http-simple-after-change-function)))))
1192 ;; We are still at the beginning of the buffer... must just be
1193 ;; waiting for a response.
1194 (url-http-debug "Spinning waiting for headers...")
1195 (when (eq process-buffer (current-buffer))
1196 (goto-char (point-max)))))
1198 (defun url-http (url callback cbargs &optional retry-buffer gateway-method)
1199 "Retrieve URL via HTTP asynchronously.
1200 URL must be a parsed URL. See `url-generic-parse-url' for details.
1202 When retrieval is completed, execute the function CALLBACK, passing it
1203 an updated value of CBARGS as arguments. The first element in CBARGS
1204 should be a plist describing what has happened so far during the
1205 request, as described in the docstring of `url-retrieve' (if in
1206 doubt, specify nil).
1208 Optional arg RETRY-BUFFER, if non-nil, specifies the buffer of a
1209 previous `url-http' call, which is being re-attempted.
1211 Optional arg GATEWAY-METHOD specifies the gateway to be used,
1212 overriding the value of `url-gateway-method'."
1213 (cl-check-type url vector "Need a pre-parsed URL.")
1214 (let* ((host (url-host (or url-using-proxy url)))
1215 (port (url-port (or url-using-proxy url)))
1216 (nsm-noninteractive (or url-request-noninteractive
1217 (and (boundp 'url-http-noninteractive)
1218 url-http-noninteractive)))
1219 (connection (url-http-find-free-connection host port gateway-method))
1220 (buffer (or retry-buffer
1221 (generate-new-buffer
1222 (format " *http %s:%d*" host port)))))
1223 (if (not connection)
1224 ;; Failed to open the connection for some reason
1226 (kill-buffer buffer)
1228 (error "Could not create connection to %s:%d" host port))
1229 (with-current-buffer buffer
1230 (mm-disable-multibyte)
1231 (setq url-current-object url
1232 mode-line-format "%b [%s]")
1234 (dolist (var '(url-http-end-of-headers
1235 url-http-content-type
1236 url-http-content-length
1237 url-http-transfer-encoding
1238 url-http-after-change-function
1239 url-http-response-version
1240 url-http-response-status
1241 url-http-chunked-length
1242 url-http-chunked-counter
1243 url-http-chunked-start
1244 url-callback-function
1245 url-callback-arguments
1249 url-http-extra-headers
1250 url-http-noninteractive
1254 url-http-connection-opened
1256 (set (make-local-variable var) nil))
1258 (setq url-http-method (or url-request-method "GET")
1259 url-http-extra-headers url-request-extra-headers
1260 url-http-noninteractive url-request-noninteractive
1261 url-http-data url-request-data
1262 url-http-process connection
1263 url-http-chunked-length nil
1264 url-http-chunked-start nil
1265 url-http-chunked-counter 0
1266 url-callback-function callback
1267 url-callback-arguments cbargs
1268 url-http-after-change-function 'url-http-wait-for-headers-change-function
1269 url-http-target-url url-current-object
1270 url-http-no-retry retry-buffer
1271 url-http-connection-opened nil
1272 url-http-proxy url-using-proxy)
1274 (set-process-buffer connection buffer)
1275 (set-process-filter connection 'url-http-generic-filter)
1276 (pcase (process-status connection)
1278 ;; Asynchronous connection
1279 (set-process-sentinel connection 'url-http-async-sentinel))
1281 ;; Asynchronous connection failed
1282 (error "Could not create connection to %s:%d" host port))
1284 (set-process-sentinel connection
1285 'url-http-end-of-document-sentinel)
1286 (process-send-string connection (url-http-create-request))))))
1289 (defun url-http-async-sentinel (proc why)
1290 ;; We are performing an asynchronous connection, and a status change
1292 (when (buffer-name (process-buffer proc))
1293 (with-current-buffer (process-buffer proc)
1295 (url-http-connection-opened
1296 (setq url-http-no-retry t)
1297 (url-http-end-of-document-sentinel proc why))
1298 ((string= (substring why 0 4) "open")
1299 (setq url-http-connection-opened t)
1300 (condition-case error
1301 (process-send-string proc (url-http-create-request))
1303 (setq url-http-connection-opened nil)
1304 (message "HTTP error: %s" error))))
1306 (setf (car url-callback-arguments)
1307 (nconc (list :error (list 'error 'connection-failed why
1308 :host (url-host (or url-http-proxy url-current-object))
1309 :service (url-port (or url-http-proxy url-current-object))))
1310 (car url-callback-arguments)))
1311 (url-http-activate-callback))))))
1313 ;; Since Emacs 19/20 does not allow you to change the
1314 ;; `after-change-functions' hook in the midst of running them, we fake
1315 ;; an after change by hooking into the process filter and inserting
1316 ;; the data ourselves. This is slightly less efficient, but there
1317 ;; were tons of weird ways the after-change code was biting us in the
1319 ;; FIXME this can probably be simplified since the above is no longer true.
1320 (defun url-http-generic-filter (proc data)
1321 ;; Sometimes we get a zero-length data chunk after the process has
1322 ;; been changed to 'free', which means it has no buffer associated
1323 ;; with it. Do nothing if there is no buffer, or 0 length data.
1324 (and (process-buffer proc)
1325 (/= (length data) 0)
1326 (with-current-buffer (process-buffer proc)
1327 (url-http-debug "Calling after change function `%s' for `%S'" url-http-after-change-function proc)
1328 (funcall url-http-after-change-function
1331 (goto-char (point-max))
1336 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1337 ;;; file-name-handler stuff from here on out
1338 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1339 (defalias 'url-http-symbol-value-in-buffer
1340 (if (fboundp 'symbol-value-in-buffer)
1341 'symbol-value-in-buffer
1342 (lambda (symbol buffer &optional unbound-value)
1343 "Return the value of SYMBOL in BUFFER, or UNBOUND-VALUE if it is unbound."
1344 (with-current-buffer buffer
1345 (if (not (boundp symbol))
1347 (symbol-value symbol))))))
1349 (defun url-http-head (url)
1350 (let ((url-request-method "HEAD")
1351 (url-request-data nil))
1352 (url-retrieve-synchronously url)))
1354 (defun url-http-file-exists-p (url)
1355 (let ((buffer (url-http-head url)))
1357 (let ((status (url-http-symbol-value-in-buffer 'url-http-response-status
1360 (and (integerp status)
1361 (>= status 200) (< status 300))
1362 (kill-buffer buffer))))))
1364 (defalias 'url-http-file-readable-p 'url-http-file-exists-p)
1366 (defun url-http-head-file-attributes (url &optional id-format)
1367 (let ((buffer (url-http-head url)))
1371 nil ;dir / link / normal file
1372 1 ;number of links to file.
1374 nil nil nil ;atime ; mtime ; ctime
1375 (url-http-symbol-value-in-buffer 'url-http-content-length
1377 (eval-when-compile (make-string 10 ?-))
1378 nil nil nil) ;whether gid would change ; inode ; device.
1379 (kill-buffer buffer)))))
1381 (declare-function url-dav-file-attributes "url-dav" (url &optional id-format))
1383 (defun url-http-file-attributes (url &optional id-format)
1384 (if (url-dav-supported-p url)
1385 (url-dav-file-attributes url id-format)
1386 (url-http-head-file-attributes url id-format)))
1388 (defun url-http-options (url)
1389 "Return a property list describing options available for URL.
1390 This list is retrieved using the `OPTIONS' HTTP method.
1392 Property list members:
1395 A list of symbols specifying what HTTP methods the resource
1399 A list of numbers specifying what DAV protocol/schema versions are
1403 A list of supported DASL search types supported (string form)
1406 A list of the units available for use in partial document fetches.
1409 The `Platform For Privacy Protection' description for the resource.
1410 Currently this is just the raw header contents. This is likely to
1411 change once P3P is formally supported by the URL package or
1413 (let* ((url-request-method "OPTIONS")
1414 (url-request-data nil)
1415 (buffer (url-retrieve-synchronously url))
1418 (when (and buffer (= 2 (/ (url-http-symbol-value-in-buffer
1419 'url-http-response-status buffer 0) 100)))
1420 ;; Only parse the options if we got a 2xx response code!
1421 (with-current-buffer buffer
1424 (mail-narrow-to-head)
1426 ;; Figure out what methods are supported.
1427 (when (setq header (mail-fetch-field "allow"))
1428 (setq options (plist-put
1430 (mapcar 'intern (split-string header "[ ,]+")))))
1433 (when (setq header (mail-fetch-field "dav"))
1434 (setq options (plist-put
1437 (mapcar 'string-to-number
1438 (split-string header "[, ]+"))))))
1441 (when (setq header (mail-fetch-field "dasl"))
1442 (setq options (plist-put
1444 (split-string header "[, ]+"))))
1446 ;; P3P - should get more detailed here. FIXME
1447 (when (setq header (mail-fetch-field "p3p"))
1448 (setq options (plist-put options 'p3p header)))
1450 ;; Check for whether they accept byte-range requests.
1451 (when (setq header (mail-fetch-field "accept-ranges"))
1452 (setq options (plist-put
1456 (split-string header "[, ]+"))))))
1458 (if buffer (kill-buffer buffer))
1461 ;; HTTPS. This used to be in url-https.el, but that file collides
1462 ;; with url-http.el on systems with 8-character file names.
1465 (defconst url-https-default-port 443 "Default HTTPS port.")
1466 (defconst url-https-asynchronous-p t "HTTPS retrievals are asynchronous.")
1468 ;; FIXME what is the point of this alias being an autoload?
1469 ;; Trying to use it will not cause url-http to be loaded,
1470 ;; since the full alias just gets dumped into loaddefs.el.
1472 ;;;###autoload (autoload 'url-default-expander "url-expand")
1474 (defalias 'url-https-expand-file-name 'url-default-expander)
1476 (defmacro url-https-create-secure-wrapper (method args)
1477 `(defun ,(intern (format (if method "url-https-%s" "url-https") method)) ,args
1478 ,(format "HTTPS wrapper around `%s' call." (or method "url-http"))
1479 (,(intern (format (if method "url-http-%s" "url-http") method))
1480 ,@(remove '&rest (remove '&optional (append args (if method nil '(nil 'tls))))))))
1482 ;;;###autoload (autoload 'url-https "url-http")
1483 (url-https-create-secure-wrapper nil (url callback cbargs))
1484 ;;;###autoload (autoload 'url-https-file-exists-p "url-http")
1485 (url-https-create-secure-wrapper file-exists-p (url))
1486 ;;;###autoload (autoload 'url-https-file-readable-p "url-http")
1487 (url-https-create-secure-wrapper file-readable-p (url))
1488 ;;;###autoload (autoload 'url-https-file-attributes "url-http")
1489 (url-https-create-secure-wrapper file-attributes (url &optional id-format))
1493 ;;; url-http.el ends here