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