Force directory paths to end with a trailing slash

This commit is contained in:
Michael Lazar 2019-09-02 20:58:22 -04:00
parent c626494bfc
commit 371b7dd01d
2 changed files with 16 additions and 8 deletions

View File

@ -1,5 +1,10 @@
# Jetforce Changelog # Jetforce Changelog
### Unreleased
- Force URLs to always end in trailing slashes when serving a directory. This
reduces duplicate selectors and makes resolving relative links more reliable.
### v0.0.7 (2019-08-30) ### v0.0.7 (2019-08-30)
- Added support for a primitive version of CGI scripting. - Added support for a primitive version of CGI scripting.

View File

@ -235,18 +235,21 @@ class StaticDirectoryApplication(JetforceApplication):
if is_cgi and is_exe: if is_cgi and is_exe:
return self.run_cgi_script(filesystem_path, request.environ) return self.run_cgi_script(filesystem_path, request.environ)
else:
mimetype = self.guess_mimetype(filesystem_path.name) mimetype = self.guess_mimetype(filesystem_path.name)
generator = self.load_file(filesystem_path) generator = self.load_file(filesystem_path)
return Response(Status.SUCCESS, mimetype, generator) return Response(Status.SUCCESS, mimetype, generator)
elif filesystem_path.is_dir(): elif filesystem_path.is_dir():
if request.path and not request.path.endswith("/"):
url_parts = urllib.parse.urlparse(request.url)
url_parts = url_parts._replace(path=request.path + "/")
return Response(Status.REDIRECT_PERMANENT, url_parts.geturl())
index_file = filesystem_path / self.index_file index_file = filesystem_path / self.index_file
if index_file.exists(): if index_file.exists():
generator = self.load_file(index_file) generator = self.load_file(index_file)
return Response(Status.SUCCESS, "text/gemini", generator) return Response(Status.SUCCESS, "text/gemini", generator)
else:
generator = self.list_directory(url_path, filesystem_path) generator = self.list_directory(url_path, filesystem_path)
return Response(Status.SUCCESS, "text/gemini", generator) return Response(Status.SUCCESS, "text/gemini", generator)
@ -310,7 +313,7 @@ class StaticDirectoryApplication(JetforceApplication):
# Skip hidden and temporary files for security reasons # Skip hidden and temporary files for security reasons
continue continue
elif file.is_dir(): elif file.is_dir():
yield f"=>/{url_path / file.name}\t{file.name}/\r\n".encode() yield f"=>/{url_path / file.name}/\t{file.name}/\r\n".encode()
else: else:
yield f"=>/{url_path / file.name}\t{file.name}\r\n".encode() yield f"=>/{url_path / file.name}\t{file.name}\r\n".encode()