pub struct ChunkedDecoder { /* private fields */ }Expand description
Decodes a chunked body (RFC 9112 section 7.1) from input the caller owns, with the exact grammar and budgets of the connections. Nothing is allocated.
§Examples
use http_net::{ChunkStep, ChunkedDecoder, Limits};
let wire = b"4\r\nWiki\r\n5;ext=1\r\npedia\r\n0\r\nDigest: x\r\n\r\nNEXT";
let mut decoder = ChunkedDecoder::new(&Limits::new());
let (mut pos, mut body) = (0, Vec::new());
loop {
match decoder.decode(&wire[pos..], false)? {
ChunkStep::Data { skip, len } => {
body.extend_from_slice(&wire[pos + skip..pos + skip + len]);
pos += skip + len;
}
ChunkStep::Trailers { skip, len } => {
assert_eq!(&wire[pos + skip..pos + skip + len], b"Digest: x\r\n\r\n");
pos += skip + len;
}
ChunkStep::Done { consumed } => {
pos += consumed;
break;
}
ChunkStep::NeedMore { .. } => unreachable!("the whole body is here"),
_ => unreachable!(),
}
}
assert_eq!(body, b"Wikipedia");
assert_eq!(&wire[pos..], b"NEXT");
assert!(decoder.is_done());
assert_eq!(decoder.total(), 9);Implementations§
Source§impl ChunkedDecoder
impl ChunkedDecoder
Sourcepub fn new(limits: &Limits) -> Self
pub fn new(limits: &Limits) -> Self
A decoder for one chunked body, with limits (body size, chunk-size
line, extension budget, trailer budgets).
§Examples
let decoder = http_net::ChunkedDecoder::new(&http_net::Limits::new().with_max_body(64));
assert!(!decoder.is_done());Sourcepub fn decode(
&mut self,
input: &[u8],
eof: bool,
) -> Result<ChunkStep, HttpError>
pub fn decode( &mut self, input: &[u8], eof: bool, ) -> Result<ChunkStep, HttpError>
Decodes from input, which starts where the previous call’s consumed
bytes ended. eof says no more input will come.
§Errors
Every chunk error of the connections: HttpError::InvalidChunkSize,
HttpError::InvalidChunkExtension,
HttpError::InvalidChunkTerminator, HttpError::ChunkLineTooLong,
HttpError::ChunkExtensionsTooLarge, HttpError::BareLf,
HttpError::BodyTooLarge; the field errors of the trailer section;
HttpError::UnexpectedEof when eof comes before the end. After an
error the body cannot be resynchronised: drop the connection.
§Examples
use http_net::{ChunkStep, ChunkedDecoder, HttpError, Limits};
let mut d = ChunkedDecoder::new(&Limits::new());
// A partial chunk-size line: nothing consumed, more needed.
assert_eq!(d.decode(b"1", false)?, ChunkStep::NeedMore { consumed: 0 });
assert_eq!(d.decode(b"1", true), Err(HttpError::UnexpectedEof));
let mut d = ChunkedDecoder::new(&Limits::new());
assert_eq!(d.decode(b"5\nhello", false), Err(HttpError::BareLf));Sourcepub fn is_done(&self) -> bool
pub fn is_done(&self) -> bool
Whether the whole body, trailers included, has been decoded.
§Examples
use http_net::{ChunkStep, ChunkedDecoder, Limits};
let mut d = ChunkedDecoder::new(&Limits::new());
assert_eq!(d.decode(b"0\r\n\r\n", false)?, ChunkStep::Done { consumed: 5 });
assert!(d.is_done());