summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBert JW Regeer <xistence@0x58.com>2016-12-09 20:56:36 -0700
committerGitHub <noreply@github.com>2016-12-09 20:56:36 -0700
commit93fc4bd66107d6b4b85f7210c8b8f98b3212e8e5 (patch)
tree2829ab39a39a9f58a7d198270957e6c04aa377bf
parentd19656fc3595af923cdcef2e06e06b71b4989946 (diff)
parente89ef1ccfdc20b8423e5150f0a5cfa085980da74 (diff)
downloadwebob-93fc4bd66107d6b4b85f7210c8b8f98b3212e8e5.tar.gz
Merge pull request #301 from Pylons/feature/remove_ctype_params
Feature: remove ctype params
-rw-r--r--docs/index.txt6
-rw-r--r--tests/test_response.py2
-rw-r--r--webob/response.py73
3 files changed, 38 insertions, 43 deletions
diff --git a/docs/index.txt b/docs/index.txt
index 2b9b3d6..6dffbfd 100644
--- a/docs/index.txt
+++ b/docs/index.txt
@@ -232,9 +232,9 @@ argument to the class, e.g.:
response = Response(body='hello world!', content_type='text/plain')
-The status defaults to ``'200 OK'``. The content_type does not
-default to anything, although if you subclass ``Response`` and set
-``default_content_type``, you can override this behavior.
+The status defaults to ``'200 OK'``. The content_type defaults to
+``default_content_type`` which is set to `text/html`, although if you subclass
+``Response`` and set ``default_content_type``, you can override this behavior.
Exceptions
==========
diff --git a/tests/test_response.py b/tests/test_response.py
index 1fb153b..1d272c6 100644
--- a/tests/test_response.py
+++ b/tests/test_response.py
@@ -39,7 +39,7 @@ def test_response():
res.charset = 'iso8859-1'
assert 'text/html; charset=iso8859-1' == res.headers['content-type']
res.content_type = 'text/xml'
- assert 'text/xml; charset=iso8859-1' == res.headers['content-type']
+ assert 'text/xml; charset=UTF-8' == res.headers['content-type']
res.content_type = 'text/xml; charset=UTF-8'
assert 'text/xml; charset=UTF-8' == res.headers['content-type']
res.headers = {'content-type': 'text/html'}
diff --git a/webob/response.py b/webob/response.py
index 818d9a8..92efd70 100644
--- a/webob/response.py
+++ b/webob/response.py
@@ -12,8 +12,6 @@ try:
except ImportError:
import json
-import warnings
-
from webob.byterange import ContentRange
from webob.cachecontrol import (
@@ -840,14 +838,12 @@ class Response(object):
.. versionchanged:: 1.7
- Setting a new Content-Type will remove charset from the
- Content-Type parameters if the Content-Type is not ``text/*`` or XML
- (``application/xml``, or ``*/*+xml``)
+ Setting a new Content-Type will remove all Content-Type parameters
+ and reset the charset to the default if the Content-Type is
+ ``text/*`` or XML (``application/xml``, or ``*/*+xml``)
- In the future all parameters will be deleted upon changing the
- Content-Type, if you explicitly want to transfer over existing
- parameters, you may retrieve them with ``content_type_params`` and
- set them after setting ``content_type``.
+ To preserve all Content-Type parameters you may use the following
+ code:
.. code::
@@ -855,12 +851,6 @@ class Response(object):
params = resp.content_type_params
resp.content_type = 'application/something'
resp.content_type_params = params
-
- .. deprecated:: 1.7
-
- If you include parameters (or ``;`` at all) when setting the
- content_type, any existing parameters will be deleted;
- otherwise they will be preserved.
"""
header = self.headers.get('Content-Type')
if not header:
@@ -871,31 +861,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)