Debian Codegraph @

lib/rfc/uri.scm

raw
1 ;;;
2 ;;; uri.scm - parse and construct URIs
3 ;;;
4 ;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;; Main reference:
35 ;; RFC2396 Uniform Resource Identifiers (URI): Generic Syntax
36 ;; <ftp://ftp.isi.edu/in-notes/rfc2396.txt>
37
38 ;; Historical:
39 ;; RFC1738 Uniform Resource Locators
40 ;; <ftp://ftp.isi.edu/in-notes/rfc1738.txt>
41 ;; RFC1808 Relative Uniform Resource Locators
42 ;; <ftp://ftp.isi.edu/in-notes/rfc1808.txt>
43 ;; RFC2368 The mailto URL Scheme
44 ;; <ftp://ftp.isi.edu/in-notes/rfc2368.txt>
45
46 ;; Also supports 'data' uri scheme specified in RFC2397.
47
48 (define-module rfc.uri
49 (use srfi.13)
50 (use util.match)
51 (use gauche.regexp)
52 (use gauche.charconv)
53 (use gauche.uvector)
54 (export uri-scheme&specific uri-decompose-hierarchical
55 uri-decompose-authority uri-parse uri-ref
56 uri-merge uri-compose
57 uri-decompose-query uri-compose-query
58 uri-decode uri-decode-string
59 uri-encode uri-encode-string
60 *rfc2396-unreserved-char-set*
61 *rfc3986-unreserved-char-set*
62 uri-compose-data uri-decompose-data
63 )
64 )
65 (select-module rfc.uri)
66
67 ;;==============================================================
68 ;; Generic parser
69 ;;
70
71 ;; Splits URI scheme and the scheme specific part from given URI.
72 ;; If URI doesn't follow the generic URI syntax, it is regarded
73 ;; as a relative URI and #f is returned for the scheme.
74 ;; The escaped characters of the scheme specific part is not unescaped;
75 ;; their interpretation is dependent on the scheme.
76
77 (define (uri-scheme&specific uri)
78 (cond [(#/^([A-Za-z][A-Za-z0-9+.-]*):/ uri)
79 => (^m (values (string-downcase (m 1)) (m 'after)))]
80 [else (values #f uri)]))
81
82 (define (uri-decompose-hierarchical specific)
83 (cond
84 [(and (string? specific)
85 (#/^(?:\/\/([^\/?#]*))?([^?#]+)?(?:\?([^#]*))?(?:#(.*))?$/ specific))
86 => (^m (values (m 1) (m 2) (m 3) (m 4)))]
87 [else (values #f #f #f #f)]))
88
89 (define (uri-decompose-authority authority)
90 (cond
91 [(and (string? authority)
92 (#/^(?:(?<userinfo>.*?)@)?(?:(?<host>[^:]*)|(?:\[(?<v6host>[a-fA-F\d:]+)\]))(?::(?<port>\d*))?$/ authority))
93 => (^m (values (m 'userinfo) (or (m 'v6host) (m 'host)) (m 'port)))]
94 [else (values #f #f #f)]))
95
96 ;; A common cliche (suggested by Kouhei Sutou)
97 ;; Returns: scheme, user-info, host, port, path, query, fragment
98 (define (uri-parse uri)
99 (define (filter-non-empty-string str)
100 (and (string? str)
101 (not (string-null? str))
102 str))
103 (receive (scheme specific)
104 (uri-scheme&specific uri)
105 (receive (authority path query fragment)
106 (uri-decompose-hierarchical specific)
107 (receive (user-info host port)
108 (uri-decompose-authority authority)
109 (values scheme
110 user-info
111 (filter-non-empty-string host)
112 (and port (string->number port))
113 (filter-non-empty-string path)
114 query
115 fragment)))))
116
117 ;; Convenience utility
118 ;; (uri-ref "http://foo:8080/baz?q" 'host) => "foo"
119 ;; (uri-ref "http://foo:8080/baz?q" 'host+port) => "foo:8080"
120 ;; (uri-ref "http://foo:8080/baz?q" 'path+query) => "/baz?q"
121 (define (uri-ref uri parts)
122 (receive (scheme userinfo host port path query fragment) (uri-parse uri)
123 (define (get-part part)
124 (ecase part
125 [(scheme) scheme]
126 [(userinfo) userinfo]
127 [(host) host]
128 [(port) port]
129 [(authority) (uri-compose :userinfo userinfo :host host :port port)]
130 [(scheme+authority)
131 (uri-compose :scheme scheme :userinfo userinfo :host host :port port)]
132 [(host+port)
133 (with-output-to-string
134 (^[]
135 (when host (display host))
136 (when port (display ":") (display port))))]
137 [(userinfo+host+port)
138 (with-output-to-string
139 (^[]
140 (when userinfo (display userinfo) (display "@"))
141 (when host (display host))
142 (when port (display ":") (display port))))]
143 [(path) path]
144 [(path+query)
145 (with-output-to-string
146 (^[]
147 (when path (display path))
148 (when query (display "?") (display query))))]
149 [(query) query]
150 [(path+query+fragment)
151 (with-output-to-string
152 (^[]
153 (display path)
154 (when query (display "?") (display query))
155 (when fragment (display "#") (display fragment))))]
156 [(fragment) fragment]))
157 (if (list? parts)
158 (map get-part parts)
159 (get-part parts))))
160
161 ;;==============================================================
162 ;; Generic constructor
163 ;;
164
165 (define (uri-compose :key (scheme #f) (userinfo #f) (host #f) (port #f)
166 (authority #f) (path #f) (path* #f) (query #f)
167 (fragment #f) (specific #f))
168 (with-output-to-string
169 (^[]
170 (when scheme (display scheme) (display ":"))
171 (if specific
172 (display specific)
173 (begin
174 (display "//")
175 (if authority
176 (begin (display authority))
177 (begin
178 (when userinfo (display userinfo) (display "@"))
179 (when host (display host))
180 (when port (display ":") (display port))))
181 (if path*
182 (begin
183 (unless (string-prefix? "/" path*) (display "/"))
184 (display path*))
185 (begin
186 (if path
187 (begin (unless (string-prefix? "/" path) (display "/"))
188 (display path))
189 (display "/"))
190 (when query (display "?") (display query))
191 (when fragment (display "#") (display fragment))))
192 ))
193 )))
194
195 ;;==============================================================
196 ;; Query string handling
197 ;; This is the basis of cgi-parse-parameters etc.
198 ;;
199 ;; NB: Query string syntax (aka application/x-www-form-urlencoded) is
200 ;; not cleary defined in RFC. The most definitive source might be
201 ;; the HTML4 specification, section 17.13.4 "Form content types",
202 ;; <http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1>.
203
204 (define (uri-decompose-query query-string :key (separators #[&\;]))
205 (map (^[elt] (let1 ss (string-split elt #\= 'infix 1)
206 (list (uri-decode-string (car ss) :cgi-decode #t)
207 (if (null? (cdr ss))
208 #t
209 (uri-decode-string (cadr ss) :cgi-decode #t)))))
210 (string-split query-string separators)))
211
212 (define (uri-compose-query params
213 :optional (encoding (gauche-character-encoding)))
214 (define (esc s) (uri-encode-string (x->string s) :encoding encoding))
215 (define (query-1 n&v)
216 (match n&v
217 [(name #t) #"~(esc name)"]
218 [(name value) #"~(esc name)=~(esc value)"]
219 [_ (error "Invalid request-uri form:" n&v)]))
220 (if (null? params)
221 ""
222 (string-concatenate (intersperse "&" (map query-1 params)))))
223
224 ;;==============================================================
225 ;; Relative -> Absolute
226 ;;
227
228 ;; This function is originally implemented by teppey
229 ;; (from http://d.hatena.ne.jp/teppey/20110517/1305613493).
230 ;; By the author's permission I took it and modified to include here.
231 ;; NB: CL has (merge-pathname <path> <base>). I always felt it
232 ;; counterintuitive, since <path> is likely to be concatenated
233 ;; after (some part of) <base>, unless <path> fully specifies all components.
234 ;; Here we reverse the arguments, so that we can merge multiple fragments
235 ;; of uris.
236 (define (uri-merge base-uri rel-uri . more)
237 (define (rec base-uri rel-uri more initial?)
238 (if (null? more)
239 (uri-merge-1 base-uri rel-uri initial?)
240 (rec (uri-merge-1 base-uri rel-uri initial?) (car more) (cdr more) #f)))
241 (rec base-uri rel-uri more #t))
242
243 (define (uri-merge-1 base-uri rel-uri pre-normalize?)
244 (define (split uri)
245 (receive (scheme specific)
246 (uri-scheme&specific uri)
247 (receive (authority path query fragment)
248 (uri-decompose-hierarchical specific)
249 (values scheme authority path query fragment))))
250
251 ;; RFC3986, Section 5.2.3
252 ;; we do prenormalize base-path, so it never be #f.
253 (define (merge base-path rel-path base-authority)
254 (if (and base-authority (string-null? base-path))
255 (string-append "/" rel-path)
256 (if-let1 m (#/^(.*)\/[^\/]*$/ base-path)
257 (string-append (m 1) "/" rel-path)
258 rel-path)))
259
260 ;; RFC3986, Section 5.2.4
261 (define (canonicalize path)
262 (let loop ([input path] [output '()])
263 (cond [(not input) (string-concatenate (reverse! output))]
264 ;; RFC3986, Section 5.2.4, 2A
265 [(#/^\.{1,2}\/(.*)$/ input) => (^m (loop (m 1) output))]
266 ;; RFC3986, Section 5.2.4, 2B
267 [(#/^(?:\/\.\/(.*)|\/\.)$/ input)
268 => (^m (loop (string-append "/" (or (m 1) "")) output))]
269 ;; RFC3986, Section 5.2.4, 2C
270 [(#/^(?:\/\.\.\/(.*)|\/\.\.)$/ input)
271 => (^m (loop (string-append "/" (or (m 1) ""))
272 (if (not (null? output)) (cdr output) output)))]
273 ;; RFC3986, Section 5.2.4, 2D
274 [(#/^\.{1,2}$/ input) (loop #f output)]
275 ;; RFC3986, Section 5.2.4, 2E
276 [(#/^(\/?[^\/]+)(\/.*)?$/ input)
277 => (^m (loop (m 2) (cons (m 1) output)))]
278 [else (loop #f (cons input output))])))
279
280 ;; RFC3986, Section 5.3
281 (define (recompose scheme authority path query fragment)
282 (with-output-to-string
283 (^[]
284 (when scheme (display scheme) (display ":"))
285 (when authority (display "//") (display authority))
286 (display path)
287 (when query (display "?") (display query))
288 (when fragment (display "#") (display fragment)))))
289
290 (receive (b.scheme b.authority b.path b.query b.fragment) (split base-uri)
291 (when pre-normalize?
292 (set! b.path (canonicalize b.path))) ;pre-normalization (section 5.2.1)
293 (receive (r.scheme r.authority r.path r.query r.fragment) (split rel-uri)
294 (recompose (or r.scheme b.scheme)
295 (if r.scheme r.authority (or r.authority b.authority))
296 (cond [(or r.scheme r.authority) (canonicalize r.path)]
297 [(not r.path) b.path]
298 [(string-prefix? "/" r.path) (canonicalize r.path)]
299 [else (canonicalize (merge b.path r.path b.authority))])
300 (if (or r.scheme r.authority r.path r.query) r.query b.query)
301 r.fragment))))
302
303 ;;==============================================================
304 ;; Encoding & decoding
305 ;;
306 ;; NB. Which character to encode, and when to encode/decode depend on
307 ;; the semantics of specific URI scheme.
308 ;; These procedures provides basic building components.
309
310 (define (uri-decode :key (cgi-decode #f))
311 (let loop ([c (read-char)])
312 (cond [(eof-object? c)]
313 [(char=? c #\%)
314 (let1 c1 (read-char)
315 (cond
316 [(eof-object? c1) (write-char c)] ;; just be permissive
317 [(digit->integer c1 16)
318 => (^[i1]
319 (let1 c2 (read-char)
320 (cond [(eof-object? c2) (write-char c) (write-char c1)]
321 [(digit->integer c2 16)
322 => (^[i2]
323 (write-byte (+ (* i1 16) i2))
324 (loop (read-char)))]
325 [else (write-char c) (write-char c1) (loop c2)])))]
326 [else (write-char c) (loop c1)]))]
327 [(char=? c #\+)
328 (if cgi-decode (write-char #\space) (write-char #\+))
329 (loop (read-char))]
330 [else (write-char c) (loop (read-char))])))
331
332 (define (uri-decode-string string :key (encoding (gauche-character-encoding))
333 :allow-other-keys args)
334 (define (wrap out)
335 (wrap-with-output-conversion out (gauche-character-encoding)
336 :from-code encoding))
337 (call-with-string-io string
338 (^[in out]
339 (with-ports in (wrap out) (current-error-port)
340 (^[]
341 (apply uri-decode args)
342 (close-output-port (current-output-port)))))))
343
344 ;; Default set of characters that can be passed without escaping.
345 ;; See 2.3 "Unreserved Characters" of RFC 2396. It is slightly
346 ;; revised in RFC3986.
347 (define-constant *rfc2396-unreserved-char-set* #[-_.!~*'()0-9A-Za-z])
348 (define-constant *rfc3986-unreserved-char-set* #[-_.~0-9A-Za-z])
349
350 ;; NB: Converts byte by byte, instead of chars, to avoid complexity
351 ;; from different character encodings (suggested by Fumitoshi UKAI).
352 ;; 'noescape' char-set is only valid in ASCII range. All bytes
353 ;; larger than #x80 are encoded unconditionally.
354 (define (uri-encode :key ((:noescape echars) *rfc3986-unreserved-char-set*))
355 (let loop ([b (read-byte)])
356 (unless (eof-object? b)
357 (if (and (< b #x80)
358 (char-set-contains? echars (integer->char b)))
359 (write-byte b)
360 (format #t "%~2,'0X" b))
361 (loop (read-byte)))))
362
363 (define (uri-encode-string string :key (encoding (gauche-character-encoding))
364 :allow-other-keys args)
365 (define (wrap in)
366 (wrap-with-input-conversion in (gauche-character-encoding)
367 :to-code encoding))
368 (call-with-string-io string
369 (^[in out]
370 (with-ports (wrap in) out (current-error-port)
371 (cut apply uri-encode args)))))
372
373 ;;==============================================================
374 ;; Data uri scheme (rfc2397)
375 ;;
376
377 (autoload rfc.base64
378 base64-encode base64-encode-string
379 base64-decode base64-decode-string)
380 (autoload gauche.vport open-input-uvector)
381 (autoload rfc.mime mime-parse-content-type)
382
383 (define (uri-compose-data data :key (content-type #f) (encoding #f))
384 (define data-is-string
385 (and (string? data) (not (string-incomplete? data))))
386 (let ([encoding (or encoding (if data-is-string 'uri 'base64))]
387 [content-type (or content-type
388 (if data-is-string
389 "text/plain;charset=utf-8"
390 "application/octet-stream"))])
391 (define (encode-by-uri)
392 (unless data-is-string
393 (error "data must be a complete string for uri-encoding data scheme:"
394 data))
395 (uri-encode-string data))
396 (define (encode-by-base64)
397 (cond [(string? data) (base64-encode-string data)]
398 [(u8vector? data) (with-output-to-string
399 (cut with-input-from-port
400 (open-input-uvector data)
401 base64-encode))]
402 [else
403 (error "data must be a string or u8vector for base64 data scheme:"
404 data)]))
405 (define (compose-content-type ct)
406 ;; We allow (type subtype (param . value) ...) in content-type.
407 ;; We don't use mime-compose-parameters to encode the content-type,
408 ;; however, since it may use quoted-string for value. See section 3
409 ;; of rfc2397 for the reason to avoid quited-string.
410 (if (pair? ct)
411 (format "~a/~a~a" (car ct) (cadr ct)
412 (string-join (map (^p (format "~a=~a"
413 (uri-encode-string (car p))
414 (uri-encode-string (cdr p))))
415 (cddr ct))
416 ";" 'prefix))
417 ct))
418
419 (format "data:~a~a,~a" (compose-content-type content-type)
420 (if (eq? encoding 'uri) "" #";~encoding")
421 (ecase encoding
422 [(uri) (encode-by-uri)]
423 [(base64) (encode-by-base64)]))))
424
425 ;; Returns parsed content-type and decoded data.
426 ;;
427 ;; Decoded data is a string if content-type is text/*, and
428 ;; u8vector otherwise. In case of content-type being text/*, charset
429 ;; is recognized and ces is converted appropriately.
430 ;; NB: We may add keyword arg to specify the return type.
431 ;;
432 ;; For the convenience, you can pass either full uri (with "data:")
433 ;; or just a specific part (without "data:"). Result is undefined if you
434 ;; pass non-data uri.
435 (define (uri-decompose-data uri)
436 (rxmatch-case uri
437 [#/^(?:data:)?(.*?)(\;base64)?,(.*)/ (_ ct enc data)
438 (match (mime-parse-content-type ct)
439 [(and (type subtype attrs ...) content-type)
440 ;; TODO: If we have efficient output-to-bytevector interface,
441 ;; we might revise this code so that we won't use intermediate string.
442 ;; (open-output-uvector can't be used yet since output is fixed-length)
443 (let* ([ces (assoc-ref attrs "charset")]
444 [encoded (if enc
445 (let1 str (base64-decode-string data)
446 (if ces
447 (ces-convert str ces)
448 str))
449 (uri-decode-string data :encoding ces))])
450 (if (equal? type "text")
451 (values content-type encoded)
452 (values content-type (string->u8vector encoded))))]
453 [_ (error "invalid content-type in data uri:" ct)])]
454 [else (error "invalid data uri:" uri)]))