parent
2456dd9bbc
commit
f6d641eeef
|
@ -152,46 +152,40 @@ def middleware(func):
|
||||||
def cgi_file_handler(environ, start_response, app):
|
def cgi_file_handler(environ, start_response, app):
|
||||||
" Simple HTTP server to serve static files (.html, .css, etc.) "
|
" Simple HTTP server to serve static files (.html, .css, etc.) "
|
||||||
|
|
||||||
files = {
|
|
||||||
'': 'text/html',
|
|
||||||
'index.html': 'text/html',
|
|
||||||
'sheet.xsl': 'text/xsl'}
|
|
||||||
|
|
||||||
if 'REQUEST_URI' in environ:
|
if 'REQUEST_URI' in environ:
|
||||||
url = environ['REQUEST_URI'][1:]
|
url = environ['REQUEST_URI'][1:]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
url = environ['PATH_INFO'][1:]
|
url = environ['PATH_INFO'][1:]
|
||||||
|
|
||||||
if url in files:
|
if url == '':
|
||||||
headers = {}
|
url = 'index.html'
|
||||||
|
|
||||||
if url == '':
|
if re.match(r'^/?([a-zA-Z0-9_-][a-zA-Z0-9\._-]+/?)*$', url):
|
||||||
url = 'index.html'
|
# if it is a legitimate url (no funny relative paths)
|
||||||
|
paths = [
|
||||||
paths = [os.path.join(sys.prefix, 'share/morss/www', url),
|
os.path.join(sys.prefix, 'share/morss/www', url),
|
||||||
os.path.join(os.path.dirname(__file__), '../www', url)]
|
os.path.join(os.path.dirname(__file__), '../www', url)
|
||||||
|
]
|
||||||
|
|
||||||
for path in paths:
|
for path in paths:
|
||||||
try:
|
try:
|
||||||
body = open(path, 'rb').read()
|
f = open(path, 'rb')
|
||||||
|
|
||||||
headers['status'] = '200 OK'
|
|
||||||
headers['content-type'] = files[url]
|
|
||||||
start_response(headers['status'], list(headers.items()))
|
|
||||||
return [body]
|
|
||||||
|
|
||||||
except IOError:
|
except IOError:
|
||||||
|
# problem with file (cannot open or not found)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# the for loop did not return, so here we are, i.e. no file found
|
# file successfully open
|
||||||
headers['status'] = '404 Not found'
|
headers = {}
|
||||||
start_response(headers['status'], list(headers.items()))
|
headers['status'] = '200 OK'
|
||||||
return ['Error %s' % headers['status']]
|
headers['content-type'] = mimetypes.guess_type(path)[0]
|
||||||
|
start_response(headers['status'], list(headers.items()))
|
||||||
|
return wsgiref.util.FileWrapper(f)
|
||||||
|
|
||||||
else:
|
# regex didn't validate or no file found
|
||||||
return app(environ, start_response)
|
return app(environ, start_response)
|
||||||
|
|
||||||
|
|
||||||
def cgi_get(environ, start_response):
|
def cgi_get(environ, start_response):
|
||||||
|
|
Loading…
Reference in New Issue