nhttp_accept_query (nhttp_lib v2.2.0)

View Source

The Accept-Query field (RFC 10008 Section 3).

The field is a List Structured Field (RFC 9651 Section 3.1). Each member is a Token or a String that holds a media range without parameters, and each parameter value is a Token or a String. A Token and a String with the same characters give the same term, because RFC 10008 Section 3 forbids a difference in processing.

parse_value/1 reads the List grammar of RFC 9651 Section 4.2.1 in full. It accepts only the item types that RFC 10008 Section 3 allows, then checks that each member is type "/" subtype, each side an RFC 9110 token. The wildcards are */* and type/* only. Every failure ignores the whole field (RFC 9651 Section 2.2), and the error names the first rule that the value breaks. An offset in an error counts octets from the start of the joined field value.

The result holds the type, the subtype and the parameter names in lowercase, the form of nhttp_media_type:media_type/0. Every binary in the result and in a not_media_range error is a fresh copy, so a caller can keep a value for as long as it stays fresh without the receive buffer.

allows/2 matches a media type against the ranges. RFC 10008 does not define a rule for parameters, so allows/2 follows the media ranges of Accept (RFC 9110 Section 12.5.1): each parameter of a range must be present in the media type with an equal value. A charset value compares without case (RFC 9110 Section 8.3.2), every other value with case.

Summary

Functions

True iff a range of Ranges matches MediaType. */* matches any type, type/* any subtype of type, and any other range its own type and subtype. Names compare without case.

Serialize a non-empty list of media ranges (RFC 9651 Section 4.1.1).

Parse every accept-query field line of Headers.

Parse one Accept-Query field value. An empty value gives {ok, []}.

Types

encode_error()

-type encode_error() ::
          {not_media_range, binary()} |
          {invalid_parameter_name, binary()} |
          {invalid_parameter_value, binary()}.

media_range()

-type media_range() :: nhttp_media_type:media_type().

parse_error()

-type parse_error() ::
          not_ascii |
          {sf_syntax, non_neg_integer()} |
          {unsupported_item, non_neg_integer()} |
          {unsupported_parameter, non_neg_integer()} |
          {not_media_range, binary()}.

Functions

allows/2

-spec allows(nhttp_media_type:media_type(), [media_range()]) -> boolean().

True iff a range of Ranges matches MediaType. */* matches any type, type/* any subtype of type, and any other range its own type and subtype. Names compare without case.

encode/1

-spec encode([media_range(), ...]) -> {ok, binary()} | {error, encode_error()}.

Serialize a non-empty list of media ranges (RFC 9651 Section 4.1.1).

A member or a parameter value goes out as a Token when it is a valid Token, and as a String otherwise. A media type with a leading digit thus goes out as a String, as RFC 10008 Section 3 requires. A parameter name goes out in lowercase. An empty List is the absence of the field (RFC 9651 Section 4.1), so the caller omits the field instead of calling this function.

parse(Headers)

-spec parse(nhttp_lib:headers()) -> {ok, [media_range()]} | {error, parse_error()}.

Parse every accept-query field line of Headers.

The field lines join with ", " before the parse (RFC 9651 Section 4.2). The lookup is exact on the lowercase name, the form that every codec of this library gives. An absent field gives {ok, []}.

parse_value(Bin)

-spec parse_value(binary()) -> {ok, [media_range()]} | {error, parse_error()}.

Parse one Accept-Query field value. An empty value gives {ok, []}.