mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Merge pull request #2931 from avaris/server-list-files
Adjust suffix in server to allow redirection when needed
This commit is contained in:
commit
324fcefae7
2 changed files with 10 additions and 1 deletions
|
|
@ -75,10 +75,15 @@ class ComplexHTTPRequestHandler(server.SimpleHTTPRequestHandler):
|
||||||
|
|
||||||
def get_path_that_exists(self, original_path):
|
def get_path_that_exists(self, original_path):
|
||||||
# Try to strip trailing slash
|
# Try to strip trailing slash
|
||||||
|
trailing_slash = original_path.endswith('/')
|
||||||
original_path = original_path.rstrip('/')
|
original_path = original_path.rstrip('/')
|
||||||
# Try to detect file by applying various suffixes
|
# Try to detect file by applying various suffixes
|
||||||
tries = []
|
tries = []
|
||||||
for suffix in self.SUFFIXES:
|
for suffix in self.SUFFIXES:
|
||||||
|
if not trailing_slash and suffix == '/':
|
||||||
|
# if original request does not have trailing slash, skip the '/' suffix
|
||||||
|
# so that base class can redirect if needed
|
||||||
|
continue
|
||||||
path = original_path + suffix
|
path = original_path + suffix
|
||||||
if os.path.exists(self.translate_path(path)):
|
if os.path.exists(self.translate_path(path)):
|
||||||
return path
|
return path
|
||||||
|
|
|
||||||
|
|
@ -43,14 +43,18 @@ class TestServer(unittest.TestCase):
|
||||||
os.mkdir(os.path.join(self.temp_output, 'baz'))
|
os.mkdir(os.path.join(self.temp_output, 'baz'))
|
||||||
|
|
||||||
for suffix in ['', '/']:
|
for suffix in ['', '/']:
|
||||||
|
# foo.html has precedence over foo/index.html
|
||||||
path = handler.get_path_that_exists('foo' + suffix)
|
path = handler.get_path_that_exists('foo' + suffix)
|
||||||
self.assertEqual(path, 'foo.html')
|
self.assertEqual(path, 'foo.html')
|
||||||
|
|
||||||
|
# folder with index.html should return folder/index.html
|
||||||
path = handler.get_path_that_exists('bar' + suffix)
|
path = handler.get_path_that_exists('bar' + suffix)
|
||||||
self.assertEqual(path, 'bar/index.html')
|
self.assertEqual(path, 'bar/index.html')
|
||||||
|
|
||||||
|
# folder without index.html should return same as input
|
||||||
path = handler.get_path_that_exists('baz' + suffix)
|
path = handler.get_path_that_exists('baz' + suffix)
|
||||||
self.assertEqual(path, 'baz/')
|
self.assertEqual(path, 'baz' + suffix)
|
||||||
|
|
||||||
|
# not existing path should return None
|
||||||
path = handler.get_path_that_exists('quux' + suffix)
|
path = handler.get_path_that_exists('quux' + suffix)
|
||||||
self.assertIsNone(path)
|
self.assertIsNone(path)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue