Changing normalize-response to be more user friendly

This commit is contained in:
Jay McCarthy 2010-05-12 11:25:22 -06:00
parent 8f5b1f622c
commit 4f17ec419e
3 changed files with 13 additions and 10 deletions

View File

@ -19,7 +19,7 @@
(define (response/full->size resp) (define (response/full->size resp)
(apply + (map bytes-length (response/full-body resp)))) (apply + (map bytes-length (response/full-body resp))))
(define (normalize-response close? resp) (define (normalize-response resp [close? #f])
(cond (cond
[(response/full? resp) [(response/full? resp)
(make-response/full (make-response/full
@ -43,14 +43,14 @@
(response/incremental-generator resp)))] (response/incremental-generator resp)))]
[(response/basic? resp) [(response/basic? resp)
(normalize-response (normalize-response
close?
(make-response/full (make-response/full
(response/basic-code resp) (response/basic-code resp)
(response/basic-message resp) (response/basic-message resp)
(response/basic-seconds resp) (response/basic-seconds resp)
(response/basic-mime resp) (response/basic-mime resp)
(response/basic-headers resp) (response/basic-headers resp)
empty))] empty)
close?)]
[(and (list? resp) [(and (list? resp)
(not (empty? resp)) (not (empty? resp))
(bytes? (first resp)) (bytes? (first resp))
@ -58,18 +58,18 @@
(bytes? i))) (bytes? i)))
(rest resp))) (rest resp)))
(normalize-response (normalize-response
close?
(make-response/full (make-response/full
200 #"Okay" (current-seconds) (car resp) empty 200 #"Okay" (current-seconds) (car resp) empty
(map (lambda (bs) (map (lambda (bs)
(if (string? bs) (if (string? bs)
(string->bytes/utf-8 bs) (string->bytes/utf-8 bs)
bs)) bs))
(rest resp))))] (rest resp)))
close?)]
[else [else
(normalize-response (normalize-response
close? (make-xexpr-response resp)
(make-xexpr-response resp))])) close?)]))
(define (make-xexpr-response (define (make-xexpr-response
xexpr xexpr
@ -108,5 +108,5 @@
((pretty-xexpr/c) ((pretty-xexpr/c)
(#:code number? #:message bytes? #:seconds number? #:mime-type bytes? #:headers (listof header?)) (#:code number? #:message bytes? #:seconds number? #:mime-type bytes? #:headers (listof header?))
. ->* . response/full?)] . ->* . response/full?)]
[normalize-response (boolean? response/c . -> . (or/c response/full? response/incremental?))] [normalize-response ((response/c) (boolean?) . ->* . (or/c response/full? response/incremental?))]
[TEXT/HTML-MIME-TYPE bytes?]) [TEXT/HTML-MIME-TYPE bytes?])

View File

@ -59,7 +59,7 @@
(output-response/method conn resp #"GET")) (output-response/method conn resp #"GET"))
(define (output-response/method conn resp meth) (define (output-response/method conn resp meth)
(define bresp (normalize-response (connection-close? conn) resp)) (define bresp (normalize-response resp (connection-close? conn)))
(output-headers+response/basic conn bresp) (output-headers+response/basic conn bresp)
(unless (bytes-ci=? meth #"HEAD") (unless (bytes-ci=? meth #"HEAD")
(output-response/basic conn bresp))) (output-response/basic conn bresp)))

View File

@ -246,9 +246,12 @@ Here is an example typical of what you will find in many applications:
(list (string->bytes/utf-8 (xexpr->string xexpr)))) (list (string->bytes/utf-8 (xexpr->string xexpr))))
]} ]}
@defproc[(normalize-response [close? boolean?] [response response/c]) @defproc[(normalize-response [response response/c] [close? boolean? #f])
(or/c response/full? response/incremental?)]{ (or/c response/full? response/incremental?)]{
Coerces @racket[response] into a full response, filling in additional details where appropriate. Coerces @racket[response] into a full response, filling in additional details where appropriate.
@racket[close?] represents whether the connection will be closed after the response is sent (i.e. if HTTP 1.0 is being used.) The accuracy of this only matters if
@racket[response] is a @racket[response/incremental?].
} }
@defthing[TEXT/HTML-MIME-TYPE bytes?]{Equivalent to @racket[#"text/html; charset=utf-8"].} @defthing[TEXT/HTML-MIME-TYPE bytes?]{Equivalent to @racket[#"text/html; charset=utf-8"].}