]> code.delx.au - gnu-emacs/blob - lisp/url/url-parse.el
(cl): Don't require.
[gnu-emacs] / lisp / url / url-parse.el
1 ;;; url-parse.el --- Uniform Resource Locator parser
2 ;; Keywords: comm, data, processes
3
4 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 ;;; Copyright (c) 1993 - 1996, 2004 by William M. Perry <wmperry@cs.indiana.edu>
6 ;;; Copyright (c) 1996 - 1999 Free Software Foundation, Inc.
7 ;;;
8 ;;; This file is part of GNU Emacs.
9 ;;;
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.
14 ;;;
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.
19 ;;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 (require 'url-vars)
26
27 (autoload 'url-scheme-get-property "url-methods")
28
29 (defmacro url-type (urlobj)
30 `(aref ,urlobj 0))
31
32 (defmacro url-user (urlobj)
33 `(aref ,urlobj 1))
34
35 (defmacro url-password (urlobj)
36 `(aref ,urlobj 2))
37
38 (defmacro url-host (urlobj)
39 `(aref ,urlobj 3))
40
41 (defmacro url-port (urlobj)
42 `(or (aref ,urlobj 4)
43 (if (url-fullness ,urlobj)
44 (url-scheme-get-property (url-type ,urlobj) 'default-port))))
45
46 (defmacro url-filename (urlobj)
47 `(aref ,urlobj 5))
48
49 (defmacro url-target (urlobj)
50 `(aref ,urlobj 6))
51
52 (defmacro url-attributes (urlobj)
53 `(aref ,urlobj 7))
54
55 (defmacro url-fullness (urlobj)
56 `(aref ,urlobj 8))
57
58 (defmacro url-set-type (urlobj type)
59 `(aset ,urlobj 0 ,type))
60
61 (defmacro url-set-user (urlobj user)
62 `(aset ,urlobj 1 ,user))
63
64 (defmacro url-set-password (urlobj pass)
65 `(aset ,urlobj 2 ,pass))
66
67 (defmacro url-set-host (urlobj host)
68 `(aset ,urlobj 3 ,host))
69
70 (defmacro url-set-port (urlobj port)
71 `(aset ,urlobj 4 ,port))
72
73 (defmacro url-set-filename (urlobj file)
74 `(aset ,urlobj 5 ,file))
75
76 (defmacro url-set-target (urlobj targ)
77 `(aset ,urlobj 6 ,targ))
78
79 (defmacro url-set-attributes (urlobj targ)
80 `(aset ,urlobj 7 ,targ))
81
82 (defmacro url-set-full (urlobj val)
83 `(aset ,urlobj 8 ,val))
84
85 ;;;###autoload
86 (defun url-recreate-url (urlobj)
87 "Recreate a URL string from the parsed URLOBJ."
88 (concat (url-type urlobj) ":" (if (url-host urlobj) "//" "")
89 (if (url-user urlobj)
90 (concat (url-user urlobj)
91 (if (url-password urlobj)
92 (concat ":" (url-password urlobj)))
93 "@"))
94 (url-host 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)
103 (concat ";"
104 (mapconcat
105 (function
106 (lambda (x)
107 (if (cdr x)
108 (concat (car x) "=" (cdr x))
109 (car x)))) (url-attributes urlobj) ";")))))
110
111 ;;;###autoload
112 (defun url-generic-parse-url (url)
113 "Return a vector of the parts of URL.
114 Format is:
115 \[TYPE USER PASSWORD HOST PORT FILE TARGET ATTRIBUTES FULL\]"
116 (cond
117 ((null url)
118 (make-vector 9 nil))
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)
124 retval))
125 (t
126 (save-excursion
127 (set-buffer (get-buffer-create " *urlparse*"))
128 (set-syntax-table url-parse-syntax-table)
129 (let ((save-pos nil)
130 (prot nil)
131 (user nil)
132 (pass nil)
133 (host nil)
134 (port nil)
135 (file nil)
136 (refs nil)
137 (attr nil)
138 (full nil)
139 (inhibit-read-only t))
140 (erase-buffer)
141 (insert url)
142 (goto-char (point-min))
143 (setq save-pos (point))
144 (if (not (looking-at "//"))
145 (progn
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))))
151
152 ;; We are doing a fully specified URL, with hostname and all
153 (if (looking-at "//")
154 (progn
155 (setq full t)
156 (forward-char 2)
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)
172 save-pos (point))))
173
174 (if (not port)
175 (setq port (url-scheme-get-property prot 'default-port)))
176
177 ;; Gross hack to preserve ';' in data URLs
178
179 (setq save-pos (point))
180
181 (if (string= "data" prot)
182 (goto-char (point-max))
183 ;; Now check for references
184 (skip-chars-forward "^#")
185 (if (eobp)
186 nil
187 (delete-region
188 (point)
189 (progn
190 (skip-chars-forward "#")
191 (setq refs (buffer-substring (point) (point-max)))
192 (point-max))))
193 (goto-char save-pos)
194 (skip-chars-forward "^;")
195 (if (not (eobp))
196 (setq attr (url-parse-args (buffer-substring (point) (point-max)) t)
197 attr (nreverse attr))))
198
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))))))
203
204 (provide 'url-parse)
205
206 ;;; arch-tag: f338325f-71ab-4bee-93cc-78fb9a03d403