Skip to content

Chunk method to detect if it is the final part of real http-chunk or not #2087

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

Closed
wants to merge 3 commits into from

Conversation

inv2004
Copy link

@inv2004 inv2004 commented Dec 20, 2019

feat(client): Chunk method to detect if it is the final part of real http-chunk or not

During chunked transfer - hyper can return only part of http-chunk and it does not provide ability to detect if it is just part of the chunk or not. The solution could be to fill chunks in the buffer until you know that you read full size of the http-chunk-len. This PR adds Chunk::is_end_of_chunk(&self) -> bool method which return true - if you read http-chunk-len.

let mut buf = Vec::new();
body.for_each(move |chunk| {
    let is_final = chunk.is_end_of_chunk();
    buf.extend_from_slice(&chunk.into_bytes());

   if !is_final {
      return Ok(())
   }

   println!("buf contains valid json");
   buf.clear();
}

@inv2004 inv2004 changed the base branch from master to 0.12.x December 20, 2019 20:23
@inv2004 inv2004 changed the title Chunk helper method to detect if it is the final part of real http-chunk or not Chunk method to detect if it is the final part of real http-chunk or not Dec 20, 2019
@sfackler
Copy link
Contributor

sfackler commented Dec 20, 2019

Why is the end of the stream not a sufficient indicator? In this example you'd just move the println out after the for_each.

EDIT: Nevermind, you're looking for chunk boundaries, not message boundaries.

@inv2004
Copy link
Author

inv2004 commented Dec 20, 2019 via email

@seanmonstar
Copy link
Member

Intermediaries (proxies and gateways) may re-interpret the chunked encoding, so this isn't reliable. Additionally, HTTP2 and 3 don't use chunked encoding. And in 0.13, we got rid of the Chunk type in favor of just Bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants