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
This commit is contained in:
Jannis Leidel 2011-05-07 16:59:16 +00:00
parent 02b837d38a
commit eb24b54634
4 changed files with 35 additions and 0 deletions

View File

@ -145,6 +145,8 @@ class MultiPartParser(object):
continue continue
transfer_encoding = meta_data.get('content-transfer-encoding') 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') field_name = force_unicode(field_name, encoding, errors='replace')
if item_type == FIELD: if item_type == FIELD:

View File

@ -1,5 +1,6 @@
#! -*- coding: utf-8 -*- #! -*- coding: utf-8 -*-
import base64
import errno import errno
import hashlib import hashlib
import os import os
@ -56,6 +57,30 @@ class FileUploadTests(TestCase):
self.assertEqual(response.status_code, 200) 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): def test_unicode_file_name(self):
tdir = tempfile.gettempdir() tdir = tempfile.gettempdir()

View File

@ -6,6 +6,7 @@ urlpatterns = patterns('',
(r'^verify/$', views.file_upload_view_verify), (r'^verify/$', views.file_upload_view_verify),
(r'^unicode_name/$', views.file_upload_unicode_name), (r'^unicode_name/$', views.file_upload_unicode_name),
(r'^echo/$', views.file_upload_echo), (r'^echo/$', views.file_upload_echo),
(r'^echo_content/$', views.file_upload_echo_content),
(r'^quota/$', views.file_upload_quota), (r'^quota/$', views.file_upload_quota),
(r'^quota/broken/$', views.file_upload_quota_broken), (r'^quota/broken/$', views.file_upload_quota_broken),
(r'^getlist_count/$', views.file_upload_getlist_count), (r'^getlist_count/$', views.file_upload_getlist_count),

View File

@ -85,6 +85,13 @@ def file_upload_echo(request):
r = dict([(k, f.name) for k, f in request.FILES.items()]) r = dict([(k, f.name) for k, f in request.FILES.items()])
return HttpResponse(simplejson.dumps(r)) 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): def file_upload_quota(request):
""" """
Dynamically add in an upload handler. Dynamically add in an upload handler.