Ticket #5076: django_request_charset.2.diff

File django_request_charset.2.diff, 2.6 KB (added by dbr <daniel@…>, 8 years ago)
  • django/http/__init__.py

     
    4747    def is_secure(self):
    4848        return os.environ.get("HTTPS") == "on"
    4949
     50    def detect_charset(self):
     51        """
     52        Try to determine the encoding of the data by examining the
     53        Content-Type header. To be called by classes inheriting HttpRequest.
     54        """
     55        if self.META.has_key('CONTENT_TYPE'):
     56            ct = self.META['CONTENT_TYPE']
     57            ct.lower()
     58            ct.replace(' ', '')
     59            i = ct.find('charset')
     60            if i > 0:
     61                self._encoding = ct[i+8:]
     62
     63       
    5064    def _set_encoding(self, val):
    5165        """
    5266        Sets the encoding used for GET/POST accesses. If the GET or POST
     
    112126            encoding = settings.DEFAULT_CHARSET
    113127        self.encoding = encoding
    114128        self._mutable = True
    115         for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True
    116             self.appendlist(force_unicode(key, encoding, errors='replace'), force_unicode(value, encoding, errors='replace'))
     129
     130        try:
     131            for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True
     132                self.appendlist(force_unicode(key, encoding, errors='strict'), force_unicode(value, encoding, errors='strict'))
     133        except UnicodeDecodeError:
     134            # cp1252 (Windows-1252) is ISO-8850-1 (latin-1) with additional
     135            # common characters defined, this is chosen over latin-1 since
     136            # most browsers say latin-1 when they really use Windows-1252
     137            encoding = 'cp1252'
     138            self.encoding = encoding
     139            for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True
     140                self.appendlist(force_unicode(key, encoding, errors='replace'), force_unicode(value, encoding, errors='replace'))
    117141        self._mutable = mutable
    118 
     142       
    119143    def _assert_mutable(self):
    120144        if not self._mutable:
    121145            raise AttributeError, "This QueryDict instance is immutable"
  • django/core/handlers/wsgi.py

     
    7777        self.path = force_unicode(environ['PATH_INFO'])
    7878        self.META = environ
    7979        self.method = environ['REQUEST_METHOD'].upper()
     80        self.detect_charset()
    8081
    8182    def __repr__(self):
    8283        # Since this is called as part of error handling, we need to be very
Back to Top