Skip to content

Commit 708b7ee

Browse files
Niklas HildebrandtNiklas Hildebrandt
authored andcommitted
fix: strip trailing slashes to prevent 307 redirects for Quarkus clients
Quarkus REST Client does not re-send the Authorization header when following a 307 redirect, causing all /payments/terminals/ requests to fail silently. Add TrailingSlashMiddleware to normalize paths before routing and set redirect_slashes=False on the FastAPI app so no redirects are issued. Made-with: Cursor
1 parent a813b7e commit 708b7ee

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

ocpi/main.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint):
6060
_HEALTH_PATHS = {"/health", "/healthz", "/ready", "/readiness", "/liveness"}
6161

6262

63+
class TrailingSlashMiddleware(BaseHTTPMiddleware):
64+
"""Strip trailing slashes before routing.
65+
66+
Avoids 307 redirects for clients (e.g. Quarkus REST Client) that do not
67+
re-send the Authorization header when following a redirect.
68+
"""
69+
70+
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint):
71+
if request.url.path != "/" and request.url.path.endswith("/"):
72+
scope = dict(request.scope)
73+
scope["path"] = request.url.path.rstrip("/")
74+
request = Request(scope, request.receive, request._send)
75+
return await call_next(request)
76+
77+
6378
class ExceptionHandlerMiddleware(BaseHTTPMiddleware):
6479
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint):
6580
is_health = request.url.path in _HEALTH_PATHS
@@ -176,6 +191,7 @@ def get_application(
176191
docs_url=f"/{settings.OCPI_PREFIX}/docs",
177192
redoc_url=f"/{settings.OCPI_PREFIX}/redoc",
178193
openapi_url=f"/{settings.OCPI_PREFIX}/openapi.json",
194+
redirect_slashes=False,
179195
)
180196

181197
_app.add_middleware(
@@ -187,6 +203,7 @@ def get_application(
187203
)
188204
_app.add_middleware(ExceptionHandlerMiddleware)
189205
_app.add_middleware(HubRequestIdMiddleware)
206+
_app.add_middleware(TrailingSlashMiddleware)
190207

191208
_app.include_router(
192209
versions_router,

0 commit comments

Comments
 (0)