Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.6.x] Fixed #21282 -- Made HttpResponse.serialize_headers accept la…

…tin-1

Thanks Raphaël Barrois for the report and the initial patch and
Aymeric Augustin for the review.

Backport of a14f087 from master.
  • Loading branch information...
commit b2f9c74ed1cd246022ab52d239eeb33f950dcc70 1 parent 1241a2c
Claude Paroz authored October 17, 2013
9  django/http/response.py
@@ -128,8 +128,11 @@ def __init__(self, content_type=None, status=None, reason=None, mimetype=None):
128 128
 
129 129
     def serialize_headers(self):
130 130
         """HTTP headers as a bytestring."""
  131
+        def to_bytes(val, encoding):
  132
+            return val if isinstance(val, bytes) else val.encode(encoding)
  133
+
131 134
         headers = [
132  
-            ('%s: %s' % (key, value)).encode('us-ascii')
  135
+            (b': '.join([to_bytes(key, 'ascii'), to_bytes(value, 'latin-1')]))
133 136
             for key, value in self._headers.values()
134 137
         ]
135 138
         return b'\r\n'.join(headers)
@@ -140,7 +143,7 @@ def serialize_headers(self):
140 143
         __str__ = serialize_headers
141 144
 
142 145
     def _convert_to_charset(self, value, charset, mime_encode=False):
143  
-        """Converts headers key/value to ascii/latin1 native strings.
  146
+        """Converts headers key/value to ascii/latin-1 native strings.
144 147
 
145 148
         `charset` must be 'ascii' or 'latin-1'. If `mime_encode` is True and
146 149
         `value` value can't be represented in the given charset, MIME-encoding
@@ -176,7 +179,7 @@ def _convert_to_charset(self, value, charset, mime_encode=False):
176 179
 
177 180
     def __setitem__(self, header, value):
178 181
         header = self._convert_to_charset(header, 'ascii')
179  
-        value = self._convert_to_charset(value, 'latin1', mime_encode=True)
  182
+        value = self._convert_to_charset(value, 'latin-1', mime_encode=True)
180 183
         self._headers[header.lower()] = (header, value)
181 184
 
182 185
     def __delitem__(self, header):
3  tests/httpwrappers/tests.py
@@ -254,6 +254,7 @@ def test_headers_type(self):
254 254
         r['key'] = 'test'.encode('ascii')
255 255
         self.assertEqual(r['key'], str('test'))
256 256
         self.assertIsInstance(r['key'], str)
  257
+        self.assertIn(b'test', r.serialize_headers())
257 258
 
258 259
         # Latin-1 unicode or bytes values are also converted to native strings.
259 260
         r['key'] = 'café'
@@ -262,11 +263,13 @@ def test_headers_type(self):
262 263
         r['key'] = 'café'.encode('latin-1')
263 264
         self.assertEqual(r['key'], smart_str('café', 'latin-1'))
264 265
         self.assertIsInstance(r['key'], str)
  266
+        self.assertIn('café'.encode('latin-1'), r.serialize_headers())
265 267
 
266 268
         # Other unicode values are MIME-encoded (there's no way to pass them as bytes).
267 269
         r['key'] = '†'
268 270
         self.assertEqual(r['key'], str('=?utf-8?b?4oCg?='))
269 271
         self.assertIsInstance(r['key'], str)
  272
+        self.assertIn(b'=?utf-8?b?4oCg?=', r.serialize_headers())
270 273
 
271 274
         # The response also converts unicode or bytes keys to strings, but requires
272 275
         # them to contain ASCII

0 notes on commit b2f9c74

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