1 ;;; url-parse.el --- Uniform Resource Locator parser
3 ;; Copyright (c) 1996,1997,1998,1999,2004 Free Software Foundation, Inc.
5 ;; Keywords: comm, data, processes
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 2, or (at your option)
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; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
30 (autoload 'url-scheme-get-property
"url-methods")
32 (defmacro url-type
(urlobj)
35 (defmacro url-user
(urlobj)
38 (defmacro url-password
(urlobj)
41 (defmacro url-host
(urlobj)
44 (defmacro url-port
(urlobj)
46 (if (url-fullness ,urlobj
)
47 (url-scheme-get-property (url-type ,urlobj
) 'default-port
))))
49 (defmacro url-filename
(urlobj)
52 (defmacro url-target
(urlobj)
55 (defmacro url-attributes
(urlobj)
58 (defmacro url-fullness
(urlobj)
61 (defmacro url-set-type
(urlobj type
)
62 `(aset ,urlobj
0 ,type
))
64 (defmacro url-set-user
(urlobj user
)
65 `(aset ,urlobj
1 ,user
))
67 (defmacro url-set-password
(urlobj pass
)
68 `(aset ,urlobj
2 ,pass
))
70 (defmacro url-set-host
(urlobj host
)
71 `(aset ,urlobj
3 ,host
))
73 (defmacro url-set-port
(urlobj port
)
74 `(aset ,urlobj
4 ,port
))
76 (defmacro url-set-filename
(urlobj file
)
77 `(aset ,urlobj
5 ,file
))
79 (defmacro url-set-target
(urlobj targ
)
80 `(aset ,urlobj
6 ,targ
))
82 (defmacro url-set-attributes
(urlobj targ
)
83 `(aset ,urlobj
7 ,targ
))
85 (defmacro url-set-full
(urlobj val
)
86 `(aset ,urlobj
8 ,val
))
89 (defun url-recreate-url (urlobj)
90 "Recreate a URL string from the parsed URLOBJ."
91 (concat (url-type urlobj
) ":" (if (url-host urlobj
) "//" "")
93 (concat (url-user urlobj
)
94 (if (url-password urlobj
)
95 (concat ":" (url-password urlobj
)))
98 (if (and (url-port urlobj
)
99 (not (equal (url-port urlobj
)
100 (url-scheme-get-property (url-type urlobj
) 'default-port
))))
101 (format ":%d" (url-port urlobj
)))
102 (or (url-filename urlobj
) "/")
103 (if (url-target urlobj
)
104 (concat "#" (url-target urlobj
)))
105 (if (url-attributes urlobj
)
111 (concat (car x
) "=" (cdr x
))
112 (car x
)))) (url-attributes urlobj
) ";")))))
115 (defun url-generic-parse-url (url)
116 "Return a vector of the parts of URL.
118 \[TYPE USER PASSWORD HOST PORT FILE TARGET ATTRIBUTES FULL\]"
122 ((or (not (string-match url-nonrelative-link url
))
123 (= ?
/ (string-to-char url
)))
124 (let ((retval (make-vector 9 nil
)))
125 (url-set-filename retval url
)
126 (url-set-full retval nil
)
130 (set-buffer (get-buffer-create " *urlparse*"))
131 (set-syntax-table url-parse-syntax-table
)
142 (inhibit-read-only t
))
145 (goto-char (point-min))
146 (setq save-pos
(point))
147 (if (not (looking-at "//"))
149 (skip-chars-forward "a-zA-Z+.\\-")
150 (downcase-region save-pos
(point))
151 (setq prot
(buffer-substring save-pos
(point)))
152 (skip-chars-forward ":")
153 (setq save-pos
(point))))
155 ;; We are doing a fully specified URL, with hostname and all
156 (if (looking-at "//")
160 (setq save-pos
(point))
161 (skip-chars-forward "^/")
162 (setq host
(buffer-substring save-pos
(point)))
163 (if (string-match "^\\([^@]+\\)@" host
)
164 (setq user
(match-string 1 host
)
165 host
(substring host
(match-end 0) nil
)))
166 (if (and user
(string-match "\\([^:]+\\):\\(.*\\)" user
))
167 (setq pass
(match-string 2 user
)
168 user
(match-string 1 user
)))
169 (if (string-match ":\\([0-9+]+\\)" host
)
170 (setq port
(string-to-int (match-string 1 host
))
171 host
(substring host
0 (match-beginning 0))))
172 (if (string-match ":$" host
)
173 (setq host
(substring host
0 (match-beginning 0))))
174 (setq host
(downcase host
)
178 (setq port
(url-scheme-get-property prot
'default-port
)))
180 ;; Gross hack to preserve ';' in data URLs
182 (setq save-pos
(point))
184 (if (string= "data" prot
)
185 (goto-char (point-max))
186 ;; Now check for references
187 (skip-chars-forward "^#")
193 (skip-chars-forward "#")
194 (setq refs
(buffer-substring (point) (point-max)))
197 (skip-chars-forward "^;")
199 (setq attr
(url-parse-args (buffer-substring (point) (point-max)) t
)
200 attr
(nreverse attr
))))
202 (setq file
(buffer-substring save-pos
(point)))
203 (if (and host
(string-match "%[0-9][0-9]" host
))
204 (setq host
(url-unhex-string host
)))
205 (vector prot user pass host port file refs attr full
))))))
209 ;; arch-tag: f338325f-71ab-4bee-93cc-78fb9a03d403
210 ;;; url-parse.el ends here