1 ;;; url-parse.el --- Uniform Resource Locator parser
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2004,
4 ;; 2005, 2006, 2007 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 3, 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 (defun url-type (urlobj)
36 (defun url-user (urlobj)
39 (defun url-password (urlobj)
42 (defun url-host (urlobj)
45 (defun url-port (urlobj)
47 (if (url-fullness urlobj)
48 (url-scheme-get-property (url-type urlobj) 'default-port))))
50 (defun url-filename (urlobj)
53 (defun url-target (urlobj)
56 (defun url-attributes (urlobj)
59 (defun url-fullness (urlobj)
62 (defun url-set-type (urlobj type)
65 (defun url-set-user (urlobj user)
68 (defun url-set-password (urlobj pass)
71 (defun url-set-host (urlobj host)
74 (defun url-set-port (urlobj port)
77 (defun url-set-filename (urlobj file)
80 (defun url-set-target (urlobj targ)
83 (defun url-set-attributes (urlobj targ)
86 (defun url-set-full (urlobj 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 (url-recreate-url-attributes urlobj)
105 (if (url-target urlobj)
106 (concat "#" (url-target urlobj)))))
108 (defun url-recreate-url-attributes (urlobj)
109 "Recreate the attributes of an URL string from the parsed URLOBJ."
110 (when (url-attributes urlobj)
112 (mapconcat (lambda (x)
114 (concat (car x) "=" (cdr x))
116 (url-attributes urlobj) ";"))))
119 (defun url-generic-parse-url (url)
120 "Return a vector of the parts of URL.
122 \[TYPE USER PASSWORD HOST PORT FILE TARGET ATTRIBUTES FULL\]"
127 ((or (not (string-match url-nonrelative-link url))
128 (= ?/ (string-to-char url)))
129 ;; This isn't correct, as a relative URL can be a fragment link
130 ;; (e.g. "#foo") and many other things (see section 4.2).
131 ;; However, let's not fix something that isn't broken, especially
132 ;; when close to a release.
133 (let ((retval (make-vector 9 nil)))
134 (url-set-filename retval url)
135 (url-set-full retval nil)
139 (set-syntax-table url-parse-syntax-table)
150 (inhibit-read-only t))
153 (goto-char (point-min))
154 (setq save-pos (point))
157 (if (not (looking-at "//"))
159 (skip-chars-forward "a-zA-Z+.\\-")
160 (downcase-region save-pos (point))
161 (setq prot (buffer-substring save-pos (point)))
162 (skip-chars-forward ":")
163 (setq save-pos (point))))
166 (if (looking-at "//")
170 (setq save-pos (point))
171 (skip-chars-forward "^/")
172 (setq host (buffer-substring save-pos (point)))
173 (if (string-match "^\\([^@]+\\)@" host)
174 (setq user (match-string 1 host)
175 host (substring host (match-end 0) nil)))
176 (if (and user (string-match "\\([^:]+\\):\\(.*\\)" user))
177 (setq pass (match-string 2 user)
178 user (match-string 1 user)))
179 ;; This gives wrong results for IPv6 literal addresses.
180 (if (string-match ":\\([0-9+]+\\)" host)
181 (setq port (string-to-number (match-string 1 host))
182 host (substring host 0 (match-beginning 0))))
183 (if (string-match ":$" host)
184 (setq host (substring host 0 (match-beginning 0))))
185 (setq host (downcase host)
189 (setq port (url-scheme-get-property prot 'default-port)))
192 ;; Gross hack to preserve ';' in data URLs
193 (setq save-pos (point))
196 (if (string= "data" prot)
197 (goto-char (point-max))
198 ;; Now check for references
199 (skip-chars-forward "^#")
205 (skip-chars-forward "#")
206 (setq refs (buffer-substring (point) (point-max)))
209 (skip-chars-forward "^;")
211 (setq attr (url-parse-args (buffer-substring (point) (point-max)) t)
212 attr (nreverse attr))))
214 (setq file (buffer-substring save-pos (point)))
215 (if (and host (string-match "%[0-9][0-9]" host))
216 (setq host (url-unhex-string host)))
217 (vector prot user pass host port file refs attr full))))))
221 ;; arch-tag: f338325f-71ab-4bee-93cc-78fb9a03d403
222 ;;; url-parse.el ends here