[Django] #29276: Advise to turn the headers of HTTPResponse from str to bytes automatically

8 views
Skip to first unread message

Django

unread,
Mar 30, 2018, 6:11:24 AM3/30/18
to django-...@googlegroups.com
#29276: Advise to turn the headers of HTTPResponse from str to bytes automatically
-------------------------------------+-------------------------------------
Reporter: sanjiely | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: HTTP | Version: 2.0
handling | Keywords: HTTPResponse header
Severity: Normal | str bytes
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Python: 3.6.4
Django: 2.0.3

When I return a HttpResponse object, Django library will do the job for
me: turn my content from str to bytes silently.
But Django will not do the same work to the headers of HTTPResponse.

That is to say, the sample as below in the official document is not
precisely correct.
(Page: https://docs.djangoproject.com/en/2.0/ref/request-response/)
>>> response = HttpResponse(my_data, content_type='application/vnd.ms-
excel')
>>> response['Content-Disposition'] = 'attachment; filename="foo.xls"'

The precise usage should be:
>>> response['Content-Disposition'] = 'attachment;
filename="foo.xls"'.encode('utf-8')

Otherwise, everthing will out of control If I have a Chinse character in
the filename.
Wrong:
>>> response['Content-Disposition'] = 'attachment; filename="中中中中中
foo.xls"'
Correct:
>>> response['Content-Disposition'] = 'attachment; filename=中中中中中
foo.xls'.encode('utf-8')

Can we accept this ticket and do a minor improvement? Then we would
happily only handle str in Django other than str and bytes together.
Thanks.

Yong Li

--
Ticket URL: <https://code.djangoproject.com/ticket/29276>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 30, 2018, 6:14:14 AM3/30/18
to django-...@googlegroups.com
#29276: Suggest to turn the headers of HTTPResponse from str to bytes automatically

-------------------------------------+-------------------------------------
Reporter: sanjiely | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: HTTP handling | Version: 2.0
Severity: Normal | Resolution:
Keywords: HTTPResponse header | Triage Stage:
str bytes | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/29276#comment:1>

Django

unread,
Mar 30, 2018, 6:15:11 AM3/30/18
to django-...@googlegroups.com
#29276: Turn the headers of HTTPResponse from str to bytes automatically

-------------------------------------+-------------------------------------
Reporter: sanjiely | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: HTTP handling | Version: 2.0
Severity: Normal | Resolution:
Keywords: HTTPResponse header | Triage Stage:
str bytes | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/29276#comment:2>

Django

unread,
Mar 30, 2018, 6:34:52 AM3/30/18
to django-...@googlegroups.com
#29276: Turn the headers of HTTPResponse from str to bytes automatically

-------------------------------------+-------------------------------------
Reporter: sanjiely | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: HTTP handling | Version: 2.0
Severity: Normal | Resolution:
Keywords: HTTPResponse header | Triage Stage:
str bytes | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Claude Paroz:

Old description:

New description:

Python: 3.6.4
Django: 2.0.3

Yong Li

--

--
Ticket URL: <https://code.djangoproject.com/ticket/29276#comment:3>

Django

unread,
Mar 30, 2018, 6:37:41 AM3/30/18
to django-...@googlegroups.com
#29276: Turn the headers of HTTPResponse from str to bytes automatically

-------------------------------------+-------------------------------------
Reporter: sanjiely | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: HTTP handling | Version: 2.0
Severity: Normal | Resolution:
Keywords: HTTPResponse header | Triage Stage:
str bytes | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Claude Paroz):

Sorry, but you didn't show by your example why setting the header as a
normal unicode string is a problem. You should develop the `everthing will
out of control` part of your description.

--
Ticket URL: <https://code.djangoproject.com/ticket/29276#comment:4>

Django

unread,
Mar 30, 2018, 7:20:30 AM3/30/18
to django-...@googlegroups.com
#29276: Turn the headers of HTTPResponse from str to bytes automatically

-------------------------------------+-------------------------------------
Reporter: sanjiely | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: HTTP handling | Version: 2.0
Severity: Normal | Resolution: duplicate

Keywords: HTTPResponse header | Triage Stage:
str bytes | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Claude Paroz):

* status: new => closed
* resolution: => duplicate


Comment:

Mmmh, I guess this is in fact a duplicate of #16470.

--
Ticket URL: <https://code.djangoproject.com/ticket/29276#comment:5>

Django

unread,
Mar 30, 2018, 11:04:18 AM3/30/18
to django-...@googlegroups.com
#29276: Compatibility of charset works not well when there are non-ascii
characters in HTTPResponse headers

-------------------------------------+-------------------------------------
Reporter: sanjiely | Owner: nobody
Type: Bug | Status: new

Component: HTTP handling | Version: 2.0
Severity: Normal | Resolution:
Keywords: HTTPResponse header | Triage Stage:
str bytes | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by sanjiely):

* status: closed => new
* type: Cleanup/optimization => Bug
* resolution: duplicate =>


Old description:

New description:

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.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/29276#comment:6>

Django

unread,
Mar 30, 2018, 12:15:46 PM3/30/18
to django-...@googlegroups.com
#29276: Compatibility of charset works not well when there are non-ascii
characters in HTTPResponse headers
-------------------------------------+-------------------------------------
Reporter: sanjiely | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: 2.0
Severity: Normal | Resolution:
Keywords: HTTPResponse header | Triage Stage:
str bytes | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

Shouldn't HTTP headers only contain ISO-
8859-1 characters and [https://tools.ietf.org/html/rfc5987 RFC5987] be
followed to include parameters containing characters outside of its range?

I think your header should be something along.

{{{#!python
response['Content-Type'] = "attachment;
filename*=UTF-8''{}".format(quote(filename))
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29276#comment:7>

Django

unread,
Apr 3, 2018, 1:02:19 PM4/3/18
to django-...@googlegroups.com
#29276: Compatibility of charset works not well when there are non-ascii
characters in HTTPResponse headers
-------------------------------------+-------------------------------------
Reporter: sanjiely | Owner: nobody
Type: Bug | Status: closed

Component: HTTP handling | Version: 2.0
Severity: Normal | Resolution: duplicate

Keywords: HTTPResponse header | Triage Stage:
str bytes | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => duplicate


--
Ticket URL: <https://code.djangoproject.com/ticket/29276#comment:8>

Reply all
Reply to author
Forward
0 new messages