Request Processing

Header parsing is only part of what you need to write modern web applications. You need to implement responsive behaviors that factor in the state of the server, the resource in question, and information from the requesting client.

Content Negotiation

RFC 7231#section-3.4 describes how Content Negotiation can be implemented. select_content_type() implements the type selection portion of Proactive Negotiation. It takes a list of requested content types (e.g., from parse_accept()) along with a list of content types that the server is capable of producing and returns the content type that is the best match. The algorithm is loosely described in Section 5.3 of RFC 7231#section-5.3.

>>> from ietfparse import headers
>>> requested = headers.parse_accept(
...   'text/*;q=0.3, text/html;q=0.7, text/html;level=1, '
...   'text/html;level=2;q=0.4, */*;q=0.5')
>>> headers.select_content_type(
...   requested,
...   ['text/html', 'text/html;level=4', 'text/html;level=3'])

A more interesting case is to select the representation to produce based on what a server knows how to produce and what a client has requested.

>>> from ietfparse import algorithms, headers
>>> requested = headers.parse_accept(
...   'application/;version=2, '
...   'application/;q=0.75, '
...   'application/json;q=0.5, text/javascript;q=0.25'
... )
>>> selected = algorithms.select_content_type(requested, [
...   headers.parse_content_type('application/;version=3'),
...   headers.parse_content_type('application/;version=2'),
... ])
>>> str(selected)
'application/; version=2'

The select_content_type() function is an implementation of Proactive Content Negotiation as described in RFC 7231#section-3.4.1.