Secure.validate_and_normalize_headers()

Validate and normalize the current header items and cache an immutable mapping.

Usage

Source

Secure.validate_and_normalize_headers(
    *, on_invalid="drop", strict=False, allow_obs_text=False, logger=None
)

This operates on header_items() (not headers_list directly) to preserve ordering, multi-valued behavior, and any prior deduplication.

The resulting mapping is stored as a normalized snapshot that is returned by headers until the configured headers change.

Parameters

on_invalid: OnInvalidPolicy = "drop"
Policy for invalid headers:
  • "drop": silently drop invalid entries (default).
  • "warn": log a warning and drop invalid entries.
  • "raise": raise ValueError on invalid entries.
strict: bool = False

If true, treat CR/LF and disallowed characters as hard errors. Other invalid cases (name/value) are governed by on_invalid.

allow_obs_text: bool = False

If true, allow “obs-text” (bytes 0x80-0xFF) as per older RFCs.

logger: logging.Logger | None = None
Optional logging.Logger used when on_invalid="warn" or when dropping headers with on_invalid="drop" but logging is desired.

Returns

Secure
The same instance, for call chaining.

Raises

ValueError
If a header name is invalid (when on_invalid="raise"), if duplicates are found when building the single-valued mapping, or if strict=True and CR/LF or disallowed characters are present.