Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15496 -- Corrected handling of base64 file upload encoding. Th…

…anks, gene and Claude Paroz.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16176 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit eb24b546340d500502278bd124dbd527963ca4c4 1 parent 02b837d
Jannis Leidel authored May 07, 2011
2  django/http/multipartparser.py
@@ -145,6 +145,8 @@ def parse(self):
145 145
                     continue
146 146
 
147 147
                 transfer_encoding = meta_data.get('content-transfer-encoding')
  148
+                if transfer_encoding is not None:
  149
+                    transfer_encoding = transfer_encoding[0].strip()
148 150
                 field_name = force_unicode(field_name, encoding, errors='replace')
149 151
 
150 152
                 if item_type == FIELD:
25  tests/regressiontests/file_uploads/tests.py
... ...
@@ -1,5 +1,6 @@
1 1
 #! -*- coding: utf-8 -*-
2 2
 
  3
+import base64
3 4
 import errno
4 5
 import hashlib
5 6
 import os
@@ -56,6 +57,30 @@ def test_large_upload(self):
56 57
 
57 58
         self.assertEqual(response.status_code, 200)
58 59
 
  60
+    def test_base64_upload(self):
  61
+        test_string = "This data will be transmitted base64-encoded."
  62
+        payload = "\r\n".join([
  63
+            '--' + client.BOUNDARY,
  64
+            'Content-Disposition: form-data; name="file"; filename="test.txt"',
  65
+            'Content-Type: application/octet-stream',
  66
+            'Content-Transfer-Encoding: base64',
  67
+            '',
  68
+            base64.b64encode(test_string),
  69
+            '--' + client.BOUNDARY + '--',
  70
+            '',
  71
+        ])
  72
+        r = {
  73
+            'CONTENT_LENGTH': len(payload),
  74
+            'CONTENT_TYPE':   client.MULTIPART_CONTENT,
  75
+            'PATH_INFO':      "/file_uploads/echo_content/",
  76
+            'REQUEST_METHOD': 'POST',
  77
+            'wsgi.input':     client.FakePayload(payload),
  78
+        }
  79
+        response = self.client.request(**r)
  80
+        received = simplejson.loads(response.content)
  81
+
  82
+        self.assertEqual(received['file'], test_string)
  83
+
59 84
     def test_unicode_file_name(self):
60 85
         tdir = tempfile.gettempdir()
61 86
 
1  tests/regressiontests/file_uploads/urls.py
@@ -6,6 +6,7 @@
6 6
     (r'^verify/$',          views.file_upload_view_verify),
7 7
     (r'^unicode_name/$',    views.file_upload_unicode_name),
8 8
     (r'^echo/$',            views.file_upload_echo),
  9
+    (r'^echo_content/$',    views.file_upload_echo_content),
9 10
     (r'^quota/$',           views.file_upload_quota),
10 11
     (r'^quota/broken/$',    views.file_upload_quota_broken),
11 12
     (r'^getlist_count/$',   views.file_upload_getlist_count),
7  tests/regressiontests/file_uploads/views.py
@@ -85,6 +85,13 @@ def file_upload_echo(request):
85 85
     r = dict([(k, f.name) for k, f in request.FILES.items()])
86 86
     return HttpResponse(simplejson.dumps(r))
87 87
 
  88
+def file_upload_echo_content(request):
  89
+    """
  90
+    Simple view to echo back the content of uploaded files for tests.
  91
+    """
  92
+    r = dict([(k, f.read()) for k, f in request.FILES.items()])
  93
+    return HttpResponse(simplejson.dumps(r))
  94
+
88 95
 def file_upload_quota(request):
89 96
     """
90 97
     Dynamically add in an upload handler.

0 notes on commit eb24b54

Please sign in to comment.
Something went wrong with that request. Please try again.