Skip to content

Commit 8aa487d

Browse files
committed
feat: add static filename and info served by fileserver to context vars
Signed-off-by: Arthur <git@arthur.pro>
1 parent 105eee6 commit 8aa487d

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

modules/caddyhttp/fileserver/staticfiles.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ func init() {
4242
caddy.RegisterModule(FileServer{})
4343
}
4444

45+
const FileServerFilenameVarKey = "fileserver_filename"
46+
const FileServerFileinfoVarKey = "fileserver_fileinfo"
47+
4548
// FileServer implements a handler that serves static files.
4649
//
4750
// The path of the file to serve is constructed by joining the site root
@@ -559,6 +562,14 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
559562
w = statusOverrideResponseWriter{ResponseWriter: w, code: statusCodeOverride}
560563
}
561564

565+
// add filename and file info to the request context vars so that other middlewares
566+
// can find out what static file has been served by the fileserver.
567+
// middlewares can access the vars as follows:
568+
// caddyhttp.GetVar(r.Context(), fileserver.FileServerFilenameVarKey).(string)
569+
// caddyhttp.GetVar(r.Context(), fileserver.FileServerFileinfoVarKey).(fs.FileInfo)
570+
caddyhttp.SetVar(r.Context(), FileServerFilenameVarKey, filename)
571+
caddyhttp.SetVar(r.Context(), FileServerFileinfoVarKey, info)
572+
562573
// let the standard library do what it does best; note, however,
563574
// that errors generated by ServeContent are written immediately
564575
// to the response, so we cannot handle them (but errors there

0 commit comments

Comments
 (0)