1 ;;; "uri.scm" Construct and decode Uniform Resource Identifiers. -*-scheme-*-
2 ; Copyright 1997, 1998, 2000, 2001 Aubrey Jaffer
4 ;Permission to copy this software, to redistribute it, and to use it
5 ;for any purpose is granted, subject to the following restrictions and
8 ;1. Any copy made of this software must include this copyright notice
11 ;2. I have made no warrantee or representation that the operation of
12 ;this software will be error-free, and I am under no obligation to
13 ;provide any services, by way of maintenance, update, or otherwise.
15 ;3. In conjunction with products arising from the use of this
16 ;material, there shall be no use of my name in any advertising,
17 ;promotional, or sales literature without prior written consent in
22 (require 'string-case)
23 (require 'string-search)
24 (require 'common-list-functions)
26 ;;@code{(require 'uri)}
28 ;;@noindent Implements @dfn{Uniform Resource Identifiers} (URI) as
29 ;;described in RFC 2396.
33 ;;@args query fragment
34 ;;@args path query fragment
35 ;;@args authority path query fragment
36 ;;@args scheme authority path query fragment
38 ;;Returns a Uniform Resource Identifier string from component arguments.
39 (define (make-uri . args)
40 (define nargs (length args))
41 (set! args (reverse args))
42 (let ((fragment (if (>= nargs 1) (car args) #f))
43 (query (if (>= nargs 2) (cadr args) #f))
44 (path (if (>= nargs 3) (caddr args) #f))
45 (authority (if (>= nargs 4) (cadddr args) #f))
46 (scheme (if (>= nargs 5) (list-ref args 4) #f)))
48 (if scheme (sprintf #f "%s:" scheme) "")
49 (cond ((string? authority)
50 (sprintf #f "//%s" (uric:encode authority "$,;:@&=+")))
52 (apply (lambda (userinfo host port)
53 (cond ((and userinfo port)
54 (sprintf #f "//%s@%s:%d"
55 (uric:encode userinfo "$,;:&=+")
59 (uric:encode userinfo "$,;:&=+")
62 (sprintf #f "//%s:%d" host port))
65 (else (or authority "")))
66 (cond ((string? path) (uric:encode path "/$,;:@&=+"))
68 ((list? path) (uri:make-path path))
70 (if query (sprintf #f "?%s" (uric:encode query "?/$,;:@&=+")) "")
71 (if fragment (sprintf #f "#%s" (uric:encode fragment "?/$,;:@&=+")) ""))))
73 (define (uri:make-path path)
75 (uric:encode (car path) "$,;:@&=+")
76 (map (lambda (pth) (string-append "/" (uric:encode pth "$,;:@&=+")))
79 ;;@body Returns a string which defines this location in the (HTML) file
80 ;;as @1. The hypertext @samp{<A HREF="#@1">} will link to this point.
83 ;;(html:anchor "(section 7)")
85 ;;"<A NAME=\"(section%207)\"></A>"
87 (define (html:anchor name)
88 (sprintf #f "<A NAME=\"%s\"></A>" (uric:encode name "#?/:@;=")))
90 ;;@body Returns a string which links the @2 text to @1.
93 ;;(html:link (make-uri "(section 7)") "section 7")
95 ;;"<A HREF=\"#(section%207)\">section 7</A>"
97 (define (html:link uri highlighted)
98 (sprintf #f "<A HREF=\"%s\">%s</A>" uri highlighted))
100 ;;@body Returns a string specifying the @dfn{base} @1 of a document, for
101 ;;inclusion in the HEAD of the document (@pxref{HTML, head}).
102 (define (html:base uri)
103 (sprintf #f "<BASE HREF=\"%s\">" uri))
105 ;;@body Returns a string specifying the search @1 of a document, for
106 ;;inclusion in the HEAD of the document (@pxref{HTML, head}).
107 (define (html:isindex prompt)
108 (sprintf #f "<ISINDEX PROMPT=\"%s\">" prompt))
110 ;;@body Returns a list of 5 elements corresponding to the parts
111 ;;(@var{scheme} @var{authority} @var{path} @var{query} @var{fragment})
112 ;;of string @1. Elements corresponding to absent parts are #f.
114 ;;The @var{path} is a list of strings. If the first string is empty,
115 ;;then the path is absolute; otherwise relative.
117 ;;If the @var{authority} component is a
118 ;;@dfn{Server-based Naming Authority}, then it is a list of the
119 ;;@var{userinfo}, @var{host}, and @var{port} strings (or #f). For other
120 ;;types of @var{authority} components the @var{authority} will be a
124 ;;(uri->tree "http://www.ics.uci.edu/pub/ietf/uri/#Related")
126 ;;(http "www.ics.uci.edu" ("" "pub" "ietf" "uri" "") #f "Related")
128 (define (uri->tree uri-reference . base-tree)
129 (define split (uri:split uri-reference))
130 (apply (lambda (b-scheme b-authority b-path b-query b-fragment)
132 (lambda (scheme authority path query fragment)
134 (and (equal? "" path) (not scheme) (not authority) (not query)))
136 (string-ci->symbol scheme)
139 (uri:decode-authority authority)
144 (map uric:decode (uri:split-fields path #\/))
145 (and (not authority) (not scheme) b-path)))
149 (or (and fragment (uric:decode fragment))
150 (and uri-empty? b-fragment))))
152 (if (or (car split) (null? base-tree) (car split))
156 (define (uri:decode-path path-list base-path)
157 (cond ((and (equal? "" (car path-list))
158 (not (equal? '("") path-list)))
161 (let* ((cpath0 (append (butlast base-path 1) path-list))
163 (let remove ((l cpath0) (result '()))
164 (cond ((null? l) (reverse result))
165 ((not (equal? "." (car l)))
166 (remove (cdr l) (cons (car l) result)))
168 (reverse (cons "" result)))
169 (else (remove (cdr l) result)))))
171 (let remove ((l cpath1) (result '()))
172 (cond ((null? l) (reverse result))
173 ((not (equal? ".." (car l)))
174 (remove (cdr l) (cons (car l) result)))
176 (equal? "" (car result)))
177 (slib:warn 'uri:decode-path cpath1)
178 (append (reverse result) l))
180 (reverse (cons "" (cdr result))))
181 (else (remove (cdr l) (cdr result)))))))
185 (define (uri:decode-authority authority)
186 (define idx-at (string-index authority #\@))
187 (let* ((userinfo (and idx-at (uric:decode (substring authority 0 idx-at))))
190 (substring authority (+ 1 idx-at) (string-length authority))
192 (idx-: (string-index hostport #\:))
193 (host (if idx-: (substring hostport 0 idx-:) hostport))
195 (substring hostport (+ 1 idx-:) (string-length hostport)))))
196 (if (or userinfo port)
197 (list userinfo host (or (string->number port) port))
200 (define uri:split-fields
201 (let ((cr (integer->char #xd)))
203 (define idx (string-index txt chr))
205 (cons (substring txt 0
206 (if (and (positive? idx)
207 (char=? cr (string-ref txt (+ -1 idx))))
210 (uri:split-fields (substring txt (+ 1 idx) (string-length txt))
214 ;; @body Converts a @dfn{URI} encoded @1 to a query-alist.
215 (define (uri:decode-query query-string)
216 (set! query-string (string-subst query-string " " "" "+" " "))
218 (edx (string-index query-string #\=)
219 (string-index query-string #\=)))
221 (let* ((rxt (substring query-string (+ 1 edx) (string-length query-string)))
222 (adx (string-index rxt #\&))
224 (substring rxt 0 (or adx (string-length rxt)))))
225 (name (string-ci->symbol
226 (uric:decode (substring query-string 0 edx)))))
227 (set! lst (append lst (if (equal? "" urid)
229 (map (lambda (value) (list name value))
230 (uri:split-fields urid #\newline)))))
232 (if adx (substring rxt (+ 1 adx) (string-length rxt)) "")))))
234 (define (uri:split uri-reference)
235 (define len (string-length uri-reference))
236 (define idx-sharp (string-index uri-reference #\#))
237 (let ((fragment (and idx-sharp
238 (substring uri-reference (+ 1 idx-sharp) len)))
240 (and (not (zero? idx-sharp))
241 (substring uri-reference 0 idx-sharp))
244 (let* ((len (string-length uri))
245 (idx-? (string-index uri #\?))
246 (query (and idx-? (substring uri (+ 1 idx-?) len)))
248 (and (not (zero? idx-?)) (substring uri 0 idx-?))
251 (let* ((len (string-length front))
252 (idx-: (string-index front #\:))
253 (scheme (and idx-: (substring front 0 idx-:)))
255 (substring front (+ 1 idx-:) len)
257 (cond ((eqv? 0 (substring? "//" path))
258 (set! len (string-length path))
259 (set! path (substring path 2 len))
260 (set! len (+ -2 len))
261 (let* ((idx-/ (string-index path #\/))
262 (authority (substring path 0 (or idx-/ len)))
264 (substring path idx-/ len)
266 (list scheme authority path query fragment)))
267 (else (list scheme #f path query fragment))))
268 (list #f #f "" query fragment)))
269 (list #f #f "" #f fragment))))
272 ;;@noindent @code{uric:} prefixes indicate procedures dealing with
275 ;;@body Returns a copy of the string @1 in which all @dfn{unsafe} octets
276 ;;(as defined in RFC 2396) have been @samp{%} @dfn{escaped}.
277 ;;@code{uric:decode} decodes strings encoded by @0.
278 (define (uric:encode uri-component allows)
279 (set! uri-component (sprintf #f "%a" uri-component))
282 (if (or (char-alphabetic? chr)
284 (string-index "-_.!~*'()" chr)
285 (string-index allows chr))
287 (let ((code (char->integer chr)))
288 (sprintf #f "%%%02x" code))))
289 (string->list uri-component))))
291 ;;@body Returns a copy of the string @1 in which each @samp{%} escaped
292 ;;characters in @1 is replaced with the character it encodes. This
293 ;;routine is useful for showing URI contents on error pages.
294 (define (uric:decode uri-component)
295 (define len (string-length uri-component))
298 ((string-index uri #\%)
300 (if (and (< (+ 2 idx) len)
301 (string->number (substring uri (+ 1 idx) (+ 2 idx)) 16)
302 (string->number (substring uri (+ 2 idx) (+ 3 idx)) 16))
304 (substring uri 0 idx)
305 (string (integer->char
307 (substring uri (+ 1 idx) (+ 3 idx))
309 (sub (substring uri (+ 3 idx) (string-length uri)))))))
313 (define (uri:path->keys path-list ptypes)
314 (and (not (null? path-list))
315 (not (equal? '("") path-list))
316 (let ((path (uri:decode-path (map uric:decode path-list) #f)))
317 (and (= (length path) (length ptypes))
318 (map coerce path ptypes)))))