From eb24b546340d500502278bd124dbd527963ca4c4 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Sat, 7 May 2011 16:59:16 +0000 Subject: [PATCH] Fixed #15496 -- Corrected handling of base64 file upload encoding. Thanks, gene and Claude Paroz. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16176 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/http/multipartparser.py | 2 ++ tests/regressiontests/file_uploads/tests.py | 25 +++++++++++++++++++++ tests/regressiontests/file_uploads/urls.py | 1 + tests/regressiontests/file_uploads/views.py | 7 ++++++ 4 files changed, 35 insertions(+) diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py index e45d5d1035..ae143f79ed 100644 --- a/django/http/multipartparser.py +++ b/django/http/multipartparser.py @@ -145,6 +145,8 @@ class MultiPartParser(object): continue transfer_encoding = meta_data.get('content-transfer-encoding') + if transfer_encoding is not None: + transfer_encoding = transfer_encoding[0].strip() field_name = force_unicode(field_name, encoding, errors='replace') if item_type == FIELD: diff --git a/tests/regressiontests/file_uploads/tests.py b/tests/regressiontests/file_uploads/tests.py index 5da0a5fafc..3c126b7d0f 100644 --- a/tests/regressiontests/file_uploads/tests.py +++ b/tests/regressiontests/file_uploads/tests.py @@ -1,5 +1,6 @@ #! -*- coding: utf-8 -*- +import base64 import errno import hashlib import os @@ -56,6 +57,30 @@ class FileUploadTests(TestCase): self.assertEqual(response.status_code, 200) + def test_base64_upload(self): + test_string = "This data will be transmitted base64-encoded." + payload = "\r\n".join([ + '--' + client.BOUNDARY, + 'Content-Disposition: form-data; name="file"; filename="test.txt"', + 'Content-Type: application/octet-stream', + 'Content-Transfer-Encoding: base64', + '', + base64.b64encode(test_string), + '--' + client.BOUNDARY + '--', + '', + ]) + r = { + 'CONTENT_LENGTH': len(payload), + 'CONTENT_TYPE': client.MULTIPART_CONTENT, + 'PATH_INFO': "/file_uploads/echo_content/", + 'REQUEST_METHOD': 'POST', + 'wsgi.input': client.FakePayload(payload), + } + response = self.client.request(**r) + received = simplejson.loads(response.content) + + self.assertEqual(received['file'], test_string) + def test_unicode_file_name(self): tdir = tempfile.gettempdir() diff --git a/tests/regressiontests/file_uploads/urls.py b/tests/regressiontests/file_uploads/urls.py index 413080eb4f..9f814c4d4c 100644 --- a/tests/regressiontests/file_uploads/urls.py +++ b/tests/regressiontests/file_uploads/urls.py @@ -6,6 +6,7 @@ urlpatterns = patterns('', (r'^verify/$', views.file_upload_view_verify), (r'^unicode_name/$', views.file_upload_unicode_name), (r'^echo/$', views.file_upload_echo), + (r'^echo_content/$', views.file_upload_echo_content), (r'^quota/$', views.file_upload_quota), (r'^quota/broken/$', views.file_upload_quota_broken), (r'^getlist_count/$', views.file_upload_getlist_count), diff --git a/tests/regressiontests/file_uploads/views.py b/tests/regressiontests/file_uploads/views.py index 0fd0b6502d..dba7522bae 100644 --- a/tests/regressiontests/file_uploads/views.py +++ b/tests/regressiontests/file_uploads/views.py @@ -85,6 +85,13 @@ def file_upload_echo(request): r = dict([(k, f.name) for k, f in request.FILES.items()]) return HttpResponse(simplejson.dumps(r)) +def file_upload_echo_content(request): + """ + Simple view to echo back the content of uploaded files for tests. + """ + r = dict([(k, f.read()) for k, f in request.FILES.items()]) + return HttpResponse(simplejson.dumps(r)) + def file_upload_quota(request): """ Dynamically add in an upload handler.