1 ;;;; (web uri) --- URI manipulation tools
3 ;;;; Copyright (C) 1997,2001,2002,2010,2011,2012 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 ;; A data type for Universal Resource Identifiers, as defined in RFC
27 (define-module (web uri)
28 #:use-module (srfi srfi-9)
29 #:use-module (ice-9 regex)
30 #:use-module (ice-9 rdelim)
31 #:use-module (ice-9 control)
32 #:use-module (rnrs bytevectors)
33 #:use-module (ice-9 binary-ports)
35 uri-scheme uri-userinfo uri-host uri-port
36 uri-path uri-query uri-fragment
40 string->uri uri->string
42 split-and-decode-uri-path
43 encode-and-join-uri-path))
45 (define-record-type <uri>
46 (make-uri scheme userinfo host port path query fragment)
49 (userinfo uri-userinfo)
54 (fragment uri-fragment))
56 (define (uri-error message . args)
57 (throw 'uri-error message args))
59 (define (positive-exact-integer? port)
60 (and (number? port) (exact? port) (integer? port) (positive? port)))
62 (define (validate-uri scheme userinfo host port path query fragment)
64 ((not (symbol? scheme))
65 (uri-error "Expected a symbol for the URI scheme: ~s" scheme))
66 ((and (or userinfo port) (not host))
67 (uri-error "Expected a host, given userinfo or port"))
68 ((and port (not (positive-exact-integer? port)))
69 (uri-error "Expected port to be an integer: ~s" port))
70 ((and host (or (not (string? host)) (not (valid-host? host))))
71 (uri-error "Expected valid host: ~s" host))
72 ((and userinfo (not (string? userinfo)))
73 (uri-error "Expected string for userinfo: ~s" userinfo))
75 (uri-error "Expected string for path: ~s" path))
76 ((and host (not (string-null? path))
77 (not (eqv? (string-ref path 0) #\/)))
78 (uri-error "Expected path of absolute URI to start with a /: ~a" path))))
80 (define* (build-uri scheme #:key userinfo host port (path "") query fragment
82 "Construct a URI object. If @var{validate?} is true, also run some
83 consistency checks to make sure that the constructed URI is valid."
85 (validate-uri scheme userinfo host port path query fragment))
86 (make-uri scheme userinfo host port path query fragment))
88 ;; See RFC 3986 #3.2.2 for comments on percent-encodings, IDNA (RFC
89 ;; 3490), and non-ASCII host names.
92 (make-regexp "^([0-9.]+)$"))
94 (make-regexp "^([0-9a-fA-F:.]+)$"))
95 (define domain-label-regexp
96 (make-regexp "^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?$"))
97 (define top-label-regexp
98 (make-regexp "^[a-zA-Z]([a-zA-Z0-9-]*[a-zA-Z0-9])?$"))
100 (define (valid-host? host)
102 ((regexp-exec ipv4-regexp host)
103 (false-if-exception (inet-pton AF_INET host)))
104 ((regexp-exec ipv6-regexp host)
105 (false-if-exception (inet-pton AF_INET6 host)))
108 (let ((end (string-index host #\. start)))
110 (and (regexp-exec domain-label-regexp
111 (substring host start end))
113 (regexp-exec top-label-regexp host start)))))))
116 "[a-zA-Z0-9_.!~*'();:&=+$,-]+")
119 (define ipv6-host-pat
123 (define authority-regexp
125 (format #f "^//((~a)@)?((~a)|(\\[(~a)\\]))(:(~a))?$"
126 userinfo-pat host-pat ipv6-host-pat port-pat)))
128 (define (parse-authority authority fail)
129 (if (equal? authority "//")
130 ;; Allow empty authorities: file:///etc/hosts is a synonym of
133 (let ((m (regexp-exec authority-regexp authority)))
134 (if (and m (valid-host? (or (match:substring m 4)
135 (match:substring m 6))))
136 (values (match:substring m 2)
137 (or (match:substring m 4)
138 (match:substring m 6))
139 (let ((port (match:substring m 8)))
140 (and port (not (string-null? port))
141 (string->number port))))
147 ;;; URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
149 ;;; hier-part = "//" authority path-abempty
155 "[a-zA-Z][a-zA-Z0-9+.-]*")
156 (define authority-pat
165 (format #f "^(~a):(//~a)?(~a)(\\?(~a))?(#(~a))?$"
166 scheme-pat authority-pat path-pat query-pat fragment-pat))
168 (make-regexp uri-pat))
170 (define (string->uri string)
171 "Parse @var{string} into a URI object. Returns @code{#f} if the string
172 could not be parsed."
173 (% (let ((m (regexp-exec uri-regexp string)))
175 (let ((scheme (string->symbol
176 (string-downcase (match:substring m 1))))
177 (authority (match:substring m 2))
178 (path (match:substring m 3))
179 (query (match:substring m 5))
180 (fragment (match:substring m 7)))
184 (parse-authority authority abort)
186 (lambda (userinfo host port)
187 (make-uri scheme userinfo host port path query fragment)))))
191 (define *default-ports* (make-hash-table))
193 (define (declare-default-port! scheme port)
194 "Declare a default port for the given URI scheme.
196 Default ports are for printing URI objects: a default port is not
198 (hashq-set! *default-ports* scheme port))
200 (define (default-port? scheme port)
202 (eqv? port (hashq-ref *default-ports* scheme))))
204 (declare-default-port! 'http 80)
205 (declare-default-port! 'https 443)
207 (define (uri->string uri)
208 "Serialize @var{uri} to a string."
209 (let* ((scheme-str (string-append
210 (symbol->string (uri-scheme uri)) ":"))
211 (userinfo (uri-userinfo uri))
212 (host (uri-host uri))
213 (port (uri-port uri))
214 (path (uri-path uri))
215 (query (uri-query uri))
216 (fragment (uri-fragment uri)))
221 (if userinfo (string-append userinfo "@")
223 (if (string-index host #\:)
224 (string-append "[" host "]")
226 (if (default-port? (uri-scheme uri) port)
228 (string-append ":" (number->string port))))
232 (string-append "?" query)
235 (string-append "#" fragment)
239 ;; like call-with-output-string, but actually closes the port (doh)
240 (define (call-with-output-string* proc)
241 (let ((port (open-output-string)))
243 (let ((str (get-output-string port)))
247 (define (call-with-output-bytevector* proc)
250 (open-bytevector-output-port))
251 (lambda (port get-bytevector)
253 (let ((bv (get-bytevector)))
257 (define (call-with-encoded-output-string encoding proc)
258 (if (string-ci=? encoding "utf-8")
259 (string->utf8 (call-with-output-string* proc))
260 (call-with-output-bytevector*
262 (set-port-encoding! port encoding)
265 (define (encode-string str encoding)
266 (if (string-ci=? encoding "utf-8")
268 (call-with-encoded-output-string encoding
270 (display str port)))))
272 (define (decode-string bv encoding)
273 (if (string-ci=? encoding "utf-8")
275 (let ((p (open-bytevector-input-port bv)))
276 (set-port-encoding! p encoding)
277 (let ((res (read-delimited "" p)))
282 ;; A note on characters and bytes: URIs are defined to be sequences of
283 ;; characters in a subset of ASCII. Those characters may encode a
284 ;; sequence of bytes (octets), which in turn may encode sequences of
285 ;; characters in other character sets.
288 ;; Return a new string made from uri-decoding @var{str}. Specifically,
289 ;; turn @code{+} into space, and hex-encoded @code{%XX} strings into
290 ;; their eight-bit characters.
293 (string->char-set "0123456789abcdefABCDEF"))
295 (define* (uri-decode str #:key (encoding "utf-8"))
296 "Percent-decode the given @var{str}, according to @var{encoding}.
298 Note that this function should not generally be applied to a full URI
299 string. For paths, use split-and-decode-uri-path instead. For query
300 strings, split the query on @code{&} and @code{=} boundaries, and decode
301 the components separately.
303 Note that percent-encoded strings encode @emph{bytes}, not characters.
304 There is no guarantee that a given byte sequence is a valid string
305 encoding. Therefore this routine may signal an error if the decoded
306 bytes are not valid for the given encoding. Pass @code{#f} for
307 @var{encoding} if you want decoded bytes as a bytevector directly."
308 (let* ((len (string-length str))
310 (call-with-output-bytevector*
314 (let ((ch (string-ref str i)))
317 (put-u8 port (char->integer #\space))
319 ((and (< (+ i 2) len) (eqv? ch #\%)
320 (let ((a (string-ref str (+ i 1)))
321 (b (string-ref str (+ i 2))))
322 (and (char-set-contains? hex-chars a)
323 (char-set-contains? hex-chars b)
324 (string->number (string a b) 16))))
328 ((< (char->integer ch) 128)
329 (put-u8 port (char->integer ch))
332 (uri-error "Invalid character in encoded URI ~a: ~s"
335 (decode-string bv encoding)
336 ;; Otherwise return raw bytevector
339 (define ascii-alnum-chars
341 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))
345 (string->char-set ":/?#[]@"))
347 (string->char-set "!$&'()*+,l="))
348 (define reserved-chars
349 (char-set-union gen-delims sub-delims))
352 (define unreserved-chars
353 (char-set-union ascii-alnum-chars
354 (string->char-set "-._~")))
356 ;; Return a new string made from uri-encoding @var{str}, unconditionally
357 ;; transforming any characters not in @var{unescaped-chars}.
359 (define* (uri-encode str #:key (encoding "utf-8")
360 (unescaped-chars unreserved-chars))
361 "Percent-encode any character not in the character set, @var{unescaped-chars}.
363 Percent-encoding first writes out the given character to a bytevector
364 within the given @var{encoding}, then encodes each byte as
365 @code{%@var{HH}}, where @var{HH} is the hexadecimal representation of
367 (define (needs-escaped? ch)
368 (not (char-set-contains? unescaped-chars ch)))
369 (if (string-index str needs-escaped?)
370 (call-with-output-string*
374 (if (char-set-contains? unescaped-chars ch)
376 (let* ((bv (encode-string (string ch) encoding))
377 (len (bytevector-length bv)))
380 (let ((byte (bytevector-u8-ref bv i)))
384 (display (number->string byte 16) port)
389 (define (split-and-decode-uri-path path)
390 "Split @var{path} into its components, and decode each
391 component, removing empty components.
393 For example, @code{\"/foo/bar/\"} decodes to the two-element list,
394 @code{(\"foo\" \"bar\")}."
395 (filter (lambda (x) (not (string-null? x)))
396 (map uri-decode (string-split path #\/))))
398 (define (encode-and-join-uri-path parts)
399 "URI-encode each element of @var{parts}, which should be a list of
400 strings, and join the parts together with @code{/} as a delimiter."
401 (string-join (map uri-encode parts) "/"))