Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
fileserver: Add debug logging
Matthew Holt committed
Nov 26, 2020 at 09:37 UTC
7d7434c9ce9778adf7b7ff6c3a6575ea1c11ed01
2 files changed
+30
modules/caddyhttp/fileserver/browse.go
+6
@@ -25,6 +25,7 @@ import (
25
26
"github.com/caddyserver/caddy/v2"
27
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
28
+ "go.uber.org/zap"
29
)
30
31
// Browse configures directory browsing.
@@ -36,11 +37,16 @@ type Browse struct {
37
}
38
39
func (fsrv *FileServer) serveBrowse(root, dirPath string, w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
40
+ fsrv.logger.Debug("browse enabled; listing directory contents",
41
+ zap.String("path", dirPath),
42
+ zap.String("root", root))
43
+
44
// navigation on the client-side gets messed up if the
45
// URL doesn't end in a trailing slash because hrefs like
46
// "/b/c" on a path like "/a" end up going to "/b/c" instead
47
// of "/a/b/c" - so we have to redirect in this case
48
if !strings.HasSuffix(r.URL.Path, "/") {
49
+ fsrv.logger.Debug("redirecting to trailing slash to preserve hrefs", zap.String("request_path", r.URL.Path))
50
r.URL.Path += "/"
51
http.Redirect(w, r, r.URL.String(), http.StatusMovedPermanently)
52
return nil
modules/caddyhttp/fileserver/staticfiles.go
+24
@@ -31,6 +31,7 @@ import (
31
32
"github.com/caddyserver/caddy/v2"
33
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
34
+ "go.uber.org/zap"
35
)
36
37
func init() {
@@ -77,6 +78,8 @@ type FileServer struct {
78
// it will invoke the next handler in the chain instead of returning
79
// a 404 error. By default, this is false (disabled).
80
PassThru bool `json:"pass_thru,omitempty"`
81
+
82
+ logger *zap.Logger
83
}
84
85
// CaddyModule returns the Caddy module information.
@@ -89,6 +92,8 @@ func (FileServer) CaddyModule() caddy.ModuleInfo {
92
93
// Provision sets up the static files responder.
94
func (fsrv *FileServer) Provision(ctx caddy.Context) error {
95
+ fsrv.logger = ctx.Logger(fsrv)
96
+
97
if fsrv.Root == "" {
98
fsrv.Root = "{http.vars.root}"
99
}
@@ -136,6 +141,11 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
141
suffix := repl.ReplaceAll(r.URL.Path, "")
142
filename := sanitizedPathJoin(root, suffix)
143
144
+ fsrv.logger.Debug("sanitized path join",
145
+ zap.String("site_root", root),
146
+ zap.String("request_path", suffix),
147
+ zap.String("result", filename))
148
+
149
// get information about the file
150
info, err := os.Stat(filename)
151
if err != nil {
@@ -157,6 +167,9 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
167
indexPath := sanitizedPathJoin(filename, indexPage)
168
if fileHidden(indexPath, filesToHide) {
169
// pretend this file doesn't exist
170
+ fsrv.logger.Debug("hiding index file",
171
+ zap.String("filename", indexPath),
172
+ zap.Strings("files_to_hide", filesToHide))
173
continue
174
}
175
@@ -176,6 +189,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
189
info = indexInfo
190
filename = indexPath
191
implicitIndexFile = true
192
+ fsrv.logger.Debug("located index file", zap.String("filename", filename))
193
break
194
}
195
}
@@ -183,6 +197,9 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
197
// if still referencing a directory, delegate
198
// to browse or return an error
199
if info.IsDir() {
200
+ fsrv.logger.Debug("no index file in directory",
201
+ zap.String("path", filename),
202
+ zap.Strings("index_filenames", fsrv.IndexNames))
203
if fsrv.Browse != nil && !fileHidden(filename, filesToHide) {
204
return fsrv.serveBrowse(root, filename, w, r, next)
205
}
@@ -194,6 +211,9 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
211
// one last check to ensure the file isn't hidden (we might
212
// have changed the filename from when we last checked)
213
if fileHidden(filename, filesToHide) {
214
+ fsrv.logger.Debug("hiding file",
215
+ zap.String("filename", filename),
216
+ zap.Strings("files_to_hide", filesToHide))
217
return fsrv.notFound(w, r, next)
218
}
219
@@ -203,12 +223,16 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
223
// in HTML (see https://github.com/caddyserver/caddy/issues/2741)
224
if fsrv.CanonicalURIs == nil || *fsrv.CanonicalURIs {
225
if implicitIndexFile && !strings.HasSuffix(r.URL.Path, "/") {
226
+ fsrv.logger.Debug("redirecting to canonical URI (adding trailing slash for directory)", zap.String("path", r.URL.Path))
227
return redirect(w, r, r.URL.Path+"/")
228
} else if !implicitIndexFile && strings.HasSuffix(r.URL.Path, "/") {
229
+ fsrv.logger.Debug("redirecting to canonical URI (removing trailing slash for file)", zap.String("path", r.URL.Path))
230
return redirect(w, r, r.URL.Path[:len(r.URL.Path)-1])
231
}
232
}
233
234
+ fsrv.logger.Debug("opening file", zap.String("filename", filename))
235
+
236
// open the file
237
file, err := fsrv.openFile(filename, w)
238
if err != nil {