Expand description
MIME multipart bodies, as RFC 2046 defines them and RFC 7578 uses them for form data, parsed and written without I/O.
MultipartParser is fed bytes as they arrive and reports each part’s header section and content as borrowed slices, never copying them, with MultipartLimits on the structure a hostile body could inflate. A part’s header section is interpreted with base-message, through PartHeaders or a section of your own, and ContentDisposition reads form field names and filenames, following the ParameterRules of forms or of other MIME parts. MultipartWriter writes a body, refusing header lines and content that would change its structure for a reader, with a Boundary generated from 192 random bits.
use base_web_multipart::{Boundary, ContentDisposition, MultipartWriter, ParameterRules, PartHeaders, parts};
use base_message::FieldSection;
let boundary = Boundary::generate();
let mut writer = MultipartWriter::new(boundary.clone());
let mut body = Vec::new();
let disposition = ContentDisposition::form_data("greeting")?.to_string();
writer.start_part([("Content-Disposition", disposition.as_bytes())], &mut body)?;
writer.write_content(b"hello", &mut body)?;
writer.finish(&mut body);
for part in parts(&body, &boundary) {
let part = part?;
let headers = PartHeaders::interpret(part.head())?;
let disposition = headers.content_disposition.expect("each form part has a disposition");
assert_eq!((disposition.name(ParameterRules::FormData)?.as_deref(), part.content()), (Some("greeting"), &b"hello"[..]));
}The crate frames parts only. Forms, with their limits on fields and uploads, belong to base-web-content, and HTTP transport to base-http. The Getting Started guide covers parsing a streamed body and writing one.
Structs§
- Boundary
- A multipart boundary, as RFC 2046 section 5.1.1 defines it: 1 to 70 characters from a restricted set, not ending in a space.
- Content
Disposition - A
Content-Dispositionfield, as RFC 2183 defines it for MIME parts and RFC 7578 uses for form data: a disposition type, such asform-data, and parameters, such asnameandfilename. - Content
Type - A part’s
Content-Typefield: its media type, which RFC 2046 defaults totext/plain; charset=us-asciiand RFC 7578 totext/plainfor form data when the field is absent. - Multipart
Error - Why a multipart body could not be parsed, with the offset in the body where the parser found it, which is the same however the body was split. It never includes the body’s bytes, which may be large or untrusted.
- Multipart
Limits - The structural limits a parser applies, so that a hostile body cannot make it buffer or count without bound.
- Multipart
Parser - A sans-I/O parser for a multipart body, as RFC 2046 section 5.1 defines it, fed bytes as they arrive.
- Multipart
Writer - A sans-I/O writer for a multipart body, which appends the body’s bytes to a caller’s buffer, to be sent as the caller chooses.
- Parsed
- What one call to
MultipartParser::parsefound, with the number of input bytes it consumed. - Part
- One part of a complete body: its header section and content, borrowed from the body.
- Part
Fields - The fields of a
PartHead. - Part
Head - A part’s header section, borrowed from the parser’s input and already checked: each line is a field name, a colon, and a value, ending with CRLF.
- Part
Headers - The header fields of a part that this crate understands, as a
FieldSectionto interpret aPartHeadwith. Other fields are ignored. - Parts
- The parts of a complete body held in memory, in order, as
partsproduces them.
Enums§
- Boundary
Error - Why a boundary was rejected.
- Content
Transfer Encoding - A part’s
Content-Transfer-Encodingfield, limited to the identity encodings of RFC 2045 section 6, under which content is passed as it was written. - Multipart
Error Kind - The kind of
MultipartError. - Multipart
Event - One step through a multipart body, borrowed from the parser’s input.
- Multipart
Write Error - Why a writer refused to write.
- Parameter
Rules - The rules for a
Content-Dispositionfield’s extended parameters, such asfilename*, when reading its name or filename. Forms and other MIME parts follow different RFCs, so a disposition does not choose for you.
Functions§
- parts
- The parts of a complete body, with the default limits.