Skip to content

Incoming HTTP1 bodies should know content_length and is_end_stream #1545

Closed
@seanmonstar

Description

@seanmonstar

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-bodyArea: body streaming.C-featureCategory: feature. This is adding a new feature.E-easyEffort: easy. A task that would be a great starting point for a new contributor.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions