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