More robust logic to read request body (Fixes #31)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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'),
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user