Mercurial > emacs
annotate lisp/url/url.el @ 57447:9984b3382a7a
*** empty log message ***
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Tue, 12 Oct 2004 07:37:37 +0000 |
parents | 01934125951e |
children | f2c7aff145a0 47f53c5c9620 |
rev | line source |
---|---|
54695 | 1 ;;; url.el --- Uniform Resource Locator retrieval tool |
54832
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
2 |
57427 | 3 ;; Copyright (c) 1996,1997,1998,1999,2001,2004 Free Software Foundation, Inc. |
54832
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
4 |
54695 | 5 ;; Author: Bill Perry <wmperry@gnu.org> |
6 ;; Keywords: comm, data, processes, hypermedia | |
7 | |
54832
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
8 ;; This file is part of GNU Emacs. |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
9 ;; |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
11 ;; it under the terms of the GNU General Public License as published by |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
12 ;; the Free Software Foundation; either version 2, or (at your option) |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
13 ;; any later version. |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
14 ;; |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
15 ;; GNU Emacs is distributed in the hope that it will be useful, |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
18 ;; GNU General Public License for more details. |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
19 ;; |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
20 ;; You should have received a copy of the GNU General Public License |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
23 ;; Boston, MA 02111-1307, USA. |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
24 |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
25 ;;; Commentary: |
54695 | 26 |
27 ;; Registered URI schemes: http://www.iana.org/assignments/uri-schemes | |
28 | |
54832
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
29 ;;; Code: |
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
30 |
54695 | 31 (eval-when-compile (require 'cl)) |
32 ;; Don't require CL at runtime if we can avoid it (Emacs 21). | |
33 ;; Otherwise we need it for hashing functions. `puthash' was never | |
34 ;; defined in the Emacs 20 cl.el for some reason. | |
35 (if (fboundp 'puthash) | |
36 nil ; internal or CL is loaded | |
37 (defalias 'puthash 'cl-puthash) | |
38 (autoload 'cl-puthash "cl") | |
39 (autoload 'gethash "cl") | |
40 (autoload 'maphash "cl") | |
41 (autoload 'make-hash-table "cl")) | |
42 | |
43 (eval-when-compile | |
44 (require 'mm-decode) | |
45 (require 'mm-view)) | |
46 | |
47 (require 'mailcap) | |
48 (require 'url-vars) | |
49 (require 'url-cookie) | |
50 (require 'url-history) | |
51 (require 'url-expand) | |
52 (require 'url-privacy) | |
53 (require 'url-methods) | |
54 (require 'url-proxy) | |
55 (require 'url-parse) | |
56 (require 'url-util) | |
57 | |
58 ;; Fixme: customize? convert-standard-filename? | |
59 ;;;###autoload | |
60 (defvar url-configuration-directory "~/.url") | |
61 | |
62 (defun url-do-setup () | |
63 "Setup the url package. | |
64 This is to avoid conflict with user settings if URL is dumped with | |
65 Emacs." | |
66 (unless url-setup-done | |
67 | |
68 ;; Make OS/2 happy | |
69 ;;(push '("http" "80") tcp-binary-process-input-services) | |
70 | |
71 (mailcap-parse-mailcaps) | |
72 (mailcap-parse-mimetypes) | |
73 | |
74 ;; Register all the authentication schemes we can handle | |
75 (url-register-auth-scheme "basic" nil 4) | |
76 (url-register-auth-scheme "digest" nil 7) | |
77 | |
78 (setq url-cookie-file | |
79 (or url-cookie-file | |
80 (expand-file-name "cookies" url-configuration-directory))) | |
81 | |
82 (setq url-history-file | |
83 (or url-history-file | |
84 (expand-file-name "history" url-configuration-directory))) | |
85 | |
86 ;; Parse the global history file if it exists, so that it can be used | |
87 ;; for URL completion, etc. | |
88 (url-history-parse-history) | |
89 (url-history-setup-save-timer) | |
90 | |
91 ;; Ditto for cookies | |
92 (url-cookie-setup-save-timer) | |
93 (url-cookie-parse-file url-cookie-file) | |
94 | |
95 ;; Read in proxy gateways | |
96 (let ((noproxy (and (not (assoc "no_proxy" url-proxy-services)) | |
97 (or (getenv "NO_PROXY") | |
98 (getenv "no_PROXY") | |
99 (getenv "no_proxy"))))) | |
100 (if noproxy | |
101 (setq url-proxy-services | |
102 (cons (cons "no_proxy" | |
103 (concat "\\(" | |
104 (mapconcat | |
105 (lambda (x) | |
106 (cond | |
107 ((= x ?,) "\\|") | |
108 ((= x ? ) "") | |
109 ((= x ?.) (regexp-quote ".")) | |
110 ((= x ?*) ".*") | |
111 ((= x ??) ".") | |
112 (t (char-to-string x)))) | |
113 noproxy "") "\\)")) | |
114 url-proxy-services)))) | |
115 | |
116 ;; Set the password entry funtion based on user defaults or guess | |
117 ;; based on which remote-file-access package they are using. | |
118 (cond | |
119 (url-passwd-entry-func nil) ; Already been set | |
120 ((fboundp 'read-passwd) ; Use secure password if available | |
121 (setq url-passwd-entry-func 'read-passwd)) | |
122 ((or (featurep 'efs) ; Using EFS | |
123 (featurep 'efs-auto)) ; or autoloading efs | |
124 (if (not (fboundp 'read-passwd)) | |
125 (autoload 'read-passwd "passwd" "Read in a password" nil)) | |
126 (setq url-passwd-entry-func 'read-passwd)) | |
127 ((or (featurep 'ange-ftp) ; Using ange-ftp | |
128 (and (boundp 'file-name-handler-alist) | |
129 (not (featurep 'xemacs)))) ; ?? | |
130 (setq url-passwd-entry-func 'ange-ftp-read-passwd)) | |
131 (t | |
132 (url-warn | |
133 'security | |
134 "(url-setup): Can't determine how to read passwords, winging it."))) | |
135 | |
136 (url-setup-privacy-info) | |
137 (run-hooks 'url-load-hook) | |
138 (setq url-setup-done t))) | |
139 | |
140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
141 ;;; Retrieval functions | |
142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
143 (defun url-retrieve (url callback &optional cbargs) | |
144 "Retrieve URL asynchronously and call CALLBACK with CBARGS when finished. | |
145 The callback is called when the object has been completely retrieved, with | |
146 the current buffer containing the object, and any MIME headers associated | |
147 with it. URL is either a string or a parsed URL. | |
148 | |
149 Return the buffer URL will load into, or nil if the process has | |
150 already completed." | |
151 (url-do-setup) | |
152 (url-gc-dead-buffers) | |
153 (if (stringp url) | |
154 (set-text-properties 0 (length url) nil url)) | |
155 (if (not (vectorp url)) | |
156 (setq url (url-generic-parse-url url))) | |
157 (if (not (functionp callback)) | |
158 (error "Must provide a callback function to url-retrieve")) | |
159 (unless (url-type url) | |
160 (error "Bad url: %s" (url-recreate-url url))) | |
161 (let ((loader (url-scheme-get-property (url-type url) 'loader)) | |
162 (url-using-proxy (if (url-host url) | |
163 (url-find-proxy-for-url url (url-host url)))) | |
164 (buffer nil) | |
165 (asynch (url-scheme-get-property (url-type url) 'asynchronous-p))) | |
166 (if url-using-proxy | |
167 (setq asynch t | |
168 loader 'url-proxy)) | |
169 (if asynch | |
170 (setq buffer (funcall loader url callback cbargs)) | |
171 (setq buffer (funcall loader url)) | |
172 (if buffer | |
54832
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
173 (with-current-buffer buffer |
54695 | 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) | |
54832
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
200 ;; Quoth Stef: |
54695 | 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) | |
54832
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
260 (with-current-buffer (get-buffer-create "*URL-WARNINGS*") |
54695 | 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 | |
54832
8a894d554fb1
(url-retrieve): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
268 ;; arch-tag: bc182f1f-d187-4f10-b961-47af2066579a |
54695 | 269 ;;; url.el ends here |