3 ;; Copyright (C) 2010, 2011 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
22 ;;; This module has a number of routines to parse textual
23 ;;; representations of HTTP data into native Scheme data structures.
25 ;;; It tries to follow RFCs fairly strictly---the road to perdition
26 ;;; being paved with compatibility hacks---though some allowances are
27 ;;; made for not-too-divergent texts (like a quality of .2 which should
32 (define-module (web http)
33 #:use-module ((srfi srfi-1) #:select (append-map! map!))
34 #:use-module (srfi srfi-9)
35 #:use-module (srfi srfi-19)
36 #:use-module (ice-9 regex)
37 #:use-module (ice-9 rdelim)
38 #:use-module (web uri)
39 #:export (string->header
68 ;;; Look at quality lists with more insight.
69 ;;; Think about `accept' a bit more.
73 (define (string->header name)
74 "Parse @var{name} to a symbolic header name."
75 (string->symbol (string-downcase name)))
77 (define-record-type <header-decl>
78 (make-header-decl name parser validator writer multiple?)
80 (name header-decl-name)
81 (parser header-decl-parser)
82 (validator header-decl-validator)
83 (writer header-decl-writer)
84 (multiple? header-decl-multiple?))
87 (define *declared-headers* (make-hash-table))
89 (define (lookup-header-decl sym)
90 (hashq-ref *declared-headers* sym))
92 (define* (declare-header! name
97 "Define a parser, validator, and writer for the HTTP header, @var{name}.
99 @var{parser} should be a procedure that takes a string and returns a
100 Scheme value. @var{validator} is a predicate for whether the given
101 Scheme value is valid for this header. @var{writer} takes a value and a
102 port, and writes the value to the port."
103 (if (and (string? name) parser validator writer)
104 (let ((decl (make-header-decl name parser validator writer multiple?)))
105 (hashq-set! *declared-headers* (string->header name) decl)
107 (error "bad header decl" name parser validator writer multiple?)))
109 (define (header->string sym)
110 "Return the string form for the header named @var{sym}."
111 (let ((decl (lookup-header-decl sym)))
113 (header-decl-name decl)
114 (string-titlecase (symbol->string sym)))))
116 (define (known-header? sym)
117 "Return @code{#t} if there are parsers and writers registered for this
118 header, otherwise @code{#f}."
119 (and (lookup-header-decl sym) #t))
121 (define (header-parser sym)
122 "Returns a procedure to parse values for the given header."
123 (let ((decl (lookup-header-decl sym)))
125 (header-decl-parser decl)
128 (define (header-validator sym)
129 "Returns a procedure to validate values for the given header."
130 (let ((decl (lookup-header-decl sym)))
132 (header-decl-validator decl)
135 (define (header-writer sym)
136 "Returns a procedure to write values for the given header to a given
138 (let ((decl (lookup-header-decl sym)))
140 (header-decl-writer decl)
143 (define (read-line* port)
144 (let* ((pair (%read-line port))
147 (if (and (string? line) (char? delim))
148 (let ((orig-len (string-length line)))
149 (let lp ((len orig-len))
151 (char-whitespace? (string-ref line (1- len))))
155 (substring line 0 len)))))
156 (bad-header '%read line))))
158 (define (read-continuation-line port val)
159 (if (or (eqv? (peek-char port) #\space)
160 (eqv? (peek-char port) #\tab))
161 (read-continuation-line port
167 (define *eof* (call-with-input-string "" read))
169 (define (read-header port)
170 "Reads one HTTP header from @var{port}. Returns two values: the header
171 name and the parsed Scheme value. May raise an exception if the header
172 was known but the value was invalid.
174 Returns the end-of-file object for both values if the end of the message
175 body was reached (i.e., a blank line)."
176 (let ((line (read-line* port)))
177 (if (or (string-null? line)
178 (string=? line "\r"))
180 (let* ((delim (or (string-index line #\:)
181 (bad-header '%read line)))
182 (sym (string->header (substring line 0 delim))))
187 (read-continuation-line
189 (string-trim-both line char-whitespace? (1+ delim)))))))))
191 (define (parse-header sym val)
192 "Parse @var{val}, a string, with the parser registered for the header
195 Returns the parsed value. If a parser was not found, the value is
196 returned as a string."
197 ((header-parser sym) val))
199 (define (valid-header? sym val)
200 "Returns a true value iff @var{val} is a valid Scheme value for the
201 header with name @var{sym}."
203 ((header-validator sym) val)
204 (error "header name not a symbol" sym)))
206 (define (write-header sym val port)
207 "Writes the given header name and value to @var{port}. If @var{sym}
208 is a known header, uses the specific writer registered for that header.
209 Otherwise the value is written using @var{display}."
210 (display (header->string sym) port)
212 ((header-writer sym) val port)
213 (display "\r\n" port))
215 (define (read-headers port)
216 "Read an HTTP message from @var{port}, returning the headers as an
218 (let lp ((headers '()))
219 (call-with-values (lambda () (read-header port))
223 (lp (acons k v headers)))))))
225 (define (write-headers headers port)
226 "Write the given header alist to @var{port}. Doesn't write the final
227 \\r\\n, as the user might want to add another header."
228 (let lp ((headers headers))
231 (write-header (caar headers) (cdar headers) port)
232 (lp (cdr headers))))))
241 (define (bad-header sym val)
242 (throw 'bad-header sym val))
243 (define (bad-header-component sym val)
244 (throw 'bad-header sym val))
246 (define (parse-opaque-string str)
248 (define (validate-opaque-string val)
250 (define (write-opaque-string val port)
253 (define separators-without-slash
254 (string->char-set "[^][()<>@,;:\\\"?= \t]"))
255 (define (validate-media-type str)
256 (let ((idx (string-index str #\/)))
257 (and idx (= idx (string-rindex str #\/))
258 (not (string-index str separators-without-slash)))))
259 (define (parse-media-type str)
260 (if (validate-media-type str)
262 (bad-header-component 'media-type str)))
264 (define* (skip-whitespace str #:optional (start 0) (end (string-length str)))
266 (if (and (< i end) (char-whitespace? (string-ref str i)))
270 (define* (trim-whitespace str #:optional (start 0) (end (string-length str)))
272 (if (and (< start i) (char-whitespace? (string-ref str (1- i))))
276 (define* (split-and-trim str #:optional (delim #\,)
277 (start 0) (end (string-length str)))
280 (let* ((idx (string-index str delim i end))
281 (tok (string-trim-both str char-whitespace? i (or idx end))))
282 (cons tok (split-and-trim str delim (if idx (1+ idx) end) end)))
285 (define (list-of-strings? val)
286 (list-of? val string?))
288 (define (write-list-of-strings val port)
289 (write-list val port display ", "))
291 (define (split-header-names str)
292 (map string->header (split-and-trim str)))
294 (define (list-of-header-names? val)
295 (list-of? val symbol?))
297 (define (write-header-list val port)
300 (display (header->string x) port))
303 (define (collect-escaped-string from start len escapes)
304 (let ((to (make-string len)))
305 (let lp ((start start) (i 0) (escapes escapes))
308 (substring-move! from start (+ start (- len i)) to i)
310 (let* ((e (car escapes))
311 (next-start (+ start (- e i) 2)))
312 (substring-move! from start (- next-start 2) to i)
313 (string-set! to e (string-ref from (- next-start 1)))
314 (lp next-start (1+ e) (cdr escapes)))))))
316 ;; in incremental mode, returns two values: the string, and the index at
317 ;; which the string ended
318 (define* (parse-qstring str #:optional
319 (start 0) (end (trim-whitespace str start))
321 (if (and (< start end) (eqv? (string-ref str start) #\"))
322 (let lp ((i (1+ start)) (qi 0) (escapes '()))
324 (case (string-ref str i)
326 (lp (+ i 2) (1+ qi) (cons qi escapes)))
328 (let ((out (collect-escaped-string str (1+ start) qi escapes)))
333 (bad-header-component 'qstring str)))))
335 (lp (1+ i) (1+ qi) escapes)))
336 (bad-header-component 'qstring str)))
337 (bad-header-component 'qstring str)))
339 (define (write-list l port write-item delim)
342 (write-item (car l) port)
348 (define (write-qstring str port)
350 (if (string-index str #\")
352 (write-list (string-split str #\") port display "\\\"")
356 (define* (parse-quality str #:optional (start 0) (end (string-length str)))
357 (define (char->decimal c)
358 (let ((i (- (char->integer c) (char->integer #\0))))
359 (if (and (<= 0 i) (< i 10))
361 (bad-header-component 'quality str))))
364 (bad-header-component 'quality str))
365 ((eqv? (string-ref str start) #\1)
366 (if (or (string= str "1" start end)
367 (string= str "1." start end)
368 (string= str "1.0" start end)
369 (string= str "1.00" start end)
370 (string= str "1.000" start end))
372 (bad-header-component 'quality str)))
373 ((eqv? (string-ref str start) #\0)
374 (if (or (string= str "0" start end)
375 (string= str "0." start end))
377 (if (< 2 (- end start) 6)
378 (let lp ((place 1) (i (+ start 4)) (q 0))
380 (if (eqv? (string-ref str (1+ start)) #\.)
382 (bad-header-component 'quality str))
383 (lp (* 10 place) (1- i)
385 (+ q (* place (char->decimal (string-ref str i))))
387 (bad-header-component 'quality str))))
388 ;; Allow the nonstandard .2 instead of 0.2.
389 ((and (eqv? (string-ref str start) #\.)
390 (< 1 (- end start) 5))
391 (let lp ((place 1) (i (+ start 3)) (q 0))
394 (lp (* 10 place) (1- i)
396 (+ q (* place (char->decimal (string-ref str i))))
399 (bad-header-component 'quality str))))
401 (define (valid-quality? q)
402 (and (non-negative-integer? q) (<= q 1000)))
404 (define (write-quality q port)
405 (define (digit->char d)
406 (integer->char (+ (char->integer #\0) d)))
407 (display (digit->char (modulo (quotient q 1000) 10)) port)
409 (display (digit->char (modulo (quotient q 100) 10)) port)
410 (display (digit->char (modulo (quotient q 10) 10)) port)
411 (display (digit->char (modulo q 10)) port))
413 (define (list-of? val pred)
417 (list-of? (cdr val) pred))))
419 (define* (parse-quality-list str)
422 ((string-rindex part #\;)
424 (let ((qpart (string-trim-both part char-whitespace? (1+ idx))))
425 (if (string-prefix? "q=" qpart)
426 (cons (parse-quality qpart 2)
427 (string-trim-both part char-whitespace? 0 idx))
428 (bad-header-component 'quality qpart)))))
430 (cons 1000 (string-trim-both part char-whitespace?)))))
431 (string-split str #\,)))
433 (define (validate-quality-list l)
437 (valid-quality? (car elt))
438 (string? (cdr elt))))))
440 (define (write-quality-list l port)
449 (write-quality q port)))))
452 (define* (parse-non-negative-integer val #:optional (start 0)
453 (end (string-length val)))
454 (define (char->decimal c)
455 (let ((i (- (char->integer c) (char->integer #\0))))
456 (if (and (<= 0 i) (< i 10))
458 (bad-header-component 'non-negative-integer val))))
459 (if (not (< start end))
460 (bad-header-component 'non-negative-integer val)
461 (let lp ((i start) (out 0))
464 (+ (* out 10) (char->decimal (string-ref val i))))
467 (define (non-negative-integer? code)
468 (and (number? code) (>= code 0) (exact? code) (integer? code)))
470 (define (default-val-parser k val)
473 (define (default-val-validator k val)
476 (define (default-val-writer k val port)
477 (if (or (string-index val #\;)
478 (string-index val #\,)
479 (string-index val #\"))
480 (write-qstring val port)
483 (define* (parse-key-value-list str #:optional
484 (val-parser default-val-parser)
485 (start 0) (end (string-length str)))
486 (let lp ((i start) (out '()))
489 (let* ((i (skip-whitespace str i end))
490 (eq (string-index str #\= i end))
491 (comma (string-index str #\, i end))
492 (delim (min (or eq end) (or comma end)))
494 (substring str i (trim-whitespace str i delim)))))
497 (if (and eq (or (not comma) (< eq comma)))
498 (let ((i (skip-whitespace str (1+ eq) end)))
499 (if (and (< i end) (eqv? (string-ref str i) #\"))
500 (parse-qstring str i end #:incremental? #t)
501 (values (substring str i
502 (trim-whitespace str i
506 (lambda (v-str next-i)
507 (let ((v (val-parser k v-str))
508 (i (skip-whitespace str next-i end)))
509 (if (or (= i end) (eqv? (string-ref str i) #\,))
510 (lp (1+ i) (cons (if v (cons k v) k) out))
511 (bad-header-component 'key-value-list
512 (substring str start end))))))))))
514 (define* (key-value-list? list #:optional
515 (valid? default-val-validator))
522 (and (or (string? k) (symbol? k))
524 ((or (string? elt) (symbol? elt))
528 (define* (write-key-value-list list port #:optional
529 (val-writer default-val-writer) (delim ", "))
533 (let ((k (if (pair? x) (car x) x))
534 (v (if (pair? x) (cdr x) #f)))
539 (val-writer k v port)))))
542 ;; param-component = token [ "=" (token | quoted-string) ] \
543 ;; *(";" token [ "=" (token | quoted-string) ])
545 (define* (parse-param-component str #:optional
546 (val-parser default-val-parser)
547 (start 0) (end (string-length str)))
548 (let lp ((i start) (out '()))
550 (values (reverse! out) end)
551 (let ((delim (string-index str
552 (lambda (c) (memq c '(#\, #\; #\=)))
554 (let ((k (string->symbol
555 (substring str i (trim-whitespace str i (or delim end)))))
556 (delimc (and delim (string-ref str delim))))
561 (let ((i (skip-whitespace str (1+ delim) end)))
562 (if (and (< i end) (eqv? (string-ref str i) #\"))
563 (parse-qstring str i end #:incremental? #t)
570 (char-whitespace? c)))
573 (values (substring str i delim)
575 (lambda (v-str next-i)
576 (let* ((v (val-parser k v-str))
577 (x (if v (cons k v) k))
578 (i (skip-whitespace str next-i end)))
579 (case (and (< i end) (string-ref str i))
581 (values (reverse! (cons x out)) end))
583 (lp (skip-whitespace str (1+ i) end)
585 (else ; including #\,
586 (values (reverse! (cons x out)) i)))))))
588 (let ((v (val-parser k #f)))
589 (lp (skip-whitespace str (1+ delim) end)
590 (cons (if v (cons k v) k) out))))
592 (else ;; either the end of the string or a #\,
593 (let ((v (val-parser k #f)))
594 (values (reverse! (cons (if v (cons k v) k) out))
595 (or delim end))))))))))
597 (define* (parse-param-list str #:optional
598 (val-parser default-val-parser)
599 (start 0) (end (string-length str)))
600 (let lp ((i start) (out '()))
602 (lambda () (parse-param-component str val-parser i end))
605 (if (eqv? (string-ref str i) #\,)
606 (lp (skip-whitespace str (1+ i) end)
608 (bad-header-component 'param-list str))
609 (reverse! (cons item out)))))))
611 (define* (validate-param-list list #:optional
612 (valid? default-val-validator))
615 (key-value-list? list valid?))))
617 (define* (write-param-list list port #:optional
618 (val-writer default-val-writer))
622 (write-key-value-list item port val-writer ";"))
625 (define (parse-date str)
626 ;; Unfortunately, there is no way to make string->date parse out the
627 ;; "GMT" bit, so we play string games to append a format it will
628 ;; understand (the +0000 bit).
630 (if (string-suffix? " GMT" str)
631 (string-append (substring str 0 (- (string-length str) 4))
633 (bad-header-component 'date str))
634 "~a, ~d ~b ~Y ~H:~M:~S ~z"))
636 (define (write-date date port)
637 (display (date->string date "~a, ~d ~b ~Y ~H:~M:~S GMT") port))
639 (define (write-uri uri port)
640 (display (uri->string uri) port))
642 (define (parse-entity-tag val)
643 (if (string-prefix? "W/" val)
644 (cons (parse-qstring val 2) #f)
645 (cons (parse-qstring val) #t)))
647 (define (entity-tag? val)
649 (string? (car val))))
651 (define (write-entity-tag val port)
654 (write-qstring (car val) port))
656 (define* (parse-entity-tag-list val #:optional
657 (start 0) (end (string-length val)))
658 (let ((strong? (not (string-prefix? "W/" val 0 2 start end))))
659 (call-with-values (lambda ()
660 (parse-qstring val (if strong? start (+ start 2))
661 end #:incremental? #t))
664 (let ((next (skip-whitespace val next end)))
666 (if (eqv? (string-ref val next) #\,)
667 (parse-entity-tag-list
669 (skip-whitespace val (1+ next) end)
671 (bad-header-component 'entity-tag-list val))
674 (define (entity-tag-list? val)
675 (list-of? val entity-tag?))
677 (define (write-entity-tag-list val port)
678 (write-list val port write-entity-tag ", "))
684 ;;; Request-Line and Response-Line
688 (define (bad-request message . args)
689 (throw 'bad-request message args))
690 (define (bad-response message . args)
691 (throw 'bad-response message args))
693 (define *known-versions* '())
695 (define* (parse-http-version str #:optional (start 0) (end (string-length str)))
696 "Parse an HTTP version from @var{str}, returning it as a major-minor
697 pair. For example, @code{HTTP/1.1} parses as the pair of integers,
699 (or (let lp ((known *known-versions*))
701 (if (string= str (caar known) start end)
704 (let ((dot-idx (string-index str #\. start end)))
705 (if (and (string-prefix? "HTTP/" str 0 5 start end)
707 (= dot-idx (string-rindex str #\. start end)))
708 (cons (parse-non-negative-integer str (+ start 5) dot-idx)
709 (parse-non-negative-integer str (1+ dot-idx) end))
710 (bad-header-component 'http-version (substring str start end))))))
712 (define (write-http-version val port)
713 "Write the given major-minor version pair to @var{port}."
714 (display "HTTP/" port)
715 (display (car val) port)
717 (display (cdr val) port))
721 (set! *known-versions*
722 (acons v (parse-http-version v 0 (string-length v))
724 '("HTTP/1.0" "HTTP/1.1"))
727 ;; Request-URI = "*" | absoluteURI | abs_path | authority
729 ;; The `authority' form is only permissible for the CONNECT method, so
730 ;; because we don't expect people to implement CONNECT, we save
731 ;; ourselves the trouble of that case, and disallow the CONNECT method.
733 (define* (parse-http-method str #:optional (start 0) (end (string-length str)))
734 "Parse an HTTP method from @var{str}. The result is an upper-case
735 symbol, like @code{GET}."
737 ((string= str "GET" start end) 'GET)
738 ((string= str "HEAD" start end) 'HEAD)
739 ((string= str "POST" start end) 'POST)
740 ((string= str "PUT" start end) 'PUT)
741 ((string= str "DELETE" start end) 'DELETE)
742 ((string= str "OPTIONS" start end) 'OPTIONS)
743 ((string= str "TRACE" start end) 'TRACE)
744 (else (bad-request "Invalid method: ~a" (substring str start end)))))
746 (define* (parse-request-uri str #:optional (start 0) (end (string-length str)))
747 "Parse a URI from an HTTP request line. Note that URIs in requests do
748 not have to have a scheme or host name. The result is a URI object."
751 (bad-request "Missing Request-URI"))
752 ((string= str "*" start end)
754 ((eq? (string-ref str start) #\/)
755 (let* ((q (string-index str #\? start end))
756 (f (string-index str #\# start end))
757 (q (and q (or (not f) (< q f)) q)))
759 #:path (substring str start (or q f end))
760 #:query (and q (substring str (1+ q) (or f end)))
761 #:fragment (and f (substring str (1+ f) end)))))
763 (or (string->uri (substring str start end))
764 (bad-request "Invalid URI: ~a" (substring str start end))))))
766 (define (read-request-line port)
767 "Read the first line of an HTTP request from @var{port}, returning
768 three values: the method, the URI, and the version."
769 (let* ((line (read-line* port))
770 (d0 (string-index line char-whitespace?)) ; "delimiter zero"
771 (d1 (string-rindex line char-whitespace?)))
772 (if (and d0 d1 (< d0 d1))
773 (values (parse-http-method line 0 d0)
774 (parse-request-uri line (skip-whitespace line (1+ d0) d1) d1)
775 (parse-http-version line (1+ d1) (string-length line)))
776 (bad-request "Bad Request-Line: ~s" line))))
778 (define (write-uri uri port)
781 (display (uri-scheme uri) port)
783 (if (uri-userinfo uri)
785 (display (uri-userinfo uri) port)
787 (display (uri-host uri) port)
788 (let ((p (uri-port uri)))
789 (if (and p (not (eqv? p 80)))
792 (display p port))))))
793 (let* ((path (uri-path uri))
794 (len (string-length path)))
796 ((and (> len 0) (not (eqv? (string-ref path 0) #\/)))
797 (bad-request "Non-absolute URI path: ~s" path))
798 ((and (zero? len) (not (uri-host uri)))
799 (bad-request "Empty path and no host for URI: ~s" uri))
801 (display path port))))
805 (display (uri-query uri) port))))
807 (define (write-request-line method uri version port)
808 "Write the first line of an HTTP request to @var{port}."
809 (display method port)
810 (display #\space port)
812 (display #\space port)
813 (write-http-version version port)
814 (display "\r\n" port))
816 (define (read-response-line port)
817 "Read the first line of an HTTP response from @var{port}, returning
818 three values: the HTTP version, the response code, and the \"reason
820 (let* ((line (read-line* port))
821 (d0 (string-index line char-whitespace?)) ; "delimiter zero"
822 (d1 (and d0 (string-index line char-whitespace?
823 (skip-whitespace line d0)))))
825 (values (parse-http-version line 0 d0)
826 (parse-non-negative-integer line (skip-whitespace line d0 d1)
828 (string-trim-both line char-whitespace? d1))
829 (bad-response "Bad Response-Line: ~s" line))))
831 (define (write-response-line version code reason-phrase port)
832 "Write the first line of an HTTP response to @var{port}."
833 (write-http-version version port)
834 (display #\space port)
836 (display #\space port)
837 (display reason-phrase port)
838 (display "\r\n" port))
844 ;;; Helpers for declaring headers
847 ;; emacs: (put 'declare-header! 'scheme-indent-function 1)
848 ;; emacs: (put 'declare-opaque!-header 'scheme-indent-function 1)
849 (define (declare-opaque-header! name)
850 (declare-header! name
851 parse-opaque-string validate-opaque-string write-opaque-string))
853 ;; emacs: (put 'declare-date-header! 'scheme-indent-function 1)
854 (define (declare-date-header! name)
855 (declare-header! name
856 parse-date date? write-date))
858 ;; emacs: (put 'declare-string-list-header! 'scheme-indent-function 1)
859 (define (declare-string-list-header! name)
860 (declare-header! name
861 split-and-trim list-of-strings? write-list-of-strings))
863 ;; emacs: (put 'declare-symbol-list-header! 'scheme-indent-function 1)
864 (define (declare-symbol-list-header! name)
865 (declare-header! name
867 (map string->symbol (split-and-trim str)))
869 (list-of? symbol? v))
871 (write-list v port display ", "))))
873 ;; emacs: (put 'declare-header-list-header! 'scheme-indent-function 1)
874 (define (declare-header-list-header! name)
875 (declare-header! name
876 split-header-names list-of-header-names? write-header-list))
878 ;; emacs: (put 'declare-integer-header! 'scheme-indent-function 1)
879 (define (declare-integer-header! name)
880 (declare-header! name
881 parse-non-negative-integer non-negative-integer? display))
883 ;; emacs: (put 'declare-uri-header! 'scheme-indent-function 1)
884 (define (declare-uri-header! name)
885 (declare-header! name
886 (lambda (str) (or (string->uri str) (bad-header-component 'uri str)))
890 ;; emacs: (put 'declare-quality-list-header! 'scheme-indent-function 1)
891 (define (declare-quality-list-header! name)
892 (declare-header! name
893 parse-quality-list validate-quality-list write-quality-list))
895 ;; emacs: (put 'declare-param-list-header! 'scheme-indent-function 1)
896 (define* (declare-param-list-header! name #:optional
897 (val-parser default-val-parser)
898 (val-validator default-val-validator)
899 (val-writer default-val-writer))
900 (declare-header! name
901 (lambda (str) (parse-param-list str val-parser))
902 (lambda (val) (validate-param-list val val-validator))
903 (lambda (val port) (write-param-list val port val-writer))))
905 ;; emacs: (put 'declare-key-value-list-header! 'scheme-indent-function 1)
906 (define* (declare-key-value-list-header! name #:optional
907 (val-parser default-val-parser)
908 (val-validator default-val-validator)
909 (val-writer default-val-writer))
910 (declare-header! name
911 (lambda (str) (parse-key-value-list str val-parser))
912 (lambda (val) (key-value-list? val val-validator))
913 (lambda (val port) (write-key-value-list val port val-writer))))
915 ;; emacs: (put 'declare-entity-tag-list-header! 'scheme-indent-function 1)
916 (define (declare-entity-tag-list-header! name)
917 (declare-header! name
918 (lambda (str) (if (string=? str "*") '* (parse-entity-tag-list str)))
919 (lambda (val) (or (eq? val '*) (entity-tag-list? val)))
923 (write-entity-tag-list val port)))))
932 ;; Cache-Control = 1#(cache-directive)
933 ;; cache-directive = cache-request-directive | cache-response-directive
934 ;; cache-request-directive =
935 ;; "no-cache" ; Section 14.9.1
936 ;; | "no-store" ; Section 14.9.2
937 ;; | "max-age" "=" delta-seconds ; Section 14.9.3, 14.9.4
938 ;; | "max-stale" [ "=" delta-seconds ] ; Section 14.9.3
939 ;; | "min-fresh" "=" delta-seconds ; Section 14.9.3
940 ;; | "no-transform" ; Section 14.9.5
941 ;; | "only-if-cached" ; Section 14.9.4
942 ;; | cache-extension ; Section 14.9.6
943 ;; cache-response-directive =
944 ;; "public" ; Section 14.9.1
945 ;; | "private" [ "=" <"> 1#field-name <"> ] ; Section 14.9.1
946 ;; | "no-cache" [ "=" <"> 1#field-name <"> ]; Section 14.9.1
947 ;; | "no-store" ; Section 14.9.2
948 ;; | "no-transform" ; Section 14.9.5
949 ;; | "must-revalidate" ; Section 14.9.4
950 ;; | "proxy-revalidate" ; Section 14.9.4
951 ;; | "max-age" "=" delta-seconds ; Section 14.9.3
952 ;; | "s-maxage" "=" delta-seconds ; Section 14.9.3
953 ;; | cache-extension ; Section 14.9.6
954 ;; cache-extension = token [ "=" ( token | quoted-string ) ]
956 (declare-key-value-list-header! "Cache-Control"
959 ((max-age max-stale min-fresh s-maxage)
960 (parse-non-negative-integer v-str))
962 (and v-str (split-header-names v-str)))
964 default-val-validator
967 ((string? v) (display v port))
970 (write-header-list v port)
975 (bad-header-component 'cache-control v)))))
977 ;; Connection = "Connection" ":" 1#(connection-token)
978 ;; connection-token = token
980 ;; Connection: close, foo-header
982 (declare-header-list-header! "Connection")
984 ;; Date = "Date" ":" HTTP-date
986 ;; Date: Tue, 15 Nov 1994 08:12:31 GMT
988 (declare-date-header! "Date")
990 ;; Pragma = "Pragma" ":" 1#pragma-directive
991 ;; pragma-directive = "no-cache" | extension-pragma
992 ;; extension-pragma = token [ "=" ( token | quoted-string ) ]
994 (declare-key-value-list-header! "Pragma")
996 ;; Trailer = "Trailer" ":" 1#field-name
998 (declare-header-list-header! "Trailer")
1000 ;; Transfer-Encoding = "Transfer-Encoding" ":" 1#transfer-coding
1002 (declare-param-list-header! "Transfer-Encoding")
1004 ;; Upgrade = "Upgrade" ":" 1#product
1006 (declare-string-list-header! "Upgrade")
1008 ;; Via = "Via" ":" 1#( received-protocol received-by [ comment ] )
1009 ;; received-protocol = [ protocol-name "/" ] protocol-version
1010 ;; protocol-name = token
1011 ;; protocol-version = token
1012 ;; received-by = ( host [ ":" port ] ) | pseudonym
1013 ;; pseudonym = token
1015 (declare-header! "Via"
1018 write-list-of-strings
1021 ;; Warning = "Warning" ":" 1#warning-value
1023 ;; warning-value = warn-code SP warn-agent SP warn-text
1026 ;; warn-code = 3DIGIT
1027 ;; warn-agent = ( host [ ":" port ] ) | pseudonym
1028 ;; ; the name or pseudonym of the server adding
1029 ;; ; the Warning header, for use in debugging
1030 ;; warn-text = quoted-string
1031 ;; warn-date = <"> HTTP-date <">
1032 (declare-header! "Warning"
1034 (let ((len (string-length str)))
1035 (let lp ((i (skip-whitespace str 0)))
1036 (let* ((idx1 (string-index str #\space i))
1037 (idx2 (string-index str #\space (1+ idx1))))
1039 (let ((code (parse-non-negative-integer str i idx1))
1040 (agent (substring str (1+ idx1) idx2)))
1042 (lambda () (parse-qstring str (1+ idx2) #:incremental? #t))
1046 (let ((c (and (< i len) (string-ref str i))))
1051 (lambda () (parse-qstring str (1+ i)
1054 (values text (parse-date date) i))))
1056 (values text #f i)))))
1057 (lambda (text date i)
1058 (let ((w (list code agent text date))
1059 (c (and (< i len) (string-ref str i))))
1062 ((#\,) (cons w (lp (skip-whitespace str (1+ i)))))
1063 (else (bad-header 'warning str))))))))))))))
1069 (apply (lambda (code host text date)
1070 (and (non-negative-integer? code) (< code 1000)
1073 (or (not date) (date? date))))
1080 (lambda (code host text date)
1082 (display #\space port)
1084 (display #\space port)
1085 (write-qstring text port)
1088 (display #\space port)
1089 (write-date date port))))
1103 (declare-symbol-list-header! "Allow")
1105 ;; Content-Encoding = 1#content-coding
1107 (declare-symbol-list-header! "Content-Encoding")
1109 ;; Content-Language = 1#language-tag
1111 (declare-string-list-header! "Content-Language")
1113 ;; Content-Length = 1*DIGIT
1115 (declare-integer-header! "Content-Length")
1117 ;; Content-Location = ( absoluteURI | relativeURI )
1119 (declare-uri-header! "Content-Location")
1121 ;; Content-MD5 = <base64 of 128 bit MD5 digest as per RFC 1864>
1123 (declare-opaque-header! "Content-MD5")
1125 ;; Content-Range = content-range-spec
1126 ;; content-range-spec = byte-content-range-spec
1127 ;; byte-content-range-spec = bytes-unit SP
1128 ;; byte-range-resp-spec "/"
1129 ;; ( instance-length | "*" )
1130 ;; byte-range-resp-spec = (first-byte-pos "-" last-byte-pos)
1132 ;; instance-length = 1*DIGIT
1134 (declare-header! "Content-Range"
1136 (let ((dash (string-index str #\-))
1137 (slash (string-index str #\/)))
1138 (if (and (string-prefix? "bytes " str) slash)
1143 (parse-non-negative-integer str 6 dash)
1144 (parse-non-negative-integer str (1+ dash) slash)))
1145 ((string= str "*" 6 slash)
1148 (bad-header 'content-range str)))
1149 (if (string= str "*" (1+ slash))
1151 (parse-non-negative-integer str (1+ slash))))
1152 (bad-header 'content-range str))))
1154 (and (list? val) (= (length val) 3)
1156 (let ((x (cadr val)))
1159 (non-negative-integer? (car x))
1160 (non-negative-integer? (cdr x)))))
1161 (let ((x (caddr val)))
1163 (non-negative-integer? x)))))
1165 (display (car val) port)
1166 (display #\space port)
1167 (if (eq? (cadr val) '*)
1170 (display (caadr val) port)
1172 (display (caadr val) port)))
1173 (if (eq? (caddr val) '*)
1175 (display (caddr val) port))))
1177 ;; Content-Type = media-type
1179 (declare-header! "Content-Type"
1181 (let ((parts (string-split str #\;)))
1182 (cons (parse-media-type (car parts))
1184 (let ((eq (string-index x #\=)))
1185 (if (and eq (= eq (string-rindex x #\=)))
1186 (cons (string->symbol
1187 (string-trim x char-whitespace? 0 eq))
1188 (string-trim-right x char-whitespace? (1+ eq)))
1189 (bad-header 'content-type str))))
1196 (and (pair? x) (symbol? (car x)) (string? (cdr x)))))))
1198 (display (car val) port)
1199 (if (pair? (cdr val))
1205 (display (car pair) port)
1207 (display (cdr pair) port))
1210 ;; Expires = HTTP-date
1212 (declare-date-header! "Expires")
1214 ;; Last-Modified = HTTP-date
1216 (declare-date-header! "Last-Modified")
1225 ;; Accept = #( media-range [ accept-params ] )
1226 ;; media-range = ( "*/*" | ( type "/" "*" ) | ( type "/" subtype ) )
1227 ;; *( ";" parameter )
1228 ;; accept-params = ";" "q" "=" qvalue *( accept-extension )
1229 ;; accept-extension = ";" token [ "=" ( token | quoted-string ) ]
1231 (declare-param-list-header! "Accept"
1232 ;; -> (type/subtype (sym-prop . str-val) ...) ...)
1234 ;; with the exception of prop `q', in which case the val will be a
1235 ;; valid quality value
1247 (write-quality v port)
1248 (default-val-writer k v port))))
1250 ;; Accept-Charset = 1#( ( charset | "*" )[ ";" "q" "=" qvalue ] )
1252 (declare-quality-list-header! "Accept-Charset")
1254 ;; Accept-Encoding = 1#( codings [ ";" "q" "=" qvalue ] )
1255 ;; codings = ( content-coding | "*" )
1257 (declare-quality-list-header! "Accept-Encoding")
1259 ;; Accept-Language = 1#( language-range [ ";" "q" "=" qvalue ] )
1260 ;; language-range = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )
1262 (declare-quality-list-header! "Accept-Language")
1264 ;; Authorization = credentials
1266 ;; Authorization is basically opaque to this HTTP stack, we just pass
1267 ;; the string value through.
1269 (declare-opaque-header! "Authorization")
1271 ;; Expect = 1#expectation
1272 ;; expectation = "100-continue" | expectation-extension
1273 ;; expectation-extension = token [ "=" ( token | quoted-string )
1275 ;; expect-params = ";" token [ "=" ( token | quoted-string ) ]
1277 (declare-param-list-header! "Expect")
1281 ;; Should be an email address; we just pass on the string as-is.
1283 (declare-opaque-header! "From")
1285 ;; Host = host [ ":" port ]
1287 (declare-header! "Host"
1289 (let ((colon (string-index str #\:)))
1291 (cons (substring str 0 colon)
1292 (parse-non-negative-integer str (1+ colon)))
1298 (non-negative-integer? (cdr val)))))
1300 (display (car val) port)
1304 (display (cdr val) port)))))
1306 ;; If-Match = ( "*" | 1#entity-tag )
1308 (declare-entity-tag-list-header! "If-Match")
1310 ;; If-Modified-Since = HTTP-date
1312 (declare-date-header! "If-Modified-Since")
1314 ;; If-None-Match = ( "*" | 1#entity-tag )
1316 (declare-entity-tag-list-header! "If-None-Match")
1318 ;; If-Range = ( entity-tag | HTTP-date )
1320 (declare-header! "If-Range"
1322 (if (or (string-prefix? "\"" str)
1323 (string-prefix? "W/" str))
1324 (parse-entity-tag str)
1327 (or (date? val) (entity-tag? val)))
1330 (write-date val port)
1331 (write-entity-tag val port))))
1333 ;; If-Unmodified-Since = HTTP-date
1335 (declare-date-header! "If-Unmodified-Since")
1337 ;; Max-Forwards = 1*DIGIT
1339 (declare-integer-header! "Max-Forwards")
1341 ;; Proxy-Authorization = credentials
1343 (declare-opaque-header! "Proxy-Authorization")
1345 ;; Range = "Range" ":" ranges-specifier
1346 ;; ranges-specifier = byte-ranges-specifier
1347 ;; byte-ranges-specifier = bytes-unit "=" byte-range-set
1348 ;; byte-range-set = 1#( byte-range-spec | suffix-byte-range-spec )
1349 ;; byte-range-spec = first-byte-pos "-" [last-byte-pos]
1350 ;; first-byte-pos = 1*DIGIT
1351 ;; last-byte-pos = 1*DIGIT
1352 ;; suffix-byte-range-spec = "-" suffix-length
1353 ;; suffix-length = 1*DIGIT
1355 (declare-header! "Range"
1357 (if (string-prefix? "bytes=" str)
1361 (let ((dash (string-index x #\-)))
1364 (bad-header 'range str))
1366 (cons #f (parse-non-negative-integer x 1)))
1367 ((= dash (1- (string-length x)))
1368 (cons (parse-non-negative-integer x 0 dash) #f))
1370 (cons (parse-non-negative-integer x 0 dash)
1371 (parse-non-negative-integer x (1+ dash)))))))
1372 (string-split (substring str 6) #\,)))
1373 (bad-header 'range str)))
1380 (let ((x (car elt)) (y (cdr elt)))
1382 (or (not x) (non-negative-integer? x))
1383 (or (not y) (non-negative-integer? y)))))))))
1385 (display (car val) port)
1391 (display (car pair) port))
1394 (display (cdr pair) port)))
1397 ;; Referer = ( absoluteURI | relativeURI )
1399 (declare-uri-header! "Referer")
1401 ;; TE = #( t-codings )
1402 ;; t-codings = "trailers" | ( transfer-extension [ accept-params ] )
1404 (declare-param-list-header! "TE")
1406 ;; User-Agent = 1*( product | comment )
1408 (declare-opaque-header! "User-Agent")
1417 ;; Accept-Ranges = acceptable-ranges
1418 ;; acceptable-ranges = 1#range-unit | "none"
1420 (declare-symbol-list-header! "Accept-Ranges")
1423 ;; age-value = delta-seconds
1425 (declare-integer-header! "Age")
1427 ;; ETag = entity-tag
1429 (declare-header! "ETag"
1434 ;; Location = absoluteURI
1436 (declare-uri-header! "Location")
1438 ;; Proxy-Authenticate = 1#challenge
1440 ;; FIXME: split challenges ?
1441 (declare-opaque-header! "Proxy-Authenticate")
1443 ;; Retry-After = ( HTTP-date | delta-seconds )
1445 (declare-header! "Retry-After"
1447 (if (and (not (string-null? str))
1448 (char-numeric? (string-ref str 0)))
1449 (parse-non-negative-integer str)
1452 (or (date? val) (non-negative-integer? val)))
1455 (write-date val port)
1456 (display val port))))
1458 ;; Server = 1*( product | comment )
1460 (declare-opaque-header! "Server")
1462 ;; Vary = ( "*" | 1#field-name )
1464 (declare-header! "Vary"
1466 (if (equal? str "*")
1468 (split-header-names str)))
1470 (or (eq? val '*) (list-of-header-names? val)))
1474 (write-header-list val port))))
1476 ;; WWW-Authenticate = 1#challenge
1479 (declare-opaque-header! "WWW-Authenticate")