read HTTP messages with a content-length

This commit is contained in:
Milo Turner 2020-04-10 21:02:39 -04:00
parent d8b7dd876a
commit 9253ba26a0
1 changed files with 24 additions and 3 deletions

View File

@ -179,8 +179,25 @@
#:break (string=? hln "")
(or (string->header-kv hln)
(raise-read-http-error "read-http-msg: invalid header line: ~s" hln))))
;; TODO: response body; the reader should be determined by the headers
(make-msg sln hdrs))
(define msg (make-msg sln hdrs))
(define bdy (read-http-body (hash-ref (http-msg-headers msg) 'transfer-encoding #f)
(hash-ref (http-msg-headers msg) 'content-length #f)
port))
(http-set-body msg bdy))
;; (http-body-reader tenc clen [port]) -> http-body?
;; tenc, clen : (or/c #f bytes?)
;; port : input-port?
(define (read-http-body tenc clen [port (current-input-port)])
(define len (and clen (string->number (bytes->string/utf-8 clen))))
(cond
[(equal? tenc #"chunked") (read-chunked-stream port)]
[(number? len) (read-bytes len port)]
[(and (not tenc) (not len)) #f]
[else (raise-read-http-error "read-http-body: not sure how to read HTTP body")]))
(define (read-chunked-stream port)
('...))
;; ==========================================
@ -197,8 +214,12 @@
(check-bytes->http-msg #"HTTP/1.1 200 OK\r\n"
#"Content-Type: text/html\r\n"
#"Content-Length: 5\r\n"
#"\r\n"
(make-http-res 200 '([content-type . "text/html"])))
#"hello"
(http-set-body (make-http-res 200 '([content-type . "text/html"]
[content-length . 5]))
#"hello"))
;; "write --> read" works
(let-values ([(in out) (make-pipe)])