Redirect '/directory' to '/directory/'.

This commit is contained in:
Sergio Martínez Portela 2023-09-27 23:09:51 +02:00
parent 816cedea4d
commit 00cb3fa203

View File

@ -55,16 +55,24 @@ class Server(http.server.SimpleHTTPRequestHandler):
time.sleep(SLEEP_TIME) time.sleep(SLEEP_TIME)
return 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 # send 200 response
self.send_response(200) self.send_response(200)
# send response headers # send response headers
self.end_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: with open(path.strip('/'), 'rb') as f:
# send the body of the response # send the body of the response
self.wfile.write(f.read()) self.wfile.write(f.read())