Skip to content

Commit

Permalink
Merge pull request #2931 from avaris/server-list-files
Browse files Browse the repository at this point in the history
Adjust suffix in server to allow redirection when needed
  • Loading branch information
justinmayer authored Oct 5, 2021
2 parents f862d64 + 2d97a45 commit 324fcef
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pelican/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,15 @@ def do_GET(self):

def get_path_that_exists(self, original_path):
# Try to strip trailing slash
trailing_slash = original_path.endswith('/')
original_path = original_path.rstrip('/')
# Try to detect file by applying various suffixes
tries = []
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
if os.path.exists(self.translate_path(path)):
return path
Expand Down
6 changes: 5 additions & 1 deletion pelican/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ def test_get_path_that_exists(self):
os.mkdir(os.path.join(self.temp_output, 'baz'))

for suffix in ['', '/']:
# foo.html has precedence over foo/index.html
path = handler.get_path_that_exists('foo' + suffix)
self.assertEqual(path, 'foo.html')

# folder with index.html should return folder/index.html
path = handler.get_path_that_exists('bar' + suffix)
self.assertEqual(path, 'bar/index.html')

# folder without index.html should return same as input
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)
self.assertIsNone(path)

0 comments on commit 324fcef

Please sign in to comment.