Spaces:
Sleeping
Sleeping
Upload main.py
Browse files
main.py
CHANGED
|
@@ -69,7 +69,7 @@ app.add_middleware(
|
|
| 69 |
# to ensure we don’t leak hop-by-hop headers
|
| 70 |
def _filter_resp_headers(h):
|
| 71 |
# pass through useful headers but strip hop-by-hop
|
| 72 |
-
allowed = {"content-
|
| 73 |
return {k: v for k, v in h.items() if k.lower() in allowed}
|
| 74 |
|
| 75 |
def _extract_user_token(req: Request) -> str | None:
|
|
@@ -124,8 +124,19 @@ async def _forward_stream(path: str, files=None, data=None, user_token: str | No
|
|
| 124 |
async with client.stream(method, url, headers=headers, files=files, data=data) as resp:
|
| 125 |
if resp.status_code >= 400:
|
| 126 |
text = await resp.aread()
|
| 127 |
-
return Response(
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
# for demo
|
| 131 |
async def _forward_demo_stream(path: str, *, files: dict | None, data: dict | None, method: str = "POST"):
|
|
@@ -142,12 +153,22 @@ async def _forward_demo_stream(path: str, *, files: dict | None, data: dict | No
|
|
| 142 |
|
| 143 |
timeout = httpx.Timeout(120.0)
|
| 144 |
async with httpx.AsyncClient(timeout=timeout) as client:
|
| 145 |
-
# stream response back to the caller (so large CSVs don’t load fully into memory)
|
| 146 |
async with client.stream(method, url, headers=headers, files=files, data=data) as resp:
|
| 147 |
if resp.status_code >= 400:
|
| 148 |
text = await resp.aread()
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
@app.get("/")
|
| 153 |
async def root(req: Request):
|
|
|
|
| 69 |
# to ensure we don’t leak hop-by-hop headers
|
| 70 |
def _filter_resp_headers(h):
|
| 71 |
# pass through useful headers but strip hop-by-hop
|
| 72 |
+
allowed = {"content-disposition"}
|
| 73 |
return {k: v for k, v in h.items() if k.lower() in allowed}
|
| 74 |
|
| 75 |
def _extract_user_token(req: Request) -> str | None:
|
|
|
|
| 124 |
async with client.stream(method, url, headers=headers, files=files, data=data) as resp:
|
| 125 |
if resp.status_code >= 400:
|
| 126 |
text = await resp.aread()
|
| 127 |
+
return Response(
|
| 128 |
+
content=text,
|
| 129 |
+
status_code=resp.status_code,
|
| 130 |
+
media_type=resp.headers.get("content-type","text/plain")
|
| 131 |
+
)
|
| 132 |
+
media_type = resp.headers.get("content-type", "application/octet-stream")
|
| 133 |
+
safe_headers = _filter_resp_headers(resp.headers) # Content-Disposition only
|
| 134 |
+
return StreamingResponse(
|
| 135 |
+
resp.aiter_raw(),
|
| 136 |
+
status_code=resp.status_code,
|
| 137 |
+
media_type=media_type,
|
| 138 |
+
headers=safe_headers,
|
| 139 |
+
)
|
| 140 |
|
| 141 |
# for demo
|
| 142 |
async def _forward_demo_stream(path: str, *, files: dict | None, data: dict | None, method: str = "POST"):
|
|
|
|
| 153 |
|
| 154 |
timeout = httpx.Timeout(120.0)
|
| 155 |
async with httpx.AsyncClient(timeout=timeout) as client:
|
|
|
|
| 156 |
async with client.stream(method, url, headers=headers, files=files, data=data) as resp:
|
| 157 |
if resp.status_code >= 400:
|
| 158 |
text = await resp.aread()
|
| 159 |
+
return Response(
|
| 160 |
+
content=text,
|
| 161 |
+
status_code=resp.status_code,
|
| 162 |
+
media_type=resp.headers.get("content-type","text/plain")
|
| 163 |
+
)
|
| 164 |
+
media_type = resp.headers.get("content-type", "application/octet-stream")
|
| 165 |
+
safe_headers = _filter_resp_headers(resp.headers) # Content-Disposition only
|
| 166 |
+
return StreamingResponse(
|
| 167 |
+
resp.aiter_raw(),
|
| 168 |
+
status_code=resp.status_code,
|
| 169 |
+
media_type=media_type,
|
| 170 |
+
headers=safe_headers,
|
| 171 |
+
)
|
| 172 |
|
| 173 |
@app.get("/")
|
| 174 |
async def root(req: Request):
|