diff --git a/modules/caddyhttp/fileserver/matcher.go b/modules/caddyhttp/fileserver/matcher.go index 57cf12dc2..390126b07 100644 --- a/modules/caddyhttp/fileserver/matcher.go +++ b/modules/caddyhttp/fileserver/matcher.go @@ -500,7 +500,7 @@ func (m MatchFile) selectFile(r *http.Request) (bool, error) { candidates := makeCandidates(pattern) for _, c := range candidates { info, err := fs.Stat(fileSystem, c.fullpath) - if err == nil && info.Size() > largestSize { + if err == nil && (largestInfo == nil || info.Size() > largestSize) { largestSize = info.Size() largest = c largestInfo = info @@ -521,7 +521,7 @@ func (m MatchFile) selectFile(r *http.Request) (bool, error) { candidates := makeCandidates(pattern) for _, c := range candidates { info, err := fs.Stat(fileSystem, c.fullpath) - if err == nil && (smallestSize == 0 || info.Size() < smallestSize) { + if err == nil && (smallestInfo == nil || info.Size() < smallestSize) { smallestSize = info.Size() smallest = c smallestInfo = info diff --git a/modules/caddyhttp/fileserver/matcher_test.go b/modules/caddyhttp/fileserver/matcher_test.go index e3496b79f..0983b4872 100644 --- a/modules/caddyhttp/fileserver/matcher_test.go +++ b/modules/caddyhttp/fileserver/matcher_test.go @@ -786,3 +786,75 @@ func TestLiteralGlobNameMatchesOnlyAsFallback(t *testing.T) { } } } + +// An empty file is a valid candidate for the size policies: smallest_size +// must keep it once chosen, and largest_size must be able to choose it. +func TestSizePoliciesWithEmptyFiles(t *testing.T) { + root := t.TempDir() + for name, content := range map[string]string{ + "empty.txt": "", + "small.txt": "x", + "big.txt": "xxxxx", + } { + if err := os.WriteFile(filepath.Join(root, name), []byte(content), 0o644); err != nil { + t.Fatal(err) + } + } + + for _, tc := range []struct { + name string + tryFiles []string + policy string + expectedPath string + }{ + { + name: "smallest_size keeps an empty file listed first", + tryFiles: []string{"empty.txt", "small.txt", "big.txt"}, + policy: tryPolicySmallestSize, + expectedPath: "/empty.txt", + }, + { + name: "smallest_size chooses an empty file listed last", + tryFiles: []string{"big.txt", "small.txt", "empty.txt"}, + policy: tryPolicySmallestSize, + expectedPath: "/empty.txt", + }, + { + name: "largest_size chooses the only file even if empty", + tryFiles: []string{"missing.txt", "empty.txt"}, + policy: tryPolicyLargestSize, + expectedPath: "/empty.txt", + }, + { + name: "largest_size prefers a larger file after an empty one", + tryFiles: []string{"empty.txt", "big.txt", "small.txt"}, + policy: tryPolicyLargestSize, + expectedPath: "/big.txt", + }, + } { + t.Run(tc.name, func(t *testing.T) { + m := &MatchFile{ + fsmap: &filesystems.FileSystemMap{}, + Root: root, + TryFiles: tc.tryFiles, + TryPolicy: tc.policy, + } + + req := &http.Request{URL: &url.URL{Path: "/"}} + repl := caddyhttp.NewTestReplacer(req) + + result, err := m.MatchWithError(req) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if !result { + t.Fatal("expected a match") + } + + rel, _ := repl.Get("http.matchers.file.relative") + if rel != tc.expectedPath { + t.Errorf("actual path: %v, expected: %v", rel, tc.expectedPath) + } + }) + } +}