﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
20889	HttpResponseBase._convert_to_charset complains about newline it inserted itself	mjl@…	Rik	"Consider the following snippet:


{{{
# coding=utf-8
from django.http import HttpResponse
h = HttpResponse()
f = 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz a\xcc\x88'.decode('utf-8')
h['Content-Disposition'] = u'attachment; filename=""%s""' % f
}}}

This contains a ""OSX-Style"" Umlaut (a + ¨), which triggers the ""convert to mime encoding"" path of _convert_to_charset. However, this string is so long that email.header.Header, which is used to mime-encode, will split the line. Later _convert_to_charset explodes because the header now contains a newline.


{{{
$ ./manage.py shell
Python 2.7.5 (default, Aug  1 2013, 01:01:17)
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type ""help"", ""copyright"", ""credits"" or ""license"" for more information.
(InteractiveConsole)
>>> import ttt
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""/Users/mjl/Projects/cms_sites/cc.intranet/cms/ttt.py"", line 5, in <module>
    h['Content-Disposition'] = u'attachment; filename=""%s""' % f
  File ""/Users/mjl/Projects/cms_sites/cc.intranet/lib/python2.7/site-packages/django/http/response.py"", line 110, in __setitem__
    value = self._convert_to_charset(value, 'latin1', mime_encode=True)
  File ""/Users/mjl/Projects/cms_sites/cc.intranet/lib/python2.7/site-packages/django/http/response.py"", line 105, in _convert_to_charset
    raise BadHeaderError(""Header values can't contain newlines (got %r)"" % value)
BadHeaderError: Header values can't contain newlines (got '=?utf-8?q?attachment=3B_filename=3D=22zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz_a?=\n =?utf-8?b?zIgi?=')
}}}

Solution: django/http/response.py#L163 should probably use

{{{
Header(value, 'utf-8', maxlinelen=1000)
}}}

to avoid generating continuation lines. Agreed, the ""1000"" is somewhat arbitrary, but realistically this fixes the problem.
"	Bug	closed	HTTP handling	dev	Normal	fixed	nlsprint14	poly gitaarik@…	Ready for checkin	1	0	0	0	0	0
