Ticket #8765: 8765.diff

File 8765.diff, 1.6 KB (added by arien, 6 years ago)

make sure any content_type (or mimetype) passed to HttpResponse is properly encoded. (Updated patch that is more explicit and with minimal changes to code.)

  • django/http/__init__.py

     
    289289
    290290        # _headers is a mapping of the lower-case name to the original case of
    291291        # the header (required for working with legacy systems) and the header
    292         # value.
    293         self._headers = {'content-type': ('Content-Type', content_type)}
     292        # value.  Both the name of the header and its value are ascii strings.
     293        self._headers = {}
     294        self.__setitem__('Content-Type', content_type)
    294295
    295296    def __str__(self):
    296297        """Full HTTP message, including headers."""
  • tests/regressiontests/httpwrappers/tests.py

     
    437437UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
    438438
    439439#
     440# Regression test for #8765: Content-Types passed to HttpResponse() are also
     441# converted to ascii strings and an exception is raised when a non-ascii
     442# value is used.
     443#
     444>>> r = HttpResponse(content_type=u'text/plain')
     445>>> isinstance(r['Content-Type'], str)
     446True
     447>>> r = HttpResponse(content_type=u'text/pl\xc3\xa6in') # doctest:+ELLIPSIS
     448Traceback (most recent call last):
     449...
     450UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
     451
     452#
    440453# Regression test for #8278: QueryDict.update(QueryDict)
    441454#
    442455>>> x = QueryDict("a=1&a=2", mutable=True)
Back to Top