[1.3.x] Fixed #15496 -- Corrected handling of base64 file upload encoding. 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
This commit is contained in:
Aymeric Augustin 2012-02-18 10:11:17 +00:00
parent c63a454bb6
commit 813dc01cd8
4 changed files with 35 additions and 0 deletions

View File

@ -150,6 +150,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:

View File

@ -1,5 +1,6 @@
#! -*- coding: utf-8 -*-
import errno
import base64
import os
import shutil
from StringIO import StringIO
@ -55,6 +56,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()

View File

@ -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),

View File

@ -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.