| 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 |
(eval-when-compile |
|---|
| 30 |
(require 'cl)) |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
(require 'url-parse) |
|---|
| 35 |
|
|---|
| 36 |
(defvar url-scheme-registry (make-hash-table :size 7 :test 'equal)) |
|---|
| 37 |
|
|---|
| 38 |
(defconst url-scheme-methods |
|---|
| 39 |
'((default-port . variable) |
|---|
| 40 |
(asynchronous-p . variable) |
|---|
| 41 |
(expand-file-name . function) |
|---|
| 42 |
(file-exists-p . function) |
|---|
| 43 |
(file-attributes . function) |
|---|
| 44 |
(parse-url . function) |
|---|
| 45 |
(file-symlink-p . function) |
|---|
| 46 |
(file-writable-p . function) |
|---|
| 47 |
(file-directory-p . function) |
|---|
| 48 |
(file-executable-p . function) |
|---|
| 49 |
(directory-files . function) |
|---|
| 50 |
(file-truename . function)) |
|---|
| 51 |
"Assoc-list of methods that each URL loader can provide.") |
|---|
| 52 |
|
|---|
| 53 |
(defconst url-scheme-default-properties |
|---|
| 54 |
(list 'name "unknown" |
|---|
| 55 |
'loader 'url-scheme-default-loader |
|---|
| 56 |
'default-port 0 |
|---|
| 57 |
'expand-file-name 'url-identity-expander |
|---|
| 58 |
'parse-url 'url-generic-parse-url |
|---|
| 59 |
'asynchronous-p nil |
|---|
| 60 |
'file-directory-p 'ignore |
|---|
| 61 |
'file-truename (lambda (&rest args) |
|---|
| 62 |
(url-recreate-url (car args))) |
|---|
| 63 |
'file-exists-p 'ignore |
|---|
| 64 |
'file-attributes 'ignore)) |
|---|
| 65 |
|
|---|
| 66 |
(defun url-scheme-default-loader (url &optional callback cbargs) |
|---|
| 67 |
"Signal an error for an unknown URL scheme." |
|---|
| 68 |
(error "Unkown URL scheme: %s" (url-type url))) |
|---|
| 69 |
|
|---|
| 70 |
(defun url-scheme-register-proxy (scheme) |
|---|
| 71 |
"Automatically find a proxy for SCHEME and put it in `url-proxy-services'." |
|---|
| 72 |
(let* ((env-var (concat scheme "_proxy")) |
|---|
| 73 |
(env-proxy (or (getenv (upcase env-var)) |
|---|
| 74 |
(getenv (downcase env-var)))) |
|---|
| 75 |
(cur-proxy (assoc scheme url-proxy-services)) |
|---|
| 76 |
(urlobj nil)) |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
(when (and (stringp env-proxy) |
|---|
| 80 |
(string= env-proxy "")) |
|---|
| 81 |
(setq env-proxy nil)) |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
(cond |
|---|
| 87 |
(cur-proxy nil) |
|---|
| 88 |
((null env-proxy) nil) |
|---|
| 89 |
|
|---|
| 90 |
((string-match "^\\([^:]+\\):\\([0-9]+\\)$" env-proxy) |
|---|
| 91 |
(setq urlobj (url-generic-parse-url nil)) |
|---|
| 92 |
(url-set-type urlobj "http") |
|---|
| 93 |
(url-set-host urlobj (match-string 1 env-proxy)) |
|---|
| 94 |
(url-set-port urlobj (string-to-number (match-string 2 env-proxy)))) |
|---|
| 95 |
|
|---|
| 96 |
((string-match url-nonrelative-link env-proxy) |
|---|
| 97 |
(setq urlobj (url-generic-parse-url env-proxy)) |
|---|
| 98 |
(url-set-type urlobj "http") |
|---|
| 99 |
(url-set-target urlobj nil)) |
|---|
| 100 |
|
|---|
| 101 |
(t |
|---|
| 102 |
(setq urlobj (url-generic-parse-url nil)) |
|---|
| 103 |
(url-set-type urlobj "http") |
|---|
| 104 |
(url-set-host urlobj env-proxy))) |
|---|
| 105 |
|
|---|
| 106 |
(if (and (not cur-proxy) urlobj) |
|---|
| 107 |
(progn |
|---|
| 108 |
(setq url-proxy-services |
|---|
| 109 |
(cons (cons scheme (format "%s:%d" (url-host urlobj) |
|---|
| 110 |
(url-port urlobj))) |
|---|
| 111 |
url-proxy-services)) |
|---|
| 112 |
(message "Using a proxy for %s..." scheme))))) |
|---|
| 113 |
|
|---|
| 114 |
(defun url-scheme-get-property (scheme property) |
|---|
| 115 |
"Get property of a URL SCHEME. |
|---|
| 116 |
Will automatically try to load a backend from url-SCHEME.el if |
|---|
| 117 |
it has not already been loaded." |
|---|
| 118 |
(setq scheme (downcase scheme)) |
|---|
| 119 |
(let ((desc (gethash scheme url-scheme-registry))) |
|---|
| 120 |
(if (not desc) |
|---|
| 121 |
(let* ((stub (concat "url-" scheme)) |
|---|
| 122 |
(loader (intern stub))) |
|---|
| 123 |
(condition-case () |
|---|
| 124 |
(require loader) |
|---|
| 125 |
(error nil)) |
|---|
| 126 |
(if (fboundp loader) |
|---|
| 127 |
(progn |
|---|
| 128 |
|
|---|
| 129 |
(url-scheme-register-proxy scheme) |
|---|
| 130 |
(setq desc (list 'name scheme |
|---|
| 131 |
'loader loader)) |
|---|
| 132 |
(dolist (cell url-scheme-methods) |
|---|
| 133 |
(let ((symbol (intern-soft (format "%s-%s" stub (car cell)))) |
|---|
| 134 |
(type (cdr cell))) |
|---|
| 135 |
(if symbol |
|---|
| 136 |
(case type |
|---|
| 137 |
(function |
|---|
| 138 |
|
|---|
| 139 |
(if (fboundp symbol) |
|---|
| 140 |
(setq desc (plist-put desc (car cell) symbol)))) |
|---|
| 141 |
(variable |
|---|
| 142 |
|
|---|
| 143 |
(if (boundp symbol) |
|---|
| 144 |
(setq desc (plist-put desc (car cell) |
|---|
| 145 |
(symbol-value symbol))))) |
|---|
| 146 |
(otherwise |
|---|
| 147 |
(error "Malformed url-scheme-methods entry: %S" |
|---|
| 148 |
cell)))))) |
|---|
| 149 |
(puthash scheme desc url-scheme-registry))))) |
|---|
| 150 |
(or (plist-get desc property) |
|---|
| 151 |
(plist-get url-scheme-default-properties property)))) |
|---|
| 152 |
|
|---|
| 153 |
(provide 'url-methods) |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|