Option to disable content-type guessing by URL
Add a new option to disable content-type guessing based on the extension of the URL. Change-Id: I5bea41fcc1011cfb19255cc0333924034007a645 Signed-off-by: Doug Hellmann <doug.hellmann@dreamhost.com>
This commit is contained in:
@@ -177,11 +177,15 @@ class Pecan(object):
|
||||
namespace automatically.
|
||||
:param force_canonical: A boolean indicating if this project should
|
||||
require canonical URLs.
|
||||
:param guess_content_type_from_ext: A boolean indicating if this project
|
||||
should use the extension in the URL for guessing
|
||||
the content type to return.
|
||||
'''
|
||||
|
||||
def __init__(self, root, default_renderer='mako',
|
||||
template_path='templates', hooks=[], custom_renderers={},
|
||||
extra_template_vars={}, force_canonical=True):
|
||||
extra_template_vars={}, force_canonical=True,
|
||||
guess_content_type_from_ext=True):
|
||||
|
||||
if isinstance(root, basestring):
|
||||
root = self.__translate_root__(root)
|
||||
@@ -192,6 +196,7 @@ class Pecan(object):
|
||||
self.hooks = hooks
|
||||
self.template_path = template_path
|
||||
self.force_canonical = force_canonical
|
||||
self.guess_content_type_from_ext = guess_content_type_from_ext
|
||||
|
||||
def __translate_root__(self, item):
|
||||
'''
|
||||
@@ -370,7 +375,9 @@ class Pecan(object):
|
||||
request.pecan['extension'] = None
|
||||
|
||||
# attempt to guess the content type based on the file extension
|
||||
if not request.pecan['content_type'] and '.' in path.split('/')[-1]:
|
||||
if self.guess_content_type_from_ext \
|
||||
and not request.pecan['content_type'] \
|
||||
and '.' in path.split('/')[-1]:
|
||||
new_path, extension = splitext(path)
|
||||
|
||||
# preface with a letter to ensure compat for 2.5
|
||||
|
||||
@@ -12,5 +12,9 @@ def setup_app(config):
|
||||
template_path=config.app.template_path,
|
||||
logging=getattr(config, 'logging', {}),
|
||||
debug=getattr(config.app, 'debug', False),
|
||||
force_canonical=getattr(config.app, 'force_canonical', True)
|
||||
force_canonical=getattr(config.app, 'force_canonical', True),
|
||||
guess_content_type_from_ext=getattr(
|
||||
config.app,
|
||||
'guess_content_type_from_ext',
|
||||
True),
|
||||
)
|
||||
|
||||
@@ -925,6 +925,21 @@ class TestFileTypeExtensions(unittest.TestCase):
|
||||
assert r.status_int == 200
|
||||
assert r.body == 'SOME VALUE'
|
||||
|
||||
def test_guessing_disabled(self):
|
||||
class RootController(object):
|
||||
@expose(content_type=None)
|
||||
def _default(self, *args):
|
||||
assert 'index.html' in args
|
||||
assert request.pecan['extension'] is None
|
||||
return 'SOME VALUE'
|
||||
|
||||
app = TestApp(Pecan(RootController(),
|
||||
guess_content_type_from_ext=False))
|
||||
|
||||
r = app.get('/index.html')
|
||||
assert r.status_int == 200
|
||||
assert r.body == 'SOME VALUE'
|
||||
|
||||
|
||||
class TestContentTypeByAcceptHeaders(unittest.TestCase):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user