uhttp_chunked_bytes 0.5.0

Zero-allocation iterator for HTTP chunked body bytes
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented1 out of 3 items with examples
  • Size
  • Source code size: 15.73 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.13 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • kchmck/uhttp_chunked_bytes.rs
    0 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • kchmck

This crate provides a zero-allocation iterator for the payload bytes in an HTTP chunked-encoded body. It wraps a given iterator over raw HTTP body bytes, decodes the chunked transfer protocol, and yields the data bytes from each chunk. The result can be fed, for example, into a byte-based parser such as serde_json::from_iter.

This implementation supports chunk lengths up to that which can be stored by usize on the target platform. Chunk extension parameters are discarded, and trailing headers aren't processed, although they can be retrieved from the wrapped source iterator at the end of chunked payload iteration.

Example

use uhttp_chunked_bytes::ChunkedBytes;

// Create a sample json body `{"key": 42}`, split over two chunks.
let body = b"4\r\n{\"ke\r\n7\r\ny\": 42}\r\n0\r\n\r\n";
let mut stream = body.iter().map(|&b| Ok(b));

let mut bytes = ChunkedBytes::new(&mut stream);
assert_eq!(bytes.next().unwrap().unwrap(), b'{');
assert_eq!(bytes.next().unwrap().unwrap(), b'"');
assert_eq!(bytes.next().unwrap().unwrap(), b'k');
assert_eq!(bytes.next().unwrap().unwrap(), b'e');
assert_eq!(bytes.next().unwrap().unwrap(), b'y');
assert_eq!(bytes.next().unwrap().unwrap(), b'"');
assert_eq!(bytes.next().unwrap().unwrap(), b':');
assert_eq!(bytes.next().unwrap().unwrap(), b' ');
assert_eq!(bytes.next().unwrap().unwrap(), b'4');
assert_eq!(bytes.next().unwrap().unwrap(), b'2');
assert_eq!(bytes.next().unwrap().unwrap(), b'}');
assert!(bytes.next().is_none());