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