You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
14 lines
661 B
14 lines
661 B
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint |
|
from starlette.requests import Request |
|
from starlette.responses import Response |
|
|
|
|
|
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
|
|
|