Coverage for app/cache_control.py: 100%
19 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-10-31 11:18 +0000
« prev ^ index » next coverage.py v7.14.1, created at 2026-10-31 11:18 +0000
1"""
2Default ``Cache-Control`` for every svc-bff response.
4Everything svc-bff answers is one user's data, served behind their session
5cookie. A shared cache in front of the origin (Cloudflare) keys on the URL
6alone, and by default caches any URL whose path ends in a static-looking
7extension — ``.pdf`` among them, which is what svc-core's document downloads
8end in. A cached copy is then served to the next request for that URL without
9reaching nginx's ``auth_request`` or svc-core's ownership check, and outlives
10the document's deletion.
12So a response that states no caching policy is marked ``private, no-store``.
13A route that states one keeps it: a deliberate ``private, max-age=…`` is
14still private to the browser, and a route that wants an edge cache must now
15say so explicitly.
16"""
18from __future__ import annotations
20from starlette.datastructures import MutableHeaders
21from starlette.types import ASGIApp, Message, Receive, Scope, Send
23DEFAULT_CACHE_CONTROL = "private, no-store"
26class DefaultCacheControlMiddleware:
27 """Pure ASGI middleware: add ``Cache-Control`` to HTTP responses lacking one.
29 WebSocket and lifespan scopes pass through untouched.
30 """
32 def __init__(self, app: ASGIApp, value: str = DEFAULT_CACHE_CONTROL) -> None:
33 self.app = app
34 self.value = value
36 async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
37 if scope["type"] != "http":
38 await self.app(scope, receive, send)
39 return
41 async def send_with_default(message: Message) -> None:
42 if message["type"] == "http.response.start":
43 headers = MutableHeaders(scope=message)
44 if "cache-control" not in headers:
45 headers["Cache-Control"] = self.value
46 await send(message)
48 await self.app(scope, receive, send_with_default)