HomeDashboard/.venv/lib/python3.12/site-packages/nicegui/middlewares.py
2026-01-03 14:54:18 +01:00

28 lines
1.2 KiB
Python

from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.requests import Request
from starlette.responses import Response
from . import core
from .version import __version__
class RedirectWithPrefixMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
prefix = request.headers.get('X-Forwarded-Prefix', '')
response = await call_next(request)
if 'Location' in response.headers and response.headers['Location'].startswith('/'):
new_location = prefix + response.headers['Location']
response.headers['Location'] = new_location
return response
class SetCacheControlMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
response = await call_next(request)
if request.url.path.startswith(f'/_nicegui/{__version__}/') \
and not request.url.path.startswith(f'/_nicegui/{__version__}/dynamic_resources/'):
response.headers['Cache-Control'] = core.app.config.cache_control_directives
return response