|
2 | 2 | from fastapi.responses import PlainTextResponse
|
3 | 3 | import requests
|
4 | 4 | from fastapi import Body, FastAPI, Request, Response
|
| 5 | +from starlette.middleware.base import BaseHTTPMiddleware |
5 | 6 |
|
6 | 7 | ALLOWED_METHODS = ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"]
|
7 | 8 |
|
8 | 9 |
|
| 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 | + |
9 | 24 | async def handle(url: str, request: Request, payload: Any | None = Body(None)):
|
10 | 25 | if request.method not in ALLOWED_METHODS:
|
11 | 26 | return PlainTextResponse("Method Not Allowed", status_code=405)
|
12 | 27 |
|
| 28 | + sanitized_url = sanitize_url(url) |
| 29 | + |
13 | 30 | headers = {}
|
14 | 31 | for key, value in request.headers.items():
|
15 | 32 | if key.lower() not in ["host", "content-length"]:
|
16 | 33 | headers[key] = value
|
17 | 34 | try:
|
18 | 35 | proxy_request = requests.request(
|
19 |
| - request.method, url, headers=headers, json=payload) |
| 36 | + request.method, sanitized_url, headers=headers, json=payload) |
20 | 37 | return Response(content=proxy_request.content)
|
21 | 38 | except requests.exceptions.ConnectionError:
|
22 | 39 | return Response(content="Connection Error", status_code=404)
|
|
0 commit comments