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#
| Type | Definition | Description |
|---|---|---|
Limit | type Limit = Literal['MaxParts', 'MaxTotalSize', 'MaxPartSize', 'MaxFieldSize'] | |
MultipartError | type MultipartError = InvalidBoundary | BadHeaders | InvalidDisposition | ReachedLimit | EndNotReached | |
OnChunk | type OnChunk = Callable[[bytes | None], None] | A file's chunks, then |
Classes#
Functions#
default_is_file#
default_is_file(info) -> boolA part is a file when it has a file name or is
application/octet-stream.
| Parameter | Type | Description |
|---|---|---|
info | PartInfo |
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) -> ParserA 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.
| Parameter | Type | Description |
|---|---|---|
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 |
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) -> strA field's value decoded by its charset as TextDecoder reads the
label (UTF-8 by default or for one outside the Encoding Standard).
| Parameter | Type | Description |
|---|---|---|
info | PartInfo | |
value | bytes |
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)