mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Merge pull request #1441 from eliben/server-try-stripping
Server script should try to rstrip '/' when serving pages
This commit is contained in:
commit
c16fdd997c
1 changed files with 20 additions and 13 deletions
|
|
@ -16,28 +16,35 @@ from six.moves import socketserver
|
||||||
|
|
||||||
class ComplexHTTPRequestHandler(srvmod.SimpleHTTPRequestHandler):
|
class ComplexHTTPRequestHandler(srvmod.SimpleHTTPRequestHandler):
|
||||||
SUFFIXES = ['', '.html', '/index.html']
|
SUFFIXES = ['', '.html', '/index.html']
|
||||||
|
RSTRIP_PATTERNS = ['', '/']
|
||||||
|
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
# cut off a query string
|
# cut off a query string
|
||||||
if '?' in self.path:
|
if '?' in self.path:
|
||||||
self.path, _ = self.path.split('?', 1)
|
self.path, _ = self.path.split('?', 1)
|
||||||
|
|
||||||
# Try to detect file by applying various suffixes
|
found = False
|
||||||
for suffix in self.SUFFIXES:
|
# Try to detect file by applying various suffixes and stripping
|
||||||
if not hasattr(self, 'original_path'):
|
# patterns.
|
||||||
self.original_path = self.path
|
for rstrip_pattern in self.RSTRIP_PATTERNS:
|
||||||
|
if found:
|
||||||
self.path = self.original_path + suffix
|
|
||||||
path = self.translate_path(self.path)
|
|
||||||
|
|
||||||
if os.path.exists(path):
|
|
||||||
srvmod.SimpleHTTPRequestHandler.do_GET(self)
|
|
||||||
logging.info("Found `%s`." % self.path)
|
|
||||||
break
|
break
|
||||||
|
for suffix in self.SUFFIXES:
|
||||||
|
if not hasattr(self, 'original_path'):
|
||||||
|
self.original_path = self.path
|
||||||
|
|
||||||
logging.info("Tried to find `%s`, but it doesn't exist.",
|
self.path = self.original_path.rstrip(rstrip_pattern) + suffix
|
||||||
self.path)
|
path = self.translate_path(self.path)
|
||||||
else:
|
|
||||||
|
if os.path.exists(path):
|
||||||
|
srvmod.SimpleHTTPRequestHandler.do_GET(self)
|
||||||
|
logging.info("Found `%s`.", self.path)
|
||||||
|
found = True
|
||||||
|
break
|
||||||
|
|
||||||
|
logging.info("Tried to find `%s`, but it doesn't exist.", path)
|
||||||
|
|
||||||
|
if not found:
|
||||||
# Fallback if there were no matches
|
# Fallback if there were no matches
|
||||||
logging.warning("Unable to find `%s` or variations.",
|
logging.warning("Unable to find `%s` or variations.",
|
||||||
self.original_path)
|
self.original_path)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue