Skip to content

Response.content_type doesn't ensure proper encoding of the header #388

@amol-

Description

@amol-

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions