Merge pull request #1109 from DirkGuijt/master

fixed bug in multipart/form-data parser
This commit is contained in:
Raphael Deem 2018-02-08 00:11:20 -08:00 committed by GitHub
commit 6d37ef7256
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,7 +18,7 @@ except ImportError:
json_loads = json.loads json_loads = json.loads
from sanic.exceptions import InvalidUsage from sanic.exceptions import InvalidUsage
from sanic.log import error_logger from sanic.log import error_logger, logger
DEFAULT_HTTP_CONTENT_TYPE = "application/octet-stream" DEFAULT_HTTP_CONTENT_TYPE = "application/octet-stream"
@ -284,7 +284,8 @@ def parse_multipart_form(body, boundary):
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]:
file_name = None file_name = None
file_type = None content_type = 'text/plain'
content_charset = 'utf-8'
field_name = None field_name = None
line_index = 2 line_index = 2
line_end_index = 0 line_end_index = 0
@ -302,24 +303,30 @@ def parse_multipart_form(body, boundary):
form_line[colon_index + 2:]) form_line[colon_index + 2:])
if form_header_field == 'content-disposition': if form_header_field == 'content-disposition':
if 'filename' in form_parameters: file_name = form_parameters.get('filename')
file_name = form_parameters['filename']
field_name = form_parameters.get('name') field_name = form_parameters.get('name')
elif form_header_field == 'content-type': elif form_header_field == 'content-type':
file_type = form_header_value content_type = form_header_value
content_charset = form_parameters.get('charset', 'utf-8')
if field_name:
post_data = form_part[line_index:-4] post_data = form_part[line_index:-4]
if file_name or file_type: if file_name:
file = File(type=file_type, name=file_name, body=post_data) form_file = File(type=content_type,
name=file_name,
body=post_data)
if field_name in files: if field_name in files:
files[field_name].append(file) files[field_name].append(form_file)
else: else:
files[field_name] = [file] files[field_name] = [form_file]
else: else:
value = post_data.decode('utf-8') value = post_data.decode(content_charset)
if field_name in fields: if field_name in fields:
fields[field_name].append(value) fields[field_name].append(value)
else: else:
fields[field_name] = [value] fields[field_name] = [value]
else:
logger.debug('Form-data field does not have a \'name\' parameter \
in the Content-Disposition header')
return fields, files return fields, files