| 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 | "osscip-go gomod github.com/golang/go/src go1.25.1 os/" |
| 22 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/" |
| 23 | "runtimescip-go gomod github.com/golang/go/src go1.25.1 runtime/" |
| 24 | "stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/" |
| 25 | "testingscip-go gomod github.com/golang/go/src go1.25.1 testing/" |
| 26 | "timescip-go gomod github.com/golang/go/src go1.25.1 time/" |
| 27 | |
| 28 | "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/" |
| 29 | ) |
| 30 | |
| 31 | func TestFileHiddenscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/TestFileHidden().(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#) { |
| 32 | for i, tc := range []struct { |
| 33 | inputHide []string |
| 34 | inputPath string |
| 35 | expect bool |
| 36 | }{ |
| 37 | { |
| 38 | inputHide: nil, |
| 39 | inputPath: "", |
| 40 | expect: false, |
| 41 | }, |
| 42 | { |
| 43 | inputHide: []string{".gitignore"}, |
| 44 | inputPath: "/.gitignore", |
| 45 | expect: true, |
| 46 | }, |
| 47 | { |
| 48 | inputHide: []string{".git"}, |
| 49 | inputPath: "/.gitignore", |
| 50 | expect: false, |
| 51 | }, |
| 52 | { |
| 53 | inputHide: []string{"/.git"}, |
| 54 | inputPath: "/.gitignore", |
| 55 | expect: false, |
| 56 | }, |
| 57 | { |
| 58 | inputHide: []string{".git"}, |
| 59 | inputPath: "/.git", |
| 60 | expect: true, |
| 61 | }, |
| 62 | { |
| 63 | inputHide: []string{".git"}, |
| 64 | inputPath: "/.git/foo", |
| 65 | expect: true, |
| 66 | }, |
| 67 | { |
| 68 | inputHide: []string{".git"}, |
| 69 | inputPath: "/foo/.git/bar", |
| 70 | expect: true, |
| 71 | }, |
| 72 | { |
| 73 | inputHide: []string{"/prefix"}, |
| 74 | inputPath: "/prefix/foo", |
| 75 | expect: true, |
| 76 | }, |
| 77 | { |
| 78 | inputHide: []string{"/foo/*/bar"}, |
| 79 | inputPath: "/foo/asdf/bar", |
| 80 | expect: true, |
| 81 | }, |
| 82 | { |
| 83 | inputHide: []string{"*.txt"}, |
| 84 | inputPath: "/foo/bar.txt", |
| 85 | expect: true, |
| 86 | }, |
| 87 | { |
| 88 | inputHide: []string{"/foo/bar/*.txt"}, |
| 89 | inputPath: "/foo/bar/baz.txt", |
| 90 | expect: true, |
| 91 | }, |
| 92 | { |
| 93 | inputHide: []string{"/foo/bar/*.txt"}, |
| 94 | inputPath: "/foo/bar.txt", |
| 95 | expect: false, |
| 96 | }, |
| 97 | { |
| 98 | inputHide: []string{"/foo/bar/*.txt"}, |
| 99 | inputPath: "/foo/bar/index.html", |
| 100 | expect: false, |
| 101 | }, |
| 102 | { |
| 103 | inputHide: []string{"/foo"}, |
| 104 | inputPath: "/foo", |
| 105 | expect: true, |
| 106 | }, |
| 107 | { |
| 108 | inputHide: []string{"/foo"}, |
| 109 | inputPath: "/foobar", |
| 110 | expect: false, |
| 111 | }, |
| 112 | { |
| 113 | inputHide: []string{"first", "second"}, |
| 114 | inputPath: "/second", |
| 115 | expect: true, |
| 116 | }, |
| 117 | } { |
| 118 | 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" { |
| 119 | if stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasPrefix().(tc.inputPath, "/") { |
| 120 | tc.inputPath, _ = filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Absscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Abs().(tc.inputPath) |
| 121 | } |
| 122 | tc.inputPath = filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.FromSlashscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/FromSlash().(tc.inputPath) |
| 123 | for i := range tc.inputHide { |
| 124 | if stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasPrefix().(tc.inputHide[i], "/") { |
| 125 | tc.inputHide[i], _ = filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Absscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Abs().(tc.inputHide[i]) |
| 126 | } |
| 127 | tc.inputHide[i] = filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.FromSlashscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/FromSlash().(tc.inputHide[i]) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | actual := fileHiddenscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/fileHidden().(tc.inputPath, tc.inputHide) |
| 132 | if actual != tc.expect { |
| 133 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Does %v hide %s? Got %t but expected %t", |
| 134 | i, tc.inputHide, tc.inputPath, actual, tc.expect) |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Check to make sure that we don't serve ETag and Last-Modified headers |
| 140 | // for files with invalid modification times |
| 141 | func TestModTimeHeadersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/TestModTimeHeaders().(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#) { |
| 142 | check_validator_headersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/check_validator_headers().(timescip-go gomod github.com/golang/go/src go1.25.1 time/.Nowscip-go gomod github.com/golang/go/src go1.25.1 time/Now().(), true, t) |
| 143 | check_validator_headersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/check_validator_headers().(timescip-go gomod github.com/golang/go/src go1.25.1 time/.Unixscip-go gomod github.com/golang/go/src go1.25.1 time/Unix().(0, 0), false, t) |
| 144 | check_validator_headersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/check_validator_headers().(timescip-go gomod github.com/golang/go/src go1.25.1 time/.Unixscip-go gomod github.com/golang/go/src go1.25.1 time/Unix().(1, 0), false, t) |
| 145 | check_validator_headersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/check_validator_headers().(timescip-go gomod github.com/golang/go/src go1.25.1 time/.Unixscip-go gomod github.com/golang/go/src go1.25.1 time/Unix().(2, 0), true, t) |
| 146 | } |
| 147 | |
| 148 | func check_validator_headersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/check_validator_headers().(modTime timescip-go gomod github.com/golang/go/src go1.25.1 time/.Timescip-go gomod github.com/golang/go/src go1.25.1 time/Time#, expect_headers bool, 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#) { |
| 149 | f := false |
| 150 | fsrv := FileServerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/FileServer#{ |
| 151 | Rootscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/FileServer#Root.: "./testdata", |
| 152 | CanonicalURIsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/FileServer#CanonicalURIs.: &f, |
| 153 | } |
| 154 | w := httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/.NewRecorderscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/NewRecorder().() |
| 155 | r, err := httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.NewRequestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/NewRequest().("GET", "/modtime.txt", nil) |
| 156 | if err != nil { |
| 157 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatal().(err) |
| 158 | } |
| 159 | repl := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.NewReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/NewReplacer().() |
| 160 | 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().(r.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) |
| 161 | r = r.WithContextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#WithContext().(ctx) |
| 162 | |
| 163 | ctx2, _ := 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().()}) // module will be nil by default |
| 164 | fsrv.Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/FileServer#Provision().(ctx2) |
| 165 | |
| 166 | path := "testdata/modtime.txt" |
| 167 | osscip-go gomod github.com/golang/go/src go1.25.1 os/.Chtimesscip-go gomod github.com/golang/go/src go1.25.1 os/Chtimes().(path, modTime, modTime) |
| 168 | |
| 169 | fsrv.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver`/FileServer#ServeHTTP().(w, r, nil) |
| 170 | |
| 171 | if expect_headers { |
| 172 | if w.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/ResponseRecorder#Header().().Getscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Get().("ETag") == "" { |
| 173 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Didn't get ETag header for file with valid mod time %s", modTime) |
| 174 | } |
| 175 | if w.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/ResponseRecorder#Header().().Getscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Get().("Last-Modified") == "" { |
| 176 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Didn't get Last-Modified header for file with valid mod time %s", modTime) |
| 177 | } |
| 178 | } else { |
| 179 | if w.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/ResponseRecorder#Header().().Getscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Get().("ETag") != "" { |
| 180 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Got ETag header for file with invalid mod time %s", modTime) |
| 181 | } |
| 182 | if w.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/ResponseRecorder#Header().().Getscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Get().("Last-Modified") != "" { |
| 183 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Got Last-Modified header for file with invalid mod time %s", modTime) |
| 184 | } |
| 185 | } |
| 186 | } |