http: Handler now uses redirects to ensure dirs end on a /

This commit is contained in:
Sasha Koshka 2024-12-07 02:34:40 -05:00
parent 7c9e1a429e
commit 62885b2e37

View File

@ -34,6 +34,7 @@ func (this *Handler) ServeHTTP (res http.ResponseWriter, req *http.Request) {
pat = "/" + pat pat = "/" + pat
req.URL.Path = pat req.URL.Path = pat
} }
hasTrailingSlash := strings.HasSuffix(pat, "/")
pat = path.Clean(req.URL.Path) pat = path.Clean(req.URL.Path)
info, err := statFile(filesystem, pathToName(pat)) info, err := statFile(filesystem, pathToName(pat))
@ -44,6 +45,12 @@ func (this *Handler) ServeHTTP (res http.ResponseWriter, req *http.Request) {
return return
} }
if info.IsDir() { if info.IsDir() {
// ensure the path ends with a /
if !hasTrailingSlash {
http.Redirect(res, req, pat + "/", http.StatusMovedPermanently)
return
}
// try to find an index // try to find an index
for _, base := range this.Index { for _, base := range this.Index {
currentPath := path.Join(pat, base) currentPath := path.Join(pat, base)