nhttp_media_type (nhttp_lib v2.1.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
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.