Merged in changes from CVS HEAD
[bpt/emacs.git] / lisp / url / url-parse.el
1 ;;; url-parse.el --- Uniform Resource Locator parser
2 ;; Author: $Author: miles $
3 ;; Created: $Date: 2004/04/04 04:44:10 $
4 ;; Version: $Revision: 1.3 $
5 ;; Keywords: comm, data, processes
6
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; Copyright (c) 1993 - 1996, 2004 by William M. Perry <wmperry@cs.indiana.edu>
9 ;;; Copyright (c) 1996 - 1999 Free Software Foundation, Inc.
10 ;;;
11 ;;; This file is part of GNU Emacs.
12 ;;;
13 ;;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;;; it under the terms of the GNU General Public License as published by
15 ;;; the Free Software Foundation; either version 2, or (at your option)
16 ;;; any later version.
17 ;;;
18 ;;; GNU Emacs is distributed in the hope that it will be useful,
19 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;;; GNU General Public License for more details.
22 ;;;
23 ;;; You should have received a copy of the GNU General Public License
24 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;;; Boston, MA 02111-1307, USA.
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 (require 'url-vars)
29
30 (autoload 'url-scheme-get-property "url-methods")
31
32 (defmacro url-type (urlobj)
33 `(aref ,urlobj 0))
34
35 (defmacro url-user (urlobj)
36 `(aref ,urlobj 1))
37
38 (defmacro url-password (urlobj)
39 `(aref ,urlobj 2))
40
41 (defmacro url-host (urlobj)
42 `(aref ,urlobj 3))
43
44 (defmacro url-port (urlobj)
45 `(or (aref ,urlobj 4)
46 (if (url-fullness ,urlobj)
47 (url-scheme-get-property (url-type ,urlobj) 'default-port))))
48
49 (defmacro url-filename (urlobj)
50 `(aref ,urlobj 5))
51
52 (defmacro url-target (urlobj)
53 `(aref ,urlobj 6))
54
55 (defmacro url-attributes (urlobj)
56 `(aref ,urlobj 7))
57
58 (defmacro url-fullness (urlobj)
59 `(aref ,urlobj 8))
60
61 (defmacro url-set-type (urlobj type)
62 `(aset ,urlobj 0 ,type))
63
64 (defmacro url-set-user (urlobj user)
65 `(aset ,urlobj 1 ,user))
66
67 (defmacro url-set-password (urlobj pass)
68 `(aset ,urlobj 2 ,pass))
69
70 (defmacro url-set-host (urlobj host)
71 `(aset ,urlobj 3 ,host))
72
73 (defmacro url-set-port (urlobj port)
74 `(aset ,urlobj 4 ,port))
75
76 (defmacro url-set-filename (urlobj file)
77 `(aset ,urlobj 5 ,file))
78
79 (defmacro url-set-target (urlobj targ)
80 `(aset ,urlobj 6 ,targ))
81
82 (defmacro url-set-attributes (urlobj targ)
83 `(aset ,urlobj 7 ,targ))
84
85 (defmacro url-set-full (urlobj val)
86 `(aset ,urlobj 8 ,val))
87
88 ;;;###autoload
89 (defun url-recreate-url (urlobj)
90 (concat (url-type urlobj) ":" (if (url-host urlobj) "//" "")
91 (if (url-user urlobj)
92 (concat (url-user urlobj)
93 (if (url-password urlobj)
94 (concat ":" (url-password urlobj)))
95 "@"))
96 (url-host urlobj)
97 (if (and (url-port urlobj)
98 (not (equal (url-port urlobj)
99 (url-scheme-get-property (url-type urlobj) 'default-port))))
100 (format ":%d" (url-port urlobj)))
101 (or (url-filename urlobj) "/")
102 (if (url-target urlobj)
103 (concat "#" (url-target urlobj)))
104 (if (url-attributes urlobj)
105 (concat ";"
106 (mapconcat
107 (function
108 (lambda (x)
109 (if (cdr x)
110 (concat (car x) "=" (cdr x))
111 (car x)))) (url-attributes urlobj) ";")))))
112
113 ;;;###autoload
114 (defun url-generic-parse-url (url)
115 "Return a vector of the parts of URL.
116 Format is:
117 \[proto username password hostname portnumber file reference attributes fullp\]"
118 (cond
119 ((null url)
120 (make-vector 9 nil))
121 ((or (not (string-match url-nonrelative-link url))
122 (= ?/ (string-to-char url)))
123 (let ((retval (make-vector 9 nil)))
124 (url-set-filename retval url)
125 (url-set-full retval nil)
126 retval))
127 (t
128 (save-excursion
129 (set-buffer (get-buffer-create " *urlparse*"))
130 (set-syntax-table url-parse-syntax-table)
131 (let ((save-pos nil)
132 (prot nil)
133 (user nil)
134 (pass nil)
135 (host nil)
136 (port nil)
137 (file nil)
138 (refs nil)
139 (attr nil)
140 (full nil)
141 (inhibit-read-only t))
142 (erase-buffer)
143 (insert url)
144 (goto-char (point-min))
145 (setq save-pos (point))
146 (if (not (looking-at "//"))
147 (progn
148 (skip-chars-forward "a-zA-Z+.\\-")
149 (downcase-region save-pos (point))
150 (setq prot (buffer-substring save-pos (point)))
151 (skip-chars-forward ":")
152 (setq save-pos (point))))
153
154 ;; We are doing a fully specified URL, with hostname and all
155 (if (looking-at "//")
156 (progn
157 (setq full t)
158 (forward-char 2)
159 (setq save-pos (point))
160 (skip-chars-forward "^/")
161 (setq host (buffer-substring save-pos (point)))
162 (if (string-match "^\\([^@]+\\)@" host)
163 (setq user (match-string 1 host)
164 host (substring host (match-end 0) nil)))
165 (if (and user (string-match "\\([^:]+\\):\\(.*\\)" user))
166 (setq pass (match-string 2 user)
167 user (match-string 1 user)))
168 (if (string-match ":\\([0-9+]+\\)" host)
169 (setq port (string-to-int (match-string 1 host))
170 host (substring host 0 (match-beginning 0))))
171 (if (string-match ":$" host)
172 (setq host (substring host 0 (match-beginning 0))))
173 (setq host (downcase host)
174 save-pos (point))))
175
176 (if (not port)
177 (setq port (url-scheme-get-property prot 'default-port)))
178
179 ;; Gross hack to preserve ';' in data URLs
180
181 (setq save-pos (point))
182
183 (if (string= "data" prot)
184 (goto-char (point-max))
185 ;; Now check for references
186 (skip-chars-forward "^#")
187 (if (eobp)
188 nil
189 (delete-region
190 (point)
191 (progn
192 (skip-chars-forward "#")
193 (setq refs (buffer-substring (point) (point-max)))
194 (point-max))))
195 (goto-char save-pos)
196 (skip-chars-forward "^;")
197 (if (not (eobp))
198 (setq attr (url-parse-args (buffer-substring (point) (point-max)) t)
199 attr (nreverse attr))))
200
201 (setq file (buffer-substring save-pos (point)))
202 (if (and host (string-match "%[0-9][0-9]" host))
203 (setq host (url-unhex-string host)))
204 (vector prot user pass host port file refs attr full))))))
205
206 (provide 'url-parse)
207
208 ;;; arch-tag: f338325f-71ab-4bee-93cc-78fb9a03d403