Spaces:
Sleeping
Sleeping
add long-polling: server blocks up to 5s on /poll, notifies immediately on new request
Browse files8 unmodified lines910111213131415162 unmodified lines19202122222324256 unmodified lines32333435363738393536423738453940416 unmodified lines4849505158525354555613 unmodified lines70717273787980818283848574757677787980818283848586878889909192939495968 unmodified linesPORT = int(os.environ.get("PORT", 7860))
pending = {}poll_cond = asyncio.Condition()
# Magic byte signatures for content type detectionMAGIC_SIGNATURES = [ (b'\xff\xd8\xff', 'image/jpeg'), (b'\x89PNG\r\n\x1a\n', 'image/png'),2 unmodified lines (b'RIFF', 'image/webp', lambda b: b[8:12] == b'WEBP'), (b'%PDF', 'application/pdf'), (b'PK\x03\x04', 'application/zip'), (b'\x1aE\xdf\xa3', 'application/octet-stream'), # Matroska/WebM container (b'\x1aE\xdf\xa3', 'application/octet-stream'),]
def detect_content_type(body, fallback="application/octet-stream"):6 unmodified lines condition = extra_check(body[:16]) if callable(extra_check) else True if body.startswith(magic) and condition: return ct # Check if it looks like HTML text = body[:500].decode('utf-8', errors='ignore').strip().lower() if text.startswith('<!doctype') or text.startswith('<html') or text.startswith('<!DOCTYPE') or text.startswith('<html'): return fallback if 'text/html' in fallback else fallback # Check if it looks like plain text try: body.decode('utf-8') # It's valid UTF-8 text - keep the fallback return fallback except (UnicodeDecodeError, ValueError): # Binary data - use detected type or octet-stream return "application/octet-stream"
async def proxy_handler(request):6 unmodified lines rid = str(uuid.uuid4()) event = asyncio.Event() result = {"body": b"", "status": 502, "content_type": "text/plain"} async with poll_cond: pending[rid] = {"event": event, "result": result, "url": target_url, "claimed": False} pending[rid] = {"event": event, "result": result, "url": target_url, "claimed": False} poll_cond.notify_all() try: await asyncio.wait_for(event.wait(), timeout=30) except asyncio.TimeoutError:13 unmodified lines return web.Response(body=body, content_type=ct)
async def poll_handler(request): async with poll_cond: for rid, info in list(pending.items()): if not info["claimed"]: info["claimed"] = True return web.json_response({ "rid": rid, "url": info["url"], "url_encoded": urllib.parse.quote(info["url"], safe='') }) for rid, info in list(pending.items()): if not info["claimed"]: info["claimed"] = True return web.json_response({ "rid": rid, "url": info["url"], "url_encoded": urllib.parse.quote(info["url"], safe='') }) try: await asyncio.wait_for(poll_cond.wait(), timeout=5.0) except asyncio.TimeoutError: return web.json_response({}) for rid, info in list(pending.items()): if not info["claimed"]: info["claimed"] = True return web.json_response({ "rid": rid, "url": info["url"], "url_encoded": urllib.parse.quote(info["url"], safe='') }) return web.json_response({})
async def respond_handler(request):app.py+26-18