vela-studio / request_limits.py
Xunzhuo's picture
Add Vela Omni similarity, routing, and clustering for images and speech
83ad60c verified
Raw History Blame Contribute Delete
1.42 kB
"""Bound request bytes before the framework parses public JSON inputs."""
from starlette.responses import JSONResponse
class RequestSizeLimitMiddleware:
def __init__(self, app, max_bytes=2 * 1024 * 1024):
self.app = app
self.max_bytes = max_bytes
async def __call__(self, scope, receive, send):
if scope["type"] != "http" or scope["path"] not in {"/api/analyze", "/api/jobs"}:
return await self.app(scope, receive, send)
body = bytearray()
while True:
message = await receive()
if message["type"] == "http.disconnect":
return
chunk = message.get("body", b"")
if len(body) + len(chunk) > self.max_bytes:
response = JSONResponse(status_code=413, content={
"detail": "Request is too large. Use fewer or smaller media files, or shorten the text.",
})
return await response(scope, receive, send)
body.extend(chunk)
if not message.get("more_body", False):
break
delivered = False
async def bounded_receive():
nonlocal delivered
if delivered:
return await receive()
delivered = True
return {"type": "http.request", "body": bytes(body), "more_body": False}
await self.app(scope, bounded_receive, send)