diff --git a/modules/caddyhttp/fileserver/browse.go b/modules/caddyhttp/fileserver/browse.go index d90751f16..7bf388944 100644 --- a/modules/caddyhttp/fileserver/browse.go +++ b/modules/caddyhttp/fileserver/browse.go @@ -128,7 +128,7 @@ func (fsrv *FileServer) serveBrowse(fileSystem fs.FS, root, dirPath string, w ht case errors.Is(err, fs.ErrPermission): return caddyhttp.Error(http.StatusForbidden, err) case errors.Is(err, fs.ErrNotExist): - return fsrv.notFound(w, r, next) + return fsrv.notFound(w, r, next, err) case err != nil: return caddyhttp.Error(http.StatusInternalServerError, err) } diff --git a/modules/caddyhttp/fileserver/staticfiles.go b/modules/caddyhttp/fileserver/staticfiles.go index 9c31d2398..40ca71bf6 100644 --- a/modules/caddyhttp/fileserver/staticfiles.go +++ b/modules/caddyhttp/fileserver/staticfiles.go @@ -333,7 +333,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c if err != nil { err = fsrv.mapDirOpenError(fileSystem, err, filename) if errors.Is(err, fs.ErrNotExist) { - return fsrv.notFound(w, r, next) + return fsrv.notFound(w, r, next, err) } else if errors.Is(err, fs.ErrInvalid) { return caddyhttp.Error(http.StatusBadRequest, err) } else if errors.Is(err, fs.ErrPermission) { @@ -395,7 +395,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c if fsrv.Browse != nil && !fileHidden(filename, filesToHide) { return fsrv.serveBrowse(fileSystem, root, filename, w, r, next) } - return fsrv.notFound(w, r, next) + return fsrv.notFound(w, r, next, fmt.Errorf("directory without index file or browse enabled")) } // one last check to ensure the file isn't hidden (we might @@ -407,7 +407,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c zap.Strings("files_to_hide", filesToHide), ) } - return fsrv.notFound(w, r, next) + return fsrv.notFound(w, r, next, fmt.Errorf("file is hidden")) } // if URL canonicalization is enabled, we need to enforce trailing @@ -540,7 +540,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c if err != nil { if herr, ok := err.(caddyhttp.HandlerError); ok && herr.StatusCode == http.StatusNotFound { - return fsrv.notFound(w, r, next) + return fsrv.notFound(w, r, next, herr.Err) } return err // error is already structured } @@ -811,11 +811,11 @@ func foldCase(s string) string { // notFound returns a 404 error or, if pass-thru is enabled, // it calls the next handler in the chain. -func (fsrv *FileServer) notFound(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { +func (fsrv *FileServer) notFound(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler, err error) error { if fsrv.PassThru { return next.ServeHTTP(w, r) } - return caddyhttp.Error(http.StatusNotFound, nil) + return caddyhttp.Error(http.StatusNotFound, err) } // Indicates whether a file's modification time is useful for validator