Return a 400 error when request object could not be created

This commit is contained in:
Miguel Grinberg
2021-09-28 17:09:02 +01:00
parent 568cd51fd2
commit 06015934b8
4 changed files with 33 additions and 16 deletions

View File

@@ -310,7 +310,6 @@ class Request():
@staticmethod @staticmethod
def _safe_readline(stream): def _safe_readline(stream):
line = stream.readline(Request.max_readline + 1) line = stream.readline(Request.max_readline + 1)
print(line, Request.max_readline)
if len(line) > Request.max_readline: if len(line) > Request.max_readline:
raise ValueError('line too long') raise ValueError('line too long')
return line return line
@@ -795,7 +794,11 @@ class Microdot():
else: else:
stream = sock stream = sock
req = None
try:
req = Request.create(self, stream, addr) req = Request.create(self, stream, addr)
except Exception as exc: # pragma: no cover
print_exception(exc)
if req: if req:
if req.content_length > req.max_content_length: if req.content_length > req.max_content_length:
if 413 in self.error_handlers: if 413 in self.error_handlers:
@@ -836,6 +839,8 @@ class Microdot():
res = self.error_handlers[500](req) res = self.error_handlers[500](req)
else: else:
res = 'Internal server error', 500 res = 'Internal server error', 500
else:
res = 'Bad request', 400
if isinstance(res, tuple): if isinstance(res, tuple):
res = Response(*res) res = Response(*res)
elif not isinstance(res, Response): elif not isinstance(res, Response):

View File

@@ -218,8 +218,12 @@ class Microdot(BaseMicrodot):
self.server.close() self.server.close()
async def dispatch_request(self, reader, writer): async def dispatch_request(self, reader, writer):
req = None
try:
req = await Request.create(self, reader, req = await Request.create(self, reader,
writer.get_extra_info('peername')) writer.get_extra_info('peername'))
except Exception as exc: # pragma: no cover
print_exception(exc)
if req: if req:
if req.content_length > req.max_content_length: if req.content_length > req.max_content_length:
if 413 in self.error_handlers: if 413 in self.error_handlers:
@@ -266,6 +270,8 @@ class Microdot(BaseMicrodot):
self.error_handlers[500], req) self.error_handlers[500], req)
else: else:
res = 'Internal server error', 500 res = 'Internal server error', 500
else:
res = 'Bad request', 400
if isinstance(res, tuple): if isinstance(res, tuple):
res = Response(*res) res = Response(*res)
elif not isinstance(res, Response): elif not isinstance(res, Response):

View File

@@ -73,7 +73,10 @@ class TestMicrodot(unittest.TestCase):
mock_socket._requests.append(fd) mock_socket._requests.append(fd)
self._add_shutdown(app) self._add_shutdown(app)
app.run() app.run()
assert fd.response == b'' self.assertTrue(fd.response.startswith(b'HTTP/1.0 400 N/A\r\n'))
self.assertIn(b'Content-Length: 11\r\n', fd.response)
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
self.assertTrue(fd.response.endswith(b'\r\n\r\nBad request'))
def test_method_decorators(self): def test_method_decorators(self):
app = Microdot() app = Microdot()

View File

@@ -84,7 +84,10 @@ class TestMicrodotAsync(unittest.TestCase):
mock_socket._requests.append(fd) mock_socket._requests.append(fd)
self._add_shutdown(app) self._add_shutdown(app)
app.run() app.run()
assert fd.response == b'' self.assertTrue(fd.response.startswith(b'HTTP/1.0 400 N/A\r\n'))
self.assertIn(b'Content-Length: 11\r\n', fd.response)
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
self.assertTrue(fd.response.endswith(b'\r\n\r\nBad request'))
def test_before_after_request(self): def test_before_after_request(self):
app = Microdot() app = Microdot()