--
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import re
|
||||
import httpx
|
||||
import asyncio
|
||||
from websockets.client import connect
|
||||
@@ -6,20 +5,18 @@ from fastapi import Request, Response, WebSocket
|
||||
|
||||
hop_by_hop_headers = ["transfer-encoding", "connection", "keep-alive", "upgrade", "proxy-authenticate", "proxy-authorization", "te", "trailers", "content-encoding", "content-length"]
|
||||
|
||||
def to_raw_headers(str_headers: dict[str, str]) -> list[tuple[bytes, bytes]]:
|
||||
return [(k.encode("latin-1"), v.encode("latin-1")) for k, v in str_headers.items()]
|
||||
|
||||
def make_http_proxy(base_url_http: str, headers: dict = {}, remove_prefix_path: bool = False):
|
||||
async def http_proxy(request: Request, path: str = "") -> Response:
|
||||
url = f"{base_url_http}/{path}" if remove_prefix_path else f"{base_url_http}{request.url.path}"
|
||||
merged_headers = dict(request.headers)
|
||||
merged_headers.pop("accept-encoding", None)
|
||||
merged_headers |= {k.lower(): v for k, v in headers.items()}
|
||||
raw_headers = [(k.encode("latin-1"), v.encode("latin-1")) for k, v in merged_headers.items()]
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.request(
|
||||
method=request.method,
|
||||
url=url,
|
||||
headers=to_raw_headers(merged_headers),
|
||||
headers=raw_headers,
|
||||
content=await request.body(),
|
||||
params=request.query_params
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user