Skip to main content

MultipartParser

Struct MultipartParser 

Source
pub struct MultipartParser { /* private fields */ }
Expand description

A sans-I/O parser for a multipart body, as RFC 2046 section 5.1 defines it, fed bytes as they arrive.

The caller owns the buffer. Each call to Self::parse reports how many bytes it consumed and what they held; the caller drops the consumed bytes, appends whatever arrives next, and calls again with what remains. Events borrow from the input, so content and header fields are never copied. A part’s header section is returned whole, so the caller must be able to buffer Self::minimum_buffer_size bytes; the parser reports an error rather than needing more.

use base_web_multipart::{Boundary, MultipartEvent, MultipartParser};

let body = b"--b\r\nContent-Disposition: form-data; name=\"a\"\r\n\r\nvalue\r\n--b--\r\n";
let mut parser = MultipartParser::new(&Boundary::new("b")?);
let mut input = &body[..];
let mut content = Vec::new();
loop {
    let parsed = parser.parse(input)?;
    match parsed.event {
        MultipartEvent::Part(head) => assert_eq!(head.len(), 1),
        MultipartEvent::Content(bytes) => content.extend_from_slice(bytes),
        MultipartEvent::PartEnd => {}
        MultipartEvent::End => break,
        MultipartEvent::Incomplete => unreachable!("the whole body is available"),
    }
    input = &input[parsed.consumed..];
}
assert_eq!(content, b"value");

Implementations§

Source§

impl MultipartParser

Source

pub fn new(boundary: &Boundary) -> Self

A parser for a body with this boundary, with the default limits.

Source

pub fn with_limits(boundary: &Boundary, limits: MultipartLimits) -> Self

A parser for a body with this boundary and these limits.

Source

pub fn limits(&self) -> MultipartLimits

The limits this parser applies.

Source

pub fn minimum_buffer_size(&self) -> usize

The input a caller must be able to buffer: given at least this many bytes, Self::parse always consumes some or reports an event other than MultipartEvent::Incomplete.

Source

pub fn is_finished(&self) -> bool

Whether the closing boundary line has been read, so that MultipartEvent::End was reported.

Source

pub fn parse<'a>( &mut self, input: &'a [u8], ) -> Result<Parsed<'a>, MultipartError>

Parse the next event from input, which must begin with the bytes the previous call did not consume.

§Errors

A MultipartError when the body is malformed or exceeds a limit. Once the parser reports an error, it reports the same error for every later call.

Source

pub fn finish(&self) -> Result<(), MultipartError>

Check that the body was complete, once its input has ended. A body may end immediately after its closing --, without a line break.

§Errors

The parser’s error, if it reported one, or MultipartErrorKind::UnexpectedEnd when the closing boundary was not reached.

Trait Implementations§

Source§

impl Clone for MultipartParser

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MultipartParser

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.