Permalink
Browse files

Merge pull request #301 from Pylons/feature/remove_ctype_params

Feature: remove ctype params
  • Loading branch information...
2 parents d19656f + e89ef1c commit 93fc4bd66107d6b4b85f7210c8b8f98b3212e8e5 @bertjwregeer bertjwregeer committed on GitHub Dec 10, 2016
Showing with 38 additions and 43 deletions.
  1. +3 −3 docs/index.txt
  2. +1 −1 tests/test_response.py
  3. +34 −39 webob/response.py
View
@@ -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
==========
@@ -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'}
View
@@ -12,8 +12,6 @@
except ImportError:
import json
-import warnings
-
from webob.byterange import ContentRange
from webob.cachecontrol import (
@@ -840,27 +838,19 @@ def _content_type__get(self):
.. 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::
resp = Response()
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 @@ def _content_type__set(self, value):
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)

0 comments on commit 93fc4bd

Please sign in to comment.