Skip to main content

Crate nostd_multipart

Crate nostd_multipart 

Source
Expand description

Heap-free, panic-free, streaming multipart/form-data parser and encoder for #![no_std].

The crate is sans-IO: you feed bytes to Parser::parse and get back at most one Event per call plus the number of bytes consumed. Part data is yielded as sub-slices of your own input buffer (zero-copy). Only part headers are copied, into a line buffer whose size is the const generic LINE.

The normative specification is DESIGN.md in the repository; the rule numbers referenced in the source (rule 3, …) point there.

use nostd_multipart::{Event, Parser};

let body = b"--xyz\r\nContent-Disposition: form-data; name=\"f\"\r\n\r\nhello\r\n--xyz--";
let mut p: Parser<'_> = Parser::new(b"xyz").unwrap();
let mut pos = 0;
let mut data = Vec::new();
loop {
    let (used, ev) = p.parse(&body[pos..], true).unwrap();
    pos += used;
    match ev {
        Some(Event::Data(d)) => data.extend_from_slice(d),
        Some(Event::Finished) => break,
        _ => {}
    }
}
assert_eq!(data, b"hello");

Structs§

ContentDisposition
A parsed Content-Disposition header value.
Decoded
Iterator over the decoded bytes of a ParamValue.
Encoder
Streaming multipart/form-data encoder.
MultipartReader
Drives a Parser from a blocking embedded_io::Read with a BUF-byte buffer (BUF >= parser.min_buffer()).
MultipartReaderAsync
Drives a Parser from an embedded_io_async::Read with a BUF-byte buffer (BUF >= parser.min_buffer()).
ParamValue
A parameter value: the raw bytes plus lazy decoding of RFC 822 quoted-pairs (\x → x) and the WHATWG escapes %22, %0D, %0A (→ ", CR, LF). Other % sequences are left as they are.
Parser
Streaming, zero-copy multipart/form-data parser.
Part
Description of one part for the Encoder.

Enums§

Error
Every failure the crate can report. See DESIGN.md §12.
Event
One parse event. 'i is the lifetime of the input slice passed to Parser::parse, 'p the borrow of the parser.
ReadError
Error of the reader adapters.

Constants§

MAX_PADDING
Maximum number of SP/HTAB transport-padding bytes accepted between the boundary and the CRLF of a part delimiter (rule 3). More padding means “not a delimiter”.

Functions§

boundary_from_random
Maps N random bytes (e.g. from a hardware RNG) to a valid boundary of N bchars from A–Z a–z 0–9 - _ (6 bits of entropy per byte).
content_disposition
Parses a Content-Disposition header value (rule 8).