Merged in changes from CVS HEAD
[bpt/emacs.git] / 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 (concat (url-type urlobj) ":" (if (url-host urlobj) "//" "")
88 (if (url-user urlobj)
89 (concat (url-user urlobj)
90 (if (url-password urlobj)
91 (concat ":" (url-password urlobj)))
92 "@"))
93 (url-host urlobj)
94 (if (and (url-port urlobj)
95 (not (equal (url-port urlobj)
96 (url-scheme-get-property (url-type urlobj) 'default-port))))
97 (format ":%d" (url-port urlobj)))
98 (or (url-filename urlobj) "/")
99 (if (url-target urlobj)
100 (concat "#" (url-target urlobj)))
101 (if (url-attributes urlobj)
102 (concat ";"
103 (mapconcat
104 (function
105 (lambda (x)
106 (if (cdr x)
107 (concat (car x) "=" (cdr x))
108 (car x)))) (url-attributes urlobj) ";")))))
109
110 ;;;###autoload
111 (defun url-generic-parse-url (url)
112 "Return a vector of the parts of URL.
113 Format is:
114 \[proto username password hostname portnumber file reference attributes fullp\]"
115 (cond
116 ((null url)
117 (make-vector 9 nil))
118 ((or (not (string-match url-nonrelative-link url))
119 (= ?/ (string-to-char url)))
120 (let ((retval (make-vector 9 nil)))
121 (url-set-filename retval url)
122 (url-set-full retval nil)
123 retval))
124 (t
125 (save-excursion
126 (set-buffer (get-buffer-create " *urlparse*"))
127 (set-syntax-table url-parse-syntax-table)
128 (let ((save-pos nil)
129 (prot nil)
130 (user nil)
131 (pass nil)
132 (host nil)
133 (port nil)
134 (file nil)
135 (refs nil)
136 (attr nil)
137 (full nil)
138 (inhibit-read-only t))
139 (erase-buffer)
140 (insert url)
141 (goto-char (point-min))
142 (setq save-pos (point))
143 (if (not (looking-at "//"))
144 (progn
145 (skip-chars-forward "a-zA-Z+.\\-")
146 (downcase-region save-pos (point))
147 (setq prot (buffer-substring save-pos (point)))
148 (skip-chars-forward ":")
149 (setq save-pos (point))))
150
151 ;; We are doing a fully specified URL, with hostname and all
152 (if (looking-at "//")
153 (progn
154 (setq full t)
155 (forward-char 2)
156 (setq save-pos (point))
157 (skip-chars-forward "^/")
158 (setq host (buffer-substring save-pos (point)))
159 (if (string-match "^\\([^@]+\\)@" host)
160 (setq user (match-string 1 host)
161 host (substring host (match-end 0) nil)))
162 (if (and user (string-match "\\([^:]+\\):\\(.*\\)" user))
163 (setq pass (match-string 2 user)
164 user (match-string 1 user)))
165 (if (string-match ":\\([0-9+]+\\)" host)
166 (setq port (string-to-int (match-string 1 host))
167 host (substring host 0 (match-beginning 0))))
168 (if (string-match ":$" host)
169 (setq host (substring host 0 (match-beginning 0))))
170 (setq host (downcase host)
171 save-pos (point))))
172
173 (if (not port)
174 (setq port (url-scheme-get-property prot 'default-port)))
175
176 ;; Gross hack to preserve ';' in data URLs
177
178 (setq save-pos (point))
179
180 (if (string= "data" prot)
181 (goto-char (point-max))
182 ;; Now check for references
183 (skip-chars-forward "^#")
184 (if (eobp)
185 nil
186 (delete-region
187 (point)
188 (progn
189 (skip-chars-forward "#")
190 (setq refs (buffer-substring (point) (point-max)))
191 (point-max))))
192 (goto-char save-pos)
193 (skip-chars-forward "^;")
194 (if (not (eobp))
195 (setq attr (url-parse-args (buffer-substring (point) (point-max)) t)
196 attr (nreverse attr))))
197
198 (setq file (buffer-substring save-pos (point)))
199 (if (and host (string-match "%[0-9][0-9]" host))
200 (setq host (url-unhex-string host)))
201 (vector prot user pass host port file refs attr full))))))
202
203 (provide 'url-parse)
204
205 ;;; arch-tag: f338325f-71ab-4bee-93cc-78fb9a03d403