Author: mtredinnick
Date: 2010-09-10 20:41:53 -0500 (Fri, 10 Sep 2010)
New Revision: 13740

Modified:
   django/trunk/django/http/__init__.py
   django/trunk/tests/regressiontests/httpwrappers/tests.py
Log:
Improved unicode-type, ASCII-convertible header handling in
HttpResponse.

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

Modified: django/trunk/django/http/__init__.py
===================================================================
--- django/trunk/django/http/__init__.py        2010-09-11 01:39:16 UTC (rev 
13739)
+++ django/trunk/django/http/__init__.py        2010-09-11 01:41:53 UTC (rev 
13740)
@@ -303,13 +303,16 @@
 
     def __init__(self, content='', mimetype=None, status=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
         if mimetype:
             content_type = mimetype     # For backwards compatibility
         if not 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__'):
             self._container = content
             self._is_string = False
@@ -320,10 +323,7 @@
         if status:
             self.status_code = status
 
-        # _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.
-        self._headers = {'content-type': ('Content-Type', content_type)}
+        self['Content-Type'] = content_type
 
     def __str__(self):
         """Full HTTP message, including headers."""

Modified: django/trunk/tests/regressiontests/httpwrappers/tests.py
===================================================================
--- django/trunk/tests/regressiontests/httpwrappers/tests.py    2010-09-11 
01:39:16 UTC (rev 13739)
+++ django/trunk/tests/regressiontests/httpwrappers/tests.py    2010-09-11 
01:41:53 UTC (rev 13740)
@@ -204,9 +204,18 @@
         r['value'] = u'test value'
         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')
         
+        # 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.)      
         r[u'test'] = 'testing key'
         l = list(r.items())

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to