From 7a876ecb66ec108d1cd3265a2fc6cadab0dc09ce Mon Sep 17 00:00:00 2001 From: sharkinsspatial Date: Thu, 3 Mar 2022 11:59:31 -0600 Subject: [PATCH 1/2] Make http error cache control behavior configurable. --- src/titiler/core/tests/test_cache_middleware.py | 14 ++++++++++++++ src/titiler/core/titiler/core/middleware.py | 7 ++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/titiler/core/tests/test_cache_middleware.py b/src/titiler/core/tests/test_cache_middleware.py index b90e2698e..c3270e4e6 100644 --- a/src/titiler/core/tests/test_cache_middleware.py +++ b/src/titiler/core/tests/test_cache_middleware.py @@ -5,6 +5,7 @@ from fastapi import FastAPI, Path +from starlette.responses import Response from starlette.testclient import TestClient @@ -36,9 +37,19 @@ async def tiles( """tiles.""" return "yeah" + @app.get("/emptytiles/{z}/{x}/{y}") + async def emptytiles( + z: int = Path(..., ge=0, le=30, description="Mercator tiles's zoom level"), + x: int = Path(..., description="Mercator tiles's column"), + y: int = Path(..., description="Mercator tiles's row"), + ): + """tiles.""" + return Response(status_code=404) + app.add_middleware( CacheControlMiddleware, cachecontrol="public", + cachecontrol_http_code_range=400, exclude_path={r"/route1", r"/route2", r"/tiles/[0-1]/.+"}, ) @@ -58,3 +69,6 @@ async def tiles( response = client.get("/tiles/3/1/1") assert response.headers["Cache-Control"] == "public" + + response = client.get("/emptytiles/3/1/1") + assert not response.headers.get("Cache-Control") diff --git a/src/titiler/core/titiler/core/middleware.py b/src/titiler/core/titiler/core/middleware.py index 8496f73a3..6dced13cf 100644 --- a/src/titiler/core/titiler/core/middleware.py +++ b/src/titiler/core/titiler/core/middleware.py @@ -19,6 +19,7 @@ def __init__( self, app: ASGIApp, cachecontrol: Optional[str] = None, + cachecontrol_http_code_range: Optional[int] = 500, exclude_path: Optional[Set[str]] = None, ) -> None: """Init Middleware. @@ -31,6 +32,7 @@ def __init__( """ super().__init__(app) self.cachecontrol = cachecontrol + self.cachecontrol_http_code_range = cachecontrol_http_code_range self.exclude_path = exclude_path or set() async def dispatch(self, request: Request, call_next): @@ -41,7 +43,10 @@ async def dispatch(self, request: Request, call_next): if re.match(path, request.url.path): return response - if request.method in ["HEAD", "GET"] and response.status_code < 500: + if ( + request.method in ["HEAD", "GET"] + and response.status_code < self.cachecontrol_http_code_range + ): response.headers["Cache-Control"] = self.cachecontrol return response From e118471b79a7834ab66d68d234bad6e9db8c70b2 Mon Sep 17 00:00:00 2001 From: sharkinsspatial Date: Thu, 3 Mar 2022 12:50:39 -0600 Subject: [PATCH 2/2] Update parameter naming as requested by @vincentsarago. --- src/titiler/core/tests/test_cache_middleware.py | 2 +- src/titiler/core/titiler/core/middleware.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/titiler/core/tests/test_cache_middleware.py b/src/titiler/core/tests/test_cache_middleware.py index c3270e4e6..fc829b4dd 100644 --- a/src/titiler/core/tests/test_cache_middleware.py +++ b/src/titiler/core/tests/test_cache_middleware.py @@ -49,7 +49,7 @@ async def emptytiles( app.add_middleware( CacheControlMiddleware, cachecontrol="public", - cachecontrol_http_code_range=400, + cachecontrol_max_http_code=400, exclude_path={r"/route1", r"/route2", r"/tiles/[0-1]/.+"}, ) diff --git a/src/titiler/core/titiler/core/middleware.py b/src/titiler/core/titiler/core/middleware.py index 6dced13cf..be655304e 100644 --- a/src/titiler/core/titiler/core/middleware.py +++ b/src/titiler/core/titiler/core/middleware.py @@ -19,7 +19,7 @@ def __init__( self, app: ASGIApp, cachecontrol: Optional[str] = None, - cachecontrol_http_code_range: Optional[int] = 500, + cachecontrol_max_http_code: Optional[int] = 500, exclude_path: Optional[Set[str]] = None, ) -> None: """Init Middleware. @@ -32,7 +32,7 @@ def __init__( """ super().__init__(app) self.cachecontrol = cachecontrol - self.cachecontrol_http_code_range = cachecontrol_http_code_range + self.cachecontrol_max_http_code = cachecontrol_max_http_code self.exclude_path = exclude_path or set() async def dispatch(self, request: Request, call_next): @@ -45,7 +45,7 @@ async def dispatch(self, request: Request, call_next): if ( request.method in ["HEAD", "GET"] - and response.status_code < self.cachecontrol_http_code_range + and response.status_code < self.cachecontrol_max_http_code ): response.headers["Cache-Control"] = self.cachecontrol