From 5c341a2b00c2ef2f7898bcc1e0783c0f373e0421 Mon Sep 17 00:00:00 2001 From: Dirk Guijt Date: Fri, 2 Feb 2018 09:43:42 +0100 Subject: [PATCH] made field name mandatory in multipart/form-data headers A field name in the Content-Disposition header is required by the multipart/form-data spec. If one field/part does not have it, it will be omitted from the request. When this happens, we log it to DEBUG. --- sanic/request.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/sanic/request.py b/sanic/request.py index b37f9f9d..0660337f 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -18,7 +18,7 @@ except ImportError: json_loads = json.loads 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" @@ -309,18 +309,21 @@ def parse_multipart_form(body, boundary): content_type = form_header_value content_charset = form_parameters.get('charset', 'utf-8') - post_data = form_part[line_index:-4] - if file_name: - file = File(type=content_type, name=file_name, body=post_data) - if field_name in files: - files[field_name].append(file) + if field_name: + post_data = form_part[line_index:-4] + if file_name: + file = File(type=content_type, name=file_name, body=post_data) + if field_name in files: + files[field_name].append(file) + else: + files[field_name] = [file] else: - files[field_name] = [file] + value = post_data.decode(content_charset) + if field_name in fields: + fields[field_name].append(value) + else: + fields[field_name] = [value] else: - value = post_data.decode(content_charset) - if field_name in fields: - fields[field_name].append(value) - else: - fields[field_name] = [value] + logger.debug('Form-data field does not have a name parameter in the Content-Disposition header') return fields, files