Skip to content

Commit

Permalink
Improved unicode-type, ASCII-convertible header handling in
Browse files Browse the repository at this point in the history
HttpResponse.

Fixed #8765. Thanks to SmileyChris and semenov for working on this one.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13740 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed Sep 11, 2010
1 parent 3444b06 commit b2d2cb4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
12 changes: 6 additions & 6 deletions django/http/__init__.py
Expand Up @@ -303,13 +303,16 @@ class HttpResponse(object):


def __init__(self, content='', mimetype=None, status=None, def __init__(self, content='', mimetype=None, status=None,
content_type=None): content_type=None):
from django.conf import settings # _headers is a mapping of the lower-case name to the original case of
# the header (required for working with legacy systems) and the header
# value. Both the name of the header and its value are ASCII strings.
self._headers = {}
self._charset = settings.DEFAULT_CHARSET self._charset = settings.DEFAULT_CHARSET
if mimetype: if mimetype:
content_type = mimetype # For backwards compatibility content_type = mimetype # For backwards compatibility
if not content_type: if not content_type:
content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
settings.DEFAULT_CHARSET) self._charset)
if not isinstance(content, basestring) and hasattr(content, '__iter__'): if not isinstance(content, basestring) and hasattr(content, '__iter__'):
self._container = content self._container = content
self._is_string = False self._is_string = False
Expand All @@ -320,10 +323,7 @@ def __init__(self, content='', mimetype=None, status=None,
if status: if status:
self.status_code = status self.status_code = status


# _headers is a mapping of the lower-case name to the original case of self['Content-Type'] = content_type
# the header (required for working with legacy systems) and the header
# value.
self._headers = {'content-type': ('Content-Type', content_type)}


def __str__(self): def __str__(self):
"""Full HTTP message, including headers.""" """Full HTTP message, including headers."""
Expand Down
11 changes: 10 additions & 1 deletion tests/regressiontests/httpwrappers/tests.py
Expand Up @@ -204,9 +204,18 @@ def test_unicode_headers(self):
r['value'] = u'test value' r['value'] = u'test value'
self.failUnless(isinstance(r['value'], str)) self.failUnless(isinstance(r['value'], str))


# An error is raised When a unicode object with non-ascii is assigned. # An error is raised ~hen a unicode object with non-ascii is assigned.
self.assertRaises(UnicodeEncodeError, r.__setitem__, 'value', u't\xebst value') self.assertRaises(UnicodeEncodeError, r.__setitem__, 'value', u't\xebst value')


# An error is raised when a unicode object with non-ASCII format is
# passed as initial mimetype or content_type.
self.assertRaises(UnicodeEncodeError, HttpResponse,
mimetype=u't\xebst value')

# HttpResponse headers must be convertible to ASCII.
self.assertRaises(UnicodeEncodeError, HttpResponse,
content_type=u't\xebst value')

# The response also converts unicode keys to strings.) # The response also converts unicode keys to strings.)
r[u'test'] = 'testing key' r[u'test'] = 'testing key'
l = list(r.items()) l = list(r.items())
Expand Down

0 comments on commit b2d2cb4

Please sign in to comment.