nhttp_media_type (nhttp_lib v2.2.0)

View Source

Media type parser (RFC 9110 Section 8.3.1) and the Content-Type lookup.

parse/1 reads media-type = type "/" subtype parameters, with the parameters of RFC 9110 Section 5.6.6. A parameter value is a token or a quoted-string (RFC 9110 Section 5.6.4), and the parser removes the backslash of each quoted-pair. An empty parameter, as in text/plain;;charset=x, is valid grammar and adds nothing to the result.

The type, the subtype and the parameter names are case-insensitive (RFC 9110 Section 8.3.1), so the result holds them in lowercase. Parameter values keep their case. A repeated parameter name keeps every value, in the order of the field.

The parser follows the grammar exactly. It accepts no whitespace around / or =, and no whitespace at the start or the end of the value other than the OWS around a ;.

Summary

Types

quoted_pair marks a parameter value whose quoted-string held one.

A media_type/0 with a mark/0 on each parameter.

Functions

Parse the content-type field of Headers.

Parse a media type into its normal form.

Types

content_type_error()

-type content_type_error() :: missing_content_type | invalid_content_type.

mark()

-type mark() :: plain | quoted_pair.

quoted_pair marks a parameter value whose quoted-string held one.

marked_media_type()

-type marked_media_type() ::
          #{type := binary(), subtype := binary(), params := [{binary(), binary(), mark()}]}.

A media_type/0 with a mark/0 on each parameter.

media_type()

-type media_type() :: #{type := binary(), subtype := binary(), params := [{binary(), binary()}]}.

Functions

content_type(Headers)

-spec content_type(nhttp_lib:headers()) -> {ok, media_type()} | {error, content_type_error()}.

Parse the content-type field of Headers.

The lookup is exact on the lowercase name, the form that every codec of this library gives. Content-Type is a singleton field (RFC 9110 Section 8.3), so two or more field lines are invalid_content_type, whatever their values. RFC 10008 Section 2 makes a server fail a QUERY request for which this function returns an error.

parse(Bin)

-spec parse(binary()) -> {ok, media_type()} | {error, invalid_media_type}.

Parse a media type into its normal form.