-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Response.content_type doesn't ensure proper encoding of the header #388
Comments
This is one of those behaviours where I think trying to be too magical and encoding from unicode to latin-1 is a mistake. None of those API's expect to take unicode. There are a ton of paths through the code base where the encoding does not properly happen because it bypasses the getters/setters. Trying to fix all of those is fraught with issues. The only place this can be safely done is as a last-pass before handing the headers back off to the WSGI server. There's other bugs related to this that have been opened, such as people trying to use Related: #247 (bypass of the getter/setter) |
Agree that tackling case by case is not the best solution in the world. I don't even know if By the way, my major concern was just the inconsistency in behaviour. Was pointed out by a TurboGears user that took a value out of |
Yeah, I'll likely fix it, but I don't like it. Thankfully PY2 support is going away soon. |
That being said, WebOb is a very thin wrapper, and there are plenty of places where the abstraction breaks away. I think it is weird/surprising that using the property means you get encoding but when you use the headers directly, you don't. Even-though both are technically abstractions. |
Outgoing headers, on PY2, normally get properly encoded to
latin-1
by WebOb if the original value wasunicode
.This is properly managed by the
header_getter
descriptor: 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_type
one.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.etag
has a good solution to this, because the custom getter/setter behave on top of_etag_raw
which is implemented usingheader_getter
descriptor and thus guarantees the encoding: https://github.com/Pylons/webob/blob/master/src/webob/response.py#L747-L750The text was updated successfully, but these errors were encountered: