From 3d75e6ed95c3fe83be93f7f73c03de0ca13b5721 Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Mon, 10 Jul 2017 12:29:47 -0700 Subject: [PATCH 1/2] case-insensitive check for header fields --- sanic/request.py | 4 ++-- tests/test_requests.py | 22 ++++++++++++++-------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/sanic/request.py b/sanic/request.py index e21b8282..da9a8cd0 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -242,11 +242,11 @@ def parse_multipart_form(body, boundary): form_header_value, form_parameters = parse_header( form_line[colon_index + 2:]) - if form_header_field == 'Content-Disposition': + if form_header_field.lower() == 'content-disposition': if 'filename' in form_parameters: file_name = form_parameters['filename'] field_name = form_parameters.get('name') - elif form_header_field == 'Content-Type': + elif form_header_field.lower() == 'content-type': file_type = form_header_value post_data = form_part[line_index:-4] diff --git a/tests/test_requests.py b/tests/test_requests.py index 81fe1a5c..997f32cb 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -259,20 +259,26 @@ def test_post_form_urlencoded(): assert request.form.get('test') == 'OK' - -def test_post_form_multipart_form_data(): +@pytest.mark.parametrize( + 'payload', [ + '------sanic\r\n' \ + 'Content-Disposition: form-data; name="test"\r\n' \ + '\r\n' \ + 'OK\r\n' \ + '------sanic--\r\n', + '------sanic\r\n' \ + 'content-disposition: form-data; name="test"\r\n' \ + '\r\n' \ + 'OK\r\n' \ + '------sanic--\r\n', + ]) +def test_post_form_multipart_form_data(payload): app = Sanic('test_post_form_multipart_form_data') @app.route('/', methods=['POST']) async def handler(request): return text('OK') - payload = '------sanic\r\n' \ - 'Content-Disposition: form-data; name="test"\r\n' \ - '\r\n' \ - 'OK\r\n' \ - '------sanic--\r\n' - headers = {'content-type': 'multipart/form-data; boundary=----sanic'} request, response = app.test_client.post(data=payload, headers=headers) From 235687d98344b4090c7766de65a34770e2c8f6ab Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Mon, 10 Jul 2017 12:37:21 -0700 Subject: [PATCH 2/2] should call lower just once --- sanic/request.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sanic/request.py b/sanic/request.py index da9a8cd0..d8674c48 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -238,15 +238,15 @@ def parse_multipart_form(body, boundary): break colon_index = form_line.index(':') - form_header_field = form_line[0:colon_index] + form_header_field = form_line[0:colon_index].lower() form_header_value, form_parameters = parse_header( form_line[colon_index + 2:]) - if form_header_field.lower() == 'content-disposition': + if form_header_field == 'content-disposition': if 'filename' in form_parameters: file_name = form_parameters['filename'] field_name = form_parameters.get('name') - elif form_header_field.lower() == 'content-type': + elif form_header_field == 'content-type': file_type = form_header_value post_data = form_part[line_index:-4]