Merge from emacs-23.
[bpt/emacs.git] / lisp / url / url-parse.el
CommitLineData
8c8b8430 1;;; url-parse.el --- Uniform Resource Locator parser
ffc00a35 2
71ddfde5 3;; Copyright (C) 1996, 1997, 1998, 1999, 2004,
5df4f04c 4;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
ffc00a35 5
8c8b8430
SM
6;; Keywords: comm, data, processes
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:
26
8c8b8430 27(require 'url-vars)
04c23739 28(require 'auth-source)
d18ec89f 29(eval-when-compile (require 'cl))
8c8b8430
SM
30
31(autoload 'url-scheme-get-property "url-methods")
32
d18ec89f
SM
33(defstruct (url
34 (:constructor nil)
35 (:constructor url-parse-make-urlobj
36 (&optional type user password host portspec filename
37 target attributes fullness))
38 (:copier nil))
08b8ba9f 39 type user password host portspec filename target attributes fullness silent)
8c8b8430 40
d18ec89f
SM
41(defsubst url-port (urlobj)
42 (or (url-portspec urlobj)
43 (if (url-fullness urlobj)
44 (url-scheme-get-property (url-type urlobj) 'default-port))))
8c8b8430 45
d18ec89f 46(defsetf url-port (urlobj) (port) `(setf (url-portspec ,urlobj) ,port))
71ddfde5 47
8c8b8430
SM
48;;;###autoload
49(defun url-recreate-url (urlobj)
61bbdf64 50 "Recreate a URL string from the parsed URLOBJ."
8c8b8430
SM
51 (concat (url-type urlobj) ":" (if (url-host urlobj) "//" "")
52 (if (url-user urlobj)
53 (concat (url-user urlobj)
54 (if (url-password urlobj)
55 (concat ":" (url-password urlobj)))
56 "@"))
57 (url-host urlobj)
58 (if (and (url-port urlobj)
59 (not (equal (url-port urlobj)
60 (url-scheme-get-property (url-type urlobj) 'default-port))))
61 (format ":%d" (url-port urlobj)))
fb7dc310
SM
62 (or (url-filename urlobj) "/")
63 (url-recreate-url-attributes urlobj)
8c8b8430 64 (if (url-target urlobj)
fb7dc310
SM
65 (concat "#" (url-target urlobj)))))
66
67(defun url-recreate-url-attributes (urlobj)
68 "Recreate the attributes of an URL string from the parsed URLOBJ."
69 (when (url-attributes urlobj)
0539db75 70 (concat ";"
fb7dc310
SM
71 (mapconcat (lambda (x)
72 (if (cdr x)
73 (concat (car x) "=" (cdr x))
74 (car x)))
75 (url-attributes urlobj) ";"))))
8c8b8430
SM
76
77;;;###autoload
78(defun url-generic-parse-url (url)
66991ff0
SM
79 "Return an URL-struct of the parts of URL.
80The CL-style struct contains the following fields:
81TYPE USER PASSWORD HOST PORTSPEC FILENAME TARGET ATTRIBUTES FULLNESS."
f6fb70fc 82 ;; See RFC 3986.
8c8b8430
SM
83 (cond
84 ((null url)
d18ec89f 85 (url-parse-make-urlobj))
8c8b8430
SM
86 ((or (not (string-match url-nonrelative-link url))
87 (= ?/ (string-to-char url)))
f6fb70fc
MH
88 ;; This isn't correct, as a relative URL can be a fragment link
89 ;; (e.g. "#foo") and many other things (see section 4.2).
90 ;; However, let's not fix something that isn't broken, especially
91 ;; when close to a release.
d18ec89f 92 (url-parse-make-urlobj nil nil nil nil nil url))
8c8b8430 93 (t
d58fae84 94 (with-temp-buffer
c074ba4a
SM
95 ;; Don't let those temp-buffer modifications accidentally
96 ;; deactivate the mark of the current-buffer.
97 (let ((deactivate-mark nil))
98 (set-syntax-table url-parse-syntax-table)
99 (let ((save-pos nil)
100 (prot nil)
101 (user nil)
102 (pass nil)
103 (host nil)
104 (port nil)
105 (file nil)
106 (refs nil)
107 (attr nil)
108 (full nil)
109 (inhibit-read-only t))
110 (erase-buffer)
111 (insert url)
112 (goto-char (point-min))
113 (setq save-pos (point))
114
115 ;; 3.1. Scheme
116 (unless (looking-at "//")
117 (skip-chars-forward "a-zA-Z+.\\-")
118 (downcase-region save-pos (point))
119 (setq prot (buffer-substring save-pos (point)))
120 (skip-chars-forward ":")
121 (setq save-pos (point)))
122
123 ;; 3.2. Authority
124 (when (looking-at "//")
125 (setq full t)
126 (forward-char 2)
127 (setq save-pos (point))
128 (skip-chars-forward "^/")
129 (setq host (buffer-substring save-pos (point)))
130 (if (string-match "^\\([^@]+\\)@" host)
131 (setq user (match-string 1 host)
132 host (substring host (match-end 0) nil)))
133 (if (and user (string-match "\\([^:]+\\):\\(.*\\)" user))
134 (setq pass (match-string 2 user)
135 user (match-string 1 user)))
136 ;; This gives wrong results for IPv6 literal addresses.
137 (if (string-match ":\\([0-9+]+\\)" host)
138 (setq port (string-to-number (match-string 1 host))
139 host (substring host 0 (match-beginning 0))))
140 (if (string-match ":$" host)
141 (setq host (substring host 0 (match-beginning 0))))
142 (setq host (downcase host)
143 save-pos (point)))
144
145 (if (not port)
146 (setq port (url-scheme-get-property prot 'default-port)))
147
148 ;; 3.3. Path
149 ;; Gross hack to preserve ';' in data URLs
150 (setq save-pos (point))
151
152 ;; 3.4. Query
153 (if (string= "data" prot)
154 (goto-char (point-max))
155 ;; Now check for references
156 (skip-chars-forward "^#")
157 (if (eobp)
158 nil
159 (delete-region
160 (point)
161 (progn
162 (skip-chars-forward "#")
163 (setq refs (buffer-substring (point) (point-max)))
164 (point-max))))
165 (goto-char save-pos)
166 (skip-chars-forward "^;")
167 (unless (eobp)
168 (setq attr (url-parse-args (buffer-substring (point) (point-max))
169 t)
0539db75 170 attr (nreverse attr))))
8c8b8430 171
c074ba4a
SM
172 (setq file (buffer-substring save-pos (point)))
173 (if (and host (string-match "%[0-9][0-9]" host))
174 (setq host (url-unhex-string host)))
175 (url-parse-make-urlobj
176 prot user pass host port file refs attr full)))))))
8c8b8430 177
04c23739
MH
178(defmacro url-bit-for-url (method lookfor url)
179 `(let* ((urlobj (url-generic-parse-url url))
180 (bit (funcall ,method urlobj))
181 (methods (list 'url-recreate-url
182 'url-host)))
183 (while (and (not bit) (> (length methods) 0))
184 (setq bit
185 (auth-source-user-or-password
186 ,lookfor (funcall (pop methods) urlobj) (url-type urlobj))))
187 bit))
188
189(defun url-user-for-url (url)
190 "Attempt to use .authinfo to find a user for this URL."
191 (url-bit-for-url 'url-user "login" url))
192
193(defun url-password-for-url (url)
194 "Attempt to use .authinfo to find a password for this URL."
195 (url-bit-for-url 'url-password "password" url))
196
8c8b8430 197(provide 'url-parse)
e5566bd5 198
ffc00a35 199;;; url-parse.el ends here