1 ;;; url-parse.el --- Uniform Resource Locator parser
2 ;; Keywords: comm, data, processes
4 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 ;;; Copyright (c) 1993 - 1996, 2004 by William M. Perry <wmperry@cs.indiana.edu>
6 ;;; Copyright (c) 1996 - 1999 Free Software Foundation, Inc.
8 ;;; This file is part of GNU Emacs.
10 ;;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;;; it under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 2, or (at your option)
13 ;;; any later version.
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.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;;; Boston, MA 02111-1307, USA.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 (autoload 'url-scheme-get-property
"url-methods")
29 (defmacro url-type
(urlobj)
32 (defmacro url-user
(urlobj)
35 (defmacro url-password
(urlobj)
38 (defmacro url-host
(urlobj)
41 (defmacro url-port
(urlobj)
43 (if (url-fullness ,urlobj
)
44 (url-scheme-get-property (url-type ,urlobj
) 'default-port
))))
46 (defmacro url-filename
(urlobj)
49 (defmacro url-target
(urlobj)
52 (defmacro url-attributes
(urlobj)
55 (defmacro url-fullness
(urlobj)
58 (defmacro url-set-type
(urlobj type
)
59 `(aset ,urlobj
0 ,type
))
61 (defmacro url-set-user
(urlobj user
)
62 `(aset ,urlobj
1 ,user
))
64 (defmacro url-set-password
(urlobj pass
)
65 `(aset ,urlobj
2 ,pass
))
67 (defmacro url-set-host
(urlobj host
)
68 `(aset ,urlobj
3 ,host
))
70 (defmacro url-set-port
(urlobj port
)
71 `(aset ,urlobj
4 ,port
))
73 (defmacro url-set-filename
(urlobj file
)
74 `(aset ,urlobj
5 ,file
))
76 (defmacro url-set-target
(urlobj targ
)
77 `(aset ,urlobj
6 ,targ
))
79 (defmacro url-set-attributes
(urlobj targ
)
80 `(aset ,urlobj
7 ,targ
))
82 (defmacro url-set-full
(urlobj val
)
83 `(aset ,urlobj
8 ,val
))
86 (defun url-recreate-url (urlobj)
87 (concat (url-type urlobj
) ":" (if (url-host urlobj
) "//" "")
89 (concat (url-user urlobj
)
90 (if (url-password urlobj
)
91 (concat ":" (url-password urlobj
)))
94 (if (and (url-port urlobj
)
95 (not (equal (url-port urlobj
)
96 (url-scheme-get-property (url-type urlobj
) 'default-port
))))
97 (format ":%d" (url-port urlobj
)))
98 (or (url-filename urlobj
) "/")
99 (if (url-target urlobj
)
100 (concat "#" (url-target urlobj
)))
101 (if (url-attributes urlobj
)
107 (concat (car x
) "=" (cdr x
))
108 (car x
)))) (url-attributes urlobj
) ";")))))
111 (defun url-generic-parse-url (url)
112 "Return a vector of the parts of URL.
114 \[proto username password hostname portnumber file reference attributes fullp\]"
118 ((or (not (string-match url-nonrelative-link url
))
119 (= ?
/ (string-to-char url
)))
120 (let ((retval (make-vector 9 nil
)))
121 (url-set-filename retval url
)
122 (url-set-full retval nil
)
126 (set-buffer (get-buffer-create " *urlparse*"))
127 (set-syntax-table url-parse-syntax-table
)
138 (inhibit-read-only t
))
141 (goto-char (point-min))
142 (setq save-pos
(point))
143 (if (not (looking-at "//"))
145 (skip-chars-forward "a-zA-Z+.\\-")
146 (downcase-region save-pos
(point))
147 (setq prot
(buffer-substring save-pos
(point)))
148 (skip-chars-forward ":")
149 (setq save-pos
(point))))
151 ;; We are doing a fully specified URL, with hostname and all
152 (if (looking-at "//")
156 (setq save-pos
(point))
157 (skip-chars-forward "^/")
158 (setq host
(buffer-substring save-pos
(point)))
159 (if (string-match "^\\([^@]+\\)@" host
)
160 (setq user
(match-string 1 host
)
161 host
(substring host
(match-end 0) nil
)))
162 (if (and user
(string-match "\\([^:]+\\):\\(.*\\)" user
))
163 (setq pass
(match-string 2 user
)
164 user
(match-string 1 user
)))
165 (if (string-match ":\\([0-9+]+\\)" host
)
166 (setq port
(string-to-int (match-string 1 host
))
167 host
(substring host
0 (match-beginning 0))))
168 (if (string-match ":$" host
)
169 (setq host
(substring host
0 (match-beginning 0))))
170 (setq host
(downcase host
)
174 (setq port
(url-scheme-get-property prot
'default-port
)))
176 ;; Gross hack to preserve ';' in data URLs
178 (setq save-pos
(point))
180 (if (string= "data" prot
)
181 (goto-char (point-max))
182 ;; Now check for references
183 (skip-chars-forward "^#")
189 (skip-chars-forward "#")
190 (setq refs
(buffer-substring (point) (point-max)))
193 (skip-chars-forward "^;")
195 (setq attr
(url-parse-args (buffer-substring (point) (point-max)) t
)
196 attr
(nreverse attr
))))
198 (setq file
(buffer-substring save-pos
(point)))
199 (if (and host
(string-match "%[0-9][0-9]" host
))
200 (setq host
(url-unhex-string host
)))
201 (vector prot user pass host port file refs attr full
))))))
205 ;;; arch-tag: f338325f-71ab-4bee-93cc-78fb9a03d403