Crate uhttp_chunked_bytes [] [src]

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());

Structs

ChunkedBytes

Iterator over payload bytes in a chunked-encoded stream.