Skip to main content

Crate base_web_multipart

Crate base_web_multipart 

Source
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.
ContentDisposition
A Content-Disposition field, as RFC 2183 defines it for MIME parts and RFC 7578 uses for form data: a disposition type, such as form-data, and parameters, such as name and filename.
ContentType
A part’s Content-Type field: its media type, which RFC 2046 defaults to text/plain; charset=us-ascii and RFC 7578 to text/plain for form data when the field is absent.
MultipartError
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.
MultipartLimits
The structural limits a parser applies, so that a hostile body cannot make it buffer or count without bound.
MultipartParser
A sans-I/O parser for a multipart body, as RFC 2046 section 5.1 defines it, fed bytes as they arrive.
MultipartWriter
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::parse found, with the number of input bytes it consumed.
Part
One part of a complete body: its header section and content, borrowed from the body.
PartFields
The fields of a PartHead.
PartHead
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.
PartHeaders
The header fields of a part that this crate understands, as a FieldSection to interpret a PartHead with. Other fields are ignored.
Parts
The parts of a complete body held in memory, in order, as parts produces them.

Enums§

BoundaryError
Why a boundary was rejected.
ContentTransferEncoding
A part’s Content-Transfer-Encoding field, limited to the identity encodings of RFC 2045 section 6, under which content is passed as it was written.
MultipartErrorKind
The kind of MultipartError.
MultipartEvent
One step through a multipart body, borrowed from the parser’s input.
MultipartWriteError
Why a writer refused to write.
ParameterRules
The rules for a Content-Disposition field’s extended parameters, such as filename*, 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.