-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathsafety.py
More file actions
37 lines (36 loc) · 1.38 KB
/
Copy pathsafety.py
File metadata and controls
37 lines (36 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import asyncio
class RequestLimits:
def __init__(self, app):
self.app = app
async def __call__(self, scope, receive, send):
if scope["type"] != "http":
return await self.app(scope, receive, send)
body = bytearray()
status = None
try:
async with asyncio.timeout(10):
for _ in range(4096):
message = await receive()
if message["type"] == "http.disconnect":
return
body.extend(message.get("body", b""))
if len(body) > 65536:
status = 413
break
if not message.get("more_body", False):
break
else:
status = 413
except TimeoutError:
status = 408
if status:
await send({"type": "http.response.start", "status": status, "headers": []})
return await send({"type": "http.response.body", "body": b"Request limit exceeded"})
consumed = False
async def bounded_receive():
nonlocal consumed
if consumed:
return await receive()
consumed = True
return {"type": "http.request", "body": bytes(body), "more_body": False}
await self.app(scope, bounded_receive, send)