Skip to content

Commit

Permalink
Fix POST with large values on Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Sutherland committed Mar 1, 2017
1 parent 0565a81 commit 9474f5a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -18,6 +18,9 @@ Changes
We now keep a reference to the FieldStorage till we are finished
processing the request.

- Fix POST with large values on Python 3. Related to cgi.FieldStorage
doing the decoding in Python 3.

4.3.0 (2016-07-04)
------------------

Expand Down
6 changes: 5 additions & 1 deletion src/zope/publisher/browser.py
Expand Up @@ -613,7 +613,11 @@ def get(self, key, default=None):
class ZopeFieldStorage(FieldStorage):

def make_file(self, binary=None):
return tempfile.NamedTemporaryFile('w+b')
if PYTHON2 or self._binary_file:
return tempfile.NamedTemporaryFile("w+b")
else:
return tempfile.NamedTemporaryFile("w+",
encoding=self.encoding, newline = '\n')


class FileUpload(object):
Expand Down
39 changes: 39 additions & 0 deletions src/zope/publisher/tests/test_browserrequest.py
Expand Up @@ -48,6 +48,13 @@
-----------------------------1--
"""])

LARGE_POSTED_VALUE = b''.join([b"""-----------------------------1
Content-Disposition: form-data; name="upload"
Here comes some text! """, (b'test'*1000), b"""
-----------------------------1--
"""])

IE_FILE_BODY = b"""-----------------------------1
Content-Disposition: form-data; name="upload"; filename="C:\\Windows\\notepad.exe"
Content-Type: text/plain
Expand Down Expand Up @@ -220,6 +227,38 @@ def testFileUploadPost(self):
# Test that we can actually read the file data
self.assertEqual(request.form['upload'].read(), b'Some data')

def testFileUploadPost(self):
"""Produce a Fieldstorage with a file handle that exposes
its filename."""

extra = {'REQUEST_METHOD':'POST',
'PATH_INFO': _u("/"),
'CONTENT_TYPE': 'multipart/form-data;\
boundary=---------------------------1'}

request = self._createRequest(extra, body=LARGE_FILE_BODY)
request.processInputs()
self.assertTrue(request.form['upload'].name)

request = self._createRequest(extra, body=IE_FILE_BODY)
request.processInputs()
self.assertEqual(request.form['upload'].filename, 'notepad.exe')

# Test that we can actually read the file data
self.assertEqual(request.form['upload'].read(), b'Some data')

def testLargePost(self):
"""Produce a Fieldstorage with a file handle that exposes
its filename."""

extra = {'REQUEST_METHOD':'POST',
'PATH_INFO': _u("/"),
'CONTENT_TYPE': 'multipart/form-data;\
boundary=---------------------------1'}

request = self._createRequest(extra, body=LARGE_POSTED_VALUE)
request.processInputs()

def testDefault2(self):
extra = {'PATH_INFO': '/folder/item2/view'}
request = self._createRequest(extra)
Expand Down

0 comments on commit 9474f5a

Please sign in to comment.