mirror of
https://github.com/caddyserver/caddy.git
synced 2025-05-24 02:02:26 -04:00
file_server: use the UTC timezone for modified time (#6830)
Some checks failed
Tests / test (./cmd/caddy/caddy, ~1.22.3, ubuntu-latest, 0, 1.22, linux) (push) Failing after 1m41s
Tests / test (./cmd/caddy/caddy, ~1.23.0, ubuntu-latest, 0, 1.23, linux) (push) Failing after 1m13s
Tests / test (s390x on IBM Z) (push) Has been skipped
Tests / goreleaser-check (push) Has been skipped
Cross-Build / build (~1.22.3, 1.22, aix) (push) Successful in 1m24s
Cross-Build / build (~1.22.3, 1.22, darwin) (push) Successful in 1m24s
Cross-Build / build (~1.22.3, 1.22, dragonfly) (push) Successful in 1m23s
Cross-Build / build (~1.22.3, 1.22, freebsd) (push) Successful in 1m27s
Cross-Build / build (~1.22.3, 1.22, illumos) (push) Successful in 1m24s
Cross-Build / build (~1.22.3, 1.22, linux) (push) Successful in 1m21s
Cross-Build / build (~1.22.3, 1.22, netbsd) (push) Successful in 1m26s
Cross-Build / build (~1.22.3, 1.22, openbsd) (push) Successful in 1m22s
Cross-Build / build (~1.22.3, 1.22, solaris) (push) Successful in 1m21s
Cross-Build / build (~1.22.3, 1.22, windows) (push) Successful in 1m20s
Cross-Build / build (~1.23.0, 1.23, aix) (push) Successful in 1m19s
Cross-Build / build (~1.23.0, 1.23, darwin) (push) Successful in 1m16s
Cross-Build / build (~1.23.0, 1.23, dragonfly) (push) Successful in 1m15s
Cross-Build / build (~1.23.0, 1.23, freebsd) (push) Successful in 1m13s
Cross-Build / build (~1.23.0, 1.23, illumos) (push) Successful in 1m13s
Cross-Build / build (~1.23.0, 1.23, linux) (push) Successful in 1m13s
Cross-Build / build (~1.23.0, 1.23, netbsd) (push) Successful in 1m14s
Cross-Build / build (~1.23.0, 1.23, openbsd) (push) Successful in 1m17s
Cross-Build / build (~1.23.0, 1.23, solaris) (push) Successful in 1m13s
Cross-Build / build (~1.23.0, 1.23, windows) (push) Successful in 1m18s
Lint / lint (ubuntu-latest, linux) (push) Successful in 2m9s
Lint / govulncheck (push) Successful in 1m18s
Tests / test (./cmd/caddy/caddy, ~1.22.3, macos-14, 0, 1.22, mac) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy, ~1.23.0, macos-14, 0, 1.23, mac) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy.exe, ~1.22.3, windows-latest, True, 1.22, windows) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy.exe, ~1.23.0, windows-latest, True, 1.23, windows) (push) Has been cancelled
Lint / lint (macos-14, mac) (push) Has been cancelled
Lint / lint (windows-latest, windows) (push) Has been cancelled
Some checks failed
Tests / test (./cmd/caddy/caddy, ~1.22.3, ubuntu-latest, 0, 1.22, linux) (push) Failing after 1m41s
Tests / test (./cmd/caddy/caddy, ~1.23.0, ubuntu-latest, 0, 1.23, linux) (push) Failing after 1m13s
Tests / test (s390x on IBM Z) (push) Has been skipped
Tests / goreleaser-check (push) Has been skipped
Cross-Build / build (~1.22.3, 1.22, aix) (push) Successful in 1m24s
Cross-Build / build (~1.22.3, 1.22, darwin) (push) Successful in 1m24s
Cross-Build / build (~1.22.3, 1.22, dragonfly) (push) Successful in 1m23s
Cross-Build / build (~1.22.3, 1.22, freebsd) (push) Successful in 1m27s
Cross-Build / build (~1.22.3, 1.22, illumos) (push) Successful in 1m24s
Cross-Build / build (~1.22.3, 1.22, linux) (push) Successful in 1m21s
Cross-Build / build (~1.22.3, 1.22, netbsd) (push) Successful in 1m26s
Cross-Build / build (~1.22.3, 1.22, openbsd) (push) Successful in 1m22s
Cross-Build / build (~1.22.3, 1.22, solaris) (push) Successful in 1m21s
Cross-Build / build (~1.22.3, 1.22, windows) (push) Successful in 1m20s
Cross-Build / build (~1.23.0, 1.23, aix) (push) Successful in 1m19s
Cross-Build / build (~1.23.0, 1.23, darwin) (push) Successful in 1m16s
Cross-Build / build (~1.23.0, 1.23, dragonfly) (push) Successful in 1m15s
Cross-Build / build (~1.23.0, 1.23, freebsd) (push) Successful in 1m13s
Cross-Build / build (~1.23.0, 1.23, illumos) (push) Successful in 1m13s
Cross-Build / build (~1.23.0, 1.23, linux) (push) Successful in 1m13s
Cross-Build / build (~1.23.0, 1.23, netbsd) (push) Successful in 1m14s
Cross-Build / build (~1.23.0, 1.23, openbsd) (push) Successful in 1m17s
Cross-Build / build (~1.23.0, 1.23, solaris) (push) Successful in 1m13s
Cross-Build / build (~1.23.0, 1.23, windows) (push) Successful in 1m18s
Lint / lint (ubuntu-latest, linux) (push) Successful in 2m9s
Lint / govulncheck (push) Successful in 1m18s
Tests / test (./cmd/caddy/caddy, ~1.22.3, macos-14, 0, 1.22, mac) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy, ~1.23.0, macos-14, 0, 1.23, mac) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy.exe, ~1.22.3, windows-latest, True, 1.22, windows) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy.exe, ~1.23.0, windows-latest, True, 1.23, windows) (push) Has been cancelled
Lint / lint (macos-14, mac) (push) Has been cancelled
Lint / lint (windows-latest, windows) (push) Has been cancelled
* 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
This commit is contained in:
parent
9b74a53e51
commit
22563a70eb
@ -130,9 +130,9 @@ func (fsrv *FileServer) serveBrowse(fileSystem fs.FS, root, dirPath string, w ht
|
||||
|
||||
// speed up browser/client experience and caching by supporting If-Modified-Since
|
||||
if ifModSinceStr := r.Header.Get("If-Modified-Since"); ifModSinceStr != "" {
|
||||
ifModSince, err := time.ParseInLocation(http.TimeFormat, ifModSinceStr, time.Local)
|
||||
lastModTrunc := listing.lastModified.Truncate(time.Second)
|
||||
if err == nil && (lastModTrunc.Equal(ifModSince) || lastModTrunc.Before(ifModSince)) {
|
||||
// basically a copy of stdlib file server's handling of If-Modified-Since
|
||||
ifModSince, err := http.ParseTime(ifModSinceStr)
|
||||
if err == nil && listing.lastModified.Truncate(time.Second).Compare(ifModSince) <= 0 {
|
||||
w.WriteHeader(http.StatusNotModified)
|
||||
return nil
|
||||
}
|
||||
@ -213,6 +213,11 @@ func (fsrv *FileServer) serveBrowse(fileSystem fs.FS, root, dirPath string, w ht
|
||||
}
|
||||
|
||||
func (fsrv *FileServer) loadDirectoryContents(ctx context.Context, fileSystem fs.FS, dir fs.ReadDirFile, root, urlPath string, repl *caddy.Replacer) (*browseTemplateContext, error) {
|
||||
// modTime for the directory itself
|
||||
stat, err := dir.Stat()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dirLimit := defaultDirEntryLimit
|
||||
if fsrv.Browse.FileLimit != 0 {
|
||||
dirLimit = fsrv.Browse.FileLimit
|
||||
@ -225,7 +230,7 @@ func (fsrv *FileServer) loadDirectoryContents(ctx context.Context, fileSystem fs
|
||||
// user can presumably browse "up" to parent folder if path is longer than "/"
|
||||
canGoUp := len(urlPath) > 1
|
||||
|
||||
return fsrv.directoryListing(ctx, fileSystem, files, canGoUp, root, urlPath, repl), nil
|
||||
return fsrv.directoryListing(ctx, fileSystem, stat.ModTime(), files, canGoUp, root, urlPath, repl), nil
|
||||
}
|
||||
|
||||
// browseApplyQueryParams applies query parameters to the listing.
|
||||
|
@ -35,15 +35,16 @@ import (
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
)
|
||||
|
||||
func (fsrv *FileServer) directoryListing(ctx context.Context, fileSystem fs.FS, entries []fs.DirEntry, canGoUp bool, root, urlPath string, repl *caddy.Replacer) *browseTemplateContext {
|
||||
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 {
|
||||
filesToHide := fsrv.transformHidePaths(repl)
|
||||
|
||||
name, _ := url.PathUnescape(urlPath)
|
||||
|
||||
tplCtx := &browseTemplateContext{
|
||||
Name: path.Base(name),
|
||||
Path: urlPath,
|
||||
CanGoUp: canGoUp,
|
||||
Name: path.Base(name),
|
||||
Path: urlPath,
|
||||
CanGoUp: canGoUp,
|
||||
lastModified: parentModTime,
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
@ -131,6 +132,10 @@ func (fsrv *FileServer) directoryListing(ctx context.Context, fileSystem fs.FS,
|
||||
})
|
||||
}
|
||||
|
||||
// this time is used for the Last-Modified header and comparing If-Modified-Since from client
|
||||
// both are expected to be in UTC, so we convert to UTC here
|
||||
// see: https://github.com/caddyserver/caddy/issues/6828
|
||||
tplCtx.lastModified = tplCtx.lastModified.UTC()
|
||||
return tplCtx
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user