httk.serve.http.accept

Parse and select against RFC 9110 Accept media ranges.

This module consolidates the RFC 9110 Accept-header parsing shared by the lightweight JSON applications and the DSP catalogue policy. Parsing is common; selection deliberately is not, so two separately named selectors are exposed: one keeps parameterised ranges and matches their parameters, the other discards any range that carries a parameter.

Classes

MediaRange

One valid media range parsed from an HTTP Accept header.

Functions

split_http_list(value, delimiter)

Split an HTTP field outside quoted strings, rejecting broken quoting.

http_parameter_value(value)

Decode one token or quoted-string parameter value.

parse_media_type(value)

Parse a JSON media type into its major, minor, and parameters.

parse_accept(header)

Parse an Accept header into its valid media ranges.

best_quality_matching_parameters(ranges, major, minor, ...)

Select the best range that keeps and matches its parameters.

best_quality_ignoring_parameterised(ranges, major, minor)

Select the best range, discarding any range carrying a parameter.

Module Contents

class httk.serve.http.accept.MediaRange[source]

One valid media range parsed from an HTTP Accept header.

Parameters:
  • major – Lowercase major type, or "*" for a wildcard.

  • minor – Lowercase minor type, or "*" for a wildcard.

  • parameters – Non-q parameters as immutable lowercase name-value pairs.

  • quality – The q weight, defaulting to 1.0 when absent.

major: str[source]
minor: str[source]
parameters: tuple[tuple[str, str], Ellipsis][source]
quality: float[source]
httk.serve.http.accept.split_http_list(value, delimiter)[source]

Split an HTTP field outside quoted strings, rejecting broken quoting.

Parameters:
  • value (str) – The raw HTTP field value to split.

  • delimiter (str) – The single-character separator to split on outside quotes.

Returns:

The delimited parts, or None when a quoted string is unterminated.

Return type:

tuple[str, Ellipsis] | None

httk.serve.http.accept.http_parameter_value(value)[source]

Decode one token or quoted-string parameter value.

Parameters:

value (str) – The raw parameter value, possibly a quoted string.

Returns:

The decoded value, or None when the value is malformed.

Return type:

str | None

httk.serve.http.accept.parse_media_type(value)[source]

Parse a JSON media type into its major, minor, and parameters.

Parameters:

value (object) – The declared media type; only application/json and application/*+json types with unique parameters are accepted.

Returns:

The lowercase major type, lowercase minor type, and parameter mapping.

Return type:

tuple[str, str, dict[str, str]]

httk.serve.http.accept.parse_accept(header)[source]

Parse an Accept header into its valid media ranges.

Parameters:

header (str) – The raw Accept header value.

Returns:

The valid parsed media ranges, or () when the header cannot be split.

Return type:

tuple[MediaRange, Ellipsis]

httk.serve.http.accept.best_quality_matching_parameters(ranges, major, minor, parameters)[source]

Select the best range that keeps and matches its parameters.

A parameterised range is kept only when every one of its parameters is present and equal in parameters. The specificity tiebreak favours a more specific major/minor match and then a range carrying more parameters.

Parameters:
  • ranges (tuple[MediaRange, Ellipsis]) – The parsed media ranges to select among.

  • major (str) – The response major type to match.

  • minor (str) – The response minor type to match.

  • parameters (collections.abc.Mapping[str, str]) – The response media-type parameters to match against.

Returns:

The winning range’s quality, or None when nothing matches.

Return type:

float | None

httk.serve.http.accept.best_quality_ignoring_parameterised(ranges, major, minor)[source]

Select the best range, discarding any range carrying a parameter.

Any range that carries a parameter is discarded outright. The specificity tiebreak favours a more specific major/minor match, with a constant second component.

Parameters:
  • ranges (tuple[MediaRange, Ellipsis]) – The parsed media ranges to select among.

  • major (str) – The response major type to match.

  • minor (str) – The response minor type to match.

Returns:

The winning range’s quality, or None when nothing matches.

Return type:

float | None