Stop files with same basename messing up finder's package--builtins
[emacs.git] / lisp / url / url-methods.el
blob84294ab68cff99395578a941a304dd1d4acdfed9
1 ;;; url-methods.el --- Load URL schemes as needed
3 ;; Copyright (C) 1996-1999, 2004-2014 Free Software Foundation, Inc.
5 ;; Keywords: comm, data, processes, hypermedia
7 ;; This file is part of GNU Emacs.
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 3 of the License, or
12 ;; (at your option) any later version.
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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;;; Code:
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.
28 (require 'url-parse)
30 (defvar url-scheme-registry (make-hash-table :size 7 :test 'equal))
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.")
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))
60 (defun url-scheme-default-loader (url &optional callback cbargs)
61 "Signal an error for an unknown URL scheme."
62 (error "Unknown URL scheme: %s" (url-type url)))
64 (defvar url-scheme--registering-proxy nil)
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))
72 (urlobj nil)
73 (url-scheme--registering-proxy t))
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))
80 ;; Store any proxying information - this will not overwrite an old
81 ;; entry, so that people can still set this information in their
82 ;; init file
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
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))))
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))
95 (setf (url-type urlobj) "http")
96 (setf (url-target urlobj) nil))
97 ;; Finally, fall back on the assumption that its just a hostname
99 (setq urlobj (url-generic-parse-url nil)) ; Get a blank object
100 (setf (url-type urlobj) "http")
101 (setf (url-host urlobj) env-proxy)))
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)))))
111 (defun url-scheme-get-property (scheme property)
112 "Get PROPERTY of a URL SCHEME.
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 ()
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))
124 (error nil))
125 (if (fboundp loader)
126 (progn
127 ;; Found the module to handle <scheme> URLs
128 (unless url-scheme--registering-proxy
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 (pcase type
137 (`function
138 ;; Store the symbol name of a function
139 (if (fboundp symbol)
140 (setq desc (plist-put desc (car cell) symbol))))
141 (`variable
142 ;; Store the VALUE of a variable
143 (if (boundp symbol)
144 (setq desc (plist-put desc (car cell)
145 (symbol-value symbol)))))
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))))
153 (provide 'url-methods)
155 ;;; url-methods.el ends here