Update FSF's address.
[bpt/emacs.git] / lisp / url / url.el
CommitLineData
8c8b8430 1;;; url.el --- Uniform Resource Locator retrieval tool
42b369cd 2
944b2ab6
SM
3;; Copyright (c) 1996, 1997, 1998, 1999, 2001, 2004, 2005
4;; Free Software Foundation, Inc.
42b369cd 5
8c8b8430 6;; Author: Bill Perry <wmperry@gnu.org>
8c8b8430
SM
7;; Keywords: comm, data, processes, hypermedia
8
42b369cd
SM
9;; This file is part of GNU Emacs.
10;;
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15;;
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20;;
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
4fc5845f
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
42b369cd
SM
25
26;;; Commentary:
8c8b8430
SM
27
28;; Registered URI schemes: http://www.iana.org/assignments/uri-schemes
29
42b369cd
SM
30;;; Code:
31
8c8b8430
SM
32(eval-when-compile (require 'cl))
33;; Don't require CL at runtime if we can avoid it (Emacs 21).
34;; Otherwise we need it for hashing functions. `puthash' was never
35;; defined in the Emacs 20 cl.el for some reason.
36(if (fboundp 'puthash)
37 nil ; internal or CL is loaded
38 (defalias 'puthash 'cl-puthash)
39 (autoload 'cl-puthash "cl")
40 (autoload 'gethash "cl")
41 (autoload 'maphash "cl")
42 (autoload 'make-hash-table "cl"))
43
44(eval-when-compile
45 (require 'mm-decode)
46 (require 'mm-view))
47
48(require 'mailcap)
49(require 'url-vars)
50(require 'url-cookie)
51(require 'url-history)
52(require 'url-expand)
53(require 'url-privacy)
54(require 'url-methods)
55(require 'url-proxy)
56(require 'url-parse)
57(require 'url-util)
58
59;; Fixme: customize? convert-standard-filename?
60;;;###autoload
61(defvar url-configuration-directory "~/.url")
62
63(defun url-do-setup ()
64 "Setup the url package.
65This is to avoid conflict with user settings if URL is dumped with
66Emacs."
67 (unless url-setup-done
68
69 ;; Make OS/2 happy
70 ;;(push '("http" "80") tcp-binary-process-input-services)
71
72 (mailcap-parse-mailcaps)
73 (mailcap-parse-mimetypes)
74
75 ;; Register all the authentication schemes we can handle
76 (url-register-auth-scheme "basic" nil 4)
77 (url-register-auth-scheme "digest" nil 7)
78
79 (setq url-cookie-file
80 (or url-cookie-file
81 (expand-file-name "cookies" url-configuration-directory)))
82
83 (setq url-history-file
84 (or url-history-file
85 (expand-file-name "history" url-configuration-directory)))
86
87 ;; Parse the global history file if it exists, so that it can be used
88 ;; for URL completion, etc.
89 (url-history-parse-history)
90 (url-history-setup-save-timer)
91
92 ;; Ditto for cookies
93 (url-cookie-setup-save-timer)
94 (url-cookie-parse-file url-cookie-file)
95
96 ;; Read in proxy gateways
97 (let ((noproxy (and (not (assoc "no_proxy" url-proxy-services))
98 (or (getenv "NO_PROXY")
99 (getenv "no_PROXY")
100 (getenv "no_proxy")))))
101 (if noproxy
102 (setq url-proxy-services
103 (cons (cons "no_proxy"
104 (concat "\\("
105 (mapconcat
106 (lambda (x)
107 (cond
108 ((= x ?,) "\\|")
109 ((= x ? ) "")
110 ((= x ?.) (regexp-quote "."))
111 ((= x ?*) ".*")
112 ((= x ??) ".")
113 (t (char-to-string x))))
114 noproxy "") "\\)"))
115 url-proxy-services))))
116
8c8b8430
SM
117 (url-setup-privacy-info)
118 (run-hooks 'url-load-hook)
119 (setq url-setup-done t)))
120
121;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122;;; Retrieval functions
123;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124(defun url-retrieve (url callback &optional cbargs)
125 "Retrieve URL asynchronously and call CALLBACK with CBARGS when finished.
126The callback is called when the object has been completely retrieved, with
127the current buffer containing the object, and any MIME headers associated
128with it. URL is either a string or a parsed URL.
129
130Return the buffer URL will load into, or nil if the process has
131already completed."
132 (url-do-setup)
133 (url-gc-dead-buffers)
134 (if (stringp url)
135 (set-text-properties 0 (length url) nil url))
136 (if (not (vectorp url))
137 (setq url (url-generic-parse-url url)))
138 (if (not (functionp callback))
139 (error "Must provide a callback function to url-retrieve"))
140 (unless (url-type url)
141 (error "Bad url: %s" (url-recreate-url url)))
142 (let ((loader (url-scheme-get-property (url-type url) 'loader))
143 (url-using-proxy (if (url-host url)
144 (url-find-proxy-for-url url (url-host url))))
145 (buffer nil)
146 (asynch (url-scheme-get-property (url-type url) 'asynchronous-p)))
147 (if url-using-proxy
148 (setq asynch t
149 loader 'url-proxy))
150 (if asynch
151 (setq buffer (funcall loader url callback cbargs))
152 (setq buffer (funcall loader url))
153 (if buffer
42b369cd 154 (with-current-buffer buffer
8c8b8430
SM
155 (apply callback cbargs))))
156 (url-history-update-url url (current-time))
157 buffer))
158
159(defun url-retrieve-synchronously (url)
160 "Retrieve URL synchronously.
161Return the buffer containing the data, or nil if there are no data
162associated with it (the case for dired, info, or mailto URLs that need
163no further processing). URL is either a string or a parsed URL."
164 (url-do-setup)
165
166 (lexical-let ((retrieval-done nil)
167 (asynch-buffer nil))
168 (setq asynch-buffer
169 (url-retrieve url (lambda (&rest ignored)
170 (url-debug 'retrieval "Synchronous fetching done (%S)" (current-buffer))
171 (setq retrieval-done t
172 asynch-buffer (current-buffer)))))
7f954571
SM
173 (if (null asynch-buffer)
174 ;; We do not need to do anything, it was a mailto or something
175 ;; similar that takes processing completely outside of the URL
176 ;; package.
177 nil
178 (let ((proc (get-buffer-process asynch-buffer)))
179 ;; If the access method was synchronous, `retrieval-done' should
180 ;; hopefully already be set to t. If it is nil, and `proc' is also
181 ;; nil, it implies that the async process is not running in
182 ;; asynch-buffer. This happens e.g. for FTP files. In such a case
183 ;; url-file.el should probably set something like a `url-process'
184 ;; buffer-local variable so we can find the exact process that we
185 ;; should be waiting for. In the mean time, we'll just wait for any
186 ;; process output.
944b2ab6
SM
187 (while (not retrieval-done)
188 (url-debug 'retrieval
189 "Spinning in url-retrieve-synchronously: %S (%S)"
190 retrieval-done asynch-buffer)
7f954571
SM
191 (if (and proc (memq (process-status proc)
192 '(closed exit signal failed)))
799fba8f
SM
193 ;; FIXME: It's not clear whether url-retrieve's callback is
194 ;; guaranteed to be called or not. It seems that url-http
195 ;; decides sometimes consciously not to call it, so it's not
196 ;; clear that it's a bug, but even if we need to decide how
197 ;; url-http can then warn us that the download has completed.
198 ;; In the mean time, we use this here workaround.
199 (setq retrieval-done t)
200 ;; We used to use `sit-for' here, but in some cases it wouldn't
201 ;; work because apparently pending keyboard input would always
202 ;; interrupt it before it got a chance to handle process input.
203 ;; `sleep-for' was tried but it lead to other forms of
204 ;; hanging. --Stef
7f954571 205 (unless (or (accept-process-output proc) (null proc))
799fba8f
SM
206 ;; accept-process-output returned nil, maybe because the process
207 ;; exited (and may have been replaced with another).
208 (setq proc (get-buffer-process asynch-buffer))))))
8c8b8430
SM
209 asynch-buffer)))
210
211(defun url-mm-callback (&rest ignored)
212 (let ((handle (mm-dissect-buffer t)))
7f954571
SM
213 (url-mark-buffer-as-dead (current-buffer))
214 (with-current-buffer
215 (generate-new-buffer (url-recreate-url url-current-object))
8c8b8430
SM
216 (if (eq (mm-display-part handle) 'external)
217 (progn
218 (set-process-sentinel
219 ;; Fixme: this shouldn't have to know the form of the
220 ;; undisplayer produced by `mm-display-part'.
221 (get-buffer-process (cdr (mm-handle-undisplayer handle)))
222 `(lambda (proc event)
223 (mm-destroy-parts (quote ,handle))))
224 (message "Viewing externally")
225 (kill-buffer (current-buffer)))
226 (display-buffer (current-buffer))
14e25c87
MY
227 (add-hook 'kill-buffer-hook
228 `(lambda () (mm-destroy-parts ',handle))
229 nil
230 t)))))
8c8b8430
SM
231
232(defun url-mm-url (url)
233 "Retrieve URL and pass to the appropriate viewing application."
234 (require 'mm-decode)
235 (require 'mm-view)
236 (url-retrieve url 'url-mm-callback nil))
237
238;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
239;;; Miscellaneous
240;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
241(defvar url-dead-buffer-list nil)
242
243(defun url-mark-buffer-as-dead (buff)
244 (push buff url-dead-buffer-list))
245
246(defun url-gc-dead-buffers ()
247 (let ((buff))
248 (while (setq buff (pop url-dead-buffer-list))
249 (if (buffer-live-p buff)
250 (kill-buffer buff)))))
251
252(cond
253 ((fboundp 'display-warning)
254 (defalias 'url-warn 'display-warning))
255 ((fboundp 'warn)
256 (defun url-warn (class message &optional level)
257 (warn "(%s/%s) %s" class (or level 'warning) message)))
258 (t
259 (defun url-warn (class message &optional level)
42b369cd 260 (with-current-buffer (get-buffer-create "*URL-WARNINGS*")
8c8b8430
SM
261 (goto-char (point-max))
262 (save-excursion
263 (insert (format "(%s/%s) %s\n" class (or level 'warning) message)))
264 (display-buffer (current-buffer))))))
265
266(provide 'url)
267
42b369cd 268;; arch-tag: bc182f1f-d187-4f10-b961-47af2066579a
8c8b8430 269;;; url.el ends here