Mercurial > emacs
annotate lisp/url/url-parse.el @ 87008:888320f5e3f7
*** empty log message ***
author | Juanma Barranquero <lekktu@gmail.com> |
---|---|
date | Mon, 03 Dec 2007 09:28:15 +0000 |
parents | dd2bcc6758a0 |
children | 107ccd98fa12 |
rev | line source |
---|---|
54695 | 1 ;;; url-parse.el --- Uniform Resource Locator parser |
54831
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
2 |
64748
875dcc490074
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64084
diff
changeset
|
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2004, |
75347 | 4 ;; 2005, 2006, 2007 Free Software Foundation, Inc. |
54831
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
5 |
54695 | 6 ;; Keywords: comm, data, processes |
7 | |
54831
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
8 ;; This file is part of GNU Emacs. |
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
9 ;; |
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify |
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
11 ;; it under the terms of the GNU General Public License as published by |
78222
8932997d0b62
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
77220
diff
changeset
|
12 ;; the Free Software Foundation; either version 3, or (at your option) |
54831
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
13 ;; any later version. |
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
14 ;; |
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
15 ;; GNU Emacs is distributed in the hope that it will be useful, |
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
18 ;; GNU General Public License for more details. |
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
19 ;; |
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
20 ;; You should have received a copy of the GNU General Public License |
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
64084 | 22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 ;; Boston, MA 02110-1301, USA. | |
54831
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
24 |
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
25 ;;; Commentary: |
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
26 |
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
27 ;;; Code: |
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
28 |
54695 | 29 (require 'url-vars) |
83823
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
30 (eval-when-compile (require 'cl)) |
54695 | 31 |
32 (autoload 'url-scheme-get-property "url-methods") | |
33 | |
83823
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
34 (defstruct (url |
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
35 (:constructor nil) |
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
36 (:constructor url-parse-make-urlobj |
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
37 (&optional type user password host portspec filename |
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
38 target attributes fullness)) |
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
39 (:copier nil)) |
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
40 type user password host portspec filename target attributes fullness) |
54695 | 41 |
83823
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
42 (defsubst url-port (urlobj) |
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
43 (or (url-portspec urlobj) |
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
44 (if (url-fullness urlobj) |
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
45 (url-scheme-get-property (url-type urlobj) 'default-port)))) |
54695 | 46 |
83823
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
47 (defsetf url-port (urlobj) (port) `(setf (url-portspec ,urlobj) ,port)) |
64748
875dcc490074
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64084
diff
changeset
|
48 |
54695 | 49 ;;;###autoload |
50 (defun url-recreate-url (urlobj) | |
54802 | 51 "Recreate a URL string from the parsed URLOBJ." |
54695 | 52 (concat (url-type urlobj) ":" (if (url-host urlobj) "//" "") |
53 (if (url-user urlobj) | |
54 (concat (url-user urlobj) | |
55 (if (url-password urlobj) | |
56 (concat ":" (url-password urlobj))) | |
57 "@")) | |
58 (url-host urlobj) | |
59 (if (and (url-port urlobj) | |
60 (not (equal (url-port urlobj) | |
61 (url-scheme-get-property (url-type urlobj) 'default-port)))) | |
62 (format ":%d" (url-port urlobj))) | |
72589
f6c1ac4c14c7
(url-recreate-url-attributes): New function, code moved from `url-recreate-url'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68640
diff
changeset
|
63 (or (url-filename urlobj) "/") |
f6c1ac4c14c7
(url-recreate-url-attributes): New function, code moved from `url-recreate-url'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68640
diff
changeset
|
64 (url-recreate-url-attributes urlobj) |
54695 | 65 (if (url-target urlobj) |
72589
f6c1ac4c14c7
(url-recreate-url-attributes): New function, code moved from `url-recreate-url'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68640
diff
changeset
|
66 (concat "#" (url-target urlobj))))) |
f6c1ac4c14c7
(url-recreate-url-attributes): New function, code moved from `url-recreate-url'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68640
diff
changeset
|
67 |
f6c1ac4c14c7
(url-recreate-url-attributes): New function, code moved from `url-recreate-url'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68640
diff
changeset
|
68 (defun url-recreate-url-attributes (urlobj) |
f6c1ac4c14c7
(url-recreate-url-attributes): New function, code moved from `url-recreate-url'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68640
diff
changeset
|
69 "Recreate the attributes of an URL string from the parsed URLOBJ." |
f6c1ac4c14c7
(url-recreate-url-attributes): New function, code moved from `url-recreate-url'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68640
diff
changeset
|
70 (when (url-attributes urlobj) |
77220
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
71 (concat ";" |
72589
f6c1ac4c14c7
(url-recreate-url-attributes): New function, code moved from `url-recreate-url'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68640
diff
changeset
|
72 (mapconcat (lambda (x) |
f6c1ac4c14c7
(url-recreate-url-attributes): New function, code moved from `url-recreate-url'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68640
diff
changeset
|
73 (if (cdr x) |
f6c1ac4c14c7
(url-recreate-url-attributes): New function, code moved from `url-recreate-url'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68640
diff
changeset
|
74 (concat (car x) "=" (cdr x)) |
f6c1ac4c14c7
(url-recreate-url-attributes): New function, code moved from `url-recreate-url'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68640
diff
changeset
|
75 (car x))) |
f6c1ac4c14c7
(url-recreate-url-attributes): New function, code moved from `url-recreate-url'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68640
diff
changeset
|
76 (url-attributes urlobj) ";")))) |
54695 | 77 |
78 ;;;###autoload | |
79 (defun url-generic-parse-url (url) | |
80 "Return a vector of the parts of URL. | |
81 Format is: | |
54802 | 82 \[TYPE USER PASSWORD HOST PORT FILE TARGET ATTRIBUTES FULL\]" |
73313
8c2a0bfc98b0
(url-generic-parse-url): Handle URLs with empty path component and
Magnus Henoch <mange@freemail.hu>
parents:
72589
diff
changeset
|
83 ;; See RFC 3986. |
54695 | 84 (cond |
85 ((null url) | |
83823
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
86 (url-parse-make-urlobj)) |
54695 | 87 ((or (not (string-match url-nonrelative-link url)) |
88 (= ?/ (string-to-char url))) | |
73313
8c2a0bfc98b0
(url-generic-parse-url): Handle URLs with empty path component and
Magnus Henoch <mange@freemail.hu>
parents:
72589
diff
changeset
|
89 ;; This isn't correct, as a relative URL can be a fragment link |
8c2a0bfc98b0
(url-generic-parse-url): Handle URLs with empty path component and
Magnus Henoch <mange@freemail.hu>
parents:
72589
diff
changeset
|
90 ;; (e.g. "#foo") and many other things (see section 4.2). |
8c2a0bfc98b0
(url-generic-parse-url): Handle URLs with empty path component and
Magnus Henoch <mange@freemail.hu>
parents:
72589
diff
changeset
|
91 ;; However, let's not fix something that isn't broken, especially |
8c2a0bfc98b0
(url-generic-parse-url): Handle URLs with empty path component and
Magnus Henoch <mange@freemail.hu>
parents:
72589
diff
changeset
|
92 ;; when close to a release. |
83823
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
93 (url-parse-make-urlobj nil nil nil nil nil url)) |
54695 | 94 (t |
74014
fd0232950658
(url-generic-parse-url): Use with-temp-buffer.
Magnus Henoch <mange@freemail.hu>
parents:
73313
diff
changeset
|
95 (with-temp-buffer |
54695 | 96 (set-syntax-table url-parse-syntax-table) |
97 (let ((save-pos nil) | |
98 (prot nil) | |
99 (user nil) | |
100 (pass nil) | |
101 (host nil) | |
102 (port nil) | |
103 (file nil) | |
104 (refs nil) | |
105 (attr nil) | |
106 (full nil) | |
107 (inhibit-read-only t)) | |
108 (erase-buffer) | |
109 (insert url) | |
110 (goto-char (point-min)) | |
111 (setq save-pos (point)) | |
73313
8c2a0bfc98b0
(url-generic-parse-url): Handle URLs with empty path component and
Magnus Henoch <mange@freemail.hu>
parents:
72589
diff
changeset
|
112 |
8c2a0bfc98b0
(url-generic-parse-url): Handle URLs with empty path component and
Magnus Henoch <mange@freemail.hu>
parents:
72589
diff
changeset
|
113 ;; 3.1. Scheme |
54695 | 114 (if (not (looking-at "//")) |
115 (progn | |
116 (skip-chars-forward "a-zA-Z+.\\-") | |
117 (downcase-region save-pos (point)) | |
118 (setq prot (buffer-substring save-pos (point))) | |
119 (skip-chars-forward ":") | |
120 (setq save-pos (point)))) | |
121 | |
73313
8c2a0bfc98b0
(url-generic-parse-url): Handle URLs with empty path component and
Magnus Henoch <mange@freemail.hu>
parents:
72589
diff
changeset
|
122 ;; 3.2. Authority |
54695 | 123 (if (looking-at "//") |
124 (progn | |
125 (setq full t) | |
126 (forward-char 2) | |
127 (setq save-pos (point)) | |
77220
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
128 (skip-chars-forward "^/") |
54695 | 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))) | |
73313
8c2a0bfc98b0
(url-generic-parse-url): Handle URLs with empty path component and
Magnus Henoch <mange@freemail.hu>
parents:
72589
diff
changeset
|
136 ;; This gives wrong results for IPv6 literal addresses. |
54695 | 137 (if (string-match ":\\([0-9+]+\\)" host) |
62400
e30c08177a3b
Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents:
57427
diff
changeset
|
138 (setq port (string-to-number (match-string 1 host)) |
54695 | 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 | |
73313
8c2a0bfc98b0
(url-generic-parse-url): Handle URLs with empty path component and
Magnus Henoch <mange@freemail.hu>
parents:
72589
diff
changeset
|
148 ;; 3.3. Path |
77220
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
149 ;; Gross hack to preserve ';' in data URLs |
54695 | 150 (setq save-pos (point)) |
151 | |
73313
8c2a0bfc98b0
(url-generic-parse-url): Handle URLs with empty path component and
Magnus Henoch <mange@freemail.hu>
parents:
72589
diff
changeset
|
152 ;; 3.4. Query |
77220
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
153 (if (string= "data" prot) |
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
154 (goto-char (point-max)) |
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
155 ;; Now check for references |
54695 | 156 (skip-chars-forward "^#") |
77220
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
157 (if (eobp) |
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
158 nil |
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
159 (delete-region |
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
160 (point) |
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
161 (progn |
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
162 (skip-chars-forward "#") |
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
163 (setq refs (buffer-substring (point) (point-max))) |
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
164 (point-max)))) |
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
165 (goto-char save-pos) |
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
166 (skip-chars-forward "^;") |
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
167 (if (not (eobp)) |
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
168 (setq attr (url-parse-args (buffer-substring (point) (point-max)) t) |
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
169 attr (nreverse attr)))) |
54695 | 170 |
77220
4368e2079b5c
(url-generic-parse-url): Revert 2006-10-09 changes.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
171 (setq file (buffer-substring save-pos (point))) |
54695 | 172 (if (and host (string-match "%[0-9][0-9]" host)) |
173 (setq host (url-unhex-string host))) | |
83823
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
174 (url-parse-make-urlobj |
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
175 prot user pass host port file refs attr full)))))) |
54695 | 176 |
177 (provide 'url-parse) | |
54699 | 178 |
54831
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
179 ;; arch-tag: f338325f-71ab-4bee-93cc-78fb9a03d403 |
ca18766bb266
Comment fixups.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54802
diff
changeset
|
180 ;;; url-parse.el ends here |