Ushka

API reference

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

charsetstr

The character set to use for decoding field names and values. Defaults to "utf-8".

max_ram_size_kbint

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_kbint

The maximum allowed size (in KB) for any single file uploaded. Exceeding this raises HTTPPayloadTooLarge. Defaults to 51200 KB.

max_text_field_size_kbint

The maximum allowed size (in KB) for any non-file (text) field. Exceeding this raises HTTPPayloadTooLarge. Defaults to 64 KB.

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

databytes

The raw bytes of the header.

startint

The starting index of the current chunk in data.

endint

The ending index (exclusive) of the current chunk in data.

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

databytes

The raw bytes of the header.

startint

The starting index of the current chunk in data.

endint

The ending index (exclusive) of the current chunk in data.

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

databytes

The raw bytes of the part data.

startint

The starting index of the current chunk in data.

endint

The ending index (exclusive) of the current chunk in data.

Raises

HTTPPayloadTooLarge

If the part's size exceeds max_file_size (for files) or max_text_size (for text fields).

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

requestRequest

The incoming ASGI request object.

max_ram_kbint

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_kbint

The maximum allowed size (in KB) for any single file uploaded. Exceeding this raises HTTPPayloadTooLarge. Defaults to 51200 KB.

Returns

tuple[dict, dict]

A tuple containing two dictionaries:

  • form_data: Key-value pairs of standard form fields.
  • files: Key-value pairs of uploaded files, where values are dictionaries containing 'filename', 'content_type', and a SpooledTemporaryFile object.

Raises

HTTPBadRequest

If the 'Content-Type' header is missing a boundary.

HTTPPayloadTooLarge

If any form field or file exceeds its configured size limit.