QuerystringParser
Relevant source files
The following files were used as context for generating this wiki page:
The QuerystringParser is a streaming parser designed to consume data formatted as a URL query string (typically application/x-www-form-urlencoded) and trigger callbacks as it identifies field names and values. Unlike standard parsers that require the entire string to be present in memory, this parser processes data in chunks, making it suitable for handling large request bodies efficiently.
Within the python-multipart project, it serves as a core parsing engine that can be used independently or as a component of the higher-level FormParser. It handles common URL-encoded separators like ampersands (&) and semicolons (;), and supports both strict and non-strict parsing modes.
Sources: python_multipart/multipart.py:651-665, python_multipart/init.py:21-29
Core Architecture and State Machine
The parser operates as a state machine, transitioning between states based on the characters encountered in the input stream. It inherits from BaseParser, which provides the underlying callback management system.
Parser States
The parser uses the QuerystringState enumeration to track its current position within the data stream:
| State | Value | Description |
|---|---|---|
BEFORE_FIELD | 0 | Initial state or state after a separator, waiting for a new field to start. |
FIELD_NAME | 1 | Currently parsing the name portion of a key-value pair. |
FIELD_DATA | 2 | Currently parsing the value portion of a key-value pair (after an = sign). |
Sources: python_multipart/multipart.py:101-109, python_multipart/multipart.py:666
Data Flow Diagram
The following diagram illustrates how the QuerystringParser transitions between states during the execution of the write() method.
Implementation Details
Callback System
QuerystringParser utilizes specific callbacks defined in the QuerystringCallbacks TypedDict. These allow the calling application to react to specific parsing events without needing the full results to be buffered.
on_field_start: Triggered when a new field is first detected.on_field_name: Triggered when a chunk of a field's name is parsed.on_field_data: Triggered when a chunk of a field's value is parsed.on_field_end: Triggered when a field (name and value) is completely processed.on_end: Triggered when the entire stream is finished.
Sources: python_multipart/multipart.py:32-38, python_multipart/multipart.py:655-661
Write and Internal Logic
The primary interface is the write(data) method. It performs size verification against max_size before delegating to _internal_write.
def write(self, data: bytes) -> int:
data_len = len(data)
if (self._current_size + data_len) > self.max_size:
new_size = int(self.max_size - self._current_size)
data_len = new_size
l = 0
try:
l = self._internal_write(data, data_len)
finally:
self._current_size += l
return l
Sources: python_multipart/multipart.py:690-711, CHANGELOG.md:46
Strict Parsing Mode
When strict_parsing is enabled, the parser enforces that every field must contain an equals sign (=). If a field name is followed immediately by a separator (e.g., name&) or the end of the stream without an equals sign, the parser raises a QuerystringParseError. In non-strict mode, such fields are treated as having a None or empty value.
Sources: python_multipart/multipart.py:663-665, python_multipart/multipart.py:768-800, tests/test_multipart.py:382-414
Configuration and Limits
The parser accepts several parameters during initialization to control its behavior and security boundaries.
| Parameter | Type | Default | Description |
|---|---|---|---|
callbacks | QuerystringCallbacks | {} | Dictionary of functions to execute on parsing events. |
strict_parsing | bool | False | If True, requires = for every field. |
max_size | float | inf | The maximum total bytes allowed for the body. |
Sources: python_multipart/multipart.py:667-672, tests/test_benchmarks.py:46-51
Integration with FormParser
The FormParser class uses QuerystringParser as its engine when the Content-Type is set to application/x-www-form-urlencoded or application/x-url-encoded. It translates the parser's low-level data callbacks into high-level Field objects.
Conclusion
QuerystringParser provides a robust, state-based mechanism for streaming URL-encoded data. By utilizing a callback-driven architecture, it minimizes memory overhead and allows the python-multipart library to handle web form submissions of arbitrary size with consistent performance.
Sources: python_multipart/multipart.py:651-665, tests/test_benchmarks.py:90-91