Persistent cache implementation for httpx and httpcore following RFC 9111 specification
74
A utility that filters HTTP response headers before caching based on Cache-Control directives.
Build a Python module that implements HTTP response header filtering logic for caching systems. The module should respect Cache-Control directives that specify which headers should not be cached, following RFC 9111 specifications for both shared and private caches.
no-cache="Authorization, X-Custom" and extracts the list of header names @testprivate="Set-Cookie, X-User-ID" and extracts the list of header names @testno-cache without quotes) by returning an empty list @test{"Content-Type": "text/html", "Authorization": "Bearer token", "Cache-Control": "no-cache=\"Authorization\""}, returns filtered headers without the Authorization header @testCache-Control: no-cache="Set-Cookie, X-Custom", removes both Set-Cookie and X-Custom headers from the result @testCache-Control: no-cache (no field names), returns all headers unchanged @testCache-Control: private="Set-Cookie", removes Set-Cookie from the filtered headers @testCache-Control: private="Set-Cookie, X-User-Data", removes both specified headers @testCache-Control: private="Set-Cookie", returns all headers including Set-Cookie (private directives are ignored in private caches) @testCache-Control: no-cache="Authorization", private="Set-Cookie" in shared cache mode, removes both Authorization and Set-Cookie @testCache-Control: no-cache="Authorization", private="Set-Cookie" in private cache mode, removes only Authorization (ignores private directive) @test@generates
from typing import Dict, List
def parse_cache_control_field_names(cache_control_value: str, directive: str) -> List[str]:
"""
Parse field names from a Cache-Control directive.
Args:
cache_control_value: The value of the Cache-Control header
directive: The directive to extract field names from ('no-cache' or 'private')
Returns:
List of header field names specified in the directive, or empty list if none specified
"""
pass
def filter_headers_for_cache(
headers: Dict[str, str],
is_shared_cache: bool = False
) -> Dict[str, str]:
"""
Filter response headers based on Cache-Control directives before storing in cache.
Removes headers specified in:
- no-cache directive field names (both shared and private caches)
- private directive field names (only in shared caches)
Args:
headers: Dictionary of HTTP response headers (case-insensitive keys)
is_shared_cache: True for shared caches (proxy, CDN), False for private caches (browser)
Returns:
Filtered dictionary of headers suitable for caching
"""
passProvides HTTP caching functionality and Cache-Control parsing capabilities.
Install with Tessl CLI
npx tessl i tessl/pypi-hisheldocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10