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 HTTP2 bodies should know content_length #1546

Closed
seanmonstar opened this issue Jun 6, 2018 · 6 comments
Closed

Incoming HTTP2 bodies should know content_length #1546

seanmonstar opened this issue Jun 6, 2018 · 6 comments
Labels
A-body Area: body streaming. A-http2 Area: HTTP/2 specific. 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

When receiving a body over HTTP2, the Body::content_length call should be able to say how big it is if the content-length header was present.

Steps to fix

  • Update Kind::H2 variant of Body to include an Option<u64>.
  • Update the Body::h2 constructor to take a secong argument, Option<u64>.
  • Update both proto::h2::client and proto::h2::server to look for and parse a content-length header before calling Body::h2.
@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. A-http2 Area: HTTP/2 specific. labels Jun 6, 2018
@joshleeb
Copy link

joshleeb commented Jun 8, 2018

Is it worth Body::content_length() returning the content length of the received response even if the header isn't present?

I assuming that a request without a content-length header is still valid, and that we can simply figure out the content length by taking the length of the body.

@lnicola
Copy link
Contributor

lnicola commented Jun 8, 2018

I think this is about returning the value of the Content-Length header. hyper doesn't buffer the message, so if the other side doesn't send the header, Body::content_length() has no way of knowing what to return.

@joshleeb
Copy link

joshleeb commented Jun 8, 2018

That makes sense. Thanks @lnicola

@seanmonstar
Copy link
Member Author

Exactly as @lnicola said. This is about when a user receives a hyper::Body from hyper, the <Body as Payload>::content_length() call can try to give more information. This is especially useful if using that body to stream to a different message, such as in a proxy, having made a client request, and gotten a response back, and then returning that body as your own server's body.

As the documentation of content_length says, it's OK to not know the answer. It's just a hint to allow hyper to add a content-length header if not present. This in turn means that several common body cases will include a content-length header without forcing the user to remember it (like returning Body::from("hello world").

@lnicola
Copy link
Contributor

lnicola commented Jun 8, 2018

@joshleeb Do you want to take this?

@joshleeb
Copy link

joshleeb commented Jun 8, 2018

Yea sure, thanks 😄

seanmonstar pushed a commit that referenced this issue Jun 18, 2018
- Add `Body::Kind::H2` to contain the content length of the body.
- Update `Body::content_length` to return the content length if `Body::Kind` is `H2`, instead of returning `None`.

Reference: #1556, #1557

Closes #1546
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-body Area: body streaming. A-http2 Area: HTTP/2 specific. 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

3 participants