fileserver: handle empty files in size try policies (#8322)
Use the chosen file info, not a zero size, to tell whether a candidate was chosen, so smallest_size keeps an empty file and largest_size can choose one.
This commit is contained in:
parent
dccce45660
commit
cce035d7ad
2 changed files with 74 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue