Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incoming HTTP1 bodies should know content_length and is_end_stream #1545

Closed
seanmonstar opened this issue Jun 6, 2018 · 0 comments
Closed
Labels
A-body Area: body streaming. C-feature Category: feature. This is adding a new feature. E-easy Effort: easy. A task that would be a great starting point for a new contributor.

Comments

@seanmonstar
Copy link
Member

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 be Some(15).
  • Body::is_end_stream() should return true after poll_data has returned chunks of length totaling 15 bytes.

Steps to fixing

  • Adding an Option<u64> to the Kind::Chan variant of Body.
  • Changing h1::Conn::read_head to return an Option<BodyLength> instead of bool.
  • Updating the h1::Dispatcher to use the Option<BodyLength> to optionally set the new Option<u64> field of the Chan variant.
  • Updating Body::content_length to return the new field from Kind::Chan.
  • Updating Body::poll_data in the Kind::Chan variant to subtract from the new field the length of each chunk that passes by.
  • Updating Body::is_end_stream in the Kind::Chan variant to return true ifthe field is exactlySome(0)`.

Testing it works

  • Add a test to tests/client.rs with a server replying with the bytes HTTP/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 the Response<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.
@seanmonstar seanmonstar added E-easy Effort: easy. A task that would be a great starting point for a new contributor. C-feature Category: feature. This is adding a new feature. A-body Area: body streaming. labels Jun 6, 2018
seanmonstar pushed a commit that referenced this issue Jun 8, 2018
When getting a `Body` from hyper, such as in a client response,
the method `Body::content_length()` now returns a value if the header
was present.

Closes #1545
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-body Area: body streaming. C-feature Category: feature. This is adding a new feature. E-easy Effort: easy. A task that would be a great starting point for a new contributor.
Projects
None yet
Development

No branches or pull requests

1 participant