Ticket #19036: 19036-1.diff

File 19036-1.diff, 2.5 KB (added by claudep, 2 years ago)

Decoding base64 by multiple of 4

  • django/http/multipartparser.py

    diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py
    index 40aefd6..156124a 100644
    a b class MultiPartParser(object): 
    199199                        for chunk in field_stream:
    200200                            if transfer_encoding == 'base64':
    201201                                # We only special-case base64 transfer encoding
     202                                # We should always read base64 streams by multiple of 4
     203                                over_bytes = len(chunk) % 4
     204                                if over_bytes:
     205                                    over_chunk = field_stream.read(4 - over_bytes)
     206                                    chunk += over_chunk
     207
    202208                                try:
    203209                                    chunk = base64.b64decode(chunk)
    204210                                except Exception as e:
  • tests/regressiontests/file_uploads/tests.py

    diff --git a/tests/regressiontests/file_uploads/tests.py b/tests/regressiontests/file_uploads/tests.py
    index 2a1ec7d..4ee7c18 100644
    a b class FileUploadTests(TestCase): 
    6060
    6161        self.assertEqual(response.status_code, 200)
    6262
    63     def test_base64_upload(self):
    64         test_string = "This data will be transmitted base64-encoded."
     63    def _test_base64_upload(self, content):
    6564        payload = "\r\n".join([
    6665            '--' + client.BOUNDARY,
    6766            'Content-Disposition: form-data; name="file"; filename="test.txt"',
    6867            'Content-Type: application/octet-stream',
    6968            'Content-Transfer-Encoding: base64',
    7069            '',
    71             base64.b64encode(force_bytes(test_string)).decode('ascii'),
     70            base64.b64encode(force_bytes(content)).decode('ascii'),
    7271            '--' + client.BOUNDARY + '--',
    7372            '',
    7473        ]).encode('utf-8')
    class FileUploadTests(TestCase): 
    8281        response = self.client.request(**r)
    8382        received = json.loads(response.content.decode('utf-8'))
    8483
    85         self.assertEqual(received['file'], test_string)
     84        self.assertEqual(received['file'], content)
     85
     86    def test_base64_upload(self):
     87        self._test_base64_upload("This data will be transmitted base64-encoded.")
     88
     89    def test_big_base64_upload(self):
     90        self._test_base64_upload("Big data" * 68000)  # > 512Kb
    8691
    8792    def test_unicode_file_name(self):
    8893        tdir = tempfile.gettempdir()
Back to Top