| 1 |
// Copyright 2010 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package http_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"bufio" |
| 9 |
"bytes" |
| 10 |
"compress/gzip" |
| 11 |
"errors" |
| 12 |
"fmt" |
| 13 |
"internal/testenv" |
| 14 |
"io" |
| 15 |
"io/fs" |
| 16 |
"mime" |
| 17 |
"mime/multipart" |
| 18 |
"net" |
| 19 |
"net/http" |
| 20 |
. "net/http" |
| 21 |
"net/http/httptest" |
| 22 |
"net/url" |
| 23 |
"os" |
| 24 |
"os/exec" |
| 25 |
"path" |
| 26 |
"path/filepath" |
| 27 |
"regexp" |
| 28 |
"runtime" |
| 29 |
"slices" |
| 30 |
"strconv" |
| 31 |
"strings" |
| 32 |
"testing" |
| 33 |
"testing/fstest" |
| 34 |
"time" |
| 35 |
) |
| 36 |
|
| 37 |
const ( |
| 38 |
testFile = "testdata/file" |
| 39 |
testFileLen = 11 |
| 40 |
) |
| 41 |
|
| 42 |
type wantRange struct { |
| 43 |
start, end int64 // range [start,end) |
| 44 |
} |
| 45 |
|
| 46 |
var ServeFileRangeTests = []struct { |
| 47 |
r string |
| 48 |
code int |
| 49 |
ranges []wantRange |
| 50 |
}{ |
| 51 |
{r: "", code: StatusOK}, |
| 52 |
{r: "bytes=0-4", code: StatusPartialContent, ranges: []wantRange{{0, 5}}}, |
| 53 |
{r: "bytes=2-", code: StatusPartialContent, ranges: []wantRange{{2, testFileLen}}}, |
| 54 |
{r: "bytes=-5", code: StatusPartialContent, ranges: []wantRange{{testFileLen - 5, testFileLen}}}, |
| 55 |
{r: "bytes=3-7", code: StatusPartialContent, ranges: []wantRange{{3, 8}}}, |
| 56 |
{r: "bytes=0-0,-2", code: StatusPartialContent, ranges: []wantRange{{0, 1}, {testFileLen - 2, testFileLen}}}, |
| 57 |
{r: "bytes=0-1,5-8", code: StatusPartialContent, ranges: []wantRange{{0, 2}, {5, 9}}}, |
| 58 |
{r: "bytes=0-1,5-", code: StatusPartialContent, ranges: []wantRange{{0, 2}, {5, testFileLen}}}, |
| 59 |
{r: "bytes=5-1000", code: StatusPartialContent, ranges: []wantRange{{5, testFileLen}}}, |
| 60 |
{r: "bytes=0-,1-,2-,3-,4-", code: StatusOK}, // ignore wasteful range request |
| 61 |
{r: "bytes=0-9", code: StatusPartialContent, ranges: []wantRange{{0, testFileLen - 1}}}, |
| 62 |
{r: "bytes=0-10", code: StatusPartialContent, ranges: []wantRange{{0, testFileLen}}}, |
| 63 |
{r: "bytes=0-11", code: StatusPartialContent, ranges: []wantRange{{0, testFileLen}}}, |
| 64 |
{r: "bytes=10-11", code: StatusPartialContent, ranges: []wantRange{{testFileLen - 1, testFileLen}}}, |
| 65 |
{r: "bytes=10-", code: StatusPartialContent, ranges: []wantRange{{testFileLen - 1, testFileLen}}}, |
| 66 |
{r: "bytes=11-", code: StatusRequestedRangeNotSatisfiable}, |
| 67 |
{r: "bytes=11-12", code: StatusRequestedRangeNotSatisfiable}, |
| 68 |
{r: "bytes=12-12", code: StatusRequestedRangeNotSatisfiable}, |
| 69 |
{r: "bytes=11-100", code: StatusRequestedRangeNotSatisfiable}, |
| 70 |
{r: "bytes=12-100", code: StatusRequestedRangeNotSatisfiable}, |
| 71 |
{r: "bytes=100-", code: StatusRequestedRangeNotSatisfiable}, |
| 72 |
{r: "bytes=100-1000", code: StatusRequestedRangeNotSatisfiable}, |
| 73 |
} |
| 74 |
|
| 75 |
func TestServeFile(t *testing.T) { run(t, testServeFile) } |
| 76 |
func testServeFile(t *testing.T, mode testMode) { |
| 77 |
ts := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) { |
| 78 |
ServeFile(w, r, "testdata/file") |
| 79 |
})).ts |
| 80 |
c := ts.Client() |
| 81 |
|
| 82 |
var err error |
| 83 |
|
| 84 |
file, err := os.ReadFile(testFile) |
| 85 |
if err != nil { |
| 86 |
t.Fatal("reading file:", err) |
| 87 |
} |
| 88 |
|
| 89 |
// set up the Request (re-used for all tests) |
| 90 |
var req Request |
| 91 |
req.Header = make(Header) |
| 92 |
if req.URL, err = url.Parse(ts.URL); err != nil { |
| 93 |
t.Fatal("ParseURL:", err) |
| 94 |
} |
| 95 |
|
| 96 |
// Get contents via various methods. |
| 97 |
// |
| 98 |
// See https://go.dev/issue/59471 for a proposal to limit the set of methods handled. |
| 99 |
// For now, test the historical behavior. |
| 100 |
for _, method := range []string{ |
| 101 |
MethodGet, |
| 102 |
MethodPost, |
| 103 |
MethodPut, |
| 104 |
MethodPatch, |
| 105 |
MethodDelete, |
| 106 |
MethodOptions, |
| 107 |
MethodTrace, |
| 108 |
} { |
| 109 |
req.Method = method |
| 110 |
_, body := getBody(t, method, req, c) |
| 111 |
if !bytes.Equal(body, file) { |
| 112 |
t.Fatalf("body mismatch for %v request: got %q, want %q", method, body, file) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
// HEAD request. |
| 117 |
req.Method = MethodHead |
| 118 |
resp, body := getBody(t, "HEAD", req, c) |
| 119 |
if len(body) != 0 { |
| 120 |
t.Fatalf("body mismatch for HEAD request: got %q, want empty", body) |
| 121 |
} |
| 122 |
if got, want := resp.Header.Get("Content-Length"), fmt.Sprint(len(file)); got != want { |
| 123 |
t.Fatalf("Content-Length mismatch for HEAD request: got %v, want %v", got, want) |
| 124 |
} |
| 125 |
|
| 126 |
// Range tests |
| 127 |
req.Method = MethodGet |
| 128 |
Cases: |
| 129 |
for _, rt := range ServeFileRangeTests { |
| 130 |
if rt.r != "" { |
| 131 |
req.Header.Set("Range", rt.r) |
| 132 |
} |
| 133 |
resp, body := getBody(t, fmt.Sprintf("range test %q", rt.r), req, c) |
| 134 |
if resp.StatusCode != rt.code { |
| 135 |
t.Errorf("range=%q: StatusCode=%d, want %d", rt.r, resp.StatusCode, rt.code) |
| 136 |
} |
| 137 |
if rt.code == StatusRequestedRangeNotSatisfiable { |
| 138 |
continue |
| 139 |
} |
| 140 |
wantContentRange := "" |
| 141 |
if len(rt.ranges) == 1 { |
| 142 |
rng := rt.ranges[0] |
| 143 |
wantContentRange = fmt.Sprintf("bytes %d-%d/%d", rng.start, rng.end-1, testFileLen) |
| 144 |
} |
| 145 |
cr := resp.Header.Get("Content-Range") |
| 146 |
if cr != wantContentRange { |
| 147 |
t.Errorf("range=%q: Content-Range = %q, want %q", rt.r, cr, wantContentRange) |
| 148 |
} |
| 149 |
ct := resp.Header.Get("Content-Type") |
| 150 |
if len(rt.ranges) == 1 { |
| 151 |
rng := rt.ranges[0] |
| 152 |
wantBody := file[rng.start:rng.end] |
| 153 |
if !bytes.Equal(body, wantBody) { |
| 154 |
t.Errorf("range=%q: body = %q, want %q", rt.r, body, wantBody) |
| 155 |
} |
| 156 |
if strings.HasPrefix(ct, "multipart/byteranges") { |
| 157 |
t.Errorf("range=%q content-type = %q; unexpected multipart/byteranges", rt.r, ct) |
| 158 |
} |
| 159 |
} |
| 160 |
if len(rt.ranges) > 1 { |
| 161 |
typ, params, err := mime.ParseMediaType(ct) |
| 162 |
if err != nil { |
| 163 |
t.Errorf("range=%q content-type = %q; %v", rt.r, ct, err) |
| 164 |
continue |
| 165 |
} |
| 166 |
if typ != "multipart/byteranges" { |
| 167 |
t.Errorf("range=%q content-type = %q; want multipart/byteranges", rt.r, typ) |
| 168 |
continue |
| 169 |
} |
| 170 |
if params["boundary"] == "" { |
| 171 |
t.Errorf("range=%q content-type = %q; lacks boundary", rt.r, ct) |
| 172 |
continue |
| 173 |
} |
| 174 |
if g, w := resp.ContentLength, int64(len(body)); g != w { |
| 175 |
t.Errorf("range=%q Content-Length = %d; want %d", rt.r, g, w) |
| 176 |
continue |
| 177 |
} |
| 178 |
mr := multipart.NewReader(bytes.NewReader(body), params["boundary"]) |
| 179 |
for ri, rng := range rt.ranges { |
| 180 |
part, err := mr.NextPart() |
| 181 |
if err != nil { |
| 182 |
t.Errorf("range=%q, reading part index %d: %v", rt.r, ri, err) |
| 183 |
continue Cases |
| 184 |
} |
| 185 |
wantContentRange = fmt.Sprintf("bytes %d-%d/%d", rng.start, rng.end-1, testFileLen) |
| 186 |
if g, w := part.Header.Get("Content-Range"), wantContentRange; g != w { |
| 187 |
t.Errorf("range=%q: part Content-Range = %q; want %q", rt.r, g, w) |
| 188 |
} |
| 189 |
body, err := io.ReadAll(part) |
| 190 |
if err != nil { |
| 191 |
t.Errorf("range=%q, reading part index %d body: %v", rt.r, ri, err) |
| 192 |
continue Cases |
| 193 |
} |
| 194 |
wantBody := file[rng.start:rng.end] |
| 195 |
if !bytes.Equal(body, wantBody) { |
| 196 |
t.Errorf("range=%q: body = %q, want %q", rt.r, body, wantBody) |
| 197 |
} |
| 198 |
} |
| 199 |
_, err = mr.NextPart() |
| 200 |
if err != io.EOF { |
| 201 |
t.Errorf("range=%q; expected final error io.EOF; got %v", rt.r, err) |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
func TestServeFile_DotDot(t *testing.T) { |
| 208 |
tests := []struct { |
| 209 |
req string |
| 210 |
wantStatus int |
| 211 |
}{ |
| 212 |
{"/testdata/file", 200}, |
| 213 |
{"/../file", 400}, |
| 214 |
{"/..", 400}, |
| 215 |
{"/../", 400}, |
| 216 |
{"/../foo", 400}, |
| 217 |
{"/..\\foo", 400}, |
| 218 |
{"/file/a", 200}, |
| 219 |
{"/file/a..", 200}, |
| 220 |
{"/file/a/..", 400}, |
| 221 |
{"/file/a\\..", 400}, |
| 222 |
} |
| 223 |
for _, tt := range tests { |
| 224 |
req, err := ReadRequest(bufio.NewReader(strings.NewReader("GET " + tt.req + " HTTP/1.1\r\nHost: foo\r\n\r\n"))) |
| 225 |
if err != nil { |
| 226 |
t.Errorf("bad request %q: %v", tt.req, err) |
| 227 |
continue |
| 228 |
} |
| 229 |
rec := httptest.NewRecorder() |
| 230 |
ServeFile(rec, req, "testdata/file") |
| 231 |
if rec.Code != tt.wantStatus { |
| 232 |
t.Errorf("for request %q, status = %d; want %d", tt.req, rec.Code, tt.wantStatus) |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
// Tests that this doesn't panic. (Issue 30165) |
| 238 |
func TestServeFileDirPanicEmptyPath(t *testing.T) { |
| 239 |
rec := httptest.NewRecorder() |
| 240 |
req := httptest.NewRequest("GET", "/", nil) |
| 241 |
req.URL.Path = "" |
| 242 |
ServeFile(rec, req, "testdata") |
| 243 |
res := rec.Result() |
| 244 |
if res.StatusCode != 301 { |
| 245 |
t.Errorf("code = %v; want 301", res.Status) |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// Tests that ranges are ignored with serving empty content. (Issue 54794) |
| 250 |
func TestServeContentWithEmptyContentIgnoreRanges(t *testing.T) { |
| 251 |
for _, r := range []string{ |
| 252 |
"bytes=0-128", |
| 253 |
"bytes=1-", |
| 254 |
} { |
| 255 |
rec := httptest.NewRecorder() |
| 256 |
req := httptest.NewRequest("GET", "/", nil) |
| 257 |
req.Header.Set("Range", r) |
| 258 |
ServeContent(rec, req, "nothing", time.Now(), bytes.NewReader(nil)) |
| 259 |
res := rec.Result() |
| 260 |
if res.StatusCode != 200 { |
| 261 |
t.Errorf("code = %v; want 200", res.Status) |
| 262 |
} |
| 263 |
bodyLen := rec.Body.Len() |
| 264 |
if bodyLen != 0 { |
| 265 |
t.Errorf("body.Len() = %v; want 0", res.Status) |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
var fsRedirectTestData = []struct { |
| 271 |
original, redirect string |
| 272 |
}{ |
| 273 |
{"/test/index.html", "/test/"}, |
| 274 |
{"/test/testdata", "/test/testdata/"}, |
| 275 |
{"/test/testdata/file/", "/test/testdata/file"}, |
| 276 |
} |
| 277 |
|
| 278 |
func TestFSRedirect(t *testing.T) { run(t, testFSRedirect) } |
| 279 |
func testFSRedirect(t *testing.T, mode testMode) { |
| 280 |
ts := newClientServerTest(t, mode, StripPrefix("/test", FileServer(Dir(".")))).ts |
| 281 |
|
| 282 |
for _, data := range fsRedirectTestData { |
| 283 |
res, err := ts.Client().Get(ts.URL + data.original) |
| 284 |
if err != nil { |
| 285 |
t.Fatal(err) |
| 286 |
} |
| 287 |
res.Body.Close() |
| 288 |
if g, e := res.Request.URL.Path, data.redirect; g != e { |
| 289 |
t.Errorf("redirect from %s: got %s, want %s", data.original, g, e) |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
type testFileSystem struct { |
| 295 |
open func(name string) (File, error) |
| 296 |
} |
| 297 |
|
| 298 |
func (fs *testFileSystem) Open(name string) (File, error) { |
| 299 |
return fs.open(name) |
| 300 |
} |
| 301 |
|
| 302 |
func TestFileServerCleans(t *testing.T) { |
| 303 |
defer afterTest(t) |
| 304 |
ch := make(chan string, 1) |
| 305 |
fs := FileServer(&testFileSystem{func(name string) (File, error) { |
| 306 |
ch <- name |
| 307 |
return nil, errors.New("file does not exist") |
| 308 |
}}) |
| 309 |
tests := []struct { |
| 310 |
reqPath, openArg string |
| 311 |
}{ |
| 312 |
{"/foo.txt", "/foo.txt"}, |
| 313 |
{"//foo.txt", "/foo.txt"}, |
| 314 |
{"/../foo.txt", "/foo.txt"}, |
| 315 |
} |
| 316 |
req, _ := NewRequest("GET", "http://example.com", nil) |
| 317 |
for n, test := range tests { |
| 318 |
rec := httptest.NewRecorder() |
| 319 |
req.URL.Path = test.reqPath |
| 320 |
fs.ServeHTTP(rec, req) |
| 321 |
if got := <-ch; got != test.openArg { |
| 322 |
t.Errorf("test %d: got %q, want %q", n, got, test.openArg) |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
func TestFileServerEscapesNames(t *testing.T) { run(t, testFileServerEscapesNames) } |
| 328 |
func testFileServerEscapesNames(t *testing.T, mode testMode) { |
| 329 |
const dirListPrefix = "<!doctype html>\n<meta name=\"viewport\" content=\"width=device-width\">\n<pre>\n" |
| 330 |
const dirListSuffix = "\n</pre>\n" |
| 331 |
tests := []struct { |
| 332 |
name, escaped string |
| 333 |
}{ |
| 334 |
{`simple_name`, `<a href="simple_name">simple_name</a>`}, |
| 335 |
{`"'<>&`, `<a href="%22%27%3C%3E&">"'<>&</a>`}, |
| 336 |
{`?foo=bar#baz`, `<a href="%3Ffoo=bar%23baz">?foo=bar#baz</a>`}, |
| 337 |
{`<combo>?foo`, `<a href="%3Ccombo%3E%3Ffoo"><combo>?foo</a>`}, |
| 338 |
{`foo:bar`, `<a href="./foo:bar">foo:bar</a>`}, |
| 339 |
} |
| 340 |
|
| 341 |
// We put each test file in its own directory in the fakeFS so we can look at it in isolation. |
| 342 |
fs := make(fakeFS) |
| 343 |
for i, test := range tests { |
| 344 |
testFile := &fakeFileInfo{basename: test.name} |
| 345 |
fs[fmt.Sprintf("/%d", i)] = &fakeFileInfo{ |
| 346 |
dir: true, |
| 347 |
modtime: time.Unix(1000000000, 0).UTC(), |
| 348 |
ents: []*fakeFileInfo{testFile}, |
| 349 |
} |
| 350 |
fs[fmt.Sprintf("/%d/%s", i, test.name)] = testFile |
| 351 |
} |
| 352 |
|
| 353 |
ts := newClientServerTest(t, mode, FileServer(&fs)).ts |
| 354 |
for i, test := range tests { |
| 355 |
url := fmt.Sprintf("%s/%d", ts.URL, i) |
| 356 |
res, err := ts.Client().Get(url) |
| 357 |
if err != nil { |
| 358 |
t.Fatalf("test %q: Get: %v", test.name, err) |
| 359 |
} |
| 360 |
b, err := io.ReadAll(res.Body) |
| 361 |
if err != nil { |
| 362 |
t.Fatalf("test %q: read Body: %v", test.name, err) |
| 363 |
} |
| 364 |
s := string(b) |
| 365 |
if !strings.HasPrefix(s, dirListPrefix) || !strings.HasSuffix(s, dirListSuffix) { |
| 366 |
t.Errorf("test %q: listing dir, full output is %q, want prefix %q and suffix %q", test.name, s, dirListPrefix, dirListSuffix) |
| 367 |
} |
| 368 |
if trimmed := strings.TrimSuffix(strings.TrimPrefix(s, dirListPrefix), dirListSuffix); trimmed != test.escaped { |
| 369 |
t.Errorf("test %q: listing dir, filename escaped to %q, want %q", test.name, trimmed, test.escaped) |
| 370 |
} |
| 371 |
res.Body.Close() |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
func TestFileServerSortsNames(t *testing.T) { run(t, testFileServerSortsNames) } |
| 376 |
func testFileServerSortsNames(t *testing.T, mode testMode) { |
| 377 |
const contents = "I am a fake file" |
| 378 |
dirMod := time.Unix(123, 0).UTC() |
| 379 |
fileMod := time.Unix(1000000000, 0).UTC() |
| 380 |
fs := fakeFS{ |
| 381 |
"/": &fakeFileInfo{ |
| 382 |
dir: true, |
| 383 |
modtime: dirMod, |
| 384 |
ents: []*fakeFileInfo{ |
| 385 |
{ |
| 386 |
basename: "b", |
| 387 |
modtime: fileMod, |
| 388 |
contents: contents, |
| 389 |
}, |
| 390 |
{ |
| 391 |
basename: "a", |
| 392 |
modtime: fileMod, |
| 393 |
contents: contents, |
| 394 |
}, |
| 395 |
}, |
| 396 |
}, |
| 397 |
} |
| 398 |
|
| 399 |
ts := newClientServerTest(t, mode, FileServer(&fs)).ts |
| 400 |
|
| 401 |
res, err := ts.Client().Get(ts.URL) |
| 402 |
if err != nil { |
| 403 |
t.Fatalf("Get: %v", err) |
| 404 |
} |
| 405 |
defer res.Body.Close() |
| 406 |
|
| 407 |
b, err := io.ReadAll(res.Body) |
| 408 |
if err != nil { |
| 409 |
t.Fatalf("read Body: %v", err) |
| 410 |
} |
| 411 |
s := string(b) |
| 412 |
if !strings.Contains(s, "<a href=\"a\">a</a>\n<a href=\"b\">b</a>") { |
| 413 |
t.Errorf("output appears to be unsorted:\n%s", s) |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
func mustRemoveAll(dir string) { |
| 418 |
err := os.RemoveAll(dir) |
| 419 |
if err != nil { |
| 420 |
panic(err) |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
func TestFileServerImplicitLeadingSlash(t *testing.T) { run(t, testFileServerImplicitLeadingSlash) } |
| 425 |
func testFileServerImplicitLeadingSlash(t *testing.T, mode testMode) { |
| 426 |
tempDir := t.TempDir() |
| 427 |
if err := os.WriteFile(filepath.Join(tempDir, "foo.txt"), []byte("Hello world"), 0644); err != nil { |
| 428 |
t.Fatalf("WriteFile: %v", err) |
| 429 |
} |
| 430 |
ts := newClientServerTest(t, mode, StripPrefix("/bar/", FileServer(Dir(tempDir)))).ts |
| 431 |
get := func(suffix string) string { |
| 432 |
res, err := ts.Client().Get(ts.URL + suffix) |
| 433 |
if err != nil { |
| 434 |
t.Fatalf("Get %s: %v", suffix, err) |
| 435 |
} |
| 436 |
b, err := io.ReadAll(res.Body) |
| 437 |
if err != nil { |
| 438 |
t.Fatalf("ReadAll %s: %v", suffix, err) |
| 439 |
} |
| 440 |
res.Body.Close() |
| 441 |
return string(b) |
| 442 |
} |
| 443 |
if s := get("/bar/"); !strings.Contains(s, ">foo.txt<") { |
| 444 |
t.Logf("expected a directory listing with foo.txt, got %q", s) |
| 445 |
} |
| 446 |
if s := get("/bar/foo.txt"); s != "Hello world" { |
| 447 |
t.Logf("expected %q, got %q", "Hello world", s) |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
func TestDirJoin(t *testing.T) { |
| 452 |
if runtime.GOOS == "windows" { |
| 453 |
t.Skip("skipping test on windows") |
| 454 |
} |
| 455 |
wfi, err := os.Stat("/etc/hosts") |
| 456 |
if err != nil { |
| 457 |
t.Skip("skipping test; no /etc/hosts file") |
| 458 |
} |
| 459 |
test := func(d Dir, name string) { |
| 460 |
f, err := d.Open(name) |
| 461 |
if err != nil { |
| 462 |
t.Fatalf("open of %s: %v", name, err) |
| 463 |
} |
| 464 |
defer f.Close() |
| 465 |
gfi, err := f.Stat() |
| 466 |
if err != nil { |
| 467 |
t.Fatalf("stat of %s: %v", name, err) |
| 468 |
} |
| 469 |
if !os.SameFile(gfi, wfi) { |
| 470 |
t.Errorf("%s got different file", name) |
| 471 |
} |
| 472 |
} |
| 473 |
test(Dir("/etc/"), "/hosts") |
| 474 |
test(Dir("/etc/"), "hosts") |
| 475 |
test(Dir("/etc/"), "../../../../hosts") |
| 476 |
test(Dir("/etc"), "/hosts") |
| 477 |
test(Dir("/etc"), "hosts") |
| 478 |
test(Dir("/etc"), "../../../../hosts") |
| 479 |
|
| 480 |
// Not really directories, but since we use this trick in |
| 481 |
// ServeFile, test it: |
| 482 |
test(Dir("/etc/hosts"), "") |
| 483 |
test(Dir("/etc/hosts"), "/") |
| 484 |
test(Dir("/etc/hosts"), "../") |
| 485 |
} |
| 486 |
|
| 487 |
func TestEmptyDirOpenCWD(t *testing.T) { |
| 488 |
test := func(d Dir) { |
| 489 |
name := "fs_test.go" |
| 490 |
f, err := d.Open(name) |
| 491 |
if err != nil { |
| 492 |
t.Fatalf("open of %s: %v", name, err) |
| 493 |
} |
| 494 |
defer f.Close() |
| 495 |
} |
| 496 |
test(Dir("")) |
| 497 |
test(Dir(".")) |
| 498 |
test(Dir("./")) |
| 499 |
} |
| 500 |
|
| 501 |
func TestServeFileContentType(t *testing.T) { run(t, testServeFileContentType) } |
| 502 |
func testServeFileContentType(t *testing.T, mode testMode) { |
| 503 |
const ctype = "icecream/chocolate" |
| 504 |
ts := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) { |
| 505 |
switch r.FormValue("override") { |
| 506 |
case "1": |
| 507 |
w.Header().Set("Content-Type", ctype) |
| 508 |
case "2": |
| 509 |
// Explicitly inhibit sniffing. |
| 510 |
w.Header()["Content-Type"] = []string{} |
| 511 |
} |
| 512 |
ServeFile(w, r, "testdata/file") |
| 513 |
})).ts |
| 514 |
get := func(override string, want []string) { |
| 515 |
resp, err := ts.Client().Get(ts.URL + "?override=" + override) |
| 516 |
if err != nil { |
| 517 |
t.Fatal(err) |
| 518 |
} |
| 519 |
if h := resp.Header["Content-Type"]; !slices.Equal(h, want) { |
| 520 |
t.Errorf("Content-Type mismatch: got %v, want %v", h, want) |
| 521 |
} |
| 522 |
resp.Body.Close() |
| 523 |
} |
| 524 |
get("0", []string{"text/plain; charset=utf-8"}) |
| 525 |
get("1", []string{ctype}) |
| 526 |
get("2", nil) |
| 527 |
} |
| 528 |
|
| 529 |
func TestServeFileMimeType(t *testing.T) { run(t, testServeFileMimeType) } |
| 530 |
func testServeFileMimeType(t *testing.T, mode testMode) { |
| 531 |
ts := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) { |
| 532 |
ServeFile(w, r, "testdata/style.css") |
| 533 |
})).ts |
| 534 |
resp, err := ts.Client().Get(ts.URL) |
| 535 |
if err != nil { |
| 536 |
t.Fatal(err) |
| 537 |
} |
| 538 |
resp.Body.Close() |
| 539 |
want := "text/css; charset=utf-8" |
| 540 |
if h := resp.Header.Get("Content-Type"); h != want { |
| 541 |
t.Errorf("Content-Type mismatch: got %q, want %q", h, want) |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
func TestServeFileFromCWD(t *testing.T) { run(t, testServeFileFromCWD) } |
| 546 |
func testServeFileFromCWD(t *testing.T, mode testMode) { |
| 547 |
ts := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) { |
| 548 |
ServeFile(w, r, "fs_test.go") |
| 549 |
})).ts |
| 550 |
r, err := ts.Client().Get(ts.URL) |
| 551 |
if err != nil { |
| 552 |
t.Fatal(err) |
| 553 |
} |
| 554 |
r.Body.Close() |
| 555 |
if r.StatusCode != 200 { |
| 556 |
t.Fatalf("expected 200 OK, got %s", r.Status) |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
// Issue 13996 |
| 561 |
func TestServeDirWithoutTrailingSlash(t *testing.T) { run(t, testServeDirWithoutTrailingSlash) } |
| 562 |
func testServeDirWithoutTrailingSlash(t *testing.T, mode testMode) { |
| 563 |
e := "/testdata/" |
| 564 |
ts := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) { |
| 565 |
ServeFile(w, r, ".") |
| 566 |
})).ts |
| 567 |
r, err := ts.Client().Get(ts.URL + "/testdata") |
| 568 |
if err != nil { |
| 569 |
t.Fatal(err) |
| 570 |
} |
| 571 |
r.Body.Close() |
| 572 |
if g := r.Request.URL.Path; g != e { |
| 573 |
t.Errorf("got %s, want %s", g, e) |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
// Tests that ServeFile doesn't add a Content-Length if a Content-Encoding is |
| 578 |
// specified. |
| 579 |
func TestServeFileWithContentEncoding(t *testing.T) { run(t, testServeFileWithContentEncoding) } |
| 580 |
func testServeFileWithContentEncoding(t *testing.T, mode testMode) { |
| 581 |
cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) { |
| 582 |
w.Header().Set("Content-Encoding", "foo") |
| 583 |
ServeFile(w, r, "testdata/file") |
| 584 |
|
| 585 |
// Because the testdata is so small, it would fit in |
| 586 |
// both the h1 and h2 Server's write buffers. For h1, |
| 587 |
// sendfile is used, though, forcing a header flush at |
| 588 |
// the io.Copy. http2 doesn't do a header flush so |
| 589 |
// buffers all 11 bytes and then adds its own |
| 590 |
// Content-Length. To prevent the Server's |
| 591 |
// Content-Length and test ServeFile only, flush here. |
| 592 |
w.(Flusher).Flush() |
| 593 |
})) |
| 594 |
resp, err := cst.c.Get(cst.ts.URL) |
| 595 |
if err != nil { |
| 596 |
t.Fatal(err) |
| 597 |
} |
| 598 |
resp.Body.Close() |
| 599 |
if g, e := resp.ContentLength, int64(-1); g != e { |
| 600 |
t.Errorf("Content-Length mismatch: got %d, want %d", g, e) |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
// Tests that ServeFile does not generate representation metadata when |
| 605 |
// file has not been modified, as per RFC 7232 section 4.1. |
| 606 |
func TestServeFileNotModified(t *testing.T) { run(t, testServeFileNotModified) } |
| 607 |
func testServeFileNotModified(t *testing.T, mode testMode) { |
| 608 |
cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) { |
| 609 |
w.Header().Set("Content-Type", "application/json") |
| 610 |
w.Header().Set("Content-Encoding", "foo") |
| 611 |
w.Header().Set("Etag", `"123"`) |
| 612 |
ServeFile(w, r, "testdata/file") |
| 613 |
|
| 614 |
// Because the testdata is so small, it would fit in |
| 615 |
// both the h1 and h2 Server's write buffers. For h1, |
| 616 |
// sendfile is used, though, forcing a header flush at |
| 617 |
// the io.Copy. http2 doesn't do a header flush so |
| 618 |
// buffers all 11 bytes and then adds its own |
| 619 |
// Content-Length. To prevent the Server's |
| 620 |
// Content-Length and test ServeFile only, flush here. |
| 621 |
w.(Flusher).Flush() |
| 622 |
})) |
| 623 |
req, err := NewRequest("GET", cst.ts.URL, nil) |
| 624 |
if err != nil { |
| 625 |
t.Fatal(err) |
| 626 |
} |
| 627 |
req.Header.Set("If-None-Match", `"123"`) |
| 628 |
resp, err := cst.c.Do(req) |
| 629 |
if err != nil { |
| 630 |
t.Fatal(err) |
| 631 |
} |
| 632 |
b, err := io.ReadAll(resp.Body) |
| 633 |
resp.Body.Close() |
| 634 |
if err != nil { |
| 635 |
t.Fatal("reading Body:", err) |
| 636 |
} |
| 637 |
if len(b) != 0 { |
| 638 |
t.Errorf("non-empty body") |
| 639 |
} |
| 640 |
if g, e := resp.StatusCode, StatusNotModified; g != e { |
| 641 |
t.Errorf("status mismatch: got %d, want %d", g, e) |
| 642 |
} |
| 643 |
// HTTP1 transport sets ContentLength to 0. |
| 644 |
if g, e1, e2 := resp.ContentLength, int64(-1), int64(0); g != e1 && g != e2 { |
| 645 |
t.Errorf("Content-Length mismatch: got %d, want %d or %d", g, e1, e2) |
| 646 |
} |
| 647 |
if resp.Header.Get("Content-Type") != "" { |
| 648 |
t.Errorf("Content-Type present, but it should not be") |
| 649 |
} |
| 650 |
if resp.Header.Get("Content-Encoding") != "" { |
| 651 |
t.Errorf("Content-Encoding present, but it should not be") |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
func TestServeIndexHtml(t *testing.T) { run(t, testServeIndexHtml) } |
| 656 |
func testServeIndexHtml(t *testing.T, mode testMode) { |
| 657 |
for i := 0; i < 2; i++ { |
| 658 |
var h Handler |
| 659 |
var name string |
| 660 |
switch i { |
| 661 |
case 0: |
| 662 |
h = FileServer(Dir(".")) |
| 663 |
name = "Dir" |
| 664 |
case 1: |
| 665 |
h = FileServer(FS(os.DirFS("."))) |
| 666 |
name = "DirFS" |
| 667 |
} |
| 668 |
t.Run(name, func(t *testing.T) { |
| 669 |
const want = "index.html says hello\n" |
| 670 |
ts := newClientServerTest(t, mode, h).ts |
| 671 |
|
| 672 |
for _, path := range []string{"/testdata/", "/testdata/index.html"} { |
| 673 |
res, err := ts.Client().Get(ts.URL + path) |
| 674 |
if err != nil { |
| 675 |
t.Fatal(err) |
| 676 |
} |
| 677 |
b, err := io.ReadAll(res.Body) |
| 678 |
if err != nil { |
| 679 |
t.Fatal("reading Body:", err) |
| 680 |
} |
| 681 |
if s := string(b); s != want { |
| 682 |
t.Errorf("for path %q got %q, want %q", path, s, want) |
| 683 |
} |
| 684 |
res.Body.Close() |
| 685 |
} |
| 686 |
}) |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
func TestServeIndexHtmlFS(t *testing.T) { run(t, testServeIndexHtmlFS) } |
| 691 |
func testServeIndexHtmlFS(t *testing.T, mode testMode) { |
| 692 |
const want = "index.html says hello\n" |
| 693 |
ts := newClientServerTest(t, mode, FileServer(Dir("."))).ts |
| 694 |
defer ts.Close() |
| 695 |
|
| 696 |
for _, path := range []string{"/testdata/", "/testdata/index.html"} { |
| 697 |
res, err := ts.Client().Get(ts.URL + path) |
| 698 |
if err != nil { |
| 699 |
t.Fatal(err) |
| 700 |
} |
| 701 |
b, err := io.ReadAll(res.Body) |
| 702 |
if err != nil { |
| 703 |
t.Fatal("reading Body:", err) |
| 704 |
} |
| 705 |
if s := string(b); s != want { |
| 706 |
t.Errorf("for path %q got %q, want %q", path, s, want) |
| 707 |
} |
| 708 |
res.Body.Close() |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
func TestFileServerZeroByte(t *testing.T) { run(t, testFileServerZeroByte) } |
| 713 |
func testFileServerZeroByte(t *testing.T, mode testMode) { |
| 714 |
ts := newClientServerTest(t, mode, FileServer(Dir("."))).ts |
| 715 |
|
| 716 |
c, err := net.Dial("tcp", ts.Listener.Addr().String()) |
| 717 |
if err != nil { |
| 718 |
t.Fatal(err) |
| 719 |
} |
| 720 |
defer c.Close() |
| 721 |
_, err = fmt.Fprintf(c, "GET /..\x00 HTTP/1.0\r\n\r\n") |
| 722 |
if err != nil { |
| 723 |
t.Fatal(err) |
| 724 |
} |
| 725 |
var got bytes.Buffer |
| 726 |
bufr := bufio.NewReader(io.TeeReader(c, &got)) |
| 727 |
res, err := ReadResponse(bufr, nil) |
| 728 |
if err != nil { |
| 729 |
t.Fatal("ReadResponse: ", err) |
| 730 |
} |
| 731 |
if res.StatusCode == 200 { |
| 732 |
t.Errorf("got status 200; want an error. Body is:\n%s", got.Bytes()) |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
func TestFileServerNullByte(t *testing.T) { run(t, testFileServerNullByte) } |
| 737 |
func testFileServerNullByte(t *testing.T, mode testMode) { |
| 738 |
ts := newClientServerTest(t, mode, FileServer(Dir("testdata"))).ts |
| 739 |
|
| 740 |
for _, path := range []string{ |
| 741 |
"/file%00", |
| 742 |
"/%00", |
| 743 |
"/file/qwe/%00", |
| 744 |
} { |
| 745 |
res, err := ts.Client().Get(ts.URL + path) |
| 746 |
if err != nil { |
| 747 |
t.Fatal(err) |
| 748 |
} |
| 749 |
res.Body.Close() |
| 750 |
if res.StatusCode != 404 { |
| 751 |
t.Errorf("Get(%q): got status %v, want 404", path, res.StatusCode) |
| 752 |
} |
| 753 |
|
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
func TestFileServerNamesEscape(t *testing.T) { run(t, testFileServerNamesEscape) } |
| 758 |
func testFileServerNamesEscape(t *testing.T, mode testMode) { |
| 759 |
ts := newClientServerTest(t, mode, FileServer(Dir("testdata"))).ts |
| 760 |
for _, path := range []string{ |
| 761 |
"/../testdata/file", |
| 762 |
"/NUL", // don't read from device files on Windows |
| 763 |
} { |
| 764 |
res, err := ts.Client().Get(ts.URL + path) |
| 765 |
if err != nil { |
| 766 |
t.Fatal(err) |
| 767 |
} |
| 768 |
res.Body.Close() |
| 769 |
if res.StatusCode < 400 || res.StatusCode > 599 { |
| 770 |
t.Errorf("Get(%q): got status %v, want 4xx or 5xx", path, res.StatusCode) |
| 771 |
} |
| 772 |
|
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
type fakeFileInfo struct { |
| 777 |
dir bool |
| 778 |
basename string |
| 779 |
modtime time.Time |
| 780 |
ents []*fakeFileInfo |
| 781 |
contents string |
| 782 |
err error |
| 783 |
} |
| 784 |
|
| 785 |
func (f *fakeFileInfo) Name() string { return f.basename } |
| 786 |
func (f *fakeFileInfo) Sys() any { return nil } |
| 787 |
func (f *fakeFileInfo) ModTime() time.Time { return f.modtime } |
| 788 |
func (f *fakeFileInfo) IsDir() bool { return f.dir } |
| 789 |
func (f *fakeFileInfo) Size() int64 { return int64(len(f.contents)) } |
| 790 |
func (f *fakeFileInfo) Mode() fs.FileMode { |
| 791 |
if f.dir { |
| 792 |
return 0755 | fs.ModeDir |
| 793 |
} |
| 794 |
return 0644 |
| 795 |
} |
| 796 |
|
| 797 |
func (f *fakeFileInfo) String() string { |
| 798 |
return fs.FormatFileInfo(f) |
| 799 |
} |
| 800 |
|
| 801 |
type fakeFile struct { |
| 802 |
io.ReadSeeker |
| 803 |
fi *fakeFileInfo |
| 804 |
path string // as opened |
| 805 |
entpos int |
| 806 |
} |
| 807 |
|
| 808 |
func (f *fakeFile) Close() error { return nil } |
| 809 |
func (f *fakeFile) Stat() (fs.FileInfo, error) { return f.fi, nil } |
| 810 |
func (f *fakeFile) Readdir(count int) ([]fs.FileInfo, error) { |
| 811 |
if !f.fi.dir { |
| 812 |
return nil, fs.ErrInvalid |
| 813 |
} |
| 814 |
var fis []fs.FileInfo |
| 815 |
|
| 816 |
limit := f.entpos + count |
| 817 |
if count <= 0 || limit > len(f.fi.ents) { |
| 818 |
limit = len(f.fi.ents) |
| 819 |
} |
| 820 |
for ; f.entpos < limit; f.entpos++ { |
| 821 |
fis = append(fis, f.fi.ents[f.entpos]) |
| 822 |
} |
| 823 |
|
| 824 |
if len(fis) == 0 && count > 0 { |
| 825 |
return fis, io.EOF |
| 826 |
} else { |
| 827 |
return fis, nil |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
type fakeFS map[string]*fakeFileInfo |
| 832 |
|
| 833 |
func (fsys fakeFS) Open(name string) (File, error) { |
| 834 |
name = path.Clean(name) |
| 835 |
f, ok := fsys[name] |
| 836 |
if !ok { |
| 837 |
return nil, fs.ErrNotExist |
| 838 |
} |
| 839 |
if f.err != nil { |
| 840 |
return nil, f.err |
| 841 |
} |
| 842 |
return &fakeFile{ReadSeeker: strings.NewReader(f.contents), fi: f, path: name}, nil |
| 843 |
} |
| 844 |
|
| 845 |
func TestDirectoryIfNotModified(t *testing.T) { run(t, testDirectoryIfNotModified) } |
| 846 |
func testDirectoryIfNotModified(t *testing.T, mode testMode) { |
| 847 |
const indexContents = "I am a fake index.html file" |
| 848 |
fileMod := time.Unix(1000000000, 0).UTC() |
| 849 |
fileModStr := fileMod.Format(TimeFormat) |
| 850 |
dirMod := time.Unix(123, 0).UTC() |
| 851 |
indexFile := &fakeFileInfo{ |
| 852 |
basename: "index.html", |
| 853 |
modtime: fileMod, |
| 854 |
contents: indexContents, |
| 855 |
} |
| 856 |
fs := fakeFS{ |
| 857 |
"/": &fakeFileInfo{ |
| 858 |
dir: true, |
| 859 |
modtime: dirMod, |
| 860 |
ents: []*fakeFileInfo{indexFile}, |
| 861 |
}, |
| 862 |
"/index.html": indexFile, |
| 863 |
} |
| 864 |
|
| 865 |
ts := newClientServerTest(t, mode, FileServer(fs)).ts |
| 866 |
|
| 867 |
res, err := ts.Client().Get(ts.URL) |
| 868 |
if err != nil { |
| 869 |
t.Fatal(err) |
| 870 |
} |
| 871 |
b, err := io.ReadAll(res.Body) |
| 872 |
if err != nil { |
| 873 |
t.Fatal(err) |
| 874 |
} |
| 875 |
if string(b) != indexContents { |
| 876 |
t.Fatalf("Got body %q; want %q", b, indexContents) |
| 877 |
} |
| 878 |
res.Body.Close() |
| 879 |
|
| 880 |
lastMod := res.Header.Get("Last-Modified") |
| 881 |
if lastMod != fileModStr { |
| 882 |
t.Fatalf("initial Last-Modified = %q; want %q", lastMod, fileModStr) |
| 883 |
} |
| 884 |
|
| 885 |
req, _ := NewRequest("GET", ts.URL, nil) |
| 886 |
req.Header.Set("If-Modified-Since", lastMod) |
| 887 |
|
| 888 |
c := ts.Client() |
| 889 |
res, err = c.Do(req) |
| 890 |
if err != nil { |
| 891 |
t.Fatal(err) |
| 892 |
} |
| 893 |
if res.StatusCode != 304 { |
| 894 |
t.Fatalf("Code after If-Modified-Since request = %v; want 304", res.StatusCode) |
| 895 |
} |
| 896 |
res.Body.Close() |
| 897 |
|
| 898 |
// Advance the index.html file's modtime, but not the directory's. |
| 899 |
indexFile.modtime = indexFile.modtime.Add(1 * time.Hour) |
| 900 |
|
| 901 |
res, err = c.Do(req) |
| 902 |
if err != nil { |
| 903 |
t.Fatal(err) |
| 904 |
} |
| 905 |
if res.StatusCode != 200 { |
| 906 |
t.Fatalf("Code after second If-Modified-Since request = %v; want 200; res is %#v", res.StatusCode, res) |
| 907 |
} |
| 908 |
res.Body.Close() |
| 909 |
} |
| 910 |
|
| 911 |
func mustStat(t *testing.T, fileName string) fs.FileInfo { |
| 912 |
fi, err := os.Stat(fileName) |
| 913 |
if err != nil { |
| 914 |
t.Fatal(err) |
| 915 |
} |
| 916 |
return fi |
| 917 |
} |
| 918 |
|
| 919 |
func TestServeContent(t *testing.T) { run(t, testServeContent) } |
| 920 |
func testServeContent(t *testing.T, mode testMode) { |
| 921 |
type serveParam struct { |
| 922 |
name string |
| 923 |
modtime time.Time |
| 924 |
content io.ReadSeeker |
| 925 |
contentType string |
| 926 |
etag string |
| 927 |
} |
| 928 |
servec := make(chan serveParam, 1) |
| 929 |
ts := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) { |
| 930 |
p := <-servec |
| 931 |
if p.etag != "" { |
| 932 |
w.Header().Set("ETag", p.etag) |
| 933 |
} |
| 934 |
if p.contentType != "" { |
| 935 |
w.Header().Set("Content-Type", p.contentType) |
| 936 |
} |
| 937 |
ServeContent(w, r, p.name, p.modtime, p.content) |
| 938 |
})).ts |
| 939 |
|
| 940 |
type testCase struct { |
| 941 |
// One of file or content must be set: |
| 942 |
file string |
| 943 |
content io.ReadSeeker |
| 944 |
|
| 945 |
modtime time.Time |
| 946 |
serveETag string // optional |
| 947 |
serveContentType string // optional |
| 948 |
reqHeader map[string]string |
| 949 |
wantLastMod string |
| 950 |
wantContentType string |
| 951 |
wantContentRange string |
| 952 |
wantStatus int |
| 953 |
} |
| 954 |
htmlModTime := mustStat(t, "testdata/index.html").ModTime() |
| 955 |
tests := map[string]testCase{ |
| 956 |
"no_last_modified": { |
| 957 |
file: "testdata/style.css", |
| 958 |
wantContentType: "text/css; charset=utf-8", |
| 959 |
wantStatus: 200, |
| 960 |
}, |
| 961 |
"with_last_modified": { |
| 962 |
file: "testdata/index.html", |
| 963 |
wantContentType: "text/html; charset=utf-8", |
| 964 |
modtime: htmlModTime, |
| 965 |
wantLastMod: htmlModTime.UTC().Format(TimeFormat), |
| 966 |
wantStatus: 200, |
| 967 |
}, |
| 968 |
"not_modified_modtime": { |
| 969 |
file: "testdata/style.css", |
| 970 |
serveETag: `"foo"`, // Last-Modified sent only when no ETag |
| 971 |
modtime: htmlModTime, |
| 972 |
reqHeader: map[string]string{ |
| 973 |
"If-Modified-Since": htmlModTime.UTC().Format(TimeFormat), |
| 974 |
}, |
| 975 |
wantStatus: 304, |
| 976 |
}, |
| 977 |
"not_modified_modtime_with_contenttype": { |
| 978 |
file: "testdata/style.css", |
| 979 |
serveContentType: "text/css", // explicit content type |
| 980 |
serveETag: `"foo"`, // Last-Modified sent only when no ETag |
| 981 |
modtime: htmlModTime, |
| 982 |
reqHeader: map[string]string{ |
| 983 |
"If-Modified-Since": htmlModTime.UTC().Format(TimeFormat), |
| 984 |
}, |
| 985 |
wantStatus: 304, |
| 986 |
}, |
| 987 |
"not_modified_etag": { |
| 988 |
file: "testdata/style.css", |
| 989 |
serveETag: `"foo"`, |
| 990 |
reqHeader: map[string]string{ |
| 991 |
"If-None-Match": `"foo"`, |
| 992 |
}, |
| 993 |
wantStatus: 304, |
| 994 |
}, |
| 995 |
"not_modified_etag_no_seek": { |
| 996 |
content: panicOnSeek{nil}, // should never be called |
| 997 |
serveETag: `W/"foo"`, // If-None-Match uses weak ETag comparison |
| 998 |
reqHeader: map[string]string{ |
| 999 |
"If-None-Match": `"baz", W/"foo"`, |
| 1000 |
}, |
| 1001 |
wantStatus: 304, |
| 1002 |
}, |
| 1003 |
"if_none_match_mismatch": { |
| 1004 |
file: "testdata/style.css", |
| 1005 |
serveETag: `"foo"`, |
| 1006 |
reqHeader: map[string]string{ |
| 1007 |
"If-None-Match": `"Foo"`, |
| 1008 |
}, |
| 1009 |
wantStatus: 200, |
| 1010 |
wantContentType: "text/css; charset=utf-8", |
| 1011 |
}, |
| 1012 |
"if_none_match_malformed": { |
| 1013 |
file: "testdata/style.css", |
| 1014 |
serveETag: `"foo"`, |
| 1015 |
reqHeader: map[string]string{ |
| 1016 |
"If-None-Match": `,`, |
| 1017 |
}, |
| 1018 |
wantStatus: 200, |
| 1019 |
wantContentType: "text/css; charset=utf-8", |
| 1020 |
}, |
| 1021 |
"range_good": { |
| 1022 |
file: "testdata/style.css", |
| 1023 |
serveETag: `"A"`, |
| 1024 |
reqHeader: map[string]string{ |
| 1025 |
"Range": "bytes=0-4", |
| 1026 |
}, |
| 1027 |
wantStatus: StatusPartialContent, |
| 1028 |
wantContentType: "text/css; charset=utf-8", |
| 1029 |
wantContentRange: "bytes 0-4/8", |
| 1030 |
}, |
| 1031 |
"range_match": { |
| 1032 |
file: "testdata/style.css", |
| 1033 |
serveETag: `"A"`, |
| 1034 |
reqHeader: map[string]string{ |
| 1035 |
"Range": "bytes=0-4", |
| 1036 |
"If-Range": `"A"`, |
| 1037 |
}, |
| 1038 |
wantStatus: StatusPartialContent, |
| 1039 |
wantContentType: "text/css; charset=utf-8", |
| 1040 |
wantContentRange: "bytes 0-4/8", |
| 1041 |
}, |
| 1042 |
"range_match_weak_etag": { |
| 1043 |
file: "testdata/style.css", |
| 1044 |
serveETag: `W/"A"`, |
| 1045 |
reqHeader: map[string]string{ |
| 1046 |
"Range": "bytes=0-4", |
| 1047 |
"If-Range": `W/"A"`, |
| 1048 |
}, |
| 1049 |
wantStatus: 200, |
| 1050 |
wantContentType: "text/css; charset=utf-8", |
| 1051 |
}, |
| 1052 |
"range_no_overlap": { |
| 1053 |
file: "testdata/style.css", |
| 1054 |
serveETag: `"A"`, |
| 1055 |
reqHeader: map[string]string{ |
| 1056 |
"Range": "bytes=10-20", |
| 1057 |
}, |
| 1058 |
wantStatus: StatusRequestedRangeNotSatisfiable, |
| 1059 |
wantContentType: "text/plain; charset=utf-8", |
| 1060 |
wantContentRange: "bytes */8", |
| 1061 |
}, |
| 1062 |
// An If-Range resource for entity "A", but entity "B" is now current. |
| 1063 |
// The Range request should be ignored. |
| 1064 |
"range_no_match": { |
| 1065 |
file: "testdata/style.css", |
| 1066 |
serveETag: `"A"`, |
| 1067 |
reqHeader: map[string]string{ |
| 1068 |
"Range": "bytes=0-4", |
| 1069 |
"If-Range": `"B"`, |
| 1070 |
}, |
| 1071 |
wantStatus: 200, |
| 1072 |
wantContentType: "text/css; charset=utf-8", |
| 1073 |
}, |
| 1074 |
"range_with_modtime": { |
| 1075 |
file: "testdata/style.css", |
| 1076 |
modtime: time.Date(2014, 6, 25, 17, 12, 18, 0 /* nanos */, time.UTC), |
| 1077 |
reqHeader: map[string]string{ |
| 1078 |
"Range": "bytes=0-4", |
| 1079 |
"If-Range": "Wed, 25 Jun 2014 17:12:18 GMT", |
| 1080 |
}, |
| 1081 |
wantStatus: StatusPartialContent, |
| 1082 |
wantContentType: "text/css; charset=utf-8", |
| 1083 |
wantContentRange: "bytes 0-4/8", |
| 1084 |
wantLastMod: "Wed, 25 Jun 2014 17:12:18 GMT", |
| 1085 |
}, |
| 1086 |
"range_with_modtime_mismatch": { |
| 1087 |
file: "testdata/style.css", |
| 1088 |
modtime: time.Date(2014, 6, 25, 17, 12, 18, 0 /* nanos */, time.UTC), |
| 1089 |
reqHeader: map[string]string{ |
| 1090 |
"Range": "bytes=0-4", |
| 1091 |
"If-Range": "Wed, 25 Jun 2014 17:12:19 GMT", |
| 1092 |
}, |
| 1093 |
wantStatus: StatusOK, |
| 1094 |
wantContentType: "text/css; charset=utf-8", |
| 1095 |
wantLastMod: "Wed, 25 Jun 2014 17:12:18 GMT", |
| 1096 |
}, |
| 1097 |
"range_with_modtime_nanos": { |
| 1098 |
file: "testdata/style.css", |
| 1099 |
modtime: time.Date(2014, 6, 25, 17, 12, 18, 123 /* nanos */, time.UTC), |
| 1100 |
reqHeader: map[string]string{ |
| 1101 |
"Range": "bytes=0-4", |
| 1102 |
"If-Range": "Wed, 25 Jun 2014 17:12:18 GMT", |
| 1103 |
}, |
| 1104 |
wantStatus: StatusPartialContent, |
| 1105 |
wantContentType: "text/css; charset=utf-8", |
| 1106 |
wantContentRange: "bytes 0-4/8", |
| 1107 |
wantLastMod: "Wed, 25 Jun 2014 17:12:18 GMT", |
| 1108 |
}, |
| 1109 |
"unix_zero_modtime": { |
| 1110 |
content: strings.NewReader("<html>foo"), |
| 1111 |
modtime: time.Unix(0, 0), |
| 1112 |
wantStatus: StatusOK, |
| 1113 |
wantContentType: "text/html; charset=utf-8", |
| 1114 |
}, |
| 1115 |
"ifmatch_matches": { |
| 1116 |
file: "testdata/style.css", |
| 1117 |
serveETag: `"A"`, |
| 1118 |
reqHeader: map[string]string{ |
| 1119 |
"If-Match": `"Z", "A"`, |
| 1120 |
}, |
| 1121 |
wantStatus: 200, |
| 1122 |
wantContentType: "text/css; charset=utf-8", |
| 1123 |
}, |
| 1124 |
"ifmatch_star": { |
| 1125 |
file: "testdata/style.css", |
| 1126 |
serveETag: `"A"`, |
| 1127 |
reqHeader: map[string]string{ |
| 1128 |
"If-Match": `*`, |
| 1129 |
}, |
| 1130 |
wantStatus: 200, |
| 1131 |
wantContentType: "text/css; charset=utf-8", |
| 1132 |
}, |
| 1133 |
"ifmatch_failed": { |
| 1134 |
file: "testdata/style.css", |
| 1135 |
serveETag: `"A"`, |
| 1136 |
reqHeader: map[string]string{ |
| 1137 |
"If-Match": `"B"`, |
| 1138 |
}, |
| 1139 |
wantStatus: 412, |
| 1140 |
}, |
| 1141 |
"ifmatch_fails_on_weak_etag": { |
| 1142 |
file: "testdata/style.css", |
| 1143 |
serveETag: `W/"A"`, |
| 1144 |
reqHeader: map[string]string{ |
| 1145 |
"If-Match": `W/"A"`, |
| 1146 |
}, |
| 1147 |
wantStatus: 412, |
| 1148 |
}, |
| 1149 |
"if_unmodified_since_true": { |
| 1150 |
file: "testdata/style.css", |
| 1151 |
modtime: htmlModTime, |
| 1152 |
reqHeader: map[string]string{ |
| 1153 |
"If-Unmodified-Since": htmlModTime.UTC().Format(TimeFormat), |
| 1154 |
}, |
| 1155 |
wantStatus: 200, |
| 1156 |
wantContentType: "text/css; charset=utf-8", |
| 1157 |
wantLastMod: htmlModTime.UTC().Format(TimeFormat), |
| 1158 |
}, |
| 1159 |
"if_unmodified_since_false": { |
| 1160 |
file: "testdata/style.css", |
| 1161 |
modtime: htmlModTime, |
| 1162 |
reqHeader: map[string]string{ |
| 1163 |
"If-Unmodified-Since": htmlModTime.Add(-2 * time.Second).UTC().Format(TimeFormat), |
| 1164 |
}, |
| 1165 |
wantStatus: 412, |
| 1166 |
wantLastMod: htmlModTime.UTC().Format(TimeFormat), |
| 1167 |
}, |
| 1168 |
} |
| 1169 |
for testName, tt := range tests { |
| 1170 |
var content io.ReadSeeker |
| 1171 |
if tt.file != "" { |
| 1172 |
f, err := os.Open(tt.file) |
| 1173 |
if err != nil { |
| 1174 |
t.Fatalf("test %q: %v", testName, err) |
| 1175 |
} |
| 1176 |
defer f.Close() |
| 1177 |
content = f |
| 1178 |
} else { |
| 1179 |
content = tt.content |
| 1180 |
} |
| 1181 |
for _, method := range []string{"GET", "HEAD"} { |
| 1182 |
//restore content in case it is consumed by previous method |
| 1183 |
if content, ok := content.(*strings.Reader); ok { |
| 1184 |
content.Seek(0, io.SeekStart) |
| 1185 |
} |
| 1186 |
|
| 1187 |
servec <- serveParam{ |
| 1188 |
name: filepath.Base(tt.file), |
| 1189 |
content: content, |
| 1190 |
modtime: tt.modtime, |
| 1191 |
etag: tt.serveETag, |
| 1192 |
contentType: tt.serveContentType, |
| 1193 |
} |
| 1194 |
req, err := NewRequest(method, ts.URL, nil) |
| 1195 |
if err != nil { |
| 1196 |
t.Fatal(err) |
| 1197 |
} |
| 1198 |
for k, v := range tt.reqHeader { |
| 1199 |
req.Header.Set(k, v) |
| 1200 |
} |
| 1201 |
|
| 1202 |
c := ts.Client() |
| 1203 |
res, err := c.Do(req) |
| 1204 |
if err != nil { |
| 1205 |
t.Fatal(err) |
| 1206 |
} |
| 1207 |
io.Copy(io.Discard, res.Body) |
| 1208 |
res.Body.Close() |
| 1209 |
if res.StatusCode != tt.wantStatus { |
| 1210 |
t.Errorf("test %q using %q: got status = %d; want %d", testName, method, res.StatusCode, tt.wantStatus) |
| 1211 |
} |
| 1212 |
if g, e := res.Header.Get("Content-Type"), tt.wantContentType; g != e { |
| 1213 |
t.Errorf("test %q using %q: got content-type = %q, want %q", testName, method, g, e) |
| 1214 |
} |
| 1215 |
if g, e := res.Header.Get("Content-Range"), tt.wantContentRange; g != e { |
| 1216 |
t.Errorf("test %q using %q: got content-range = %q, want %q", testName, method, g, e) |
| 1217 |
} |
| 1218 |
if g, e := res.Header.Get("Last-Modified"), tt.wantLastMod; g != e { |
| 1219 |
t.Errorf("test %q using %q: got last-modified = %q, want %q", testName, method, g, e) |
| 1220 |
} |
| 1221 |
} |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
// Issue 12991 |
| 1226 |
func TestServerFileStatError(t *testing.T) { |
| 1227 |
rec := httptest.NewRecorder() |
| 1228 |
r, _ := NewRequest("GET", "http://foo/", nil) |
| 1229 |
redirect := false |
| 1230 |
name := "file.txt" |
| 1231 |
fs := issue12991FS{} |
| 1232 |
ExportServeFile(rec, r, fs, name, redirect) |
| 1233 |
if body := rec.Body.String(); !strings.Contains(body, "403") || !strings.Contains(body, "Forbidden") { |
| 1234 |
t.Errorf("wanted 403 forbidden message; got: %s", body) |
| 1235 |
} |
| 1236 |
} |
| 1237 |
|
| 1238 |
type issue12991FS struct{} |
| 1239 |
|
| 1240 |
func (issue12991FS) Open(string) (File, error) { return issue12991File{}, nil } |
| 1241 |
|
| 1242 |
type issue12991File struct{ File } |
| 1243 |
|
| 1244 |
func (issue12991File) Stat() (fs.FileInfo, error) { return nil, fs.ErrPermission } |
| 1245 |
func (issue12991File) Close() error { return nil } |
| 1246 |
|
| 1247 |
func TestFileServerErrorMessages(t *testing.T) { |
| 1248 |
run(t, func(t *testing.T, mode testMode) { |
| 1249 |
t.Run("keepheaders=0", func(t *testing.T) { |
| 1250 |
testFileServerErrorMessages(t, mode, false) |
| 1251 |
}) |
| 1252 |
t.Run("keepheaders=1", func(t *testing.T) { |
| 1253 |
testFileServerErrorMessages(t, mode, true) |
| 1254 |
}) |
| 1255 |
}, testNotParallel) |
| 1256 |
} |
| 1257 |
func testFileServerErrorMessages(t *testing.T, mode testMode, keepHeaders bool) { |
| 1258 |
if keepHeaders { |
| 1259 |
t.Setenv("GODEBUG", "httpservecontentkeepheaders=1") |
| 1260 |
} |
| 1261 |
fs := fakeFS{ |
| 1262 |
"/500": &fakeFileInfo{ |
| 1263 |
err: errors.New("random error"), |
| 1264 |
}, |
| 1265 |
"/403": &fakeFileInfo{ |
| 1266 |
err: &fs.PathError{Err: fs.ErrPermission}, |
| 1267 |
}, |
| 1268 |
} |
| 1269 |
server := FileServer(fs) |
| 1270 |
h := func(w http.ResponseWriter, r *http.Request) { |
| 1271 |
w.Header().Set("Etag", "étude") |
| 1272 |
w.Header().Set("Cache-Control", "yes") |
| 1273 |
w.Header().Set("Content-Type", "awesome") |
| 1274 |
w.Header().Set("Last-Modified", "yesterday") |
| 1275 |
server.ServeHTTP(w, r) |
| 1276 |
} |
| 1277 |
ts := newClientServerTest(t, mode, http.HandlerFunc(h)).ts |
| 1278 |
c := ts.Client() |
| 1279 |
for _, code := range []int{403, 404, 500} { |
| 1280 |
res, err := c.Get(fmt.Sprintf("%s/%d", ts.URL, code)) |
| 1281 |
if err != nil { |
| 1282 |
t.Errorf("Error fetching /%d: %v", code, err) |
| 1283 |
continue |
| 1284 |
} |
| 1285 |
res.Body.Close() |
| 1286 |
if res.StatusCode != code { |
| 1287 |
t.Errorf("GET /%d: StatusCode = %d; want %d", code, res.StatusCode, code) |
| 1288 |
} |
| 1289 |
for _, hdr := range []string{"Etag", "Last-Modified", "Cache-Control"} { |
| 1290 |
if v, got := res.Header[hdr]; got != keepHeaders { |
| 1291 |
want := "not present" |
| 1292 |
if keepHeaders { |
| 1293 |
want = "present" |
| 1294 |
} |
| 1295 |
t.Errorf("GET /%d: Header[%q] = %q, want %v", code, hdr, v, want) |
| 1296 |
} |
| 1297 |
} |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
// verifies that sendfile is being used on Linux |
| 1302 |
func TestLinuxSendfile(t *testing.T) { |
| 1303 |
setParallel(t) |
| 1304 |
defer afterTest(t) |
| 1305 |
if runtime.GOOS != "linux" { |
| 1306 |
t.Skip("skipping; linux-only test") |
| 1307 |
} |
| 1308 |
if _, err := exec.LookPath("strace"); err != nil { |
| 1309 |
t.Skip("skipping; strace not found in path") |
| 1310 |
} |
| 1311 |
|
| 1312 |
ln, err := net.Listen("tcp", "127.0.0.1:0") |
| 1313 |
if err != nil { |
| 1314 |
t.Fatal(err) |
| 1315 |
} |
| 1316 |
lnf, err := ln.(*net.TCPListener).File() |
| 1317 |
if err != nil { |
| 1318 |
t.Fatal(err) |
| 1319 |
} |
| 1320 |
defer ln.Close() |
| 1321 |
|
| 1322 |
// Attempt to run strace, and skip on failure - this test requires SYS_PTRACE. |
| 1323 |
if err := testenv.Command(t, "strace", "-f", "-q", os.Args[0], "-test.run=^$").Run(); err != nil { |
| 1324 |
t.Skipf("skipping; failed to run strace: %v", err) |
| 1325 |
} |
| 1326 |
|
| 1327 |
filename := fmt.Sprintf("1kb-%d", os.Getpid()) |
| 1328 |
filepath := path.Join(os.TempDir(), filename) |
| 1329 |
|
| 1330 |
if err := os.WriteFile(filepath, bytes.Repeat([]byte{'a'}, 1<<10), 0755); err != nil { |
| 1331 |
t.Fatal(err) |
| 1332 |
} |
| 1333 |
defer os.Remove(filepath) |
| 1334 |
|
| 1335 |
var buf strings.Builder |
| 1336 |
child := testenv.Command(t, "strace", "-f", "-q", os.Args[0], "-test.run=^TestLinuxSendfileChild$") |
| 1337 |
child.ExtraFiles = append(child.ExtraFiles, lnf) |
| 1338 |
child.Env = append([]string{"GO_WANT_HELPER_PROCESS=1"}, os.Environ()...) |
| 1339 |
child.Stdout = &buf |
| 1340 |
child.Stderr = &buf |
| 1341 |
if err := child.Start(); err != nil { |
| 1342 |
t.Skipf("skipping; failed to start straced child: %v", err) |
| 1343 |
} |
| 1344 |
|
| 1345 |
res, err := Get(fmt.Sprintf("http://%s/%s", ln.Addr(), filename)) |
| 1346 |
if err != nil { |
| 1347 |
t.Fatalf("http client error: %v", err) |
| 1348 |
} |
| 1349 |
_, err = io.Copy(io.Discard, res.Body) |
| 1350 |
if err != nil { |
| 1351 |
t.Fatalf("client body read error: %v", err) |
| 1352 |
} |
| 1353 |
res.Body.Close() |
| 1354 |
|
| 1355 |
// Force child to exit cleanly. |
| 1356 |
Post(fmt.Sprintf("http://%s/quit", ln.Addr()), "", nil) |
| 1357 |
child.Wait() |
| 1358 |
|
| 1359 |
rx := regexp.MustCompile(`\b(n64:)?sendfile(64)?\(`) |
| 1360 |
out := buf.String() |
| 1361 |
if !rx.MatchString(out) { |
| 1362 |
t.Errorf("no sendfile system call found in:\n%s", out) |
| 1363 |
} |
| 1364 |
} |
| 1365 |
|
| 1366 |
func getBody(t *testing.T, testName string, req Request, client *Client) (*Response, []byte) { |
| 1367 |
r, err := client.Do(&req) |
| 1368 |
if err != nil { |
| 1369 |
t.Fatalf("%s: for URL %q, send error: %v", testName, req.URL.String(), err) |
| 1370 |
} |
| 1371 |
b, err := io.ReadAll(r.Body) |
| 1372 |
if err != nil { |
| 1373 |
t.Fatalf("%s: for URL %q, reading body: %v", testName, req.URL.String(), err) |
| 1374 |
} |
| 1375 |
return r, b |
| 1376 |
} |
| 1377 |
|
| 1378 |
// TestLinuxSendfileChild isn't a real test. It's used as a helper process |
| 1379 |
// for TestLinuxSendfile. |
| 1380 |
func TestLinuxSendfileChild(*testing.T) { |
| 1381 |
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { |
| 1382 |
return |
| 1383 |
} |
| 1384 |
defer os.Exit(0) |
| 1385 |
fd3 := os.NewFile(3, "ephemeral-port-listener") |
| 1386 |
ln, err := net.FileListener(fd3) |
| 1387 |
if err != nil { |
| 1388 |
panic(err) |
| 1389 |
} |
| 1390 |
mux := NewServeMux() |
| 1391 |
mux.Handle("/", FileServer(Dir(os.TempDir()))) |
| 1392 |
mux.HandleFunc("/quit", func(ResponseWriter, *Request) { |
| 1393 |
os.Exit(0) |
| 1394 |
}) |
| 1395 |
s := &Server{Handler: mux} |
| 1396 |
err = s.Serve(ln) |
| 1397 |
if err != nil { |
| 1398 |
panic(err) |
| 1399 |
} |
| 1400 |
} |
| 1401 |
|
| 1402 |
// Issues 18984, 49552: tests that requests for paths beyond files return not-found errors |
| 1403 |
func TestFileServerNotDirError(t *testing.T) { |
| 1404 |
run(t, func(t *testing.T, mode testMode) { |
| 1405 |
t.Run("Dir", func(t *testing.T) { |
| 1406 |
testFileServerNotDirError(t, mode, func(path string) FileSystem { return Dir(path) }) |
| 1407 |
}) |
| 1408 |
t.Run("FS", func(t *testing.T) { |
| 1409 |
testFileServerNotDirError(t, mode, func(path string) FileSystem { return FS(os.DirFS(path)) }) |
| 1410 |
}) |
| 1411 |
}) |
| 1412 |
} |
| 1413 |
|
| 1414 |
func testFileServerNotDirError(t *testing.T, mode testMode, newfs func(string) FileSystem) { |
| 1415 |
ts := newClientServerTest(t, mode, FileServer(newfs("testdata"))).ts |
| 1416 |
|
| 1417 |
res, err := ts.Client().Get(ts.URL + "/index.html/not-a-file") |
| 1418 |
if err != nil { |
| 1419 |
t.Fatal(err) |
| 1420 |
} |
| 1421 |
res.Body.Close() |
| 1422 |
if res.StatusCode != 404 { |
| 1423 |
t.Errorf("StatusCode = %v; want 404", res.StatusCode) |
| 1424 |
} |
| 1425 |
|
| 1426 |
test := func(name string, fsys FileSystem) { |
| 1427 |
t.Run(name, func(t *testing.T) { |
| 1428 |
_, err = fsys.Open("/index.html/not-a-file") |
| 1429 |
if err == nil { |
| 1430 |
t.Fatal("err == nil; want != nil") |
| 1431 |
} |
| 1432 |
if !errors.Is(err, fs.ErrNotExist) { |
| 1433 |
t.Errorf("err = %v; errors.Is(err, fs.ErrNotExist) = %v; want true", err, |
| 1434 |
errors.Is(err, fs.ErrNotExist)) |
| 1435 |
} |
| 1436 |
|
| 1437 |
_, err = fsys.Open("/index.html/not-a-dir/not-a-file") |
| 1438 |
if err == nil { |
| 1439 |
t.Fatal("err == nil; want != nil") |
| 1440 |
} |
| 1441 |
if !errors.Is(err, fs.ErrNotExist) { |
| 1442 |
t.Errorf("err = %v; errors.Is(err, fs.ErrNotExist) = %v; want true", err, |
| 1443 |
errors.Is(err, fs.ErrNotExist)) |
| 1444 |
} |
| 1445 |
}) |
| 1446 |
} |
| 1447 |
|
| 1448 |
absPath, err := filepath.Abs("testdata") |
| 1449 |
if err != nil { |
| 1450 |
t.Fatal("get abs path:", err) |
| 1451 |
} |
| 1452 |
|
| 1453 |
test("RelativePath", newfs("testdata")) |
| 1454 |
test("AbsolutePath", newfs(absPath)) |
| 1455 |
} |
| 1456 |
|
| 1457 |
func TestFileServerCleanPath(t *testing.T) { |
| 1458 |
tests := []struct { |
| 1459 |
path string |
| 1460 |
wantCode int |
| 1461 |
wantOpen []string |
| 1462 |
}{ |
| 1463 |
{"/", 200, []string{"/", "/index.html"}}, |
| 1464 |
{"/dir", 301, []string{"/dir"}}, |
| 1465 |
{"/dir/", 200, []string{"/dir", "/dir/index.html"}}, |
| 1466 |
} |
| 1467 |
for _, tt := range tests { |
| 1468 |
var log []string |
| 1469 |
rr := httptest.NewRecorder() |
| 1470 |
req, _ := NewRequest("GET", "http://foo.localhost"+tt.path, nil) |
| 1471 |
FileServer(fileServerCleanPathDir{&log}).ServeHTTP(rr, req) |
| 1472 |
if !slices.Equal(log, tt.wantOpen) { |
| 1473 |
t.Logf("For %s: Opens = %q; want %q", tt.path, log, tt.wantOpen) |
| 1474 |
} |
| 1475 |
if rr.Code != tt.wantCode { |
| 1476 |
t.Logf("For %s: Response code = %d; want %d", tt.path, rr.Code, tt.wantCode) |
| 1477 |
} |
| 1478 |
} |
| 1479 |
} |
| 1480 |
|
| 1481 |
type fileServerCleanPathDir struct { |
| 1482 |
log *[]string |
| 1483 |
} |
| 1484 |
|
| 1485 |
func (d fileServerCleanPathDir) Open(path string) (File, error) { |
| 1486 |
*(d.log) = append(*(d.log), path) |
| 1487 |
if path == "/" || path == "/dir" || path == "/dir/" { |
| 1488 |
// Just return back something that's a directory. |
| 1489 |
return Dir(".").Open(".") |
| 1490 |
} |
| 1491 |
return nil, fs.ErrNotExist |
| 1492 |
} |
| 1493 |
|
| 1494 |
type panicOnSeek struct{ io.ReadSeeker } |
| 1495 |
|
| 1496 |
func TestScanETag(t *testing.T) { |
| 1497 |
tests := []struct { |
| 1498 |
in string |
| 1499 |
wantETag string |
| 1500 |
wantRemain string |
| 1501 |
}{ |
| 1502 |
{`W/"etag-1"`, `W/"etag-1"`, ""}, |
| 1503 |
{`"etag-2"`, `"etag-2"`, ""}, |
| 1504 |
{`"etag-1", "etag-2"`, `"etag-1"`, `, "etag-2"`}, |
| 1505 |
{"", "", ""}, |
| 1506 |
{"W/", "", ""}, |
| 1507 |
{`W/"truc`, "", ""}, |
| 1508 |
{`w/"case-sensitive"`, "", ""}, |
| 1509 |
{`"spaced etag"`, "", ""}, |
| 1510 |
} |
| 1511 |
for _, test := range tests { |
| 1512 |
etag, remain := ExportScanETag(test.in) |
| 1513 |
if etag != test.wantETag || remain != test.wantRemain { |
| 1514 |
t.Errorf("scanETag(%q)=%q %q, want %q %q", test.in, etag, remain, test.wantETag, test.wantRemain) |
| 1515 |
} |
| 1516 |
} |
| 1517 |
} |
| 1518 |
|
| 1519 |
// Issue 40940: Ensure that we only accept non-negative suffix-lengths |
| 1520 |
// in "Range": "bytes=-N", and should reject "bytes=--2". |
| 1521 |
func TestServeFileRejectsInvalidSuffixLengths(t *testing.T) { |
| 1522 |
run(t, testServeFileRejectsInvalidSuffixLengths, []testMode{http1Mode, https1Mode, http2Mode}) |
| 1523 |
} |
| 1524 |
func testServeFileRejectsInvalidSuffixLengths(t *testing.T, mode testMode) { |
| 1525 |
cst := newClientServerTest(t, mode, FileServer(Dir("testdata"))).ts |
| 1526 |
|
| 1527 |
tests := []struct { |
| 1528 |
r string |
| 1529 |
wantCode int |
| 1530 |
wantBody string |
| 1531 |
}{ |
| 1532 |
{"bytes=--6", 416, "invalid range\n"}, |
| 1533 |
{"bytes=--0", 416, "invalid range\n"}, |
| 1534 |
{"bytes=---0", 416, "invalid range\n"}, |
| 1535 |
{"bytes=-6", 206, "hello\n"}, |
| 1536 |
{"bytes=6-", 206, "html says hello\n"}, |
| 1537 |
{"bytes=-6-", 416, "invalid range\n"}, |
| 1538 |
{"bytes=-0", 206, ""}, |
| 1539 |
{"bytes=", 200, "index.html says hello\n"}, |
| 1540 |
} |
| 1541 |
|
| 1542 |
for _, tt := range tests { |
| 1543 |
tt := tt |
| 1544 |
t.Run(tt.r, func(t *testing.T) { |
| 1545 |
req, err := NewRequest("GET", cst.URL+"/index.html", nil) |
| 1546 |
if err != nil { |
| 1547 |
t.Fatal(err) |
| 1548 |
} |
| 1549 |
req.Header.Set("Range", tt.r) |
| 1550 |
res, err := cst.Client().Do(req) |
| 1551 |
if err != nil { |
| 1552 |
t.Fatal(err) |
| 1553 |
} |
| 1554 |
if g, w := res.StatusCode, tt.wantCode; g != w { |
| 1555 |
t.Errorf("StatusCode mismatch: got %d want %d", g, w) |
| 1556 |
} |
| 1557 |
slurp, err := io.ReadAll(res.Body) |
| 1558 |
res.Body.Close() |
| 1559 |
if err != nil { |
| 1560 |
t.Fatal(err) |
| 1561 |
} |
| 1562 |
if g, w := string(slurp), tt.wantBody; g != w { |
| 1563 |
t.Fatalf("Content mismatch:\nGot: %q\nWant: %q", g, w) |
| 1564 |
} |
| 1565 |
}) |
| 1566 |
} |
| 1567 |
} |
| 1568 |
|
| 1569 |
func TestFileServerMethods(t *testing.T) { |
| 1570 |
run(t, testFileServerMethods) |
| 1571 |
} |
| 1572 |
func testFileServerMethods(t *testing.T, mode testMode) { |
| 1573 |
ts := newClientServerTest(t, mode, FileServer(Dir("testdata"))).ts |
| 1574 |
|
| 1575 |
file, err := os.ReadFile(testFile) |
| 1576 |
if err != nil { |
| 1577 |
t.Fatal("reading file:", err) |
| 1578 |
} |
| 1579 |
|
| 1580 |
// Get contents via various methods. |
| 1581 |
// |
| 1582 |
// See https://go.dev/issue/59471 for a proposal to limit the set of methods handled. |
| 1583 |
// For now, test the historical behavior. |
| 1584 |
for _, method := range []string{ |
| 1585 |
MethodGet, |
| 1586 |
MethodHead, |
| 1587 |
MethodPost, |
| 1588 |
MethodPut, |
| 1589 |
MethodPatch, |
| 1590 |
MethodDelete, |
| 1591 |
MethodOptions, |
| 1592 |
MethodTrace, |
| 1593 |
} { |
| 1594 |
req, _ := NewRequest(method, ts.URL+"/file", nil) |
| 1595 |
t.Log(req.URL) |
| 1596 |
res, err := ts.Client().Do(req) |
| 1597 |
if err != nil { |
| 1598 |
t.Fatal(err) |
| 1599 |
} |
| 1600 |
body, err := io.ReadAll(res.Body) |
| 1601 |
res.Body.Close() |
| 1602 |
if err != nil { |
| 1603 |
t.Fatal(err) |
| 1604 |
} |
| 1605 |
wantBody := file |
| 1606 |
if method == MethodHead { |
| 1607 |
wantBody = nil |
| 1608 |
} |
| 1609 |
if !bytes.Equal(body, wantBody) { |
| 1610 |
t.Fatalf("%v: got body %q, want %q", method, body, wantBody) |
| 1611 |
} |
| 1612 |
if got, want := res.Header.Get("Content-Length"), fmt.Sprint(len(file)); got != want { |
| 1613 |
t.Fatalf("%v: got Content-Length %q, want %q", method, got, want) |
| 1614 |
} |
| 1615 |
} |
| 1616 |
} |
| 1617 |
|
| 1618 |
func TestFileServerFS(t *testing.T) { |
| 1619 |
filename := "index.html" |
| 1620 |
contents := []byte("index.html says hello") |
| 1621 |
fsys := fstest.MapFS{ |
| 1622 |
filename: {Data: contents}, |
| 1623 |
} |
| 1624 |
ts := newClientServerTest(t, http1Mode, FileServerFS(fsys)).ts |
| 1625 |
defer ts.Close() |
| 1626 |
|
| 1627 |
res, err := ts.Client().Get(ts.URL + "/" + filename) |
| 1628 |
if err != nil { |
| 1629 |
t.Fatal(err) |
| 1630 |
} |
| 1631 |
b, err := io.ReadAll(res.Body) |
| 1632 |
if err != nil { |
| 1633 |
t.Fatal("reading Body:", err) |
| 1634 |
} |
| 1635 |
if s := string(b); s != string(contents) { |
| 1636 |
t.Errorf("for path %q got %q, want %q", filename, s, contents) |
| 1637 |
} |
| 1638 |
res.Body.Close() |
| 1639 |
} |
| 1640 |
|
| 1641 |
func TestServeFileFS(t *testing.T) { |
| 1642 |
filename := "index.html" |
| 1643 |
contents := []byte("index.html says hello") |
| 1644 |
fsys := fstest.MapFS{ |
| 1645 |
filename: {Data: contents}, |
| 1646 |
} |
| 1647 |
ts := newClientServerTest(t, http1Mode, HandlerFunc(func(w ResponseWriter, r *Request) { |
| 1648 |
ServeFileFS(w, r, fsys, filename) |
| 1649 |
})).ts |
| 1650 |
defer ts.Close() |
| 1651 |
|
| 1652 |
res, err := ts.Client().Get(ts.URL + "/" + filename) |
| 1653 |
if err != nil { |
| 1654 |
t.Fatal(err) |
| 1655 |
} |
| 1656 |
b, err := io.ReadAll(res.Body) |
| 1657 |
if err != nil { |
| 1658 |
t.Fatal("reading Body:", err) |
| 1659 |
} |
| 1660 |
if s := string(b); s != string(contents) { |
| 1661 |
t.Errorf("for path %q got %q, want %q", filename, s, contents) |
| 1662 |
} |
| 1663 |
res.Body.Close() |
| 1664 |
} |
| 1665 |
|
| 1666 |
func TestServeFileZippingResponseWriter(t *testing.T) { |
| 1667 |
// This test exercises a pattern which is incorrect, |
| 1668 |
// but has been observed enough in the world that we don't want to break it. |
| 1669 |
// |
| 1670 |
// The server is setting "Content-Encoding: gzip", |
| 1671 |
// wrapping the ResponseWriter in an implementation which gzips data written to it, |
| 1672 |
// and passing this ResponseWriter to ServeFile. |
| 1673 |
// |
| 1674 |
// This means ServeFile cannot properly set a Content-Length header, because it |
| 1675 |
// doesn't know what content it is going to send--the ResponseWriter is modifying |
| 1676 |
// the bytes sent. |
| 1677 |
// |
| 1678 |
// Range requests are always going to be broken in this scenario, |
| 1679 |
// but verify that we can serve non-range requests correctly. |
| 1680 |
filename := "index.html" |
| 1681 |
contents := []byte("contents will be sent with Content-Encoding: gzip") |
| 1682 |
fsys := fstest.MapFS{ |
| 1683 |
filename: {Data: contents}, |
| 1684 |
} |
| 1685 |
ts := newClientServerTest(t, http1Mode, HandlerFunc(func(w ResponseWriter, r *Request) { |
| 1686 |
w.Header().Set("Content-Encoding", "gzip") |
| 1687 |
gzw := gzip.NewWriter(w) |
| 1688 |
defer gzw.Close() |
| 1689 |
ServeFileFS(gzipResponseWriter{w: gzw, ResponseWriter: w}, r, fsys, filename) |
| 1690 |
})).ts |
| 1691 |
defer ts.Close() |
| 1692 |
|
| 1693 |
res, err := ts.Client().Get(ts.URL + "/" + filename) |
| 1694 |
if err != nil { |
| 1695 |
t.Fatal(err) |
| 1696 |
} |
| 1697 |
b, err := io.ReadAll(res.Body) |
| 1698 |
if err != nil { |
| 1699 |
t.Fatal("reading Body:", err) |
| 1700 |
} |
| 1701 |
if s := string(b); s != string(contents) { |
| 1702 |
t.Errorf("for path %q got %q, want %q", filename, s, contents) |
| 1703 |
} |
| 1704 |
res.Body.Close() |
| 1705 |
} |
| 1706 |
|
| 1707 |
type gzipResponseWriter struct { |
| 1708 |
ResponseWriter |
| 1709 |
w *gzip.Writer |
| 1710 |
} |
| 1711 |
|
| 1712 |
func (grw gzipResponseWriter) Write(b []byte) (int, error) { |
| 1713 |
return grw.w.Write(b) |
| 1714 |
} |
| 1715 |
|
| 1716 |
func (grw gzipResponseWriter) Flush() { |
| 1717 |
grw.w.Flush() |
| 1718 |
if fw, ok := grw.ResponseWriter.(http.Flusher); ok { |
| 1719 |
fw.Flush() |
| 1720 |
} |
| 1721 |
} |
| 1722 |
|
| 1723 |
// Issue 63769 |
| 1724 |
func TestFileServerDirWithRootFile(t *testing.T) { run(t, testFileServerDirWithRootFile) } |
| 1725 |
func testFileServerDirWithRootFile(t *testing.T, mode testMode) { |
| 1726 |
testDirFile := func(t *testing.T, h Handler) { |
| 1727 |
ts := newClientServerTest(t, mode, h).ts |
| 1728 |
defer ts.Close() |
| 1729 |
|
| 1730 |
res, err := ts.Client().Get(ts.URL) |
| 1731 |
if err != nil { |
| 1732 |
t.Fatal(err) |
| 1733 |
} |
| 1734 |
if g, w := res.StatusCode, StatusInternalServerError; g != w { |
| 1735 |
t.Errorf("StatusCode mismatch: got %d, want: %d", g, w) |
| 1736 |
} |
| 1737 |
res.Body.Close() |
| 1738 |
} |
| 1739 |
|
| 1740 |
t.Run("FileServer", func(t *testing.T) { |
| 1741 |
testDirFile(t, FileServer(Dir("testdata/index.html"))) |
| 1742 |
}) |
| 1743 |
|
| 1744 |
t.Run("FileServerFS", func(t *testing.T) { |
| 1745 |
testDirFile(t, FileServerFS(os.DirFS("testdata/index.html"))) |
| 1746 |
}) |
| 1747 |
} |
| 1748 |
|
| 1749 |
func TestServeContentHeadersWithError(t *testing.T) { |
| 1750 |
t.Run("keepheaders=0", func(t *testing.T) { |
| 1751 |
testServeContentHeadersWithError(t, false) |
| 1752 |
}) |
| 1753 |
t.Run("keepheaders=1", func(t *testing.T) { |
| 1754 |
testServeContentHeadersWithError(t, true) |
| 1755 |
}) |
| 1756 |
} |
| 1757 |
func testServeContentHeadersWithError(t *testing.T, keepHeaders bool) { |
| 1758 |
if keepHeaders { |
| 1759 |
t.Setenv("GODEBUG", "httpservecontentkeepheaders=1") |
| 1760 |
} |
| 1761 |
contents := []byte("content") |
| 1762 |
ts := newClientServerTest(t, http1Mode, HandlerFunc(func(w ResponseWriter, r *Request) { |
| 1763 |
w.Header().Set("Content-Type", "application/octet-stream") |
| 1764 |
w.Header().Set("Content-Length", strconv.Itoa(len(contents))) |
| 1765 |
w.Header().Set("Content-Encoding", "gzip") |
| 1766 |
w.Header().Set("Etag", `"abcdefgh"`) |
| 1767 |
w.Header().Set("Last-Modified", "Wed, 21 Oct 2015 07:28:00 GMT") |
| 1768 |
w.Header().Set("Cache-Control", "immutable") |
| 1769 |
w.Header().Set("Other-Header", "test") |
| 1770 |
ServeContent(w, r, "", time.Time{}, bytes.NewReader(contents)) |
| 1771 |
})).ts |
| 1772 |
defer ts.Close() |
| 1773 |
|
| 1774 |
req, err := NewRequest("GET", ts.URL, nil) |
| 1775 |
if err != nil { |
| 1776 |
t.Fatal(err) |
| 1777 |
} |
| 1778 |
req.Header.Set("Range", "bytes=100-10000") |
| 1779 |
|
| 1780 |
c := ts.Client() |
| 1781 |
res, err := c.Do(req) |
| 1782 |
if err != nil { |
| 1783 |
t.Fatal(err) |
| 1784 |
} |
| 1785 |
|
| 1786 |
out, _ := io.ReadAll(res.Body) |
| 1787 |
res.Body.Close() |
| 1788 |
|
| 1789 |
ifKept := func(s string) string { |
| 1790 |
if keepHeaders { |
| 1791 |
return s |
| 1792 |
} |
| 1793 |
return "" |
| 1794 |
} |
| 1795 |
if g, e := res.StatusCode, 416; g != e { |
| 1796 |
t.Errorf("got status = %d; want %d", g, e) |
| 1797 |
} |
| 1798 |
if g, e := string(out), "invalid range: failed to overlap\n"; g != e { |
| 1799 |
t.Errorf("got body = %q; want %q", g, e) |
| 1800 |
} |
| 1801 |
if g, e := res.Header.Get("Content-Type"), "text/plain; charset=utf-8"; g != e { |
| 1802 |
t.Errorf("got content-type = %q, want %q", g, e) |
| 1803 |
} |
| 1804 |
if g, e := res.Header.Get("Content-Length"), strconv.Itoa(len(out)); g != e { |
| 1805 |
t.Errorf("got content-length = %q, want %q", g, e) |
| 1806 |
} |
| 1807 |
if g, e := res.Header.Get("Content-Encoding"), ifKept("gzip"); g != e { |
| 1808 |
t.Errorf("got content-encoding = %q, want %q", g, e) |
| 1809 |
} |
| 1810 |
if g, e := res.Header.Get("Etag"), ifKept(`"abcdefgh"`); g != e { |
| 1811 |
t.Errorf("got etag = %q, want %q", g, e) |
| 1812 |
} |
| 1813 |
if g, e := res.Header.Get("Last-Modified"), ifKept("Wed, 21 Oct 2015 07:28:00 GMT"); g != e { |
| 1814 |
t.Errorf("got last-modified = %q, want %q", g, e) |
| 1815 |
} |
| 1816 |
if g, e := res.Header.Get("Cache-Control"), ifKept("immutable"); g != e { |
| 1817 |
t.Errorf("got cache-control = %q, want %q", g, e) |
| 1818 |
} |
| 1819 |
if g, e := res.Header.Get("Content-Range"), "bytes */7"; g != e { |
| 1820 |
t.Errorf("got content-range = %q, want %q", g, e) |
| 1821 |
} |
| 1822 |
if g, e := res.Header.Get("Other-Header"), "test"; g != e { |
| 1823 |
t.Errorf("got other-header = %q, want %q", g, e) |
| 1824 |
} |
| 1825 |
} |