GitHub

multipart_parser

API reference for multipart_parser.

A streaming multipart/form-data parser (Effect's MultipartParser): write the body's chunks, and it calls back with each field's value, each file's chunks, errors, and the end.

make(...) takes the request's headers (for the boundary), the callbacks, and limits (max_parts, max_total_size, max_part_size, max_field_size; 1 MiB for fields by default).

Types

TypeDefinitionDescription
Limittype Limit = Literal['MaxParts', 'MaxTotalSize', 'MaxPartSize', 'MaxFieldSize']
MultipartErrortype MultipartError = InvalidBoundary | BadHeaders | InvalidDisposition | ReachedLimit | EndNotReached
OnChunktype OnChunk = Callable[[bytes | None], None]

A file's chunks, then None at its end.

Classes

Functions

default_is_file

default_is_file(info) -> bool

A part is a file when it has a file name or is application/octet-stream.

ParameterTypeDescription
infoPartInfo

Returns bool

def default_is_file(info: PartInfo) -> bool:
    """A part is a file when it has a file name or is
    ``application/octet-stream``."""
    return info.filename is not None or info.content_type == "application/octet-stream"

make

make(*, headers, on_file, on_field, on_error, on_done, is_file=default_is_file, max_parts=None, max_total_size=None, max_part_size=None, max_field_size=None) -> Parser

A parser of the body headers describe. Without a boundary in the content type, or with one RFC 2046 does not allow (1 to 70 characters; Effect takes any), it reports InvalidBoundary at once and ignores every write.

ParameterTypeDescription
headersMapping[str, str]
on_fileCallable[[PartInfo], OnChunk]
on_fieldCallable[[PartInfo, bytes], None]
on_errorCallable[[MultipartError], None]
on_doneCallable[[], None]
is_fileCallable[[PartInfo], bool] = default_is_file
max_partsfloat | None = None
max_total_sizebyte_size.Input | None = None
max_part_sizebyte_size.Input | None = None
max_field_sizebyte_size.Input | None = None

Returns Parser

def make(
    *,
    headers: Mapping[str, str],
    on_file: Callable[[PartInfo], OnChunk],
    on_field: Callable[[PartInfo, bytes], None],
    on_error: Callable[[MultipartError], None],
    on_done: Callable[[], None],
    is_file: Callable[[PartInfo], bool] = default_is_file,
    max_parts: float | None = None,
    max_total_size: byte_size.Input | None = None,
    max_part_size: byte_size.Input | None = None,
    max_field_size: byte_size.Input | None = None,
) -> Parser:
    """A parser of the body ``headers`` describe. Without a boundary in
    the content type, or with one RFC 2046 does not allow (1 to 70
    characters; Effect takes any), it reports ``InvalidBoundary`` at once
    and ignores every write."""
    boundary = ct.parse(headers.get("content-type")).parameters.get("boundary")
    if boundary is None or not _BOUNDARY.fullmatch(boundary):
        on_error(InvalidBoundary())
        return _Noop()
    return _Parser(
        boundary,
        on_file,
        on_field,
        on_error,
        on_done,
        is_file,
        math.inf if max_parts is None else max_parts,
        _limit(max_total_size, math.inf),
        _limit(max_part_size, math.inf),
        _limit(max_field_size, 1024 * 1024),
    )

decode_field

decode_field(info, value) -> str

A field's value decoded by its charset as TextDecoder reads the label (UTF-8 by default or for one outside the Encoding Standard).

ParameterTypeDescription
infoPartInfo
valuebytes

Returns str

def decode_field(info: PartInfo, value: bytes) -> str:
    """A field's value decoded by its ``charset`` as ``TextDecoder`` reads the
    label (UTF-8 by default or for one outside the Encoding Standard)."""
    return _whatwg_encoding.decode(info.content_type_parameters.get("charset", "utf-8"), value)