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