more symbols in (web http)
[bpt/guile.git] / module / web / http.scm
1 ;;; HTTP messages
2
3 ;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
4
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.
9 ;;
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.
14 ;;
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
18 ;; 02110-1301 USA
19
20 ;;; Commentary:
21 ;;;
22 ;;; This module has a number of routines to parse textual
23 ;;; representations of HTTP data into native Scheme data structures.
24 ;;;
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
28 ;;; be 0.2, etc).
29 ;;;
30 ;;; Code:
31
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
40 header->string
41
42 declare-header!
43 known-header?
44 header-parser
45 header-validator
46 header-writer
47
48 read-header
49 parse-header
50 valid-header?
51 write-header
52
53 read-headers
54 write-headers
55
56 parse-http-method
57 parse-http-version
58 parse-request-uri
59
60 read-request-line
61 write-request-line
62 read-response-line
63 write-response-line))
64
65
66 ;;; TODO
67 ;;;
68 ;;; Look at quality lists with more insight.
69 ;;; Think about `accept' a bit more.
70 ;;;
71
72
73 (define (string->header name)
74 "Parse @var{name} to a symbolic header name."
75 (string->symbol (string-downcase name)))
76
77 (define-record-type <header-decl>
78 (make-header-decl name parser validator writer multiple?)
79 header-decl?
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?))
85
86 ;; sym -> header
87 (define *declared-headers* (make-hash-table))
88
89 (define (lookup-header-decl sym)
90 (hashq-ref *declared-headers* sym))
91
92 (define* (declare-header! name
93 parser
94 validator
95 writer
96 #:key multiple?)
97 "Define a parser, validator, and writer for the HTTP header, @var{name}.
98
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)
106 decl)
107 (error "bad header decl" name parser validator writer multiple?)))
108
109 (define (header->string sym)
110 "Return the string form for the header named @var{sym}."
111 (let ((decl (lookup-header-decl sym)))
112 (if decl
113 (header-decl-name decl)
114 (string-titlecase (symbol->string sym)))))
115
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))
120
121 (define (header-parser sym)
122 "Returns a procedure to parse values for the given header."
123 (let ((decl (lookup-header-decl sym)))
124 (if decl
125 (header-decl-parser decl)
126 (lambda (x) x))))
127
128 (define (header-validator sym)
129 "Returns a procedure to validate values for the given header."
130 (let ((decl (lookup-header-decl sym)))
131 (if decl
132 (header-decl-validator decl)
133 string?)))
134
135 (define (header-writer sym)
136 "Returns a procedure to write values for the given header to a given
137 port."
138 (let ((decl (lookup-header-decl sym)))
139 (if decl
140 (header-decl-writer decl)
141 display)))
142
143 (define (read-line* port)
144 (let* ((pair (%read-line port))
145 (line (car pair))
146 (delim (cdr pair)))
147 (if (and (string? line) (char? delim))
148 (let ((orig-len (string-length line)))
149 (let lp ((len orig-len))
150 (if (and (> len 0)
151 (char-whitespace? (string-ref line (1- len))))
152 (lp (1- len))
153 (if (= len orig-len)
154 line
155 (substring line 0 len)))))
156 (bad-header '%read line))))
157
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
162 (string-append val
163 (begin
164 (read-line* port))))
165 val))
166
167 (define *eof* (call-with-input-string "" read))
168
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.
173
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"))
179 (values *eof* *eof*)
180 (let* ((delim (or (string-index line #\:)
181 (bad-header '%read line)))
182 (sym (string->header (substring line 0 delim))))
183 (values
184 sym
185 (parse-header
186 sym
187 (read-continuation-line
188 port
189 (string-trim-both line char-whitespace? (1+ delim)))))))))
190
191 (define (parse-header sym val)
192 "Parse @var{val}, a string, with the parser registered for the header
193 named @var{sym}.
194
195 Returns the parsed value. If a parser was not found, the value is
196 returned as a string."
197 ((header-parser sym) val))
198
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}."
202 (if (symbol? sym)
203 ((header-validator sym) val)
204 (error "header name not a symbol" sym)))
205
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)
211 (display ": " port)
212 ((header-writer sym) val port)
213 (display "\r\n" port))
214
215 (define (read-headers port)
216 "Read an HTTP message from @var{port}, returning the headers as an
217 ordered alist."
218 (let lp ((headers '()))
219 (call-with-values (lambda () (read-header port))
220 (lambda (k v)
221 (if (eof-object? k)
222 (reverse! headers)
223 (lp (acons k v headers)))))))
224
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))
229 (if (pair? headers)
230 (begin
231 (write-header (caar headers) (cdar headers) port)
232 (lp (cdr headers))))))
233
234
235 \f
236
237 ;;;
238 ;;; Utilities
239 ;;;
240
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))
245
246 (define (parse-opaque-string str)
247 str)
248 (define (validate-opaque-string val)
249 (string? val))
250 (define (write-opaque-string val port)
251 (display val port))
252
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)
261 (string->symbol str)
262 (bad-header-component 'media-type str)))
263
264 (define* (skip-whitespace str #:optional (start 0) (end (string-length str)))
265 (let lp ((i start))
266 (if (and (< i end) (char-whitespace? (string-ref str i)))
267 (lp (1+ i))
268 i)))
269
270 (define* (trim-whitespace str #:optional (start 0) (end (string-length str)))
271 (let lp ((i end))
272 (if (and (< start i) (char-whitespace? (string-ref str (1- i))))
273 (lp (1- i))
274 i)))
275
276 (define* (split-and-trim str #:optional (delim #\,)
277 (start 0) (end (string-length str)))
278 (let lp ((i start))
279 (if (< i end)
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)))
283 '())))
284
285 (define (list-of-strings? val)
286 (list-of? val string?))
287
288 (define (write-list-of-strings val port)
289 (write-list val port display ", "))
290
291 (define (split-header-names str)
292 (map string->header (split-and-trim str)))
293
294 (define (list-of-header-names? val)
295 (list-of? val symbol?))
296
297 (define (write-header-list val port)
298 (write-list val port
299 (lambda (x port)
300 (display (header->string x) port))
301 ", "))
302
303 (define (collect-escaped-string from start len escapes)
304 (let ((to (make-string len)))
305 (let lp ((start start) (i 0) (escapes escapes))
306 (if (null? escapes)
307 (begin
308 (substring-move! from start (+ start (- len i)) to i)
309 to)
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)))))))
315
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))
320 #:key incremental?)
321 (if (and (< start end) (eqv? (string-ref str start) #\"))
322 (let lp ((i (1+ start)) (qi 0) (escapes '()))
323 (if (< i end)
324 (case (string-ref str i)
325 ((#\\)
326 (lp (+ i 2) (1+ qi) (cons qi escapes)))
327 ((#\")
328 (let ((out (collect-escaped-string str (1+ start) qi escapes)))
329 (if incremental?
330 (values out (1+ i))
331 (if (= (1+ i) end)
332 out
333 (bad-header-component 'qstring str)))))
334 (else
335 (lp (1+ i) (1+ qi) escapes)))
336 (bad-header-component 'qstring str)))
337 (bad-header-component 'qstring str)))
338
339 (define (write-list l port write-item delim)
340 (if (pair? l)
341 (let lp ((l l))
342 (write-item (car l) port)
343 (if (pair? (cdr l))
344 (begin
345 (display delim port)
346 (lp (cdr l)))))))
347
348 (define (write-qstring str port)
349 (display #\" port)
350 (if (string-index str #\")
351 ;; optimize me
352 (write-list (string-split str #\") port display "\\\"")
353 (display str port))
354 (display #\" port))
355
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))
360 i
361 (bad-header-component 'quality str))))
362 (cond
363 ((not (< start end))
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))
371 1000
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))
376 0
377 (if (< 2 (- end start) 6)
378 (let lp ((place 1) (i (+ start 4)) (q 0))
379 (if (= i (1+ start))
380 (if (eqv? (string-ref str (1+ start)) #\.)
381 q
382 (bad-header-component 'quality str))
383 (lp (* 10 place) (1- i)
384 (if (< i end)
385 (+ q (* place (char->decimal (string-ref str i))))
386 q))))
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))
392 (if (= i start)
393 q
394 (lp (* 10 place) (1- i)
395 (if (< i end)
396 (+ q (* place (char->decimal (string-ref str i))))
397 q)))))
398 (else
399 (bad-header-component 'quality str))))
400
401 (define (valid-quality? q)
402 (and (non-negative-integer? q) (<= q 1000)))
403
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)
408 (display #\. 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))
412
413 (define (list-of? val pred)
414 (or (null? val)
415 (and (pair? val)
416 (pred (car val))
417 (list-of? (cdr val) pred))))
418
419 (define* (parse-quality-list str)
420 (map (lambda (part)
421 (cond
422 ((string-rindex part #\;)
423 => (lambda (idx)
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)))))
429 (else
430 (cons 1000 (string-trim-both part char-whitespace?)))))
431 (string-split str #\,)))
432
433 (define (validate-quality-list l)
434 (list-of? l
435 (lambda (elt)
436 (and (pair? elt)
437 (valid-quality? (car elt))
438 (string? (cdr elt))))))
439
440 (define (write-quality-list l port)
441 (write-list l port
442 (lambda (x port)
443 (let ((q (car x))
444 (str (cdr x)))
445 (display str port)
446 (if (< q 1000)
447 (begin
448 (display ";q=" port)
449 (write-quality q port)))))
450 ","))
451
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))
457 i
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))
462 (if (< i end)
463 (lp (1+ i)
464 (+ (* out 10) (char->decimal (string-ref val i))))
465 out))))
466
467 (define (non-negative-integer? code)
468 (and (number? code) (>= code 0) (exact? code) (integer? code)))
469
470 (define (default-val-parser k val)
471 val)
472
473 (define (default-val-validator k val)
474 (string? val))
475
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)
481 (display val port)))
482
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 '()))
487 (if (not (< i end))
488 (reverse! 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)))
493 (k (string->symbol
494 (substring str i (trim-whitespace str i delim)))))
495 (call-with-values
496 (lambda ()
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
503 (or comma end)))
504 (or comma end))))
505 (values #f delim)))
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))))))))))
513
514 (define* (key-value-list? list #:optional
515 (valid? default-val-validator))
516 (list-of? list
517 (lambda (elt)
518 (cond
519 ((pair? elt)
520 (let ((k (car elt))
521 (v (cdr elt)))
522 (and (or (string? k) (symbol? k))
523 (valid? k v))))
524 ((or (string? elt) (symbol? elt))
525 (valid? elt #f))
526 (else #f)))))
527
528 (define* (write-key-value-list list port #:optional
529 (val-writer default-val-writer) (delim ", "))
530 (write-list
531 list port
532 (lambda (x port)
533 (let ((k (if (pair? x) (car x) x))
534 (v (if (pair? x) (cdr x) #f)))
535 (display k port)
536 (if v
537 (begin
538 (display #\= port)
539 (val-writer k v port)))))
540 delim))
541
542 ;; param-component = token [ "=" (token | quoted-string) ] \
543 ;; *(";" token [ "=" (token | quoted-string) ])
544 ;;
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 '()))
549 (if (not (< i end))
550 (values (reverse! out) end)
551 (let ((delim (string-index str
552 (lambda (c) (memq c '(#\, #\; #\=)))
553 i)))
554 (let ((k (string->symbol
555 (substring str i (trim-whitespace str i (or delim end)))))
556 (delimc (and delim (string-ref str delim))))
557 (case delimc
558 ((#\=)
559 (call-with-values
560 (lambda ()
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)
564 (let ((delim
565 (or (string-index
566 str
567 (lambda (c)
568 (or (eqv? c #\;)
569 (eqv? c #\,)
570 (char-whitespace? c)))
571 i end)
572 end)))
573 (values (substring str i delim)
574 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))
580 ((#f)
581 (values (reverse! (cons x out)) end))
582 ((#\;)
583 (lp (skip-whitespace str (1+ i) end)
584 (cons x out)))
585 (else ; including #\,
586 (values (reverse! (cons x out)) i)))))))
587 ((#\;)
588 (let ((v (val-parser k #f)))
589 (lp (skip-whitespace str (1+ delim) end)
590 (cons (if v (cons k v) k) out))))
591
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))))))))))
596
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 '()))
601 (call-with-values
602 (lambda () (parse-param-component str val-parser i end))
603 (lambda (item i)
604 (if (< i end)
605 (if (eqv? (string-ref str i) #\,)
606 (lp (skip-whitespace str (1+ i) end)
607 (cons item out))
608 (bad-header-component 'param-list str))
609 (reverse! (cons item out)))))))
610
611 (define* (validate-param-list list #:optional
612 (valid? default-val-validator))
613 (list-of? list
614 (lambda (elt)
615 (key-value-list? list valid?))))
616
617 (define* (write-param-list list port #:optional
618 (val-writer default-val-writer))
619 (write-list
620 list port
621 (lambda (item port)
622 (write-key-value-list item port val-writer ";"))
623 ","))
624
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).
629 (string->date
630 (if (string-suffix? " GMT" str)
631 (string-append (substring str 0 (- (string-length str) 4))
632 " +0000")
633 (bad-header-component 'date str))
634 "~a, ~d ~b ~Y ~H:~M:~S ~z"))
635
636 (define (write-date date port)
637 (display (date->string date "~a, ~d ~b ~Y ~H:~M:~S GMT") port))
638
639 (define (write-uri uri port)
640 (display (uri->string uri) port))
641
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)))
646
647 (define (entity-tag? val)
648 (and (pair? val)
649 (string? (car val))))
650
651 (define (write-entity-tag val port)
652 (if (not (cdr val))
653 (display "W/" port))
654 (write-qstring (car val) port))
655
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))
662 (lambda (tag next)
663 (acons tag strong?
664 (let ((next (skip-whitespace val next end)))
665 (if (< next end)
666 (if (eqv? (string-ref val next) #\,)
667 (parse-entity-tag-list
668 val
669 (skip-whitespace val (1+ next) end)
670 end)
671 (bad-header-component 'entity-tag-list val))
672 '())))))))
673
674 (define (entity-tag-list? val)
675 (list-of? val entity-tag?))
676
677 (define (write-entity-tag-list val port)
678 (write-list val port write-entity-tag ", "))
679
680
681 \f
682
683 ;;;
684 ;;; Request-Line and Response-Line
685 ;;;
686
687 ;; Hmm.
688 (define (bad-request message . args)
689 (throw 'bad-request message args))
690 (define (bad-response message . args)
691 (throw 'bad-response message args))
692
693 (define *known-versions* '())
694
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,
698 @code{(1 . 1)}."
699 (or (let lp ((known *known-versions*))
700 (and (pair? known)
701 (if (string= str (caar known) start end)
702 (cdar known)
703 (lp (cdr known)))))
704 (let ((dot-idx (string-index str #\. start end)))
705 (if (and (string-prefix? "HTTP/" str 0 5 start end)
706 dot-idx
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))))))
711
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)
716 (display #\. port)
717 (display (cdr val) port))
718
719 (for-each
720 (lambda (v)
721 (set! *known-versions*
722 (acons v (parse-http-version v 0 (string-length v))
723 *known-versions*)))
724 '("HTTP/1.0" "HTTP/1.1"))
725
726
727 ;; Request-URI = "*" | absoluteURI | abs_path | authority
728 ;;
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.
732 ;;
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}."
736 (cond
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)))))
745
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."
749 (cond
750 ((= start end)
751 (bad-request "Missing Request-URI"))
752 ((string= str "*" start end)
753 #f)
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)))
758 (build-uri 'http
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)))))
762 (else
763 (or (string->uri (substring str start end))
764 (bad-request "Invalid URI: ~a" (substring str start end))))))
765
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))))
777
778 (define (write-uri uri port)
779 (if (uri-host uri)
780 (begin
781 (display (uri-scheme uri) port)
782 (display "://" port)
783 (if (uri-userinfo uri)
784 (begin
785 (display (uri-userinfo uri) port)
786 (display #\@ port)))
787 (display (uri-host uri) port)
788 (let ((p (uri-port uri)))
789 (if (and p (not (eqv? p 80)))
790 (begin
791 (display #\: port)
792 (display p port))))))
793 (let* ((path (uri-path uri))
794 (len (string-length path)))
795 (cond
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))
800 (else
801 (display path port))))
802 (if (uri-query uri)
803 (begin
804 (display #\? port)
805 (display (uri-query uri) port))))
806
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)
811 (write-uri uri port)
812 (display #\space port)
813 (write-http-version version port)
814 (display "\r\n" port))
815
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
819 phrase\"."
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)))))
824 (if (and d0 d1)
825 (values (parse-http-version line 0 d0)
826 (parse-non-negative-integer line (skip-whitespace line d0 d1)
827 d1)
828 (string-trim-both line char-whitespace? d1))
829 (bad-response "Bad Response-Line: ~s" line))))
830
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)
835 (display code port)
836 (display #\space port)
837 (display reason-phrase port)
838 (display "\r\n" port))
839
840
841 \f
842
843 ;;;
844 ;;; Helpers for declaring headers
845 ;;;
846
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))
852
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))
857
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))
862
863 ;; emacs: (put 'declare-symbol-list-header! 'scheme-indent-function 1)
864 (define (declare-symbol-list-header! name)
865 (declare-header! name
866 (lambda (str)
867 (map string->symbol (split-and-trim str)))
868 (lambda (v)
869 (list-of? symbol? v))
870 (lambda (v port)
871 (write-list v port display ", "))))
872
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))
877
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))
882
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)))
887 uri?
888 write-uri))
889
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))
894
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))))
904
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))))
914
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)))
920 (lambda (val port)
921 (if (eq? val '*)
922 (display "*" port)
923 (write-entity-tag-list val port)))))
924
925
926 \f
927
928 ;;;
929 ;;; General headers
930 ;;;
931
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 ) ]
955 ;;
956 (declare-key-value-list-header! "Cache-Control"
957 (lambda (k v-str)
958 (case k
959 ((max-age max-stale min-fresh s-maxage)
960 (parse-non-negative-integer v-str))
961 ((private no-cache)
962 (and v-str (split-header-names v-str)))
963 (else v-str)))
964 default-val-validator
965 (lambda (k v port)
966 (cond
967 ((string? v) (display v port))
968 ((pair? v)
969 (display #\" port)
970 (write-header-list v port)
971 (display #\" port))
972 ((integer? v)
973 (display v port))
974 (else
975 (bad-header-component 'cache-control v)))))
976
977 ;; Connection = "Connection" ":" 1#(connection-token)
978 ;; connection-token = token
979 ;; e.g.
980 ;; Connection: close, foo-header
981 ;;
982 (declare-header-list-header! "Connection")
983
984 ;; Date = "Date" ":" HTTP-date
985 ;; e.g.
986 ;; Date: Tue, 15 Nov 1994 08:12:31 GMT
987 ;;
988 (declare-date-header! "Date")
989
990 ;; Pragma = "Pragma" ":" 1#pragma-directive
991 ;; pragma-directive = "no-cache" | extension-pragma
992 ;; extension-pragma = token [ "=" ( token | quoted-string ) ]
993 ;;
994 (declare-key-value-list-header! "Pragma")
995
996 ;; Trailer = "Trailer" ":" 1#field-name
997 ;;
998 (declare-header-list-header! "Trailer")
999
1000 ;; Transfer-Encoding = "Transfer-Encoding" ":" 1#transfer-coding
1001 ;;
1002 (declare-param-list-header! "Transfer-Encoding")
1003
1004 ;; Upgrade = "Upgrade" ":" 1#product
1005 ;;
1006 (declare-string-list-header! "Upgrade")
1007
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
1014 ;;
1015 (declare-header! "Via"
1016 split-and-trim
1017 list-of-strings?
1018 write-list-of-strings
1019 #:multiple? #t)
1020
1021 ;; Warning = "Warning" ":" 1#warning-value
1022 ;;
1023 ;; warning-value = warn-code SP warn-agent SP warn-text
1024 ;; [SP warn-date]
1025 ;;
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"
1033 (lambda (str)
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))))
1038 (if (and idx1 idx2)
1039 (let ((code (parse-non-negative-integer str i idx1))
1040 (agent (substring str (1+ idx1) idx2)))
1041 (call-with-values
1042 (lambda () (parse-qstring str (1+ idx2) #:incremental? #t))
1043 (lambda (text i)
1044 (call-with-values
1045 (lambda ()
1046 (let ((c (and (< i len) (string-ref str i))))
1047 (case c
1048 ((#\space)
1049 ;; we have a date.
1050 (call-with-values
1051 (lambda () (parse-qstring str (1+ i)
1052 #:incremental? #t))
1053 (lambda (date i)
1054 (values text (parse-date date) i))))
1055 (else
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))))
1060 (case c
1061 ((#f) (list w))
1062 ((#\,) (cons w (lp (skip-whitespace str (1+ i)))))
1063 (else (bad-header 'warning str))))))))))))))
1064 (lambda (val)
1065 (list-of? val
1066 (lambda (elt)
1067 (and (list? elt)
1068 (= (length elt) 4)
1069 (apply (lambda (code host text date)
1070 (and (non-negative-integer? code) (< code 1000)
1071 (string? host)
1072 (string? text)
1073 (or (not date) (date? date))))
1074 elt)))))
1075 (lambda (val port)
1076 (write-list
1077 val port
1078 (lambda (w port)
1079 (apply
1080 (lambda (code host text date)
1081 (display code port)
1082 (display #\space port)
1083 (display host port)
1084 (display #\space port)
1085 (write-qstring text port)
1086 (if date
1087 (begin
1088 (display #\space port)
1089 (write-date date port))))
1090 w))
1091 ", "))
1092 #:multiple? #t)
1093
1094
1095 \f
1096
1097 ;;;
1098 ;;; Entity headers
1099 ;;;
1100
1101 ;; Allow = #Method
1102 ;;
1103 (declare-symbol-list-header! "Allow")
1104
1105 ;; Content-Encoding = 1#content-coding
1106 ;;
1107 (declare-symbol-list-header! "Content-Encoding")
1108
1109 ;; Content-Language = 1#language-tag
1110 ;;
1111 (declare-string-list-header! "Content-Language")
1112
1113 ;; Content-Length = 1*DIGIT
1114 ;;
1115 (declare-integer-header! "Content-Length")
1116
1117 ;; Content-Location = ( absoluteURI | relativeURI )
1118 ;;
1119 (declare-uri-header! "Content-Location")
1120
1121 ;; Content-MD5 = <base64 of 128 bit MD5 digest as per RFC 1864>
1122 ;;
1123 (declare-opaque-header! "Content-MD5")
1124
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)
1131 ;; | "*"
1132 ;; instance-length = 1*DIGIT
1133 ;;
1134 (declare-header! "Content-Range"
1135 (lambda (str)
1136 (let ((dash (string-index str #\-))
1137 (slash (string-index str #\/)))
1138 (if (and (string-prefix? "bytes " str) slash)
1139 (list 'bytes
1140 (cond
1141 (dash
1142 (cons
1143 (parse-non-negative-integer str 6 dash)
1144 (parse-non-negative-integer str (1+ dash) slash)))
1145 ((string= str "*" 6 slash)
1146 '*)
1147 (else
1148 (bad-header 'content-range str)))
1149 (if (string= str "*" (1+ slash))
1150 '*
1151 (parse-non-negative-integer str (1+ slash))))
1152 (bad-header 'content-range str))))
1153 (lambda (val)
1154 (and (list? val) (= (length val) 3)
1155 (symbol? (car val))
1156 (let ((x (cadr val)))
1157 (or (eq? x '*)
1158 (and (pair? x)
1159 (non-negative-integer? (car x))
1160 (non-negative-integer? (cdr x)))))
1161 (let ((x (caddr val)))
1162 (or (eq? x '*)
1163 (non-negative-integer? x)))))
1164 (lambda (val port)
1165 (display (car val) port)
1166 (display #\space port)
1167 (if (eq? (cadr val) '*)
1168 (display #\* port)
1169 (begin
1170 (display (caadr val) port)
1171 (display #\- port)
1172 (display (caadr val) port)))
1173 (if (eq? (caddr val) '*)
1174 (display #\* port)
1175 (display (caddr val) port))))
1176
1177 ;; Content-Type = media-type
1178 ;;
1179 (declare-header! "Content-Type"
1180 (lambda (str)
1181 (let ((parts (string-split str #\;)))
1182 (cons (parse-media-type (car parts))
1183 (map (lambda (x)
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))))
1190 (cdr parts)))))
1191 (lambda (val)
1192 (and (pair? val)
1193 (symbol? (car val))
1194 (list-of? (cdr val)
1195 (lambda (x)
1196 (and (pair? x) (symbol? (car x)) (string? (cdr x)))))))
1197 (lambda (val port)
1198 (display (car val) port)
1199 (if (pair? (cdr val))
1200 (begin
1201 (display ";" port)
1202 (write-list
1203 (cdr val) port
1204 (lambda (pair port)
1205 (display (car pair) port)
1206 (display #\= port)
1207 (display (cdr pair) port))
1208 ";")))))
1209
1210 ;; Expires = HTTP-date
1211 ;;
1212 (declare-date-header! "Expires")
1213
1214 ;; Last-Modified = HTTP-date
1215 ;;
1216 (declare-date-header! "Last-Modified")
1217
1218
1219 \f
1220
1221 ;;;
1222 ;;; Request headers
1223 ;;;
1224
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 ) ]
1230 ;;
1231 (declare-param-list-header! "Accept"
1232 ;; -> (type/subtype (sym-prop . str-val) ...) ...)
1233 ;;
1234 ;; with the exception of prop `q', in which case the val will be a
1235 ;; valid quality value
1236 ;;
1237 (lambda (k v)
1238 (if (eq? k 'q)
1239 (parse-quality v)
1240 v))
1241 (lambda (k v)
1242 (if (eq? k 'q)
1243 (valid-quality? v)
1244 (string? v)))
1245 (lambda (k v port)
1246 (if (eq? k 'q)
1247 (write-quality v port)
1248 (default-val-writer k v port))))
1249
1250 ;; Accept-Charset = 1#( ( charset | "*" )[ ";" "q" "=" qvalue ] )
1251 ;;
1252 (declare-quality-list-header! "Accept-Charset")
1253
1254 ;; Accept-Encoding = 1#( codings [ ";" "q" "=" qvalue ] )
1255 ;; codings = ( content-coding | "*" )
1256 ;;
1257 (declare-quality-list-header! "Accept-Encoding")
1258
1259 ;; Accept-Language = 1#( language-range [ ";" "q" "=" qvalue ] )
1260 ;; language-range = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )
1261 ;;
1262 (declare-quality-list-header! "Accept-Language")
1263
1264 ;; Authorization = credentials
1265 ;;
1266 ;; Authorization is basically opaque to this HTTP stack, we just pass
1267 ;; the string value through.
1268 ;;
1269 (declare-opaque-header! "Authorization")
1270
1271 ;; Expect = 1#expectation
1272 ;; expectation = "100-continue" | expectation-extension
1273 ;; expectation-extension = token [ "=" ( token | quoted-string )
1274 ;; *expect-params ]
1275 ;; expect-params = ";" token [ "=" ( token | quoted-string ) ]
1276 ;;
1277 (declare-param-list-header! "Expect")
1278
1279 ;; From = mailbox
1280 ;;
1281 ;; Should be an email address; we just pass on the string as-is.
1282 ;;
1283 (declare-opaque-header! "From")
1284
1285 ;; Host = host [ ":" port ]
1286 ;;
1287 (declare-header! "Host"
1288 (lambda (str)
1289 (let ((colon (string-index str #\:)))
1290 (if colon
1291 (cons (substring str 0 colon)
1292 (parse-non-negative-integer str (1+ colon)))
1293 (cons str #f))))
1294 (lambda (val)
1295 (and (pair? val)
1296 (string? (car val))
1297 (or (not (cdr val))
1298 (non-negative-integer? (cdr val)))))
1299 (lambda (val port)
1300 (display (car val) port)
1301 (if (cdr val)
1302 (begin
1303 (display #\: port)
1304 (display (cdr val) port)))))
1305
1306 ;; If-Match = ( "*" | 1#entity-tag )
1307 ;;
1308 (declare-entity-tag-list-header! "If-Match")
1309
1310 ;; If-Modified-Since = HTTP-date
1311 ;;
1312 (declare-date-header! "If-Modified-Since")
1313
1314 ;; If-None-Match = ( "*" | 1#entity-tag )
1315 ;;
1316 (declare-entity-tag-list-header! "If-None-Match")
1317
1318 ;; If-Range = ( entity-tag | HTTP-date )
1319 ;;
1320 (declare-header! "If-Range"
1321 (lambda (str)
1322 (if (or (string-prefix? "\"" str)
1323 (string-prefix? "W/" str))
1324 (parse-entity-tag str)
1325 (parse-date str)))
1326 (lambda (val)
1327 (or (date? val) (entity-tag? val)))
1328 (lambda (val port)
1329 (if (date? val)
1330 (write-date val port)
1331 (write-entity-tag val port))))
1332
1333 ;; If-Unmodified-Since = HTTP-date
1334 ;;
1335 (declare-date-header! "If-Unmodified-Since")
1336
1337 ;; Max-Forwards = 1*DIGIT
1338 ;;
1339 (declare-integer-header! "Max-Forwards")
1340
1341 ;; Proxy-Authorization = credentials
1342 ;;
1343 (declare-opaque-header! "Proxy-Authorization")
1344
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
1354 ;;
1355 (declare-header! "Range"
1356 (lambda (str)
1357 (if (string-prefix? "bytes=" str)
1358 (cons
1359 'bytes
1360 (map (lambda (x)
1361 (let ((dash (string-index x #\-)))
1362 (cond
1363 ((not dash)
1364 (bad-header 'range str))
1365 ((zero? dash)
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))
1369 (else
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)))
1374 (lambda (val)
1375 (and (pair? val)
1376 (symbol? (car val))
1377 (list-of? (cdr val)
1378 (lambda (elt)
1379 (and (pair? elt)
1380 (let ((x (car elt)) (y (cdr elt)))
1381 (and (or x y)
1382 (or (not x) (non-negative-integer? x))
1383 (or (not y) (non-negative-integer? y)))))))))
1384 (lambda (val port)
1385 (display (car val) port)
1386 (display #\= port)
1387 (write-list
1388 (cdr val) port
1389 (lambda (pair port)
1390 (if (car pair)
1391 (display (car pair) port))
1392 (display #\- port)
1393 (if (cdr pair)
1394 (display (cdr pair) port)))
1395 ",")))
1396
1397 ;; Referer = ( absoluteURI | relativeURI )
1398 ;;
1399 (declare-uri-header! "Referer")
1400
1401 ;; TE = #( t-codings )
1402 ;; t-codings = "trailers" | ( transfer-extension [ accept-params ] )
1403 ;;
1404 (declare-param-list-header! "TE")
1405
1406 ;; User-Agent = 1*( product | comment )
1407 ;;
1408 (declare-opaque-header! "User-Agent")
1409
1410
1411 \f
1412
1413 ;;;
1414 ;;; Reponse headers
1415 ;;;
1416
1417 ;; Accept-Ranges = acceptable-ranges
1418 ;; acceptable-ranges = 1#range-unit | "none"
1419 ;;
1420 (declare-symbol-list-header! "Accept-Ranges")
1421
1422 ;; Age = age-value
1423 ;; age-value = delta-seconds
1424 ;;
1425 (declare-integer-header! "Age")
1426
1427 ;; ETag = entity-tag
1428 ;;
1429 (declare-header! "ETag"
1430 parse-entity-tag
1431 entity-tag?
1432 write-entity-tag)
1433
1434 ;; Location = absoluteURI
1435 ;;
1436 (declare-uri-header! "Location")
1437
1438 ;; Proxy-Authenticate = 1#challenge
1439 ;;
1440 ;; FIXME: split challenges ?
1441 (declare-opaque-header! "Proxy-Authenticate")
1442
1443 ;; Retry-After = ( HTTP-date | delta-seconds )
1444 ;;
1445 (declare-header! "Retry-After"
1446 (lambda (str)
1447 (if (and (not (string-null? str))
1448 (char-numeric? (string-ref str 0)))
1449 (parse-non-negative-integer str)
1450 (parse-date str)))
1451 (lambda (val)
1452 (or (date? val) (non-negative-integer? val)))
1453 (lambda (val port)
1454 (if (date? val)
1455 (write-date val port)
1456 (display val port))))
1457
1458 ;; Server = 1*( product | comment )
1459 ;;
1460 (declare-opaque-header! "Server")
1461
1462 ;; Vary = ( "*" | 1#field-name )
1463 ;;
1464 (declare-header! "Vary"
1465 (lambda (str)
1466 (if (equal? str "*")
1467 '*
1468 (split-header-names str)))
1469 (lambda (val)
1470 (or (eq? val '*) (list-of-header-names? val)))
1471 (lambda (val port)
1472 (if (eq? val '*)
1473 (display "*" port)
1474 (write-header-list val port))))
1475
1476 ;; WWW-Authenticate = 1#challenge
1477 ;;
1478 ;; Hum.
1479 (declare-opaque-header! "WWW-Authenticate")