More robust logic to read request body (Fixes #31)

This commit is contained in:
Miguel Grinberg
2021-10-23 19:03:31 +01:00
parent 7bc5d724f0
commit bd82c4deab
4 changed files with 15 additions and 3 deletions

View File

@@ -273,8 +273,13 @@ class Request():
content_length = int(value)
# body
body = client_stream.read(content_length) if content_length and \
content_length <= Request.max_content_length else b''
body = b''
if content_length and content_length <= Request.max_content_length:
while len(body) < content_length:
data = client_stream.read(content_length - len(body))
if len(data) == 0: # pragma: no cover
raise EOFError()
body += data
return Request(app, client_addr, method, url, http_version, headers,
body)

View File

@@ -55,7 +55,8 @@ class Request(BaseRequest):
content_length = int(value)
# body
body = await client_stream.read(content_length) if content_length and \
body = await client_stream.readexactly(content_length) \
if content_length and \
content_length <= Request.max_content_length else b''
return Request(app, client_addr, method, url, http_version, headers,

View File

@@ -167,6 +167,9 @@ class TestResponse(unittest.TestCase):
self.assertEqual(res.status_code, 301)
self.assertEqual(res.headers['Location'], '/foo')
with self.assertRaises(ValueError):
Response.redirect('/foo\x0d\x0a\x0d\x0a<p>Foo</p>')
def test_send_file(self):
files = [
('test.txt', 'text/plain'),

View File

@@ -59,6 +59,9 @@ class FakeStreamAsync:
async def read(self, n):
return self.stream.read(n)
async def readexactly(self, n):
return self.stream.read(n)
async def awrite(self, data):
self.stream.write(data)