duck.http.fileuploads.multipartΒΆ

BytesMultiPartParser Module

This module provides the BytesMultiPartParser class, which is designed to parse multipart form-data, particularly from raw bytes input. It is useful for handling file uploads or form submissions where the data is received in a multipart format.

Classes:

  • BytesMultiPartParser: A parser for multipart form-data.

Exceptions:

  • MultiPartParserError: Raised when parsing errors occur.

Usage Example:

headers = {
    'Content-Type': b'multipart/form-data; boundary=---------------------------354901075210407969363875912417'
}
input_data = b'''-----------------------------354901075210407969363875912417
Content-Disposition: form-data; name="field1"

value1
-----------------------------354901075210407969363875912417
Content-Disposition: form-data; name="file1"; filename="example.txt"
Content-Type: text/plain

Hello, world!
-----------------------------354901075210407969363875912417--'''.replace(b'
', b'
')

parser = BytesMultiPartParser(headers, input_data)
for headers, content in parser.get_parts():
    print("Headers:", headers)
    print("Content:", content)

Module ContentsΒΆ

ClassesΒΆ

BytesMultiPartParser

A parser for multipart form-data, especially suited for parsing bytes input.

APIΒΆ

class duck.http.fileuploads.multipart.BytesMultiPartParser(headers, input_data)ΒΆ

A parser for multipart form-data, especially suited for parsing bytes input.

Variables:
  • headers – The headers of the multipart data.

  • input_data – The raw bytes input data to parse.

  • boundary – The boundary string extracted from headers.

  • parts – Parsed parts of the multipart data.

Initialization

Initialize the parser with headers and input data.

Parameters:
  • headers – The headers of the multipart data.

  • input_data – The raw bytes input data.

_parse_boundary()ΒΆ

Extract the boundary string from the headers.

Returns:

The boundary string.

Return type:

str

Raises:

MultiPartParserError – If no boundary isdecode(β€˜utf-8’)

_parse_headers(raw_headers)ΒΆ

Parse raw headers into a dictionary.

Parameters:

raw_headers – The raw headers in bytes.

Returns:

Parsed headers as a dictionary.

Return type:

dict

Raises:

MultiPartParserError – If an invalid header line is encountered.

_parse_parts()ΒΆ

Parse the input data into its constituent parts.

Returns:

A list of tuples, each containing headers and content of a part.

Return type:

list

Raises:

MultiPartParserError – If an invalid part is encountered during parsing.

get_parts()ΒΆ

Get the parsed parts of the multipart data.

Returns:

A list of tuples, each containing headers and content of a part.

Return type:

list