diff --git a/modules/caddyhttp/fileserver/calculate_etag_bench_test.go b/modules/caddyhttp/fileserver/calculate_etag_bench_test.go index ab6cde791..004e57335 100644 --- a/modules/caddyhttp/fileserver/calculate_etag_bench_test.go +++ b/modules/caddyhttp/fileserver/calculate_etag_bench_test.go @@ -1,3 +1,17 @@ +// Copyright 2015 Matthew Holt and The Caddy Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package fileserver import ( @@ -12,11 +26,11 @@ type benchFileInfo struct { } func (fi benchFileInfo) Name() string { return "file.txt" } -func (fi benchFileInfo) Size() int64 { return fi.size } -func (fi benchFileInfo) Mode() fs.FileMode { return 0o644 } -func (fi benchFileInfo) ModTime() time.Time { return fi.mtime } -func (fi benchFileInfo) IsDir() bool { return false } -func (fi benchFileInfo) Sys() any { return nil } +func (fi benchFileInfo) Size() int64 { return fi.size } +func (fi benchFileInfo) Mode() fs.FileMode { return 0o644 } +func (fi benchFileInfo) ModTime() time.Time { return fi.mtime } +func (fi benchFileInfo) IsDir() bool { return false } +func (fi benchFileInfo) Sys() any { return nil } func BenchmarkCalculateEtag(b *testing.B) { fi := benchFileInfo{size: 1234567, mtime: time.Unix(1700000000, 123456789)} diff --git a/modules/caddyhttp/fileserver/staticfiles.go b/modules/caddyhttp/fileserver/staticfiles.go index ead284dd7..09062b446 100644 --- a/modules/caddyhttp/fileserver/staticfiles.go +++ b/modules/caddyhttp/fileserver/staticfiles.go @@ -779,11 +779,11 @@ func calculateEtag(d os.FileInfo) string { if !usefulModTime(mtime) { return "" } - // A quoted etag of two base-36 int64 values is 30 bytes, thus fitting - // comfortably in 32 bytes, on the stack to prevent allocations + // Two base-36 int64 values, a separator and quotes need at most 31 bytes. var buf [32]byte b := append(buf[:0], '"') b = strconv.AppendInt(b, mtime.UnixNano(), 36) + b = append(b, '-') b = strconv.AppendInt(b, d.Size(), 36) b = append(b, '"') return string(b)