diff --git a/Lib/cgi.py b/Lib/cgi.py index c22c71b3878516..54f2812f5c414c 100755 --- a/Lib/cgi.py +++ b/Lib/cgi.py @@ -663,11 +663,9 @@ def read_multi(self, environ, keep_blank_values, strict_parsing): def read_single(self): """Internal: read an atomic part.""" + self.read_lines() if self.length >= 0: - self.read_binary() self.skip_lines() - else: - self.read_lines() self.file.seek(0) bufsize = 8*1024 # I/O buffering size for copy to file diff --git a/Lib/test/test_cgi.py b/Lib/test/test_cgi.py index ab8677199f32e7..9080ac6f96d8e2 100644 --- a/Lib/test/test_cgi.py +++ b/Lib/test/test_cgi.py @@ -352,6 +352,18 @@ def test_fieldstorage_part_content_length(self): self.assertEqual(fs.list[0].name, 'submit-name') self.assertEqual(fs.list[0].value, 'Larry') + def test_content_length_no_content_disposition(self): + body = b'{"test":123}' + env = { + 'CONTENT_LENGTH': len(body), + 'REQUEST_METHOD': 'POST', + 'CONTENT_TYPE': 'application/json', + 'wsgi.input': BytesIO(body), + } + + form = cgi.FieldStorage(fp=env['wsgi.input'], environ=env) + self.assertEqual(form.file.read(), body.decode(form.encoding)) + def test_field_storage_multipart_no_content_length(self): fp = BytesIO(b"""--MyBoundary Content-Disposition: form-data; name="my-arg"; filename="foo" diff --git a/Misc/NEWS.d/next/Library/2018-11-28-17-47-47.bpo-27777.AKUxDR.rst b/Misc/NEWS.d/next/Library/2018-11-28-17-47-47.bpo-27777.AKUxDR.rst new file mode 100644 index 00000000000000..c967372ae7a9c8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-11-28-17-47-47.bpo-27777.AKUxDR.rst @@ -0,0 +1,2 @@ +Fixes a bug in :mod:`cgi` when a request has a "Content-Length" header but +without "Content-Disposition" headers.