Skip to content

Commit b9f04ec

Browse files
chore: fix url being broken on linux
1 parent 0b845e9 commit b9f04ec

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

Diff for: server.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,38 @@
22
from fastapi.responses import PlainTextResponse
33
import requests
44
from fastapi import Body, FastAPI, Request, Response
5+
from starlette.middleware.base import BaseHTTPMiddleware
56

67
ALLOWED_METHODS = ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"]
78

89

10+
def sanitize_url(url: str) -> str:
11+
if not url.startswith("http"):
12+
return url
13+
14+
if url.startswith("http://") or url.startswith("https://"):
15+
return url
16+
17+
# Weird behaviour of FastAPI on linux to collapse multiple slashes
18+
if url.startswith("http:/") or url.startswith("https:/"):
19+
return url.replace("http:/", "http://").replace("https:/", "https://")
20+
21+
return url
22+
23+
924
async def handle(url: str, request: Request, payload: Any | None = Body(None)):
1025
if request.method not in ALLOWED_METHODS:
1126
return PlainTextResponse("Method Not Allowed", status_code=405)
1227

28+
sanitized_url = sanitize_url(url)
29+
1330
headers = {}
1431
for key, value in request.headers.items():
1532
if key.lower() not in ["host", "content-length"]:
1633
headers[key] = value
1734
try:
1835
proxy_request = requests.request(
19-
request.method, url, headers=headers, json=payload)
36+
request.method, sanitized_url, headers=headers, json=payload)
2037
return Response(content=proxy_request.content)
2138
except requests.exceptions.ConnectionError:
2239
return Response(content="Connection Error", status_code=404)

0 commit comments

Comments
 (0)