Ticket #5076: django_request_charset.diff

File django_request_charset.diff, 2.6 KB (added by dbr <daniel@…>, 17 years ago)

A first try at a fix

  • 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        ct = self.META['CONTENT_TYPE']
     56        ct.lower()
     57        ct.replace(' ', '')
     58        i = ct.find('charset')
     59        if i > 0:
     60            self._encoding = ct[i+8:]
     61
     62       
    5063    def _set_encoding(self, val):
    5164        """
    5265        Sets the encoding used for GET/POST accesses. If the GET or POST
     
    112125            encoding = settings.DEFAULT_CHARSET
    113126        self.encoding = encoding
    114127        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'))
     128
     129        try:
     130            for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True
     131                self.appendlist(force_unicode(key, encoding, errors='strict'), force_unicode(value, encoding, errors='strict'))
     132        except UnicodeDecodeError:
     133            # cp1252 (Windows-1252) is ISO-8850-1 (latin-1) with additional
     134            # common characters defined, this is chosen over latin-1 since
     135            # most browsers say latin-1 when they really use Windows-1252
     136            encoding = 'cp1252'
     137            self.encoding = encoding
     138            for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True
     139                self.appendlist(force_unicode(key, encoding, errors='replace'), force_unicode(value, encoding, errors='replace'))
    117140        self._mutable = mutable
    118 
     141       
    119142    def _assert_mutable(self):
    120143        if not self._mutable:
    121144            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