Code

Ticket #8765: 8765.diff

File 8765.diff, 1.6 KB (added by arien, 5 years ago)

make sure any content_type (or mimetype) passed to HttpResponse is properly encoded. (Updated patch that is more explicit and with minimal changes to code.)

Line 
1Index: django/http/__init__.py
2===================================================================
3--- django/http/__init__.py     (revision 9977)
4+++ django/http/__init__.py     (working copy)
5@@ -289,8 +289,9 @@
6 
7         # _headers is a mapping of the lower-case name to the original case of
8         # the header (required for working with legacy systems) and the header
9-        # value.
10-        self._headers = {'content-type': ('Content-Type', content_type)}
11+        # value.  Both the name of the header and its value are ascii strings.
12+        self._headers = {}
13+        self.__setitem__('Content-Type', content_type)
14 
15     def __str__(self):
16         """Full HTTP message, including headers."""
17Index: tests/regressiontests/httpwrappers/tests.py
18===================================================================
19--- tests/regressiontests/httpwrappers/tests.py (revision 9977)
20+++ tests/regressiontests/httpwrappers/tests.py (working copy)
21@@ -437,6 +437,19 @@
22 UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
23 
24 #
25+# Regression test for #8765: Content-Types passed to HttpResponse() are also
26+# converted to ascii strings and an exception is raised when a non-ascii
27+# value is used.
28+#
29+>>> r = HttpResponse(content_type=u'text/plain')
30+>>> isinstance(r['Content-Type'], str)
31+True
32+>>> r = HttpResponse(content_type=u'text/pl\xc3\xa6in') # doctest:+ELLIPSIS
33+Traceback (most recent call last):
34+...
35+UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
36+
37+#
38 # Regression test for #8278: QueryDict.update(QueryDict)
39 #
40 >>> x = QueryDict("a=1&a=2", mutable=True)