]> code.delx.au - gnu-emacs/blob - lisp/url/url.el
b219151a30c8206c2726f061a43de49c7276de21
[gnu-emacs] / lisp / url / url.el
1 ;;; url.el --- Uniform Resource Locator retrieval tool -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1996-1999, 2001, 2004-2012 Free Software Foundation, Inc.
4
5 ;; Author: Bill Perry <wmperry@gnu.org>
6 ;; Keywords: comm, data, processes, hypermedia
7
8 ;; This file is part of GNU Emacs.
9 ;;
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; Registered URI schemes: http://www.iana.org/assignments/uri-schemes
26
27 ;;; Code:
28
29
30 (require 'mailcap)
31
32 (eval-when-compile
33 (require 'mm-decode)
34 (require 'mm-view))
35
36 (require 'url-vars)
37 (require 'url-cookie)
38 (require 'url-history)
39 (require 'url-expand)
40 (require 'url-privacy)
41 (require 'url-methods)
42 (require 'url-proxy)
43 (require 'url-parse)
44 (require 'url-util)
45
46
47 (defcustom url-configuration-directory
48 (locate-user-emacs-file "url/" ".url/")
49 "Directory used by the URL package for cookies, history, etc."
50 :type 'directory
51 :group 'url)
52
53 (defun url-do-setup ()
54 "Setup the URL package.
55 This is to avoid conflict with user settings if URL is dumped with
56 Emacs."
57 (unless url-setup-done
58
59 ;; Make OS/2 happy
60 ;;(push '("http" "80") tcp-binary-process-input-services)
61
62 (mailcap-parse-mailcaps)
63 (mailcap-parse-mimetypes)
64
65 ;; Register all the authentication schemes we can handle
66 (url-register-auth-scheme "basic" nil 4)
67 (url-register-auth-scheme "digest" nil 7)
68
69 (setq url-cookie-file
70 (or url-cookie-file
71 (expand-file-name "cookies" url-configuration-directory)))
72
73 (setq url-history-file
74 (or url-history-file
75 (expand-file-name "history" url-configuration-directory)))
76
77 ;; Parse the global history file if it exists, so that it can be used
78 ;; for URL completion, etc.
79 (url-history-parse-history)
80 (url-history-setup-save-timer)
81
82 ;; Ditto for cookies
83 (url-cookie-setup-save-timer)
84 (url-cookie-parse-file url-cookie-file)
85
86 ;; Read in proxy gateways
87 (let ((noproxy (and (not (assoc "no_proxy" url-proxy-services))
88 (or (getenv "NO_PROXY")
89 (getenv "no_PROXY")
90 (getenv "no_proxy")))))
91 (if noproxy
92 (setq url-proxy-services
93 (cons (cons "no_proxy"
94 (concat "\\("
95 (mapconcat
96 (lambda (x)
97 (cond
98 ((= x ?,) "\\|")
99 ((= x ? ) "")
100 ((= x ?.) (regexp-quote "."))
101 ((= x ?*) ".*")
102 ((= x ??) ".")
103 (t (char-to-string x))))
104 noproxy "") "\\)"))
105 url-proxy-services))))
106
107 (url-setup-privacy-info)
108 (run-hooks 'url-load-hook)
109 (setq url-setup-done t)))
110
111 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
112 ;;; Retrieval functions
113 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
114
115 (defvar url-redirect-buffer nil
116 "New buffer into which the retrieval will take place.
117 Sometimes while retrieving a URL, the URL library needs to use another buffer
118 than the one returned initially by `url-retrieve'. In this case, it sets this
119 variable in the original buffer as a forwarding pointer.")
120
121 (defvar url-retrieve-number-of-calls 0)
122 (autoload 'url-cache-prune-cache "url-cache")
123
124 ;;;###autoload
125 (defun url-retrieve (url callback &optional cbargs silent inhibit-cookies)
126 "Retrieve URL asynchronously and call CALLBACK with CBARGS when finished.
127 URL is either a string or a parsed URL. If it is a string
128 containing characters that are not valid in a URI, those
129 characters are percent-encoded; see `url-encode-url'.
130
131 CALLBACK is called when the object has been completely retrieved, with
132 the current buffer containing the object, and any MIME headers associated
133 with it. It is called as (apply CALLBACK STATUS CBARGS).
134 STATUS is a plist representing what happened during the request,
135 with most recent events first, or an empty list if no events have
136 occurred. Each pair is one of:
137
138 \(:redirect REDIRECTED-TO) - the request was redirected to this URL
139 \(:error (ERROR-SYMBOL . DATA)) - an error occurred. The error can be
140 signaled with (signal ERROR-SYMBOL DATA).
141
142 Return the buffer URL will load into, or nil if the process has
143 already completed (i.e. URL was a mailto URL or similar; in this case
144 the callback is not called).
145
146 The variables `url-request-data', `url-request-method' and
147 `url-request-extra-headers' can be dynamically bound around the
148 request; dynamic binding of other variables doesn't necessarily
149 take effect.
150
151 If SILENT, then don't message progress reports and the like.
152 If INHIBIT-COOKIES, cookies will neither be stored nor sent to
153 the server.
154 If URL is a multibyte string, it will be encoded as utf-8 and
155 URL-encoded before it's used."
156 ;;; XXX: There is code in Emacs that does dynamic binding
157 ;;; of the following variables around url-retrieve:
158 ;;; url-standalone-mode, url-gateway-unplugged, w3-honor-stylesheets,
159 ;;; url-confirmation-func, url-cookie-multiple-line,
160 ;;; url-cookie-{{,secure-}storage,confirmation}
161 ;;; url-standalone-mode and url-gateway-unplugged should work as
162 ;;; usual. url-confirmation-func is only used in nnwarchive.el and
163 ;;; webmail.el; the latter should be updated. Is
164 ;;; url-cookie-multiple-line needed anymore? The other url-cookie-*
165 ;;; are (for now) only used in synchronous retrievals.
166 (url-retrieve-internal url callback (cons nil cbargs) silent
167 inhibit-cookies))
168
169 (defun url-retrieve-internal (url callback cbargs &optional silent
170 inhibit-cookies)
171 "Internal function; external interface is `url-retrieve'.
172 CBARGS is the list of arguments that the callback function will
173 receive; its first element should be a plist specifying what has
174 happened so far during the request, as described in the docstring
175 of `url-retrieve' (if in doubt, specify nil).
176
177 If SILENT, don't message progress reports and the like.
178 If INHIBIT-COOKIES, cookies will neither be stored nor sent to
179 the server.
180 If URL is a multibyte string, it will be encoded as utf-8 and
181 URL-encoded before it's used."
182 (url-do-setup)
183 (url-gc-dead-buffers)
184 (when (stringp url)
185 (set-text-properties 0 (length url) nil url)
186 (setq url (url-encode-url url)))
187 (if (not (vectorp url))
188 (setq url (url-generic-parse-url url)))
189 (if (not (functionp callback))
190 (error "Must provide a callback function to url-retrieve"))
191 (unless (url-type url)
192 (error "Bad url: %s" (url-recreate-url url)))
193 (setf (url-silent url) silent)
194 (setf (url-use-cookies url) (not inhibit-cookies))
195 ;; Once in a while, remove old entries from the URL cache.
196 (when (zerop (% url-retrieve-number-of-calls 1000))
197 (condition-case error
198 (url-cache-prune-cache)
199 (file-error
200 (message "Error when expiring the cache: %s" error))))
201 (setq url-retrieve-number-of-calls (1+ url-retrieve-number-of-calls))
202 (let ((loader (url-scheme-get-property (url-type url) 'loader))
203 (url-using-proxy (if (url-host url)
204 (url-find-proxy-for-url url (url-host url))))
205 (buffer nil)
206 (asynch (url-scheme-get-property (url-type url) 'asynchronous-p)))
207 (if url-using-proxy
208 (setq asynch t
209 loader 'url-proxy))
210 (if asynch
211 (let ((url-current-object url))
212 (setq buffer (funcall loader url callback cbargs)))
213 (setq buffer (funcall loader url))
214 (if buffer
215 (with-current-buffer buffer
216 (apply callback cbargs))))
217 (if url-history-track
218 (url-history-update-url url (current-time)))
219 buffer))
220
221 ;;;###autoload
222 (defun url-retrieve-synchronously (url)
223 "Retrieve URL synchronously.
224 Return the buffer containing the data, or nil if there are no data
225 associated with it (the case for dired, info, or mailto URLs that need
226 no further processing). URL is either a string or a parsed URL."
227 (url-do-setup)
228
229 (let ((retrieval-done nil)
230 (asynch-buffer nil))
231 (setq asynch-buffer
232 (url-retrieve url (lambda (&rest ignored)
233 (url-debug 'retrieval "Synchronous fetching done (%S)" (current-buffer))
234 (setq retrieval-done t
235 asynch-buffer (current-buffer)))))
236 (if (null asynch-buffer)
237 ;; We do not need to do anything, it was a mailto or something
238 ;; similar that takes processing completely outside of the URL
239 ;; package.
240 nil
241 (let ((proc (get-buffer-process asynch-buffer)))
242 ;; If the access method was synchronous, `retrieval-done' should
243 ;; hopefully already be set to t. If it is nil, and `proc' is also
244 ;; nil, it implies that the async process is not running in
245 ;; asynch-buffer. This happens e.g. for FTP files. In such a case
246 ;; url-file.el should probably set something like a `url-process'
247 ;; buffer-local variable so we can find the exact process that we
248 ;; should be waiting for. In the mean time, we'll just wait for any
249 ;; process output.
250 (while (not retrieval-done)
251 (url-debug 'retrieval
252 "Spinning in url-retrieve-synchronously: %S (%S)"
253 retrieval-done asynch-buffer)
254 (if (buffer-local-value 'url-redirect-buffer asynch-buffer)
255 (setq proc (get-buffer-process
256 (setq asynch-buffer
257 (buffer-local-value 'url-redirect-buffer
258 asynch-buffer))))
259 (if (and proc (memq (process-status proc)
260 '(closed exit signal failed))
261 ;; Make sure another process hasn't been started.
262 (eq proc (or (get-buffer-process asynch-buffer) proc)))
263 ;; FIXME: It's not clear whether url-retrieve's callback is
264 ;; guaranteed to be called or not. It seems that url-http
265 ;; decides sometimes consciously not to call it, so it's not
266 ;; clear that it's a bug, but even then we need to decide how
267 ;; url-http can then warn us that the download has completed.
268 ;; In the mean time, we use this here workaround.
269 ;; XXX: The callback must always be called. Any
270 ;; exception is a bug that should be fixed, not worked
271 ;; around.
272 (progn ;; Call delete-process so we run any sentinel now.
273 (delete-process proc)
274 (setq retrieval-done t)))
275 ;; We used to use `sit-for' here, but in some cases it wouldn't
276 ;; work because apparently pending keyboard input would always
277 ;; interrupt it before it got a chance to handle process input.
278 ;; `sleep-for' was tried but it lead to other forms of
279 ;; hanging. --Stef
280 (unless (or (with-local-quit
281 (accept-process-output proc))
282 (null proc))
283 ;; accept-process-output returned nil, maybe because the process
284 ;; exited (and may have been replaced with another). If we got
285 ;; a quit, just stop.
286 (when quit-flag
287 (delete-process proc))
288 (setq proc (and (not quit-flag)
289 (get-buffer-process asynch-buffer)))))))
290 asynch-buffer)))
291
292 (defun url-mm-callback (&rest ignored)
293 (let ((handle (mm-dissect-buffer t)))
294 (url-mark-buffer-as-dead (current-buffer))
295 (with-current-buffer
296 (generate-new-buffer (url-recreate-url url-current-object))
297 (if (eq (mm-display-part handle) 'external)
298 (progn
299 (set-process-sentinel
300 ;; Fixme: this shouldn't have to know the form of the
301 ;; undisplayer produced by `mm-display-part'.
302 (get-buffer-process (cdr (mm-handle-undisplayer handle)))
303 `(lambda (proc event)
304 (mm-destroy-parts (quote ,handle))))
305 (message "Viewing externally")
306 (kill-buffer (current-buffer)))
307 (display-buffer (current-buffer))
308 (add-hook 'kill-buffer-hook
309 `(lambda () (mm-destroy-parts ',handle))
310 nil
311 t)))))
312
313 (defun url-mm-url (url)
314 "Retrieve URL and pass to the appropriate viewing application."
315 ;; These requires could advantageously be moved to url-mm-callback or
316 ;; turned into autoloads, but I suspect that it would introduce some bugs
317 ;; because loading those files from a process sentinel or filter may
318 ;; result in some undesirable corner cases.
319 (require 'mm-decode)
320 (require 'mm-view)
321 (url-retrieve url 'url-mm-callback nil))
322
323 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
324 ;;; Miscellaneous
325 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
326 (defvar url-dead-buffer-list nil)
327
328 (defun url-mark-buffer-as-dead (buff)
329 (push buff url-dead-buffer-list))
330
331 (defun url-gc-dead-buffers ()
332 (let ((buff))
333 (while (setq buff (pop url-dead-buffer-list))
334 (if (buffer-live-p buff)
335 (kill-buffer buff)))))
336
337 (cond
338 ((fboundp 'display-warning)
339 (defalias 'url-warn 'display-warning))
340 ((fboundp 'warn)
341 (defun url-warn (class message &optional level)
342 (warn "(%s/%s) %s" class (or level 'warning) message)))
343 (t
344 (defun url-warn (class message &optional level)
345 (with-current-buffer (get-buffer-create "*URL-WARNINGS*")
346 (goto-char (point-max))
347 (save-excursion
348 (insert (format "(%s/%s) %s\n" class (or level 'warning) message)))
349 (display-buffer (current-buffer))))))
350
351 (provide 'url)
352
353 ;;; url.el ends here