Return a 400 error when request object could not be created
This commit is contained in:
@@ -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 = Request.create(self, stream, addr)
|
req = None
|
||||||
|
try:
|
||||||
|
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,11 +839,13 @@ 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
|
||||||
if isinstance(res, tuple):
|
else:
|
||||||
res = Response(*res)
|
res = 'Bad request', 400
|
||||||
elif not isinstance(res, Response):
|
if isinstance(res, tuple):
|
||||||
res = Response(res)
|
res = Response(*res)
|
||||||
res.write(stream)
|
elif not isinstance(res, Response):
|
||||||
|
res = Response(res)
|
||||||
|
res.write(stream)
|
||||||
stream.close()
|
stream.close()
|
||||||
if stream != sock: # pragma: no cover
|
if stream != sock: # pragma: no cover
|
||||||
sock.close()
|
sock.close()
|
||||||
|
|||||||
@@ -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 = await Request.create(self, reader,
|
req = None
|
||||||
writer.get_extra_info('peername'))
|
try:
|
||||||
|
req = await Request.create(self, reader,
|
||||||
|
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,11 +270,13 @@ 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
|
||||||
if isinstance(res, tuple):
|
else:
|
||||||
res = Response(*res)
|
res = 'Bad request', 400
|
||||||
elif not isinstance(res, Response):
|
if isinstance(res, tuple):
|
||||||
res = Response(res)
|
res = Response(*res)
|
||||||
await res.write(writer)
|
elif not isinstance(res, Response):
|
||||||
|
res = Response(res)
|
||||||
|
await res.write(writer)
|
||||||
await writer.aclose()
|
await writer.aclose()
|
||||||
if self.debug and req: # pragma: no cover
|
if self.debug and req: # pragma: no cover
|
||||||
print('{method} {path} {status_code}'.format(
|
print('{method} {path} {status_code}'.format(
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
Reference in New Issue
Block a user