Source file
src/net/http/fs_test.go
1
2
3
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
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},
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
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
97
98
99
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
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
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
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
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
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
481
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
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
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
578
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
586
587
588
589
590
591
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
605
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
615
616
617
618
619
620
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
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 TestFileServerNamesEscape(t *testing.T) { run(t, testFileServerNamesEscape) }
737 func testFileServerNamesEscape(t *testing.T, mode testMode) {
738 ts := newClientServerTest(t, mode, FileServer(Dir("testdata"))).ts
739 for _, path := range []string{
740 "/../testdata/file",
741 "/NUL",
742 } {
743 res, err := ts.Client().Get(ts.URL + path)
744 if err != nil {
745 t.Fatal(err)
746 }
747 res.Body.Close()
748 if res.StatusCode < 400 || res.StatusCode > 599 {
749 t.Errorf("Get(%q): got status %v, want 4xx or 5xx", path, res.StatusCode)
750 }
751
752 }
753 }
754
755 type fakeFileInfo struct {
756 dir bool
757 basename string
758 modtime time.Time
759 ents []*fakeFileInfo
760 contents string
761 err error
762 }
763
764 func (f *fakeFileInfo) Name() string { return f.basename }
765 func (f *fakeFileInfo) Sys() any { return nil }
766 func (f *fakeFileInfo) ModTime() time.Time { return f.modtime }
767 func (f *fakeFileInfo) IsDir() bool { return f.dir }
768 func (f *fakeFileInfo) Size() int64 { return int64(len(f.contents)) }
769 func (f *fakeFileInfo) Mode() fs.FileMode {
770 if f.dir {
771 return 0755 | fs.ModeDir
772 }
773 return 0644
774 }
775
776 func (f *fakeFileInfo) String() string {
777 return fs.FormatFileInfo(f)
778 }
779
780 type fakeFile struct {
781 io.ReadSeeker
782 fi *fakeFileInfo
783 path string
784 entpos int
785 }
786
787 func (f *fakeFile) Close() error { return nil }
788 func (f *fakeFile) Stat() (fs.FileInfo, error) { return f.fi, nil }
789 func (f *fakeFile) Readdir(count int) ([]fs.FileInfo, error) {
790 if !f.fi.dir {
791 return nil, fs.ErrInvalid
792 }
793 var fis []fs.FileInfo
794
795 limit := f.entpos + count
796 if count <= 0 || limit > len(f.fi.ents) {
797 limit = len(f.fi.ents)
798 }
799 for ; f.entpos < limit; f.entpos++ {
800 fis = append(fis, f.fi.ents[f.entpos])
801 }
802
803 if len(fis) == 0 && count > 0 {
804 return fis, io.EOF
805 } else {
806 return fis, nil
807 }
808 }
809
810 type fakeFS map[string]*fakeFileInfo
811
812 func (fsys fakeFS) Open(name string) (File, error) {
813 name = path.Clean(name)
814 f, ok := fsys[name]
815 if !ok {
816 return nil, fs.ErrNotExist
817 }
818 if f.err != nil {
819 return nil, f.err
820 }
821 return &fakeFile{ReadSeeker: strings.NewReader(f.contents), fi: f, path: name}, nil
822 }
823
824 func TestDirectoryIfNotModified(t *testing.T) { run(t, testDirectoryIfNotModified) }
825 func testDirectoryIfNotModified(t *testing.T, mode testMode) {
826 const indexContents = "I am a fake index.html file"
827 fileMod := time.Unix(1000000000, 0).UTC()
828 fileModStr := fileMod.Format(TimeFormat)
829 dirMod := time.Unix(123, 0).UTC()
830 indexFile := &fakeFileInfo{
831 basename: "index.html",
832 modtime: fileMod,
833 contents: indexContents,
834 }
835 fs := fakeFS{
836 "/": &fakeFileInfo{
837 dir: true,
838 modtime: dirMod,
839 ents: []*fakeFileInfo{indexFile},
840 },
841 "/index.html": indexFile,
842 }
843
844 ts := newClientServerTest(t, mode, FileServer(fs)).ts
845
846 res, err := ts.Client().Get(ts.URL)
847 if err != nil {
848 t.Fatal(err)
849 }
850 b, err := io.ReadAll(res.Body)
851 if err != nil {
852 t.Fatal(err)
853 }
854 if string(b) != indexContents {
855 t.Fatalf("Got body %q; want %q", b, indexContents)
856 }
857 res.Body.Close()
858
859 lastMod := res.Header.Get("Last-Modified")
860 if lastMod != fileModStr {
861 t.Fatalf("initial Last-Modified = %q; want %q", lastMod, fileModStr)
862 }
863
864 req, _ := NewRequest("GET", ts.URL, nil)
865 req.Header.Set("If-Modified-Since", lastMod)
866
867 c := ts.Client()
868 res, err = c.Do(req)
869 if err != nil {
870 t.Fatal(err)
871 }
872 if res.StatusCode != 304 {
873 t.Fatalf("Code after If-Modified-Since request = %v; want 304", res.StatusCode)
874 }
875 res.Body.Close()
876
877
878 indexFile.modtime = indexFile.modtime.Add(1 * time.Hour)
879
880 res, err = c.Do(req)
881 if err != nil {
882 t.Fatal(err)
883 }
884 if res.StatusCode != 200 {
885 t.Fatalf("Code after second If-Modified-Since request = %v; want 200; res is %#v", res.StatusCode, res)
886 }
887 res.Body.Close()
888 }
889
890 func mustStat(t *testing.T, fileName string) fs.FileInfo {
891 fi, err := os.Stat(fileName)
892 if err != nil {
893 t.Fatal(err)
894 }
895 return fi
896 }
897
898 func TestServeContent(t *testing.T) { run(t, testServeContent) }
899 func testServeContent(t *testing.T, mode testMode) {
900 type serveParam struct {
901 name string
902 modtime time.Time
903 content io.ReadSeeker
904 contentType string
905 etag string
906 }
907 servec := make(chan serveParam, 1)
908 ts := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
909 p := <-servec
910 if p.etag != "" {
911 w.Header().Set("ETag", p.etag)
912 }
913 if p.contentType != "" {
914 w.Header().Set("Content-Type", p.contentType)
915 }
916 ServeContent(w, r, p.name, p.modtime, p.content)
917 })).ts
918
919 type testCase struct {
920
921 file string
922 content io.ReadSeeker
923
924 modtime time.Time
925 serveETag string
926 serveContentType string
927 reqHeader map[string]string
928 wantLastMod string
929 wantContentType string
930 wantContentRange string
931 wantStatus int
932 }
933 htmlModTime := mustStat(t, "testdata/index.html").ModTime()
934 tests := map[string]testCase{
935 "no_last_modified": {
936 file: "testdata/style.css",
937 wantContentType: "text/css; charset=utf-8",
938 wantStatus: 200,
939 },
940 "with_last_modified": {
941 file: "testdata/index.html",
942 wantContentType: "text/html; charset=utf-8",
943 modtime: htmlModTime,
944 wantLastMod: htmlModTime.UTC().Format(TimeFormat),
945 wantStatus: 200,
946 },
947 "not_modified_modtime": {
948 file: "testdata/style.css",
949 serveETag: `"foo"`,
950 modtime: htmlModTime,
951 reqHeader: map[string]string{
952 "If-Modified-Since": htmlModTime.UTC().Format(TimeFormat),
953 },
954 wantStatus: 304,
955 },
956 "not_modified_modtime_with_contenttype": {
957 file: "testdata/style.css",
958 serveContentType: "text/css",
959 serveETag: `"foo"`,
960 modtime: htmlModTime,
961 reqHeader: map[string]string{
962 "If-Modified-Since": htmlModTime.UTC().Format(TimeFormat),
963 },
964 wantStatus: 304,
965 },
966 "not_modified_etag": {
967 file: "testdata/style.css",
968 serveETag: `"foo"`,
969 reqHeader: map[string]string{
970 "If-None-Match": `"foo"`,
971 },
972 wantStatus: 304,
973 },
974 "not_modified_etag_no_seek": {
975 content: panicOnSeek{nil},
976 serveETag: `W/"foo"`,
977 reqHeader: map[string]string{
978 "If-None-Match": `"baz", W/"foo"`,
979 },
980 wantStatus: 304,
981 },
982 "if_none_match_mismatch": {
983 file: "testdata/style.css",
984 serveETag: `"foo"`,
985 reqHeader: map[string]string{
986 "If-None-Match": `"Foo"`,
987 },
988 wantStatus: 200,
989 wantContentType: "text/css; charset=utf-8",
990 },
991 "if_none_match_malformed": {
992 file: "testdata/style.css",
993 serveETag: `"foo"`,
994 reqHeader: map[string]string{
995 "If-None-Match": `,`,
996 },
997 wantStatus: 200,
998 wantContentType: "text/css; charset=utf-8",
999 },
1000 "range_good": {
1001 file: "testdata/style.css",
1002 serveETag: `"A"`,
1003 reqHeader: map[string]string{
1004 "Range": "bytes=0-4",
1005 },
1006 wantStatus: StatusPartialContent,
1007 wantContentType: "text/css; charset=utf-8",
1008 wantContentRange: "bytes 0-4/8",
1009 },
1010 "range_match": {
1011 file: "testdata/style.css",
1012 serveETag: `"A"`,
1013 reqHeader: map[string]string{
1014 "Range": "bytes=0-4",
1015 "If-Range": `"A"`,
1016 },
1017 wantStatus: StatusPartialContent,
1018 wantContentType: "text/css; charset=utf-8",
1019 wantContentRange: "bytes 0-4/8",
1020 },
1021 "range_match_weak_etag": {
1022 file: "testdata/style.css",
1023 serveETag: `W/"A"`,
1024 reqHeader: map[string]string{
1025 "Range": "bytes=0-4",
1026 "If-Range": `W/"A"`,
1027 },
1028 wantStatus: 200,
1029 wantContentType: "text/css; charset=utf-8",
1030 },
1031 "range_no_overlap": {
1032 file: "testdata/style.css",
1033 serveETag: `"A"`,
1034 reqHeader: map[string]string{
1035 "Range": "bytes=10-20",
1036 },
1037 wantStatus: StatusRequestedRangeNotSatisfiable,
1038 wantContentType: "text/plain; charset=utf-8",
1039 wantContentRange: "bytes */8",
1040 },
1041
1042
1043 "range_no_match": {
1044 file: "testdata/style.css",
1045 serveETag: `"A"`,
1046 reqHeader: map[string]string{
1047 "Range": "bytes=0-4",
1048 "If-Range": `"B"`,
1049 },
1050 wantStatus: 200,
1051 wantContentType: "text/css; charset=utf-8",
1052 },
1053 "range_with_modtime": {
1054 file: "testdata/style.css",
1055 modtime: time.Date(2014, 6, 25, 17, 12, 18, 0 , time.UTC),
1056 reqHeader: map[string]string{
1057 "Range": "bytes=0-4",
1058 "If-Range": "Wed, 25 Jun 2014 17:12:18 GMT",
1059 },
1060 wantStatus: StatusPartialContent,
1061 wantContentType: "text/css; charset=utf-8",
1062 wantContentRange: "bytes 0-4/8",
1063 wantLastMod: "Wed, 25 Jun 2014 17:12:18 GMT",
1064 },
1065 "range_with_modtime_mismatch": {
1066 file: "testdata/style.css",
1067 modtime: time.Date(2014, 6, 25, 17, 12, 18, 0 , time.UTC),
1068 reqHeader: map[string]string{
1069 "Range": "bytes=0-4",
1070 "If-Range": "Wed, 25 Jun 2014 17:12:19 GMT",
1071 },
1072 wantStatus: StatusOK,
1073 wantContentType: "text/css; charset=utf-8",
1074 wantLastMod: "Wed, 25 Jun 2014 17:12:18 GMT",
1075 },
1076 "range_with_modtime_nanos": {
1077 file: "testdata/style.css",
1078 modtime: time.Date(2014, 6, 25, 17, 12, 18, 123 , time.UTC),
1079 reqHeader: map[string]string{
1080 "Range": "bytes=0-4",
1081 "If-Range": "Wed, 25 Jun 2014 17:12:18 GMT",
1082 },
1083 wantStatus: StatusPartialContent,
1084 wantContentType: "text/css; charset=utf-8",
1085 wantContentRange: "bytes 0-4/8",
1086 wantLastMod: "Wed, 25 Jun 2014 17:12:18 GMT",
1087 },
1088 "unix_zero_modtime": {
1089 content: strings.NewReader("<html>foo"),
1090 modtime: time.Unix(0, 0),
1091 wantStatus: StatusOK,
1092 wantContentType: "text/html; charset=utf-8",
1093 },
1094 "ifmatch_matches": {
1095 file: "testdata/style.css",
1096 serveETag: `"A"`,
1097 reqHeader: map[string]string{
1098 "If-Match": `"Z", "A"`,
1099 },
1100 wantStatus: 200,
1101 wantContentType: "text/css; charset=utf-8",
1102 },
1103 "ifmatch_star": {
1104 file: "testdata/style.css",
1105 serveETag: `"A"`,
1106 reqHeader: map[string]string{
1107 "If-Match": `*`,
1108 },
1109 wantStatus: 200,
1110 wantContentType: "text/css; charset=utf-8",
1111 },
1112 "ifmatch_failed": {
1113 file: "testdata/style.css",
1114 serveETag: `"A"`,
1115 reqHeader: map[string]string{
1116 "If-Match": `"B"`,
1117 },
1118 wantStatus: 412,
1119 },
1120 "ifmatch_fails_on_weak_etag": {
1121 file: "testdata/style.css",
1122 serveETag: `W/"A"`,
1123 reqHeader: map[string]string{
1124 "If-Match": `W/"A"`,
1125 },
1126 wantStatus: 412,
1127 },
1128 "if_unmodified_since_true": {
1129 file: "testdata/style.css",
1130 modtime: htmlModTime,
1131 reqHeader: map[string]string{
1132 "If-Unmodified-Since": htmlModTime.UTC().Format(TimeFormat),
1133 },
1134 wantStatus: 200,
1135 wantContentType: "text/css; charset=utf-8",
1136 wantLastMod: htmlModTime.UTC().Format(TimeFormat),
1137 },
1138 "if_unmodified_since_false": {
1139 file: "testdata/style.css",
1140 modtime: htmlModTime,
1141 reqHeader: map[string]string{
1142 "If-Unmodified-Since": htmlModTime.Add(-2 * time.Second).UTC().Format(TimeFormat),
1143 },
1144 wantStatus: 412,
1145 wantLastMod: htmlModTime.UTC().Format(TimeFormat),
1146 },
1147 }
1148 for testName, tt := range tests {
1149 var content io.ReadSeeker
1150 if tt.file != "" {
1151 f, err := os.Open(tt.file)
1152 if err != nil {
1153 t.Fatalf("test %q: %v", testName, err)
1154 }
1155 defer f.Close()
1156 content = f
1157 } else {
1158 content = tt.content
1159 }
1160 for _, method := range []string{"GET", "HEAD"} {
1161
1162 if content, ok := content.(*strings.Reader); ok {
1163 content.Seek(0, io.SeekStart)
1164 }
1165
1166 servec <- serveParam{
1167 name: filepath.Base(tt.file),
1168 content: content,
1169 modtime: tt.modtime,
1170 etag: tt.serveETag,
1171 contentType: tt.serveContentType,
1172 }
1173 req, err := NewRequest(method, ts.URL, nil)
1174 if err != nil {
1175 t.Fatal(err)
1176 }
1177 for k, v := range tt.reqHeader {
1178 req.Header.Set(k, v)
1179 }
1180
1181 c := ts.Client()
1182 res, err := c.Do(req)
1183 if err != nil {
1184 t.Fatal(err)
1185 }
1186 io.Copy(io.Discard, res.Body)
1187 res.Body.Close()
1188 if res.StatusCode != tt.wantStatus {
1189 t.Errorf("test %q using %q: got status = %d; want %d", testName, method, res.StatusCode, tt.wantStatus)
1190 }
1191 if g, e := res.Header.Get("Content-Type"), tt.wantContentType; g != e {
1192 t.Errorf("test %q using %q: got content-type = %q, want %q", testName, method, g, e)
1193 }
1194 if g, e := res.Header.Get("Content-Range"), tt.wantContentRange; g != e {
1195 t.Errorf("test %q using %q: got content-range = %q, want %q", testName, method, g, e)
1196 }
1197 if g, e := res.Header.Get("Last-Modified"), tt.wantLastMod; g != e {
1198 t.Errorf("test %q using %q: got last-modified = %q, want %q", testName, method, g, e)
1199 }
1200 }
1201 }
1202 }
1203
1204
1205 func TestServerFileStatError(t *testing.T) {
1206 rec := httptest.NewRecorder()
1207 r, _ := NewRequest("GET", "http://foo/", nil)
1208 redirect := false
1209 name := "file.txt"
1210 fs := issue12991FS{}
1211 ExportServeFile(rec, r, fs, name, redirect)
1212 if body := rec.Body.String(); !strings.Contains(body, "403") || !strings.Contains(body, "Forbidden") {
1213 t.Errorf("wanted 403 forbidden message; got: %s", body)
1214 }
1215 }
1216
1217 type issue12991FS struct{}
1218
1219 func (issue12991FS) Open(string) (File, error) { return issue12991File{}, nil }
1220
1221 type issue12991File struct{ File }
1222
1223 func (issue12991File) Stat() (fs.FileInfo, error) { return nil, fs.ErrPermission }
1224 func (issue12991File) Close() error { return nil }
1225
1226 func TestFileServerErrorMessages(t *testing.T) {
1227 run(t, func(t *testing.T, mode testMode) {
1228 t.Run("keepheaders=0", func(t *testing.T) {
1229 testFileServerErrorMessages(t, mode, false)
1230 })
1231 t.Run("keepheaders=1", func(t *testing.T) {
1232 testFileServerErrorMessages(t, mode, true)
1233 })
1234 }, testNotParallel)
1235 }
1236 func testFileServerErrorMessages(t *testing.T, mode testMode, keepHeaders bool) {
1237 if keepHeaders {
1238 t.Setenv("GODEBUG", "httpservecontentkeepheaders=1")
1239 }
1240 fs := fakeFS{
1241 "/500": &fakeFileInfo{
1242 err: errors.New("random error"),
1243 },
1244 "/403": &fakeFileInfo{
1245 err: &fs.PathError{Err: fs.ErrPermission},
1246 },
1247 }
1248 server := FileServer(fs)
1249 h := func(w http.ResponseWriter, r *http.Request) {
1250 w.Header().Set("Etag", "étude")
1251 w.Header().Set("Cache-Control", "yes")
1252 w.Header().Set("Content-Type", "awesome")
1253 w.Header().Set("Last-Modified", "yesterday")
1254 server.ServeHTTP(w, r)
1255 }
1256 ts := newClientServerTest(t, mode, http.HandlerFunc(h)).ts
1257 c := ts.Client()
1258 for _, code := range []int{403, 404, 500} {
1259 res, err := c.Get(fmt.Sprintf("%s/%d", ts.URL, code))
1260 if err != nil {
1261 t.Errorf("Error fetching /%d: %v", code, err)
1262 continue
1263 }
1264 res.Body.Close()
1265 if res.StatusCode != code {
1266 t.Errorf("GET /%d: StatusCode = %d; want %d", code, res.StatusCode, code)
1267 }
1268 for _, hdr := range []string{"Etag", "Last-Modified", "Cache-Control"} {
1269 if v, got := res.Header[hdr]; got != keepHeaders {
1270 want := "not present"
1271 if keepHeaders {
1272 want = "present"
1273 }
1274 t.Errorf("GET /%d: Header[%q] = %q, want %v", code, hdr, v, want)
1275 }
1276 }
1277 }
1278 }
1279
1280
1281 func TestLinuxSendfile(t *testing.T) {
1282 setParallel(t)
1283 defer afterTest(t)
1284 if runtime.GOOS != "linux" {
1285 t.Skip("skipping; linux-only test")
1286 }
1287 if _, err := exec.LookPath("strace"); err != nil {
1288 t.Skip("skipping; strace not found in path")
1289 }
1290
1291 ln, err := net.Listen("tcp", "127.0.0.1:0")
1292 if err != nil {
1293 t.Fatal(err)
1294 }
1295 lnf, err := ln.(*net.TCPListener).File()
1296 if err != nil {
1297 t.Fatal(err)
1298 }
1299 defer ln.Close()
1300
1301
1302 if err := testenv.Command(t, "strace", "-f", "-q", os.Args[0], "-test.run=^$").Run(); err != nil {
1303 t.Skipf("skipping; failed to run strace: %v", err)
1304 }
1305
1306 filename := fmt.Sprintf("1kb-%d", os.Getpid())
1307 filepath := path.Join(os.TempDir(), filename)
1308
1309 if err := os.WriteFile(filepath, bytes.Repeat([]byte{'a'}, 1<<10), 0755); err != nil {
1310 t.Fatal(err)
1311 }
1312 defer os.Remove(filepath)
1313
1314 var buf strings.Builder
1315 child := testenv.Command(t, "strace", "-f", "-q", os.Args[0], "-test.run=^TestLinuxSendfileChild$")
1316 child.ExtraFiles = append(child.ExtraFiles, lnf)
1317 child.Env = append([]string{"GO_WANT_HELPER_PROCESS=1"}, os.Environ()...)
1318 child.Stdout = &buf
1319 child.Stderr = &buf
1320 if err := child.Start(); err != nil {
1321 t.Skipf("skipping; failed to start straced child: %v", err)
1322 }
1323
1324 res, err := Get(fmt.Sprintf("http://%s/%s", ln.Addr(), filename))
1325 if err != nil {
1326 t.Fatalf("http client error: %v", err)
1327 }
1328 _, err = io.Copy(io.Discard, res.Body)
1329 if err != nil {
1330 t.Fatalf("client body read error: %v", err)
1331 }
1332 res.Body.Close()
1333
1334
1335 Post(fmt.Sprintf("http://%s/quit", ln.Addr()), "", nil)
1336 child.Wait()
1337
1338 rx := regexp.MustCompile(`\b(n64:)?sendfile(64)?\(`)
1339 out := buf.String()
1340 if !rx.MatchString(out) {
1341 t.Errorf("no sendfile system call found in:\n%s", out)
1342 }
1343 }
1344
1345 func getBody(t *testing.T, testName string, req Request, client *Client) (*Response, []byte) {
1346 r, err := client.Do(&req)
1347 if err != nil {
1348 t.Fatalf("%s: for URL %q, send error: %v", testName, req.URL.String(), err)
1349 }
1350 b, err := io.ReadAll(r.Body)
1351 if err != nil {
1352 t.Fatalf("%s: for URL %q, reading body: %v", testName, req.URL.String(), err)
1353 }
1354 return r, b
1355 }
1356
1357
1358
1359 func TestLinuxSendfileChild(*testing.T) {
1360 if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
1361 return
1362 }
1363 defer os.Exit(0)
1364 fd3 := os.NewFile(3, "ephemeral-port-listener")
1365 ln, err := net.FileListener(fd3)
1366 if err != nil {
1367 panic(err)
1368 }
1369 mux := NewServeMux()
1370 mux.Handle("/", FileServer(Dir(os.TempDir())))
1371 mux.HandleFunc("/quit", func(ResponseWriter, *Request) {
1372 os.Exit(0)
1373 })
1374 s := &Server{Handler: mux}
1375 err = s.Serve(ln)
1376 if err != nil {
1377 panic(err)
1378 }
1379 }
1380
1381
1382 func TestFileServerNotDirError(t *testing.T) {
1383 run(t, func(t *testing.T, mode testMode) {
1384 t.Run("Dir", func(t *testing.T) {
1385 testFileServerNotDirError(t, mode, func(path string) FileSystem { return Dir(path) })
1386 })
1387 t.Run("FS", func(t *testing.T) {
1388 testFileServerNotDirError(t, mode, func(path string) FileSystem { return FS(os.DirFS(path)) })
1389 })
1390 })
1391 }
1392
1393 func testFileServerNotDirError(t *testing.T, mode testMode, newfs func(string) FileSystem) {
1394 ts := newClientServerTest(t, mode, FileServer(newfs("testdata"))).ts
1395
1396 res, err := ts.Client().Get(ts.URL + "/index.html/not-a-file")
1397 if err != nil {
1398 t.Fatal(err)
1399 }
1400 res.Body.Close()
1401 if res.StatusCode != 404 {
1402 t.Errorf("StatusCode = %v; want 404", res.StatusCode)
1403 }
1404
1405 test := func(name string, fsys FileSystem) {
1406 t.Run(name, func(t *testing.T) {
1407 _, err = fsys.Open("/index.html/not-a-file")
1408 if err == nil {
1409 t.Fatal("err == nil; want != nil")
1410 }
1411 if !errors.Is(err, fs.ErrNotExist) {
1412 t.Errorf("err = %v; errors.Is(err, fs.ErrNotExist) = %v; want true", err,
1413 errors.Is(err, fs.ErrNotExist))
1414 }
1415
1416 _, err = fsys.Open("/index.html/not-a-dir/not-a-file")
1417 if err == nil {
1418 t.Fatal("err == nil; want != nil")
1419 }
1420 if !errors.Is(err, fs.ErrNotExist) {
1421 t.Errorf("err = %v; errors.Is(err, fs.ErrNotExist) = %v; want true", err,
1422 errors.Is(err, fs.ErrNotExist))
1423 }
1424 })
1425 }
1426
1427 absPath, err := filepath.Abs("testdata")
1428 if err != nil {
1429 t.Fatal("get abs path:", err)
1430 }
1431
1432 test("RelativePath", newfs("testdata"))
1433 test("AbsolutePath", newfs(absPath))
1434 }
1435
1436 func TestFileServerCleanPath(t *testing.T) {
1437 tests := []struct {
1438 path string
1439 wantCode int
1440 wantOpen []string
1441 }{
1442 {"/", 200, []string{"/", "/index.html"}},
1443 {"/dir", 301, []string{"/dir"}},
1444 {"/dir/", 200, []string{"/dir", "/dir/index.html"}},
1445 }
1446 for _, tt := range tests {
1447 var log []string
1448 rr := httptest.NewRecorder()
1449 req, _ := NewRequest("GET", "http://foo.localhost"+tt.path, nil)
1450 FileServer(fileServerCleanPathDir{&log}).ServeHTTP(rr, req)
1451 if !slices.Equal(log, tt.wantOpen) {
1452 t.Logf("For %s: Opens = %q; want %q", tt.path, log, tt.wantOpen)
1453 }
1454 if rr.Code != tt.wantCode {
1455 t.Logf("For %s: Response code = %d; want %d", tt.path, rr.Code, tt.wantCode)
1456 }
1457 }
1458 }
1459
1460 type fileServerCleanPathDir struct {
1461 log *[]string
1462 }
1463
1464 func (d fileServerCleanPathDir) Open(path string) (File, error) {
1465 *(d.log) = append(*(d.log), path)
1466 if path == "/" || path == "/dir" || path == "/dir/" {
1467
1468 return Dir(".").Open(".")
1469 }
1470 return nil, fs.ErrNotExist
1471 }
1472
1473 type panicOnSeek struct{ io.ReadSeeker }
1474
1475 func TestScanETag(t *testing.T) {
1476 tests := []struct {
1477 in string
1478 wantETag string
1479 wantRemain string
1480 }{
1481 {`W/"etag-1"`, `W/"etag-1"`, ""},
1482 {`"etag-2"`, `"etag-2"`, ""},
1483 {`"etag-1", "etag-2"`, `"etag-1"`, `, "etag-2"`},
1484 {"", "", ""},
1485 {"W/", "", ""},
1486 {`W/"truc`, "", ""},
1487 {`w/"case-sensitive"`, "", ""},
1488 {`"spaced etag"`, "", ""},
1489 }
1490 for _, test := range tests {
1491 etag, remain := ExportScanETag(test.in)
1492 if etag != test.wantETag || remain != test.wantRemain {
1493 t.Errorf("scanETag(%q)=%q %q, want %q %q", test.in, etag, remain, test.wantETag, test.wantRemain)
1494 }
1495 }
1496 }
1497
1498
1499
1500 func TestServeFileRejectsInvalidSuffixLengths(t *testing.T) {
1501 run(t, testServeFileRejectsInvalidSuffixLengths, []testMode{http1Mode, https1Mode, http2Mode})
1502 }
1503 func testServeFileRejectsInvalidSuffixLengths(t *testing.T, mode testMode) {
1504 cst := newClientServerTest(t, mode, FileServer(Dir("testdata"))).ts
1505
1506 tests := []struct {
1507 r string
1508 wantCode int
1509 wantBody string
1510 }{
1511 {"bytes=--6", 416, "invalid range\n"},
1512 {"bytes=--0", 416, "invalid range\n"},
1513 {"bytes=---0", 416, "invalid range\n"},
1514 {"bytes=-6", 206, "hello\n"},
1515 {"bytes=6-", 206, "html says hello\n"},
1516 {"bytes=-6-", 416, "invalid range\n"},
1517 {"bytes=-0", 206, ""},
1518 {"bytes=", 200, "index.html says hello\n"},
1519 }
1520
1521 for _, tt := range tests {
1522 tt := tt
1523 t.Run(tt.r, func(t *testing.T) {
1524 req, err := NewRequest("GET", cst.URL+"/index.html", nil)
1525 if err != nil {
1526 t.Fatal(err)
1527 }
1528 req.Header.Set("Range", tt.r)
1529 res, err := cst.Client().Do(req)
1530 if err != nil {
1531 t.Fatal(err)
1532 }
1533 if g, w := res.StatusCode, tt.wantCode; g != w {
1534 t.Errorf("StatusCode mismatch: got %d want %d", g, w)
1535 }
1536 slurp, err := io.ReadAll(res.Body)
1537 res.Body.Close()
1538 if err != nil {
1539 t.Fatal(err)
1540 }
1541 if g, w := string(slurp), tt.wantBody; g != w {
1542 t.Fatalf("Content mismatch:\nGot: %q\nWant: %q", g, w)
1543 }
1544 })
1545 }
1546 }
1547
1548 func TestFileServerMethods(t *testing.T) {
1549 run(t, testFileServerMethods)
1550 }
1551 func testFileServerMethods(t *testing.T, mode testMode) {
1552 ts := newClientServerTest(t, mode, FileServer(Dir("testdata"))).ts
1553
1554 file, err := os.ReadFile(testFile)
1555 if err != nil {
1556 t.Fatal("reading file:", err)
1557 }
1558
1559
1560
1561
1562
1563 for _, method := range []string{
1564 MethodGet,
1565 MethodHead,
1566 MethodPost,
1567 MethodPut,
1568 MethodPatch,
1569 MethodDelete,
1570 MethodOptions,
1571 MethodTrace,
1572 } {
1573 req, _ := NewRequest(method, ts.URL+"/file", nil)
1574 t.Log(req.URL)
1575 res, err := ts.Client().Do(req)
1576 if err != nil {
1577 t.Fatal(err)
1578 }
1579 body, err := io.ReadAll(res.Body)
1580 res.Body.Close()
1581 if err != nil {
1582 t.Fatal(err)
1583 }
1584 wantBody := file
1585 if method == MethodHead {
1586 wantBody = nil
1587 }
1588 if !bytes.Equal(body, wantBody) {
1589 t.Fatalf("%v: got body %q, want %q", method, body, wantBody)
1590 }
1591 if got, want := res.Header.Get("Content-Length"), fmt.Sprint(len(file)); got != want {
1592 t.Fatalf("%v: got Content-Length %q, want %q", method, got, want)
1593 }
1594 }
1595 }
1596
1597 func TestFileServerFS(t *testing.T) {
1598 filename := "index.html"
1599 contents := []byte("index.html says hello")
1600 fsys := fstest.MapFS{
1601 filename: {Data: contents},
1602 }
1603 ts := newClientServerTest(t, http1Mode, FileServerFS(fsys)).ts
1604 defer ts.Close()
1605
1606 res, err := ts.Client().Get(ts.URL + "/" + filename)
1607 if err != nil {
1608 t.Fatal(err)
1609 }
1610 b, err := io.ReadAll(res.Body)
1611 if err != nil {
1612 t.Fatal("reading Body:", err)
1613 }
1614 if s := string(b); s != string(contents) {
1615 t.Errorf("for path %q got %q, want %q", filename, s, contents)
1616 }
1617 res.Body.Close()
1618 }
1619
1620 func TestServeFileFS(t *testing.T) {
1621 filename := "index.html"
1622 contents := []byte("index.html says hello")
1623 fsys := fstest.MapFS{
1624 filename: {Data: contents},
1625 }
1626 ts := newClientServerTest(t, http1Mode, HandlerFunc(func(w ResponseWriter, r *Request) {
1627 ServeFileFS(w, r, fsys, filename)
1628 })).ts
1629 defer ts.Close()
1630
1631 res, err := ts.Client().Get(ts.URL + "/" + filename)
1632 if err != nil {
1633 t.Fatal(err)
1634 }
1635 b, err := io.ReadAll(res.Body)
1636 if err != nil {
1637 t.Fatal("reading Body:", err)
1638 }
1639 if s := string(b); s != string(contents) {
1640 t.Errorf("for path %q got %q, want %q", filename, s, contents)
1641 }
1642 res.Body.Close()
1643 }
1644
1645 func TestServeFileZippingResponseWriter(t *testing.T) {
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659 filename := "index.html"
1660 contents := []byte("contents will be sent with Content-Encoding: gzip")
1661 fsys := fstest.MapFS{
1662 filename: {Data: contents},
1663 }
1664 ts := newClientServerTest(t, http1Mode, HandlerFunc(func(w ResponseWriter, r *Request) {
1665 w.Header().Set("Content-Encoding", "gzip")
1666 gzw := gzip.NewWriter(w)
1667 defer gzw.Close()
1668 ServeFileFS(gzipResponseWriter{w: gzw, ResponseWriter: w}, r, fsys, filename)
1669 })).ts
1670 defer ts.Close()
1671
1672 res, err := ts.Client().Get(ts.URL + "/" + filename)
1673 if err != nil {
1674 t.Fatal(err)
1675 }
1676 b, err := io.ReadAll(res.Body)
1677 if err != nil {
1678 t.Fatal("reading Body:", err)
1679 }
1680 if s := string(b); s != string(contents) {
1681 t.Errorf("for path %q got %q, want %q", filename, s, contents)
1682 }
1683 res.Body.Close()
1684 }
1685
1686 type gzipResponseWriter struct {
1687 ResponseWriter
1688 w *gzip.Writer
1689 }
1690
1691 func (grw gzipResponseWriter) Write(b []byte) (int, error) {
1692 return grw.w.Write(b)
1693 }
1694
1695 func (grw gzipResponseWriter) Flush() {
1696 grw.w.Flush()
1697 if fw, ok := grw.ResponseWriter.(http.Flusher); ok {
1698 fw.Flush()
1699 }
1700 }
1701
1702
1703 func TestFileServerDirWithRootFile(t *testing.T) { run(t, testFileServerDirWithRootFile) }
1704 func testFileServerDirWithRootFile(t *testing.T, mode testMode) {
1705 testDirFile := func(t *testing.T, h Handler) {
1706 ts := newClientServerTest(t, mode, h).ts
1707 defer ts.Close()
1708
1709 res, err := ts.Client().Get(ts.URL)
1710 if err != nil {
1711 t.Fatal(err)
1712 }
1713 if g, w := res.StatusCode, StatusInternalServerError; g != w {
1714 t.Errorf("StatusCode mismatch: got %d, want: %d", g, w)
1715 }
1716 res.Body.Close()
1717 }
1718
1719 t.Run("FileServer", func(t *testing.T) {
1720 testDirFile(t, FileServer(Dir("testdata/index.html")))
1721 })
1722
1723 t.Run("FileServerFS", func(t *testing.T) {
1724 testDirFile(t, FileServerFS(os.DirFS("testdata/index.html")))
1725 })
1726 }
1727
1728 func TestServeContentHeadersWithError(t *testing.T) {
1729 t.Run("keepheaders=0", func(t *testing.T) {
1730 testServeContentHeadersWithError(t, false)
1731 })
1732 t.Run("keepheaders=1", func(t *testing.T) {
1733 testServeContentHeadersWithError(t, true)
1734 })
1735 }
1736 func testServeContentHeadersWithError(t *testing.T, keepHeaders bool) {
1737 if keepHeaders {
1738 t.Setenv("GODEBUG", "httpservecontentkeepheaders=1")
1739 }
1740 contents := []byte("content")
1741 ts := newClientServerTest(t, http1Mode, HandlerFunc(func(w ResponseWriter, r *Request) {
1742 w.Header().Set("Content-Type", "application/octet-stream")
1743 w.Header().Set("Content-Length", strconv.Itoa(len(contents)))
1744 w.Header().Set("Content-Encoding", "gzip")
1745 w.Header().Set("Etag", `"abcdefgh"`)
1746 w.Header().Set("Last-Modified", "Wed, 21 Oct 2015 07:28:00 GMT")
1747 w.Header().Set("Cache-Control", "immutable")
1748 w.Header().Set("Other-Header", "test")
1749 ServeContent(w, r, "", time.Time{}, bytes.NewReader(contents))
1750 })).ts
1751 defer ts.Close()
1752
1753 req, err := NewRequest("GET", ts.URL, nil)
1754 if err != nil {
1755 t.Fatal(err)
1756 }
1757 req.Header.Set("Range", "bytes=100-10000")
1758
1759 c := ts.Client()
1760 res, err := c.Do(req)
1761 if err != nil {
1762 t.Fatal(err)
1763 }
1764
1765 out, _ := io.ReadAll(res.Body)
1766 res.Body.Close()
1767
1768 ifKept := func(s string) string {
1769 if keepHeaders {
1770 return s
1771 }
1772 return ""
1773 }
1774 if g, e := res.StatusCode, 416; g != e {
1775 t.Errorf("got status = %d; want %d", g, e)
1776 }
1777 if g, e := string(out), "invalid range: failed to overlap\n"; g != e {
1778 t.Errorf("got body = %q; want %q", g, e)
1779 }
1780 if g, e := res.Header.Get("Content-Type"), "text/plain; charset=utf-8"; g != e {
1781 t.Errorf("got content-type = %q, want %q", g, e)
1782 }
1783 if g, e := res.Header.Get("Content-Length"), strconv.Itoa(len(out)); g != e {
1784 t.Errorf("got content-length = %q, want %q", g, e)
1785 }
1786 if g, e := res.Header.Get("Content-Encoding"), ifKept("gzip"); g != e {
1787 t.Errorf("got content-encoding = %q, want %q", g, e)
1788 }
1789 if g, e := res.Header.Get("Etag"), ifKept(`"abcdefgh"`); g != e {
1790 t.Errorf("got etag = %q, want %q", g, e)
1791 }
1792 if g, e := res.Header.Get("Last-Modified"), ifKept("Wed, 21 Oct 2015 07:28:00 GMT"); g != e {
1793 t.Errorf("got last-modified = %q, want %q", g, e)
1794 }
1795 if g, e := res.Header.Get("Cache-Control"), ifKept("immutable"); g != e {
1796 t.Errorf("got cache-control = %q, want %q", g, e)
1797 }
1798 if g, e := res.Header.Get("Content-Range"), "bytes */7"; g != e {
1799 t.Errorf("got content-range = %q, want %q", g, e)
1800 }
1801 if g, e := res.Header.Get("Other-Header"), "test"; g != e {
1802 t.Errorf("got other-header = %q, want %q", g, e)
1803 }
1804 }
1805
View as plain text