Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing #1038 by allowing nice URLs. #1040

Merged
merged 1 commit into from
Aug 16, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 24 additions & 5 deletions pelican/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import print_function
import os
import sys
import logging
try:
import SimpleHTTPServer as srvmod
except ImportError:
Expand All @@ -11,19 +13,36 @@
import socketserver # NOQA

PORT = len(sys.argv) == 2 and int(sys.argv[1]) or 8000
SUFFIXES = ['','.html','/index.html']

Handler = srvmod.SimpleHTTPRequestHandler
class ComplexHTTPRequestHandler(srvmod.SimpleHTTPRequestHandler):
def do_GET(self):
# we are trying to detect the file by having a fallback mechanism
r = None
for suffix in SUFFIXES:
if not hasattr(self,'original_path'):
self.original_path = self.path
self.path = self.original_path + suffix
path = self.translate_path(self.path)
if os.path.exists(path):
r = srvmod.SimpleHTTPRequestHandler.do_GET(self)
if r is not None:
break
logging.warning("Unable to find %s file." % self.path)
return r

Handler = ComplexHTTPRequestHandler

try:
httpd = socketserver.TCPServer(("", PORT), Handler)
except OSError as e:
print("Could not listen on port", PORT)
logging.error("Could not listen on port %s" % PORT)
sys.exit(getattr(e, 'exitcode', 1))


print("serving at port", PORT)
logging.info("serving at port %s" % PORT)
try:
httpd.serve_forever()
except KeyboardInterrupt as e:
print("shutting down server")
httpd.socket.close()
logging.info("shutting down server")
httpd.socket.close()