Spaces:
Sleeping
Sleeping
use asyncio.to_thread + urllib for direct CDN fetch
Browse files3 unmodified lines45677891042 unmodified lines53545556575657585960616263645859606162636465666768693 unmodified linesimport sysimport uuidimport urllib.parsefrom aiohttp import web, ClientSession, ClientTimeoutfrom aiohttp import web
PORT = int(os.environ.get("PORT", 7860))
42 unmodified lines # Fetch CDN assets directly (fast, no router needed) if target_url.startswith(DIRECT_PREFIXES): try: import urllib.request def _fetch(): async with ClientSession() as sess: async with sess.get(target_url, timeout=ClientTimeout(total=20)) as resp: body = await resp.read() ct = resp.content_type or "application/octet-stream" if ct in ("text/html", "text/plain", "application/octet-stream"): detected = detect_content_type(body, ct) if detected != ct: ct = detected return web.Response(body=body, status=resp.status, content_type=ct) req = urllib.request.Request(target_url, headers={"User-Agent": "Mozilla/5.0"}) r = urllib.request.urlopen(req, timeout=20) return r.read(), r.getcode(), r.headers.get("Content-Type", "application/octet-stream") body, status, ct = await asyncio.to_thread(_fetch) if ct in ("text/html", "text/plain", "application/octet-stream"): detected = detect_content_type(body, ct) if detected != ct: ct = detected return web.Response(body=body, status=status, content_type=ct) except Exception as e: return web.Response(body=str(e).encode(), status=502, content_type="text/plain")
app.py+12-10