1 ;;; url-http.el --- HTTP retrieval routines
3 ;; Copyright (C) 1999, 2001, 2004-2015 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/>.
33 (defvar url-callback-arguments)
34 (defvar url-callback-function)
35 (defvar url-current-object)
36 (defvar url-http-after-change-function)
37 (defvar url-http-chunked-counter)
38 (defvar url-http-chunked-length)
39 (defvar url-http-chunked-start)
40 (defvar url-http-connection-opened)
41 (defvar url-http-content-length)
42 (defvar url-http-content-type)
43 (defvar url-http-data)
44 (defvar url-http-end-of-headers)
45 (defvar url-http-extra-headers)
46 (defvar url-http-noninteractive)
47 (defvar url-http-method)
48 (defvar url-http-no-retry)
49 (defvar url-http-process)
50 (defvar url-http-proxy)
51 (defvar url-http-response-status)
52 (defvar url-http-response-version)
53 (defvar url-http-target-url)
54 (defvar url-http-transfer-encoding)
55 (defvar url-show-status)
63 (autoload 'url-cache-create-filename "url-cache")
65 (defconst url-http-default-port 80 "Default HTTP port.")
66 (defconst url-http-asynchronous-p t "HTTP retrievals are asynchronous.")
67 (defalias 'url-http-expand-file-name 'url-default-expander)
69 (defvar url-http-real-basic-auth-storage nil)
70 (defvar url-http-proxy-basic-auth-storage nil)
72 (defvar url-http-open-connections (make-hash-table :test 'equal
74 "A hash table of all open network connections.")
76 (defvar url-http-version "1.1"
77 "What version of HTTP we advertise, as a string.
78 Valid values are 1.1 and 1.0.
79 This is only useful when debugging the HTTP subsystem.
81 Setting this to 1.0 will tell servers not to send chunked encoding,
82 and other HTTP/1.1 specific features.")
84 (defvar url-http-attempt-keepalives t
85 "Whether to use a single TCP connection multiple times in HTTP.
86 This is only useful when debugging the HTTP subsystem. Setting to
87 nil will explicitly close the connection to the server after every
90 (defconst url-http-codes
91 '((100 continue "Continue with request")
92 (101 switching-protocols "Switching protocols")
93 (102 processing "Processing (Added by DAV)")
95 (201 created "Created")
96 (202 accepted "Accepted")
97 (203 non-authoritative "Non-authoritative information")
98 (204 no-content "No content")
99 (205 reset-content "Reset content")
100 (206 partial-content "Partial content")
101 (207 multi-status "Multi-status (Added by DAV)")
102 (300 multiple-choices "Multiple choices")
103 (301 moved-permanently "Moved permanently")
105 (303 see-other "See other")
106 (304 not-modified "Not modified")
107 (305 use-proxy "Use proxy")
108 (307 temporary-redirect "Temporary redirect")
109 (400 bad-request "Bad Request")
110 (401 unauthorized "Unauthorized")
111 (402 payment-required "Payment required")
112 (403 forbidden "Forbidden")
113 (404 not-found "Not found")
114 (405 method-not-allowed "Method not allowed")
115 (406 not-acceptable "Not acceptable")
116 (407 proxy-authentication-required "Proxy authentication required")
117 (408 request-timeout "Request time-out")
118 (409 conflict "Conflict")
120 (411 length-required "Length required")
121 (412 precondition-failed "Precondition failed")
122 (413 request-entity-too-large "Request entity too large")
123 (414 request-uri-too-large "Request-URI too large")
124 (415 unsupported-media-type "Unsupported media type")
125 (416 requested-range-not-satisfiable "Requested range not satisfiable")
126 (417 expectation-failed "Expectation failed")
127 (422 unprocessable-entity "Unprocessable Entity (Added by DAV)")
128 (423 locked "Locked")
129 (424 failed-Dependency "Failed Dependency")
130 (500 internal-server-error "Internal server error")
131 (501 not-implemented "Not implemented")
132 (502 bad-gateway "Bad gateway")
133 (503 service-unavailable "Service unavailable")
134 (504 gateway-timeout "Gateway time-out")
135 (505 http-version-not-supported "HTTP version not supported")
136 (507 insufficient-storage "Insufficient storage"))
137 "The HTTP return codes and their text.")
140 ;; These are all macros so that they are hidden from external sight
141 ;; when the file is byte-compiled.
143 ;; This allows us to expose just the entry points we want.
145 ;; These routines will allow us to implement persistent HTTP
147 (defsubst url-http-debug (&rest args)
149 (let ((proc (get-buffer-process (current-buffer))))
150 ;; The user hit C-g, honor it! Some things can get in an
151 ;; incredibly tight loop (chunked encoding)
154 (set-process-sentinel proc nil)
155 (set-process-filter proc nil)))
156 (error "Transfer interrupted!")))
157 (apply 'url-debug 'http args))
159 (defun url-http-mark-connection-as-busy (host port proc)
160 (url-http-debug "Marking connection as busy: %s:%d %S" host port proc)
161 (set-process-query-on-exit-flag proc t)
162 (puthash (cons host port)
163 (delq proc (gethash (cons host port) url-http-open-connections))
164 url-http-open-connections)
167 (defun url-http-mark-connection-as-free (host port proc)
168 (url-http-debug "Marking connection as free: %s:%d %S" host port proc)
169 (when (memq (process-status proc) '(open run connect))
170 (set-process-buffer proc nil)
171 (set-process-sentinel proc 'url-http-idle-sentinel)
172 (set-process-query-on-exit-flag proc nil)
173 (puthash (cons host port)
174 (cons proc (gethash (cons host port) url-http-open-connections))
175 url-http-open-connections))
178 (defun url-http-find-free-connection (host port &optional gateway-method)
179 (let ((conns (gethash (cons host port) url-http-open-connections))
181 (while (and conns (not connection))
182 (if (not (memq (process-status (car conns)) '(run open connect)))
184 (url-http-debug "Cleaning up dead process: %s:%d %S"
185 host port (car conns))
186 (url-http-idle-sentinel (car conns) nil))
187 (setq connection (car conns))
188 (url-http-debug "Found existing connection: %s:%d %S" host port connection))
191 (url-http-debug "Reusing existing connection: %s:%d" host port)
192 (url-http-debug "Contacting host: %s:%d" host port))
193 (url-lazy-message "Contacting host: %s:%d" host port)
196 (let ((buf (generate-new-buffer " *url-http-temp*")))
197 ;; `url-open-stream' needs a buffer in which to do things
198 ;; like authentication. But we use another buffer afterwards.
200 (let ((proc (url-open-stream host buf host port gateway-method)))
201 ;; url-open-stream might return nil.
202 (when (processp proc)
203 ;; Drop the temp buffer link before killing the buffer.
204 (set-process-buffer proc nil)
205 (setq connection proc)))
206 ;; If there was an error on connect, make sure we don't
208 (when (get-buffer-process buf)
209 (set-process-query-on-exit-flag (get-buffer-process buf) nil))
213 (url-http-mark-connection-as-busy host port connection))))
215 ;; Building an HTTP request
216 (defun url-http-user-agent-string ()
217 (if (or (eq url-privacy-level 'paranoid)
218 (and (listp url-privacy-level)
219 (memq 'agent url-privacy-level)))
221 (if (functionp url-user-agent)
222 (funcall url-user-agent)
225 (defun url-http-create-request (&optional ref-url)
226 "Create an HTTP request for `url-http-target-url', referred to by REF-URL."
227 (let* ((extra-headers)
229 (no-cache (cdr-safe (assoc "Pragma" url-http-extra-headers)))
230 (using-proxy url-http-proxy)
231 (proxy-auth (if (or (cdr-safe (assoc "Proxy-Authorization"
232 url-http-extra-headers))
235 (let ((url-basic-auth-storage
236 'url-http-proxy-basic-auth-storage))
237 (url-get-authentication url-http-proxy nil 'any nil))))
238 (real-fname (url-filename url-http-target-url))
239 (host (url-host url-http-target-url))
240 (auth (if (cdr-safe (assoc "Authorization" url-http-extra-headers))
242 (url-get-authentication (or
243 (and (boundp 'proxy-info)
245 url-http-target-url) nil 'any nil))))
246 (if (equal "" real-fname)
247 (setq real-fname "/"))
248 (setq no-cache (and no-cache (string-match "no-cache" no-cache)))
250 (setq auth (concat "Authorization: " auth "\r\n")))
252 (setq proxy-auth (concat "Proxy-Authorization: " proxy-auth "\r\n")))
254 ;; Protection against stupid values in the referrer
255 (if (and ref-url (stringp ref-url) (or (string= ref-url "file:nil")
256 (string= ref-url "")))
259 ;; We do not want to expose the referrer if the user is paranoid.
260 (if (or (memq url-privacy-level '(low high paranoid))
261 (and (listp url-privacy-level)
262 (memq 'lastloc url-privacy-level)))
265 ;; url-http-extra-headers contains an assoc-list of
266 ;; header/value pairs that we need to put into the request.
267 (setq extra-headers (mapconcat
269 (concat (car x) ": " (cdr x)))
270 url-http-extra-headers "\r\n"))
271 (if (not (equal extra-headers ""))
272 (setq extra-headers (concat extra-headers "\r\n")))
274 ;; This was done with a call to `format'. Concatenating parts has
275 ;; the advantage of keeping the parts of each header together and
276 ;; allows us to elide null lines directly, at the cost of making
277 ;; the layout less clear.
279 ;; We used to concat directly, but if one of the strings happens
280 ;; to being multibyte (even if it only contains pure ASCII) then
281 ;; every string gets converted with `string-MAKE-multibyte' which
282 ;; turns the 127-255 codes into things like latin-1 accented chars
283 ;; (it would work right if it used `string-TO-multibyte' instead).
284 ;; So to avoid the problem we force every string to be unibyte.
286 ;; FIXME: Instead of `string-AS-unibyte' we'd want
287 ;; `string-to-unibyte', so as to properly signal an error if one
288 ;; of the strings contains a multibyte char.
293 (or url-http-method "GET") " "
294 (if using-proxy (url-recreate-url url-http-target-url) real-fname)
295 " HTTP/" url-http-version "\r\n"
296 ;; Version of MIME we speak
297 "MIME-Version: 1.0\r\n"
298 ;; (maybe) Try to keep the connection open
299 "Connection: " (if (or using-proxy
300 (not url-http-attempt-keepalives))
301 "close" "keep-alive") "\r\n"
302 ;; HTTP extensions we support
303 (if url-extensions-header
305 "Extension: %s\r\n" url-extensions-header))
306 ;; Who we want to talk to
307 (if (/= (url-port url-http-target-url)
308 (url-scheme-get-property
309 (url-type url-http-target-url) 'default-port))
311 "Host: %s:%d\r\n" (puny-encode-domain host)
312 (url-port url-http-target-url))
313 (format "Host: %s\r\n" (puny-encode-domain host)))
315 (if url-personal-mail-address
317 "From: " url-personal-mail-address "\r\n"))
318 ;; Encodings we understand
319 (if (or url-mime-encoding-string
320 ;; MS-Windows loads zlib dynamically, so recheck
321 ;; in case they made it available since
322 ;; initialization in url-vars.el.
323 (and (eq 'system-type 'windows-nt)
324 (fboundp 'zlib-available-p)
326 (setq url-mime-encoding-string "gzip")))
328 "Accept-encoding: " url-mime-encoding-string "\r\n"))
329 (if url-mime-charset-string
331 "Accept-charset: " url-mime-charset-string "\r\n"))
332 ;; Languages we understand
333 (if url-mime-language-string
335 "Accept-language: " url-mime-language-string "\r\n"))
336 ;; Types we understand
337 "Accept: " (or url-mime-accept-string "*/*") "\r\n"
339 (url-http-user-agent-string)
340 ;; Proxy Authorization
345 (when (url-use-cookies url-http-target-url)
346 (url-cookie-generate-header-lines
348 (equal "https" (url-type url-http-target-url))))
350 (if (and (not no-cache)
351 (member url-http-method '("GET" nil)))
352 (let ((tm (url-is-cached url-http-target-url)))
354 (concat "If-modified-since: "
355 (url-get-normalized-date tm) "\r\n"))))
358 "Referer: " ref-url "\r\n"))
363 "Content-length: " (number-to-string
364 (length url-http-data))
371 (url-http-debug "Request is: \n%s" request)
375 (defun url-http-clean-headers ()
376 "Remove trailing \r from header lines.
377 This allows us to use `mail-fetch-field', etc.
378 Return the number of characters removed."
379 (let ((end (marker-position url-http-end-of-headers)))
380 (goto-char (point-min))
381 (while (re-search-forward "\r$" url-http-end-of-headers t)
383 (- end url-http-end-of-headers)))
385 (defun url-http-handle-authentication (proxy)
386 (url-http-debug "Handling %s authentication" (if proxy "proxy" "normal"))
387 (let ((auths (or (nreverse
389 (if proxy "proxy-authenticate" "www-authenticate")
393 (url (url-recreate-url url-current-object))
394 (auth-url (url-recreate-url
395 (if (and proxy (boundp 'url-http-proxy))
397 url-current-object)))
398 (url-basic-auth-storage (if proxy
399 ;; Cheating, but who cares? :)
400 'url-http-proxy-basic-auth-storage
401 'url-http-real-basic-auth-storage))
405 ;; find strongest supported auth
406 (dolist (this-auth auths)
407 (setq this-auth (url-eat-trailing-space
408 (url-strip-leading-spaces
411 (downcase (if (string-match "[ \t]" this-auth)
412 (substring this-auth 0 (match-beginning 0))
414 (registered (url-auth-registered this-type))
415 (this-strength (cddr registered)))
416 (when (and registered (> this-strength strength))
419 strength this-strength))))
421 (if (not (url-auth-registered type))
424 (goto-char (point-max))
425 (insert "<hr>Sorry, but I do not know how to handle " (or type auth url "")
426 " authentication. If you'd like to write it,"
427 " please use M-x report-emacs-bug RET.<hr>")
428 ;; We used to set a `status' var (declared "special") but I can't
429 ;; find the corresponding let-binding, so it's probably an error.
430 ;; FIXME: Maybe it was supposed to set `success', i.e. to return t?
432 nil) ;; Not success yet.
434 (let* ((args (url-parse-args (subst-char-in-string ?, ?\; auth)))
435 (auth (url-get-authentication auth-url
436 (cdr-safe (assoc "realm" args))
440 (push (cons (if proxy "Proxy-Authorization" "Authorization") auth)
441 url-http-extra-headers)
442 (let ((url-request-method url-http-method)
443 (url-request-data url-http-data)
444 (url-request-extra-headers url-http-extra-headers))
445 (url-retrieve-internal url url-callback-function
446 url-callback-arguments))
447 nil))))) ;; Not success yet.
449 (defun url-http-parse-response ()
450 "Parse just the response code."
451 (if (not url-http-end-of-headers)
452 (error "Trying to parse HTTP response code in odd buffer: %s" (buffer-name)))
453 (url-http-debug "url-http-parse-response called in (%s)" (buffer-name))
454 (goto-char (point-min))
455 (skip-chars-forward " \t\n") ; Skip any blank crap
456 (skip-chars-forward "HTTP/") ; Skip HTTP Version
457 (setq url-http-response-version
458 (buffer-substring (point)
460 (skip-chars-forward "[0-9].")
462 (setq url-http-response-status (read (current-buffer))))
464 (defun url-http-handle-cookies ()
465 "Handle all set-cookie / set-cookie2 headers in an HTTP response.
466 The buffer must already be narrowed to the headers, so `mail-fetch-field' will
468 (let ((cookies (nreverse (mail-fetch-field "Set-Cookie" nil nil t)))
469 (cookies2 (nreverse (mail-fetch-field "Set-Cookie2" nil nil t))))
470 (and cookies (url-http-debug "Found %d Set-Cookie headers" (length cookies)))
471 (and cookies2 (url-http-debug "Found %d Set-Cookie2 headers" (length cookies2)))
473 (url-cookie-handle-set-cookie (pop cookies)))
475 ;;; (url-cookie-handle-set-cookie2 (pop cookies)))
479 (declare-function gnutls-peer-status "gnutls.c" (proc))
481 (defun url-http-parse-headers ()
482 "Parse and handle HTTP specific headers.
483 Return t if and only if the current buffer is still active and
484 should be shown to the user."
485 ;; The comments after each status code handled are taken from RFC
487 (url-http-mark-connection-as-free (url-host url-current-object)
488 (url-port url-current-object)
490 ;; Pass the https certificate on to the caller.
491 (when (gnutls-available-p)
492 (let ((status (gnutls-peer-status url-http-process)))
494 (plist-get (car url-callback-arguments) :peer))
495 (setcar url-callback-arguments
496 (plist-put (car url-callback-arguments)
498 (if (or (not (boundp 'url-http-end-of-headers))
499 (not url-http-end-of-headers))
500 (error "Trying to parse headers in odd buffer: %s" (buffer-name)))
501 (goto-char (point-min))
502 (url-http-debug "url-http-parse-headers called in (%s)" (buffer-name))
503 (url-http-parse-response)
504 (mail-narrow-to-head)
505 ;;(narrow-to-region (point-min) url-http-end-of-headers)
506 (let ((connection (mail-fetch-field "Connection")))
507 ;; In HTTP 1.0, keep the connection only if there is a
508 ;; "Connection: keep-alive" header.
509 ;; In HTTP 1.1 (and greater), keep the connection unless there is a
510 ;; "Connection: close" header
512 ((string= url-http-response-version "1.0")
513 (unless (and connection
514 (string= (downcase connection) "keep-alive"))
515 (delete-process url-http-process)))
517 (when (and connection
518 (string= (downcase connection) "close"))
519 (delete-process url-http-process)))))
520 (let* ((buffer (current-buffer))
521 (class (/ url-http-response-status 100))
523 ;; other status symbols: jewelry and luxury cars
524 (status-symbol (cadr (assq url-http-response-status url-http-codes))))
525 (url-http-debug "Parsed HTTP headers: class=%d status=%d"
526 class url-http-response-status)
527 (when (url-use-cookies url-http-target-url)
528 (url-http-handle-cookies))
531 ;; Classes of response codes
533 ;; 5xx = Server Error
534 ;; 4xx = Client Error
537 ;; 1xx = Informational
538 (1 ; Information messages
539 ;; 100 = Continue with request
540 ;; 101 = Switching protocols
541 ;; 102 = Processing (Added by DAV)
542 (url-mark-buffer-as-dead buffer)
543 (error "HTTP responses in class 1xx not supported (%d)"
544 url-http-response-status))
549 ;; 203 Non-authoritative information
552 ;; 206 Partial content
553 ;; 207 Multi-status (Added by DAV)
555 ((or `no-content `reset-content)
556 ;; No new data, just stay at the same document
557 (url-mark-buffer-as-dead buffer))
559 ;; Generic success for all others. Store in the cache, and
560 ;; mark it as successful.
562 (if (and url-automatic-caching (equal url-http-method "GET"))
563 (url-store-in-cache buffer))))
566 ;; 300 Multiple choices
567 ;; 301 Moved permanently
572 ;; 307 Temporary redirect
573 (let ((redirect-uri (or (mail-fetch-field "Location")
574 (mail-fetch-field "URI"))))
576 (`multiple-choices ; 300
577 ;; Quoth the spec (section 10.3.1)
578 ;; -------------------------------
579 ;; The requested resource corresponds to any one of a set of
580 ;; representations, each with its own specific location and
581 ;; agent-driven negotiation information is being provided so
582 ;; that the user can select a preferred representation and
583 ;; redirect its request to that location.
585 ;; If the server has a preferred choice of representation, it
586 ;; SHOULD include the specific URI for that representation in
587 ;; the Location field; user agents MAY use the Location field
588 ;; value for automatic redirection.
589 ;; -------------------------------
590 ;; We do not support agent-driven negotiation, so we just
591 ;; redirect to the preferred URI if one is provided.
593 ((or `moved-permanently `found `temporary-redirect) ; 301 302 307
594 ;; If the 301|302 status code is received in response to a
595 ;; request other than GET or HEAD, the user agent MUST NOT
596 ;; automatically redirect the request unless it can be
597 ;; confirmed by the user, since this might change the
598 ;; conditions under which the request was issued.
599 (unless (member url-http-method '("HEAD" "GET"))
600 (setq redirect-uri nil)))
602 ;; The response to the request can be found under a different
603 ;; URI and SHOULD be retrieved using a GET method on that
605 (setq url-http-method "GET"
608 ;; The 304 response MUST NOT contain a message-body.
609 (url-http-debug "Extracting document from cache... (%s)"
610 (url-cache-create-filename (url-view-url t)))
611 (url-cache-extract (url-cache-create-filename (url-view-url t)))
612 (setq redirect-uri nil
615 ;; The requested resource MUST be accessed through the
616 ;; proxy given by the Location field. The Location field
617 ;; gives the URI of the proxy. The recipient is expected
618 ;; to repeat this single request via the proxy. 305
619 ;; responses MUST only be generated by origin servers.
620 (error "Redirection thru a proxy server not supported: %s"
623 ;; Treat everything like '300'
626 ;; Clean off any whitespace and/or <...> cruft.
627 (if (string-match "\\([^ \t]+\\)[ \t]" redirect-uri)
628 (setq redirect-uri (match-string 1 redirect-uri)))
629 (if (string-match "^<\\(.*\\)>$" redirect-uri)
630 (setq redirect-uri (match-string 1 redirect-uri)))
632 ;; Some stupid sites (like sourceforge) send a
633 ;; non-fully-qualified URL (ie: /), which royally confuses
635 (if (not (string-match url-nonrelative-link redirect-uri))
636 ;; Be careful to use the real target URL, otherwise we may
637 ;; compute the redirection relative to the URL of the proxy.
639 (url-expand-file-name redirect-uri url-http-target-url)))
640 ;; Do not automatically include an authorization header in the
641 ;; redirect. If needed it will be regenerated by the relevant
642 ;; auth scheme when the new request happens.
643 (setq url-http-extra-headers
644 (cl-remove "Authorization"
645 url-http-extra-headers :key 'car :test 'equal))
646 (let ((url-request-method url-http-method)
647 (url-request-data url-http-data)
648 (url-request-extra-headers url-http-extra-headers))
649 ;; Check existing number of redirects
650 (if (or (< url-max-redirections 0)
651 (and (> url-max-redirections 0)
652 (let ((events (car url-callback-arguments))
655 (if (eq (car events) :redirect)
656 (setq old-redirects (1+ old-redirects)))
657 (and (setq events (cdr events))
658 (setq events (cdr events))))
659 (< old-redirects url-max-redirections))))
660 ;; url-max-redirections hasn't been reached, so go
661 ;; ahead and redirect.
663 ;; Remember that the request was redirected.
664 (setf (car url-callback-arguments)
665 (nconc (list :redirect redirect-uri)
666 (car url-callback-arguments)))
667 ;; Put in the current buffer a forwarding pointer to the new
668 ;; destination buffer.
669 ;; FIXME: This is a hack to fix url-retrieve-synchronously
670 ;; without changing the API. Instead url-retrieve should
671 ;; either simply not return the "destination" buffer, or it
672 ;; should take an optional `dest-buf' argument.
673 (set (make-local-variable 'url-redirect-buffer)
674 (url-retrieve-internal
675 redirect-uri url-callback-function
676 url-callback-arguments
677 (url-silent url-current-object)
678 (not (url-use-cookies url-current-object))))
679 (url-mark-buffer-as-dead buffer))
680 ;; We hit url-max-redirections, so issue an error and
682 (url-http-debug "Maximum redirections reached")
683 (setf (car url-callback-arguments)
684 (nconc (list :error (list 'error 'http-redirect-limit
686 (car url-callback-arguments)))
687 (setq success t))))))
691 ;; 402 Payment required
694 ;; 405 Method not allowed
695 ;; 406 Not acceptable
696 ;; 407 Proxy authentication required
697 ;; 408 Request time-out
700 ;; 411 Length required
701 ;; 412 Precondition failed
702 ;; 413 Request entity too large
703 ;; 414 Request-URI too large
704 ;; 415 Unsupported media type
705 ;; 416 Requested range not satisfiable
706 ;; 417 Expectation failed
707 ;; 422 Unprocessable Entity (Added by DAV)
709 ;; 424 Failed Dependency
713 ;; The request requires user authentication. The response
714 ;; MUST include a WWW-Authenticate header field containing a
715 ;; challenge applicable to the requested resource. The
716 ;; client MAY repeat the request with a suitable
717 ;; Authorization header field.
718 (url-http-handle-authentication nil))
719 (`payment-required ; 402
720 ;; This code is reserved for future use
721 (url-mark-buffer-as-dead buffer)
722 (error "Somebody wants you to give them money"))
724 ;; The server understood the request, but is refusing to
725 ;; fulfill it. Authorization will not help and the request
726 ;; SHOULD NOT be repeated.
731 (`method-not-allowed ; 405
732 ;; The method specified in the Request-Line is not allowed
733 ;; for the resource identified by the Request-URI. The
734 ;; response MUST include an Allow header containing a list of
735 ;; valid methods for the requested resource.
737 (`not-acceptable ; 406
738 ;; The resource identified by the request is only capable of
739 ;; generating response entities which have content
740 ;; characteristics not acceptable according to the accept
741 ;; headers sent in the request.
743 (`proxy-authentication-required ; 407
744 ;; This code is similar to 401 (Unauthorized), but indicates
745 ;; that the client must first authenticate itself with the
746 ;; proxy. The proxy MUST return a Proxy-Authenticate header
747 ;; field containing a challenge applicable to the proxy for
748 ;; the requested resource.
749 (url-http-handle-authentication t))
750 (`request-timeout ; 408
751 ;; The client did not produce a request within the time that
752 ;; the server was prepared to wait. The client MAY repeat
753 ;; the request without modifications at any later time.
756 ;; The request could not be completed due to a conflict with
757 ;; the current state of the resource. This code is only
758 ;; allowed in situations where it is expected that the user
759 ;; might be able to resolve the conflict and resubmit the
760 ;; request. The response body SHOULD include enough
761 ;; information for the user to recognize the source of the
765 ;; The requested resource is no longer available at the
766 ;; server and no forwarding address is known.
768 (`length-required ; 411
769 ;; The server refuses to accept the request without a defined
770 ;; Content-Length. The client MAY repeat the request if it
771 ;; adds a valid Content-Length header field containing the
772 ;; length of the message-body in the request message.
774 ;; NOTE - this will never happen because
775 ;; `url-http-create-request' automatically calculates the
778 (`precondition-failed ; 412
779 ;; The precondition given in one or more of the
780 ;; request-header fields evaluated to false when it was
781 ;; tested on the server.
783 ((or `request-entity-too-large `request-uri-too-large) ; 413 414
784 ;; The server is refusing to process a request because the
785 ;; request entity|URI is larger than the server is willing or
788 (`unsupported-media-type ; 415
789 ;; The server is refusing to service the request because the
790 ;; entity of the request is in a format not supported by the
791 ;; requested resource for the requested method.
793 (`requested-range-not-satisfiable ; 416
794 ;; A server SHOULD return a response with this status code if
795 ;; a request included a Range request-header field, and none
796 ;; of the range-specifier values in this field overlap the
797 ;; current extent of the selected resource, and the request
798 ;; did not include an If-Range request-header field.
800 (`expectation-failed ; 417
801 ;; The expectation given in an Expect request-header field
802 ;; could not be met by this server, or, if the server is a
803 ;; proxy, the server has unambiguous evidence that the
804 ;; request could not be met by the next-hop server.
807 ;; The request could not be understood by the server due to
808 ;; malformed syntax. The client SHOULD NOT repeat the
809 ;; request without modifications.
811 ;; Tell the callback that an error occurred, and what the
814 (setf (car url-callback-arguments)
815 (nconc (list :error (list 'error 'http url-http-response-status))
816 (car url-callback-arguments)))))
818 ;; 500 Internal server error
819 ;; 501 Not implemented
821 ;; 503 Service unavailable
822 ;; 504 Gateway time-out
823 ;; 505 HTTP version not supported
824 ;; 507 Insufficient storage
826 (pcase url-http-response-status
827 (`not-implemented ; 501
828 ;; The server does not support the functionality required to
829 ;; fulfill the request.
832 ;; The server, while acting as a gateway or proxy, received
833 ;; an invalid response from the upstream server it accessed
834 ;; in attempting to fulfill the request.
836 (`service-unavailable ; 503
837 ;; The server is currently unable to handle the request due
838 ;; to a temporary overloading or maintenance of the server.
839 ;; The implication is that this is a temporary condition
840 ;; which will be alleviated after some delay. If known, the
841 ;; length of the delay MAY be indicated in a Retry-After
842 ;; header. If no Retry-After is given, the client SHOULD
843 ;; handle the response as it would for a 500 response.
845 (`gateway-timeout ; 504
846 ;; The server, while acting as a gateway or proxy, did not
847 ;; receive a timely response from the upstream server
848 ;; specified by the URI (e.g. HTTP, FTP, LDAP) or some other
849 ;; auxiliary server (e.g. DNS) it needed to access in
850 ;; attempting to complete the request.
852 (`http-version-not-supported ; 505
853 ;; The server does not support, or refuses to support, the
854 ;; HTTP protocol version that was used in the request
857 (`insufficient-storage ; 507 (DAV)
858 ;; The method could not be performed on the resource
859 ;; because the server is unable to store the representation
860 ;; needed to successfully complete the request. This
861 ;; condition is considered to be temporary. If the request
862 ;; which received this status code was the result of a user
863 ;; action, the request MUST NOT be repeated until it is
864 ;; requested by a separate user action.
866 ;; Tell the callback that an error occurred, and what the
869 (setf (car url-callback-arguments)
870 (nconc (list :error (list 'error 'http url-http-response-status))
871 (car url-callback-arguments)))))
873 (error "Unknown class of HTTP response code: %d (%d)"
874 class url-http-response-status)))
876 (url-mark-buffer-as-dead buffer)
877 (url-handle-content-transfer-encoding))
878 (url-http-debug "Finished parsing HTTP headers: %S" success)
880 (goto-char (point-min))
883 (declare-function zlib-decompress-region "decompress.c" (start end))
885 (defun url-handle-content-transfer-encoding ()
886 (let ((encoding (mail-fetch-field "content-encoding")))
888 (fboundp 'zlib-available-p)
890 (equal (downcase encoding) "gzip"))
893 (goto-char (point-min))
894 (when (search-forward "\n\n")
895 (zlib-decompress-region (point) (point-max)))))))
898 (defun url-http-activate-callback ()
899 "Activate callback specified when this buffer was created."
900 (url-http-mark-connection-as-free (url-host url-current-object)
901 (url-port url-current-object)
903 (url-http-debug "Activating callback in buffer (%s): %S %S"
904 (buffer-name) url-callback-function url-callback-arguments)
905 (apply url-callback-function url-callback-arguments))
909 ;; These unfortunately cannot be macros... please ignore them!
910 (defun url-http-idle-sentinel (proc why)
911 "Remove (now defunct) process PROC from the list of open connections."
912 (maphash (lambda (key val)
914 (puthash key (delq proc val) url-http-open-connections)))
915 url-http-open-connections))
917 (defun url-http-end-of-document-sentinel (proc why)
918 ;; Sentinel used to handle (i) terminated old HTTP/0.9 connections,
919 ;; and (ii) closed connection due to reusing a HTTP connection which
920 ;; we believed was still alive, but which the server closed on us.
921 ;; We handle case (ii) by calling `url-http' again.
922 (url-http-debug "url-http-end-of-document-sentinel in buffer (%s)"
923 (process-buffer proc))
924 (url-http-idle-sentinel proc why)
925 (when (buffer-name (process-buffer proc))
926 (with-current-buffer (process-buffer proc)
927 (goto-char (point-min))
928 (cond ((not (looking-at "HTTP/"))
929 (if url-http-no-retry
930 ;; HTTP/0.9 just gets passed back no matter what
931 (url-http-activate-callback)
932 ;; Call `url-http' again if our connection expired.
934 (let ((url-request-method url-http-method)
935 (url-request-extra-headers url-http-extra-headers)
936 (url-request-data url-http-data))
937 (url-http url-current-object url-callback-function
938 url-callback-arguments (current-buffer)))))
939 ((url-http-parse-headers)
940 (url-http-activate-callback))))))
942 (defun url-http-simple-after-change-function (st nd length)
943 ;; Function used when we do NOT know how long the document is going to be
944 ;; Just _very_ simple 'downloaded %d' type of info.
945 (url-lazy-message "Reading %s..." (file-size-human-readable nd)))
947 (defun url-http-content-length-after-change-function (st nd length)
948 "Function used when we DO know how long the document is going to be.
949 More sophisticated percentage downloaded, etc.
950 Also does minimal parsing of HTTP headers and will actually cause
951 the callback to be triggered."
952 (if url-http-content-type
953 (url-display-percentage
954 "Reading [%s]... %s of %s (%d%%)"
955 (url-percentage (- nd url-http-end-of-headers)
956 url-http-content-length)
957 url-http-content-type
958 (file-size-human-readable (- nd url-http-end-of-headers))
959 (file-size-human-readable url-http-content-length)
960 (url-percentage (- nd url-http-end-of-headers)
961 url-http-content-length))
962 (url-display-percentage
963 "Reading... %s of %s (%d%%)"
964 (url-percentage (- nd url-http-end-of-headers)
965 url-http-content-length)
966 (file-size-human-readable (- nd url-http-end-of-headers))
967 (file-size-human-readable url-http-content-length)
968 (url-percentage (- nd url-http-end-of-headers)
969 url-http-content-length)))
971 (if (> (- nd url-http-end-of-headers) url-http-content-length)
973 ;; Found the end of the document! Wheee!
974 (url-display-percentage nil nil)
975 (url-lazy-message "Reading... done.")
976 (if (url-http-parse-headers)
977 (url-http-activate-callback)))))
979 (defun url-http-chunked-encoding-after-change-function (st nd length)
980 "Function used when dealing with chunked encoding.
981 Cannot give a sophisticated percentage, but we need a different
982 function to look for the special 0-length chunk that signifies
983 the end of the document."
986 (let ((read-next-chunk t)
989 (no-initial-crlf nil))
990 ;; We need to loop thru looking for more chunks even within
991 ;; one after-change-function call.
992 (while read-next-chunk
993 (setq no-initial-crlf (= 0 url-http-chunked-counter))
994 (if url-http-content-type
995 (url-display-percentage nil
996 "Reading [%s]... chunk #%d"
997 url-http-content-type url-http-chunked-counter)
998 (url-display-percentage nil
999 "Reading... chunk #%d"
1000 url-http-chunked-counter))
1001 (url-http-debug "Reading chunk %d (%d %d %d)"
1002 url-http-chunked-counter st nd length)
1003 (setq regexp (if no-initial-crlf
1004 "\\([0-9a-z]+\\).*\r?\n"
1005 "\r?\n\\([0-9a-z]+\\).*\r?\n"))
1007 (if url-http-chunked-start
1008 ;; We know how long the chunk is supposed to be, skip over
1009 ;; leading crap if possible.
1010 (if (> nd (+ url-http-chunked-start url-http-chunked-length))
1012 (url-http-debug "Got to the end of chunk #%d!"
1013 url-http-chunked-counter)
1014 (goto-char (+ url-http-chunked-start
1015 url-http-chunked-length)))
1016 (url-http-debug "Still need %d bytes to hit end of chunk"
1017 (- (+ url-http-chunked-start
1018 url-http-chunked-length)
1020 (setq read-next-chunk nil)))
1021 (if (not read-next-chunk)
1022 (url-http-debug "Still spinning for next chunk...")
1023 (if no-initial-crlf (skip-chars-forward "\r\n"))
1024 (if (not (looking-at regexp))
1026 ;; Must not have received the entirety of the chunk header,
1027 ;; need to spin some more.
1028 (url-http-debug "Did not see start of chunk @ %d!" (point))
1029 (setq read-next-chunk nil))
1030 (add-text-properties (match-beginning 0) (match-end 0)
1036 (setq url-http-chunked-length (string-to-number (buffer-substring
1040 url-http-chunked-counter (1+ url-http-chunked-counter)
1041 url-http-chunked-start (set-marker
1042 (or url-http-chunked-start
1045 ; (if (not url-http-debug)
1046 (delete-region (match-beginning 0) (match-end 0));)
1047 (url-http-debug "Saw start of chunk %d (length=%d, start=%d"
1048 url-http-chunked-counter url-http-chunked-length
1049 (marker-position url-http-chunked-start))
1050 (if (= 0 url-http-chunked-length)
1052 ;; Found the end of the document! Wheee!
1053 (url-http-debug "Saw end of stream chunk!")
1054 (setq read-next-chunk nil)
1055 (url-display-percentage nil nil)
1056 ;; Every chunk, even the last 0-length one, is
1057 ;; terminated by CRLF. Skip it.
1058 (when (looking-at "\r?\n")
1059 (url-http-debug "Removing terminator of last chunk")
1060 (delete-region (match-beginning 0) (match-end 0)))
1061 (if (re-search-forward "^\r?\n" nil t)
1062 (url-http-debug "Saw end of trailers..."))
1063 (if (url-http-parse-headers)
1064 (url-http-activate-callback))))))))))
1066 (defun url-http-wait-for-headers-change-function (st nd length)
1067 ;; This will wait for the headers to arrive and then splice in the
1068 ;; next appropriate after-change-function, etc.
1069 (url-http-debug "url-http-wait-for-headers-change-function (%s)"
1071 (let ((end-of-headers nil)
1073 (process-buffer (current-buffer))
1074 (content-length nil))
1076 (goto-char (point-min))
1077 (if (and (looking-at ".*\n") ; have one line at least
1078 (not (looking-at "^HTTP/[1-9]\\.[0-9]")))
1079 ;; Not HTTP/x.y data, must be 0.9
1080 ;; God, I wish this could die.
1081 (setq end-of-headers t
1082 url-http-end-of-headers 0
1084 ;; Blank line at end of headers.
1085 (when (re-search-forward "^\r?\n" nil t)
1087 ;; Saw the end of the headers
1088 (url-http-debug "Saw end of headers... (%s)" (buffer-name))
1089 (setq url-http-end-of-headers (set-marker (make-marker)
1092 (setq nd (- nd (url-http-clean-headers)))))
1094 (if (not end-of-headers)
1095 ;; Haven't seen the end of the headers yet, need to wait
1096 ;; for more data to arrive.
1099 (url-http-parse-response)
1100 (mail-narrow-to-head)
1101 (setq url-http-transfer-encoding (mail-fetch-field
1102 "transfer-encoding")
1103 url-http-content-type (mail-fetch-field "content-type"))
1104 (if (mail-fetch-field "content-length")
1105 (setq url-http-content-length
1106 (string-to-number (mail-fetch-field "content-length"))))
1108 (when url-http-transfer-encoding
1109 (setq url-http-transfer-encoding
1110 (downcase url-http-transfer-encoding)))
1113 ((null url-http-response-status)
1114 ;; We got back a headerless malformed response from the
1116 (url-http-activate-callback))
1117 ((or (= url-http-response-status 204)
1118 (= url-http-response-status 205))
1119 (url-http-debug "%d response must have headers only (%s)."
1120 url-http-response-status (buffer-name))
1121 (when (url-http-parse-headers)
1122 (url-http-activate-callback)))
1123 ((string= "HEAD" url-http-method)
1124 ;; A HEAD request is _ALWAYS_ terminated by the header
1125 ;; information, regardless of any entity headers,
1126 ;; according to section 4.4 of the HTTP/1.1 draft.
1127 (url-http-debug "HEAD request must have headers only (%s)."
1129 (when (url-http-parse-headers)
1130 (url-http-activate-callback)))
1131 ((string= "CONNECT" url-http-method)
1132 ;; A CONNECT request is finished, but we cannot stick this
1133 ;; back on the free connection list
1134 (url-http-debug "CONNECT request must have headers only.")
1135 (when (url-http-parse-headers)
1136 (url-http-activate-callback)))
1137 ((equal url-http-response-status 304)
1138 ;; Only allowed to have a header section. We have to handle
1139 ;; this here instead of in url-http-parse-headers because if
1140 ;; you have a cached copy of something without a known
1141 ;; content-length, and try to retrieve it from the cache, we'd
1142 ;; fall into the 'being dumb' section and wait for the
1143 ;; connection to terminate, which means we'd wait for 10
1144 ;; seconds for the keep-alives to time out on some servers.
1145 (when (url-http-parse-headers)
1146 (url-http-activate-callback)))
1148 ;; HTTP/0.9 always signaled end-of-connection by closing the
1151 "Saw HTTP/0.9 response, connection closed means end of document.")
1152 (setq url-http-after-change-function
1153 'url-http-simple-after-change-function))
1154 ((equal url-http-transfer-encoding "chunked")
1155 (url-http-debug "Saw chunked encoding.")
1156 (setq url-http-after-change-function
1157 'url-http-chunked-encoding-after-change-function)
1158 (when (> nd url-http-end-of-headers)
1160 "Calling initial chunked-encoding for extra data at end of headers")
1161 (url-http-chunked-encoding-after-change-function
1162 (marker-position url-http-end-of-headers) nd
1163 (- nd url-http-end-of-headers))))
1164 ((integerp url-http-content-length)
1166 "Got a content-length, being smart about document end.")
1167 (setq url-http-after-change-function
1168 'url-http-content-length-after-change-function)
1170 ((= 0 url-http-content-length)
1171 ;; We got a NULL body! Activate the callback
1174 "Got 0-length content-length, activating callback immediately.")
1175 (when (url-http-parse-headers)
1176 (url-http-activate-callback)))
1177 ((> nd url-http-end-of-headers)
1178 ;; Have some leftover data
1179 (url-http-debug "Calling initial content-length for extra data at end of headers")
1180 (url-http-content-length-after-change-function
1181 (marker-position url-http-end-of-headers)
1183 (- nd url-http-end-of-headers)))
1187 (url-http-debug "No content-length, being dumb.")
1188 (setq url-http-after-change-function
1189 'url-http-simple-after-change-function)))))
1190 ;; We are still at the beginning of the buffer... must just be
1191 ;; waiting for a response.
1192 (url-http-debug "Spinning waiting for headers...")
1193 (when (eq process-buffer (current-buffer))
1194 (goto-char (point-max)))))
1196 (defun url-http (url callback cbargs &optional retry-buffer gateway-method)
1197 "Retrieve URL via HTTP asynchronously.
1198 URL must be a parsed URL. See `url-generic-parse-url' for details.
1200 When retrieval is completed, execute the function CALLBACK,
1201 passing it an updated value of CBARGS as arguments. The first
1202 element in CBARGS should be a plist describing what has happened
1203 so far during the request, as described in the docstring of
1204 `url-retrieve' (if in doubt, specify nil). The current buffer
1205 then CALLBACK is executed is the retrieval buffer.
1207 Optional arg RETRY-BUFFER, if non-nil, specifies the buffer of a
1208 previous `url-http' call, which is being re-attempted.
1210 Optional arg GATEWAY-METHOD specifies the gateway to be used,
1211 overriding the value of `url-gateway-method'.
1213 The return value of this function is the retrieval buffer."
1214 (cl-check-type url vector "Need a pre-parsed URL.")
1215 (let* ((host (url-host (or url-using-proxy url)))
1216 (port (url-port (or url-using-proxy url)))
1217 (nsm-noninteractive (or url-request-noninteractive
1218 (and (boundp 'url-http-noninteractive)
1219 url-http-noninteractive)))
1220 (connection (url-http-find-free-connection host port gateway-method))
1221 (buffer (or retry-buffer
1222 (generate-new-buffer
1223 (format " *http %s:%d*" host port)))))
1224 (if (not connection)
1225 ;; Failed to open the connection for some reason
1227 (kill-buffer buffer)
1229 (error "Could not create connection to %s:%d" host port))
1230 (with-current-buffer buffer
1231 (mm-disable-multibyte)
1232 (setq url-current-object url
1233 mode-line-format "%b [%s]")
1235 (dolist (var '(url-http-end-of-headers
1236 url-http-content-type
1237 url-http-content-length
1238 url-http-transfer-encoding
1239 url-http-after-change-function
1240 url-http-response-version
1241 url-http-response-status
1242 url-http-chunked-length
1243 url-http-chunked-counter
1244 url-http-chunked-start
1245 url-callback-function
1246 url-callback-arguments
1250 url-http-extra-headers
1251 url-http-noninteractive
1255 url-http-connection-opened
1257 (set (make-local-variable var) nil))
1259 (setq url-http-method (or url-request-method "GET")
1260 url-http-extra-headers url-request-extra-headers
1261 url-http-noninteractive url-request-noninteractive
1262 url-http-data url-request-data
1263 url-http-process connection
1264 url-http-chunked-length nil
1265 url-http-chunked-start nil
1266 url-http-chunked-counter 0
1267 url-callback-function callback
1268 url-callback-arguments cbargs
1269 url-http-after-change-function 'url-http-wait-for-headers-change-function
1270 url-http-target-url url-current-object
1271 url-http-no-retry retry-buffer
1272 url-http-connection-opened nil
1273 url-http-proxy url-using-proxy)
1275 (set-process-buffer connection buffer)
1276 (set-process-filter connection 'url-http-generic-filter)
1277 (pcase (process-status connection)
1279 ;; Asynchronous connection
1280 (set-process-sentinel connection 'url-http-async-sentinel))
1282 ;; Asynchronous connection failed
1283 (error "Could not create connection to %s:%d" host port))
1285 (set-process-sentinel connection
1286 'url-http-end-of-document-sentinel)
1287 (process-send-string connection (url-http-create-request))))))
1290 (defun url-http-async-sentinel (proc why)
1291 ;; We are performing an asynchronous connection, and a status change
1293 (when (buffer-name (process-buffer proc))
1294 (with-current-buffer (process-buffer proc)
1296 (url-http-connection-opened
1297 (setq url-http-no-retry t)
1298 (url-http-end-of-document-sentinel proc why))
1299 ((string= (substring why 0 4) "open")
1300 (setq url-http-connection-opened t)
1301 (condition-case error
1302 (process-send-string proc (url-http-create-request))
1304 (setq url-http-connection-opened nil)
1305 (message "HTTP error: %s" error))))
1307 (setf (car url-callback-arguments)
1308 (nconc (list :error (list 'error 'connection-failed why
1309 :host (url-host (or url-http-proxy url-current-object))
1310 :service (url-port (or url-http-proxy url-current-object))))
1311 (car url-callback-arguments)))
1312 (url-http-activate-callback))))))
1314 ;; Since Emacs 19/20 does not allow you to change the
1315 ;; `after-change-functions' hook in the midst of running them, we fake
1316 ;; an after change by hooking into the process filter and inserting
1317 ;; the data ourselves. This is slightly less efficient, but there
1318 ;; were tons of weird ways the after-change code was biting us in the
1320 ;; FIXME this can probably be simplified since the above is no longer true.
1321 (defun url-http-generic-filter (proc data)
1322 ;; Sometimes we get a zero-length data chunk after the process has
1323 ;; been changed to 'free', which means it has no buffer associated
1324 ;; with it. Do nothing if there is no buffer, or 0 length data.
1325 (and (process-buffer proc)
1326 (/= (length data) 0)
1327 (with-current-buffer (process-buffer proc)
1328 (url-http-debug "Calling after change function `%s' for `%S'" url-http-after-change-function proc)
1329 (funcall url-http-after-change-function
1332 (goto-char (point-max))
1337 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1338 ;;; file-name-handler stuff from here on out
1339 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1340 (defalias 'url-http-symbol-value-in-buffer
1341 (if (fboundp 'symbol-value-in-buffer)
1342 'symbol-value-in-buffer
1343 (lambda (symbol buffer &optional unbound-value)
1344 "Return the value of SYMBOL in BUFFER, or UNBOUND-VALUE if it is unbound."
1345 (with-current-buffer buffer
1346 (if (not (boundp symbol))
1348 (symbol-value symbol))))))
1350 (defun url-http-head (url)
1351 (let ((url-request-method "HEAD")
1352 (url-request-data nil))
1353 (url-retrieve-synchronously url)))
1355 (defun url-http-file-exists-p (url)
1356 (let ((buffer (url-http-head url)))
1358 (let ((status (url-http-symbol-value-in-buffer 'url-http-response-status
1361 (and (integerp status)
1362 (>= status 200) (< status 300))
1363 (kill-buffer buffer))))))
1365 (defalias 'url-http-file-readable-p 'url-http-file-exists-p)
1367 (defun url-http-head-file-attributes (url &optional id-format)
1368 (let ((buffer (url-http-head url)))
1372 nil ;dir / link / normal file
1373 1 ;number of links to file.
1375 nil nil nil ;atime ; mtime ; ctime
1376 (url-http-symbol-value-in-buffer 'url-http-content-length
1378 (eval-when-compile (make-string 10 ?-))
1379 nil nil nil) ;whether gid would change ; inode ; device.
1380 (kill-buffer buffer)))))
1382 (declare-function url-dav-file-attributes "url-dav" (url &optional id-format))
1384 (defun url-http-file-attributes (url &optional id-format)
1385 (if (url-dav-supported-p url)
1386 (url-dav-file-attributes url id-format)
1387 (url-http-head-file-attributes url id-format)))
1389 (defun url-http-options (url)
1390 "Return a property list describing options available for URL.
1391 This list is retrieved using the `OPTIONS' HTTP method.
1393 Property list members:
1396 A list of symbols specifying what HTTP methods the resource
1400 A list of numbers specifying what DAV protocol/schema versions are
1404 A list of supported DASL search types supported (string form)
1407 A list of the units available for use in partial document fetches.
1410 The `Platform For Privacy Protection' description for the resource.
1411 Currently this is just the raw header contents. This is likely to
1412 change once P3P is formally supported by the URL package or
1414 (let* ((url-request-method "OPTIONS")
1415 (url-request-data nil)
1416 (buffer (url-retrieve-synchronously url))
1419 (when (and buffer (= 2 (/ (url-http-symbol-value-in-buffer
1420 'url-http-response-status buffer 0) 100)))
1421 ;; Only parse the options if we got a 2xx response code!
1422 (with-current-buffer buffer
1425 (mail-narrow-to-head)
1427 ;; Figure out what methods are supported.
1428 (when (setq header (mail-fetch-field "allow"))
1429 (setq options (plist-put
1431 (mapcar 'intern (split-string header "[ ,]+")))))
1434 (when (setq header (mail-fetch-field "dav"))
1435 (setq options (plist-put
1438 (mapcar 'string-to-number
1439 (split-string header "[, ]+"))))))
1442 (when (setq header (mail-fetch-field "dasl"))
1443 (setq options (plist-put
1445 (split-string header "[, ]+"))))
1447 ;; P3P - should get more detailed here. FIXME
1448 (when (setq header (mail-fetch-field "p3p"))
1449 (setq options (plist-put options 'p3p header)))
1451 ;; Check for whether they accept byte-range requests.
1452 (when (setq header (mail-fetch-field "accept-ranges"))
1453 (setq options (plist-put
1457 (split-string header "[, ]+"))))))
1459 (if buffer (kill-buffer buffer))
1462 ;; HTTPS. This used to be in url-https.el, but that file collides
1463 ;; with url-http.el on systems with 8-character file names.
1466 (defconst url-https-default-port 443 "Default HTTPS port.")
1467 (defconst url-https-asynchronous-p t "HTTPS retrievals are asynchronous.")
1469 ;; FIXME what is the point of this alias being an autoload?
1470 ;; Trying to use it will not cause url-http to be loaded,
1471 ;; since the full alias just gets dumped into loaddefs.el.
1473 ;;;###autoload (autoload 'url-default-expander "url-expand")
1475 (defalias 'url-https-expand-file-name 'url-default-expander)
1477 (defmacro url-https-create-secure-wrapper (method args)
1478 `(defun ,(intern (format (if method "url-https-%s" "url-https") method)) ,args
1479 ,(format "HTTPS wrapper around `%s' call." (or method "url-http"))
1480 (,(intern (format (if method "url-http-%s" "url-http") method))
1481 ,@(remove '&rest (remove '&optional (append args (if method nil '(nil 'tls))))))))
1483 ;;;###autoload (autoload 'url-https "url-http")
1484 (url-https-create-secure-wrapper nil (url callback cbargs))
1485 ;;;###autoload (autoload 'url-https-file-exists-p "url-http")
1486 (url-https-create-secure-wrapper file-exists-p (url))
1487 ;;;###autoload (autoload 'url-https-file-readable-p "url-http")
1488 (url-https-create-secure-wrapper file-readable-p (url))
1489 ;;;###autoload (autoload 'url-https-file-attributes "url-http")
1490 (url-https-create-secure-wrapper file-attributes (url &optional id-format))
1494 ;;; url-http.el ends here