1 ;;; url-parse.el --- Uniform Resource Locator parser
2 ;; Author: $Author: miles $
3 ;; Created: $Date: 2004/04/04 04:44:10 $
4 ;; Version: $Revision: 1.3 $
5 ;; Keywords: comm, data, processes
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; Copyright (c) 1993 - 1996, 2004 by William M. Perry <wmperry@cs.indiana.edu>
9 ;;; Copyright (c) 1996 - 1999 Free Software Foundation, Inc.
11 ;;; This file is part of GNU Emacs.
13 ;;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;;; it under the terms of the GNU General Public License as published by
15 ;;; the Free Software Foundation; either version 2, or (at your option)
16 ;;; any later version.
18 ;;; GNU Emacs is distributed in the hope that it will be useful,
19 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;;; GNU General Public License for more details.
23 ;;; You should have received a copy of the GNU General Public License
24 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;;; Boston, MA 02111-1307, USA.
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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 (concat (url-type urlobj
) ":" (if (url-host urlobj
) "//" "")
92 (concat (url-user urlobj
)
93 (if (url-password urlobj
)
94 (concat ":" (url-password urlobj
)))
97 (if (and (url-port urlobj
)
98 (not (equal (url-port urlobj
)
99 (url-scheme-get-property (url-type urlobj
) 'default-port
))))
100 (format ":%d" (url-port urlobj
)))
101 (or (url-filename urlobj
) "/")
102 (if (url-target urlobj
)
103 (concat "#" (url-target urlobj
)))
104 (if (url-attributes urlobj
)
110 (concat (car x
) "=" (cdr x
))
111 (car x
)))) (url-attributes urlobj
) ";")))))
114 (defun url-generic-parse-url (url)
115 "Return a vector of the parts of URL.
117 \[proto username password hostname portnumber file reference attributes fullp\]"
121 ((or (not (string-match url-nonrelative-link url
))
122 (= ?
/ (string-to-char url
)))
123 (let ((retval (make-vector 9 nil
)))
124 (url-set-filename retval url
)
125 (url-set-full retval nil
)
129 (set-buffer (get-buffer-create " *urlparse*"))
130 (set-syntax-table url-parse-syntax-table
)
141 (inhibit-read-only t
))
144 (goto-char (point-min))
145 (setq save-pos
(point))
146 (if (not (looking-at "//"))
148 (skip-chars-forward "a-zA-Z+.\\-")
149 (downcase-region save-pos
(point))
150 (setq prot
(buffer-substring save-pos
(point)))
151 (skip-chars-forward ":")
152 (setq save-pos
(point))))
154 ;; We are doing a fully specified URL, with hostname and all
155 (if (looking-at "//")
159 (setq save-pos
(point))
160 (skip-chars-forward "^/")
161 (setq host
(buffer-substring save-pos
(point)))
162 (if (string-match "^\\([^@]+\\)@" host
)
163 (setq user
(match-string 1 host
)
164 host
(substring host
(match-end 0) nil
)))
165 (if (and user
(string-match "\\([^:]+\\):\\(.*\\)" user
))
166 (setq pass
(match-string 2 user
)
167 user
(match-string 1 user
)))
168 (if (string-match ":\\([0-9+]+\\)" host
)
169 (setq port
(string-to-int (match-string 1 host
))
170 host
(substring host
0 (match-beginning 0))))
171 (if (string-match ":$" host
)
172 (setq host
(substring host
0 (match-beginning 0))))
173 (setq host
(downcase host
)
177 (setq port
(url-scheme-get-property prot
'default-port
)))
179 ;; Gross hack to preserve ';' in data URLs
181 (setq save-pos
(point))
183 (if (string= "data" prot
)
184 (goto-char (point-max))
185 ;; Now check for references
186 (skip-chars-forward "^#")
192 (skip-chars-forward "#")
193 (setq refs
(buffer-substring (point) (point-max)))
196 (skip-chars-forward "^;")
198 (setq attr
(url-parse-args (buffer-substring (point) (point-max)) t
)
199 attr
(nreverse attr
))))
201 (setq file
(buffer-substring save-pos
(point)))
202 (if (and host
(string-match "%[0-9][0-9]" host
))
203 (setq host
(url-unhex-string host
)))
204 (vector prot user pass host port file refs attr full
))))))
208 ;;; arch-tag: f338325f-71ab-4bee-93cc-78fb9a03d403