| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
(eval-when-compile (require 'cl)) |
|---|
| 33 |
|
|---|
| 34 |
(eval-when-compile |
|---|
| 35 |
(require 'mm-decode) |
|---|
| 36 |
(require 'mm-view)) |
|---|
| 37 |
|
|---|
| 38 |
(require 'mailcap) |
|---|
| 39 |
(require 'url-vars) |
|---|
| 40 |
(require 'url-cookie) |
|---|
| 41 |
(require 'url-history) |
|---|
| 42 |
(require 'url-expand) |
|---|
| 43 |
(require 'url-privacy) |
|---|
| 44 |
(require 'url-methods) |
|---|
| 45 |
(require 'url-proxy) |
|---|
| 46 |
(require 'url-parse) |
|---|
| 47 |
(require 'url-util) |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
(defvar url-configuration-directory |
|---|
| 51 |
(cond |
|---|
| 52 |
((file-directory-p "~/.url") "~/.url") |
|---|
| 53 |
((file-directory-p "~/.emacs.d") "~/.emacs.d/url") |
|---|
| 54 |
(t "~/.url"))) |
|---|
| 55 |
|
|---|
| 56 |
(defun url-do-setup () |
|---|
| 57 |
"Setup the url package. |
|---|
| 58 |
This is to avoid conflict with user settings if URL is dumped with |
|---|
| 59 |
Emacs." |
|---|
| 60 |
(unless url-setup-done |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
(mailcap-parse-mailcaps) |
|---|
| 66 |
(mailcap-parse-mimetypes) |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
(url-register-auth-scheme "basic" nil 4) |
|---|
| 70 |
(url-register-auth-scheme "digest" nil 7) |
|---|
| 71 |
|
|---|
| 72 |
(setq url-cookie-file |
|---|
| 73 |
(or url-cookie-file |
|---|
| 74 |
(expand-file-name "cookies" url-configuration-directory))) |
|---|
| 75 |
|
|---|
| 76 |
(setq url-history-file |
|---|
| 77 |
(or url-history-file |
|---|
| 78 |
(expand-file-name "history" url-configuration-directory))) |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
(url-history-parse-history) |
|---|
| 83 |
(url-history-setup-save-timer) |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
(url-cookie-setup-save-timer) |
|---|
| 87 |
(url-cookie-parse-file url-cookie-file) |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
(let ((noproxy (and (not (assoc "no_proxy" url-proxy-services)) |
|---|
| 91 |
(or (getenv "NO_PROXY") |
|---|
| 92 |
(getenv "no_PROXY") |
|---|
| 93 |
(getenv "no_proxy"))))) |
|---|
| 94 |
(if noproxy |
|---|
| 95 |
(setq url-proxy-services |
|---|
| 96 |
(cons (cons "no_proxy" |
|---|
| 97 |
(concat "\\(" |
|---|
| 98 |
(mapconcat |
|---|
| 99 |
(lambda (x) |
|---|
| 100 |
(cond |
|---|
| 101 |
((= x ?,) "\\|") |
|---|
| 102 |
((= x ? ) "") |
|---|
| 103 |
((= x ?.) (regexp-quote ".")) |
|---|
| 104 |
((= x ?*) ".*") |
|---|
| 105 |
((= x ??) ".") |
|---|
| 106 |
(t (char-to-string x)))) |
|---|
| 107 |
noproxy "") "\\)")) |
|---|
| 108 |
url-proxy-services)))) |
|---|
| 109 |
|
|---|
| 110 |
(url-setup-privacy-info) |
|---|
| 111 |
(run-hooks 'url-load-hook) |
|---|
| 112 |
(setq url-setup-done t))) |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
(defvar url-redirect-buffer nil |
|---|
| 119 |
"New buffer into which the retrieval will take place. |
|---|
| 120 |
Sometimes while retrieving a URL, the URL library needs to use another buffer |
|---|
| 121 |
than the one returned initially by `url-retrieve'. In this case, it sets this |
|---|
| 122 |
variable in the original buffer as a forwarding pointer.") |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
(defun url-retrieve (url callback &optional cbargs) |
|---|
| 126 |
"Retrieve URL asynchronously and call CALLBACK with CBARGS when finished. |
|---|
| 127 |
URL is either a string or a parsed URL. |
|---|
| 128 |
|
|---|
| 129 |
CALLBACK is called when the object has been completely retrieved, with |
|---|
| 130 |
the current buffer containing the object, and any MIME headers associated |
|---|
| 131 |
with it. It is called as (apply CALLBACK STATUS CBARGS). |
|---|
| 132 |
STATUS is a list with an even number of elements representing |
|---|
| 133 |
what happened during the request, with most recent events first, |
|---|
| 134 |
or an empty list if no events have occurred. Each pair is one of: |
|---|
| 135 |
|
|---|
| 136 |
\(:redirect REDIRECTED-TO) - the request was redirected to this URL |
|---|
| 137 |
\(:error (ERROR-SYMBOL . DATA)) - an error occurred. The error can be |
|---|
| 138 |
signaled with (signal ERROR-SYMBOL DATA). |
|---|
| 139 |
|
|---|
| 140 |
Return the buffer URL will load into, or nil if the process has |
|---|
| 141 |
already completed (i.e. URL was a mailto URL or similar; in this case |
|---|
| 142 |
the callback is not called). |
|---|
| 143 |
|
|---|
| 144 |
The variables `url-request-data', `url-request-method' and |
|---|
| 145 |
`url-request-extra-headers' can be dynamically bound around the |
|---|
| 146 |
request; dynamic binding of other variables doesn't necessarily |
|---|
| 147 |
take effect." |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
(url-retrieve-internal url callback (cons nil cbargs))) |
|---|
| 159 |
|
|---|
| 160 |
(defun url-retrieve-internal (url callback cbargs) |
|---|
| 161 |
"Internal function; external interface is `url-retrieve'. |
|---|
| 162 |
CBARGS is what the callback will actually receive - the first item is |
|---|
| 163 |
the list of events, as described in the docstring of `url-retrieve'." |
|---|
| 164 |
(url-do-setup) |
|---|
| 165 |
(url-gc-dead-buffers) |
|---|
| 166 |
(if (stringp url) |
|---|
| 167 |
(set-text-properties 0 (length url) nil url)) |
|---|
| 168 |
(if (not (vectorp url)) |
|---|
| 169 |
(setq url (url-generic-parse-url url))) |
|---|
| 170 |
(if (not (functionp callback)) |
|---|
| 171 |
(error "Must provide a callback function to url-retrieve")) |
|---|
| 172 |
(unless (url-type url) |
|---|
| 173 |
(error "Bad url: %s" (url-recreate-url url))) |
|---|
| 174 |
(let ((loader (url-scheme-get-property (url-type url) 'loader)) |
|---|
| 175 |
(url-using-proxy (if (url-host url) |
|---|
| 176 |
(url-find-proxy-for-url url (url-host url)))) |
|---|
| 177 |
(buffer nil) |
|---|
| 178 |
(asynch (url-scheme-get-property (url-type url) 'asynchronous-p))) |
|---|
| 179 |
(if url-using-proxy |
|---|
| 180 |
(setq asynch t |
|---|
| 181 |
loader 'url-proxy)) |
|---|
| 182 |
(if asynch |
|---|
| 183 |
(setq buffer (funcall loader url callback cbargs)) |
|---|
| 184 |
(setq buffer (funcall loader url)) |
|---|
| 185 |
(if buffer |
|---|
| 186 |
(with-current-buffer buffer |
|---|
| 187 |
(apply callback cbargs)))) |
|---|
| 188 |
(if url-history-track |
|---|
| 189 |
(url-history-update-url url (current-time))) |
|---|
| 190 |
buffer)) |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
(defun url-retrieve-synchronously (url) |
|---|
| 194 |
"Retrieve URL synchronously. |
|---|
| 195 |
Return the buffer containing the data, or nil if there are no data |
|---|
| 196 |
associated with it (the case for dired, info, or mailto URLs that need |
|---|
| 197 |
no further processing). URL is either a string or a parsed URL." |
|---|
| 198 |
(url-do-setup) |
|---|
| 199 |
|
|---|
| 200 |
(lexical-let ((retrieval-done nil) |
|---|
| 201 |
(asynch-buffer nil)) |
|---|
| 202 |
(setq asynch-buffer |
|---|
| 203 |
(url-retrieve url (lambda (&rest ignored) |
|---|
| 204 |
(url-debug 'retrieval "Synchronous fetching done (%S)" (current-buffer)) |
|---|
| 205 |
(setq retrieval-done t |
|---|
| 206 |
asynch-buffer (current-buffer))))) |
|---|
| 207 |
(if (null asynch-buffer) |
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
nil |
|---|
| 212 |
(let ((proc (get-buffer-process asynch-buffer))) |
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
(while (not retrieval-done) |
|---|
| 222 |
(url-debug 'retrieval |
|---|
| 223 |
"Spinning in url-retrieve-synchronously: %S (%S)" |
|---|
| 224 |
retrieval-done asynch-buffer) |
|---|
| 225 |
(if (buffer-local-value 'url-redirect-buffer asynch-buffer) |
|---|
| 226 |
(setq proc (get-buffer-process |
|---|
| 227 |
(setq asynch-buffer |
|---|
| 228 |
(buffer-local-value 'url-redirect-buffer |
|---|
| 229 |
asynch-buffer)))) |
|---|
| 230 |
(if (and proc (memq (process-status proc) |
|---|
| 231 |
'(closed exit signal failed)) |
|---|
| 232 |
|
|---|
| 233 |
(eq proc (or (get-buffer-process asynch-buffer) proc))) |
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
(progn |
|---|
| 244 |
(delete-process proc) |
|---|
| 245 |
(setq retrieval-done t))) |
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
(unless (or (with-local-quit |
|---|
| 252 |
(accept-process-output proc)) |
|---|
| 253 |
(null proc)) |
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
(when quit-flag |
|---|
| 258 |
(delete-process proc)) |
|---|
| 259 |
(setq proc (and (not quit-flag) |
|---|
| 260 |
(get-buffer-process asynch-buffer))))))) |
|---|
| 261 |
asynch-buffer))) |
|---|
| 262 |
|
|---|
| 263 |
(defun url-mm-callback (&rest ignored) |
|---|
| 264 |
(let ((handle (mm-dissect-buffer t))) |
|---|
| 265 |
(url-mark-buffer-as-dead (current-buffer)) |
|---|
| 266 |
(with-current-buffer |
|---|
| 267 |
(generate-new-buffer (url-recreate-url url-current-object)) |
|---|
| 268 |
(if (eq (mm-display-part handle) 'external) |
|---|
| 269 |
(progn |
|---|
| 270 |
(set-process-sentinel |
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
(get-buffer-process (cdr (mm-handle-undisplayer handle))) |
|---|
| 274 |
`(lambda (proc event) |
|---|
| 275 |
(mm-destroy-parts (quote ,handle)))) |
|---|
| 276 |
(message "Viewing externally") |
|---|
| 277 |
(kill-buffer (current-buffer))) |
|---|
| 278 |
(display-buffer (current-buffer)) |
|---|
| 279 |
(add-hook 'kill-buffer-hook |
|---|
| 280 |
`(lambda () (mm-destroy-parts ',handle)) |
|---|
| 281 |
nil |
|---|
| 282 |
t))))) |
|---|
| 283 |
|
|---|
| 284 |
(defun url-mm-url (url) |
|---|
| 285 |
"Retrieve URL and pass to the appropriate viewing application." |
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 |
(require 'mm-decode) |
|---|
| 291 |
(require 'mm-view) |
|---|
| 292 |
(url-retrieve url 'url-mm-callback nil)) |
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
(defvar url-dead-buffer-list nil) |
|---|
| 298 |
|
|---|
| 299 |
(defun url-mark-buffer-as-dead (buff) |
|---|
| 300 |
(push buff url-dead-buffer-list)) |
|---|
| 301 |
|
|---|
| 302 |
(defun url-gc-dead-buffers () |
|---|
| 303 |
(let ((buff)) |
|---|
| 304 |
(while (setq buff (pop url-dead-buffer-list)) |
|---|
| 305 |
(if (buffer-live-p buff) |
|---|
| 306 |
(kill-buffer buff))))) |
|---|
| 307 |
|
|---|
| 308 |
(cond |
|---|
| 309 |
((fboundp 'display-warning) |
|---|
| 310 |
(defalias 'url-warn 'display-warning)) |
|---|
| 311 |
((fboundp 'warn) |
|---|
| 312 |
(defun url-warn (class message &optional level) |
|---|
| 313 |
(warn "(%s/%s) %s" class (or level 'warning) message))) |
|---|
| 314 |
(t |
|---|
| 315 |
(defun url-warn (class message &optional level) |
|---|
| 316 |
(with-current-buffer (get-buffer-create "*URL-WARNINGS*") |
|---|
| 317 |
(goto-char (point-max)) |
|---|
| 318 |
(save-excursion |
|---|
| 319 |
(insert (format "(%s/%s) %s\n" class (or level 'warning) message))) |
|---|
| 320 |
(display-buffer (current-buffer)))))) |
|---|
| 321 |
|
|---|
| 322 |
(provide 'url) |
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
|
|---|