| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package fileserverscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/ |
| 16 | |
| 17 | import ( |
| 18 | "contextscip-go gomod github.com/golang/go/src go1.25.1 context/" |
| 19 | "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/" |
| 20 | "net/http/httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/" |
| 21 | "net/urlscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/" |
| 22 | "osscip-go gomod github.com/golang/go/src go1.25.1 os/" |
| 23 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/" |
| 24 | "runtimescip-go gomod github.com/golang/go/src go1.25.1 runtime/" |
| 25 | "stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/" |
| 26 | "testingscip-go gomod github.com/golang/go/src go1.25.1 testing/" |
| 27 | |
| 28 | "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/" |
| 29 | "github.com/caddyserver/caddy/v2/internal/filesystemsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/filesystems`/" |
| 30 | "github.com/caddyserver/caddy/v2/modules/caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/" |
| 31 | "github.com/caddyserver/caddy/v2/modules/caddyhttp/rewritescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/" |
| 32 | ) |
| 33 | |
| 34 | type testCasescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase# struct { |
| 35 | pathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#path. string |
| 36 | expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedPath. string |
| 37 | expectedTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedType. string |
| 38 | matchedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#matched. bool |
| 39 | } |
| 40 | |
| 41 | func TestFileMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/TestFileMatcher().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 42 | // Windows doesn't like colons in files names |
| 43 | isWindows := runtimescip-go gomod github.com/golang/go/src go1.25.1 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.1 runtime/GOOS. == "windows" |
| 44 | if !isWindows { |
| 45 | filename := "with:in-name.txt" |
| 46 | f, err := osscip-go gomod github.com/golang/go/src go1.25.1 os/.Createscip-go gomod github.com/golang/go/src go1.25.1 os/Create().("./testdata/" + filename) |
| 47 | if err != nil { |
| 48 | t.Failscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fail().() |
| 49 | return |
| 50 | } |
| 51 | t.Cleanupscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Cleanup().(func() { |
| 52 | osscip-go gomod github.com/golang/go/src go1.25.1 os/.Removescip-go gomod github.com/golang/go/src go1.25.1 os/Remove().("./testdata/" + filename) |
| 53 | }) |
| 54 | f.WriteStringscip-go gomod github.com/golang/go/src go1.25.1 os/File#WriteString().(filename) |
| 55 | f.Closescip-go gomod github.com/golang/go/src go1.25.1 os/File#Close().() |
| 56 | } |
| 57 | |
| 58 | for i, tc := range []testCasescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#{ |
| 59 | { |
| 60 | pathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#path.: "/foo.txt", |
| 61 | expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedPath.: "/foo.txt", |
| 62 | expectedTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedType.: "file", |
| 63 | matchedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#matched.: true, |
| 64 | }, |
| 65 | { |
| 66 | pathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#path.: "/foo.txt/", |
| 67 | expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedPath.: "/foo.txt", |
| 68 | expectedTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedType.: "file", |
| 69 | matchedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#matched.: true, |
| 70 | }, |
| 71 | { |
| 72 | pathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#path.: "/foo.txt?a=b", |
| 73 | expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedPath.: "/foo.txt", |
| 74 | expectedTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedType.: "file", |
| 75 | matchedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#matched.: true, |
| 76 | }, |
| 77 | { |
| 78 | pathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#path.: "/foodir", |
| 79 | expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedPath.: "/foodir/", |
| 80 | expectedTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedType.: "directory", |
| 81 | matchedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#matched.: true, |
| 82 | }, |
| 83 | { |
| 84 | pathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#path.: "/foodir/", |
| 85 | expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedPath.: "/foodir/", |
| 86 | expectedTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedType.: "directory", |
| 87 | matchedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#matched.: true, |
| 88 | }, |
| 89 | { |
| 90 | pathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#path.: "/foodir/foo.txt", |
| 91 | expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedPath.: "/foodir/foo.txt", |
| 92 | expectedTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedType.: "file", |
| 93 | matchedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#matched.: true, |
| 94 | }, |
| 95 | { |
| 96 | pathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#path.: "/missingfile.php", |
| 97 | matchedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#matched.: false, |
| 98 | }, |
| 99 | { |
| 100 | pathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#path.: "ملف.txt", // the path file name is not escaped |
| 101 | expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedPath.: "/ملف.txt", |
| 102 | expectedTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedType.: "file", |
| 103 | matchedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#matched.: true, |
| 104 | }, |
| 105 | { |
| 106 | pathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#path.: urlscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/.PathEscapescip-go gomod github.com/golang/go/src go1.25.1 `net/url`/PathEscape().("ملف.txt"), // singly-escaped path |
| 107 | expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedPath.: "/ملف.txt", |
| 108 | expectedTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedType.: "file", |
| 109 | matchedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#matched.: true, |
| 110 | }, |
| 111 | { |
| 112 | pathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#path.: urlscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/.PathEscapescip-go gomod github.com/golang/go/src go1.25.1 `net/url`/PathEscape().(urlscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/.PathEscapescip-go gomod github.com/golang/go/src go1.25.1 `net/url`/PathEscape().("ملف.txt")), // doubly-escaped path |
| 113 | expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedPath.: "/%D9%85%D9%84%D9%81.txt", |
| 114 | expectedTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedType.: "file", |
| 115 | matchedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#matched.: true, |
| 116 | }, |
| 117 | { |
| 118 | pathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#path.: "./with:in-name.txt", // browsers send the request with the path as such |
| 119 | expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedPath.: "/with:in-name.txt", |
| 120 | expectedTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedType.: "file", |
| 121 | matchedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#matched.: !isWindows, |
| 122 | }, |
| 123 | } { |
| 124 | fileMatcherTestscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/fileMatcherTest().(t, i, tc) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func TestFileMatcherNonWindowsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/TestFileMatcherNonWindows().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 129 | if runtimescip-go gomod github.com/golang/go/src go1.25.1 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.1 runtime/GOOS. == "windows" { |
| 130 | return |
| 131 | } |
| 132 | |
| 133 | // this is impossible to test on Windows, but tests a security patch for other platforms |
| 134 | tc := testCasescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#{ |
| 135 | pathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#path.: "/foodir/secr%5Cet.txt", |
| 136 | expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedPath.: "/foodir/secr\\et.txt", |
| 137 | expectedTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedType.: "file", |
| 138 | matchedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#matched.: true, |
| 139 | } |
| 140 | |
| 141 | f, err := osscip-go gomod github.com/golang/go/src go1.25.1 os/.Createscip-go gomod github.com/golang/go/src go1.25.1 os/Create().(filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Join().("testdata", stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.25.1 strings/TrimPrefix().(tc.expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedPath., "/"))) |
| 142 | if err != nil { |
| 143 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("could not create test file: %v", err) |
| 144 | } |
| 145 | defer f.Closescip-go gomod github.com/golang/go/src go1.25.1 os/File#Close().() |
| 146 | defer osscip-go gomod github.com/golang/go/src go1.25.1 os/.Removescip-go gomod github.com/golang/go/src go1.25.1 os/Remove().(f.Namescip-go gomod github.com/golang/go/src go1.25.1 os/File#Name().()) |
| 147 | |
| 148 | fileMatcherTestscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/fileMatcherTest().(t, 0, tc) |
| 149 | } |
| 150 | |
| 151 | func fileMatcherTestscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/fileMatcherTest().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#, i int, tc testCasescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#) { |
| 152 | m := &MatchFilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#{ |
| 153 | fsmapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#fsmap.: &filesystemsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/filesystems`/.FileSystemMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/filesystems`/FileSystemMap#{}, |
| 154 | Rootscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#Root.: "./testdata", |
| 155 | TryFilesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#TryFiles.: []string{"{http.request.uri.path}", "{http.request.uri.path}/"}, |
| 156 | } |
| 157 | |
| 158 | u, err := urlscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/.Parsescip-go gomod github.com/golang/go/src go1.25.1 `net/url`/Parse().(tc.pathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#path.) |
| 159 | if err != nil { |
| 160 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: parsing path: %v", i, err) |
| 161 | } |
| 162 | |
| 163 | req := &httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#{URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL.: u} |
| 164 | repl := caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.NewTestReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/NewTestReplacer().(req) |
| 165 | |
| 166 | result, err := m.MatchWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#MatchWithError().(req) |
| 167 | if err != nil { |
| 168 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: unexpected error: %v", i, err) |
| 169 | } |
| 170 | if result != tc.matchedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#matched. { |
| 171 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: expected match=%t, got %t", i, tc.matchedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#matched., result) |
| 172 | } |
| 173 | |
| 174 | rel, ok := repl.Getscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#Get().("http.matchers.file.relative") |
| 175 | if !ok && result { |
| 176 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: expected replacer value", i) |
| 177 | } |
| 178 | if !result { |
| 179 | return |
| 180 | } |
| 181 | |
| 182 | if rel != tc.expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedPath. { |
| 183 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: actual path: %v, expected: %v", i, rel, tc.expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedPath.) |
| 184 | } |
| 185 | |
| 186 | fileType, _ := repl.Getscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#Get().("http.matchers.file.type") |
| 187 | if fileType != tc.expectedTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedType. { |
| 188 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: actual file type: %v, expected: %v", i, fileType, tc.expectedTypescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/testCase#expectedType.) |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | func TestTryFilesRewriteEscapesMatchedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/TestTryFilesRewriteEscapesMatchedPath().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 193 | root := t.TempDirscip-go gomod github.com/golang/go/src go1.25.1 testing/common#TempDir().() |
| 194 | |
| 195 | tests := []struct { |
| 196 | name string |
| 197 | requestTarget string |
| 198 | filename string |
| 199 | extraFiles []string |
| 200 | wantPath string |
| 201 | wantRequestURI string |
| 202 | skipWindows bool |
| 203 | }{ |
| 204 | { |
| 205 | name: "question mark in path", |
| 206 | requestTarget: "/%3F.html", |
| 207 | filename: "?.html", |
| 208 | wantPath: "/?.html", |
| 209 | wantRequestURI: "/%3F.html", |
| 210 | skipWindows: true, |
| 211 | }, |
| 212 | { |
| 213 | name: "percent in path", |
| 214 | requestTarget: "/%25.html", |
| 215 | filename: "%.html", |
| 216 | wantPath: "/%.html", |
| 217 | wantRequestURI: "/%25.html", |
| 218 | }, |
| 219 | { |
| 220 | name: "encoded question mark remains percent-encoded", |
| 221 | requestTarget: "/%253F.html", |
| 222 | filename: "%3F.html", |
| 223 | wantPath: "/%3F.html", |
| 224 | wantRequestURI: "/%253F.html", |
| 225 | }, |
| 226 | { |
| 227 | name: "question mark in nested path", |
| 228 | requestTarget: "/nested/%3F.html", |
| 229 | filename: filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Join().("nested", "?.html"), |
| 230 | wantPath: "/nested/?.html", |
| 231 | wantRequestURI: "/nested/%3F.html", |
| 232 | skipWindows: true, |
| 233 | }, |
| 234 | { |
| 235 | name: "encoded slash in filename does not conflict with nesting", |
| 236 | requestTarget: "/nested%252Ffile.html", |
| 237 | filename: "nested%2Ffile.html", |
| 238 | extraFiles: []string{filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Join().("nested", "file.html")}, |
| 239 | wantPath: "/nested%2Ffile.html", |
| 240 | wantRequestURI: "/nested%252Ffile.html", |
| 241 | }, |
| 242 | } |
| 243 | |
| 244 | for _, tc := range tests { |
| 245 | t.Runscip-go gomod github.com/golang/go/src go1.25.1 testing/T#Run().(tc.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 246 | if tc.skipWindows && runtimescip-go gomod github.com/golang/go/src go1.25.1 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.1 runtime/GOOS. == "windows" { |
| 247 | t.Skipscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Skip().("Windows file names cannot contain question marks") |
| 248 | } |
| 249 | |
| 250 | for _, name := range append([]string{tc.filename}, tc.extraFiles...) { |
| 251 | filename := filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Join().(root, name) |
| 252 | if err := osscip-go gomod github.com/golang/go/src go1.25.1 os/.MkdirAllscip-go gomod github.com/golang/go/src go1.25.1 os/MkdirAll().(filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Dir().(filename), 0o700); err != nil { |
| 253 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("creating test file parent directory: %v", err) |
| 254 | } |
| 255 | if err := osscip-go gomod github.com/golang/go/src go1.25.1 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.1 os/WriteFile().(filename, []byte(name), 0o600); err != nil { |
| 256 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("writing test file: %v", err) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | m := &MatchFilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#{ |
| 261 | fsmapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#fsmap.: &filesystemsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/filesystems`/.FileSystemMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/filesystems`/FileSystemMap#{}, |
| 262 | Rootscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#Root.: root, |
| 263 | TryFilesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#TryFiles.: []string{"{http.request.uri.path}"}, |
| 264 | } |
| 265 | req := httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/.NewRequestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/NewRequest().(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.MethodGetscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/MethodGet., "http://example.com"+tc.requestTarget, nil) |
| 266 | repl := caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.NewTestReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/NewTestReplacer().(req) |
| 267 | |
| 268 | matched, err := m.MatchWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#MatchWithError().(req) |
| 269 | if err != nil { |
| 270 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("matching file: %v", err) |
| 271 | } |
| 272 | if !matched { |
| 273 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("expected request %s to match %s", tc.requestTarget, tc.filename) |
| 274 | } |
| 275 | |
| 276 | rewritescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/.Rewritescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#{URIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#URI.: "{http.matchers.file.relative}"}.Rewritescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#Rewrite().(req, repl) |
| 277 | |
| 278 | if req.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#Path. != tc.wantPath { |
| 279 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("rewritten path = %q, want %q", req.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#Path., tc.wantPath) |
| 280 | } |
| 281 | if req.RequestURIscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#RequestURI. != tc.wantRequestURI { |
| 282 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("rewritten request URI = %q, want %q", req.RequestURIscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#RequestURI., tc.wantRequestURI) |
| 283 | } |
| 284 | if req.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..RawQueryscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#RawQuery. != "" { |
| 285 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("rewritten raw query = %q, want empty", req.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..RawQueryscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#RawQuery.) |
| 286 | } |
| 287 | }) |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | func TestPHPFileMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/TestPHPFileMatcher().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 292 | for i, tc := range []struct { |
| 293 | path string |
| 294 | expectedPath string |
| 295 | expectedType string |
| 296 | matched bool |
| 297 | }{ |
| 298 | { |
| 299 | path: "/index.php", |
| 300 | expectedPath: "/index.php", |
| 301 | expectedType: "file", |
| 302 | matched: true, |
| 303 | }, |
| 304 | { |
| 305 | path: "/index.php/somewhere", |
| 306 | expectedPath: "/index.php", |
| 307 | expectedType: "file", |
| 308 | matched: true, |
| 309 | }, |
| 310 | { |
| 311 | path: "/remote.php", |
| 312 | expectedPath: "/remote.php", |
| 313 | expectedType: "file", |
| 314 | matched: true, |
| 315 | }, |
| 316 | { |
| 317 | path: "/remote.php/somewhere", |
| 318 | expectedPath: "/remote.php", |
| 319 | expectedType: "file", |
| 320 | matched: true, |
| 321 | }, |
| 322 | { |
| 323 | path: "/missingfile.php", |
| 324 | matched: false, |
| 325 | }, |
| 326 | { |
| 327 | path: "/notphp.php.txt", |
| 328 | expectedPath: "/notphp.php.txt", |
| 329 | expectedType: "file", |
| 330 | matched: true, |
| 331 | }, |
| 332 | { |
| 333 | path: "/notphp.php.txt/", |
| 334 | expectedPath: "/notphp.php.txt", |
| 335 | expectedType: "file", |
| 336 | matched: true, |
| 337 | }, |
| 338 | { |
| 339 | path: "/notphp.php.txt.suffixed", |
| 340 | matched: false, |
| 341 | }, |
| 342 | { |
| 343 | path: "/foo.php.php/index.php", |
| 344 | expectedPath: "/foo.php.php/index.php", |
| 345 | expectedType: "file", |
| 346 | matched: true, |
| 347 | }, |
| 348 | { |
| 349 | // See https://github.com/caddyserver/caddy/issues/3623 |
| 350 | path: "/%E2%C3", |
| 351 | expectedPath: "/%E2%C3", |
| 352 | expectedType: "file", |
| 353 | matched: false, |
| 354 | }, |
| 355 | { |
| 356 | path: "/index.php?path={path}&{query}", |
| 357 | expectedPath: "/index.php", |
| 358 | expectedType: "file", |
| 359 | matched: true, |
| 360 | }, |
| 361 | } { |
| 362 | m := &MatchFilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#{ |
| 363 | fsmapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#fsmap.: &filesystemsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/filesystems`/.FileSystemMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/filesystems`/FileSystemMap#{}, |
| 364 | Rootscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#Root.: "./testdata", |
| 365 | TryFilesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#TryFiles.: []string{"{http.request.uri.path}", "{http.request.uri.path}/index.php"}, |
| 366 | SplitPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#SplitPath.: []string{".php"}, |
| 367 | } |
| 368 | |
| 369 | u, err := urlscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/.Parsescip-go gomod github.com/golang/go/src go1.25.1 `net/url`/Parse().(tc.path) |
| 370 | if err != nil { |
| 371 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: parsing path: %v", i, err) |
| 372 | } |
| 373 | |
| 374 | req := &httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#{URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL.: u} |
| 375 | repl := caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.NewTestReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/NewTestReplacer().(req) |
| 376 | |
| 377 | result, err := m.MatchWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#MatchWithError().(req) |
| 378 | if err != nil { |
| 379 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: unexpected error: %v", i, err) |
| 380 | } |
| 381 | if result != tc.matched { |
| 382 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: expected match=%t, got %t", i, tc.matched, result) |
| 383 | } |
| 384 | |
| 385 | rel, ok := repl.Getscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#Get().("http.matchers.file.relative") |
| 386 | if !ok && result { |
| 387 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: expected replacer value", i) |
| 388 | } |
| 389 | if !result { |
| 390 | continue |
| 391 | } |
| 392 | |
| 393 | if rel != tc.expectedPath { |
| 394 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: actual path: %v, expected: %v", i, rel, tc.expectedPath) |
| 395 | } |
| 396 | |
| 397 | fileType, _ := repl.Getscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#Get().("http.matchers.file.type") |
| 398 | if fileType != tc.expectedType { |
| 399 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: actual file type: %v, expected: %v", i, fileType, tc.expectedType) |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | func TestFirstSplitscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/TestFirstSplit().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 405 | m := MatchFilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#{ |
| 406 | SplitPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#SplitPath.: []string{".php"}, |
| 407 | fsmapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#fsmap.: &filesystemsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/filesystems`/.FileSystemMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/filesystems`/FileSystemMap#{}, |
| 408 | } |
| 409 | actual, remainder := m.firstSplitscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/MatchFile#firstSplit().("index.PHP/somewhere") |
| 410 | expected := "index.PHP" |
| 411 | expectedRemainder := "/somewhere" |
| 412 | if actual != expected { |
| 413 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Expected split %s but got %s", expected, actual) |
| 414 | } |
| 415 | if remainder != expectedRemainder { |
| 416 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Expected remainder %s but got %s", expectedRemainder, remainder) |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | var expressionTestsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests. = []struct { |
| 421 | namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:name. string |
| 422 | expressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expression. *caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.MatchExpressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression# |
| 423 | urlTargetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:urlTarget. string |
| 424 | httpMethodscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:httpMethod. string |
| 425 | httpHeaderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:httpHeader. *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header# |
| 426 | wantErrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:wantErr. bool |
| 427 | wantResultscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:wantResult. bool |
| 428 | clientCertificatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:clientCertificate. []byte |
| 429 | expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expectedPath. string |
| 430 | }{ |
| 431 | { |
| 432 | namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:name.: "file error no args (MatchFile)", |
| 433 | expressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expression.: &caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.MatchExpressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#{ |
| 434 | Exprscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#Expr.: `file()`, |
| 435 | }, |
| 436 | urlTargetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:urlTarget.: "https://example.com/foo.txt", |
| 437 | wantResultscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:wantResult.: true, |
| 438 | }, |
| 439 | { |
| 440 | namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:name.: "file error bad try files (MatchFile)", |
| 441 | expressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expression.: &caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.MatchExpressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#{ |
| 442 | Exprscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#Expr.: `file({"try_file": ["bad_arg"]})`, |
| 443 | }, |
| 444 | urlTargetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:urlTarget.: "https://example.com/foo", |
| 445 | wantErrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:wantErr.: true, |
| 446 | }, |
| 447 | { |
| 448 | namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:name.: "file match short pattern index.php (MatchFile)", |
| 449 | expressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expression.: &caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.MatchExpressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#{ |
| 450 | Exprscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#Expr.: `file("index.php")`, |
| 451 | }, |
| 452 | urlTargetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:urlTarget.: "https://example.com/foo", |
| 453 | wantResultscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:wantResult.: true, |
| 454 | }, |
| 455 | { |
| 456 | namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:name.: "file match short pattern foo.txt (MatchFile)", |
| 457 | expressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expression.: &caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.MatchExpressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#{ |
| 458 | Exprscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#Expr.: `file({http.request.uri.path})`, |
| 459 | }, |
| 460 | urlTargetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:urlTarget.: "https://example.com/foo.txt", |
| 461 | wantResultscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:wantResult.: true, |
| 462 | }, |
| 463 | { |
| 464 | namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:name.: "file match index.php (MatchFile)", |
| 465 | expressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expression.: &caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.MatchExpressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#{ |
| 466 | Exprscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#Expr.: `file({"root": "./testdata", "try_files": [{http.request.uri.path}, "/index.php"]})`, |
| 467 | }, |
| 468 | urlTargetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:urlTarget.: "https://example.com/foo", |
| 469 | wantResultscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:wantResult.: true, |
| 470 | }, |
| 471 | { |
| 472 | namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:name.: "file match long pattern foo.txt (MatchFile)", |
| 473 | expressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expression.: &caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.MatchExpressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#{ |
| 474 | Exprscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#Expr.: `file({"root": "./testdata", "try_files": [{http.request.uri.path}]})`, |
| 475 | }, |
| 476 | urlTargetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:urlTarget.: "https://example.com/foo.txt", |
| 477 | wantResultscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:wantResult.: true, |
| 478 | }, |
| 479 | { |
| 480 | namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:name.: "file match long pattern foo.txt with concatenation (MatchFile)", |
| 481 | expressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expression.: &caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.MatchExpressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#{ |
| 482 | Exprscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#Expr.: `file({"root": ".", "try_files": ["./testdata" + {http.request.uri.path}]})`, |
| 483 | }, |
| 484 | urlTargetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:urlTarget.: "https://example.com/foo.txt", |
| 485 | wantResultscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:wantResult.: true, |
| 486 | }, |
| 487 | { |
| 488 | namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:name.: "file not match long pattern (MatchFile)", |
| 489 | expressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expression.: &caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.MatchExpressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#{ |
| 490 | Exprscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#Expr.: `file({"root": "./testdata", "try_files": [{http.request.uri.path}]})`, |
| 491 | }, |
| 492 | urlTargetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:urlTarget.: "https://example.com/nopenope.txt", |
| 493 | wantResultscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:wantResult.: false, |
| 494 | }, |
| 495 | { |
| 496 | namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:name.: "file match long pattern foo.txt with try_policy (MatchFile)", |
| 497 | expressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expression.: &caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.MatchExpressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#{ |
| 498 | Exprscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#Expr.: `file({"root": "./testdata", "try_policy": "largest_size", "try_files": ["foo.txt", "large.txt"]})`, |
| 499 | }, |
| 500 | urlTargetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:urlTarget.: "https://example.com/", |
| 501 | wantResultscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:wantResult.: true, |
| 502 | expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expectedPath.: "/large.txt", |
| 503 | }, |
| 504 | } |
| 505 | |
| 506 | func TestMatchExpressionMatchscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/TestMatchExpressionMatch().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 507 | for _, tst := range expressionTestsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests. { |
| 508 | tc := tst |
| 509 | t.Runscip-go gomod github.com/golang/go/src go1.25.1 testing/T#Run().(tc.namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:name., func(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 510 | caddyCtx, cancel := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.NewContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/NewContext().(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#{Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#Context.: contextscip-go gomod github.com/golang/go/src go1.25.1 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.1 context/Background().()}) |
| 511 | defer cancel() |
| 512 | err := tc.expressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expression..Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#Provision().(caddyCtx) |
| 513 | if err != nil { |
| 514 | if !tc.wantErrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:wantErr. { |
| 515 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("MatchExpression.Provision() error = %v, wantErr %v", err, tc.wantErrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:wantErr.) |
| 516 | } |
| 517 | return |
| 518 | } |
| 519 | |
| 520 | req := httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/.NewRequestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/NewRequest().(tc.httpMethodscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:httpMethod., tc.urlTargetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:urlTarget., nil) |
| 521 | if tc.httpHeaderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:httpHeader. != nil { |
| 522 | req.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Header. = *tc.httpHeaderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:httpHeader. |
| 523 | } |
| 524 | repl := caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.NewTestReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/NewTestReplacer().(req) |
| 525 | repl.Setscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#Set().("http.vars.root", "./testdata") |
| 526 | ctx := contextscip-go gomod github.com/golang/go/src go1.25.1 context/.WithValuescip-go gomod github.com/golang/go/src go1.25.1 context/WithValue().(req.Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().(), caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ReplacerCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ReplacerCtxKey., repl) |
| 527 | req = req.WithContextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#WithContext().(ctx) |
| 528 | |
| 529 | matches, err := tc.expressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expression..MatchWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#MatchWithError().(req) |
| 530 | if err != nil { |
| 531 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("MatchExpression.Match() error = %v", err) |
| 532 | return |
| 533 | } |
| 534 | if matches != tc.wantResultscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:wantResult. { |
| 535 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("MatchExpression.Match() expected to return '%t', for expression : '%s'", tc.wantResultscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:wantResult., tc.expressionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expression..Exprscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchExpression#Expr.) |
| 536 | } |
| 537 | |
| 538 | if tc.expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expectedPath. != "" { |
| 539 | path, ok := repl.Getscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#Get().("http.matchers.file.relative") |
| 540 | if !ok { |
| 541 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("MatchExpression.Match() expected to return path '%s', but got none", tc.expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expectedPath.) |
| 542 | } |
| 543 | if path != tc.expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expectedPath. { |
| 544 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("MatchExpression.Match() expected to return path '%s', but got '%s'", tc.expectedPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/expressionTests:expectedPath., path) |
| 545 | } |
| 546 | } |
| 547 | }) |
| 548 | } |
| 549 | } |