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