From 00cb3fa203aefc1400840c8481027d912aa2dd93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Wed, 27 Sep 2023 23:09:51 +0200 Subject: [PATCH] Redirect '/directory' to '/directory/'. --- scripts/autoserve.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/scripts/autoserve.py b/scripts/autoserve.py index ed1fb51..87d6b41 100644 --- a/scripts/autoserve.py +++ b/scripts/autoserve.py @@ -55,16 +55,24 @@ class Server(http.server.SimpleHTTPRequestHandler): time.sleep(SLEEP_TIME) return + path = self.path + if path.strip('/') == '': + path = '/index.html' + if os.path.isdir(path.strip('/')): + if path.endswith('/'): + path = path.strip('/') + '/index.html' + else: + # Redirect to + / + self.send_response(301) + self.send_header('Location', path + '/') + self.end_headers() + return + # send 200 response self.send_response(200) # send response headers self.end_headers() - path = self.path - if path.strip('/') == '': - path = '/index.html' - if os.path.isdir(path.strip('/')): - path = path.strip('/') + '/index.html' with open(path.strip('/'), 'rb') as f: # send the body of the response self.wfile.write(f.read())