Don't strip a dotted extension from the path unless it has a matching mimetype.
This commit is contained in:
@@ -371,10 +371,15 @@ class Pecan(object):
|
||||
|
||||
# attempt to guess the content type based on the file extension
|
||||
if not request.pecan['content_type'] and '.' in path.split('/')[-1]:
|
||||
path, extension = splitext(path)
|
||||
request.pecan['extension'] = extension
|
||||
new_path, extension = splitext(path)
|
||||
|
||||
# preface with a letter to ensure compat for 2.5
|
||||
request.pecan['content_type'] = guess_type('x' + extension)[0]
|
||||
potential_type = guess_type('x' + extension)[0]
|
||||
|
||||
if potential_type is not None:
|
||||
path = new_path
|
||||
request.pecan['extension'] = extension
|
||||
request.pecan['content_type'] = potential_type
|
||||
|
||||
controller, remainder = self.route(self.root, path)
|
||||
cfg = _cfg(controller)
|
||||
|
||||
@@ -863,7 +863,11 @@ class TestFileTypeExtensions(unittest.TestCase):
|
||||
class RootController(object):
|
||||
@expose(content_type=None)
|
||||
def _default(self, *args):
|
||||
return request.pecan['extension']
|
||||
ext = request.pecan.get('extension')
|
||||
assert len(args) == 1
|
||||
if ext:
|
||||
assert ext not in args[0]
|
||||
return ext or ''
|
||||
|
||||
return TestApp(Pecan(RootController()))
|
||||
|
||||
@@ -883,7 +887,7 @@ class TestFileTypeExtensions(unittest.TestCase):
|
||||
assert r.body == ''
|
||||
|
||||
def test_multi_dot_extension(self):
|
||||
r = self.app_.get('/gradient.js.js')
|
||||
r = self.app_.get('/gradient.min.js')
|
||||
assert r.status_int == 200
|
||||
assert r.body == '.js'
|
||||
|
||||
@@ -907,6 +911,20 @@ class TestFileTypeExtensions(unittest.TestCase):
|
||||
r = app.get('/index.txt', expect_errors=True)
|
||||
assert r.status_int == 404
|
||||
|
||||
def test_unknown_file_extension(self):
|
||||
class RootController(object):
|
||||
@expose(content_type=None)
|
||||
def _default(self, *args):
|
||||
assert 'example:x.tiny' in args
|
||||
assert 'extension' not in request.pecan
|
||||
return 'SOME VALUE'
|
||||
|
||||
app = TestApp(Pecan(RootController()))
|
||||
|
||||
r = app.get('/example:x.tiny')
|
||||
assert r.status_int == 200
|
||||
assert r.body == 'SOME VALUE'
|
||||
|
||||
|
||||
class TestContentTypeByAcceptHeaders(unittest.TestCase):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user