Mercurial > hg > xemacs-beta
comparison lisp/w3/url-parse.el @ 14:9ee227acff29 r19-15b90
Import from CVS: tag r19-15b90
author | cvs |
---|---|
date | Mon, 13 Aug 2007 08:48:42 +0200 |
parents | |
children | 0293115a14e9 6a378aca36af |
comparison
equal
deleted
inserted
replaced
13:13c6d0aaafe5 | 14:9ee227acff29 |
---|---|
1 ;;; url-parse.el --- Uniform Resource Locator parser | |
2 ;; Author: wmperry | |
3 ;; Created: 1996/12/26 23:25:55 | |
4 ;; Version: 1.3 | |
5 ;; Keywords: comm, data, processes | |
6 | |
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
8 ;;; Copyright (c) 1993-1996 by William M. Perry (wmperry@cs.indiana.edu) | |
9 ;;; Copyright (c) 1996 Free Software Foundation, Inc. | |
10 ;;; | |
11 ;;; This file is not part of GNU Emacs, but the same permissions apply. | |
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 (defmacro url-type (urlobj) | |
29 (` (aref (, urlobj) 0))) | |
30 | |
31 (defmacro url-user (urlobj) | |
32 (` (aref (, urlobj) 1))) | |
33 | |
34 (defmacro url-password (urlobj) | |
35 (` (aref (, urlobj) 2))) | |
36 | |
37 (defmacro url-host (urlobj) | |
38 (` (aref (, urlobj) 3))) | |
39 | |
40 (defmacro url-port (urlobj) | |
41 (` (or (aref (, urlobj) 4) | |
42 (if (url-fullness (, urlobj)) | |
43 (cdr-safe (assoc (url-type (, urlobj)) url-default-ports)))))) | |
44 | |
45 (defmacro url-filename (urlobj) | |
46 (` (aref (, urlobj) 5))) | |
47 | |
48 (defmacro url-target (urlobj) | |
49 (` (aref (, urlobj) 6))) | |
50 | |
51 (defmacro url-attributes (urlobj) | |
52 (` (aref (, urlobj) 7))) | |
53 | |
54 (defmacro url-fullness (urlobj) | |
55 (` (aref (, urlobj) 8))) | |
56 | |
57 (defmacro url-set-type (urlobj type) | |
58 (` (aset (, urlobj) 0 (, type)))) | |
59 | |
60 (defmacro url-set-user (urlobj user) | |
61 (` (aset (, urlobj) 1 (, user)))) | |
62 | |
63 (defmacro url-set-password (urlobj pass) | |
64 (` (aset (, urlobj) 2 (, pass)))) | |
65 | |
66 (defmacro url-set-host (urlobj host) | |
67 (` (aset (, urlobj) 3 (, host)))) | |
68 | |
69 (defmacro url-set-port (urlobj port) | |
70 (` (aset (, urlobj) 4 (, port)))) | |
71 | |
72 (defmacro url-set-filename (urlobj file) | |
73 (` (aset (, urlobj) 5 (, file)))) | |
74 | |
75 (defmacro url-set-target (urlobj targ) | |
76 (` (aset (, urlobj) 6 (, targ)))) | |
77 | |
78 (defmacro url-set-attributes (urlobj targ) | |
79 (` (aset (, urlobj) 7 (, targ)))) | |
80 | |
81 (defmacro url-set-full (urlobj val) | |
82 (` (aset (, urlobj) 8 (, val)))) | |
83 | |
84 (defun url-recreate-url (urlobj) | |
85 (concat (url-type urlobj) ":" (if (url-host urlobj) "//" "") | |
86 (if (url-user urlobj) | |
87 (concat (url-user urlobj) | |
88 (if (url-password urlobj) | |
89 (concat ":" (url-password urlobj))) | |
90 "@")) | |
91 (url-host urlobj) | |
92 (if (and (url-port urlobj) | |
93 (not (equal (url-port urlobj) | |
94 (cdr-safe (assoc (url-type urlobj) | |
95 url-default-ports))))) | |
96 (concat ":" (url-port urlobj))) | |
97 (or (url-filename urlobj) "/") | |
98 (if (url-target urlobj) | |
99 (concat "#" (url-target urlobj))) | |
100 (if (url-attributes urlobj) | |
101 (concat ";" | |
102 (mapconcat | |
103 (function | |
104 (lambda (x) | |
105 (if (cdr x) | |
106 (concat (car x) "=" (cdr x)) | |
107 (car x)))) (url-attributes urlobj) ";"))))) | |
108 | |
109 (defun url-generic-parse-url (url) | |
110 "Return a vector of the parts of URL. | |
111 Format is [protocol username password hostname portnumber file reference]" | |
112 (cond | |
113 ((null url) | |
114 (make-vector 9 nil)) | |
115 ((or (not (string-match url-nonrelative-link url)) | |
116 (= ?/ (string-to-char url))) | |
117 (let ((retval (make-vector 9 nil))) | |
118 (url-set-filename retval url) | |
119 (url-set-full retval nil) | |
120 retval)) | |
121 (t | |
122 (save-excursion | |
123 (set-buffer (get-buffer-create " *urlparse*")) | |
124 (set-syntax-table url-mailserver-syntax-table) | |
125 (let ((save-pos nil) | |
126 (prot nil) | |
127 (user nil) | |
128 (pass nil) | |
129 (host nil) | |
130 (port nil) | |
131 (file nil) | |
132 (refs nil) | |
133 (attr nil) | |
134 (full nil) | |
135 (inhibit-read-only t)) | |
136 (erase-buffer) | |
137 (insert url) | |
138 (goto-char (point-min)) | |
139 (setq save-pos (point)) | |
140 (if (not (looking-at "//")) | |
141 (progn | |
142 (skip-chars-forward "a-zA-Z+.\\-") | |
143 (downcase-region save-pos (point)) | |
144 (setq prot (buffer-substring save-pos (point))) | |
145 (skip-chars-forward ":") | |
146 (setq save-pos (point)))) | |
147 | |
148 ;; We are doing a fully specified URL, with hostname and all | |
149 (if (looking-at "//") | |
150 (progn | |
151 (setq full t) | |
152 (forward-char 2) | |
153 (setq save-pos (point)) | |
154 (skip-chars-forward "^/") | |
155 (downcase-region save-pos (point)) | |
156 (setq host (buffer-substring save-pos (point))) | |
157 (if (string-match "^\\([^@]+\\)@" host) | |
158 (setq user (url-match host 1) | |
159 host (substring host (match-end 0) nil))) | |
160 (if (and user (string-match "\\([^:]+\\):\\(.*\\)" user)) | |
161 (setq pass (url-match user 2) | |
162 user (url-match user 1))) | |
163 (if (string-match ":\\([0-9+]+\\)" host) | |
164 (setq port (url-match host 1) | |
165 host (substring host 0 (match-beginning 0)))) | |
166 (if (string-match ":$" host) | |
167 (setq host (substring host 0 (match-beginning 0)))) | |
168 (setq save-pos (point)))) | |
169 ;; Now check for references | |
170 (setq save-pos (point)) | |
171 (skip-chars-forward "^#") | |
172 (if (eobp) | |
173 nil | |
174 (delete-region | |
175 (point) | |
176 (progn | |
177 (skip-chars-forward "#") | |
178 (setq refs (buffer-substring (point) (point-max))) | |
179 (point-max)))) | |
180 (goto-char save-pos) | |
181 (skip-chars-forward "^;") | |
182 (if (not (eobp)) | |
183 (setq attr (mm-parse-args (point) (point-max)) | |
184 attr (nreverse attr))) | |
185 (setq file (buffer-substring save-pos (point))) | |
186 (and port (string= port (or (cdr-safe (assoc prot url-default-ports)) | |
187 "")) | |
188 (setq port nil)) | |
189 (if (and host (string-match "%[0-9][0-9]" host)) | |
190 (setq host (url-unhex-string host))) | |
191 (vector prot user pass host port file refs attr full)))))) | |
192 | |
193 (provide 'url-parse) |