Skip to content

Commit

Permalink
[1.3.x] Fixed #15496 -- Corrected handling of base64 file upload enco…
Browse files Browse the repository at this point in the history
…ding. Backport of r16176 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.3.X@17546 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
aaugustin committed Feb 18, 2012
1 parent c63a454 commit 813dc01
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions django/http/multipartparser.py
Expand Up @@ -150,6 +150,8 @@ def parse(self):
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:
Expand Down
25 changes: 25 additions & 0 deletions tests/regressiontests/file_uploads/tests.py
@@ -1,5 +1,6 @@
#! -*- coding: utf-8 -*-
import errno
import base64
import os
import shutil
from StringIO import StringIO
Expand Down Expand Up @@ -55,6 +56,30 @@ def test_large_upload(self):

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()

Expand Down
1 change: 1 addition & 0 deletions tests/regressiontests/file_uploads/urls.py
Expand Up @@ -6,6 +6,7 @@
(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),
Expand Down
7 changes: 7 additions & 0 deletions tests/regressiontests/file_uploads/views.py
Expand Up @@ -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.
Expand Down

0 comments on commit 813dc01

Please sign in to comment.