Ticket #29323: 29323.diff

File 29323.diff, 2.7 KB (added by Claude Paroz, 7 years ago)

Let bytes be bytes in QueryDict

  • django/http/request.py

    diff --git a/django/http/request.py b/django/http/request.py
    index f7f89913ef..51a7e6c4f1 100644
    a b class QueryDict(MultiValueDict):  
    416416        """
    417417        q = cls('', mutable=True, encoding=encoding)
    418418        for key in iterable:
    419             q.appendlist(key, value)
     419            q.appendlist(key, bytes_to_text(value, encoding))
    420420        if not mutable:
    421421            q._mutable = False
    422422        return q
    class QueryDict(MultiValueDict):  
    471471    def appendlist(self, key, value):
    472472        self._assert_mutable()
    473473        key = bytes_to_text(key, self.encoding)
    474         value = bytes_to_text(value, self.encoding)
    475474        super().appendlist(key, value)
    476475
    477476    def pop(self, key, *args):
  • django/utils/http.py

    diff --git a/django/utils/http.py b/django/utils/http.py
    index 4558c6874a..340f64c590 100644
    a b from binascii import Error as BinasciiError  
    88from email.utils import formatdate
    99from urllib.parse import (
    1010    ParseResult, SplitResult, _coerce_args, _splitnetloc, _splitparams, quote,
    11     quote_plus, scheme_chars, unquote, unquote_plus,
     11    quote_plus, scheme_chars, unquote, unquote_plus, unquote_to_bytes,
    1212    urlencode as original_urlencode, uses_params,
    1313)
    1414
    def limited_parse_qsl(qs, keep_blank_values=False, encoding='utf-8',  
    430430            name = nv[0].replace('+', ' ')
    431431            name = unquote(name, encoding=encoding, errors=errors)
    432432            value = nv[1].replace('+', ' ')
    433             value = unquote(value, encoding=encoding, errors=errors)
     433            try:
     434                value = unquote(value, encoding=encoding, errors='strict')
     435            except UnicodeDecodeError:
     436                value = unquote_to_bytes(value)
    434437            r.append((name, value))
    435438    return r
  • tests/requests/tests.py

    diff --git a/tests/requests/tests.py b/tests/requests/tests.py
    index e1c25a2f6d..7cdc69fe9d 100644
    a b class RequestsTests(SimpleTestCase):  
    380380        self.assertEqual(request.FILES, {})
    381381        self.assertEqual(request.body, payload)
    382382
     383    def test_POST_binary_form_urlencoded(self):
     384        payload = b'foo=%00%7f%80%ff&bar=value'
     385        environ = {'REQUEST_METHOD': 'POST',
     386                   'CONTENT_TYPE': 'application/x-www-form-urlencoded',
     387                   'CONTENT_LENGTH': len(payload),
     388                   'wsgi.input': BytesIO(payload)}
     389        request = WSGIRequest(environ)
     390        self.assertEqual(request.POST, {'foo': [b'\x00\x7f\x80\xff'], 'bar': ['value']})
     391
    383392    def test_read_by_lines(self):
    384393        payload = FakePayload('name=value')
    385394        request = WSGIRequest({'REQUEST_METHOD': 'POST',
Back to Top