Skip to content

Commit

Permalink
We need a different approach to preserve query string parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
davisagli committed Jun 26, 2018
1 parent de64eb7 commit f9b7388
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
29 changes: 11 additions & 18 deletions src/ZPublisher/HTTPRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,23 +497,8 @@ def processInputs(
environ['QUERY_STRING'] = ''

meth = None

# Workaround for https://bugs.python.org/issue27777:
# If Content-Length is nonzero, manufacture a Content-Disposition
# with a filename to make sure a binary file is opened.
headers = None
if 'CONTENT_LENGTH' in environ and environ['CONTENT_LENGTH'] != '0':
# In order to override content-disposition we need to
# specify the full headers; this is based on FileStorage.__init__
headers = {
'content-type': 'application/x-www-form-urlencoded'
}
if 'CONTENT_TYPE' in environ:
headers['content-type'] = environ['CONTENT_TYPE']
headers['content-length'] = environ['CONTENT_LENGTH']
headers['content-disposition'] = 'inline; filename="stdin"'
fs = FieldStorage(
fp=fp, headers=headers, environ=environ, keep_blank_values=1)
fs = ZopeFieldStorage(
fp=fp, environ=environ, keep_blank_values=1)

# Keep a reference to the FieldStorage. Otherwise it's
# __del__ method is called too early and closing FieldStorage.file.
Expand Down Expand Up @@ -1643,7 +1628,15 @@ def sane_environment(env):
return dict


ZopeFieldStorage = FieldStorage # BBB
class ZopeFieldStorage(FieldStorage):
"""This subclass exists to work around a Python bug
(see https://bugs.python.org/issue27777) to make sure
we can read binary data from a request body.
"""

def read_binary(self):
self._binary_file = True
return super(ZopeFieldStorage, self).read_binary()


# Original version: zope.publisher.browser.FileUpload
Expand Down
13 changes: 13 additions & 0 deletions src/ZPublisher/tests/testHTTPRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,19 @@ def test_processInputs_xmlrpc(self):
self.assertEqual(req.PATH_INFO, '/test')
self.assertEqual(req.args, ())

def test_processInputs_w_urlencoded_and_qs(self):
body = b'foo=1'
environ = {
'CONTENT_TYPE': 'application/x-www-form-urlencoded',
'CONTENT_LENGTH': len(body),
'QUERY_STRING': 'bar=2',
'REQUEST_METHOD': 'POST',
}
req = self._makeOne(stdin=BytesIO(body), environ=environ)
req.processInputs()
self.assertEqual(req.form['foo'], '1')
self.assertEqual(req.form['bar'], '2')

def test_postProcessInputs(self):
from ZPublisher.HTTPRequest import default_encoding

Expand Down

0 comments on commit f9b7388

Please sign in to comment.