httpd: disable directory index for static files

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino
2023-09-08 19:55:45 +02:00
parent dc7c829b73
commit fac022090d
6 changed files with 31 additions and 12 deletions

View File

@@ -1047,7 +1047,7 @@ func getServicesStatus() *ServicesStatus {
return status
}
func fileServer(r chi.Router, path string, root http.FileSystem) {
func fileServer(r chi.Router, path string, root http.FileSystem, disableDirectoryIndex bool) {
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", http.StatusMovedPermanently).ServeHTTP)
path += "/"
@@ -1057,7 +1057,11 @@ func fileServer(r chi.Router, path string, root http.FileSystem) {
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
fs := http.StripPrefix(pathPrefix, http.FileServer(root))
handler := http.FileServer(root)
if disableDirectoryIndex {
handler = neuter(handler)
}
fs := http.StripPrefix(pathPrefix, handler)
fs.ServeHTTP(w, r)
})
}