Skip to content

Commit 4eb78b8

Browse files
committed
[1.2.X] Improved unicode-type, ASCII-convertible header handling in
HttpResponse. Fixed #8765. Thanks to SmileyChris and semenov for working on this one. Backport of r13740 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@13748 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent dc00873 commit 4eb78b8

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

django/http/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,16 @@ class HttpResponse(object):
303303

304304
def __init__(self, content='', mimetype=None, status=None,
305305
content_type=None):
306-
from django.conf import settings
306+
# _headers is a mapping of the lower-case name to the original case of
307+
# the header (required for working with legacy systems) and the header
308+
# value. Both the name of the header and its value are ASCII strings.
309+
self._headers = {}
307310
self._charset = settings.DEFAULT_CHARSET
308311
if mimetype:
309312
content_type = mimetype # For backwards compatibility
310313
if not content_type:
311314
content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
312-
settings.DEFAULT_CHARSET)
315+
self._charset)
313316
if not isinstance(content, basestring) and hasattr(content, '__iter__'):
314317
self._container = content
315318
self._is_string = False
@@ -320,10 +323,7 @@ def __init__(self, content='', mimetype=None, status=None,
320323
if status:
321324
self.status_code = status
322325

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

328328
def __str__(self):
329329
"""Full HTTP message, including headers."""

tests/regressiontests/httpwrappers/tests.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,18 @@ def test_unicode_headers(self):
204204
r['value'] = u'test value'
205205
self.failUnless(isinstance(r['value'], str))
206206

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

210+
# An error is raised when a unicode object with non-ASCII format is
211+
# passed as initial mimetype or content_type.
212+
self.assertRaises(UnicodeEncodeError, HttpResponse,
213+
mimetype=u't\xebst value')
214+
215+
# HttpResponse headers must be convertible to ASCII.
216+
self.assertRaises(UnicodeEncodeError, HttpResponse,
217+
content_type=u't\xebst value')
218+
210219
# The response also converts unicode keys to strings.)
211220
r[u'test'] = 'testing key'
212221
l = list(r.items())

0 commit comments

Comments
 (0)