Serve any file in www/

Also fixes #41
master
pictuga 2020-08-28 20:45:39 +02:00
parent 2456dd9bbc
commit f6d641eeef
1 changed files with 19 additions and 25 deletions

View File

@ -152,45 +152,39 @@ 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:
headers = {}
if url == '': if url == '':
url = 'index.html' url = 'index.html'
paths = [os.path.join(sys.prefix, 'share/morss/www', url), if re.match(r'^/?([a-zA-Z0-9_-][a-zA-Z0-9\._-]+/?)*$', url):
os.path.join(os.path.dirname(__file__), '../www', url)] # if it is a legitimate url (no funny relative paths)
paths = [
os.path.join(sys.prefix, 'share/morss/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 = {}
headers['status'] = '200 OK'
headers['content-type'] = mimetypes.guess_type(path)[0]
start_response(headers['status'], list(headers.items())) start_response(headers['status'], list(headers.items()))
return ['Error %s' % headers['status']] return wsgiref.util.FileWrapper(f)
else: # regex didn't validate or no file found
return app(environ, start_response) return app(environ, start_response)