implement file size upload limit

This commit is contained in:
Jakob Ketterl
2021-04-29 18:18:18 +02:00
parent 7115d5c951
commit af553c422d
4 changed files with 75 additions and 17 deletions

View File

@@ -1,6 +1,10 @@
from datetime import datetime, timezone
class BodySizeError(Exception):
pass
class Controller(object):
def __init__(self, handler, request, options):
self.handler = handler
@@ -33,10 +37,12 @@ class Controller(object):
self.handler.send_header("Location", location)
self.handler.end_headers()
def get_body(self):
def get_body(self, max_size=None):
if "Content-Length" not in self.handler.headers:
return None
length = int(self.handler.headers["Content-Length"])
if max_size is not None and length > max_size:
raise BodySizeError("HTTP body exceeds maximum allowed size")
return self.handler.rfile.read(length)
def handle_request(self):