1 unstable release
Uses old Rust 2015
0.5.0 | Jan 30, 2017 |
---|
#12 in #chunked
Used in uhttp_json_api
15KB
224 lines
uhttp_chunked_bytes -- Iterator for HTTP chunked body bytes
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());
Usage
This crate can be used through cargo by
adding it as a dependency in Cargo.toml
:
[dependencies]
uhttp_chunked_bytes = "0.5.0"
and importing it in the crate root:
extern crate uhttp_chunked_bytes;