#21282 closed Bug (fixed)
The serialize_headers method of HttpResponse fails to handle latin1-compatible values
Reported by: | Raphaël Barrois | Owned by: | nobody |
---|---|---|---|
Component: | HTTP handling | Version: | 1.4 |
Severity: | Normal | Keywords: | http header encoding |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
If a header value (passed as unicode) contains only ascii data, serialize_headers()
doesn't fail
If it contains characters that can't be encoded in latin1, serialize_headers()
doesn't fail
If it contains only characters valid in latin1, including some outside the ascii range, serialize_headers()
fails with a UnicodeDecodeError
.
The culprit seems to lie on line 132 of django/http/response.py
: that line calls ('%s: %s' % (key, value)).encode('us-ascii')
, but at this point, key is ascii bytes and value is a bytes array containing either latin1-encoded text or mime-encoded utf8 text.
Since we're using unicode_literals
, Python tries value.decode('ascii')
, which fails if, and only if, value contains latin1 characters outside the ascii range.
I have attached a patch containing both a test exhibiting the issue and a patch fixing it (no test failure).
Note: This is not a release blocker, since the serialize_headers()
and its callers (serialize()
and __str__()
) aren't used while answering requests.
Attachments (2)
Change History (7)
by , 11 years ago
Attachment: | fix_ticket_21282_httpresponse_serialize.patch added |
---|
comment:1 by , 11 years ago
The test suite passes with the proposed patch:
Ran 5906 tests in 201.214s OK (skipped=348, expected failures=11)
comment:2 by , 11 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 11 years ago
I've attached a slightly different patch, Python 3 compatible. Aymeric's review mandatory!
comment:4 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Test and fix for HttpResponse.serialize_headers() bug.