Spaces:
Sleeping
Sleeping
use asyncio.to_thread + urllib for direct CDN fetch
Browse files3 unmodified lines4567891042 unmodified lines5354555657585960616263646566673 unmodified linesimport sysimport uuidimport urllib.parsefrom aiohttp import web, ClientSession, ClientTimeoutPORT = int(os.environ.get("PORT", 7860))42 unmodified lines# Fetch CDN assets directly (fast, no router needed)if target_url.startswith(DIRECT_PREFIXES):try: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 = detectedreturn web.Response(body=body, status=resp.status, content_type=ct)except Exception as e:return web.Response(body=str(e).encode(), status=502, content_type="text/plain")3 unmodified lines4567891042 unmodified lines53545556575859606162636465666768693 unmodified linesimport sysimport uuidimport urllib.parsefrom aiohttp import webPORT = 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.requestdef _fetch():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 = detectedreturn 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