Spaces:
Running
Running
Upload 3 files
Browse files- router_proxy.py +15 -4
router_proxy.py
CHANGED
|
@@ -150,7 +150,21 @@ async def proxy_github_zip(req_data: ProxyGithubZipRequest, db: Session = Depend
|
|
| 150 |
stream=True
|
| 151 |
)
|
| 152 |
|
| 153 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
content_length = response.headers.get('content-length', '')
|
| 155 |
resp_headers = {}
|
| 156 |
if content_length:
|
|
@@ -158,9 +172,6 @@ async def proxy_github_zip(req_data: ProxyGithubZipRequest, db: Session = Depend
|
|
| 158 |
|
| 159 |
async def stream_generator():
|
| 160 |
try:
|
| 161 |
-
if response.status_code != 200:
|
| 162 |
-
yield b"GITHUB_DOWNLOAD_FAILED"
|
| 163 |
-
return
|
| 164 |
async for chunk in response.aiter_bytes():
|
| 165 |
yield chunk
|
| 166 |
except Exception:
|
|
|
|
| 150 |
stream=True
|
| 151 |
)
|
| 152 |
|
| 153 |
+
# 非 200 直接返回错误,避免 Content-Length 不匹配导致 h11 协议错误
|
| 154 |
+
if response.status_code != 200:
|
| 155 |
+
error_body = ""
|
| 156 |
+
try:
|
| 157 |
+
error_body = (await response.aread()).decode('utf-8', errors='ignore')[:200]
|
| 158 |
+
except Exception:
|
| 159 |
+
pass
|
| 160 |
+
await response.aclose()
|
| 161 |
+
await client.aclose()
|
| 162 |
+
status_map = {404: "仓库不存在或无权访问", 401: "Token 无效或已过期", 403: "访问被拒绝(可能触发速率限制)"}
|
| 163 |
+
err_msg = status_map.get(response.status_code, f"GitHub API 返回 HTTP {response.status_code}")
|
| 164 |
+
print(f"[proxy_github_zip] ❌ GitHub API 失败: {response.status_code}, repo: {github_zip_api}, body: {error_body[:100]}")
|
| 165 |
+
return JSONResponse(content={"error": f"GITHUB_DOWNLOAD_FAILED: {err_msg}"}, status_code=502)
|
| 166 |
+
|
| 167 |
+
# 成功时才透传 Content-Length 和流式响应
|
| 168 |
content_length = response.headers.get('content-length', '')
|
| 169 |
resp_headers = {}
|
| 170 |
if content_length:
|
|
|
|
| 172 |
|
| 173 |
async def stream_generator():
|
| 174 |
try:
|
|
|
|
|
|
|
|
|
|
| 175 |
async for chunk in response.aiter_bytes():
|
| 176 |
yield chunk
|
| 177 |
except Exception:
|