Home
Relevant source files
The following files were used as context for generating this wiki page:
python-multipart is a streaming multipart parser for Python, released under the Apache-2.0 license. It is designed to handle large file uploads efficiently by processing data as it arrives rather than loading entire payloads into memory. The library supports multiple encoding formats, including multipart/form-data, application/x-www-form-urlencoded, and application/octet-stream.
Sources: README.md:1-13, python_multipart/multipart.py:1-20, pyproject.toml:1-15
Core Architecture
The library follows a callback-based streaming architecture. Instead of returning a full object model of the parsed data, it triggers events as specific components (headers, fields, or file chunks) are identified. This design is centered around several specialized parser classes that inherit from a common base.
Parser Hierarchy
The diagram shows the inheritance structure where specialized parsers extend the notification and callback logic of the BaseParser.
Sources: python_multipart/multipart.py:644-688, python_multipart/multipart.py:726, python_multipart/multipart.py:771, python_multipart/multipart.py:930
Parser Types and Responsibilities
| Parser | Target Content-Type | Key Responsibility |
|---|---|---|
OctetStreamParser | application/octet-stream | Directly pipes raw binary data to file handlers. |
QuerystringParser | application/x-www-form-urlencoded | Parses URL-encoded key-value pairs from the request body. |
MultipartParser | multipart/form-data | Complex state machine for identifying boundaries and sub-parts. |
Sources: python_multipart/multipart.py:726-768, python_multipart/multipart.py:771-927, python_multipart/multipart.py:930-1300
The FormParser System
The FormParser serves as the high-level orchestrator. It inspects the Content-Type header and instantiates the appropriate underlying parser. It manages the lifecycle of Field and File objects, providing a unified interface for the application developer.
Data Flow Execution
This flowchart illustrates how FormParser routes raw data through specialized parsers to produce final Field or File objects.
Sources: python_multipart/multipart.py:1342-1550
Configuration Options
The FormParser and its associated File handler are configured via a TypedDict or dictionary.
| Option | Type | Default | Description |
|---|---|---|---|
MAX_BODY_SIZE | float | inf | Maximum total bytes to parse. |
MAX_MEMORY_FILE_SIZE | int | 1 MiB | Threshold before flushing in-memory data to disk. |
UPLOAD_DIR | str/bytes | None | Target directory for temporary files. |
UPLOAD_KEEP_FILENAME | bool | False | Whether to preserve the client-provided filename. |
MAX_HEADER_COUNT | int | 8 | Max headers allowed per multipart part. |
Sources: python_multipart/multipart.py:348-356, python_multipart/multipart.py:1330-1340
State Management
The MultipartParser utilizes a strict state machine to handle the complex structure of multipart messages. It tracks boundaries, headers, and payload data through the MultipartState enumeration.
Multipart States
START/START_BOUNDARY: Locating the initial boundary.HEADER_FIELD/HEADER_VALUE: Parsing sub-part metadata.PART_DATA: Streaming the actual content of a part.END_BOUNDARY/END: Identifying the final closing boundary.
Sources: python_multipart/multipart.py:98-112, python_multipart/multipart.py:1086-1280
Boundary Detection Logic
The parser implements an efficient boundary search, capped at a maximum length of 256 bytes. It uses bytes.find for partial-boundary tail scans to optimize performance when processing large chunks.
Sources: python_multipart/multipart.py:126-131, CHANGELOG.md:5-7
Data Persistence (File vs Field)
Data is stored in one of two ways based on the part's metadata:
- Field: Used for standard form inputs. Data is accumulated in a list of bytes and joined into a single string upon finalization.
Sources: python_multipart/multipart.py:186-210 - File: Used for file uploads. It initially stores data in an
io.BytesIOobject. If the data size exceedsMAX_MEMORY_FILE_SIZE, it automatically callsflush_to_disk(), switching to a temporary file managed bytempfile.NamedTemporaryFile.
Sources: python_multipart/multipart.py:361-450
Conclusion
python-multipart provides a robust, low-level streaming interface for handling various web form encodings. Its strength lies in its memory efficiency and the flexibility of its callback system, making it suitable for high-performance Python web frameworks.
Sources: README.md:1-13, python_multipart/multipart.py:1-40