Ticket #8765: 8765.2.diff

File 8765.2.diff, 3.0 KB (added by Chris Beaven, 15 years ago)
  • tests/regressiontests/httpwrappers/tests.py

    ### Eclipse Workspace Patch 1.0
    #P Django trunk
     
    429429...
    430430UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
    431431
     432An error is raised when a unicode object with non-ascii is set as the initial
     433mimetype or content_type.
     434
     435>>> HttpResponse(mimetype=u't\xebst value') # doctest:+ELLIPSIS
     436Traceback (most recent call last):
     437...
     438UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
     439>>> HttpResponse(content_type=u't\xebst value') # doctest:+ELLIPSIS
     440Traceback (most recent call last):
     441...
     442UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
     443
    432444The response also converts unicode keys to strings.
    433445
    434446>>> r[u'test'] = 'testing key'
  • django/http/__init__.py

     
    272272    status_code = 200
    273273
    274274    def __init__(self, content='', mimetype=None, status=None,
    275             content_type=None):
    276         from django.conf import settings
     275                 content_type=None):
     276        # _headers is a mapping of the lower-case name to the original case of
     277        # the header (required for working with legacy systems) and the header
     278        # value.  Both the name of the header and its value are ASCII strings.
     279        self._headers = {}
    277280        self._charset = settings.DEFAULT_CHARSET
    278         if mimetype:
    279             content_type = mimetype     # For backwards compatibility
    280         if not content_type:
    281             content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
    282                     settings.DEFAULT_CHARSET)
     281        self.cookies = SimpleCookie()
     282
    283283        if not isinstance(content, basestring) and hasattr(content, '__iter__'):
    284284            self._container = content
    285285            self._is_string = False
    286286        else:
    287287            self._container = [content]
    288288            self._is_string = True
    289         self.cookies = SimpleCookie()
     289
     290        if mimetype:
     291            content_type = mimetype     # For backwards compatibility
     292        if not content_type:
     293            content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
     294                                               self._charset)
     295        self['Content-Type'] = content_type
     296
    290297        if status:
    291298            self.status_code = status
    292299
    293         # _headers is a mapping of the lower-case name to the original case of
    294         # the header (required for working with legacy systems) and the header
    295         # value.
    296         self._headers = {'content-type': ('Content-Type', content_type)}
    297 
    298300    def __str__(self):
    299301        """Full HTTP message, including headers."""
    300302        return '\n'.join(['%s: %s' % (key, value)
Back to Top