1 ;;;; (web uri) --- URI manipulation tools
3 ;;;; Copyright (C) 1997,2001,2002,2010 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 ;; Based on (www url). To be documented.
26 (define-module (web uri)
28 uri-scheme uri-userinfo uri-host uri-port
29 uri-path uri-query uri-fragment
35 split-and-decode-uri-path
36 encode-and-join-uri-path)
37 #:use-module (srfi srfi-9)
38 #:use-module (ice-9 regex)
39 #:use-module (ice-9 control)
40 #:use-module (rnrs bytevectors)
41 #:use-module (rnrs io ports))
43 (define-record-type <uri>
44 (make-uri scheme userinfo host port path query fragment)
47 (userinfo uri-userinfo)
52 (fragment uri-fragment))
54 (define (uri-error message . args)
55 (throw 'uri-error message args))
57 (define (positive-exact-integer? port)
58 (and (number? port) (exact? port) (integer? port) (positive? port)))
60 (define (validate-uri scheme userinfo host port path query fragment)
62 ((not (symbol? scheme))
63 (uri-error "Expected a symbol for the URI scheme: ~s" scheme))
64 ((and (or userinfo port) (not host))
65 (uri-error "Expected a host, given userinfo or port"))
66 ((and port (not (positive-exact-integer? port)))
67 (uri-error "Expected port to be an integer: ~s" port))
68 ((and host (or (not (string? host)) (not (valid-host? host))))
69 (uri-error "Expected valid host: ~s" host))
70 ((and userinfo (not (string? userinfo)))
71 (uri-error "Expected string for userinfo: ~s" userinfo))
73 (uri-error "Expected string for path: ~s" path))
74 ((and host (not (string-null? path))
75 (not (eqv? (string-ref path 0) #\/)))
76 (uri-error "Expected path of absolute URI to start with a /: ~a" path))))
78 (define* (build-uri scheme #:key userinfo host port (path "") query fragment
81 (validate-uri scheme userinfo host port path query fragment))
82 (make-uri scheme userinfo host port path query fragment))
84 ;; See RFC 3986 #3.2.2 for comments on percent-encodings, IDNA (RFC
85 ;; 3490), and non-ASCII host names.
88 (make-regexp "^([0-9.]+)"))
90 (make-regexp "^\\[([0-9a-fA-F:]+)\\]+"))
91 (define domain-label-regexp
92 (make-regexp "^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?$"))
93 (define top-label-regexp
94 (make-regexp "^[a-zA-Z]([a-zA-Z0-9-]*[a-zA-Z0-9])?$"))
96 (define (valid-host? host)
98 ((regexp-exec ipv4-regexp host)
100 (false-if-exception (inet-pton AF_INET (match:substring m 1)))))
101 ((regexp-exec ipv6-regexp host)
103 (false-if-exception (inet-pton AF_INET6 (match:substring m 1)))))
105 (let ((labels (reverse (string-split host #\.))))
107 (regexp-exec top-label-regexp (car labels))
108 (and-map (lambda (label)
109 (regexp-exec domain-label-regexp label))
113 "[a-zA-Z0-9_.!~*'();:&=+$,-]+")
118 (define authority-regexp
120 (format #f "^//((~a)@)?(~a)(:(~a))?$"
121 userinfo-pat host-pat port-pat)))
123 (define (parse-authority authority fail)
124 (let ((m (regexp-exec authority-regexp authority)))
125 (if (and m (valid-host? (match:substring m 3)))
126 (values (match:substring m 2)
127 (match:substring m 3)
128 (let ((port (match:substring m 5)))
129 (and port (not (string-null? port))
130 (string->number port))))
136 ;;; URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
138 ;;; hier-part = "//" authority path-abempty
144 "[a-zA-Z][a-zA-Z0-9+.-]*")
145 (define authority-pat
154 (format #f "^(~a):(//~a)?(~a)(\\?(~a))?(#(~a))?$"
155 scheme-pat authority-pat path-pat query-pat fragment-pat))
157 (make-regexp uri-pat))
159 (define (parse-uri string)
160 (% (let ((m (regexp-exec uri-regexp string)))
162 (let ((scheme (string->symbol
163 (string-downcase (match:substring m 1))))
164 (authority (match:substring m 2))
165 (path (match:substring m 3))
166 (query (match:substring m 5))
167 (fragment (match:substring m 7)))
171 (parse-authority authority abort)
173 (lambda (userinfo host port)
174 (make-uri scheme userinfo host port path query fragment)))))
178 (define *default-ports* (make-hash-table))
180 (define (declare-default-port! scheme port)
181 (hashq-set! *default-ports* scheme port))
183 (define (default-port? scheme port)
185 (eqv? port (hashq-ref *default-ports* scheme))))
187 (declare-default-port! 'http 80)
188 (declare-default-port! 'https 443)
190 (define (unparse-uri uri)
191 (let* ((scheme-str (string-append
192 (symbol->string (uri-scheme uri)) ":"))
193 (userinfo (uri-userinfo uri))
194 (host (uri-host uri))
195 (port (uri-port uri))
196 (path (uri-path uri))
197 (query (uri-query uri))
198 (fragment (uri-fragment uri)))
203 (if userinfo (string-append userinfo "@")
206 (if (default-port? (uri-scheme uri) port)
208 (string-append ":" (number->string port))))
212 (string-append "?" query)
215 (string-append "#" fragment)
219 ;; A note on characters and bytes: URIs are defined to be sequences of
220 ;; characters in a subset of ASCII. Those characters may encode a
221 ;; sequence of bytes (octets), which in turn may encode sequences of
222 ;; characters in other character sets.
225 ;; Return a new string made from uri-decoding @var{str}. Specifically,
226 ;; turn @code{+} into space, and hex-encoded @code{%XX} strings into
227 ;; their eight-bit characters.
230 (string->char-set "0123456789abcdefABCDEF"))
232 (define* (uri-decode str #:key (charset 'utf-8))
233 (let ((len (string-length str)))
234 (call-with-values open-bytevector-output-port
235 (lambda (port get-bytevector)
239 ((utf-8) utf8->string)
240 ((#f) (lambda (x) x)) ; raw bytevector
241 (else (uri-error "Unknown charset: ~s" charset)))
243 (let ((ch (string-ref str i)))
246 (put-u8 port (char->integer #\space))
248 ((and (< (+ i 2) len) (eqv? ch #\%)
249 (let ((a (string-ref str (+ i 1)))
250 (b (string-ref str (+ i 2))))
251 (and (char-set-contains? hex-chars a)
252 (char-set-contains? hex-chars b)
253 (string->number (string a b) 16))))
257 ((< (char->integer ch) 128)
258 (put-u8 port (char->integer ch))
261 (uri-error "Invalid character in encoded URI ~a: ~s"
264 (define ascii-alnum-chars
266 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))
270 (string->char-set ":/?#[]@"))
272 (string->char-set "!$&'()*+,l="))
273 (define reserved-chars
274 (char-set-union gen-delims sub-delims))
277 (define unreserved-chars
278 (char-set-union ascii-alnum-chars
279 (string->char-set "-._~")))
281 ;; Return a new string made from uri-encoding @var{str}, unconditionally
282 ;; transforming any characters not in @var{unescaped-chars}.
284 (define* (uri-encode str #:key (charset 'utf-8)
285 (unescaped-chars unreserved-chars))
286 (define (put-utf8 binary-port str)
287 (put-bytevector binary-port (string->utf8 str)))
290 ((utf-8) utf8->string)
291 ((#f) (lambda (x) x)) ; raw bytevector
292 (else (uri-error "Unknown charset: ~s" charset)))
293 (call-with-values open-bytevector-output-port
294 (lambda (port get-bytevector)
297 (if (char-set-contains? unescaped-chars ch)
298 (put-utf8 port (string ch))
299 (let* ((utf8 (string->utf8 (string ch)))
300 (len (bytevector-length utf8)))
305 (put-utf8 port (string #\%))
307 (number->string (bytevector-u8-ref utf8 i) 16))
312 (define (split-and-decode-uri-path path)
313 (filter (lambda (x) (not (string-null? x)))
314 (map uri-decode (string-split path #\/))))
316 (define (encode-and-join-uri-path parts)
317 (string-join (map uri-encode parts) "/"))