Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5956 -- Added a better error description for non-ASCII HTTP he…

…aders. Patch from jvloothuis.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6927 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 03f1eb23e5c39e130744f885d480b2aa70d93088 1 parent 1fcb4e4
Malcolm Tredinnick authored
13  django/http/__init__.py
@@ -277,7 +277,20 @@ def __str__(self):
277 277
             for key, value in self._headers.values()]) \
278 278
             + '\n\n' + self.content
279 279
 
  280
+    def _convert_to_ascii(self, *values):
  281
+        "Convert all values to ascii strings"
  282
+        for value in values:
  283
+            if isinstance(value, unicode):
  284
+                try:
  285
+                    yield value.encode('us-ascii')
  286
+                except UnicodeError, e:
  287
+                    e.reason += ', HTTP response headers must be in US-ASCII format'
  288
+                    raise
  289
+            else:
  290
+                yield str(value)
  291
+
280 292
     def __setitem__(self, header, value):
  293
+        header, value = self._convert_to_ascii(header, value)
281 294
         self._headers[header.lower()] = (header, value)
282 295
 
283 296
     def __delitem__(self, header):
36  tests/regressiontests/httpwrappers/tests.py
@@ -391,9 +391,43 @@
391 391
 >>> q.getlist('foo')
392 392
 [u'bar', u'\ufffd']
393 393
 
  394
+
  395
+###################################### 
  396
+# HttpResponse with Unicode headers  # 
  397
+###################################### 
  398
+ 
  399
+>>> r = HttpResponse() 
  400
+ 
  401
+If we insert a unicode value it will be converted to an ascii
  402
+string. This makes sure we comply with the HTTP specifications.
  403
+ 
  404
+>>> r['value'] = u'test value' 
  405
+>>> isinstance(r['value'], str) 
  406
+True
  407
+
  408
+An error is raised When a unicode object with non-ascii is assigned.
  409
+
  410
+>>> r['value'] = u't\xebst value' # doctest:+ELLIPSIS
  411
+Traceback (most recent call last):
  412
+...
  413
+UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
  414
+ 
  415
+The response also converts unicode keys to strings. 
  416
+ 
  417
+>>> r[u'test'] = 'testing key' 
  418
+>>> list(sorted(r.items()))[1]
  419
+('test', 'testing key')
  420
+
  421
+It will also raise errors for keys with non-ascii data.
  422
+
  423
+>>> r[u't\xebst'] = 'testing key'  # doctest:+ELLIPSIS
  424
+Traceback (most recent call last):
  425
+...
  426
+UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
  427
+ 
394 428
 """
395 429
 
396  
-from django.http import QueryDict
  430
+from django.http import QueryDict, HttpResponse
397 431
 
398 432
 if __name__ == "__main__":
399 433
     import doctest

0 notes on commit 03f1eb2

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