py3: port formpost middleware

Change-Id: I8f3d4d5f6976ef5b63facd9b5723aac894066b74
This commit is contained in:
Pete Zaitcev
2019-02-26 23:06:52 -06:00
parent 96013436a1
commit 61e6ac0ebd
4 changed files with 239 additions and 213 deletions

View File

@@ -126,7 +126,9 @@ import hmac
from hashlib import sha1 from hashlib import sha1
from time import time from time import time
import six
from six.moves.urllib.parse import quote from six.moves.urllib.parse import quote
from swift.common.exceptions import MimeInvalid from swift.common.exceptions import MimeInvalid
from swift.common.middleware.tempurl import get_tempurl_keys_from_metadata from swift.common.middleware.tempurl import get_tempurl_keys_from_metadata
from swift.common.utils import streq_const_time, register_swift_info, \ from swift.common.utils import streq_const_time, register_swift_info, \
@@ -229,7 +231,7 @@ class FormPost(object):
start_response(status, headers) start_response(status, headers)
return [body] return [body]
except MimeInvalid: except MimeInvalid:
body = 'FormPost: invalid starting boundary' body = b'FormPost: invalid starting boundary'
start_response( start_response(
'400 Bad Request', '400 Bad Request',
(('Content-Type', 'text/plain'), (('Content-Type', 'text/plain'),
@@ -237,6 +239,8 @@ class FormPost(object):
return [body] return [body]
except (FormInvalid, EOFError) as err: except (FormInvalid, EOFError) as err:
body = 'FormPost: %s' % err body = 'FormPost: %s' % err
if six.PY3:
body = body.encode('utf-8')
start_response( start_response(
'400 Bad Request', '400 Bad Request',
(('Content-Type', 'text/plain'), (('Content-Type', 'text/plain'),
@@ -258,6 +262,8 @@ class FormPost(object):
:returns: status_line, headers_list, body :returns: status_line, headers_list, body
""" """
keys = self._get_keys(env) keys = self._get_keys(env)
if six.PY3:
boundary = boundary.encode('utf-8')
status = message = '' status = message = ''
attributes = {} attributes = {}
subheaders = [] subheaders = []
@@ -282,14 +288,13 @@ class FormPost(object):
hdrs['Content-Type'] or 'application/octet-stream' hdrs['Content-Type'] or 'application/octet-stream'
if 'content-encoding' not in attributes and \ if 'content-encoding' not in attributes and \
'content-encoding' in hdrs: 'content-encoding' in hdrs:
attributes['content-encoding'] = \ attributes['content-encoding'] = hdrs['Content-Encoding']
hdrs['Content-Encoding']
status, subheaders = \ status, subheaders = \
self._perform_subrequest(env, attributes, fp, keys) self._perform_subrequest(env, attributes, fp, keys)
if not status.startswith('2'): if not status.startswith('2'):
break break
else: else:
data = '' data = b''
mxln = MAX_VALUE_LENGTH mxln = MAX_VALUE_LENGTH
while mxln: while mxln:
chunk = fp.read(mxln) chunk = fp.read(mxln)
@@ -299,6 +304,8 @@ class FormPost(object):
data += chunk data += chunk
while fp.read(READ_CHUNK_SIZE): while fp.read(READ_CHUNK_SIZE):
pass pass
if six.PY3:
data = data.decode('utf-8')
if 'name' in attrs: if 'name' in attrs:
attributes[attrs['name'].lower()] = data.rstrip('\r\n--') attributes[attrs['name'].lower()] = data.rstrip('\r\n--')
if not status: if not status:
@@ -315,6 +322,8 @@ class FormPost(object):
body = status + '\r\nFormPost: ' + message.title() body = status + '\r\nFormPost: ' + message.title()
headers.extend([('Content-Type', 'text/plain'), headers.extend([('Content-Type', 'text/plain'),
('Content-Length', len(body))]) ('Content-Length', len(body))])
if six.PY3:
body = body.encode('utf-8')
return status, headers, body return status, headers, body
status = status.split(' ', 1)[0] status = status.split(' ', 1)[0]
if '?' in redirect: if '?' in redirect:
@@ -324,6 +333,8 @@ class FormPost(object):
redirect += 'status=%s&message=%s' % (quote(status), quote(message)) redirect += 'status=%s&message=%s' % (quote(status), quote(message))
body = '<html><body><p><a href="%s">' \ body = '<html><body><p><a href="%s">' \
'Click to continue...</a></p></body></html>' % redirect 'Click to continue...</a></p></body></html>' % redirect
if six.PY3:
body = body.encode('utf-8')
headers.extend( headers.extend(
[('Location', redirect), ('Content-Length', str(len(body)))]) [('Location', redirect), ('Content-Length', str(len(body)))])
return '303 See Other', headers, body return '303 See Other', headers, body
@@ -385,6 +396,8 @@ class FormPost(object):
attributes.get('max_file_size') or '0', attributes.get('max_file_size') or '0',
attributes.get('max_file_count') or '0', attributes.get('max_file_count') or '0',
attributes.get('expires') or '0') attributes.get('expires') or '0')
if six.PY3:
hmac_body = hmac_body.encode('utf-8')
has_valid_sig = False has_valid_sig = False
for key in keys: for key in keys:

View File

@@ -4273,8 +4273,7 @@ def iter_multipart_mime_documents(wsgi_input, boundary, read_chunk_size=4096):
for doing that if necessary. for doing that if necessary.
:param wsgi_input: The file-like object to read from. :param wsgi_input: The file-like object to read from.
:param boundary: The mime boundary to separate new file-like :param boundary: The mime boundary to separate new file-like objects on.
objects on.
:returns: A generator of file-like objects for each part. :returns: A generator of file-like objects for each part.
:raises MimeInvalid: if the document is malformed :raises MimeInvalid: if the document is malformed
""" """

File diff suppressed because it is too large Load Diff

View File

@@ -45,6 +45,7 @@ commands =
test/unit/common/middleware/test_copy.py \ test/unit/common/middleware/test_copy.py \
test/unit/common/middleware/test_crossdomain.py \ test/unit/common/middleware/test_crossdomain.py \
test/unit/common/middleware/test_domain_remap.py \ test/unit/common/middleware/test_domain_remap.py \
test/unit/common/middleware/test_formpost.py \
test/unit/common/middleware/test_gatekeeper.py \ test/unit/common/middleware/test_gatekeeper.py \
test/unit/common/middleware/test_healthcheck.py \ test/unit/common/middleware/test_healthcheck.py \
test/unit/common/middleware/test_keystoneauth.py \ test/unit/common/middleware/test_keystoneauth.py \