summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBert JW Regeer <bertjw@regeer.org>2016-12-09 20:26:29 -0700
committerBert JW Regeer <bertjw@regeer.org>2016-12-09 20:54:02 -0700
commitc422ae4a6895ef00f796ac461aece5915811ebd3 (patch)
treeb0427875da2b4057902e1239ff52ec358455d702
parent31ae3ef93e955807ec9136df4902ed0e88321e25 (diff)
downloadwebob-c422ae4a6895ef00f796ac461aece5915811ebd3.tar.gz
Remove all Content-Type params when setting Content-Type
This rips the band-aid off, instead of lingering on and only removing the charset from the the Content-Type, it just removes all Content-Type parameters which is more correct than previous. This also will add back a charset if the Content-Type is "texty", using the default_charset if it exists.
-rw-r--r--webob/response.py53
1 files changed, 29 insertions, 24 deletions
diff --git a/webob/response.py b/webob/response.py
index 818d9a8..d6422ca 100644
--- a/webob/response.py
+++ b/webob/response.py
@@ -871,31 +871,36 @@ class Response(object):
if not value:
self._content_type__del()
return
- if ';' not in value:
- header = self.headers.get('Content-Type', '')
- if ';' in header:
- warn_deprecation(
- 'Preserving Content-Type parameters. In the '
- 'future upon changing the Content-Type no paramaters '
- 'will be preserved.', 1.9, 1)
- params = self.content_type_params
- self.headers['Content-Type'] = value
-
- if 'charset' in params:
- if not _content_type_has_charset(value):
- warnings.warn(
- 'Explicitly removing charset as new content_type '
- 'does not allow charset as a parameter. If you are '
- 'expecting a charset to be set, please add it back '
- 'explicitly after setting the content_type.',
- RuntimeWarning)
- del params['charset']
-
- self.content_type_params = params
- else:
- self.headers['Content-Type'] = value
else:
- self.headers['Content-Type'] = value
+ content_type = value
+
+ # Set up the charset if the content-type doesn't have one
+
+ has_charset = 'charset=' in content_type
+
+ new_charset = None
+
+ if (
+ not has_charset and
+ self.default_charset
+ ):
+ new_charset = self.default_charset
+
+ # Optimize for the default_content_type as shipped by
+ # WebOb, becuase we know that 'text/html' has a charset,
+ # otherwise add a charset if the content_type has a charset.
+ #
+ # We add the default charset if the content-type is "texty".
+ if (
+ new_charset and
+ (
+ content_type == 'text/html' or
+ _content_type_has_charset(content_type)
+ )
+ ):
+ content_type += '; charset=' + new_charset
+
+ self.headers['Content-Type'] = content_type
def _content_type__del(self):
self.headers.pop('Content-Type', None)