Skip to content

ietfparse.headers

ietfparse.headers.parse_accept(header_value, *, strict=False)

Parse an HTTP Accept header.

"Accept" is a class of headers that contain a list of values and an associated preference value. The ever present Accept header is a perfect example. It is a list of content types and an optional parameter named q that indicates the relative weight of a particular type. The most basic example is:

Accept: audio/*;q=0.2, audio/basic

Which states that I prefer the audio/basic content type but will accept other audio subtypes with an 80% mark down.

Warning

This function will raise a ValueError when in encounters an invalid value such as * which happens much more frequently than you might expect.

Parameters:

Name Type Description Default
header_value str

the header value to parse

required
strict bool

if truthy, then invalid content type values within header_value will raise ValueError; otherwise, they are ignored

False

Returns:

Type Description
list[ContentType]

a list of ietfparse.datastructures.ContentType instances in decreasing quality order. Each instance is augmented with the associated quality as a float property named quality.

Raises:

Type Description
ValueError

if strict is truthy and at least one value in header_value could not be parsed by ietfparse.headers.parse_content_type

ietfparse.headers.parse_accept_charset(header_value)

Parse an Accept-Charset header into a sorted list.

The Accept-Charset header is a list of character set names with optional quality values. The quality value indicates the strength of the preference where 1.0 is a strong preference and less than 0.001 is outright rejection by the client.

Note

Character sets are rejected if their quality value is less than 0.001. If a wildcard is included in the header, then it will appear BEFORE any rejected values.

Parameters:

Name Type Description Default
header_value str

header value to parse

required

Returns:

Type Description
list[str]

list of character sets sorted from highest to lowest priority

ietfparse.headers.parse_accept_encoding(header_value)

Parse an Accept-Encoding header into a sorted list.

The Accept-Encoding header is a list of encodings with optional quality values. The quality value indicates the strength of the preference where 1.0 is a strong preference and less than 0.001 is outright rejection by the client.

Note

Encodings are rejected if their quality value is less than 0.001. If a wildcard is included in the header, then it will appear BEFORE any rejected values.

Parameters:

Name Type Description Default
header_value str

header value to parse

required

Returns:

Type Description
list[str]

list of encodings sorted from highest to lowest priority

ietfparse.headers.parse_accept_language(header_value)

Parse an Accept-Language header into a sorted list.

The Accept-Language header is a list of languages with optional quality values. The quality value indicates the strength of the preference where 1.0 is a strong preference and less than 0.001 is outright rejection by the client.

Note

Languages are rejected if their quality value is less than 0.001. If a wildcard is included in the header, then it will appear BEFORE any rejected values.

Parameters:

Name Type Description Default
header_value str

header value to parse

required

Returns:

Type Description
list[str]

list of languages sorted from highest to lowest priority

ietfparse.headers.parse_cache_control(header_value)

Parse a Cache-Control header, returning a dict of key-value pairs.

Any of the Cache-Control parameters that do not have directives, such as public or no-cache will be returned with a value of True if they are set in the header.

Parameters:

Name Type Description Default
header_value str

the header value to parse

required

Returns:

Type Description
dict[str, str | int | bool | None]

the parsed Cache-Control directives

ietfparse.headers.parse_content_type(content_type, *, normalize_parameter_values=True)

Parse a content type like header.

The Content-Type header describes the format and semantics of the enclosed entity. Though they look similar, this header differs from the Accept header which advertises the client's preferred response types.

Parameters:

Name Type Description Default
content_type str

the string to parse as a content type

required
normalize_parameter_values bool

setting this to False will enable strict RFC-2045 compliance in which content parameter values are case preserving.

True

Returns:

Type Description
ContentType

the parsed content type

Raises:

Type Description
ietfparse.errors.MalformedContentType

if the content type cannot be parsed (eg, Content-Type: *)

ietfparse.headers.parse_forwarded(header_value, *, only_standard_parameters=False)

Parse an RFC-7239 Forwarded header.

This function parses a Forwarded header into a list of dict instances with each instance containing the parameter values. The list is ordered as received from left to right and the parameter names are folded to lower case strings.

Parameters:

Name Type Description Default
header_value str

value to parse

required
only_standard_parameters bool

if specified and truthy, then a non-standard parameter name will result in a ietfparse.errors.StrictHeaderParsingFailure

False

Returns:

Type Description
list[dict[str, str]]

an ordered list of dict instances

Raises:

Type Description
ietfparse.errors.StrictHeaderParsingFailure

if only_standard_parameters is enabled and a non-standard parameter name is encountered

Parse a HTTP Link header.

Parses the Link header into a sequence of ietfparse.datastructures.LinkHeader instances.

Parameters:

Name Type Description Default
header_value str

the header value to parse

required
strict bool

set this to False to disable semantic checking. Syntactical errors will still raise an exception. Use this if you want to receive all parameters.

True

Returns:

Type Description
list[LinkHeader]

a sequence of ietfparse.datastructures.LinkHeader instances

Raises:

Type Description
ietfparse.errors.MalformedLinkValue

if the specified header_value cannot be parsed

ietfparse.headers.parse_list(value)

Parse a comma-separated list header.

Parameters:

Name Type Description Default
value str

header value to split into elements

required

Returns:

Type Description
list[str]

list of header elements as strings