Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[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
  • Loading branch information...
commit 4eb78b89ce198dc27daea4bbe1ab71ba4bd5cb89 1 parent dc00873
Malcolm Tredinnick authored September 11, 2010
12  django/http/__init__.py
@@ -303,13 +303,16 @@ class HttpResponse(object):
303 303
 
304 304
     def __init__(self, content='', mimetype=None, status=None,
305 305
             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 = {}
307 310
         self._charset = settings.DEFAULT_CHARSET
308 311
         if mimetype:
309 312
             content_type = mimetype     # For backwards compatibility
310 313
         if not content_type:
311 314
             content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
312  
-                    settings.DEFAULT_CHARSET)
  315
+                    self._charset)
313 316
         if not isinstance(content, basestring) and hasattr(content, '__iter__'):
314 317
             self._container = content
315 318
             self._is_string = False
@@ -320,10 +323,7 @@ def __init__(self, content='', mimetype=None, status=None,
320 323
         if status:
321 324
             self.status_code = status
322 325
 
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
327 327
 
328 328
     def __str__(self):
329 329
         """Full HTTP message, including headers."""
11  tests/regressiontests/httpwrappers/tests.py
@@ -204,9 +204,18 @@ def test_unicode_headers(self):
204 204
         r['value'] = u'test value'
205 205
         self.failUnless(isinstance(r['value'], str))
206 206
         
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.
208 208
         self.assertRaises(UnicodeEncodeError, r.__setitem__, 'value', u't\xebst value')
209 209
         
  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
+
210 219
         # The response also converts unicode keys to strings.)      
211 220
         r[u'test'] = 'testing key'
212 221
         l = list(r.items())

0 notes on commit 4eb78b8

Please sign in to comment.
Something went wrong with that request. Please try again.