﻿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
29276	Compatibility of charset works not  well when there are non-ascii characters in HTTPResponse headers	sanjiely	nobody	"Python: 3.6.4
Django: 2.0.3
Browser: Chrome v65

To tell the browser to download a file, my code as below:

{{{
response = HttpResponse(my_data, content_type='text/plain')
response['Content-Disposition'] = 'attachment; filename=""中中foo.xls""'
}}}
(Note: there are non-ascii characters in the filename.)
My code worked well in Python2.7 and Django1.8.

But when I transform my version to Django v2 and Python v3. The behavior of the browser changed: Chrome opened my file directly and wouldn't download the file as an attachment any more.
I catch the network packet, and found the response headers become as:

{{{
Content-Disposition: =?utf-8?b?YXR0YWNobWVudDsgZmlsZW5hbWU9IuS4reS4rWZvby54bHMi?=
Content-Length: 13
Content-Type: text/plain
}}}

Very strange header ""Content-Disposition"", isn't it?
I guess there is an unexpected code path in the Django lib file ""response.py"".
I tracked it, and found the code running into an exception statement, line 125, file ""response.py""
 
{{{
       except UnicodeError as e:
            if mime_encode:
                print(value)
                value = Header(value, 'utf-8', maxlinelen=sys.maxsize).encode()
                print(value)
}}}
The output of these two print statement are:

{{{
attachment; filename=""中中foo.xls""
=?utf-8?b?YXR0YWNobWVudDsgZmlsZW5hbWU9IuS4reS4rWZvby54bHMi?=
}}}


Thanks."	Bug	new	HTTP handling	2.0	Normal		HTTPResponse header str bytes		Unreviewed	0	0	0	0	0	0
