nhttp_headers (nhttp_lib v1.0.0)

View Source

Protocol-agnostic header utilities.

Headers carried by nhttp_lib:headers/0 are an ordered list of {Name, Value} binary pairs. The codec layers normalise names to lowercase at parse time. Application code should construct headers with lowercase names; the lookup functions in this module accept any casing and normalise internally so mixed-case calls are still safe.

Key invariants:

  • Header names compare case-insensitively (RFC 9110 §5.1). All lookup functions in this module call to_lower/1 on the input name before matching.
  • Multi-valued headers retain insertion order. get/2,3 returns the first match; delete/2 removes every occurrence; append/3 appends without removing existing entries.

Summary

Functions

Append {Name, Value} to the end of the headers list, preserving any existing occurrences. Useful for multi-valued headers such as set-cookie. The name is stored lowercase.

Remove every header whose name matches Name. Case-insensitive.

Keep only the headers for which Pred(Name, Value) returns true. Order is preserved.

Get the first value for Name, or undefined if absent. Case-insensitive.

Get the first value for Name, or Default if absent. Case-insensitive.

True iff a header with the given name exists. Case-insensitive.

Replace every occurrence of Name with a single {Name, Value} entry. The name is stored lowercase. The replacement is appended to the end of the headers list when no prior occurrence exists.

Lowercase an ASCII binary using HTTP header semantics. Common header names hit a binary-pattern fast path; everything else falls through to a comprehension. RFC 9110 §5.1: field names are ASCII, so non-ASCII upper-half bytes pass through unchanged.

Functions

append(Name, Value, Headers)

-spec append(binary(), binary(), nhttp_lib:headers()) -> nhttp_lib:headers().

Append {Name, Value} to the end of the headers list, preserving any existing occurrences. Useful for multi-valued headers such as set-cookie. The name is stored lowercase.

delete(Name, Headers)

-spec delete(binary(), nhttp_lib:headers()) -> nhttp_lib:headers().

Remove every header whose name matches Name. Case-insensitive.

filter(Pred, Headers)

-spec filter(fun((binary(), binary()) -> boolean()), nhttp_lib:headers()) -> nhttp_lib:headers().

Keep only the headers for which Pred(Name, Value) returns true. Order is preserved.

get(Name, Headers)

-spec get(binary(), nhttp_lib:headers()) -> binary() | undefined.

Get the first value for Name, or undefined if absent. Case-insensitive.

get(Name, Headers, Default)

-spec get(binary(), nhttp_lib:headers(), Default) -> binary() | Default.

Get the first value for Name, or Default if absent. Case-insensitive.

has(Name, Headers)

-spec has(binary(), nhttp_lib:headers()) -> boolean().

True iff a header with the given name exists. Case-insensitive.

set(Name, Value, Headers)

Replace every occurrence of Name with a single {Name, Value} entry. The name is stored lowercase. The replacement is appended to the end of the headers list when no prior occurrence exists.

to_lower/1

-spec to_lower(binary()) -> binary().

Lowercase an ASCII binary using HTTP header semantics. Common header names hit a binary-pattern fast path; everything else falls through to a comprehension. RFC 9110 §5.1: field names are ASCII, so non-ASCII upper-half bytes pass through unchanged.