Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 03:15 UTC
file_server: use the UTC timezone for modified time (#6830)
* use UTC timezone for modified time * use http.ParseTime to handle If-Modified-Since * use time.Compare to simplify comparison * take the directory's modtime into consideration when calculating lastModified * update comments about If-Modified-Since's handling
WeidiDeng committed
Feb 10, 2025 at 23:39 UTC
22563a70eb7b590fcb698680a3ec6d76c0968748
2 files changed
+18
-8
modules/caddyhttp/fileserver/browse.go
+9
-4
@@ -130,9 +130,9 @@ func (fsrv *FileServer) serveBrowse(fileSystem fs.FS, root, dirPath string, w ht
130
131
// speed up browser/client experience and caching by supporting If-Modified-Since
132
if ifModSinceStr := r.Header.Get("If-Modified-Since"); ifModSinceStr != "" {
133
- ifModSince, err := time.ParseInLocation(http.TimeFormat, ifModSinceStr, time.Local)
134
- lastModTrunc := listing.lastModified.Truncate(time.Second)
135
- if err == nil && (lastModTrunc.Equal(ifModSince) || lastModTrunc.Before(ifModSince)) {
133
+ // basically a copy of stdlib file server's handling of If-Modified-Since
134
+ ifModSince, err := http.ParseTime(ifModSinceStr)
135
+ if err == nil && listing.lastModified.Truncate(time.Second).Compare(ifModSince) <= 0 {
136
w.WriteHeader(http.StatusNotModified)
137
return nil
138
}
@@ -213,6 +213,11 @@ func (fsrv *FileServer) serveBrowse(fileSystem fs.FS, root, dirPath string, w ht
213
}
214
215
func (fsrv *FileServer) loadDirectoryContents(ctx context.Context, fileSystem fs.FS, dir fs.ReadDirFile, root, urlPath string, repl *caddy.Replacer) (*browseTemplateContext, error) {
216
+ // modTime for the directory itself
217
+ stat, err := dir.Stat()
218
+ if err != nil {
219
+ return nil, err
220
+ }
221
dirLimit := defaultDirEntryLimit
222
if fsrv.Browse.FileLimit != 0 {
223
dirLimit = fsrv.Browse.FileLimit
@@ -225,7 +230,7 @@ func (fsrv *FileServer) loadDirectoryContents(ctx context.Context, fileSystem fs
230
// user can presumably browse "up" to parent folder if path is longer than "/"
231
canGoUp := len(urlPath) > 1
232
228
- return fsrv.directoryListing(ctx, fileSystem, files, canGoUp, root, urlPath, repl), nil
233
+ return fsrv.directoryListing(ctx, fileSystem, stat.ModTime(), files, canGoUp, root, urlPath, repl), nil
234
}
235
236
// browseApplyQueryParams applies query parameters to the listing.
modules/caddyhttp/fileserver/browsetplcontext.go
+9
-4
@@ -35,15 +35,16 @@ import (
35
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
36
)
37
38
-func (fsrv *FileServer) directoryListing(ctx context.Context, fileSystem fs.FS, entries []fs.DirEntry, canGoUp bool, root, urlPath string, repl *caddy.Replacer) *browseTemplateContext {
38
+func (fsrv *FileServer) directoryListing(ctx context.Context, fileSystem fs.FS, parentModTime time.Time, entries []fs.DirEntry, canGoUp bool, root, urlPath string, repl *caddy.Replacer) *browseTemplateContext {
39
filesToHide := fsrv.transformHidePaths(repl)
40
41
name, _ := url.PathUnescape(urlPath)
42
43
tplCtx := &browseTemplateContext{
44
- Name: path.Base(name),
45
- Path: urlPath,
46
- CanGoUp: canGoUp,
44
+ Name: path.Base(name),
45
+ Path: urlPath,
46
+ CanGoUp: canGoUp,
47
+ lastModified: parentModTime,
48
}
49
50
for _, entry := range entries {
@@ -131,6 +132,10 @@ func (fsrv *FileServer) directoryListing(ctx context.Context, fileSystem fs.FS,
132
})
133
}
134
135
+ // this time is used for the Last-Modified header and comparing If-Modified-Since from client
136
+ // both are expected to be in UTC, so we convert to UTC here
137
+ // see: https://github.com/caddyserver/caddy/issues/6828
138
+ tplCtx.lastModified = tplCtx.lastModified.UTC()
139
return tplCtx
140
}
141