Outgoing headers, on PY2, normally get properly encoded to latin-1 by WebOb if the original value was unicode.
>>> r = Response()
>>> r.location = u'/UnicodeLocation'
>>> r.headers['Location']
'/UnicodeLocation'
>>> type(r.headers['Location'])
<type 'str'>
This is properly managed by the header_getter descriptor: https://github.com/Pylons/webob/blob/master/src/webob/descriptors.py#L153
Problem is that not all headers go through that descriptor, some headers have a custom property.
For example the content_type one.
It seems that in those cases the encoding doesn't happen properly, leading to some inconsistencies in behaviour.
>>> r = Response()
>>> r.content_type = u'text/html'
>>> r.headers['Content-Type']
u'text/html; charset=UTF-8'
>>> type(r.headers['Content-Type'])
<type 'unicode'>
I guess an if isinstance(content_type, text_type) and PY2: content_type = content_type.encode('latin-1') at https://github.com/Pylons/webob/blob/master/src/webob/response.py#L879 might be a solution, but I didn't verify all headers that have a custom setter (IE: Cache-Control).
Response.etag has a good solution to this, because the custom getter/setter behave on top of _etag_raw which is implemented using header_getter descriptor and thus guarantees the encoding: https://github.com/Pylons/webob/blob/master/src/webob/response.py#L747-L750
Outgoing headers, on PY2, normally get properly encoded to
latin-1by WebOb if the original value wasunicode.This is properly managed by the
header_getterdescriptor: https://github.com/Pylons/webob/blob/master/src/webob/descriptors.py#L153Problem is that not all headers go through that descriptor, some headers have a custom property.
For example the
content_typeone.It seems that in those cases the encoding doesn't happen properly, leading to some inconsistencies in behaviour.
I guess an
if isinstance(content_type, text_type) and PY2: content_type = content_type.encode('latin-1')at https://github.com/Pylons/webob/blob/master/src/webob/response.py#L879 might be a solution, but I didn't verify all headers that have a custom setter (IE:Cache-Control).Response.etaghas a good solution to this, because the custom getter/setter behave on top of_etag_rawwhich is implemented usingheader_getterdescriptor and thus guarantees the encoding: https://github.com/Pylons/webob/blob/master/src/webob/response.py#L747-L750