summaryrefslogtreecommitdiff
path: root/pecan/core.py
diff options
context:
space:
mode:
authorRyan Petrello <lists@ryanpetrello.com>2013-04-24 10:52:45 -0400
committerRyan Petrello <lists@ryanpetrello.com>2013-04-24 10:52:45 -0400
commit25d518541cd9e1532ca8e3c24888b22ff3c22604 (patch)
tree22f80923c279dc421abe815d444c704dd5df0970 /pecan/core.py
parenta51029f1cf8ee010225ce1ad1d30c8da065b4a3d (diff)
downloadpecan-25d518541cd9e1532ca8e3c24888b22ff3c22604.tar.gz
More tinkering on performant Accept method handling.
Diffstat (limited to 'pecan/core.py')
-rw-r--r--pecan/core.py62
1 files changed, 29 insertions, 33 deletions
diff --git a/pecan/core.py b/pecan/core.py
index 2139bcb..4b940af 100644
--- a/pecan/core.py
+++ b/pecan/core.py
@@ -188,6 +188,11 @@ class Pecan(object):
the content type to return.
'''
+ SIMPLEST_CONTENT_TYPES = (
+ ['text/html'],
+ ['text/plain']
+ )
+
def __init__(self, root, default_renderer='mako',
template_path='templates', hooks=[], custom_renderers={},
extra_template_vars={}, force_canonical=True,
@@ -423,43 +428,34 @@ class Pecan(object):
if not req.pecan['content_type']:
# attempt to find a best match based on accept headers (if they
# exist)
- if 'Accept' in req.headers:
- accept = req.headers['Accept']
- if accept == '*/*' or (
- accept.startswith('text/html,') and
- content_types.keys() in (
- ['text/html'],
- ['text/plain']
- )):
- req.pecan['content_type'] = cfg.get(
- 'content_type',
- 'text/html'
- )
- else:
- best_default = acceptparse.MIMEAccept(
- accept
- ).best_match(
- content_types.keys()
- )
-
- if best_default is None:
- msg = "Controller '%s' defined does not support " + \
- "content_type '%s'. Supported type(s): %s"
- logger.error(
- msg % (
- controller.__name__,
- req.pecan['content_type'],
- content_types.keys()
- )
- )
- raise exc.HTTPNotAcceptable()
-
- req.pecan['content_type'] = best_default
- else:
+ accept = req.headers.get('Accept', '*/*')
+ if accept == '*/*' or (
+ accept.startswith('text/html,') and
+ list(content_types.keys()) in self.SIMPLEST_CONTENT_TYPES):
req.pecan['content_type'] = cfg.get(
'content_type',
'text/html'
)
+ else:
+ best_default = acceptparse.MIMEAccept(
+ accept
+ ).best_match(
+ content_types.keys()
+ )
+
+ if best_default is None:
+ msg = "Controller '%s' defined does not support " + \
+ "content_type '%s'. Supported type(s): %s"
+ logger.error(
+ msg % (
+ controller.__name__,
+ req.pecan['content_type'],
+ content_types.keys()
+ )
+ )
+ raise exc.HTTPNotAcceptable()
+
+ req.pecan['content_type'] = best_default
elif cfg.get('content_type') is not None and \
req.pecan['content_type'] not in \
content_types: