1 ;;; url-parse.el --- Uniform Resource Locator parser
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Keywords: comm, data, processes
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)
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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
31 (autoload 'url-scheme-get-property "url-methods")
33 (defmacro url-type (urlobj)
36 (defmacro url-user (urlobj)
39 (defmacro url-password (urlobj)
42 (defmacro url-host (urlobj)
45 (defmacro url-port (urlobj)
47 (if (url-fullness ,urlobj)
48 (url-scheme-get-property (url-type ,urlobj) 'default-port))))
50 (defmacro url-filename (urlobj)
53 (defmacro url-target (urlobj)
56 (defmacro url-attributes (urlobj)
59 (defmacro url-fullness (urlobj)
62 (defmacro url-set-type (urlobj type)
63 `(aset ,urlobj 0 ,type))
65 (defmacro url-set-user (urlobj user)
66 `(aset ,urlobj 1 ,user))
68 (defmacro url-set-password (urlobj pass)
69 `(aset ,urlobj 2 ,pass))
71 (defmacro url-set-host (urlobj host)
72 `(aset ,urlobj 3 ,host))
74 (defmacro url-set-port (urlobj port)
75 `(aset ,urlobj 4 ,port))
77 (defmacro url-set-filename (urlobj file)
78 `(aset ,urlobj 5 ,file))
80 (defmacro url-set-target (urlobj targ)
81 `(aset ,urlobj 6 ,targ))
83 (defmacro url-set-attributes (urlobj targ)
84 `(aset ,urlobj 7 ,targ))
86 (defmacro url-set-full (urlobj val)
87 `(aset ,urlobj 8 ,val))
90 (defun url-recreate-url (urlobj)
91 "Recreate a URL string from the parsed URLOBJ."
92 (concat (url-type urlobj) ":" (if (url-host urlobj) "//" "")
94 (concat (url-user urlobj)
95 (if (url-password urlobj)
96 (concat ":" (url-password urlobj)))
99 (if (and (url-port urlobj)
100 (not (equal (url-port urlobj)
101 (url-scheme-get-property (url-type urlobj) 'default-port))))
102 (format ":%d" (url-port urlobj)))
103 (or (url-filename urlobj) "/")
104 (if (url-target urlobj)
105 (concat "#" (url-target urlobj)))
106 (if (url-attributes urlobj)
112 (concat (car x) "=" (cdr x))
113 (car x)))) (url-attributes urlobj) ";")))))
116 (defun url-generic-parse-url (url)
117 "Return a vector of the parts of URL.
119 \[TYPE USER PASSWORD HOST PORT FILE TARGET ATTRIBUTES FULL\]"
123 ((or (not (string-match url-nonrelative-link url))
124 (= ?/ (string-to-char url)))
125 (let ((retval (make-vector 9 nil)))
126 (url-set-filename retval url)
127 (url-set-full retval nil)
131 (set-buffer (get-buffer-create " *urlparse*"))
132 (set-syntax-table url-parse-syntax-table)
143 (inhibit-read-only t))
146 (goto-char (point-min))
147 (setq save-pos (point))
148 (if (not (looking-at "//"))
150 (skip-chars-forward "a-zA-Z+.\\-")
151 (downcase-region save-pos (point))
152 (setq prot (buffer-substring save-pos (point)))
153 (skip-chars-forward ":")
154 (setq save-pos (point))))
156 ;; We are doing a fully specified URL, with hostname and all
157 (if (looking-at "//")
161 (setq save-pos (point))
162 (skip-chars-forward "^/")
163 (setq host (buffer-substring save-pos (point)))
164 (if (string-match "^\\([^@]+\\)@" host)
165 (setq user (match-string 1 host)
166 host (substring host (match-end 0) nil)))
167 (if (and user (string-match "\\([^:]+\\):\\(.*\\)" user))
168 (setq pass (match-string 2 user)
169 user (match-string 1 user)))
170 (if (string-match ":\\([0-9+]+\\)" host)
171 (setq port (string-to-number (match-string 1 host))
172 host (substring host 0 (match-beginning 0))))
173 (if (string-match ":$" host)
174 (setq host (substring host 0 (match-beginning 0))))
175 (setq host (downcase host)
179 (setq port (url-scheme-get-property prot 'default-port)))
181 ;; Gross hack to preserve ';' in data URLs
183 (setq save-pos (point))
185 (if (string= "data" prot)
186 (goto-char (point-max))
187 ;; Now check for references
188 (skip-chars-forward "^#")
194 (skip-chars-forward "#")
195 (setq refs (buffer-substring (point) (point-max)))
198 (skip-chars-forward "^;")
200 (setq attr (url-parse-args (buffer-substring (point) (point-max)) t)
201 attr (nreverse attr))))
203 (setq file (buffer-substring save-pos (point)))
204 (if (and host (string-match "%[0-9][0-9]" host))
205 (setq host (url-unhex-string host)))
206 (vector prot user pass host port file refs attr full))))))
210 ;; arch-tag: f338325f-71ab-4bee-93cc-78fb9a03d403
211 ;;; url-parse.el ends here