2efabed5cd85d51b05eb68764ba8e3c9d8a13512
[bpt/emacs.git] / lisp / url / url-parse.el
1 ;;; url-parse.el --- Uniform Resource Locator parser
2
3 ;; Copyright (C) 1996-1999, 2004-2012 Free Software Foundation, Inc.
4
5 ;; Keywords: comm, data, processes
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 3 of the License, or
12 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 (require 'url-vars)
27 (require 'auth-source)
28 (eval-when-compile (require 'cl-lib))
29
30 (autoload 'url-scheme-get-property "url-methods")
31
32 (cl-defstruct (url
33 (:constructor nil)
34 (:constructor url-parse-make-urlobj
35 (&optional type user password host portspec filename
36 target attributes fullness))
37 (:copier nil))
38 type user password host portspec filename target attributes fullness
39 silent (use-cookies t))
40
41 (defsubst url-port (urlobj)
42 "Return the port number for the URL specified by URLOBJ."
43 (declare (gv-setter (lambda (port) `(setf (url-portspec ,urlobj) ,port))))
44 (or (url-portspec urlobj)
45 (if (url-type urlobj)
46 (url-scheme-get-property (url-type urlobj) 'default-port))))
47
48
49 (defun url-path-and-query (urlobj)
50 "Return the path and query components of URLOBJ.
51 These two components are stored together in the FILENAME slot of
52 the object. The return value of this function is (PATH . QUERY),
53 where each of PATH and QUERY are strings or nil."
54 (let ((name (url-filename urlobj))
55 path query)
56 (when name
57 (if (string-match "\\?" name)
58 (setq path (substring name 0 (match-beginning 0))
59 query (substring name (match-end 0)))
60 (setq path name)))
61 (if (equal path "") (setq path nil))
62 (if (equal query "") (setq query nil))
63 (cons path query)))
64
65 (defun url-port-if-non-default (urlobj)
66 "Return the port number specified by URLOBJ, if it is not the default.
67 If the specified port number is the default, return nil."
68 (let ((port (url-portspec urlobj))
69 type)
70 (and port
71 (or (null (setq type (url-type urlobj)))
72 (not (equal port (url-scheme-get-property type 'default-port))))
73 port)))
74
75 ;;;###autoload
76 (defun url-recreate-url (urlobj)
77 "Recreate a URL string from the parsed URLOBJ."
78 (let* ((type (url-type urlobj))
79 (user (url-user urlobj))
80 (pass (url-password urlobj))
81 (host (url-host urlobj))
82 ;; RFC 3986: "omit the port component and its : delimiter if
83 ;; port is empty or if its value would be the same as that of
84 ;; the scheme's default."
85 (port (url-port-if-non-default urlobj))
86 (file (url-filename urlobj))
87 (frag (url-target urlobj)))
88 (concat (if type (concat type ":"))
89 (if (url-fullness urlobj) "//")
90 (if (or user pass)
91 (concat user
92 (if pass (concat ":" pass))
93 "@"))
94 host
95 (if port (format ":%d" (url-port urlobj)))
96 (or file "/")
97 (if frag (concat "#" frag)))))
98
99 (defun url-recreate-url-attributes (urlobj)
100 "Recreate the attributes of an URL string from the parsed URLOBJ."
101 (declare (obsolete nil "24.3"))
102 (when (url-attributes urlobj)
103 (concat ";"
104 (mapconcat (lambda (x)
105 (if (cdr x)
106 (concat (car x) "=" (cdr x))
107 (car x)))
108 (url-attributes urlobj) ";"))))
109
110 ;;;###autoload
111 (defun url-generic-parse-url (url)
112 "Return an URL-struct of the parts of URL.
113 The CL-style struct contains the following fields:
114
115 TYPE is the URI scheme (string or nil).
116 USER is the user name (string or nil).
117 PASSWORD is the password (string [deprecated] or nil).
118 HOST is the host (a registered name, IP literal in square
119 brackets, or IPv4 address in dotted-decimal form).
120 PORTSPEC is the specified port (a number), or nil.
121 FILENAME is the path AND the query component of the URI.
122 TARGET is the fragment identifier component (used to refer to a
123 subordinate resource, e.g. a part of a webpage).
124 ATTRIBUTES is nil; this slot originally stored the attribute and
125 value alists for IMAP URIs, but this feature was removed
126 since it conflicts with RFC 3986.
127 FULLNESS is non-nil iff the hierarchical sequence component of
128 the URL starts with two slashes, \"//\".
129
130 The parser follows RFC 3986, except that it also tries to handle
131 URIs that are not fully specified (e.g. lacking TYPE), and it
132 does not check for or perform %-encoding.
133
134 Here is an example. The URL
135
136 foo://bob:pass@example.com:42/a/b/c.dtb?type=animal&name=narwhal#nose
137
138 parses to
139
140 TYPE = \"foo\"
141 USER = \"bob\"
142 PASSWORD = \"pass\"
143 HOST = \"example.com\"
144 PORTSPEC = 42
145 FILENAME = \"/a/b/c.dtb?type=animal&name=narwhal\"
146 TARGET = \"nose\"
147 ATTRIBUTES = nil
148 FULLNESS = t"
149 (if (null url)
150 (url-parse-make-urlobj)
151 (with-temp-buffer
152 ;; Don't let those temp-buffer modifications accidentally
153 ;; deactivate the mark of the current-buffer.
154 (let ((deactivate-mark nil))
155 (set-syntax-table url-parse-syntax-table)
156 (erase-buffer)
157 (insert url)
158 (goto-char (point-min))
159 (let ((save-pos (point))
160 scheme user pass host port file fragment full
161 (inhibit-read-only t))
162
163 ;; 3.1. Scheme
164 ;; This is nil for a URI that is not fully specified.
165 (when (looking-at "\\([a-zA-Z][-a-zA-Z0-9+.]*\\):")
166 (goto-char (match-end 0))
167 (setq save-pos (point))
168 (setq scheme (downcase (match-string 1))))
169
170 ;; 3.2. Authority
171 (when (looking-at "//")
172 (setq full t)
173 (forward-char 2)
174 (setq save-pos (point))
175 (skip-chars-forward "^/?#")
176 (setq host (buffer-substring save-pos (point)))
177 ;; 3.2.1 User Information
178 (if (string-match "^\\([^@]+\\)@" host)
179 (setq user (match-string 1 host)
180 host (substring host (match-end 0))))
181 (if (and user (string-match "\\`\\([^:]*\\):\\(.*\\)" user))
182 (setq pass (match-string 2 user)
183 user (match-string 1 user)))
184 (cond
185 ;; IPv6 literal address.
186 ((string-match "^\\(\\[[^]]+\\]\\)\\(?::\\([0-9]*\\)\\)?$" host)
187 (setq port (match-string 2 host)
188 host (match-string 1 host)))
189 ;; Registered name or IPv4 address.
190 ((string-match ":\\([0-9]*\\)$" host)
191 (setq port (match-string 1 host)
192 host (substring host 0 (match-beginning 0)))))
193 (cond ((equal port "")
194 (setq port nil))
195 (port
196 (setq port (string-to-number port))))
197 (setq host (downcase host)))
198
199 ;; Now point is on the / ? or # which terminates the
200 ;; authority, or at the end of the URI, or (if there is no
201 ;; authority) at the beginning of the absolute path.
202
203 (setq save-pos (point))
204 (if (string= "data" scheme)
205 ;; For the "data" URI scheme, all the rest is the FILE.
206 (setq file (buffer-substring save-pos (point-max)))
207 ;; For hysterical raisins, our data structure returns the
208 ;; path and query components together in one slot.
209 ;; 3.3. Path
210 (skip-chars-forward "^?#")
211 ;; 3.4. Query
212 (when (looking-at "?")
213 (skip-chars-forward "^#"))
214 (setq file (buffer-substring save-pos (point)))
215 ;; 3.5 Fragment
216 (when (looking-at "#")
217 (let ((opoint (point)))
218 (forward-char 1)
219 (unless (eobp)
220 (setq fragment (buffer-substring (point) (point-max))))
221 (delete-region opoint (point-max)))))
222
223 (if (and host (string-match "%[0-9][0-9]" host))
224 (setq host (url-unhex-string host)))
225 (url-parse-make-urlobj scheme user pass host port file
226 fragment nil full))))))
227
228 (defmacro url-bit-for-url (method lookfor url)
229 `(let* ((urlobj (url-generic-parse-url url))
230 (bit (funcall ,method urlobj))
231 (methods (list 'url-recreate-url
232 'url-host))
233 auth-info)
234 (while (and (not bit) (> (length methods) 0))
235 (setq auth-info (auth-source-search
236 :max 1
237 :host (funcall (pop methods) urlobj)
238 :port (url-type urlobj)))
239 (setq bit (plist-get (nth 0 auth-info) ,lookfor))
240 (when (functionp bit)
241 (setq bit (funcall bit))))
242 bit))
243
244 (defun url-user-for-url (url)
245 "Attempt to use .authinfo to find a user for this URL."
246 (url-bit-for-url 'url-user :user url))
247
248 (defun url-password-for-url (url)
249 "Attempt to use .authinfo to find a password for this URL."
250 (url-bit-for-url 'url-password :secret url))
251
252 (provide 'url-parse)
253
254 ;;; url-parse.el ends here