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