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
impl MultipartParser
Sourcepub fn new(boundary: &Boundary) -> Self
pub fn new(boundary: &Boundary) -> Self
A parser for a body with this boundary, with the default limits.
Sourcepub fn with_limits(boundary: &Boundary, limits: MultipartLimits) -> Self
pub fn with_limits(boundary: &Boundary, limits: MultipartLimits) -> Self
A parser for a body with this boundary and these limits.
Sourcepub fn limits(&self) -> MultipartLimits
pub fn limits(&self) -> MultipartLimits
The limits this parser applies.
Sourcepub fn minimum_buffer_size(&self) -> usize
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.
Sourcepub fn is_finished(&self) -> bool
pub fn is_finished(&self) -> bool
Whether the closing boundary line has been read, so that MultipartEvent::End was reported.
Sourcepub fn parse<'a>(
&mut self,
input: &'a [u8],
) -> Result<Parsed<'a>, MultipartError>
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.
Sourcepub fn finish(&self) -> Result<(), MultipartError>
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.