Commit | Line | Data |
---|---|---|
8c8b8430 | 1 | ;;; url-methods.el --- Load URL schemes as needed |
ffc00a35 | 2 | |
ba318903 | 3 | ;; Copyright (C) 1996-1999, 2004-2014 Free Software Foundation, Inc. |
ffc00a35 | 4 | |
8c8b8430 SM |
5 | ;; Keywords: comm, data, processes, hypermedia |
6 | ||
ffc00a35 SM |
7 | ;; This file is part of GNU Emacs. |
8 | ||
4936186e | 9 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
ffc00a35 | 10 | ;; it under the terms of the GNU General Public License as published by |
4936186e GM |
11 | ;; the Free Software Foundation, either version 3 of the License, or |
12 | ;; (at your option) any later version. | |
13 | ||
ffc00a35 SM |
14 | ;; GNU Emacs is distributed in the hope that it will be useful, |
15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;; GNU General Public License for more details. | |
4936186e | 18 | |
ffc00a35 | 19 | ;; You should have received a copy of the GNU General Public License |
4936186e | 20 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
ffc00a35 SM |
21 | |
22 | ;;; Commentary: | |
23 | ||
24 | ;;; Code: | |
8c8b8430 | 25 | |
8c8b8430 SM |
26 | ;; This loads up some of the small, silly URLs that I really don't |
27 | ;; want to bother putting in their own separate files. | |
8c8b8430 SM |
28 | (require 'url-parse) |
29 | ||
30 | (defvar url-scheme-registry (make-hash-table :size 7 :test 'equal)) | |
31 | ||
32 | (defconst url-scheme-methods | |
33 | '((default-port . variable) | |
34 | (asynchronous-p . variable) | |
35 | (expand-file-name . function) | |
36 | (file-exists-p . function) | |
37 | (file-attributes . function) | |
38 | (parse-url . function) | |
39 | (file-symlink-p . function) | |
40 | (file-writable-p . function) | |
41 | (file-directory-p . function) | |
42 | (file-executable-p . function) | |
43 | (directory-files . function) | |
44 | (file-truename . function)) | |
45 | "Assoc-list of methods that each URL loader can provide.") | |
46 | ||
47 | (defconst url-scheme-default-properties | |
48 | (list 'name "unknown" | |
49 | 'loader 'url-scheme-default-loader | |
50 | 'default-port 0 | |
51 | 'expand-file-name 'url-identity-expander | |
52 | 'parse-url 'url-generic-parse-url | |
53 | 'asynchronous-p nil | |
54 | 'file-directory-p 'ignore | |
55 | 'file-truename (lambda (&rest args) | |
56 | (url-recreate-url (car args))) | |
57 | 'file-exists-p 'ignore | |
58 | 'file-attributes 'ignore)) | |
59 | ||
60 | (defun url-scheme-default-loader (url &optional callback cbargs) | |
61 | "Signal an error for an unknown URL scheme." | |
22bcf204 | 62 | (error "Unknown URL scheme: %s" (url-type url))) |
8c8b8430 | 63 | |
63255710 CY |
64 | (defvar url-scheme--registering-proxy nil) |
65 | ||
8c8b8430 SM |
66 | (defun url-scheme-register-proxy (scheme) |
67 | "Automatically find a proxy for SCHEME and put it in `url-proxy-services'." | |
68 | (let* ((env-var (concat scheme "_proxy")) | |
69 | (env-proxy (or (getenv (upcase env-var)) | |
70 | (getenv (downcase env-var)))) | |
71 | (cur-proxy (assoc scheme url-proxy-services)) | |
63255710 CY |
72 | (urlobj nil) |
73 | (url-scheme--registering-proxy t)) | |
8c8b8430 | 74 | |
f316efd9 MB |
75 | ;; If env-proxy is an empty string, treat it as if it were nil |
76 | (when (and (stringp env-proxy) | |
77 | (string= env-proxy "")) | |
78 | (setq env-proxy nil)) | |
79 | ||
8c8b8430 SM |
80 | ;; Store any proxying information - this will not overwrite an old |
81 | ;; entry, so that people can still set this information in their | |
865fe16f | 82 | ;; init file |
8c8b8430 SM |
83 | (cond |
84 | (cur-proxy nil) ; Keep their old settings | |
85 | ((null env-proxy) nil) ; No proxy setup | |
86 | ;; First check if its something like hostname:port | |
87 | ((string-match "^\\([^:]+\\):\\([0-9]+\\)$" env-proxy) | |
88 | (setq urlobj (url-generic-parse-url nil)) ; Get a blank object | |
d18ec89f SM |
89 | (setf (url-type urlobj) "http") |
90 | (setf (url-host urlobj) (match-string 1 env-proxy)) | |
91 | (setf (url-port urlobj) (string-to-number (match-string 2 env-proxy)))) | |
8c8b8430 SM |
92 | ;; Then check if its a fully specified URL |
93 | ((string-match url-nonrelative-link env-proxy) | |
94 | (setq urlobj (url-generic-parse-url env-proxy)) | |
d18ec89f SM |
95 | (setf (url-type urlobj) "http") |
96 | (setf (url-target urlobj) nil)) | |
8c8b8430 SM |
97 | ;; Finally, fall back on the assumption that its just a hostname |
98 | (t | |
99 | (setq urlobj (url-generic-parse-url nil)) ; Get a blank object | |
d18ec89f SM |
100 | (setf (url-type urlobj) "http") |
101 | (setf (url-host urlobj) env-proxy))) | |
8c8b8430 SM |
102 | |
103 | (if (and (not cur-proxy) urlobj) | |
104 | (progn | |
105 | (setq url-proxy-services | |
106 | (cons (cons scheme (format "%s:%d" (url-host urlobj) | |
107 | (url-port urlobj))) | |
108 | url-proxy-services)) | |
109 | (message "Using a proxy for %s..." scheme))))) | |
110 | ||
111 | (defun url-scheme-get-property (scheme property) | |
d1ce47b0 | 112 | "Get PROPERTY of a URL SCHEME. |
8c8b8430 SM |
113 | Will automatically try to load a backend from url-SCHEME.el if |
114 | it has not already been loaded." | |
115 | (setq scheme (downcase scheme)) | |
116 | (let ((desc (gethash scheme url-scheme-registry))) | |
117 | (if (not desc) | |
118 | (let* ((stub (concat "url-" scheme)) | |
119 | (loader (intern stub))) | |
120 | (condition-case () | |
97fbed87 GM |
121 | ;; url-https.el was merged into url-http because of 8+3 |
122 | ;; filename limitations, so we have to do this dance. | |
123 | (require (if (equal "https" scheme) 'url-http loader)) | |
8c8b8430 SM |
124 | (error nil)) |
125 | (if (fboundp loader) | |
126 | (progn | |
127 | ;; Found the module to handle <scheme> URLs | |
63255710 CY |
128 | (unless url-scheme--registering-proxy |
129 | (url-scheme-register-proxy scheme)) | |
8c8b8430 SM |
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 | |
a464a6c7 SM |
136 | (pcase type |
137 | (`function | |
8c8b8430 SM |
138 | ;; Store the symbol name of a function |
139 | (if (fboundp symbol) | |
140 | (setq desc (plist-put desc (car cell) symbol)))) | |
a464a6c7 | 141 | (`variable |
8c8b8430 SM |
142 | ;; Store the VALUE of a variable |
143 | (if (boundp symbol) | |
144 | (setq desc (plist-put desc (car cell) | |
145 | (symbol-value symbol))))) | |
a464a6c7 | 146 | (_ |
8c8b8430 SM |
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) | |
e5566bd5 | 154 | |
ffc00a35 | 155 | ;;; url-methods.el ends here |