nhttp_media_type (nhttp_lib v2.2.0)
View SourceMedia 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.
Types
-type content_type_error() :: missing_content_type | invalid_content_type.
-type mark() :: plain | quoted_pair.
quoted_pair marks a parameter value whose quoted-string held one.
-type marked_media_type() :: #{type := binary(), subtype := binary(), params := [{binary(), binary(), mark()}]}.
A media_type/0 with a mark/0 on each parameter.
Functions
-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.
-spec parse(binary()) -> {ok, media_type()} | {error, invalid_media_type}.
Parse a media type into its normal form.