Skip to main content

parse_cache_control

Function parse_cache_control 

Source
pub fn parse_cache_control(s: &str) -> CacheControl
Expand description

Parse a Cache-Control field value per RFC 9111 §5.2.

ABNF (§5.2):

Cache-Control   = #cache-directive
cache-directive = token [ "=" ( token / quoted-string ) ]

The #-list (RFC 9110 §5.6.1) is split on top-level commas with quoted-string awareness (a comma inside a "…" argument does not end a directive), empty elements are skipped per §5.6.1’s “tolerate empty list elements” recipient posture, and surrounding OWS (§5.6.3) is trimmed from each element. An obs-fold (RFC 7230 §3.2.4) anywhere in the value is normalised to a single SP before splitting.

§5.2: “Cache directives are identified by a token, to be compared case-insensitively” — directive names are lowercased before dispatch. “[Directives] have an optional argument that can use both token and quoted-string syntax. For the directives defined below that define arguments, recipients ought to accept both forms” — so the argument is read via the §5.6.4 quoted-string unwrap when it opens with DQUOTE, and as a bare token otherwise, regardless of which form §5.2 nominally requires the sender to use.

Recognized directives populate the typed CacheControl fields; delta-seconds arguments are validated and §1.2.2-saturated via [parse_delta_seconds] (a non-numeric argument leaves the field None — §4.2.1 stale-on-non-integer). The qualified #field-name forms of no-cache / private (§5.2.2.4 / §5.2.2.7) split their decoded quoted-string argument into lowercased field names. Unknown directive tokens land in CacheControl::extensions (§5.2.3 “ignore unrecognized” — preserved, not dropped). Duplicate valued directives keep the first occurrence (§4.2.1).

This is always a structural parse: a malformed element (a bad token name, OWS around the =, an unterminated quoted-string) is skipped, never a hard error, matching the recipient robustness the rest of the driver applies to §5.6.1 list fields.