nhttp_headers (nhttp_lib v1.0.0)
View SourceProtocol-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/1on the input name before matching. - Multi-valued headers retain insertion order.
get/2,3returns the first match;delete/2removes every occurrence;append/3appends 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
-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.
-spec delete(binary(), nhttp_lib:headers()) -> nhttp_lib:headers().
Remove every header whose name matches Name. Case-insensitive.
-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.
-spec get(binary(), nhttp_lib:headers()) -> binary() | undefined.
Get the first value for Name, or undefined if absent. Case-insensitive.
-spec get(binary(), nhttp_lib:headers(), Default) -> binary() | Default.
Get the first value for Name, or Default if absent. Case-insensitive.
-spec has(binary(), nhttp_lib:headers()) -> boolean().
True iff a header with the given name exists. Case-insensitive.
-spec set(binary(), binary(), nhttp_lib:headers()) -> nhttp_lib: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.
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.