wsgi: handle url decoding

master
pictuga 2021-08-31 21:51:21 +02:00
parent 8f24214915
commit 24c26d3850
1 changed files with 24 additions and 19 deletions

View File

@ -50,7 +50,7 @@ def parse_options(options):
split = option.split('=', 1) split = option.split('=', 1)
if len(split) > 1: if len(split) > 1:
out[split[0]] = split[1] out[split[0]] = unquote(split[1])
else: else:
out[split[0]] = True out[split[0]] = True
@ -58,14 +58,18 @@ def parse_options(options):
return out return out
def get_path(environ): def request_uri(environ):
if 'REQUEST_URI' in environ: if 'REQUEST_URI' in environ:
# when running on Apache # when running on Apache/uwsgi
url = unquote(environ['REQUEST_URI'][1:]) url = environ['REQUEST_URI']
elif 'RAW_URI' in environ:
# gunicorn
url = environ['RAW_URI']
else: else:
# when using internal server # when using other servers
url = environ['PATH_INFO'][1:] url = environ['PATH_INFO']
if environ['QUERY_STRING']: if environ['QUERY_STRING']:
url += '?' + environ['QUERY_STRING'] url += '?' + environ['QUERY_STRING']
@ -76,19 +80,13 @@ def get_path(environ):
def cgi_parse_environ(environ): def cgi_parse_environ(environ):
# get options # get options
url = get_path(environ) url = request_uri(environ)[1:]
url = re.sub(r'^/?(cgi/)?(morss.py|main.py)/', '', url) url = re.sub(r'^(cgi/)?(morss.py|main.py)/', '', url)
if url.startswith(':'): if url.startswith(':'):
split = url.split('/', 1) parts = url.split('/', 1)
raw_options = parts[0].replace('|', '/').split(':')[1:]# | -> / for backward compatibility
raw_options = split[0].replace('|', '/').replace('\\\'', '\'').split(':')[1:] url = parts[1] if len(parts) > 1 else ''
if len(split) > 1:
url = split[1]
else:
url = ''
else: else:
raw_options = [] raw_options = []
@ -164,7 +162,7 @@ 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.) "
url = get_path(environ) url = request_uri(environ)[1:]
if url == '': if url == '':
url = 'index.html' url = 'index.html'
@ -283,11 +281,18 @@ def cgi_handle_request():
wsgiref.handlers.CGIHandler().run(app) wsgiref.handlers.CGIHandler().run(app)
class WSGIRequestHandlerRequestUri(wsgiref.simple_server.WSGIRequestHandler):
def get_environ(self):
env = super().get_environ()
env['REQUEST_URI'] = self.path
return env
def cgi_start_server(): def cgi_start_server():
crawler.default_cache.autotrim() crawler.default_cache.autotrim()
print('Serving http://localhost:%s/' % PORT) print('Serving http://localhost:%s/' % PORT)
httpd = wsgiref.simple_server.make_server('', PORT, application) httpd = wsgiref.simple_server.make_server('', PORT, application, handler_class=WSGIRequestHandlerRequestUri)
httpd.serve_forever() httpd.serve_forever()