ushka.http.multipart
This module provides utilities for parsing multipart/form-data requests, commonly used for file uploads in web applications.
It includes the MultiPartCollector class, which acts as a callback handler
for a low-level multipart parser, and parse_multipart_asgi for integrating
with ASGI requests.
class MultiPartCollector
Collects and processes data parts from a multipart HTTP request.
This class acts as a callback handler for a low-level multipart parser, collecting form fields and files, and enforcing size limits to prevent denial-of-service attacks.
__init__(self, charset = 'utf-8', max_ram_size_kb = 2048, max_file_size_kb = 51200, max_text_field_size_kb = 64)
Initializes the MultiPartCollector.
Parameters
charset | str | The character set to use for decoding field names and values. Defaults to "utf-8". |
max_ram_size_kb | int | The maximum size (in KB) a file part can occupy in RAM before being written to a temporary disk file. Defaults to 2048 KB. |
max_file_size_kb | int | The maximum allowed size (in KB) for any single file uploaded.
Exceeding this raises |
max_text_field_size_kb | int | The maximum allowed size (in KB) for any non-file (text) field.
Exceeding this raises |
on_part_begin(self)
Callback invoked at the beginning of parsing a new multipart part.
Resets internal state variables for the new part, including headers, field name, file status, and data buffers.
on_header_field(self, data: bytes, start: int, end: int)
Callback invoked when a header field name is parsed.
Appends the chunk of data to the _current_header_name buffer.
Parameters
data | bytes | The raw bytes of the header. |
start | int | The starting index of the current chunk in |
end | int | The ending index (exclusive) of the current chunk in |
on_header_value(self, data: bytes, start: int, end: int)
Callback invoked when a header field value is parsed.
Appends the chunk of data to the _current_header_value buffer.
Parameters
data | bytes | The raw bytes of the header. |
start | int | The starting index of the current chunk in |
end | int | The ending index (exclusive) of the current chunk in |
on_header_end(self)
Callback invoked when a header field (name and value) has finished parsing.
Stores the parsed header in _current_headers and resets buffers for the next header.
on_headers_finished(self)
Callback invoked when all headers for a part have been parsed.
Determines if the part is a file based on 'content-disposition' and
prepares the _current_file (SpooledTemporaryFile) or _data_buffer
accordingly.
on_part_data(self, data: bytes, start: int, end: int)
Callback invoked when a chunk of data for a part is parsed.
Appends the data chunk to the appropriate buffer (_current_file for
files, _data_buffer for text fields) and enforces size limits.
Parameters
data | bytes | The raw bytes of the part data. |
start | int | The starting index of the current chunk in |
end | int | The ending index (exclusive) of the current chunk in |
Raises
HTTPPayloadTooLarge | If the part's size exceeds |
on_part_end(self)
Callback invoked when parsing of a multipart part has finished.
If the part was a file, its temporary file handle is reset to the beginning.
If it was a text field, its decoded value is stored in form_data.
get_callbacks(self) -> Dict[str, Callable]
Returns a dictionary of callback functions for the multipart parser.
These callbacks are used by the python-multipart library to process
different events during the parsing of a multipart request.
Returns
Dict[str, Callable] | A dictionary where keys are event names and values are the corresponding callback methods of this collector. |
def parse_multipart_asgi(request: Request, max_ram_kb = 2048, max_disk_kb = 51200)
Parses a multipart/form-data ASGI request.
This asynchronous function processes the incoming request stream, collecting form fields and uploaded files, while enforcing configured size limits.
Parameters
request | Request | The incoming ASGI request object. |
max_ram_kb | int | The maximum size (in KB) a file part can occupy in RAM before being written to a temporary disk file. Defaults to 2048 KB. |
max_disk_kb | int | The maximum allowed size (in KB) for any single file uploaded.
Exceeding this raises |
Returns
tuple[dict, dict] | A tuple containing two dictionaries:
|
Raises
HTTPBadRequest | If the 'Content-Type' header is missing a boundary. | |
HTTPPayloadTooLarge | If any form field or file exceeds its configured size limit. |