Merge pull request #145 from pahaz/fix-request-parse-multipart-form

Fix request parse multipart form
This commit is contained in:
Eli Uriegas 2016-11-10 09:00:06 -06:00 committed by GitHub
commit 42e3a50274

View File

@ -132,10 +132,10 @@ def parse_multipart_form(body, boundary):
Parses a request body and returns fields and files Parses a request body and returns fields and files
:param body: Bytes request body :param body: Bytes request body
:param boundary: Bytes multipart boundary :param boundary: Bytes multipart boundary
:return: fields (dict), files (dict) :return: fields (RequestParameters), files (RequestParameters)
""" """
files = {} files = RequestParameters()
fields = {} fields = RequestParameters()
form_parts = body.split(boundary) form_parts = body.split(boundary)
for form_part in form_parts[1:-1]: for form_part in form_parts[1:-1]:
@ -166,9 +166,16 @@ def parse_multipart_form(body, boundary):
post_data = form_part[line_index:-4] post_data = form_part[line_index:-4]
if file_name or file_type: if file_name or file_type:
files[field_name] = File( file = File(type=file_type, name=file_name, body=post_data)
type=file_type, name=file_name, body=post_data) if field_name in files:
files[field_name].append(file)
else:
files[field_name] = [file]
else: else:
fields[field_name] = post_data.decode('utf-8') value = post_data.decode('utf-8')
if field_name in fields:
fields[field_name].append(value)
else:
fields[field_name] = [value]
return fields, files return fields, files