Closed
Description
For instance, if a server receives the follow request:
POST /foo HTTP/1.1
content-length: 15
The Body
in the Request
should know these things, neither of which are currently true:
Body::content_length()
should beSome(15)
.Body::is_end_stream()
should return true afterpoll_data
has returned chunks of length totaling 15 bytes.
Steps to fixing
- Adding an
Option<u64>
to theKind::Chan
variant ofBody
. - Changing
h1::Conn::read_head
to return anOption<BodyLength>
instead ofbool
. - Updating the
h1::Dispatcher
to use theOption<BodyLength>
to optionally set the newOption<u64>
field of theChan
variant. - Updating
Body::content_length
to return the new field fromKind::Chan
. - Updating
Body::poll_data
in theKind::Chan
variant to subtract from the new field the length of each chunk that passes by. - Updating
Body::is_end_stream
in theKind::Chan
variant to return true ifthe field is exactly
Some(0)`.
Testing it works
- Add a test to
tests/client.rs
with a server replying with the bytesHTTP/1.1 200 OK\r\ncontent-length: 5\r\n\r\nhello
. - In the test, make a
GET
request to the server, and wait for theResponse<Body>
. - Add an
assert_eq!(res.body().content_length(), Some(5))
. - Add an
assert!(!res.body().is_end_stream())
(the body should be done yet, there's still 5 bytes to stream). - Call
res.body_mut().poll_data()
to get the chunk. Assert it is 5 bytes for sanity. - Finally,
assert!(res.body().is_end_stream())
, as the 5 bytes have now been streamed.