Changing normalize-response to be more user friendly
This commit is contained in:
parent
8f5b1f622c
commit
4f17ec419e
|
@ -19,7 +19,7 @@
|
|||
(define (response/full->size resp)
|
||||
(apply + (map bytes-length (response/full-body resp))))
|
||||
|
||||
(define (normalize-response close? resp)
|
||||
(define (normalize-response resp [close? #f])
|
||||
(cond
|
||||
[(response/full? resp)
|
||||
(make-response/full
|
||||
|
@ -43,14 +43,14 @@
|
|||
(response/incremental-generator resp)))]
|
||||
[(response/basic? resp)
|
||||
(normalize-response
|
||||
close?
|
||||
(make-response/full
|
||||
(response/basic-code resp)
|
||||
(response/basic-message resp)
|
||||
(response/basic-seconds resp)
|
||||
(response/basic-mime resp)
|
||||
(response/basic-headers resp)
|
||||
empty))]
|
||||
empty)
|
||||
close?)]
|
||||
[(and (list? resp)
|
||||
(not (empty? resp))
|
||||
(bytes? (first resp))
|
||||
|
@ -58,18 +58,18 @@
|
|||
(bytes? i)))
|
||||
(rest resp)))
|
||||
(normalize-response
|
||||
close?
|
||||
(make-response/full
|
||||
200 #"Okay" (current-seconds) (car resp) empty
|
||||
(map (lambda (bs)
|
||||
(if (string? bs)
|
||||
(string->bytes/utf-8 bs)
|
||||
bs))
|
||||
(rest resp))))]
|
||||
(rest resp)))
|
||||
close?)]
|
||||
[else
|
||||
(normalize-response
|
||||
close?
|
||||
(make-xexpr-response resp))]))
|
||||
(make-xexpr-response resp)
|
||||
close?)]))
|
||||
|
||||
(define (make-xexpr-response
|
||||
xexpr
|
||||
|
@ -108,5 +108,5 @@
|
|||
((pretty-xexpr/c)
|
||||
(#:code number? #:message bytes? #:seconds number? #:mime-type bytes? #:headers (listof header?))
|
||||
. ->* . 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?])
|
||||
|
|
|
@ -59,7 +59,7 @@
|
|||
(output-response/method conn resp #"GET"))
|
||||
|
||||
(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)
|
||||
(unless (bytes-ci=? meth #"HEAD")
|
||||
(output-response/basic conn bresp)))
|
||||
|
|
|
@ -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))))
|
||||
]}
|
||||
|
||||
@defproc[(normalize-response [close? boolean?] [response response/c])
|
||||
@defproc[(normalize-response [response response/c] [close? boolean? #f])
|
||||
(or/c response/full? response/incremental?)]{
|
||||
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"].}
|
||||
|
|
Loading…
Reference in New Issue
Block a user