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 "Recreate a URL string from the parsed URLOBJ."
88 (concat (url-type urlobj) ":" (if (url-host urlobj) "//" "")
90 (concat (url-user urlobj)
91 (if (url-password urlobj)
92 (concat ":" (url-password urlobj)))
95 (if (and (url-port urlobj)
96 (not (equal (url-port urlobj)
97 (url-scheme-get-property (url-type urlobj) 'default-port))))
98 (format ":%d" (url-port urlobj)))
99 (or (url-filename urlobj) "/")
100 (if (url-target urlobj)
101 (concat "#" (url-target urlobj)))
102 (if (url-attributes urlobj)
108 (concat (car x) "=" (cdr x))
109 (car x)))) (url-attributes urlobj) ";")))))
112 (defun url-generic-parse-url (url)
113 "Return a vector of the parts of URL.
115 \[TYPE USER PASSWORD HOST PORT FILE TARGET ATTRIBUTES FULL\]"
119 ((or (not (string-match url-nonrelative-link url))
120 (= ?/ (string-to-char url)))
121 (let ((retval (make-vector 9 nil)))
122 (url-set-filename retval url)
123 (url-set-full retval nil)
127 (set-buffer (get-buffer-create " *urlparse*"))
128 (set-syntax-table url-parse-syntax-table)
139 (inhibit-read-only t))
142 (goto-char (point-min))
143 (setq save-pos (point))
144 (if (not (looking-at "//"))
146 (skip-chars-forward "a-zA-Z+.\\-")
147 (downcase-region save-pos (point))
148 (setq prot (buffer-substring save-pos (point)))
149 (skip-chars-forward ":")
150 (setq save-pos (point))))
152 ;; We are doing a fully specified URL, with hostname and all
153 (if (looking-at "//")
157 (setq save-pos (point))
158 (skip-chars-forward "^/")
159 (setq host (buffer-substring save-pos (point)))
160 (if (string-match "^\\([^@]+\\)@" host)
161 (setq user (match-string 1 host)
162 host (substring host (match-end 0) nil)))
163 (if (and user (string-match "\\([^:]+\\):\\(.*\\)" user))
164 (setq pass (match-string 2 user)
165 user (match-string 1 user)))
166 (if (string-match ":\\([0-9+]+\\)" host)
167 (setq port (string-to-int (match-string 1 host))
168 host (substring host 0 (match-beginning 0))))
169 (if (string-match ":$" host)
170 (setq host (substring host 0 (match-beginning 0))))
171 (setq host (downcase host)
175 (setq port (url-scheme-get-property prot 'default-port)))
177 ;; Gross hack to preserve ';' in data URLs
179 (setq save-pos (point))
181 (if (string= "data" prot)
182 (goto-char (point-max))
183 ;; Now check for references
184 (skip-chars-forward "^#")
190 (skip-chars-forward "#")
191 (setq refs (buffer-substring (point) (point-max)))
194 (skip-chars-forward "^;")
196 (setq attr (url-parse-args (buffer-substring (point) (point-max)) t)
197 attr (nreverse attr))))
199 (setq file (buffer-substring save-pos (point)))
200 (if (and host (string-match "%[0-9][0-9]" host))
201 (setq host (url-unhex-string host)))
202 (vector prot user pass host port file refs attr full))))))
206 ;;; arch-tag: f338325f-71ab-4bee-93cc-78fb9a03d403