summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2020-11-26 09:37:42 -0700
committerMatthew Holt <mholt@users.noreply.github.com>2020-11-26 09:37:42 -0700
commit7d7434c9ce9778adf7b7ff6c3a6575ea1c11ed01 (patch)
tree357751e55af98608901c5614d1c2dc775d74d76e
parent53aa60afff2e66b4a2b2ca6968fe5cb6c8567437 (diff)
fileserver: Add debug logging
-rw-r--r--modules/caddyhttp/fileserver/browse.go6
-rw-r--r--modules/caddyhttp/fileserver/staticfiles.go24
2 files changed, 30 insertions, 0 deletions
diff --git a/modules/caddyhttp/fileserver/browse.go b/modules/caddyhttp/fileserver/browse.go
index af3620a..1f6986f 100644
--- a/modules/caddyhttp/fileserver/browse.go
+++ b/modules/caddyhttp/fileserver/browse.go
@@ -25,6 +25,7 @@ import (
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
+ "go.uber.org/zap"
)
// Browse configures directory browsing.
@@ -36,11 +37,16 @@ type Browse struct {
}
func (fsrv *FileServer) serveBrowse(root, dirPath string, w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
+ fsrv.logger.Debug("browse enabled; listing directory contents",
+ zap.String("path", dirPath),
+ zap.String("root", root))
+
// navigation on the client-side gets messed up if the
// URL doesn't end in a trailing slash because hrefs like
// "/b/c" on a path like "/a" end up going to "/b/c" instead
// of "/a/b/c" - so we have to redirect in this case
if !strings.HasSuffix(r.URL.Path, "/") {
+ fsrv.logger.Debug("redirecting to trailing slash to preserve hrefs", zap.String("request_path", r.URL.Path))
r.URL.Path += "/"
http.Redirect(w, r, r.URL.String(), http.StatusMovedPermanently)
return nil
diff --git a/modules/caddyhttp/fileserver/staticfiles.go b/modules/caddyhttp/fileserver/staticfiles.go
index f640564..daf4b54 100644
--- a/modules/caddyhttp/fileserver/staticfiles.go
+++ b/modules/caddyhttp/fileserver/staticfiles.go
@@ -31,6 +31,7 @@ import (
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
+ "go.uber.org/zap"
)
func init() {
@@ -77,6 +78,8 @@ type FileServer struct {
// it will invoke the next handler in the chain instead of returning
// a 404 error. By default, this is false (disabled).
PassThru bool `json:"pass_thru,omitempty"`
+
+ logger *zap.Logger
}
// CaddyModule returns the Caddy module information.
@@ -89,6 +92,8 @@ func (FileServer) CaddyModule() caddy.ModuleInfo {
// Provision sets up the static files responder.
func (fsrv *FileServer) Provision(ctx caddy.Context) error {
+ fsrv.logger = ctx.Logger(fsrv)
+
if fsrv.Root == "" {
fsrv.Root = "{http.vars.root}"
}
@@ -136,6 +141,11 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
suffix := repl.ReplaceAll(r.URL.Path, "")
filename := sanitizedPathJoin(root, suffix)
+ fsrv.logger.Debug("sanitized path join",
+ zap.String("site_root", root),
+ zap.String("request_path", suffix),
+ zap.String("result", filename))
+
// get information about the file
info, err := os.Stat(filename)
if err != nil {
@@ -157,6 +167,9 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
indexPath := sanitizedPathJoin(filename, indexPage)
if fileHidden(indexPath, filesToHide) {
// pretend this file doesn't exist
+ fsrv.logger.Debug("hiding index file",
+ zap.String("filename", indexPath),
+ zap.Strings("files_to_hide", filesToHide))
continue
}
@@ -176,6 +189,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
info = indexInfo
filename = indexPath
implicitIndexFile = true
+ fsrv.logger.Debug("located index file", zap.String("filename", filename))
break
}
}
@@ -183,6 +197,9 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
// if still referencing a directory, delegate
// to browse or return an error
if info.IsDir() {
+ fsrv.logger.Debug("no index file in directory",
+ zap.String("path", filename),
+ zap.Strings("index_filenames", fsrv.IndexNames))
if fsrv.Browse != nil && !fileHidden(filename, filesToHide) {
return fsrv.serveBrowse(root, filename, w, r, next)
}
@@ -194,6 +211,9 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
// one last check to ensure the file isn't hidden (we might
// have changed the filename from when we last checked)
if fileHidden(filename, filesToHide) {
+ fsrv.logger.Debug("hiding file",
+ zap.String("filename", filename),
+ zap.Strings("files_to_hide", filesToHide))
return fsrv.notFound(w, r, next)
}
@@ -203,12 +223,16 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
// in HTML (see https://github.com/caddyserver/caddy/issues/2741)
if fsrv.CanonicalURIs == nil || *fsrv.CanonicalURIs {
if implicitIndexFile && !strings.HasSuffix(r.URL.Path, "/") {
+ fsrv.logger.Debug("redirecting to canonical URI (adding trailing slash for directory)", zap.String("path", r.URL.Path))
return redirect(w, r, r.URL.Path+"/")
} else if !implicitIndexFile && strings.HasSuffix(r.URL.Path, "/") {
+ fsrv.logger.Debug("redirecting to canonical URI (removing trailing slash for file)", zap.String("path", r.URL.Path))
return redirect(w, r, r.URL.Path[:len(r.URL.Path)-1])
}
}
+ fsrv.logger.Debug("opening file", zap.String("filename", filename))
+
// open the file
file, err := fsrv.openFile(filename, w)
if err != nil {