Commit | Line | Data |
---|---|---|
8c8b8430 SM |
1 | ;;; url.el --- Uniform Resource Locator retrieval tool |
2 | ;; Author: Bill Perry <wmperry@gnu.org> | |
3 | ;; Version: $Revision: 1.15 $ | |
4 | ;; Keywords: comm, data, processes, hypermedia | |
5 | ||
6 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
7 | ;;; Copyright (c) 1993 - 1996 by William M. Perry <wmperry@cs.indiana.edu> | |
8 | ;;; Copyright (c) 1996, 97, 98, 99, 2001 Free Software Foundation, Inc. | |
9 | ;;; | |
10 | ;;; This file is part of GNU Emacs. | |
11 | ;;; | |
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 2, or (at your option) | |
15 | ;;; any later version. | |
16 | ;;; | |
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. | |
21 | ;;; | |
22 | ;;; You should have received a copy of the GNU General Public License | |
23 | ;;; along with GNU Emacs; see the file COPYING. If not, write to the | |
24 | ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 | ;;; Boston, MA 02111-1307, USA. | |
26 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
27 | ||
28 | ;; Registered URI schemes: http://www.iana.org/assignments/uri-schemes | |
29 | ||
30 | (eval-when-compile (require 'cl)) | |
31 | ;; Don't require CL at runtime if we can avoid it (Emacs 21). | |
32 | ;; Otherwise we need it for hashing functions. `puthash' was never | |
33 | ;; defined in the Emacs 20 cl.el for some reason. | |
34 | (if (fboundp 'puthash) | |
35 | nil ; internal or CL is loaded | |
36 | (defalias 'puthash 'cl-puthash) | |
37 | (autoload 'cl-puthash "cl") | |
38 | (autoload 'gethash "cl") | |
39 | (autoload 'maphash "cl") | |
40 | (autoload 'make-hash-table "cl")) | |
41 | ||
42 | (eval-when-compile | |
43 | (require 'mm-decode) | |
44 | (require 'mm-view)) | |
45 | ||
46 | (require 'mailcap) | |
47 | (require 'url-vars) | |
48 | (require 'url-cookie) | |
49 | (require 'url-history) | |
50 | (require 'url-expand) | |
51 | (require 'url-privacy) | |
52 | (require 'url-methods) | |
53 | (require 'url-proxy) | |
54 | (require 'url-parse) | |
55 | (require 'url-util) | |
56 | ||
57 | ;; Fixme: customize? convert-standard-filename? | |
58 | ;;;###autoload | |
59 | (defvar url-configuration-directory "~/.url") | |
60 | ||
61 | (defun url-do-setup () | |
62 | "Setup the url package. | |
63 | This is to avoid conflict with user settings if URL is dumped with | |
64 | Emacs." | |
65 | (unless url-setup-done | |
66 | ||
67 | ;; Make OS/2 happy | |
68 | ;;(push '("http" "80") tcp-binary-process-input-services) | |
69 | ||
70 | (mailcap-parse-mailcaps) | |
71 | (mailcap-parse-mimetypes) | |
72 | ||
73 | ;; Register all the authentication schemes we can handle | |
74 | (url-register-auth-scheme "basic" nil 4) | |
75 | (url-register-auth-scheme "digest" nil 7) | |
76 | ||
77 | (setq url-cookie-file | |
78 | (or url-cookie-file | |
79 | (expand-file-name "cookies" url-configuration-directory))) | |
80 | ||
81 | (setq url-history-file | |
82 | (or url-history-file | |
83 | (expand-file-name "history" url-configuration-directory))) | |
84 | ||
85 | ;; Parse the global history file if it exists, so that it can be used | |
86 | ;; for URL completion, etc. | |
87 | (url-history-parse-history) | |
88 | (url-history-setup-save-timer) | |
89 | ||
90 | ;; Ditto for cookies | |
91 | (url-cookie-setup-save-timer) | |
92 | (url-cookie-parse-file url-cookie-file) | |
93 | ||
94 | ;; Read in proxy gateways | |
95 | (let ((noproxy (and (not (assoc "no_proxy" url-proxy-services)) | |
96 | (or (getenv "NO_PROXY") | |
97 | (getenv "no_PROXY") | |
98 | (getenv "no_proxy"))))) | |
99 | (if noproxy | |
100 | (setq url-proxy-services | |
101 | (cons (cons "no_proxy" | |
102 | (concat "\\(" | |
103 | (mapconcat | |
104 | (lambda (x) | |
105 | (cond | |
106 | ((= x ?,) "\\|") | |
107 | ((= x ? ) "") | |
108 | ((= x ?.) (regexp-quote ".")) | |
109 | ((= x ?*) ".*") | |
110 | ((= x ??) ".") | |
111 | (t (char-to-string x)))) | |
112 | noproxy "") "\\)")) | |
113 | url-proxy-services)))) | |
114 | ||
115 | ;; Set the password entry funtion based on user defaults or guess | |
116 | ;; based on which remote-file-access package they are using. | |
117 | (cond | |
118 | (url-passwd-entry-func nil) ; Already been set | |
119 | ((fboundp 'read-passwd) ; Use secure password if available | |
120 | (setq url-passwd-entry-func 'read-passwd)) | |
121 | ((or (featurep 'efs) ; Using EFS | |
122 | (featurep 'efs-auto)) ; or autoloading efs | |
123 | (if (not (fboundp 'read-passwd)) | |
124 | (autoload 'read-passwd "passwd" "Read in a password" nil)) | |
125 | (setq url-passwd-entry-func 'read-passwd)) | |
126 | ((or (featurep 'ange-ftp) ; Using ange-ftp | |
127 | (and (boundp 'file-name-handler-alist) | |
128 | (not (featurep 'xemacs)))) ; ?? | |
129 | (setq url-passwd-entry-func 'ange-ftp-read-passwd)) | |
130 | (t | |
131 | (url-warn | |
132 | 'security | |
133 | "(url-setup): Can't determine how to read passwords, winging it."))) | |
134 | ||
135 | (url-setup-privacy-info) | |
136 | (run-hooks 'url-load-hook) | |
137 | (setq url-setup-done t))) | |
138 | ||
139 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
140 | ;;; Retrieval functions | |
141 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
142 | (defun url-retrieve (url callback &optional cbargs) | |
143 | "Retrieve URL asynchronously and call CALLBACK with CBARGS when finished. | |
144 | The callback is called when the object has been completely retrieved, with | |
145 | the current buffer containing the object, and any MIME headers associated | |
146 | with it. URL is either a string or a parsed URL. | |
147 | ||
148 | Return the buffer URL will load into, or nil if the process has | |
149 | already completed." | |
150 | (url-do-setup) | |
151 | (url-gc-dead-buffers) | |
152 | (if (stringp url) | |
153 | (set-text-properties 0 (length url) nil url)) | |
154 | (if (not (vectorp url)) | |
155 | (setq url (url-generic-parse-url url))) | |
156 | (if (not (functionp callback)) | |
157 | (error "Must provide a callback function to url-retrieve")) | |
158 | (unless (url-type url) | |
159 | (error "Bad url: %s" (url-recreate-url url))) | |
160 | (let ((loader (url-scheme-get-property (url-type url) 'loader)) | |
161 | (url-using-proxy (if (url-host url) | |
162 | (url-find-proxy-for-url url (url-host url)))) | |
163 | (buffer nil) | |
164 | (asynch (url-scheme-get-property (url-type url) 'asynchronous-p))) | |
165 | (if url-using-proxy | |
166 | (setq asynch t | |
167 | loader 'url-proxy)) | |
168 | (if asynch | |
169 | (setq buffer (funcall loader url callback cbargs)) | |
170 | (setq buffer (funcall loader url)) | |
171 | (if buffer | |
172 | (save-excursion | |
173 | (set-buffer buffer) | |
174 | (apply callback cbargs)))) | |
175 | (url-history-update-url url (current-time)) | |
176 | buffer)) | |
177 | ||
178 | (defun url-retrieve-synchronously (url) | |
179 | "Retrieve URL synchronously. | |
180 | Return the buffer containing the data, or nil if there are no data | |
181 | associated with it (the case for dired, info, or mailto URLs that need | |
182 | no further processing). URL is either a string or a parsed URL." | |
183 | (url-do-setup) | |
184 | ||
185 | (lexical-let ((retrieval-done nil) | |
186 | (asynch-buffer nil)) | |
187 | (setq asynch-buffer | |
188 | (url-retrieve url (lambda (&rest ignored) | |
189 | (url-debug 'retrieval "Synchronous fetching done (%S)" (current-buffer)) | |
190 | (setq retrieval-done t | |
191 | asynch-buffer (current-buffer))))) | |
192 | (if (not asynch-buffer) | |
193 | ;; We do not need to do anything, it was a mailto or something | |
194 | ;; similar that takes processing completely outside of the URL | |
195 | ;; package. | |
196 | nil | |
197 | (while (not retrieval-done) | |
198 | (url-debug 'retrieval "Spinning in url-retrieve-synchronously: %S (%S)" | |
199 | retrieval-done asynch-buffer) | |
200 | ;; Quoth monnier: | |
201 | ;; It turns out that the problem seems to be that the (sit-for | |
202 | ;; 0.1) below doesn't actually process the data: instead it | |
203 | ;; returns immediately because there is keyboard input | |
204 | ;; waiting, so we end up spinning endlessly waiting for the | |
205 | ;; process to finish while not letting it finish. | |
206 | ||
207 | ;; However, raman claims that it blocks Emacs with Emacspeak | |
208 | ;; for unexplained reasons. Put back for his benefit until | |
209 | ;; someone can understand it. | |
210 | ;; (sleep-for 0.1) | |
211 | (sit-for 0.1)) | |
212 | asynch-buffer))) | |
213 | ||
214 | (defun url-mm-callback (&rest ignored) | |
215 | (let ((handle (mm-dissect-buffer t))) | |
216 | (save-excursion | |
217 | (url-mark-buffer-as-dead (current-buffer)) | |
218 | (set-buffer (generate-new-buffer (url-recreate-url url-current-object))) | |
219 | (if (eq (mm-display-part handle) 'external) | |
220 | (progn | |
221 | (set-process-sentinel | |
222 | ;; Fixme: this shouldn't have to know the form of the | |
223 | ;; undisplayer produced by `mm-display-part'. | |
224 | (get-buffer-process (cdr (mm-handle-undisplayer handle))) | |
225 | `(lambda (proc event) | |
226 | (mm-destroy-parts (quote ,handle)))) | |
227 | (message "Viewing externally") | |
228 | (kill-buffer (current-buffer))) | |
229 | (display-buffer (current-buffer)) | |
230 | (mm-destroy-parts handle))))) | |
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) | |
260 | (save-excursion | |
261 | (set-buffer (get-buffer-create "*URL-WARNINGS*")) | |
262 | (goto-char (point-max)) | |
263 | (save-excursion | |
264 | (insert (format "(%s/%s) %s\n" class (or level 'warning) message))) | |
265 | (display-buffer (current-buffer)))))) | |
266 | ||
267 | (provide 'url) | |
268 | ||
269 | ;;; url.el ends here |