Skip to main content

ccdirectives

Parse, validate and build the HTTP Cache-Control header (RFC 9111) and targeted fields such as CDN-Cache-Control (RFC 9213), and decide freshness, typed and zero-dependency.

Install

pip install ccdirectives

Usage

Read what a response allows:

from ccdirectives import parse

cc = parse('max-age=600, no-cache="Set-Cookie", x-vendor=custom, MAX-AGE=5')
print(cc.max_age, cc.no_cache, cc.no_cache_fields)
# 600 True ('set-cookie',)
print(cc["x-vendor"], cc.extensions(), sorted(cc.duplicates))
# custom ('x-vendor',) ['max-age']

Names compare case-insensitively (ASCII letters only), delta-seconds become ints, and the first occurrence of a repeated directive wins, as RFC 9111 section 4.2.1 says; repeated names are listed in duplicates. delta-seconds are ASCII digits only, and a value too large to represent is read as 2147483648, the "forever" of section 1.2.2:

from ccdirectives import parse

print(parse("max-age=99999999999").max_age, parse("max-age=-5").max_age)
# 2147483648 None
print(parse("max-stale").max_stale, parse(["max-age=60", "public"]).public)
# 2147483648 True

The default parser never raises on content: what it cannot read is skipped, and a broken quote cannot hide the directives after it. Strict mode validates instead, with the rules of a request or a response when you pass context. It holds a header to what a recipient must accept, so max-age="60" passes although a sender should not write it:

from ccdirectives import CacheControlError, parse

print(parse('private="a, x="open, no-store').no_store)
# True
try:
    parse("max-age=60, no-store=1", strict=True, context="response")
except CacheControlError as error:
    print(error)
# 'no-store' takes no argument at offset 12

build writes a value back and refuses what the grammar cannot carry, such as a line break in an argument, a negative age or a name that is not a token. delta-seconds go out as tokens and field lists as quoted strings, so the output always passes strict mode:

from ccdirectives import CacheControl, build

print(build(CacheControl({"max-age": 3600, "private": "Set-Cookie", "immutable": True})))
# max-age=3600, private="Set-Cookie", immutable

Freshness

assess applies RFC 9111 section 4.2 and RFC 5861 to a stored response of a given age, optionally narrowed by the request directives:

from ccdirectives import assess, parse

response = parse("max-age=600, stale-while-revalidate=30, stale-if-error=1200")
verdict = assess(response, age=620)
print(verdict.fresh, verdict.reusable, verdict.stale_while_revalidate, verdict.stale_if_error)
# False False True True
print(assess(response, age=620, request=parse("max-stale=60")).reusable)
# True

freshness_lifetime and current_age expose the two calculations on their own (Expires and Date are passed as seconds since the epoch), and may_store says whether the directives let a private or shared cache store a response at all.

Targeted fields

Targeted fields are structured field dictionaries (RFC 9651) with their own error rules: an empty or invalid field is ignored. select walks a cache's target list and falls back on Cache-Control:

from ccdirectives import assess, select

headers = {"Cache-Control": "max-age=60, s-maxage=120", "CDN-Cache-Control": "max-age=600"}
field, policy = select(headers, ["CDN-Cache-Control"])
print(field, assess(policy, age=0, shared=True).lifetime)
# CDN-Cache-Control 600
broken = {"CDN-Cache-Control": "max-age=1.5", "Cache-Control": "no-store"}
print(select(broken, ["CDN-Cache-Control"]))
# ('CDN-Cache-Control', CacheControl(directives={}, duplicates=frozenset()))

A max-age that is a Decimal breaks the directive's rules, so RFC 9213 says not to use it; the field itself is valid, so it still wins over Cache-Control.

Command line

$ curl -sI https://example.com/ | ccdirectives --shared
$ ccdirectives --strict --age 30 "max-age=60, stale-while-revalidate=30"

It reads a header value, or a response head on standard input (the last response of a redirect chain), and prints the chosen field, its directives and the freshness verdict as JSON. From a head it takes the age from Age and, when Cache-Control applies, the lifetime fallback from Expires and Date. --target sets the target list; it defaults to CDN-Cache-Control with --shared. The exit status is 1 for a value --strict rejects and 2 for a usage error.

API

Name What it does
parse(header, *, strict=False, context=None) Read a field value or a list of field lines into a CacheControl
build(cc) Write a CacheControl as a field value
CacheControl Ordered, case-insensitive mapping with typed accessors and duplicates
DirectiveValue int, str or bool, the type of a directive value
CacheControlError ValueError with reason and offset for strict mode and build
parse_targeted(value) Read a targeted field, or None when RFC 9213 says to ignore it
build_targeted(cc) Write a CacheControl as a structured field dictionary
select(headers, targets) The field name and policy a cache with that target list follows
assess(response, *, age, ...) A Freshness verdict for a stored response
Freshness Lifetime, age, fresh, reusable and the two stale windows
freshness_lifetime(response, ...) Lifetime by the rules of RFC 9111 section 4.2.1
current_age(...) Age by the rules of RFC 9111 section 4.2.3
may_store(response, ...) Whether the directives allow a cache to store the response
DELTA_MAX 2147483648, the value an overflowing delta-seconds becomes
REGISTERED_DIRECTIVES The names in the IANA HTTP Cache Directive Registry
REQUEST_DIRECTIVES, RESPONSE_DIRECTIVES The registered names defined for each direction

Why

Every HTTP client, server and cache has to read Cache-Control, yet the parsers that exist are framework-bound: werkzeug.http.parse_cache_control_header ties you to Werkzeug and webob.cachecontrol to WebOb, while packages such as CacheControl and hishel are full cache transports, not directive codecs. None of them reads CDN-Cache-Control as the structured field it is. This library is the codec and the freshness arithmetic: a typed structure in, a correct header out, and nothing else to install.

License

MIT. See LICENSE.

Metadata

Release files for ccdirectives 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ccdirectives 0.2.0
File Size Uploaded Provenance Actions
ccdirectives-0.2.0.tar.gz 41.3 kB Without provenance Details for ccdirectives-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ccdirectives 0.2.0
File Interpreter ABI Platform Actions
ccdirectives-0.2.0-py3-none-any.whl Python 3 none any Details for ccdirectives-0.2.0-py3-none-any.whl

Total release size: 67.5 kB

Release files / ccdirectives-0.2.0.tar.gz

Download URL ccdirectives-0.2.0.tar.gz
Size 41.3 kB
Tags Source
SHA-256 checksum
How to use checksums
7a0a0d23ba9aecfa047039cbd3d91ac4c1b6efbb86a52571a1e9636f28bdfd2f
BLAKE2b-256 checksum
How to use checksums
12d6a62c54e32e62c613a0fba4c0eb926648651de94952fb831bb6beadf06283
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / ccdirectives-0.2.0-py3-none-any.whl

Download URL ccdirectives-0.2.0-py3-none-any.whl
Size 26.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f89e3cda3820087768a5c73bcf275953fda958c912549774ceeb4e7dcac66d34
BLAKE2b-256 checksum
How to use checksums
74ab24b7b37168422b8e9ae74bcbb5b16f0923e2211af2fd60d7bdce6b94b4cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Security for ccdirectives 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page