From 76c1a6ae479fe06911149e45e293d2fd5e17038b Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 19 Jun 2018 11:36:55 -0700 Subject: [PATCH 1/2] Proposed fixes for issue 27777 --- Lib/cgi.py | 6 +----- Lib/test/test_cgi.py | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Lib/cgi.py b/Lib/cgi.py index f82cc6c8bd56d3..ebe1210ddb4b74 100755 --- a/Lib/cgi.py +++ b/Lib/cgi.py @@ -646,10 +646,6 @@ def read_multi(self, environ, keep_blank_values, strict_parsing): parser.feed(hdr_text.decode(self.encoding, self.errors)) headers = parser.close() - # Some clients add Content-Length for part headers, ignore them - if 'content-length' in headers: - del headers['content-length'] - part = klass(self.fp, headers, ib, environ, keep_blank_values, strict_parsing,self.limit-self.bytes_read, self.encoding, self.errors) @@ -818,7 +814,7 @@ def make_file(self): which unlinks the temporary files you have created. """ - if self._binary_file: + if self._binary_file or self.length >= 0: return tempfile.TemporaryFile("wb+") else: return tempfile.TemporaryFile("w+", diff --git a/Lib/test/test_cgi.py b/Lib/test/test_cgi.py index 4f2bba14a1bdc4..6863c0fd522a6e 100644 --- a/Lib/test/test_cgi.py +++ b/Lib/test/test_cgi.py @@ -361,7 +361,20 @@ def test_fieldstorage_part_content_length(self): fs = cgi.FieldStorage(fp, environ=env, encoding="latin-1") self.assertEqual(len(fs.list), 1) self.assertEqual(fs.list[0].name, 'submit-name') - self.assertEqual(fs.list[0].value, 'Larry') + self.assertEqual(fs.list[0].value, b'Larry') + + def test_fieldstorage_not_multipart(self): + POSTDATA = b'{"name": "Bert"}' + + env = { + 'REQUEST_METHOD': 'POST', + 'CONTENT_TYPE': 'text/plain', + 'CONTENT_LENGTH': str(len(POSTDATA)) + } + fp = BytesIO(POSTDATA) + fs = cgi.FieldStorage(fp, environ=env) + self.assertEqual(fs.list, None) + self.assertEqual(fs.value, b'{"name": "Bert"}') def test_fieldstorage_as_context_manager(self): fp = BytesIO(b'x' * 10) From 0ebe13854d3278b6dd3deb29cd924b005ee40e1a Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 19 Jun 2018 12:04:39 -0700 Subject: [PATCH 2/2] Add blurb describing fix --- .../Core and Builtins/2018-06-19-12-02-47.bpo-27777.Uw2zdd.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2018-06-19-12-02-47.bpo-27777.Uw2zdd.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-06-19-12-02-47.bpo-27777.Uw2zdd.rst b/Misc/NEWS.d/next/Core and Builtins/2018-06-19-12-02-47.bpo-27777.Uw2zdd.rst new file mode 100644 index 00000000000000..9427e148f48899 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2018-06-19-12-02-47.bpo-27777.Uw2zdd.rst @@ -0,0 +1,3 @@ +Fix issue in cgi.py sending requests with Content-Length but without +Content-Disposition headers, also choosing between creating files in text or +binary mode.