Commit | Line | Data |
---|---|---|
8c8b8430 | 1 | ;;; url-parse.el --- Uniform Resource Locator parser |
ffc00a35 | 2 | |
71ddfde5 | 3 | ;; Copyright (C) 1996, 1997, 1998, 1999, 2004, |
ae940284 | 4 | ;; 2005, 2006, 2007, 2008, 2009 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) |
d18ec89f | 28 | (eval-when-compile (require 'cl)) |
8c8b8430 SM |
29 | |
30 | (autoload 'url-scheme-get-property "url-methods") | |
31 | ||
d18ec89f SM |
32 | (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) | |
8c8b8430 | 39 | |
d18ec89f SM |
40 | (defsubst url-port (urlobj) |
41 | (or (url-portspec urlobj) | |
42 | (if (url-fullness urlobj) | |
43 | (url-scheme-get-property (url-type urlobj) 'default-port)))) | |
8c8b8430 | 44 | |
d18ec89f | 45 | (defsetf url-port (urlobj) (port) `(setf (url-portspec ,urlobj) ,port)) |
71ddfde5 | 46 | |
8c8b8430 SM |
47 | ;;;###autoload |
48 | (defun url-recreate-url (urlobj) | |
61bbdf64 | 49 | "Recreate a URL string from the parsed URLOBJ." |
8c8b8430 SM |
50 | (concat (url-type urlobj) ":" (if (url-host urlobj) "//" "") |
51 | (if (url-user urlobj) | |
52 | (concat (url-user urlobj) | |
53 | (if (url-password urlobj) | |
54 | (concat ":" (url-password urlobj))) | |
55 | "@")) | |
56 | (url-host urlobj) | |
57 | (if (and (url-port urlobj) | |
58 | (not (equal (url-port urlobj) | |
59 | (url-scheme-get-property (url-type urlobj) 'default-port)))) | |
60 | (format ":%d" (url-port urlobj))) | |
fb7dc310 SM |
61 | (or (url-filename urlobj) "/") |
62 | (url-recreate-url-attributes urlobj) | |
8c8b8430 | 63 | (if (url-target urlobj) |
fb7dc310 SM |
64 | (concat "#" (url-target urlobj))))) |
65 | ||
66 | (defun url-recreate-url-attributes (urlobj) | |
67 | "Recreate the attributes of an URL string from the parsed URLOBJ." | |
68 | (when (url-attributes urlobj) | |
0539db75 | 69 | (concat ";" |
fb7dc310 SM |
70 | (mapconcat (lambda (x) |
71 | (if (cdr x) | |
72 | (concat (car x) "=" (cdr x)) | |
73 | (car x))) | |
74 | (url-attributes urlobj) ";")))) | |
8c8b8430 SM |
75 | |
76 | ;;;###autoload | |
77 | (defun url-generic-parse-url (url) | |
66991ff0 SM |
78 | "Return an URL-struct of the parts of URL. |
79 | The CL-style struct contains the following fields: | |
80 | TYPE USER PASSWORD HOST PORTSPEC FILENAME TARGET ATTRIBUTES FULLNESS." | |
f6fb70fc | 81 | ;; See RFC 3986. |
8c8b8430 SM |
82 | (cond |
83 | ((null url) | |
d18ec89f | 84 | (url-parse-make-urlobj)) |
8c8b8430 SM |
85 | ((or (not (string-match url-nonrelative-link url)) |
86 | (= ?/ (string-to-char url))) | |
f6fb70fc MH |
87 | ;; This isn't correct, as a relative URL can be a fragment link |
88 | ;; (e.g. "#foo") and many other things (see section 4.2). | |
89 | ;; However, let's not fix something that isn't broken, especially | |
90 | ;; when close to a release. | |
d18ec89f | 91 | (url-parse-make-urlobj nil nil nil nil nil url)) |
8c8b8430 | 92 | (t |
d58fae84 | 93 | (with-temp-buffer |
8c8b8430 SM |
94 | (set-syntax-table url-parse-syntax-table) |
95 | (let ((save-pos nil) | |
96 | (prot nil) | |
97 | (user nil) | |
98 | (pass nil) | |
99 | (host nil) | |
100 | (port nil) | |
101 | (file nil) | |
102 | (refs nil) | |
103 | (attr nil) | |
104 | (full nil) | |
105 | (inhibit-read-only t)) | |
106 | (erase-buffer) | |
107 | (insert url) | |
108 | (goto-char (point-min)) | |
109 | (setq save-pos (point)) | |
f6fb70fc MH |
110 | |
111 | ;; 3.1. Scheme | |
8c8b8430 SM |
112 | (if (not (looking-at "//")) |
113 | (progn | |
114 | (skip-chars-forward "a-zA-Z+.\\-") | |
115 | (downcase-region save-pos (point)) | |
116 | (setq prot (buffer-substring save-pos (point))) | |
117 | (skip-chars-forward ":") | |
118 | (setq save-pos (point)))) | |
119 | ||
f6fb70fc | 120 | ;; 3.2. Authority |
8c8b8430 SM |
121 | (if (looking-at "//") |
122 | (progn | |
123 | (setq full t) | |
124 | (forward-char 2) | |
125 | (setq save-pos (point)) | |
0539db75 | 126 | (skip-chars-forward "^/") |
8c8b8430 SM |
127 | (setq host (buffer-substring save-pos (point))) |
128 | (if (string-match "^\\([^@]+\\)@" host) | |
129 | (setq user (match-string 1 host) | |
130 | host (substring host (match-end 0) nil))) | |
131 | (if (and user (string-match "\\([^:]+\\):\\(.*\\)" user)) | |
132 | (setq pass (match-string 2 user) | |
133 | user (match-string 1 user))) | |
f6fb70fc | 134 | ;; This gives wrong results for IPv6 literal addresses. |
8c8b8430 | 135 | (if (string-match ":\\([0-9+]+\\)" host) |
216d3806 | 136 | (setq port (string-to-number (match-string 1 host)) |
8c8b8430 SM |
137 | host (substring host 0 (match-beginning 0)))) |
138 | (if (string-match ":$" host) | |
139 | (setq host (substring host 0 (match-beginning 0)))) | |
140 | (setq host (downcase host) | |
141 | save-pos (point)))) | |
142 | ||
143 | (if (not port) | |
144 | (setq port (url-scheme-get-property prot 'default-port))) | |
145 | ||
f6fb70fc | 146 | ;; 3.3. Path |
0539db75 | 147 | ;; Gross hack to preserve ';' in data URLs |
8c8b8430 SM |
148 | (setq save-pos (point)) |
149 | ||
f6fb70fc | 150 | ;; 3.4. Query |
0539db75 CY |
151 | (if (string= "data" prot) |
152 | (goto-char (point-max)) | |
153 | ;; Now check for references | |
8c8b8430 | 154 | (skip-chars-forward "^#") |
0539db75 CY |
155 | (if (eobp) |
156 | nil | |
157 | (delete-region | |
158 | (point) | |
159 | (progn | |
160 | (skip-chars-forward "#") | |
161 | (setq refs (buffer-substring (point) (point-max))) | |
162 | (point-max)))) | |
163 | (goto-char save-pos) | |
164 | (skip-chars-forward "^;") | |
165 | (if (not (eobp)) | |
166 | (setq attr (url-parse-args (buffer-substring (point) (point-max)) t) | |
167 | attr (nreverse attr)))) | |
8c8b8430 | 168 | |
0539db75 | 169 | (setq file (buffer-substring save-pos (point))) |
8c8b8430 SM |
170 | (if (and host (string-match "%[0-9][0-9]" host)) |
171 | (setq host (url-unhex-string host))) | |
d18ec89f SM |
172 | (url-parse-make-urlobj |
173 | prot user pass host port file refs attr full)))))) | |
8c8b8430 SM |
174 | |
175 | (provide 'url-parse) | |
e5566bd5 | 176 | |
ffc00a35 SM |
177 | ;; arch-tag: f338325f-71ab-4bee-93cc-78fb9a03d403 |
178 | ;;; url-parse.el ends here |