File size: 2,637 Bytes
4aac746
 
534c2fc
 
d471223
534c2fc
4aac746
 
 
d471223
4aac746
 
 
d471223
 
 
 
 
 
4aac746
d471223
4aac746
 
 
 
534c2fc
4aac746
 
 
 
 
 
 
534c2fc
 
 
4aac746
534c2fc
4aac746
534c2fc
4aac746
 
 
 
 
 
 
 
 
 
 
 
 
e1b229d
d471223
4aac746
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import Response
import aiohttp
from urllib.parse import urlparse
from aiohttp_client_cache import CachedSession,CacheBackend

app = FastAPI()

# Initialize the aiohttp ClientSession
#http_client = aiohttp.CachedSession()

@app.on_event("startup")
async def startup_event():
    cache = CacheBackend(
        allowed_methods=('GET', 'POST'),
        allowed_codes=(200,503,502),
        include_headers=True,
        expire_after = 50
    )
    global http_client
    http_client = CachedSession(cache=cache)

@app.on_event("shutdown")
async def shutdown_event():
    await http_client.close()

@app.api_route("/{url:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH", "TRACE"])
async def proxy(request: Request, url: str):
    # The base_url includes scheme (http/https), so ensure it is provided
    target_url = request.query_params.get("url")
    if not target_url:
        raise HTTPException(status_code=400, detail="Missing 'url' query parameter.")
    
    # Optional: Validate the URL schema
    parsed_url = urlparse(target_url)
    if parsed_url.scheme not in ['http', 'https']:
        raise HTTPException(status_code=400, detail="Invalid URL scheme. Only HTTP and HTTPS are supported.")

    # Prepare headers from the original request
    headers = dict(request.headers)
    headers.pop('host', None)
    
    # Prepare the body for methods that allow a body
    body = None
    if request.method in ["POST", "PUT", "PATCH"]:
        body = await request.body()

    try:
        async with http_client.request(
            method=request.method,
            url=target_url,
            headers=headers,
            data=body,
            allow_redirects=False,
            ssl = False
        ) as upstream_response:
            # Prepare the response headers, excluding some that might cause issues
            response_headers = {k: v for k, v in upstream_response.headers.items()
                                if k.lower() not in ['transfer-encoding', 'content-encoding']}
            
            # Stream the response back to the client
            content = await upstream_response.read()
            return Response(content=content, status_code=upstream_response.status,
                            headers=response_headers)
    except aiohttp.ClientError as e:
        raise HTTPException(status_code=500, detail=str(e))

# Run the application
# Note: If running this file directly, uncomment the following lines:
# if __name__ == "__main__":
#     import uvicorn
#     uvicorn.run(app, host="0.0.0.0", port=8000)