Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upOddities due to having a default_content_type/charset #238
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
mmerickel
Mar 30, 2016
Member
As an aside, just wanted to point out that werkzeug documents that they only add the charset to responses with a mimetype of text/*[1], however if you manually set content type then it will go through unchanged. This approach may solve part of the problem regarding when to add a charset to a response. I don't think you can get rid of the charset entirely because it is used to encode response.text = .. into bytes even if the content is not text.
|
As an aside, just wanted to point out that werkzeug documents that they only add the |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
bertjwregeer
Mar 30, 2016
Member
I have no intentions of getting rid of charset entirely, just that there will be no default perhaps. I do like the idea of setting it by default only for text/*.
|
I have no intentions of getting rid of charset entirely, just that there will be no default perhaps. I do like the idea of setting it by default only for |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
This would solve the issue in #237 as well. |
bertjwregeer
referenced this issue
Jul 9, 2016
Closed
default_content_type does not match documented value #213
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
bertjwregeer
Jul 9, 2016
Member
Numbers 2 and 3 in the list above have now been fixed in #261. That leaves number 1 in the list of items that should still be fixed.
|
Numbers 2 and 3 in the list above have now been fixed in #261. That leaves number 1 in the list of items that should still be fixed. |
bertjwregeer commentedMar 30, 2016
There has been some discussion between myself and @mmerickel regarding
default_content_type, and he rightfully requested a list of issues that we would want to see solved:Problems that should be solved
default_content_typethat was set uponResponseobject creationdefault_content_typeis set totext/html, WebOb currently automatically adds a charset of UTF-8. Upon changing thecontent_typeon the object, the charset is sticky and unless explicitly unset will continue to be set to UTF-8. This leads toimage/jpegwith a charset of UTF-8 orapplication/jsonwith a charset of UTF-8, both of which are nonsensical. For more information see discussion here: #237default_content_typewill sometimes do the wrong thing, specifically if the user usesRequest.get_response(app)and the original app returns a response that doesn't contain a body, and thus shouldn't have a content-typeThere is a quite a bit of code dedicated within WebOb itself to fixing up the content-type/charset at various points as required due to setting a default upon object instantiation, and there is lot of clean-up that should be done just to make things a little saner, and it should make the
Responseobject smarter, and in some cases dumber by not trying to be as smart.Solutions
Radical, and too far reaching
I was personally a big fan of removing the
default_content_typeentirely, reverting back to what is documented. See #213.At the same time this will be a major backwards incompatibility, and other Request/Response libraries such as werkzeug set the default content type to
text/plain: https://github.com/pallets/werkzeug/blob/master/werkzeug/wrappers.py#L732 so this would be a huge departure from what has become the accepted norm since WebOb 1.2 and other similar libraries, and I have been talked out of it, not just by Michael but also by myself as I've had a chance to chew on it some more.So this is out. Possibly changing it to match
text/plaincould make sense, so that a bareResponse('hello')has a content-type oftext/plaininstead oftext/html. I'd argue that the former is more correct, but we may well be stuck with the default we've got.Possible Solutions to No. 1
Solving 1 means we need some way of tracking changes to the
content_type. At the moment this is difficult because it is a descriptor around a HTTP headers array, and it dynamically sets/fetches content-type header, so using a simple marker object doesn't work. This could be changed, so that the content-type is special cased, however there is still the issue of the user bypassing the descriptor and instead setting the header directly, and now the descriptor gets out of sync, so logic would need to be employed to keep the two in sync.This would allow then adding an API of
content_type_changedor something along those lines that simply checks and sees if thecontent_typeis the marker interface and if so it returns False.Possible Solutions to No. 2
Instead of adding smarts to WebOb, we should only set the charset to UTF-8 by default on
text/htmlandtext/plainresponses. This list may grow one or two more, but we want to define the bare minimum. If there is any othercontent_typeit is a requirement that the user adds the charset themselves. This way upon thecontent_typebeing changed, we simply drop whatever charset is set.Any users that explicitly reset charset after setting the content-type would be unaffected, and those that are not resetting the charset now won't accidentally send the charset along with the content-type unnecessarily thereby possibly breaking rendering. Many different media types don't support optional or required parameters for the content-type header.
Possible Solutions to No. 3
When creating a new
Responseobject fromRequest.get_responsewe should disable almost all "defaults" processing that currently happens. This way we can be sure to represent the response from the application as best as possible.I still personally think that having a
Responseobject with adefault_content_typeis bad, but I much prefer to be explicit when I am creating responses rather than allowing for defaults, but we have it now, so not much we can do about it.I'd love to have some discussion as to what the appropriate actions should be, please add to the discussion below.