Skip to main content

ChunkedDecoder

Struct ChunkedDecoder 

Source
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

Source

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

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

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

pub fn total(&self) -> u64

The body bytes delivered so far, after chunked decoding.

§Examples
use http_net::{ChunkedDecoder, Limits};

let mut d = ChunkedDecoder::new(&Limits::new());
let _ = d.decode(b"3\r\nabc\r\n", false)?;
assert_eq!(d.total(), 3);

Trait Implementations§

Source§

impl Debug for ChunkedDecoder

Shows progress and limits; never body bytes.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.