golang.org/toolchain@v0.0.1-go1.9rc2.windows-amd64/src/net/http/serve_test.go (about) 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 // End-to-end serving tests 6 7 package http_test 8 9 import ( 10 "bufio" 11 "bytes" 12 "compress/gzip" 13 "context" 14 "crypto/tls" 15 "encoding/json" 16 "errors" 17 "fmt" 18 "internal/testenv" 19 "io" 20 "io/ioutil" 21 "log" 22 "math/rand" 23 "net" 24 . "net/http" 25 "net/http/httptest" 26 "net/http/httputil" 27 "net/http/internal" 28 "net/url" 29 "os" 30 "os/exec" 31 "reflect" 32 "runtime" 33 "runtime/debug" 34 "sort" 35 "strconv" 36 "strings" 37 "sync" 38 "sync/atomic" 39 "syscall" 40 "testing" 41 "time" 42 ) 43 44 type dummyAddr string 45 type oneConnListener struct { 46 conn net.Conn 47 } 48 49 func (l *oneConnListener) Accept() (c net.Conn, err error) { 50 c = l.conn 51 if c == nil { 52 err = io.EOF 53 return 54 } 55 err = nil 56 l.conn = nil 57 return 58 } 59 60 func (l *oneConnListener) Close() error { 61 return nil 62 } 63 64 func (l *oneConnListener) Addr() net.Addr { 65 return dummyAddr("test-address") 66 } 67 68 func (a dummyAddr) Network() string { 69 return string(a) 70 } 71 72 func (a dummyAddr) String() string { 73 return string(a) 74 } 75 76 type noopConn struct{} 77 78 func (noopConn) LocalAddr() net.Addr { return dummyAddr("local-addr") } 79 func (noopConn) RemoteAddr() net.Addr { return dummyAddr("remote-addr") } 80 func (noopConn) SetDeadline(t time.Time) error { return nil } 81 func (noopConn) SetReadDeadline(t time.Time) error { return nil } 82 func (noopConn) SetWriteDeadline(t time.Time) error { return nil } 83 84 type rwTestConn struct { 85 io.Reader 86 io.Writer 87 noopConn 88 89 closeFunc func() error // called if non-nil 90 closec chan bool // else, if non-nil, send value to it on close 91 } 92 93 func (c *rwTestConn) Close() error { 94 if c.closeFunc != nil { 95 return c.closeFunc() 96 } 97 select { 98 case c.closec <- true: 99 default: 100 } 101 return nil 102 } 103 104 type testConn struct { 105 readMu sync.Mutex // for TestHandlerBodyClose 106 readBuf bytes.Buffer 107 writeBuf bytes.Buffer 108 closec chan bool // if non-nil, send value to it on close 109 noopConn 110 } 111 112 func (c *testConn) Read(b []byte) (int, error) { 113 c.readMu.Lock() 114 defer c.readMu.Unlock() 115 return c.readBuf.Read(b) 116 } 117 118 func (c *testConn) Write(b []byte) (int, error) { 119 return c.writeBuf.Write(b) 120 } 121 122 func (c *testConn) Close() error { 123 select { 124 case c.closec <- true: 125 default: 126 } 127 return nil 128 } 129 130 // reqBytes treats req as a request (with \n delimiters) and returns it with \r\n delimiters, 131 // ending in \r\n\r\n 132 func reqBytes(req string) []byte { 133 return []byte(strings.Replace(strings.TrimSpace(req), "\n", "\r\n", -1) + "\r\n\r\n") 134 } 135 136 type handlerTest struct { 137 handler Handler 138 } 139 140 func newHandlerTest(h Handler) handlerTest { 141 return handlerTest{h} 142 } 143 144 func (ht handlerTest) rawResponse(req string) string { 145 reqb := reqBytes(req) 146 var output bytes.Buffer 147 conn := &rwTestConn{ 148 Reader: bytes.NewReader(reqb), 149 Writer: &output, 150 closec: make(chan bool, 1), 151 } 152 ln := &oneConnListener{conn: conn} 153 go Serve(ln, ht.handler) 154 <-conn.closec 155 return output.String() 156 } 157 158 func TestConsumingBodyOnNextConn(t *testing.T) { 159 t.Parallel() 160 defer afterTest(t) 161 conn := new(testConn) 162 for i := 0; i < 2; i++ { 163 conn.readBuf.Write([]byte( 164 "POST / HTTP/1.1\r\n" + 165 "Host: test\r\n" + 166 "Content-Length: 11\r\n" + 167 "\r\n" + 168 "foo=1&bar=1")) 169 } 170 171 reqNum := 0 172 ch := make(chan *Request) 173 servech := make(chan error) 174 listener := &oneConnListener{conn} 175 handler := func(res ResponseWriter, req *Request) { 176 reqNum++ 177 ch <- req 178 } 179 180 go func() { 181 servech <- Serve(listener, HandlerFunc(handler)) 182 }() 183 184 var req *Request 185 req = <-ch 186 if req == nil { 187 t.Fatal("Got nil first request.") 188 } 189 if req.Method != "POST" { 190 t.Errorf("For request #1's method, got %q; expected %q", 191 req.Method, "POST") 192 } 193 194 req = <-ch 195 if req == nil { 196 t.Fatal("Got nil first request.") 197 } 198 if req.Method != "POST" { 199 t.Errorf("For request #2's method, got %q; expected %q", 200 req.Method, "POST") 201 } 202 203 if serveerr := <-servech; serveerr != io.EOF { 204 t.Errorf("Serve returned %q; expected EOF", serveerr) 205 } 206 } 207 208 type stringHandler string 209 210 func (s stringHandler) ServeHTTP(w ResponseWriter, r *Request) { 211 w.Header().Set("Result", string(s)) 212 } 213 214 var handlers = []struct { 215 pattern string 216 msg string 217 }{ 218 {"/", "Default"}, 219 {"/someDir/", "someDir"}, 220 {"/#/", "hash"}, 221 {"someHost.com/someDir/", "someHost.com/someDir"}, 222 } 223 224 var vtests = []struct { 225 url string 226 expected string 227 }{ 228 {"http://localhost/someDir/apage", "someDir"}, 229 {"http://localhost/%23/apage", "hash"}, 230 {"http://localhost/otherDir/apage", "Default"}, 231 {"http://someHost.com/someDir/apage", "someHost.com/someDir"}, 232 {"http://otherHost.com/someDir/apage", "someDir"}, 233 {"http://otherHost.com/aDir/apage", "Default"}, 234 // redirections for trees 235 {"http://localhost/someDir", "/someDir/"}, 236 {"http://localhost/%23", "/%23/"}, 237 {"http://someHost.com/someDir", "/someDir/"}, 238 } 239 240 func TestHostHandlers(t *testing.T) { 241 setParallel(t) 242 defer afterTest(t) 243 mux := NewServeMux() 244 for _, h := range handlers { 245 mux.Handle(h.pattern, stringHandler(h.msg)) 246 } 247 ts := httptest.NewServer(mux) 248 defer ts.Close() 249 250 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 251 if err != nil { 252 t.Fatal(err) 253 } 254 defer conn.Close() 255 cc := httputil.NewClientConn(conn, nil) 256 for _, vt := range vtests { 257 var r *Response 258 var req Request 259 if req.URL, err = url.Parse(vt.url); err != nil { 260 t.Errorf("cannot parse url: %v", err) 261 continue 262 } 263 if err := cc.Write(&req); err != nil { 264 t.Errorf("writing request: %v", err) 265 continue 266 } 267 r, err := cc.Read(&req) 268 if err != nil { 269 t.Errorf("reading response: %v", err) 270 continue 271 } 272 switch r.StatusCode { 273 case StatusOK: 274 s := r.Header.Get("Result") 275 if s != vt.expected { 276 t.Errorf("Get(%q) = %q, want %q", vt.url, s, vt.expected) 277 } 278 case StatusMovedPermanently: 279 s := r.Header.Get("Location") 280 if s != vt.expected { 281 t.Errorf("Get(%q) = %q, want %q", vt.url, s, vt.expected) 282 } 283 default: 284 t.Errorf("Get(%q) unhandled status code %d", vt.url, r.StatusCode) 285 } 286 } 287 } 288 289 var serveMuxRegister = []struct { 290 pattern string 291 h Handler 292 }{ 293 {"/dir/", serve(200)}, 294 {"/search", serve(201)}, 295 {"codesearch.google.com/search", serve(202)}, 296 {"codesearch.google.com/", serve(203)}, 297 {"example.com/", HandlerFunc(checkQueryStringHandler)}, 298 } 299 300 // serve returns a handler that sends a response with the given code. 301 func serve(code int) HandlerFunc { 302 return func(w ResponseWriter, r *Request) { 303 w.WriteHeader(code) 304 } 305 } 306 307 // checkQueryStringHandler checks if r.URL.RawQuery has the same value 308 // as the URL excluding the scheme and the query string and sends 200 309 // response code if it is, 500 otherwise. 310 func checkQueryStringHandler(w ResponseWriter, r *Request) { 311 u := *r.URL 312 u.Scheme = "http" 313 u.Host = r.Host 314 u.RawQuery = "" 315 if "http://"+r.URL.RawQuery == u.String() { 316 w.WriteHeader(200) 317 } else { 318 w.WriteHeader(500) 319 } 320 } 321 322 var serveMuxTests = []struct { 323 method string 324 host string 325 path string 326 code int 327 pattern string 328 }{ 329 {"GET", "google.com", "/", 404, ""}, 330 {"GET", "google.com", "/dir", 301, "/dir/"}, 331 {"GET", "google.com", "/dir/", 200, "/dir/"}, 332 {"GET", "google.com", "/dir/file", 200, "/dir/"}, 333 {"GET", "google.com", "/search", 201, "/search"}, 334 {"GET", "google.com", "/search/", 404, ""}, 335 {"GET", "google.com", "/search/foo", 404, ""}, 336 {"GET", "codesearch.google.com", "/search", 202, "codesearch.google.com/search"}, 337 {"GET", "codesearch.google.com", "/search/", 203, "codesearch.google.com/"}, 338 {"GET", "codesearch.google.com", "/search/foo", 203, "codesearch.google.com/"}, 339 {"GET", "codesearch.google.com", "/", 203, "codesearch.google.com/"}, 340 {"GET", "codesearch.google.com:443", "/", 203, "codesearch.google.com/"}, 341 {"GET", "images.google.com", "/search", 201, "/search"}, 342 {"GET", "images.google.com", "/search/", 404, ""}, 343 {"GET", "images.google.com", "/search/foo", 404, ""}, 344 {"GET", "google.com", "/../search", 301, "/search"}, 345 {"GET", "google.com", "/dir/..", 301, ""}, 346 {"GET", "google.com", "/dir/..", 301, ""}, 347 {"GET", "google.com", "/dir/./file", 301, "/dir/"}, 348 349 // The /foo -> /foo/ redirect applies to CONNECT requests 350 // but the path canonicalization does not. 351 {"CONNECT", "google.com", "/dir", 301, "/dir/"}, 352 {"CONNECT", "google.com", "/../search", 404, ""}, 353 {"CONNECT", "google.com", "/dir/..", 200, "/dir/"}, 354 {"CONNECT", "google.com", "/dir/..", 200, "/dir/"}, 355 {"CONNECT", "google.com", "/dir/./file", 200, "/dir/"}, 356 } 357 358 func TestServeMuxHandler(t *testing.T) { 359 setParallel(t) 360 mux := NewServeMux() 361 for _, e := range serveMuxRegister { 362 mux.Handle(e.pattern, e.h) 363 } 364 365 for _, tt := range serveMuxTests { 366 r := &Request{ 367 Method: tt.method, 368 Host: tt.host, 369 URL: &url.URL{ 370 Path: tt.path, 371 }, 372 } 373 h, pattern := mux.Handler(r) 374 rr := httptest.NewRecorder() 375 h.ServeHTTP(rr, r) 376 if pattern != tt.pattern || rr.Code != tt.code { 377 t.Errorf("%s %s %s = %d, %q, want %d, %q", tt.method, tt.host, tt.path, rr.Code, pattern, tt.code, tt.pattern) 378 } 379 } 380 } 381 382 var serveMuxTests2 = []struct { 383 method string 384 host string 385 url string 386 code int 387 redirOk bool 388 }{ 389 {"GET", "google.com", "/", 404, false}, 390 {"GET", "example.com", "/test/?example.com/test/", 200, false}, 391 {"GET", "example.com", "test/?example.com/test/", 200, true}, 392 } 393 394 // TestServeMuxHandlerRedirects tests that automatic redirects generated by 395 // mux.Handler() shouldn't clear the request's query string. 396 func TestServeMuxHandlerRedirects(t *testing.T) { 397 setParallel(t) 398 mux := NewServeMux() 399 for _, e := range serveMuxRegister { 400 mux.Handle(e.pattern, e.h) 401 } 402 403 for _, tt := range serveMuxTests2 { 404 tries := 1 // expect at most 1 redirection if redirOk is true. 405 turl := tt.url 406 for { 407 u, e := url.Parse(turl) 408 if e != nil { 409 t.Fatal(e) 410 } 411 r := &Request{ 412 Method: tt.method, 413 Host: tt.host, 414 URL: u, 415 } 416 h, _ := mux.Handler(r) 417 rr := httptest.NewRecorder() 418 h.ServeHTTP(rr, r) 419 if rr.Code != 301 { 420 if rr.Code != tt.code { 421 t.Errorf("%s %s %s = %d, want %d", tt.method, tt.host, tt.url, rr.Code, tt.code) 422 } 423 break 424 } 425 if !tt.redirOk { 426 t.Errorf("%s %s %s, unexpected redirect", tt.method, tt.host, tt.url) 427 break 428 } 429 turl = rr.HeaderMap.Get("Location") 430 tries-- 431 } 432 if tries < 0 { 433 t.Errorf("%s %s %s, too many redirects", tt.method, tt.host, tt.url) 434 } 435 } 436 } 437 438 // Tests for https://golang.org/issue/900 439 func TestMuxRedirectLeadingSlashes(t *testing.T) { 440 setParallel(t) 441 paths := []string{"//foo.txt", "///foo.txt", "/../../foo.txt"} 442 for _, path := range paths { 443 req, err := ReadRequest(bufio.NewReader(strings.NewReader("GET " + path + " HTTP/1.1\r\nHost: test\r\n\r\n"))) 444 if err != nil { 445 t.Errorf("%s", err) 446 } 447 mux := NewServeMux() 448 resp := httptest.NewRecorder() 449 450 mux.ServeHTTP(resp, req) 451 452 if loc, expected := resp.Header().Get("Location"), "/foo.txt"; loc != expected { 453 t.Errorf("Expected Location header set to %q; got %q", expected, loc) 454 return 455 } 456 457 if code, expected := resp.Code, StatusMovedPermanently; code != expected { 458 t.Errorf("Expected response code of StatusMovedPermanently; got %d", code) 459 return 460 } 461 } 462 } 463 464 func BenchmarkServeMux(b *testing.B) { 465 466 type test struct { 467 path string 468 code int 469 req *Request 470 } 471 472 // Build example handlers and requests 473 var tests []test 474 endpoints := []string{"search", "dir", "file", "change", "count", "s"} 475 for _, e := range endpoints { 476 for i := 200; i < 230; i++ { 477 p := fmt.Sprintf("/%s/%d/", e, i) 478 tests = append(tests, test{ 479 path: p, 480 code: i, 481 req: &Request{Method: "GET", Host: "localhost", URL: &url.URL{Path: p}}, 482 }) 483 } 484 } 485 mux := NewServeMux() 486 for _, tt := range tests { 487 mux.Handle(tt.path, serve(tt.code)) 488 } 489 490 rw := httptest.NewRecorder() 491 b.ReportAllocs() 492 b.ResetTimer() 493 for i := 0; i < b.N; i++ { 494 for _, tt := range tests { 495 *rw = httptest.ResponseRecorder{} 496 h, pattern := mux.Handler(tt.req) 497 h.ServeHTTP(rw, tt.req) 498 if pattern != tt.path || rw.Code != tt.code { 499 b.Fatalf("got %d, %q, want %d, %q", rw.Code, pattern, tt.code, tt.path) 500 } 501 } 502 } 503 } 504 505 func TestServerTimeouts(t *testing.T) { 506 setParallel(t) 507 defer afterTest(t) 508 // Try three times, with increasing timeouts. 509 tries := []time.Duration{250 * time.Millisecond, 500 * time.Millisecond, 1 * time.Second} 510 for i, timeout := range tries { 511 err := testServerTimeouts(timeout) 512 if err == nil { 513 return 514 } 515 t.Logf("failed at %v: %v", timeout, err) 516 if i != len(tries)-1 { 517 t.Logf("retrying at %v ...", tries[i+1]) 518 } 519 } 520 t.Fatal("all attempts failed") 521 } 522 523 func testServerTimeouts(timeout time.Duration) error { 524 reqNum := 0 525 ts := httptest.NewUnstartedServer(HandlerFunc(func(res ResponseWriter, req *Request) { 526 reqNum++ 527 fmt.Fprintf(res, "req=%d", reqNum) 528 })) 529 ts.Config.ReadTimeout = timeout 530 ts.Config.WriteTimeout = timeout 531 ts.Start() 532 defer ts.Close() 533 534 // Hit the HTTP server successfully. 535 c := ts.Client() 536 r, err := c.Get(ts.URL) 537 if err != nil { 538 return fmt.Errorf("http Get #1: %v", err) 539 } 540 got, err := ioutil.ReadAll(r.Body) 541 expected := "req=1" 542 if string(got) != expected || err != nil { 543 return fmt.Errorf("Unexpected response for request #1; got %q ,%v; expected %q, nil", 544 string(got), err, expected) 545 } 546 547 // Slow client that should timeout. 548 t1 := time.Now() 549 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 550 if err != nil { 551 return fmt.Errorf("Dial: %v", err) 552 } 553 buf := make([]byte, 1) 554 n, err := conn.Read(buf) 555 conn.Close() 556 latency := time.Since(t1) 557 if n != 0 || err != io.EOF { 558 return fmt.Errorf("Read = %v, %v, wanted %v, %v", n, err, 0, io.EOF) 559 } 560 minLatency := timeout / 5 * 4 561 if latency < minLatency { 562 return fmt.Errorf("got EOF after %s, want >= %s", latency, minLatency) 563 } 564 565 // Hit the HTTP server successfully again, verifying that the 566 // previous slow connection didn't run our handler. (that we 567 // get "req=2", not "req=3") 568 r, err = c.Get(ts.URL) 569 if err != nil { 570 return fmt.Errorf("http Get #2: %v", err) 571 } 572 got, err = ioutil.ReadAll(r.Body) 573 r.Body.Close() 574 expected = "req=2" 575 if string(got) != expected || err != nil { 576 return fmt.Errorf("Get #2 got %q, %v, want %q, nil", string(got), err, expected) 577 } 578 579 if !testing.Short() { 580 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 581 if err != nil { 582 return fmt.Errorf("long Dial: %v", err) 583 } 584 defer conn.Close() 585 go io.Copy(ioutil.Discard, conn) 586 for i := 0; i < 5; i++ { 587 _, err := conn.Write([]byte("GET / HTTP/1.1\r\nHost: foo\r\n\r\n")) 588 if err != nil { 589 return fmt.Errorf("on write %d: %v", i, err) 590 } 591 time.Sleep(timeout / 2) 592 } 593 } 594 return nil 595 } 596 597 // Test that the HTTP/2 server handles Server.WriteTimeout (Issue 18437) 598 func TestHTTP2WriteDeadlineExtendedOnNewRequest(t *testing.T) { 599 if testing.Short() { 600 t.Skip("skipping in short mode") 601 } 602 setParallel(t) 603 defer afterTest(t) 604 ts := httptest.NewUnstartedServer(HandlerFunc(func(res ResponseWriter, req *Request) {})) 605 ts.Config.WriteTimeout = 250 * time.Millisecond 606 ts.TLS = &tls.Config{NextProtos: []string{"h2"}} 607 ts.StartTLS() 608 defer ts.Close() 609 610 c := ts.Client() 611 if err := ExportHttp2ConfigureTransport(c.Transport.(*Transport)); err != nil { 612 t.Fatal(err) 613 } 614 615 for i := 1; i <= 3; i++ { 616 req, err := NewRequest("GET", ts.URL, nil) 617 if err != nil { 618 t.Fatal(err) 619 } 620 621 // fail test if no response after 1 second 622 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 623 defer cancel() 624 req = req.WithContext(ctx) 625 626 r, err := c.Do(req) 627 select { 628 case <-ctx.Done(): 629 if ctx.Err() == context.DeadlineExceeded { 630 t.Fatalf("http2 Get #%d response timed out", i) 631 } 632 default: 633 } 634 if err != nil { 635 t.Fatalf("http2 Get #%d: %v", i, err) 636 } 637 r.Body.Close() 638 if r.ProtoMajor != 2 { 639 t.Fatalf("http2 Get expected HTTP/2.0, got %q", r.Proto) 640 } 641 time.Sleep(ts.Config.WriteTimeout / 2) 642 } 643 } 644 645 // tryTimeouts runs testFunc with increasing timeouts. Test passes on first success, 646 // and fails if all timeouts fail. 647 func tryTimeouts(t *testing.T, testFunc func(timeout time.Duration) error) { 648 tries := []time.Duration{250 * time.Millisecond, 500 * time.Millisecond, 1 * time.Second} 649 for i, timeout := range tries { 650 err := testFunc(timeout) 651 if err == nil { 652 return 653 } 654 t.Logf("failed at %v: %v", timeout, err) 655 if i != len(tries)-1 { 656 t.Logf("retrying at %v ...", tries[i+1]) 657 } 658 } 659 t.Fatal("all attempts failed") 660 } 661 662 // Test that the HTTP/2 server RSTs stream on slow write. 663 func TestHTTP2WriteDeadlineEnforcedPerStream(t *testing.T) { 664 if testing.Short() { 665 t.Skip("skipping in short mode") 666 } 667 setParallel(t) 668 defer afterTest(t) 669 tryTimeouts(t, testHTTP2WriteDeadlineEnforcedPerStream) 670 } 671 672 func testHTTP2WriteDeadlineEnforcedPerStream(timeout time.Duration) error { 673 reqNum := 0 674 ts := httptest.NewUnstartedServer(HandlerFunc(func(res ResponseWriter, req *Request) { 675 reqNum++ 676 if reqNum == 1 { 677 return // first request succeeds 678 } 679 time.Sleep(timeout) // second request times out 680 })) 681 ts.Config.WriteTimeout = timeout / 2 682 ts.TLS = &tls.Config{NextProtos: []string{"h2"}} 683 ts.StartTLS() 684 defer ts.Close() 685 686 c := ts.Client() 687 if err := ExportHttp2ConfigureTransport(c.Transport.(*Transport)); err != nil { 688 return fmt.Errorf("ExportHttp2ConfigureTransport: %v", err) 689 } 690 691 req, err := NewRequest("GET", ts.URL, nil) 692 if err != nil { 693 return fmt.Errorf("NewRequest: %v", err) 694 } 695 r, err := c.Do(req) 696 if err != nil { 697 return fmt.Errorf("http2 Get #1: %v", err) 698 } 699 r.Body.Close() 700 if r.ProtoMajor != 2 { 701 return fmt.Errorf("http2 Get expected HTTP/2.0, got %q", r.Proto) 702 } 703 704 req, err = NewRequest("GET", ts.URL, nil) 705 if err != nil { 706 return fmt.Errorf("NewRequest: %v", err) 707 } 708 r, err = c.Do(req) 709 if err == nil { 710 r.Body.Close() 711 if r.ProtoMajor != 2 { 712 return fmt.Errorf("http2 Get expected HTTP/2.0, got %q", r.Proto) 713 } 714 return fmt.Errorf("http2 Get #2 expected error, got nil") 715 } 716 expected := "stream ID 3; INTERNAL_ERROR" // client IDs are odd, second stream should be 3 717 if !strings.Contains(err.Error(), expected) { 718 return fmt.Errorf("http2 Get #2: expected error to contain %q, got %q", expected, err) 719 } 720 return nil 721 } 722 723 // Test that the HTTP/2 server does not send RST when WriteDeadline not set. 724 func TestHTTP2NoWriteDeadline(t *testing.T) { 725 if testing.Short() { 726 t.Skip("skipping in short mode") 727 } 728 setParallel(t) 729 defer afterTest(t) 730 tryTimeouts(t, testHTTP2NoWriteDeadline) 731 } 732 733 func testHTTP2NoWriteDeadline(timeout time.Duration) error { 734 reqNum := 0 735 ts := httptest.NewUnstartedServer(HandlerFunc(func(res ResponseWriter, req *Request) { 736 reqNum++ 737 if reqNum == 1 { 738 return // first request succeeds 739 } 740 time.Sleep(timeout) // second request timesout 741 })) 742 ts.TLS = &tls.Config{NextProtos: []string{"h2"}} 743 ts.StartTLS() 744 defer ts.Close() 745 746 c := ts.Client() 747 if err := ExportHttp2ConfigureTransport(c.Transport.(*Transport)); err != nil { 748 return fmt.Errorf("ExportHttp2ConfigureTransport: %v", err) 749 } 750 751 for i := 0; i < 2; i++ { 752 req, err := NewRequest("GET", ts.URL, nil) 753 if err != nil { 754 return fmt.Errorf("NewRequest: %v", err) 755 } 756 r, err := c.Do(req) 757 if err != nil { 758 return fmt.Errorf("http2 Get #%d: %v", i, err) 759 } 760 r.Body.Close() 761 if r.ProtoMajor != 2 { 762 return fmt.Errorf("http2 Get expected HTTP/2.0, got %q", r.Proto) 763 } 764 } 765 return nil 766 } 767 768 // golang.org/issue/4741 -- setting only a write timeout that triggers 769 // shouldn't cause a handler to block forever on reads (next HTTP 770 // request) that will never happen. 771 func TestOnlyWriteTimeout(t *testing.T) { 772 setParallel(t) 773 defer afterTest(t) 774 var ( 775 mu sync.RWMutex 776 conn net.Conn 777 ) 778 var afterTimeoutErrc = make(chan error, 1) 779 ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, req *Request) { 780 buf := make([]byte, 512<<10) 781 _, err := w.Write(buf) 782 if err != nil { 783 t.Errorf("handler Write error: %v", err) 784 return 785 } 786 mu.RLock() 787 defer mu.RUnlock() 788 if conn == nil { 789 t.Error("no established connection found") 790 return 791 } 792 conn.SetWriteDeadline(time.Now().Add(-30 * time.Second)) 793 _, err = w.Write(buf) 794 afterTimeoutErrc <- err 795 })) 796 ts.Listener = trackLastConnListener{ts.Listener, &mu, &conn} 797 ts.Start() 798 defer ts.Close() 799 800 c := ts.Client() 801 802 errc := make(chan error) 803 go func() { 804 res, err := c.Get(ts.URL) 805 if err != nil { 806 errc <- err 807 return 808 } 809 _, err = io.Copy(ioutil.Discard, res.Body) 810 res.Body.Close() 811 errc <- err 812 }() 813 select { 814 case err := <-errc: 815 if err == nil { 816 t.Errorf("expected an error from Get request") 817 } 818 case <-time.After(5 * time.Second): 819 t.Fatal("timeout waiting for Get error") 820 } 821 if err := <-afterTimeoutErrc; err == nil { 822 t.Error("expected write error after timeout") 823 } 824 } 825 826 // trackLastConnListener tracks the last net.Conn that was accepted. 827 type trackLastConnListener struct { 828 net.Listener 829 830 mu *sync.RWMutex 831 last *net.Conn // destination 832 } 833 834 func (l trackLastConnListener) Accept() (c net.Conn, err error) { 835 c, err = l.Listener.Accept() 836 if err == nil { 837 l.mu.Lock() 838 *l.last = c 839 l.mu.Unlock() 840 } 841 return 842 } 843 844 // TestIdentityResponse verifies that a handler can unset 845 func TestIdentityResponse(t *testing.T) { 846 setParallel(t) 847 defer afterTest(t) 848 handler := HandlerFunc(func(rw ResponseWriter, req *Request) { 849 rw.Header().Set("Content-Length", "3") 850 rw.Header().Set("Transfer-Encoding", req.FormValue("te")) 851 switch { 852 case req.FormValue("overwrite") == "1": 853 _, err := rw.Write([]byte("foo TOO LONG")) 854 if err != ErrContentLength { 855 t.Errorf("expected ErrContentLength; got %v", err) 856 } 857 case req.FormValue("underwrite") == "1": 858 rw.Header().Set("Content-Length", "500") 859 rw.Write([]byte("too short")) 860 default: 861 rw.Write([]byte("foo")) 862 } 863 }) 864 865 ts := httptest.NewServer(handler) 866 defer ts.Close() 867 868 c := ts.Client() 869 870 // Note: this relies on the assumption (which is true) that 871 // Get sends HTTP/1.1 or greater requests. Otherwise the 872 // server wouldn't have the choice to send back chunked 873 // responses. 874 for _, te := range []string{"", "identity"} { 875 url := ts.URL + "/?te=" + te 876 res, err := c.Get(url) 877 if err != nil { 878 t.Fatalf("error with Get of %s: %v", url, err) 879 } 880 if cl, expected := res.ContentLength, int64(3); cl != expected { 881 t.Errorf("for %s expected res.ContentLength of %d; got %d", url, expected, cl) 882 } 883 if cl, expected := res.Header.Get("Content-Length"), "3"; cl != expected { 884 t.Errorf("for %s expected Content-Length header of %q; got %q", url, expected, cl) 885 } 886 if tl, expected := len(res.TransferEncoding), 0; tl != expected { 887 t.Errorf("for %s expected len(res.TransferEncoding) of %d; got %d (%v)", 888 url, expected, tl, res.TransferEncoding) 889 } 890 res.Body.Close() 891 } 892 893 // Verify that ErrContentLength is returned 894 url := ts.URL + "/?overwrite=1" 895 res, err := c.Get(url) 896 if err != nil { 897 t.Fatalf("error with Get of %s: %v", url, err) 898 } 899 res.Body.Close() 900 901 // Verify that the connection is closed when the declared Content-Length 902 // is larger than what the handler wrote. 903 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 904 if err != nil { 905 t.Fatalf("error dialing: %v", err) 906 } 907 _, err = conn.Write([]byte("GET /?underwrite=1 HTTP/1.1\r\nHost: foo\r\n\r\n")) 908 if err != nil { 909 t.Fatalf("error writing: %v", err) 910 } 911 912 // The ReadAll will hang for a failing test, so use a Timer to 913 // fail explicitly. 914 goTimeout(t, 2*time.Second, func() { 915 got, _ := ioutil.ReadAll(conn) 916 expectedSuffix := "\r\n\r\ntoo short" 917 if !strings.HasSuffix(string(got), expectedSuffix) { 918 t.Errorf("Expected output to end with %q; got response body %q", 919 expectedSuffix, string(got)) 920 } 921 }) 922 } 923 924 func testTCPConnectionCloses(t *testing.T, req string, h Handler) { 925 setParallel(t) 926 defer afterTest(t) 927 s := httptest.NewServer(h) 928 defer s.Close() 929 930 conn, err := net.Dial("tcp", s.Listener.Addr().String()) 931 if err != nil { 932 t.Fatal("dial error:", err) 933 } 934 defer conn.Close() 935 936 _, err = fmt.Fprint(conn, req) 937 if err != nil { 938 t.Fatal("print error:", err) 939 } 940 941 r := bufio.NewReader(conn) 942 res, err := ReadResponse(r, &Request{Method: "GET"}) 943 if err != nil { 944 t.Fatal("ReadResponse error:", err) 945 } 946 947 didReadAll := make(chan bool, 1) 948 go func() { 949 select { 950 case <-time.After(5 * time.Second): 951 t.Error("body not closed after 5s") 952 return 953 case <-didReadAll: 954 } 955 }() 956 957 _, err = ioutil.ReadAll(r) 958 if err != nil { 959 t.Fatal("read error:", err) 960 } 961 didReadAll <- true 962 963 if !res.Close { 964 t.Errorf("Response.Close = false; want true") 965 } 966 } 967 968 func testTCPConnectionStaysOpen(t *testing.T, req string, handler Handler) { 969 setParallel(t) 970 defer afterTest(t) 971 ts := httptest.NewServer(handler) 972 defer ts.Close() 973 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 974 if err != nil { 975 t.Fatal(err) 976 } 977 defer conn.Close() 978 br := bufio.NewReader(conn) 979 for i := 0; i < 2; i++ { 980 if _, err := io.WriteString(conn, req); err != nil { 981 t.Fatal(err) 982 } 983 res, err := ReadResponse(br, nil) 984 if err != nil { 985 t.Fatalf("res %d: %v", i+1, err) 986 } 987 if _, err := io.Copy(ioutil.Discard, res.Body); err != nil { 988 t.Fatalf("res %d body copy: %v", i+1, err) 989 } 990 res.Body.Close() 991 } 992 } 993 994 // TestServeHTTP10Close verifies that HTTP/1.0 requests won't be kept alive. 995 func TestServeHTTP10Close(t *testing.T) { 996 testTCPConnectionCloses(t, "GET / HTTP/1.0\r\n\r\n", HandlerFunc(func(w ResponseWriter, r *Request) { 997 ServeFile(w, r, "testdata/file") 998 })) 999 } 1000 1001 // TestClientCanClose verifies that clients can also force a connection to close. 1002 func TestClientCanClose(t *testing.T) { 1003 testTCPConnectionCloses(t, "GET / HTTP/1.1\r\nHost: foo\r\nConnection: close\r\n\r\n", HandlerFunc(func(w ResponseWriter, r *Request) { 1004 // Nothing. 1005 })) 1006 } 1007 1008 // TestHandlersCanSetConnectionClose verifies that handlers can force a connection to close, 1009 // even for HTTP/1.1 requests. 1010 func TestHandlersCanSetConnectionClose11(t *testing.T) { 1011 testTCPConnectionCloses(t, "GET / HTTP/1.1\r\nHost: foo\r\n\r\n\r\n", HandlerFunc(func(w ResponseWriter, r *Request) { 1012 w.Header().Set("Connection", "close") 1013 })) 1014 } 1015 1016 func TestHandlersCanSetConnectionClose10(t *testing.T) { 1017 testTCPConnectionCloses(t, "GET / HTTP/1.0\r\nConnection: keep-alive\r\n\r\n", HandlerFunc(func(w ResponseWriter, r *Request) { 1018 w.Header().Set("Connection", "close") 1019 })) 1020 } 1021 1022 func TestHTTP2UpgradeClosesConnection(t *testing.T) { 1023 testTCPConnectionCloses(t, "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", HandlerFunc(func(w ResponseWriter, r *Request) { 1024 // Nothing. (if not hijacked, the server should close the connection 1025 // afterwards) 1026 })) 1027 } 1028 1029 func send204(w ResponseWriter, r *Request) { w.WriteHeader(204) } 1030 func send304(w ResponseWriter, r *Request) { w.WriteHeader(304) } 1031 1032 // Issue 15647: 204 responses can't have bodies, so HTTP/1.0 keep-alive conns should stay open. 1033 func TestHTTP10KeepAlive204Response(t *testing.T) { 1034 testTCPConnectionStaysOpen(t, "GET / HTTP/1.0\r\nConnection: keep-alive\r\n\r\n", HandlerFunc(send204)) 1035 } 1036 1037 func TestHTTP11KeepAlive204Response(t *testing.T) { 1038 testTCPConnectionStaysOpen(t, "GET / HTTP/1.1\r\nHost: foo\r\n\r\n", HandlerFunc(send204)) 1039 } 1040 1041 func TestHTTP10KeepAlive304Response(t *testing.T) { 1042 testTCPConnectionStaysOpen(t, 1043 "GET / HTTP/1.0\r\nConnection: keep-alive\r\nIf-Modified-Since: Mon, 02 Jan 2006 15:04:05 GMT\r\n\r\n", 1044 HandlerFunc(send304)) 1045 } 1046 1047 // Issue 15703 1048 func TestKeepAliveFinalChunkWithEOF(t *testing.T) { 1049 setParallel(t) 1050 defer afterTest(t) 1051 cst := newClientServerTest(t, false /* h1 */, HandlerFunc(func(w ResponseWriter, r *Request) { 1052 w.(Flusher).Flush() // force chunked encoding 1053 w.Write([]byte("{\"Addr\": \"" + r.RemoteAddr + "\"}")) 1054 })) 1055 defer cst.close() 1056 type data struct { 1057 Addr string 1058 } 1059 var addrs [2]data 1060 for i := range addrs { 1061 res, err := cst.c.Get(cst.ts.URL) 1062 if err != nil { 1063 t.Fatal(err) 1064 } 1065 if err := json.NewDecoder(res.Body).Decode(&addrs[i]); err != nil { 1066 t.Fatal(err) 1067 } 1068 if addrs[i].Addr == "" { 1069 t.Fatal("no address") 1070 } 1071 res.Body.Close() 1072 } 1073 if addrs[0] != addrs[1] { 1074 t.Fatalf("connection not reused") 1075 } 1076 } 1077 1078 func TestSetsRemoteAddr_h1(t *testing.T) { testSetsRemoteAddr(t, h1Mode) } 1079 func TestSetsRemoteAddr_h2(t *testing.T) { testSetsRemoteAddr(t, h2Mode) } 1080 1081 func testSetsRemoteAddr(t *testing.T, h2 bool) { 1082 setParallel(t) 1083 defer afterTest(t) 1084 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 1085 fmt.Fprintf(w, "%s", r.RemoteAddr) 1086 })) 1087 defer cst.close() 1088 1089 res, err := cst.c.Get(cst.ts.URL) 1090 if err != nil { 1091 t.Fatalf("Get error: %v", err) 1092 } 1093 body, err := ioutil.ReadAll(res.Body) 1094 if err != nil { 1095 t.Fatalf("ReadAll error: %v", err) 1096 } 1097 ip := string(body) 1098 if !strings.HasPrefix(ip, "127.0.0.1:") && !strings.HasPrefix(ip, "[::1]:") { 1099 t.Fatalf("Expected local addr; got %q", ip) 1100 } 1101 } 1102 1103 type blockingRemoteAddrListener struct { 1104 net.Listener 1105 conns chan<- net.Conn 1106 } 1107 1108 func (l *blockingRemoteAddrListener) Accept() (net.Conn, error) { 1109 c, err := l.Listener.Accept() 1110 if err != nil { 1111 return nil, err 1112 } 1113 brac := &blockingRemoteAddrConn{ 1114 Conn: c, 1115 addrs: make(chan net.Addr, 1), 1116 } 1117 l.conns <- brac 1118 return brac, nil 1119 } 1120 1121 type blockingRemoteAddrConn struct { 1122 net.Conn 1123 addrs chan net.Addr 1124 } 1125 1126 func (c *blockingRemoteAddrConn) RemoteAddr() net.Addr { 1127 return <-c.addrs 1128 } 1129 1130 // Issue 12943 1131 func TestServerAllowsBlockingRemoteAddr(t *testing.T) { 1132 defer afterTest(t) 1133 ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) { 1134 fmt.Fprintf(w, "RA:%s", r.RemoteAddr) 1135 })) 1136 conns := make(chan net.Conn) 1137 ts.Listener = &blockingRemoteAddrListener{ 1138 Listener: ts.Listener, 1139 conns: conns, 1140 } 1141 ts.Start() 1142 defer ts.Close() 1143 1144 c := ts.Client() 1145 c.Timeout = time.Second 1146 // Force separate connection for each: 1147 c.Transport.(*Transport).DisableKeepAlives = true 1148 1149 fetch := func(num int, response chan<- string) { 1150 resp, err := c.Get(ts.URL) 1151 if err != nil { 1152 t.Errorf("Request %d: %v", num, err) 1153 response <- "" 1154 return 1155 } 1156 defer resp.Body.Close() 1157 body, err := ioutil.ReadAll(resp.Body) 1158 if err != nil { 1159 t.Errorf("Request %d: %v", num, err) 1160 response <- "" 1161 return 1162 } 1163 response <- string(body) 1164 } 1165 1166 // Start a request. The server will block on getting conn.RemoteAddr. 1167 response1c := make(chan string, 1) 1168 go fetch(1, response1c) 1169 1170 // Wait for the server to accept it; grab the connection. 1171 conn1 := <-conns 1172 1173 // Start another request and grab its connection 1174 response2c := make(chan string, 1) 1175 go fetch(2, response2c) 1176 var conn2 net.Conn 1177 1178 select { 1179 case conn2 = <-conns: 1180 case <-time.After(time.Second): 1181 t.Fatal("Second Accept didn't happen") 1182 } 1183 1184 // Send a response on connection 2. 1185 conn2.(*blockingRemoteAddrConn).addrs <- &net.TCPAddr{ 1186 IP: net.ParseIP("12.12.12.12"), Port: 12} 1187 1188 // ... and see it 1189 response2 := <-response2c 1190 if g, e := response2, "RA:12.12.12.12:12"; g != e { 1191 t.Fatalf("response 2 addr = %q; want %q", g, e) 1192 } 1193 1194 // Finish the first response. 1195 conn1.(*blockingRemoteAddrConn).addrs <- &net.TCPAddr{ 1196 IP: net.ParseIP("21.21.21.21"), Port: 21} 1197 1198 // ... and see it 1199 response1 := <-response1c 1200 if g, e := response1, "RA:21.21.21.21:21"; g != e { 1201 t.Fatalf("response 1 addr = %q; want %q", g, e) 1202 } 1203 } 1204 1205 func TestIdentityResponseHeaders(t *testing.T) { 1206 // Not parallel; changes log output. 1207 defer afterTest(t) 1208 log.SetOutput(ioutil.Discard) // is noisy otherwise 1209 defer log.SetOutput(os.Stderr) 1210 1211 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { 1212 w.Header().Set("Transfer-Encoding", "identity") 1213 w.(Flusher).Flush() 1214 fmt.Fprintf(w, "I am an identity response.") 1215 })) 1216 defer ts.Close() 1217 1218 c := ts.Client() 1219 res, err := c.Get(ts.URL) 1220 if err != nil { 1221 t.Fatalf("Get error: %v", err) 1222 } 1223 defer res.Body.Close() 1224 1225 if g, e := res.TransferEncoding, []string(nil); !reflect.DeepEqual(g, e) { 1226 t.Errorf("expected TransferEncoding of %v; got %v", e, g) 1227 } 1228 if _, haveCL := res.Header["Content-Length"]; haveCL { 1229 t.Errorf("Unexpected Content-Length") 1230 } 1231 if !res.Close { 1232 t.Errorf("expected Connection: close; got %v", res.Close) 1233 } 1234 } 1235 1236 // TestHeadResponses verifies that all MIME type sniffing and Content-Length 1237 // counting of GET requests also happens on HEAD requests. 1238 func TestHeadResponses_h1(t *testing.T) { testHeadResponses(t, h1Mode) } 1239 func TestHeadResponses_h2(t *testing.T) { testHeadResponses(t, h2Mode) } 1240 1241 func testHeadResponses(t *testing.T, h2 bool) { 1242 setParallel(t) 1243 defer afterTest(t) 1244 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 1245 _, err := w.Write([]byte("<html>")) 1246 if err != nil { 1247 t.Errorf("ResponseWriter.Write: %v", err) 1248 } 1249 1250 // Also exercise the ReaderFrom path 1251 _, err = io.Copy(w, strings.NewReader("789a")) 1252 if err != nil { 1253 t.Errorf("Copy(ResponseWriter, ...): %v", err) 1254 } 1255 })) 1256 defer cst.close() 1257 res, err := cst.c.Head(cst.ts.URL) 1258 if err != nil { 1259 t.Error(err) 1260 } 1261 if len(res.TransferEncoding) > 0 { 1262 t.Errorf("expected no TransferEncoding; got %v", res.TransferEncoding) 1263 } 1264 if ct := res.Header.Get("Content-Type"); ct != "text/html; charset=utf-8" { 1265 t.Errorf("Content-Type: %q; want text/html; charset=utf-8", ct) 1266 } 1267 if v := res.ContentLength; v != 10 { 1268 t.Errorf("Content-Length: %d; want 10", v) 1269 } 1270 body, err := ioutil.ReadAll(res.Body) 1271 if err != nil { 1272 t.Error(err) 1273 } 1274 if len(body) > 0 { 1275 t.Errorf("got unexpected body %q", string(body)) 1276 } 1277 } 1278 1279 func TestTLSHandshakeTimeout(t *testing.T) { 1280 setParallel(t) 1281 defer afterTest(t) 1282 ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) {})) 1283 errc := make(chanWriter, 10) // but only expecting 1 1284 ts.Config.ReadTimeout = 250 * time.Millisecond 1285 ts.Config.ErrorLog = log.New(errc, "", 0) 1286 ts.StartTLS() 1287 defer ts.Close() 1288 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 1289 if err != nil { 1290 t.Fatalf("Dial: %v", err) 1291 } 1292 defer conn.Close() 1293 goTimeout(t, 10*time.Second, func() { 1294 var buf [1]byte 1295 n, err := conn.Read(buf[:]) 1296 if err == nil || n != 0 { 1297 t.Errorf("Read = %d, %v; want an error and no bytes", n, err) 1298 } 1299 }) 1300 select { 1301 case v := <-errc: 1302 if !strings.Contains(v, "timeout") && !strings.Contains(v, "TLS handshake") { 1303 t.Errorf("expected a TLS handshake timeout error; got %q", v) 1304 } 1305 case <-time.After(5 * time.Second): 1306 t.Errorf("timeout waiting for logged error") 1307 } 1308 } 1309 1310 func TestTLSServer(t *testing.T) { 1311 setParallel(t) 1312 defer afterTest(t) 1313 ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) { 1314 if r.TLS != nil { 1315 w.Header().Set("X-TLS-Set", "true") 1316 if r.TLS.HandshakeComplete { 1317 w.Header().Set("X-TLS-HandshakeComplete", "true") 1318 } 1319 } 1320 })) 1321 ts.Config.ErrorLog = log.New(ioutil.Discard, "", 0) 1322 defer ts.Close() 1323 1324 // Connect an idle TCP connection to this server before we run 1325 // our real tests. This idle connection used to block forever 1326 // in the TLS handshake, preventing future connections from 1327 // being accepted. It may prevent future accidental blocking 1328 // in newConn. 1329 idleConn, err := net.Dial("tcp", ts.Listener.Addr().String()) 1330 if err != nil { 1331 t.Fatalf("Dial: %v", err) 1332 } 1333 defer idleConn.Close() 1334 goTimeout(t, 10*time.Second, func() { 1335 if !strings.HasPrefix(ts.URL, "https://") { 1336 t.Errorf("expected test TLS server to start with https://, got %q", ts.URL) 1337 return 1338 } 1339 client := ts.Client() 1340 res, err := client.Get(ts.URL) 1341 if err != nil { 1342 t.Error(err) 1343 return 1344 } 1345 if res == nil { 1346 t.Errorf("got nil Response") 1347 return 1348 } 1349 defer res.Body.Close() 1350 if res.Header.Get("X-TLS-Set") != "true" { 1351 t.Errorf("expected X-TLS-Set response header") 1352 return 1353 } 1354 if res.Header.Get("X-TLS-HandshakeComplete") != "true" { 1355 t.Errorf("expected X-TLS-HandshakeComplete header") 1356 } 1357 }) 1358 } 1359 1360 func TestServeTLS(t *testing.T) { 1361 // Not parallel: uses global test hooks. 1362 defer afterTest(t) 1363 defer SetTestHookServerServe(nil) 1364 1365 cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey) 1366 if err != nil { 1367 t.Fatal(err) 1368 } 1369 tlsConf := &tls.Config{ 1370 Certificates: []tls.Certificate{cert}, 1371 } 1372 1373 ln := newLocalListener(t) 1374 defer ln.Close() 1375 addr := ln.Addr().String() 1376 1377 serving := make(chan bool, 1) 1378 SetTestHookServerServe(func(s *Server, ln net.Listener) { 1379 serving <- true 1380 }) 1381 handler := HandlerFunc(func(w ResponseWriter, r *Request) {}) 1382 s := &Server{ 1383 Addr: addr, 1384 TLSConfig: tlsConf, 1385 Handler: handler, 1386 } 1387 errc := make(chan error, 1) 1388 go func() { errc <- s.ServeTLS(ln, "", "") }() 1389 select { 1390 case err := <-errc: 1391 t.Fatalf("ServeTLS: %v", err) 1392 case <-serving: 1393 case <-time.After(5 * time.Second): 1394 t.Fatal("timeout") 1395 } 1396 1397 c, err := tls.Dial("tcp", ln.Addr().String(), &tls.Config{ 1398 InsecureSkipVerify: true, 1399 NextProtos: []string{"h2", "http/1.1"}, 1400 }) 1401 if err != nil { 1402 t.Fatal(err) 1403 } 1404 defer c.Close() 1405 if got, want := c.ConnectionState().NegotiatedProtocol, "h2"; got != want { 1406 t.Errorf("NegotiatedProtocol = %q; want %q", got, want) 1407 } 1408 if got, want := c.ConnectionState().NegotiatedProtocolIsMutual, true; got != want { 1409 t.Errorf("NegotiatedProtocolIsMutual = %v; want %v", got, want) 1410 } 1411 } 1412 1413 // Issue 15908 1414 func TestAutomaticHTTP2_Serve_NoTLSConfig(t *testing.T) { 1415 testAutomaticHTTP2_Serve(t, nil, true) 1416 } 1417 1418 func TestAutomaticHTTP2_Serve_NonH2TLSConfig(t *testing.T) { 1419 testAutomaticHTTP2_Serve(t, &tls.Config{}, false) 1420 } 1421 1422 func TestAutomaticHTTP2_Serve_H2TLSConfig(t *testing.T) { 1423 testAutomaticHTTP2_Serve(t, &tls.Config{NextProtos: []string{"h2"}}, true) 1424 } 1425 1426 func testAutomaticHTTP2_Serve(t *testing.T, tlsConf *tls.Config, wantH2 bool) { 1427 setParallel(t) 1428 defer afterTest(t) 1429 ln := newLocalListener(t) 1430 ln.Close() // immediately (not a defer!) 1431 var s Server 1432 s.TLSConfig = tlsConf 1433 if err := s.Serve(ln); err == nil { 1434 t.Fatal("expected an error") 1435 } 1436 gotH2 := s.TLSNextProto["h2"] != nil 1437 if gotH2 != wantH2 { 1438 t.Errorf("http2 configured = %v; want %v", gotH2, wantH2) 1439 } 1440 } 1441 1442 func TestAutomaticHTTP2_Serve_WithTLSConfig(t *testing.T) { 1443 setParallel(t) 1444 defer afterTest(t) 1445 ln := newLocalListener(t) 1446 ln.Close() // immediately (not a defer!) 1447 var s Server 1448 // Set the TLSConfig. In reality, this would be the 1449 // *tls.Config given to tls.NewListener. 1450 s.TLSConfig = &tls.Config{ 1451 NextProtos: []string{"h2"}, 1452 } 1453 if err := s.Serve(ln); err == nil { 1454 t.Fatal("expected an error") 1455 } 1456 on := s.TLSNextProto["h2"] != nil 1457 if !on { 1458 t.Errorf("http2 wasn't automatically enabled") 1459 } 1460 } 1461 1462 func TestAutomaticHTTP2_ListenAndServe(t *testing.T) { 1463 cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey) 1464 if err != nil { 1465 t.Fatal(err) 1466 } 1467 testAutomaticHTTP2_ListenAndServe(t, &tls.Config{ 1468 Certificates: []tls.Certificate{cert}, 1469 }) 1470 } 1471 1472 func TestAutomaticHTTP2_ListenAndServe_GetCertificate(t *testing.T) { 1473 cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey) 1474 if err != nil { 1475 t.Fatal(err) 1476 } 1477 testAutomaticHTTP2_ListenAndServe(t, &tls.Config{ 1478 GetCertificate: func(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) { 1479 return &cert, nil 1480 }, 1481 }) 1482 } 1483 1484 func testAutomaticHTTP2_ListenAndServe(t *testing.T, tlsConf *tls.Config) { 1485 // Not parallel: uses global test hooks. 1486 defer afterTest(t) 1487 defer SetTestHookServerServe(nil) 1488 var ok bool 1489 var s *Server 1490 const maxTries = 5 1491 var ln net.Listener 1492 Try: 1493 for try := 0; try < maxTries; try++ { 1494 ln = newLocalListener(t) 1495 addr := ln.Addr().String() 1496 ln.Close() 1497 t.Logf("Got %v", addr) 1498 lnc := make(chan net.Listener, 1) 1499 SetTestHookServerServe(func(s *Server, ln net.Listener) { 1500 lnc <- ln 1501 }) 1502 s = &Server{ 1503 Addr: addr, 1504 TLSConfig: tlsConf, 1505 } 1506 errc := make(chan error, 1) 1507 go func() { errc <- s.ListenAndServeTLS("", "") }() 1508 select { 1509 case err := <-errc: 1510 t.Logf("On try #%v: %v", try+1, err) 1511 continue 1512 case ln = <-lnc: 1513 ok = true 1514 t.Logf("Listening on %v", ln.Addr().String()) 1515 break Try 1516 } 1517 } 1518 if !ok { 1519 t.Fatalf("Failed to start up after %d tries", maxTries) 1520 } 1521 defer ln.Close() 1522 c, err := tls.Dial("tcp", ln.Addr().String(), &tls.Config{ 1523 InsecureSkipVerify: true, 1524 NextProtos: []string{"h2", "http/1.1"}, 1525 }) 1526 if err != nil { 1527 t.Fatal(err) 1528 } 1529 defer c.Close() 1530 if got, want := c.ConnectionState().NegotiatedProtocol, "h2"; got != want { 1531 t.Errorf("NegotiatedProtocol = %q; want %q", got, want) 1532 } 1533 if got, want := c.ConnectionState().NegotiatedProtocolIsMutual, true; got != want { 1534 t.Errorf("NegotiatedProtocolIsMutual = %v; want %v", got, want) 1535 } 1536 } 1537 1538 type serverExpectTest struct { 1539 contentLength int // of request body 1540 chunked bool 1541 expectation string // e.g. "100-continue" 1542 readBody bool // whether handler should read the body (if false, sends StatusUnauthorized) 1543 expectedResponse string // expected substring in first line of http response 1544 } 1545 1546 func expectTest(contentLength int, expectation string, readBody bool, expectedResponse string) serverExpectTest { 1547 return serverExpectTest{ 1548 contentLength: contentLength, 1549 expectation: expectation, 1550 readBody: readBody, 1551 expectedResponse: expectedResponse, 1552 } 1553 } 1554 1555 var serverExpectTests = []serverExpectTest{ 1556 // Normal 100-continues, case-insensitive. 1557 expectTest(100, "100-continue", true, "100 Continue"), 1558 expectTest(100, "100-cOntInUE", true, "100 Continue"), 1559 1560 // No 100-continue. 1561 expectTest(100, "", true, "200 OK"), 1562 1563 // 100-continue but requesting client to deny us, 1564 // so it never reads the body. 1565 expectTest(100, "100-continue", false, "401 Unauthorized"), 1566 // Likewise without 100-continue: 1567 expectTest(100, "", false, "401 Unauthorized"), 1568 1569 // Non-standard expectations are failures 1570 expectTest(0, "a-pony", false, "417 Expectation Failed"), 1571 1572 // Expect-100 requested but no body (is apparently okay: Issue 7625) 1573 expectTest(0, "100-continue", true, "200 OK"), 1574 // Expect-100 requested but handler doesn't read the body 1575 expectTest(0, "100-continue", false, "401 Unauthorized"), 1576 // Expect-100 continue with no body, but a chunked body. 1577 { 1578 expectation: "100-continue", 1579 readBody: true, 1580 chunked: true, 1581 expectedResponse: "100 Continue", 1582 }, 1583 } 1584 1585 // Tests that the server responds to the "Expect" request header 1586 // correctly. 1587 // http2 test: TestServer_Response_Automatic100Continue 1588 func TestServerExpect(t *testing.T) { 1589 setParallel(t) 1590 defer afterTest(t) 1591 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { 1592 // Note using r.FormValue("readbody") because for POST 1593 // requests that would read from r.Body, which we only 1594 // conditionally want to do. 1595 if strings.Contains(r.URL.RawQuery, "readbody=true") { 1596 ioutil.ReadAll(r.Body) 1597 w.Write([]byte("Hi")) 1598 } else { 1599 w.WriteHeader(StatusUnauthorized) 1600 } 1601 })) 1602 defer ts.Close() 1603 1604 runTest := func(test serverExpectTest) { 1605 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 1606 if err != nil { 1607 t.Fatalf("Dial: %v", err) 1608 } 1609 defer conn.Close() 1610 1611 // Only send the body immediately if we're acting like an HTTP client 1612 // that doesn't send 100-continue expectations. 1613 writeBody := test.contentLength != 0 && strings.ToLower(test.expectation) != "100-continue" 1614 1615 go func() { 1616 contentLen := fmt.Sprintf("Content-Length: %d", test.contentLength) 1617 if test.chunked { 1618 contentLen = "Transfer-Encoding: chunked" 1619 } 1620 _, err := fmt.Fprintf(conn, "POST /?readbody=%v HTTP/1.1\r\n"+ 1621 "Connection: close\r\n"+ 1622 "%s\r\n"+ 1623 "Expect: %s\r\nHost: foo\r\n\r\n", 1624 test.readBody, contentLen, test.expectation) 1625 if err != nil { 1626 t.Errorf("On test %#v, error writing request headers: %v", test, err) 1627 return 1628 } 1629 if writeBody { 1630 var targ io.WriteCloser = struct { 1631 io.Writer 1632 io.Closer 1633 }{ 1634 conn, 1635 ioutil.NopCloser(nil), 1636 } 1637 if test.chunked { 1638 targ = httputil.NewChunkedWriter(conn) 1639 } 1640 body := strings.Repeat("A", test.contentLength) 1641 _, err = fmt.Fprint(targ, body) 1642 if err == nil { 1643 err = targ.Close() 1644 } 1645 if err != nil { 1646 if !test.readBody { 1647 // Server likely already hung up on us. 1648 // See larger comment below. 1649 t.Logf("On test %#v, acceptable error writing request body: %v", test, err) 1650 return 1651 } 1652 t.Errorf("On test %#v, error writing request body: %v", test, err) 1653 } 1654 } 1655 }() 1656 bufr := bufio.NewReader(conn) 1657 line, err := bufr.ReadString('\n') 1658 if err != nil { 1659 if writeBody && !test.readBody { 1660 // This is an acceptable failure due to a possible TCP race: 1661 // We were still writing data and the server hung up on us. A TCP 1662 // implementation may send a RST if our request body data was known 1663 // to be lost, which may trigger our reads to fail. 1664 // See RFC 1122 page 88. 1665 t.Logf("On test %#v, acceptable error from ReadString: %v", test, err) 1666 return 1667 } 1668 t.Fatalf("On test %#v, ReadString: %v", test, err) 1669 } 1670 if !strings.Contains(line, test.expectedResponse) { 1671 t.Errorf("On test %#v, got first line = %q; want %q", test, line, test.expectedResponse) 1672 } 1673 } 1674 1675 for _, test := range serverExpectTests { 1676 runTest(test) 1677 } 1678 } 1679 1680 // Under a ~256KB (maxPostHandlerReadBytes) threshold, the server 1681 // should consume client request bodies that a handler didn't read. 1682 func TestServerUnreadRequestBodyLittle(t *testing.T) { 1683 setParallel(t) 1684 defer afterTest(t) 1685 conn := new(testConn) 1686 body := strings.Repeat("x", 100<<10) 1687 conn.readBuf.Write([]byte(fmt.Sprintf( 1688 "POST / HTTP/1.1\r\n"+ 1689 "Host: test\r\n"+ 1690 "Content-Length: %d\r\n"+ 1691 "\r\n", len(body)))) 1692 conn.readBuf.Write([]byte(body)) 1693 1694 done := make(chan bool) 1695 1696 readBufLen := func() int { 1697 conn.readMu.Lock() 1698 defer conn.readMu.Unlock() 1699 return conn.readBuf.Len() 1700 } 1701 1702 ls := &oneConnListener{conn} 1703 go Serve(ls, HandlerFunc(func(rw ResponseWriter, req *Request) { 1704 defer close(done) 1705 if bufLen := readBufLen(); bufLen < len(body)/2 { 1706 t.Errorf("on request, read buffer length is %d; expected about 100 KB", bufLen) 1707 } 1708 rw.WriteHeader(200) 1709 rw.(Flusher).Flush() 1710 if g, e := readBufLen(), 0; g != e { 1711 t.Errorf("after WriteHeader, read buffer length is %d; want %d", g, e) 1712 } 1713 if c := rw.Header().Get("Connection"); c != "" { 1714 t.Errorf(`Connection header = %q; want ""`, c) 1715 } 1716 })) 1717 <-done 1718 } 1719 1720 // Over a ~256KB (maxPostHandlerReadBytes) threshold, the server 1721 // should ignore client request bodies that a handler didn't read 1722 // and close the connection. 1723 func TestServerUnreadRequestBodyLarge(t *testing.T) { 1724 setParallel(t) 1725 if testing.Short() && testenv.Builder() == "" { 1726 t.Log("skipping in short mode") 1727 } 1728 conn := new(testConn) 1729 body := strings.Repeat("x", 1<<20) 1730 conn.readBuf.Write([]byte(fmt.Sprintf( 1731 "POST / HTTP/1.1\r\n"+ 1732 "Host: test\r\n"+ 1733 "Content-Length: %d\r\n"+ 1734 "\r\n", len(body)))) 1735 conn.readBuf.Write([]byte(body)) 1736 conn.closec = make(chan bool, 1) 1737 1738 ls := &oneConnListener{conn} 1739 go Serve(ls, HandlerFunc(func(rw ResponseWriter, req *Request) { 1740 if conn.readBuf.Len() < len(body)/2 { 1741 t.Errorf("on request, read buffer length is %d; expected about 1MB", conn.readBuf.Len()) 1742 } 1743 rw.WriteHeader(200) 1744 rw.(Flusher).Flush() 1745 if conn.readBuf.Len() < len(body)/2 { 1746 t.Errorf("post-WriteHeader, read buffer length is %d; expected about 1MB", conn.readBuf.Len()) 1747 } 1748 })) 1749 <-conn.closec 1750 1751 if res := conn.writeBuf.String(); !strings.Contains(res, "Connection: close") { 1752 t.Errorf("Expected a Connection: close header; got response: %s", res) 1753 } 1754 } 1755 1756 type handlerBodyCloseTest struct { 1757 bodySize int 1758 bodyChunked bool 1759 reqConnClose bool 1760 1761 wantEOFSearch bool // should Handler's Body.Close do Reads, looking for EOF? 1762 wantNextReq bool // should it find the next request on the same conn? 1763 } 1764 1765 func (t handlerBodyCloseTest) connectionHeader() string { 1766 if t.reqConnClose { 1767 return "Connection: close\r\n" 1768 } 1769 return "" 1770 } 1771 1772 var handlerBodyCloseTests = [...]handlerBodyCloseTest{ 1773 // Small enough to slurp past to the next request + 1774 // has Content-Length. 1775 0: { 1776 bodySize: 20 << 10, 1777 bodyChunked: false, 1778 reqConnClose: false, 1779 wantEOFSearch: true, 1780 wantNextReq: true, 1781 }, 1782 1783 // Small enough to slurp past to the next request + 1784 // is chunked. 1785 1: { 1786 bodySize: 20 << 10, 1787 bodyChunked: true, 1788 reqConnClose: false, 1789 wantEOFSearch: true, 1790 wantNextReq: true, 1791 }, 1792 1793 // Small enough to slurp past to the next request + 1794 // has Content-Length + 1795 // declares Connection: close (so pointless to read more). 1796 2: { 1797 bodySize: 20 << 10, 1798 bodyChunked: false, 1799 reqConnClose: true, 1800 wantEOFSearch: false, 1801 wantNextReq: false, 1802 }, 1803 1804 // Small enough to slurp past to the next request + 1805 // declares Connection: close, 1806 // but chunked, so it might have trailers. 1807 // TODO: maybe skip this search if no trailers were declared 1808 // in the headers. 1809 3: { 1810 bodySize: 20 << 10, 1811 bodyChunked: true, 1812 reqConnClose: true, 1813 wantEOFSearch: true, 1814 wantNextReq: false, 1815 }, 1816 1817 // Big with Content-Length, so give up immediately if we know it's too big. 1818 4: { 1819 bodySize: 1 << 20, 1820 bodyChunked: false, // has a Content-Length 1821 reqConnClose: false, 1822 wantEOFSearch: false, 1823 wantNextReq: false, 1824 }, 1825 1826 // Big chunked, so read a bit before giving up. 1827 5: { 1828 bodySize: 1 << 20, 1829 bodyChunked: true, 1830 reqConnClose: false, 1831 wantEOFSearch: true, 1832 wantNextReq: false, 1833 }, 1834 1835 // Big with Connection: close, but chunked, so search for trailers. 1836 // TODO: maybe skip this search if no trailers were declared 1837 // in the headers. 1838 6: { 1839 bodySize: 1 << 20, 1840 bodyChunked: true, 1841 reqConnClose: true, 1842 wantEOFSearch: true, 1843 wantNextReq: false, 1844 }, 1845 1846 // Big with Connection: close, so don't do any reads on Close. 1847 // With Content-Length. 1848 7: { 1849 bodySize: 1 << 20, 1850 bodyChunked: false, 1851 reqConnClose: true, 1852 wantEOFSearch: false, 1853 wantNextReq: false, 1854 }, 1855 } 1856 1857 func TestHandlerBodyClose(t *testing.T) { 1858 setParallel(t) 1859 if testing.Short() && testenv.Builder() == "" { 1860 t.Skip("skipping in -short mode") 1861 } 1862 for i, tt := range handlerBodyCloseTests { 1863 testHandlerBodyClose(t, i, tt) 1864 } 1865 } 1866 1867 func testHandlerBodyClose(t *testing.T, i int, tt handlerBodyCloseTest) { 1868 conn := new(testConn) 1869 body := strings.Repeat("x", tt.bodySize) 1870 if tt.bodyChunked { 1871 conn.readBuf.WriteString("POST / HTTP/1.1\r\n" + 1872 "Host: test\r\n" + 1873 tt.connectionHeader() + 1874 "Transfer-Encoding: chunked\r\n" + 1875 "\r\n") 1876 cw := internal.NewChunkedWriter(&conn.readBuf) 1877 io.WriteString(cw, body) 1878 cw.Close() 1879 conn.readBuf.WriteString("\r\n") 1880 } else { 1881 conn.readBuf.Write([]byte(fmt.Sprintf( 1882 "POST / HTTP/1.1\r\n"+ 1883 "Host: test\r\n"+ 1884 tt.connectionHeader()+ 1885 "Content-Length: %d\r\n"+ 1886 "\r\n", len(body)))) 1887 conn.readBuf.Write([]byte(body)) 1888 } 1889 if !tt.reqConnClose { 1890 conn.readBuf.WriteString("GET / HTTP/1.1\r\nHost: test\r\n\r\n") 1891 } 1892 conn.closec = make(chan bool, 1) 1893 1894 readBufLen := func() int { 1895 conn.readMu.Lock() 1896 defer conn.readMu.Unlock() 1897 return conn.readBuf.Len() 1898 } 1899 1900 ls := &oneConnListener{conn} 1901 var numReqs int 1902 var size0, size1 int 1903 go Serve(ls, HandlerFunc(func(rw ResponseWriter, req *Request) { 1904 numReqs++ 1905 if numReqs == 1 { 1906 size0 = readBufLen() 1907 req.Body.Close() 1908 size1 = readBufLen() 1909 } 1910 })) 1911 <-conn.closec 1912 if numReqs < 1 || numReqs > 2 { 1913 t.Fatalf("%d. bug in test. unexpected number of requests = %d", i, numReqs) 1914 } 1915 didSearch := size0 != size1 1916 if didSearch != tt.wantEOFSearch { 1917 t.Errorf("%d. did EOF search = %v; want %v (size went from %d to %d)", i, didSearch, !didSearch, size0, size1) 1918 } 1919 if tt.wantNextReq && numReqs != 2 { 1920 t.Errorf("%d. numReq = %d; want 2", i, numReqs) 1921 } 1922 } 1923 1924 // testHandlerBodyConsumer represents a function injected into a test handler to 1925 // vary work done on a request Body. 1926 type testHandlerBodyConsumer struct { 1927 name string 1928 f func(io.ReadCloser) 1929 } 1930 1931 var testHandlerBodyConsumers = []testHandlerBodyConsumer{ 1932 {"nil", func(io.ReadCloser) {}}, 1933 {"close", func(r io.ReadCloser) { r.Close() }}, 1934 {"discard", func(r io.ReadCloser) { io.Copy(ioutil.Discard, r) }}, 1935 } 1936 1937 func TestRequestBodyReadErrorClosesConnection(t *testing.T) { 1938 setParallel(t) 1939 defer afterTest(t) 1940 for _, handler := range testHandlerBodyConsumers { 1941 conn := new(testConn) 1942 conn.readBuf.WriteString("POST /public HTTP/1.1\r\n" + 1943 "Host: test\r\n" + 1944 "Transfer-Encoding: chunked\r\n" + 1945 "\r\n" + 1946 "hax\r\n" + // Invalid chunked encoding 1947 "GET /secret HTTP/1.1\r\n" + 1948 "Host: test\r\n" + 1949 "\r\n") 1950 1951 conn.closec = make(chan bool, 1) 1952 ls := &oneConnListener{conn} 1953 var numReqs int 1954 go Serve(ls, HandlerFunc(func(_ ResponseWriter, req *Request) { 1955 numReqs++ 1956 if strings.Contains(req.URL.Path, "secret") { 1957 t.Error("Request for /secret encountered, should not have happened.") 1958 } 1959 handler.f(req.Body) 1960 })) 1961 <-conn.closec 1962 if numReqs != 1 { 1963 t.Errorf("Handler %v: got %d reqs; want 1", handler.name, numReqs) 1964 } 1965 } 1966 } 1967 1968 func TestInvalidTrailerClosesConnection(t *testing.T) { 1969 setParallel(t) 1970 defer afterTest(t) 1971 for _, handler := range testHandlerBodyConsumers { 1972 conn := new(testConn) 1973 conn.readBuf.WriteString("POST /public HTTP/1.1\r\n" + 1974 "Host: test\r\n" + 1975 "Trailer: hack\r\n" + 1976 "Transfer-Encoding: chunked\r\n" + 1977 "\r\n" + 1978 "3\r\n" + 1979 "hax\r\n" + 1980 "0\r\n" + 1981 "I'm not a valid trailer\r\n" + 1982 "GET /secret HTTP/1.1\r\n" + 1983 "Host: test\r\n" + 1984 "\r\n") 1985 1986 conn.closec = make(chan bool, 1) 1987 ln := &oneConnListener{conn} 1988 var numReqs int 1989 go Serve(ln, HandlerFunc(func(_ ResponseWriter, req *Request) { 1990 numReqs++ 1991 if strings.Contains(req.URL.Path, "secret") { 1992 t.Errorf("Handler %s, Request for /secret encountered, should not have happened.", handler.name) 1993 } 1994 handler.f(req.Body) 1995 })) 1996 <-conn.closec 1997 if numReqs != 1 { 1998 t.Errorf("Handler %s: got %d reqs; want 1", handler.name, numReqs) 1999 } 2000 } 2001 } 2002 2003 // slowTestConn is a net.Conn that provides a means to simulate parts of a 2004 // request being received piecemeal. Deadlines can be set and enforced in both 2005 // Read and Write. 2006 type slowTestConn struct { 2007 // over multiple calls to Read, time.Durations are slept, strings are read. 2008 script []interface{} 2009 closec chan bool 2010 2011 mu sync.Mutex // guards rd/wd 2012 rd, wd time.Time // read, write deadline 2013 noopConn 2014 } 2015 2016 func (c *slowTestConn) SetDeadline(t time.Time) error { 2017 c.SetReadDeadline(t) 2018 c.SetWriteDeadline(t) 2019 return nil 2020 } 2021 2022 func (c *slowTestConn) SetReadDeadline(t time.Time) error { 2023 c.mu.Lock() 2024 defer c.mu.Unlock() 2025 c.rd = t 2026 return nil 2027 } 2028 2029 func (c *slowTestConn) SetWriteDeadline(t time.Time) error { 2030 c.mu.Lock() 2031 defer c.mu.Unlock() 2032 c.wd = t 2033 return nil 2034 } 2035 2036 func (c *slowTestConn) Read(b []byte) (n int, err error) { 2037 c.mu.Lock() 2038 defer c.mu.Unlock() 2039 restart: 2040 if !c.rd.IsZero() && time.Now().After(c.rd) { 2041 return 0, syscall.ETIMEDOUT 2042 } 2043 if len(c.script) == 0 { 2044 return 0, io.EOF 2045 } 2046 2047 switch cue := c.script[0].(type) { 2048 case time.Duration: 2049 if !c.rd.IsZero() { 2050 // If the deadline falls in the middle of our sleep window, deduct 2051 // part of the sleep, then return a timeout. 2052 if remaining := time.Until(c.rd); remaining < cue { 2053 c.script[0] = cue - remaining 2054 time.Sleep(remaining) 2055 return 0, syscall.ETIMEDOUT 2056 } 2057 } 2058 c.script = c.script[1:] 2059 time.Sleep(cue) 2060 goto restart 2061 2062 case string: 2063 n = copy(b, cue) 2064 // If cue is too big for the buffer, leave the end for the next Read. 2065 if len(cue) > n { 2066 c.script[0] = cue[n:] 2067 } else { 2068 c.script = c.script[1:] 2069 } 2070 2071 default: 2072 panic("unknown cue in slowTestConn script") 2073 } 2074 2075 return 2076 } 2077 2078 func (c *slowTestConn) Close() error { 2079 select { 2080 case c.closec <- true: 2081 default: 2082 } 2083 return nil 2084 } 2085 2086 func (c *slowTestConn) Write(b []byte) (int, error) { 2087 if !c.wd.IsZero() && time.Now().After(c.wd) { 2088 return 0, syscall.ETIMEDOUT 2089 } 2090 return len(b), nil 2091 } 2092 2093 func TestRequestBodyTimeoutClosesConnection(t *testing.T) { 2094 if testing.Short() { 2095 t.Skip("skipping in -short mode") 2096 } 2097 defer afterTest(t) 2098 for _, handler := range testHandlerBodyConsumers { 2099 conn := &slowTestConn{ 2100 script: []interface{}{ 2101 "POST /public HTTP/1.1\r\n" + 2102 "Host: test\r\n" + 2103 "Content-Length: 10000\r\n" + 2104 "\r\n", 2105 "foo bar baz", 2106 600 * time.Millisecond, // Request deadline should hit here 2107 "GET /secret HTTP/1.1\r\n" + 2108 "Host: test\r\n" + 2109 "\r\n", 2110 }, 2111 closec: make(chan bool, 1), 2112 } 2113 ls := &oneConnListener{conn} 2114 2115 var numReqs int 2116 s := Server{ 2117 Handler: HandlerFunc(func(_ ResponseWriter, req *Request) { 2118 numReqs++ 2119 if strings.Contains(req.URL.Path, "secret") { 2120 t.Error("Request for /secret encountered, should not have happened.") 2121 } 2122 handler.f(req.Body) 2123 }), 2124 ReadTimeout: 400 * time.Millisecond, 2125 } 2126 go s.Serve(ls) 2127 <-conn.closec 2128 2129 if numReqs != 1 { 2130 t.Errorf("Handler %v: got %d reqs; want 1", handler.name, numReqs) 2131 } 2132 } 2133 } 2134 2135 func TestTimeoutHandler_h1(t *testing.T) { testTimeoutHandler(t, h1Mode) } 2136 func TestTimeoutHandler_h2(t *testing.T) { testTimeoutHandler(t, h2Mode) } 2137 func testTimeoutHandler(t *testing.T, h2 bool) { 2138 setParallel(t) 2139 defer afterTest(t) 2140 sendHi := make(chan bool, 1) 2141 writeErrors := make(chan error, 1) 2142 sayHi := HandlerFunc(func(w ResponseWriter, r *Request) { 2143 <-sendHi 2144 _, werr := w.Write([]byte("hi")) 2145 writeErrors <- werr 2146 }) 2147 timeout := make(chan time.Time, 1) // write to this to force timeouts 2148 cst := newClientServerTest(t, h2, NewTestTimeoutHandler(sayHi, timeout)) 2149 defer cst.close() 2150 2151 // Succeed without timing out: 2152 sendHi <- true 2153 res, err := cst.c.Get(cst.ts.URL) 2154 if err != nil { 2155 t.Error(err) 2156 } 2157 if g, e := res.StatusCode, StatusOK; g != e { 2158 t.Errorf("got res.StatusCode %d; expected %d", g, e) 2159 } 2160 body, _ := ioutil.ReadAll(res.Body) 2161 if g, e := string(body), "hi"; g != e { 2162 t.Errorf("got body %q; expected %q", g, e) 2163 } 2164 if g := <-writeErrors; g != nil { 2165 t.Errorf("got unexpected Write error on first request: %v", g) 2166 } 2167 2168 // Times out: 2169 timeout <- time.Time{} 2170 res, err = cst.c.Get(cst.ts.URL) 2171 if err != nil { 2172 t.Error(err) 2173 } 2174 if g, e := res.StatusCode, StatusServiceUnavailable; g != e { 2175 t.Errorf("got res.StatusCode %d; expected %d", g, e) 2176 } 2177 body, _ = ioutil.ReadAll(res.Body) 2178 if !strings.Contains(string(body), "<title>Timeout</title>") { 2179 t.Errorf("expected timeout body; got %q", string(body)) 2180 } 2181 2182 // Now make the previously-timed out handler speak again, 2183 // which verifies the panic is handled: 2184 sendHi <- true 2185 if g, e := <-writeErrors, ErrHandlerTimeout; g != e { 2186 t.Errorf("expected Write error of %v; got %v", e, g) 2187 } 2188 } 2189 2190 // See issues 8209 and 8414. 2191 func TestTimeoutHandlerRace(t *testing.T) { 2192 setParallel(t) 2193 defer afterTest(t) 2194 2195 delayHi := HandlerFunc(func(w ResponseWriter, r *Request) { 2196 ms, _ := strconv.Atoi(r.URL.Path[1:]) 2197 if ms == 0 { 2198 ms = 1 2199 } 2200 for i := 0; i < ms; i++ { 2201 w.Write([]byte("hi")) 2202 time.Sleep(time.Millisecond) 2203 } 2204 }) 2205 2206 ts := httptest.NewServer(TimeoutHandler(delayHi, 20*time.Millisecond, "")) 2207 defer ts.Close() 2208 2209 c := ts.Client() 2210 2211 var wg sync.WaitGroup 2212 gate := make(chan bool, 10) 2213 n := 50 2214 if testing.Short() { 2215 n = 10 2216 gate = make(chan bool, 3) 2217 } 2218 for i := 0; i < n; i++ { 2219 gate <- true 2220 wg.Add(1) 2221 go func() { 2222 defer wg.Done() 2223 defer func() { <-gate }() 2224 res, err := c.Get(fmt.Sprintf("%s/%d", ts.URL, rand.Intn(50))) 2225 if err == nil { 2226 io.Copy(ioutil.Discard, res.Body) 2227 res.Body.Close() 2228 } 2229 }() 2230 } 2231 wg.Wait() 2232 } 2233 2234 // See issues 8209 and 8414. 2235 func TestTimeoutHandlerRaceHeader(t *testing.T) { 2236 setParallel(t) 2237 defer afterTest(t) 2238 2239 delay204 := HandlerFunc(func(w ResponseWriter, r *Request) { 2240 w.WriteHeader(204) 2241 }) 2242 2243 ts := httptest.NewServer(TimeoutHandler(delay204, time.Nanosecond, "")) 2244 defer ts.Close() 2245 2246 var wg sync.WaitGroup 2247 gate := make(chan bool, 50) 2248 n := 500 2249 if testing.Short() { 2250 n = 10 2251 } 2252 2253 c := ts.Client() 2254 for i := 0; i < n; i++ { 2255 gate <- true 2256 wg.Add(1) 2257 go func() { 2258 defer wg.Done() 2259 defer func() { <-gate }() 2260 res, err := c.Get(ts.URL) 2261 if err != nil { 2262 t.Error(err) 2263 return 2264 } 2265 defer res.Body.Close() 2266 io.Copy(ioutil.Discard, res.Body) 2267 }() 2268 } 2269 wg.Wait() 2270 } 2271 2272 // Issue 9162 2273 func TestTimeoutHandlerRaceHeaderTimeout(t *testing.T) { 2274 setParallel(t) 2275 defer afterTest(t) 2276 sendHi := make(chan bool, 1) 2277 writeErrors := make(chan error, 1) 2278 sayHi := HandlerFunc(func(w ResponseWriter, r *Request) { 2279 w.Header().Set("Content-Type", "text/plain") 2280 <-sendHi 2281 _, werr := w.Write([]byte("hi")) 2282 writeErrors <- werr 2283 }) 2284 timeout := make(chan time.Time, 1) // write to this to force timeouts 2285 cst := newClientServerTest(t, h1Mode, NewTestTimeoutHandler(sayHi, timeout)) 2286 defer cst.close() 2287 2288 // Succeed without timing out: 2289 sendHi <- true 2290 res, err := cst.c.Get(cst.ts.URL) 2291 if err != nil { 2292 t.Error(err) 2293 } 2294 if g, e := res.StatusCode, StatusOK; g != e { 2295 t.Errorf("got res.StatusCode %d; expected %d", g, e) 2296 } 2297 body, _ := ioutil.ReadAll(res.Body) 2298 if g, e := string(body), "hi"; g != e { 2299 t.Errorf("got body %q; expected %q", g, e) 2300 } 2301 if g := <-writeErrors; g != nil { 2302 t.Errorf("got unexpected Write error on first request: %v", g) 2303 } 2304 2305 // Times out: 2306 timeout <- time.Time{} 2307 res, err = cst.c.Get(cst.ts.URL) 2308 if err != nil { 2309 t.Error(err) 2310 } 2311 if g, e := res.StatusCode, StatusServiceUnavailable; g != e { 2312 t.Errorf("got res.StatusCode %d; expected %d", g, e) 2313 } 2314 body, _ = ioutil.ReadAll(res.Body) 2315 if !strings.Contains(string(body), "<title>Timeout</title>") { 2316 t.Errorf("expected timeout body; got %q", string(body)) 2317 } 2318 2319 // Now make the previously-timed out handler speak again, 2320 // which verifies the panic is handled: 2321 sendHi <- true 2322 if g, e := <-writeErrors, ErrHandlerTimeout; g != e { 2323 t.Errorf("expected Write error of %v; got %v", e, g) 2324 } 2325 } 2326 2327 // Issue 14568. 2328 func TestTimeoutHandlerStartTimerWhenServing(t *testing.T) { 2329 if testing.Short() { 2330 t.Skip("skipping sleeping test in -short mode") 2331 } 2332 defer afterTest(t) 2333 var handler HandlerFunc = func(w ResponseWriter, _ *Request) { 2334 w.WriteHeader(StatusNoContent) 2335 } 2336 timeout := 300 * time.Millisecond 2337 ts := httptest.NewServer(TimeoutHandler(handler, timeout, "")) 2338 defer ts.Close() 2339 2340 c := ts.Client() 2341 2342 // Issue was caused by the timeout handler starting the timer when 2343 // was created, not when the request. So wait for more than the timeout 2344 // to ensure that's not the case. 2345 time.Sleep(2 * timeout) 2346 res, err := c.Get(ts.URL) 2347 if err != nil { 2348 t.Fatal(err) 2349 } 2350 defer res.Body.Close() 2351 if res.StatusCode != StatusNoContent { 2352 t.Errorf("got res.StatusCode %d, want %v", res.StatusCode, StatusNoContent) 2353 } 2354 } 2355 2356 // https://golang.org/issue/15948 2357 func TestTimeoutHandlerEmptyResponse(t *testing.T) { 2358 setParallel(t) 2359 defer afterTest(t) 2360 var handler HandlerFunc = func(w ResponseWriter, _ *Request) { 2361 // No response. 2362 } 2363 timeout := 300 * time.Millisecond 2364 ts := httptest.NewServer(TimeoutHandler(handler, timeout, "")) 2365 defer ts.Close() 2366 2367 c := ts.Client() 2368 2369 res, err := c.Get(ts.URL) 2370 if err != nil { 2371 t.Fatal(err) 2372 } 2373 defer res.Body.Close() 2374 if res.StatusCode != StatusOK { 2375 t.Errorf("got res.StatusCode %d, want %v", res.StatusCode, StatusOK) 2376 } 2377 } 2378 2379 func TestRedirectBadPath(t *testing.T) { 2380 // This used to crash. It's not valid input (bad path), but it 2381 // shouldn't crash. 2382 rr := httptest.NewRecorder() 2383 req := &Request{ 2384 Method: "GET", 2385 URL: &url.URL{ 2386 Scheme: "http", 2387 Path: "not-empty-but-no-leading-slash", // bogus 2388 }, 2389 } 2390 Redirect(rr, req, "", 304) 2391 if rr.Code != 304 { 2392 t.Errorf("Code = %d; want 304", rr.Code) 2393 } 2394 } 2395 2396 // Test different URL formats and schemes 2397 func TestRedirect(t *testing.T) { 2398 req, _ := NewRequest("GET", "http://example.com/qux/", nil) 2399 2400 var tests = []struct { 2401 in string 2402 want string 2403 }{ 2404 // normal http 2405 {"http://foobar.com/baz", "http://foobar.com/baz"}, 2406 // normal https 2407 {"https://foobar.com/baz", "https://foobar.com/baz"}, 2408 // custom scheme 2409 {"test://foobar.com/baz", "test://foobar.com/baz"}, 2410 // schemeless 2411 {"//foobar.com/baz", "//foobar.com/baz"}, 2412 // relative to the root 2413 {"/foobar.com/baz", "/foobar.com/baz"}, 2414 // relative to the current path 2415 {"foobar.com/baz", "/qux/foobar.com/baz"}, 2416 // relative to the current path (+ going upwards) 2417 {"../quux/foobar.com/baz", "/quux/foobar.com/baz"}, 2418 // incorrect number of slashes 2419 {"///foobar.com/baz", "/foobar.com/baz"}, 2420 2421 // Verifies we don't path.Clean() on the wrong parts in redirects: 2422 {"/foo?next=http://bar.com/", "/foo?next=http://bar.com/"}, 2423 {"http://localhost:8080/_ah/login?continue=http://localhost:8080/", 2424 "http://localhost:8080/_ah/login?continue=http://localhost:8080/"}, 2425 2426 {"/фубар", "/%d1%84%d1%83%d0%b1%d0%b0%d1%80"}, 2427 {"http://foo.com/фубар", "http://foo.com/%d1%84%d1%83%d0%b1%d0%b0%d1%80"}, 2428 } 2429 2430 for _, tt := range tests { 2431 rec := httptest.NewRecorder() 2432 Redirect(rec, req, tt.in, 302) 2433 if got := rec.Header().Get("Location"); got != tt.want { 2434 t.Errorf("Redirect(%q) generated Location header %q; want %q", tt.in, got, tt.want) 2435 } 2436 } 2437 } 2438 2439 // TestZeroLengthPostAndResponse exercises an optimization done by the Transport: 2440 // when there is no body (either because the method doesn't permit a body, or an 2441 // explicit Content-Length of zero is present), then the transport can re-use the 2442 // connection immediately. But when it re-uses the connection, it typically closes 2443 // the previous request's body, which is not optimal for zero-lengthed bodies, 2444 // as the client would then see http.ErrBodyReadAfterClose and not 0, io.EOF. 2445 func TestZeroLengthPostAndResponse_h1(t *testing.T) { 2446 testZeroLengthPostAndResponse(t, h1Mode) 2447 } 2448 func TestZeroLengthPostAndResponse_h2(t *testing.T) { 2449 testZeroLengthPostAndResponse(t, h2Mode) 2450 } 2451 2452 func testZeroLengthPostAndResponse(t *testing.T, h2 bool) { 2453 setParallel(t) 2454 defer afterTest(t) 2455 cst := newClientServerTest(t, h2, HandlerFunc(func(rw ResponseWriter, r *Request) { 2456 all, err := ioutil.ReadAll(r.Body) 2457 if err != nil { 2458 t.Fatalf("handler ReadAll: %v", err) 2459 } 2460 if len(all) != 0 { 2461 t.Errorf("handler got %d bytes; expected 0", len(all)) 2462 } 2463 rw.Header().Set("Content-Length", "0") 2464 })) 2465 defer cst.close() 2466 2467 req, err := NewRequest("POST", cst.ts.URL, strings.NewReader("")) 2468 if err != nil { 2469 t.Fatal(err) 2470 } 2471 req.ContentLength = 0 2472 2473 var resp [5]*Response 2474 for i := range resp { 2475 resp[i], err = cst.c.Do(req) 2476 if err != nil { 2477 t.Fatalf("client post #%d: %v", i, err) 2478 } 2479 } 2480 2481 for i := range resp { 2482 all, err := ioutil.ReadAll(resp[i].Body) 2483 if err != nil { 2484 t.Fatalf("req #%d: client ReadAll: %v", i, err) 2485 } 2486 if len(all) != 0 { 2487 t.Errorf("req #%d: client got %d bytes; expected 0", i, len(all)) 2488 } 2489 } 2490 } 2491 2492 func TestHandlerPanicNil_h1(t *testing.T) { testHandlerPanic(t, false, h1Mode, nil) } 2493 func TestHandlerPanicNil_h2(t *testing.T) { testHandlerPanic(t, false, h2Mode, nil) } 2494 2495 func TestHandlerPanic_h1(t *testing.T) { 2496 testHandlerPanic(t, false, h1Mode, "intentional death for testing") 2497 } 2498 func TestHandlerPanic_h2(t *testing.T) { 2499 testHandlerPanic(t, false, h2Mode, "intentional death for testing") 2500 } 2501 2502 func TestHandlerPanicWithHijack(t *testing.T) { 2503 // Only testing HTTP/1, and our http2 server doesn't support hijacking. 2504 testHandlerPanic(t, true, h1Mode, "intentional death for testing") 2505 } 2506 2507 func testHandlerPanic(t *testing.T, withHijack, h2 bool, panicValue interface{}) { 2508 defer afterTest(t) 2509 // Unlike the other tests that set the log output to ioutil.Discard 2510 // to quiet the output, this test uses a pipe. The pipe serves three 2511 // purposes: 2512 // 2513 // 1) The log.Print from the http server (generated by the caught 2514 // panic) will go to the pipe instead of stderr, making the 2515 // output quiet. 2516 // 2517 // 2) We read from the pipe to verify that the handler 2518 // actually caught the panic and logged something. 2519 // 2520 // 3) The blocking Read call prevents this TestHandlerPanic 2521 // function from exiting before the HTTP server handler 2522 // finishes crashing. If this text function exited too 2523 // early (and its defer log.SetOutput(os.Stderr) ran), 2524 // then the crash output could spill into the next test. 2525 pr, pw := io.Pipe() 2526 log.SetOutput(pw) 2527 defer log.SetOutput(os.Stderr) 2528 defer pw.Close() 2529 2530 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 2531 if withHijack { 2532 rwc, _, err := w.(Hijacker).Hijack() 2533 if err != nil { 2534 t.Logf("unexpected error: %v", err) 2535 } 2536 defer rwc.Close() 2537 } 2538 panic(panicValue) 2539 })) 2540 defer cst.close() 2541 2542 // Do a blocking read on the log output pipe so its logging 2543 // doesn't bleed into the next test. But wait only 5 seconds 2544 // for it. 2545 done := make(chan bool, 1) 2546 go func() { 2547 buf := make([]byte, 4<<10) 2548 _, err := pr.Read(buf) 2549 pr.Close() 2550 if err != nil && err != io.EOF { 2551 t.Error(err) 2552 } 2553 done <- true 2554 }() 2555 2556 _, err := cst.c.Get(cst.ts.URL) 2557 if err == nil { 2558 t.Logf("expected an error") 2559 } 2560 2561 if panicValue == nil { 2562 return 2563 } 2564 2565 select { 2566 case <-done: 2567 return 2568 case <-time.After(5 * time.Second): 2569 t.Fatal("expected server handler to log an error") 2570 } 2571 } 2572 2573 type terrorWriter struct{ t *testing.T } 2574 2575 func (w terrorWriter) Write(p []byte) (int, error) { 2576 w.t.Errorf("%s", p) 2577 return len(p), nil 2578 } 2579 2580 // Issue 16456: allow writing 0 bytes on hijacked conn to test hijack 2581 // without any log spam. 2582 func TestServerWriteHijackZeroBytes(t *testing.T) { 2583 defer afterTest(t) 2584 done := make(chan struct{}) 2585 ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) { 2586 defer close(done) 2587 w.(Flusher).Flush() 2588 conn, _, err := w.(Hijacker).Hijack() 2589 if err != nil { 2590 t.Errorf("Hijack: %v", err) 2591 return 2592 } 2593 defer conn.Close() 2594 _, err = w.Write(nil) 2595 if err != ErrHijacked { 2596 t.Errorf("Write error = %v; want ErrHijacked", err) 2597 } 2598 })) 2599 ts.Config.ErrorLog = log.New(terrorWriter{t}, "Unexpected write: ", 0) 2600 ts.Start() 2601 defer ts.Close() 2602 2603 c := ts.Client() 2604 res, err := c.Get(ts.URL) 2605 if err != nil { 2606 t.Fatal(err) 2607 } 2608 res.Body.Close() 2609 select { 2610 case <-done: 2611 case <-time.After(5 * time.Second): 2612 t.Fatal("timeout") 2613 } 2614 } 2615 2616 func TestServerNoDate_h1(t *testing.T) { testServerNoHeader(t, h1Mode, "Date") } 2617 func TestServerNoDate_h2(t *testing.T) { testServerNoHeader(t, h2Mode, "Date") } 2618 func TestServerNoContentType_h1(t *testing.T) { testServerNoHeader(t, h1Mode, "Content-Type") } 2619 func TestServerNoContentType_h2(t *testing.T) { testServerNoHeader(t, h2Mode, "Content-Type") } 2620 2621 func testServerNoHeader(t *testing.T, h2 bool, header string) { 2622 setParallel(t) 2623 defer afterTest(t) 2624 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 2625 w.Header()[header] = nil 2626 io.WriteString(w, "<html>foo</html>") // non-empty 2627 })) 2628 defer cst.close() 2629 res, err := cst.c.Get(cst.ts.URL) 2630 if err != nil { 2631 t.Fatal(err) 2632 } 2633 res.Body.Close() 2634 if got, ok := res.Header[header]; ok { 2635 t.Fatalf("Expected no %s header; got %q", header, got) 2636 } 2637 } 2638 2639 func TestStripPrefix(t *testing.T) { 2640 setParallel(t) 2641 defer afterTest(t) 2642 h := HandlerFunc(func(w ResponseWriter, r *Request) { 2643 w.Header().Set("X-Path", r.URL.Path) 2644 }) 2645 ts := httptest.NewServer(StripPrefix("/foo", h)) 2646 defer ts.Close() 2647 2648 c := ts.Client() 2649 2650 res, err := c.Get(ts.URL + "/foo/bar") 2651 if err != nil { 2652 t.Fatal(err) 2653 } 2654 if g, e := res.Header.Get("X-Path"), "/bar"; g != e { 2655 t.Errorf("test 1: got %s, want %s", g, e) 2656 } 2657 res.Body.Close() 2658 2659 res, err = Get(ts.URL + "/bar") 2660 if err != nil { 2661 t.Fatal(err) 2662 } 2663 if g, e := res.StatusCode, 404; g != e { 2664 t.Errorf("test 2: got status %v, want %v", g, e) 2665 } 2666 res.Body.Close() 2667 } 2668 2669 // https://golang.org/issue/18952. 2670 func TestStripPrefix_notModifyRequest(t *testing.T) { 2671 h := StripPrefix("/foo", NotFoundHandler()) 2672 req := httptest.NewRequest("GET", "/foo/bar", nil) 2673 h.ServeHTTP(httptest.NewRecorder(), req) 2674 if req.URL.Path != "/foo/bar" { 2675 t.Errorf("StripPrefix should not modify the provided Request, but it did") 2676 } 2677 } 2678 2679 func TestRequestLimit_h1(t *testing.T) { testRequestLimit(t, h1Mode) } 2680 func TestRequestLimit_h2(t *testing.T) { testRequestLimit(t, h2Mode) } 2681 func testRequestLimit(t *testing.T, h2 bool) { 2682 setParallel(t) 2683 defer afterTest(t) 2684 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 2685 t.Fatalf("didn't expect to get request in Handler") 2686 }), optQuietLog) 2687 defer cst.close() 2688 req, _ := NewRequest("GET", cst.ts.URL, nil) 2689 var bytesPerHeader = len("header12345: val12345\r\n") 2690 for i := 0; i < ((DefaultMaxHeaderBytes+4096)/bytesPerHeader)+1; i++ { 2691 req.Header.Set(fmt.Sprintf("header%05d", i), fmt.Sprintf("val%05d", i)) 2692 } 2693 res, err := cst.c.Do(req) 2694 if err != nil { 2695 // Some HTTP clients may fail on this undefined behavior (server replying and 2696 // closing the connection while the request is still being written), but 2697 // we do support it (at least currently), so we expect a response below. 2698 t.Fatalf("Do: %v", err) 2699 } 2700 defer res.Body.Close() 2701 if res.StatusCode != 431 { 2702 t.Fatalf("expected 431 response status; got: %d %s", res.StatusCode, res.Status) 2703 } 2704 } 2705 2706 type neverEnding byte 2707 2708 func (b neverEnding) Read(p []byte) (n int, err error) { 2709 for i := range p { 2710 p[i] = byte(b) 2711 } 2712 return len(p), nil 2713 } 2714 2715 type countReader struct { 2716 r io.Reader 2717 n *int64 2718 } 2719 2720 func (cr countReader) Read(p []byte) (n int, err error) { 2721 n, err = cr.r.Read(p) 2722 atomic.AddInt64(cr.n, int64(n)) 2723 return 2724 } 2725 2726 func TestRequestBodyLimit_h1(t *testing.T) { testRequestBodyLimit(t, h1Mode) } 2727 func TestRequestBodyLimit_h2(t *testing.T) { testRequestBodyLimit(t, h2Mode) } 2728 func testRequestBodyLimit(t *testing.T, h2 bool) { 2729 setParallel(t) 2730 defer afterTest(t) 2731 const limit = 1 << 20 2732 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 2733 r.Body = MaxBytesReader(w, r.Body, limit) 2734 n, err := io.Copy(ioutil.Discard, r.Body) 2735 if err == nil { 2736 t.Errorf("expected error from io.Copy") 2737 } 2738 if n != limit { 2739 t.Errorf("io.Copy = %d, want %d", n, limit) 2740 } 2741 })) 2742 defer cst.close() 2743 2744 nWritten := new(int64) 2745 req, _ := NewRequest("POST", cst.ts.URL, io.LimitReader(countReader{neverEnding('a'), nWritten}, limit*200)) 2746 2747 // Send the POST, but don't care it succeeds or not. The 2748 // remote side is going to reply and then close the TCP 2749 // connection, and HTTP doesn't really define if that's 2750 // allowed or not. Some HTTP clients will get the response 2751 // and some (like ours, currently) will complain that the 2752 // request write failed, without reading the response. 2753 // 2754 // But that's okay, since what we're really testing is that 2755 // the remote side hung up on us before we wrote too much. 2756 _, _ = cst.c.Do(req) 2757 2758 if atomic.LoadInt64(nWritten) > limit*100 { 2759 t.Errorf("handler restricted the request body to %d bytes, but client managed to write %d", 2760 limit, nWritten) 2761 } 2762 } 2763 2764 // TestClientWriteShutdown tests that if the client shuts down the write 2765 // side of their TCP connection, the server doesn't send a 400 Bad Request. 2766 func TestClientWriteShutdown(t *testing.T) { 2767 if runtime.GOOS == "plan9" { 2768 t.Skip("skipping test; see https://golang.org/issue/7237") 2769 } 2770 defer afterTest(t) 2771 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {})) 2772 defer ts.Close() 2773 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 2774 if err != nil { 2775 t.Fatalf("Dial: %v", err) 2776 } 2777 err = conn.(*net.TCPConn).CloseWrite() 2778 if err != nil { 2779 t.Fatalf("CloseWrite: %v", err) 2780 } 2781 donec := make(chan bool) 2782 go func() { 2783 defer close(donec) 2784 bs, err := ioutil.ReadAll(conn) 2785 if err != nil { 2786 t.Errorf("ReadAll: %v", err) 2787 } 2788 got := string(bs) 2789 if got != "" { 2790 t.Errorf("read %q from server; want nothing", got) 2791 } 2792 }() 2793 select { 2794 case <-donec: 2795 case <-time.After(10 * time.Second): 2796 t.Fatalf("timeout") 2797 } 2798 } 2799 2800 // Tests that chunked server responses that write 1 byte at a time are 2801 // buffered before chunk headers are added, not after chunk headers. 2802 func TestServerBufferedChunking(t *testing.T) { 2803 conn := new(testConn) 2804 conn.readBuf.Write([]byte("GET / HTTP/1.1\r\nHost: foo\r\n\r\n")) 2805 conn.closec = make(chan bool, 1) 2806 ls := &oneConnListener{conn} 2807 go Serve(ls, HandlerFunc(func(rw ResponseWriter, req *Request) { 2808 rw.(Flusher).Flush() // force the Header to be sent, in chunking mode, not counting the length 2809 rw.Write([]byte{'x'}) 2810 rw.Write([]byte{'y'}) 2811 rw.Write([]byte{'z'}) 2812 })) 2813 <-conn.closec 2814 if !bytes.HasSuffix(conn.writeBuf.Bytes(), []byte("\r\n\r\n3\r\nxyz\r\n0\r\n\r\n")) { 2815 t.Errorf("response didn't end with a single 3 byte 'xyz' chunk; got:\n%q", 2816 conn.writeBuf.Bytes()) 2817 } 2818 } 2819 2820 // Tests that the server flushes its response headers out when it's 2821 // ignoring the response body and waits a bit before forcefully 2822 // closing the TCP connection, causing the client to get a RST. 2823 // See https://golang.org/issue/3595 2824 func TestServerGracefulClose(t *testing.T) { 2825 setParallel(t) 2826 defer afterTest(t) 2827 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { 2828 Error(w, "bye", StatusUnauthorized) 2829 })) 2830 defer ts.Close() 2831 2832 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 2833 if err != nil { 2834 t.Fatal(err) 2835 } 2836 defer conn.Close() 2837 const bodySize = 5 << 20 2838 req := []byte(fmt.Sprintf("POST / HTTP/1.1\r\nHost: foo.com\r\nContent-Length: %d\r\n\r\n", bodySize)) 2839 for i := 0; i < bodySize; i++ { 2840 req = append(req, 'x') 2841 } 2842 writeErr := make(chan error) 2843 go func() { 2844 _, err := conn.Write(req) 2845 writeErr <- err 2846 }() 2847 br := bufio.NewReader(conn) 2848 lineNum := 0 2849 for { 2850 line, err := br.ReadString('\n') 2851 if err == io.EOF { 2852 break 2853 } 2854 if err != nil { 2855 t.Fatalf("ReadLine: %v", err) 2856 } 2857 lineNum++ 2858 if lineNum == 1 && !strings.Contains(line, "401 Unauthorized") { 2859 t.Errorf("Response line = %q; want a 401", line) 2860 } 2861 } 2862 // Wait for write to finish. This is a broken pipe on both 2863 // Darwin and Linux, but checking this isn't the point of 2864 // the test. 2865 <-writeErr 2866 } 2867 2868 func TestCaseSensitiveMethod_h1(t *testing.T) { testCaseSensitiveMethod(t, h1Mode) } 2869 func TestCaseSensitiveMethod_h2(t *testing.T) { testCaseSensitiveMethod(t, h2Mode) } 2870 func testCaseSensitiveMethod(t *testing.T, h2 bool) { 2871 defer afterTest(t) 2872 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 2873 if r.Method != "get" { 2874 t.Errorf(`Got method %q; want "get"`, r.Method) 2875 } 2876 })) 2877 defer cst.close() 2878 req, _ := NewRequest("get", cst.ts.URL, nil) 2879 res, err := cst.c.Do(req) 2880 if err != nil { 2881 t.Error(err) 2882 return 2883 } 2884 2885 res.Body.Close() 2886 } 2887 2888 // TestContentLengthZero tests that for both an HTTP/1.0 and HTTP/1.1 2889 // request (both keep-alive), when a Handler never writes any 2890 // response, the net/http package adds a "Content-Length: 0" response 2891 // header. 2892 func TestContentLengthZero(t *testing.T) { 2893 ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) {})) 2894 defer ts.Close() 2895 2896 for _, version := range []string{"HTTP/1.0", "HTTP/1.1"} { 2897 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 2898 if err != nil { 2899 t.Fatalf("error dialing: %v", err) 2900 } 2901 _, err = fmt.Fprintf(conn, "GET / %v\r\nConnection: keep-alive\r\nHost: foo\r\n\r\n", version) 2902 if err != nil { 2903 t.Fatalf("error writing: %v", err) 2904 } 2905 req, _ := NewRequest("GET", "/", nil) 2906 res, err := ReadResponse(bufio.NewReader(conn), req) 2907 if err != nil { 2908 t.Fatalf("error reading response: %v", err) 2909 } 2910 if te := res.TransferEncoding; len(te) > 0 { 2911 t.Errorf("For version %q, Transfer-Encoding = %q; want none", version, te) 2912 } 2913 if cl := res.ContentLength; cl != 0 { 2914 t.Errorf("For version %q, Content-Length = %v; want 0", version, cl) 2915 } 2916 conn.Close() 2917 } 2918 } 2919 2920 func TestCloseNotifier(t *testing.T) { 2921 defer afterTest(t) 2922 gotReq := make(chan bool, 1) 2923 sawClose := make(chan bool, 1) 2924 ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) { 2925 gotReq <- true 2926 cc := rw.(CloseNotifier).CloseNotify() 2927 <-cc 2928 sawClose <- true 2929 })) 2930 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 2931 if err != nil { 2932 t.Fatalf("error dialing: %v", err) 2933 } 2934 diec := make(chan bool) 2935 go func() { 2936 _, err = fmt.Fprintf(conn, "GET / HTTP/1.1\r\nConnection: keep-alive\r\nHost: foo\r\n\r\n") 2937 if err != nil { 2938 t.Error(err) 2939 return 2940 } 2941 <-diec 2942 conn.Close() 2943 }() 2944 For: 2945 for { 2946 select { 2947 case <-gotReq: 2948 diec <- true 2949 case <-sawClose: 2950 break For 2951 case <-time.After(5 * time.Second): 2952 t.Fatal("timeout") 2953 } 2954 } 2955 ts.Close() 2956 } 2957 2958 // Tests that a pipelined request causes the first request's Handler's CloseNotify 2959 // channel to fire. Previously it deadlocked. 2960 // 2961 // Issue 13165 2962 func TestCloseNotifierPipelined(t *testing.T) { 2963 defer afterTest(t) 2964 gotReq := make(chan bool, 2) 2965 sawClose := make(chan bool, 2) 2966 ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) { 2967 gotReq <- true 2968 cc := rw.(CloseNotifier).CloseNotify() 2969 <-cc 2970 sawClose <- true 2971 })) 2972 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 2973 if err != nil { 2974 t.Fatalf("error dialing: %v", err) 2975 } 2976 diec := make(chan bool, 1) 2977 go func() { 2978 const req = "GET / HTTP/1.1\r\nConnection: keep-alive\r\nHost: foo\r\n\r\n" 2979 _, err = io.WriteString(conn, req+req) // two requests 2980 if err != nil { 2981 t.Error(err) 2982 return 2983 } 2984 <-diec 2985 conn.Close() 2986 }() 2987 reqs := 0 2988 closes := 0 2989 For: 2990 for { 2991 select { 2992 case <-gotReq: 2993 reqs++ 2994 if reqs > 2 { 2995 t.Fatal("too many requests") 2996 } else if reqs > 1 { 2997 diec <- true 2998 } 2999 case <-sawClose: 3000 closes++ 3001 if closes > 1 { 3002 break For 3003 } 3004 case <-time.After(5 * time.Second): 3005 ts.CloseClientConnections() 3006 t.Fatal("timeout") 3007 } 3008 } 3009 ts.Close() 3010 } 3011 3012 func TestCloseNotifierChanLeak(t *testing.T) { 3013 defer afterTest(t) 3014 req := reqBytes("GET / HTTP/1.0\nHost: golang.org") 3015 for i := 0; i < 20; i++ { 3016 var output bytes.Buffer 3017 conn := &rwTestConn{ 3018 Reader: bytes.NewReader(req), 3019 Writer: &output, 3020 closec: make(chan bool, 1), 3021 } 3022 ln := &oneConnListener{conn: conn} 3023 handler := HandlerFunc(func(rw ResponseWriter, r *Request) { 3024 // Ignore the return value and never read from 3025 // it, testing that we don't leak goroutines 3026 // on the sending side: 3027 _ = rw.(CloseNotifier).CloseNotify() 3028 }) 3029 go Serve(ln, handler) 3030 <-conn.closec 3031 } 3032 } 3033 3034 // Tests that we can use CloseNotifier in one request, and later call Hijack 3035 // on a second request on the same connection. 3036 // 3037 // It also tests that the connReader stitches together its background 3038 // 1-byte read for CloseNotifier when CloseNotifier doesn't fire with 3039 // the rest of the second HTTP later. 3040 // 3041 // Issue 9763. 3042 // HTTP/1-only test. (http2 doesn't have Hijack) 3043 func TestHijackAfterCloseNotifier(t *testing.T) { 3044 defer afterTest(t) 3045 script := make(chan string, 2) 3046 script <- "closenotify" 3047 script <- "hijack" 3048 close(script) 3049 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { 3050 plan := <-script 3051 switch plan { 3052 default: 3053 panic("bogus plan; too many requests") 3054 case "closenotify": 3055 w.(CloseNotifier).CloseNotify() // discard result 3056 w.Header().Set("X-Addr", r.RemoteAddr) 3057 case "hijack": 3058 c, _, err := w.(Hijacker).Hijack() 3059 if err != nil { 3060 t.Errorf("Hijack in Handler: %v", err) 3061 return 3062 } 3063 if _, ok := c.(*net.TCPConn); !ok { 3064 // Verify it's not wrapped in some type. 3065 // Not strictly a go1 compat issue, but in practice it probably is. 3066 t.Errorf("type of hijacked conn is %T; want *net.TCPConn", c) 3067 } 3068 fmt.Fprintf(c, "HTTP/1.0 200 OK\r\nX-Addr: %v\r\nContent-Length: 0\r\n\r\n", r.RemoteAddr) 3069 c.Close() 3070 return 3071 } 3072 })) 3073 defer ts.Close() 3074 res1, err := Get(ts.URL) 3075 if err != nil { 3076 log.Fatal(err) 3077 } 3078 res2, err := Get(ts.URL) 3079 if err != nil { 3080 log.Fatal(err) 3081 } 3082 addr1 := res1.Header.Get("X-Addr") 3083 addr2 := res2.Header.Get("X-Addr") 3084 if addr1 == "" || addr1 != addr2 { 3085 t.Errorf("addr1, addr2 = %q, %q; want same", addr1, addr2) 3086 } 3087 } 3088 3089 func TestHijackBeforeRequestBodyRead(t *testing.T) { 3090 setParallel(t) 3091 defer afterTest(t) 3092 var requestBody = bytes.Repeat([]byte("a"), 1<<20) 3093 bodyOkay := make(chan bool, 1) 3094 gotCloseNotify := make(chan bool, 1) 3095 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { 3096 defer close(bodyOkay) // caller will read false if nothing else 3097 3098 reqBody := r.Body 3099 r.Body = nil // to test that server.go doesn't use this value. 3100 3101 gone := w.(CloseNotifier).CloseNotify() 3102 slurp, err := ioutil.ReadAll(reqBody) 3103 if err != nil { 3104 t.Errorf("Body read: %v", err) 3105 return 3106 } 3107 if len(slurp) != len(requestBody) { 3108 t.Errorf("Backend read %d request body bytes; want %d", len(slurp), len(requestBody)) 3109 return 3110 } 3111 if !bytes.Equal(slurp, requestBody) { 3112 t.Error("Backend read wrong request body.") // 1MB; omitting details 3113 return 3114 } 3115 bodyOkay <- true 3116 select { 3117 case <-gone: 3118 gotCloseNotify <- true 3119 case <-time.After(5 * time.Second): 3120 gotCloseNotify <- false 3121 } 3122 })) 3123 defer ts.Close() 3124 3125 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 3126 if err != nil { 3127 t.Fatal(err) 3128 } 3129 defer conn.Close() 3130 3131 fmt.Fprintf(conn, "POST / HTTP/1.1\r\nHost: foo\r\nContent-Length: %d\r\n\r\n%s", 3132 len(requestBody), requestBody) 3133 if !<-bodyOkay { 3134 // already failed. 3135 return 3136 } 3137 conn.Close() 3138 if !<-gotCloseNotify { 3139 t.Error("timeout waiting for CloseNotify") 3140 } 3141 } 3142 3143 func TestOptions(t *testing.T) { 3144 uric := make(chan string, 2) // only expect 1, but leave space for 2 3145 mux := NewServeMux() 3146 mux.HandleFunc("/", func(w ResponseWriter, r *Request) { 3147 uric <- r.RequestURI 3148 }) 3149 ts := httptest.NewServer(mux) 3150 defer ts.Close() 3151 3152 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 3153 if err != nil { 3154 t.Fatal(err) 3155 } 3156 defer conn.Close() 3157 3158 // An OPTIONS * request should succeed. 3159 _, err = conn.Write([]byte("OPTIONS * HTTP/1.1\r\nHost: foo.com\r\n\r\n")) 3160 if err != nil { 3161 t.Fatal(err) 3162 } 3163 br := bufio.NewReader(conn) 3164 res, err := ReadResponse(br, &Request{Method: "OPTIONS"}) 3165 if err != nil { 3166 t.Fatal(err) 3167 } 3168 if res.StatusCode != 200 { 3169 t.Errorf("Got non-200 response to OPTIONS *: %#v", res) 3170 } 3171 3172 // A GET * request on a ServeMux should fail. 3173 _, err = conn.Write([]byte("GET * HTTP/1.1\r\nHost: foo.com\r\n\r\n")) 3174 if err != nil { 3175 t.Fatal(err) 3176 } 3177 res, err = ReadResponse(br, &Request{Method: "GET"}) 3178 if err != nil { 3179 t.Fatal(err) 3180 } 3181 if res.StatusCode != 400 { 3182 t.Errorf("Got non-400 response to GET *: %#v", res) 3183 } 3184 3185 res, err = Get(ts.URL + "/second") 3186 if err != nil { 3187 t.Fatal(err) 3188 } 3189 res.Body.Close() 3190 if got := <-uric; got != "/second" { 3191 t.Errorf("Handler saw request for %q; want /second", got) 3192 } 3193 } 3194 3195 // Tests regarding the ordering of Write, WriteHeader, Header, and 3196 // Flush calls. In Go 1.0, rw.WriteHeader immediately flushed the 3197 // (*response).header to the wire. In Go 1.1, the actual wire flush is 3198 // delayed, so we could maybe tack on a Content-Length and better 3199 // Content-Type after we see more (or all) of the output. To preserve 3200 // compatibility with Go 1, we need to be careful to track which 3201 // headers were live at the time of WriteHeader, so we write the same 3202 // ones, even if the handler modifies them (~erroneously) after the 3203 // first Write. 3204 func TestHeaderToWire(t *testing.T) { 3205 tests := []struct { 3206 name string 3207 handler func(ResponseWriter, *Request) 3208 check func(output string) error 3209 }{ 3210 { 3211 name: "write without Header", 3212 handler: func(rw ResponseWriter, r *Request) { 3213 rw.Write([]byte("hello world")) 3214 }, 3215 check: func(got string) error { 3216 if !strings.Contains(got, "Content-Length:") { 3217 return errors.New("no content-length") 3218 } 3219 if !strings.Contains(got, "Content-Type: text/plain") { 3220 return errors.New("no content-type") 3221 } 3222 return nil 3223 }, 3224 }, 3225 { 3226 name: "Header mutation before write", 3227 handler: func(rw ResponseWriter, r *Request) { 3228 h := rw.Header() 3229 h.Set("Content-Type", "some/type") 3230 rw.Write([]byte("hello world")) 3231 h.Set("Too-Late", "bogus") 3232 }, 3233 check: func(got string) error { 3234 if !strings.Contains(got, "Content-Length:") { 3235 return errors.New("no content-length") 3236 } 3237 if !strings.Contains(got, "Content-Type: some/type") { 3238 return errors.New("wrong content-type") 3239 } 3240 if strings.Contains(got, "Too-Late") { 3241 return errors.New("don't want too-late header") 3242 } 3243 return nil 3244 }, 3245 }, 3246 { 3247 name: "write then useless Header mutation", 3248 handler: func(rw ResponseWriter, r *Request) { 3249 rw.Write([]byte("hello world")) 3250 rw.Header().Set("Too-Late", "Write already wrote headers") 3251 }, 3252 check: func(got string) error { 3253 if strings.Contains(got, "Too-Late") { 3254 return errors.New("header appeared from after WriteHeader") 3255 } 3256 return nil 3257 }, 3258 }, 3259 { 3260 name: "flush then write", 3261 handler: func(rw ResponseWriter, r *Request) { 3262 rw.(Flusher).Flush() 3263 rw.Write([]byte("post-flush")) 3264 rw.Header().Set("Too-Late", "Write already wrote headers") 3265 }, 3266 check: func(got string) error { 3267 if !strings.Contains(got, "Transfer-Encoding: chunked") { 3268 return errors.New("not chunked") 3269 } 3270 if strings.Contains(got, "Too-Late") { 3271 return errors.New("header appeared from after WriteHeader") 3272 } 3273 return nil 3274 }, 3275 }, 3276 { 3277 name: "header then flush", 3278 handler: func(rw ResponseWriter, r *Request) { 3279 rw.Header().Set("Content-Type", "some/type") 3280 rw.(Flusher).Flush() 3281 rw.Write([]byte("post-flush")) 3282 rw.Header().Set("Too-Late", "Write already wrote headers") 3283 }, 3284 check: func(got string) error { 3285 if !strings.Contains(got, "Transfer-Encoding: chunked") { 3286 return errors.New("not chunked") 3287 } 3288 if strings.Contains(got, "Too-Late") { 3289 return errors.New("header appeared from after WriteHeader") 3290 } 3291 if !strings.Contains(got, "Content-Type: some/type") { 3292 return errors.New("wrong content-type") 3293 } 3294 return nil 3295 }, 3296 }, 3297 { 3298 name: "sniff-on-first-write content-type", 3299 handler: func(rw ResponseWriter, r *Request) { 3300 rw.Write([]byte("<html><head></head><body>some html</body></html>")) 3301 rw.Header().Set("Content-Type", "x/wrong") 3302 }, 3303 check: func(got string) error { 3304 if !strings.Contains(got, "Content-Type: text/html") { 3305 return errors.New("wrong content-type; want html") 3306 } 3307 return nil 3308 }, 3309 }, 3310 { 3311 name: "explicit content-type wins", 3312 handler: func(rw ResponseWriter, r *Request) { 3313 rw.Header().Set("Content-Type", "some/type") 3314 rw.Write([]byte("<html><head></head><body>some html</body></html>")) 3315 }, 3316 check: func(got string) error { 3317 if !strings.Contains(got, "Content-Type: some/type") { 3318 return errors.New("wrong content-type; want html") 3319 } 3320 return nil 3321 }, 3322 }, 3323 { 3324 name: "empty handler", 3325 handler: func(rw ResponseWriter, r *Request) { 3326 }, 3327 check: func(got string) error { 3328 if !strings.Contains(got, "Content-Type: text/plain") { 3329 return errors.New("wrong content-type; want text/plain") 3330 } 3331 if !strings.Contains(got, "Content-Length: 0") { 3332 return errors.New("want 0 content-length") 3333 } 3334 return nil 3335 }, 3336 }, 3337 { 3338 name: "only Header, no write", 3339 handler: func(rw ResponseWriter, r *Request) { 3340 rw.Header().Set("Some-Header", "some-value") 3341 }, 3342 check: func(got string) error { 3343 if !strings.Contains(got, "Some-Header") { 3344 return errors.New("didn't get header") 3345 } 3346 return nil 3347 }, 3348 }, 3349 { 3350 name: "WriteHeader call", 3351 handler: func(rw ResponseWriter, r *Request) { 3352 rw.WriteHeader(404) 3353 rw.Header().Set("Too-Late", "some-value") 3354 }, 3355 check: func(got string) error { 3356 if !strings.Contains(got, "404") { 3357 return errors.New("wrong status") 3358 } 3359 if strings.Contains(got, "Too-Late") { 3360 return errors.New("shouldn't have seen Too-Late") 3361 } 3362 return nil 3363 }, 3364 }, 3365 } 3366 for _, tc := range tests { 3367 ht := newHandlerTest(HandlerFunc(tc.handler)) 3368 got := ht.rawResponse("GET / HTTP/1.1\nHost: golang.org") 3369 if err := tc.check(got); err != nil { 3370 t.Errorf("%s: %v\nGot response:\n%s", tc.name, err, got) 3371 } 3372 } 3373 } 3374 3375 // goTimeout runs f, failing t if f takes more than ns to complete. 3376 func goTimeout(t *testing.T, d time.Duration, f func()) { 3377 ch := make(chan bool, 2) 3378 timer := time.AfterFunc(d, func() { 3379 t.Errorf("Timeout expired after %v", d) 3380 ch <- true 3381 }) 3382 defer timer.Stop() 3383 go func() { 3384 defer func() { ch <- true }() 3385 f() 3386 }() 3387 <-ch 3388 } 3389 3390 type errorListener struct { 3391 errs []error 3392 } 3393 3394 func (l *errorListener) Accept() (c net.Conn, err error) { 3395 if len(l.errs) == 0 { 3396 return nil, io.EOF 3397 } 3398 err = l.errs[0] 3399 l.errs = l.errs[1:] 3400 return 3401 } 3402 3403 func (l *errorListener) Close() error { 3404 return nil 3405 } 3406 3407 func (l *errorListener) Addr() net.Addr { 3408 return dummyAddr("test-address") 3409 } 3410 3411 func TestAcceptMaxFds(t *testing.T) { 3412 setParallel(t) 3413 3414 ln := &errorListener{[]error{ 3415 &net.OpError{ 3416 Op: "accept", 3417 Err: syscall.EMFILE, 3418 }}} 3419 server := &Server{ 3420 Handler: HandlerFunc(HandlerFunc(func(ResponseWriter, *Request) {})), 3421 ErrorLog: log.New(ioutil.Discard, "", 0), // noisy otherwise 3422 } 3423 err := server.Serve(ln) 3424 if err != io.EOF { 3425 t.Errorf("got error %v, want EOF", err) 3426 } 3427 } 3428 3429 func TestWriteAfterHijack(t *testing.T) { 3430 req := reqBytes("GET / HTTP/1.1\nHost: golang.org") 3431 var buf bytes.Buffer 3432 wrotec := make(chan bool, 1) 3433 conn := &rwTestConn{ 3434 Reader: bytes.NewReader(req), 3435 Writer: &buf, 3436 closec: make(chan bool, 1), 3437 } 3438 handler := HandlerFunc(func(rw ResponseWriter, r *Request) { 3439 conn, bufrw, err := rw.(Hijacker).Hijack() 3440 if err != nil { 3441 t.Error(err) 3442 return 3443 } 3444 go func() { 3445 bufrw.Write([]byte("[hijack-to-bufw]")) 3446 bufrw.Flush() 3447 conn.Write([]byte("[hijack-to-conn]")) 3448 conn.Close() 3449 wrotec <- true 3450 }() 3451 }) 3452 ln := &oneConnListener{conn: conn} 3453 go Serve(ln, handler) 3454 <-conn.closec 3455 <-wrotec 3456 if g, w := buf.String(), "[hijack-to-bufw][hijack-to-conn]"; g != w { 3457 t.Errorf("wrote %q; want %q", g, w) 3458 } 3459 } 3460 3461 func TestDoubleHijack(t *testing.T) { 3462 req := reqBytes("GET / HTTP/1.1\nHost: golang.org") 3463 var buf bytes.Buffer 3464 conn := &rwTestConn{ 3465 Reader: bytes.NewReader(req), 3466 Writer: &buf, 3467 closec: make(chan bool, 1), 3468 } 3469 handler := HandlerFunc(func(rw ResponseWriter, r *Request) { 3470 conn, _, err := rw.(Hijacker).Hijack() 3471 if err != nil { 3472 t.Error(err) 3473 return 3474 } 3475 _, _, err = rw.(Hijacker).Hijack() 3476 if err == nil { 3477 t.Errorf("got err = nil; want err != nil") 3478 } 3479 conn.Close() 3480 }) 3481 ln := &oneConnListener{conn: conn} 3482 go Serve(ln, handler) 3483 <-conn.closec 3484 } 3485 3486 // https://golang.org/issue/5955 3487 // Note that this does not test the "request too large" 3488 // exit path from the http server. This is intentional; 3489 // not sending Connection: close is just a minor wire 3490 // optimization and is pointless if dealing with a 3491 // badly behaved client. 3492 func TestHTTP10ConnectionHeader(t *testing.T) { 3493 defer afterTest(t) 3494 3495 mux := NewServeMux() 3496 mux.Handle("/", HandlerFunc(func(ResponseWriter, *Request) {})) 3497 ts := httptest.NewServer(mux) 3498 defer ts.Close() 3499 3500 // net/http uses HTTP/1.1 for requests, so write requests manually 3501 tests := []struct { 3502 req string // raw http request 3503 expect []string // expected Connection header(s) 3504 }{ 3505 { 3506 req: "GET / HTTP/1.0\r\n\r\n", 3507 expect: nil, 3508 }, 3509 { 3510 req: "OPTIONS * HTTP/1.0\r\n\r\n", 3511 expect: nil, 3512 }, 3513 { 3514 req: "GET / HTTP/1.0\r\nConnection: keep-alive\r\n\r\n", 3515 expect: []string{"keep-alive"}, 3516 }, 3517 } 3518 3519 for _, tt := range tests { 3520 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 3521 if err != nil { 3522 t.Fatal("dial err:", err) 3523 } 3524 3525 _, err = fmt.Fprint(conn, tt.req) 3526 if err != nil { 3527 t.Fatal("conn write err:", err) 3528 } 3529 3530 resp, err := ReadResponse(bufio.NewReader(conn), &Request{Method: "GET"}) 3531 if err != nil { 3532 t.Fatal("ReadResponse err:", err) 3533 } 3534 conn.Close() 3535 resp.Body.Close() 3536 3537 got := resp.Header["Connection"] 3538 if !reflect.DeepEqual(got, tt.expect) { 3539 t.Errorf("wrong Connection headers for request %q. Got %q expect %q", tt.req, got, tt.expect) 3540 } 3541 } 3542 } 3543 3544 // See golang.org/issue/5660 3545 func TestServerReaderFromOrder_h1(t *testing.T) { testServerReaderFromOrder(t, h1Mode) } 3546 func TestServerReaderFromOrder_h2(t *testing.T) { testServerReaderFromOrder(t, h2Mode) } 3547 func testServerReaderFromOrder(t *testing.T, h2 bool) { 3548 setParallel(t) 3549 defer afterTest(t) 3550 pr, pw := io.Pipe() 3551 const size = 3 << 20 3552 cst := newClientServerTest(t, h2, HandlerFunc(func(rw ResponseWriter, req *Request) { 3553 rw.Header().Set("Content-Type", "text/plain") // prevent sniffing path 3554 done := make(chan bool) 3555 go func() { 3556 io.Copy(rw, pr) 3557 close(done) 3558 }() 3559 time.Sleep(25 * time.Millisecond) // give Copy a chance to break things 3560 n, err := io.Copy(ioutil.Discard, req.Body) 3561 if err != nil { 3562 t.Errorf("handler Copy: %v", err) 3563 return 3564 } 3565 if n != size { 3566 t.Errorf("handler Copy = %d; want %d", n, size) 3567 } 3568 pw.Write([]byte("hi")) 3569 pw.Close() 3570 <-done 3571 })) 3572 defer cst.close() 3573 3574 req, err := NewRequest("POST", cst.ts.URL, io.LimitReader(neverEnding('a'), size)) 3575 if err != nil { 3576 t.Fatal(err) 3577 } 3578 res, err := cst.c.Do(req) 3579 if err != nil { 3580 t.Fatal(err) 3581 } 3582 all, err := ioutil.ReadAll(res.Body) 3583 if err != nil { 3584 t.Fatal(err) 3585 } 3586 res.Body.Close() 3587 if string(all) != "hi" { 3588 t.Errorf("Body = %q; want hi", all) 3589 } 3590 } 3591 3592 // Issue 6157, Issue 6685 3593 func TestCodesPreventingContentTypeAndBody(t *testing.T) { 3594 for _, code := range []int{StatusNotModified, StatusNoContent, StatusContinue} { 3595 ht := newHandlerTest(HandlerFunc(func(w ResponseWriter, r *Request) { 3596 if r.URL.Path == "/header" { 3597 w.Header().Set("Content-Length", "123") 3598 } 3599 w.WriteHeader(code) 3600 if r.URL.Path == "/more" { 3601 w.Write([]byte("stuff")) 3602 } 3603 })) 3604 for _, req := range []string{ 3605 "GET / HTTP/1.0", 3606 "GET /header HTTP/1.0", 3607 "GET /more HTTP/1.0", 3608 "GET / HTTP/1.1\nHost: foo", 3609 "GET /header HTTP/1.1\nHost: foo", 3610 "GET /more HTTP/1.1\nHost: foo", 3611 } { 3612 got := ht.rawResponse(req) 3613 wantStatus := fmt.Sprintf("%d %s", code, StatusText(code)) 3614 if !strings.Contains(got, wantStatus) { 3615 t.Errorf("Code %d: Wanted %q Modified for %q: %s", code, wantStatus, req, got) 3616 } else if strings.Contains(got, "Content-Length") { 3617 t.Errorf("Code %d: Got a Content-Length from %q: %s", code, req, got) 3618 } else if strings.Contains(got, "stuff") { 3619 t.Errorf("Code %d: Response contains a body from %q: %s", code, req, got) 3620 } 3621 } 3622 } 3623 } 3624 3625 func TestContentTypeOkayOn204(t *testing.T) { 3626 ht := newHandlerTest(HandlerFunc(func(w ResponseWriter, r *Request) { 3627 w.Header().Set("Content-Length", "123") // suppressed 3628 w.Header().Set("Content-Type", "foo/bar") 3629 w.WriteHeader(204) 3630 })) 3631 got := ht.rawResponse("GET / HTTP/1.1\nHost: foo") 3632 if !strings.Contains(got, "Content-Type: foo/bar") { 3633 t.Errorf("Response = %q; want Content-Type: foo/bar", got) 3634 } 3635 if strings.Contains(got, "Content-Length: 123") { 3636 t.Errorf("Response = %q; don't want a Content-Length", got) 3637 } 3638 } 3639 3640 // Issue 6995 3641 // A server Handler can receive a Request, and then turn around and 3642 // give a copy of that Request.Body out to the Transport (e.g. any 3643 // proxy). So then two people own that Request.Body (both the server 3644 // and the http client), and both think they can close it on failure. 3645 // Therefore, all incoming server requests Bodies need to be thread-safe. 3646 func TestTransportAndServerSharedBodyRace_h1(t *testing.T) { 3647 testTransportAndServerSharedBodyRace(t, h1Mode) 3648 } 3649 func TestTransportAndServerSharedBodyRace_h2(t *testing.T) { 3650 testTransportAndServerSharedBodyRace(t, h2Mode) 3651 } 3652 func testTransportAndServerSharedBodyRace(t *testing.T, h2 bool) { 3653 setParallel(t) 3654 defer afterTest(t) 3655 3656 const bodySize = 1 << 20 3657 3658 // errorf is like t.Errorf, but also writes to println. When 3659 // this test fails, it hangs. This helps debugging and I've 3660 // added this enough times "temporarily". It now gets added 3661 // full time. 3662 errorf := func(format string, args ...interface{}) { 3663 v := fmt.Sprintf(format, args...) 3664 println(v) 3665 t.Error(v) 3666 } 3667 3668 unblockBackend := make(chan bool) 3669 backend := newClientServerTest(t, h2, HandlerFunc(func(rw ResponseWriter, req *Request) { 3670 gone := rw.(CloseNotifier).CloseNotify() 3671 didCopy := make(chan interface{}) 3672 go func() { 3673 n, err := io.CopyN(rw, req.Body, bodySize) 3674 didCopy <- []interface{}{n, err} 3675 }() 3676 isGone := false 3677 Loop: 3678 for { 3679 select { 3680 case <-didCopy: 3681 break Loop 3682 case <-gone: 3683 isGone = true 3684 case <-time.After(time.Second): 3685 println("1 second passes in backend, proxygone=", isGone) 3686 } 3687 } 3688 <-unblockBackend 3689 })) 3690 var quitTimer *time.Timer 3691 defer func() { quitTimer.Stop() }() 3692 defer backend.close() 3693 3694 backendRespc := make(chan *Response, 1) 3695 var proxy *clientServerTest 3696 proxy = newClientServerTest(t, h2, HandlerFunc(func(rw ResponseWriter, req *Request) { 3697 req2, _ := NewRequest("POST", backend.ts.URL, req.Body) 3698 req2.ContentLength = bodySize 3699 cancel := make(chan struct{}) 3700 req2.Cancel = cancel 3701 3702 bresp, err := proxy.c.Do(req2) 3703 if err != nil { 3704 errorf("Proxy outbound request: %v", err) 3705 return 3706 } 3707 _, err = io.CopyN(ioutil.Discard, bresp.Body, bodySize/2) 3708 if err != nil { 3709 errorf("Proxy copy error: %v", err) 3710 return 3711 } 3712 backendRespc <- bresp // to close later 3713 3714 // Try to cause a race: Both the Transport and the proxy handler's Server 3715 // will try to read/close req.Body (aka req2.Body) 3716 if h2 { 3717 close(cancel) 3718 } else { 3719 proxy.c.Transport.(*Transport).CancelRequest(req2) 3720 } 3721 rw.Write([]byte("OK")) 3722 })) 3723 defer proxy.close() 3724 defer func() { 3725 // Before we shut down our two httptest.Servers, start a timer. 3726 // We choose 7 seconds because httptest.Server starts logging 3727 // warnings to stderr at 5 seconds. If we don't disarm this bomb 3728 // in 7 seconds (after the two httptest.Server.Close calls above), 3729 // then we explode with stacks. 3730 quitTimer = time.AfterFunc(7*time.Second, func() { 3731 debug.SetTraceback("ALL") 3732 stacks := make([]byte, 1<<20) 3733 stacks = stacks[:runtime.Stack(stacks, true)] 3734 fmt.Fprintf(os.Stderr, "%s", stacks) 3735 log.Fatalf("Timeout.") 3736 }) 3737 }() 3738 3739 defer close(unblockBackend) 3740 req, _ := NewRequest("POST", proxy.ts.URL, io.LimitReader(neverEnding('a'), bodySize)) 3741 res, err := proxy.c.Do(req) 3742 if err != nil { 3743 t.Fatalf("Original request: %v", err) 3744 } 3745 3746 // Cleanup, so we don't leak goroutines. 3747 res.Body.Close() 3748 select { 3749 case res := <-backendRespc: 3750 res.Body.Close() 3751 default: 3752 // We failed earlier. (e.g. on proxy.c.Do(req2)) 3753 } 3754 } 3755 3756 // Test that a hanging Request.Body.Read from another goroutine can't 3757 // cause the Handler goroutine's Request.Body.Close to block. 3758 // See issue 7121. 3759 func TestRequestBodyCloseDoesntBlock(t *testing.T) { 3760 if testing.Short() { 3761 t.Skip("skipping in -short mode") 3762 } 3763 defer afterTest(t) 3764 3765 readErrCh := make(chan error, 1) 3766 errCh := make(chan error, 2) 3767 3768 server := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) { 3769 go func(body io.Reader) { 3770 _, err := body.Read(make([]byte, 100)) 3771 readErrCh <- err 3772 }(req.Body) 3773 time.Sleep(500 * time.Millisecond) 3774 })) 3775 defer server.Close() 3776 3777 closeConn := make(chan bool) 3778 defer close(closeConn) 3779 go func() { 3780 conn, err := net.Dial("tcp", server.Listener.Addr().String()) 3781 if err != nil { 3782 errCh <- err 3783 return 3784 } 3785 defer conn.Close() 3786 _, err = conn.Write([]byte("POST / HTTP/1.1\r\nConnection: close\r\nHost: foo\r\nContent-Length: 100000\r\n\r\n")) 3787 if err != nil { 3788 errCh <- err 3789 return 3790 } 3791 // And now just block, making the server block on our 3792 // 100000 bytes of body that will never arrive. 3793 <-closeConn 3794 }() 3795 select { 3796 case err := <-readErrCh: 3797 if err == nil { 3798 t.Error("Read was nil. Expected error.") 3799 } 3800 case err := <-errCh: 3801 t.Error(err) 3802 case <-time.After(5 * time.Second): 3803 t.Error("timeout") 3804 } 3805 } 3806 3807 // test that ResponseWriter implements io.stringWriter. 3808 func TestResponseWriterWriteString(t *testing.T) { 3809 okc := make(chan bool, 1) 3810 ht := newHandlerTest(HandlerFunc(func(w ResponseWriter, r *Request) { 3811 type stringWriter interface { 3812 WriteString(s string) (n int, err error) 3813 } 3814 _, ok := w.(stringWriter) 3815 okc <- ok 3816 })) 3817 ht.rawResponse("GET / HTTP/1.0") 3818 select { 3819 case ok := <-okc: 3820 if !ok { 3821 t.Error("ResponseWriter did not implement io.stringWriter") 3822 } 3823 default: 3824 t.Error("handler was never called") 3825 } 3826 } 3827 3828 func TestAppendTime(t *testing.T) { 3829 var b [len(TimeFormat)]byte 3830 t1 := time.Date(2013, 9, 21, 15, 41, 0, 0, time.FixedZone("CEST", 2*60*60)) 3831 res := ExportAppendTime(b[:0], t1) 3832 t2, err := ParseTime(string(res)) 3833 if err != nil { 3834 t.Fatalf("Error parsing time: %s", err) 3835 } 3836 if !t1.Equal(t2) { 3837 t.Fatalf("Times differ; expected: %v, got %v (%s)", t1, t2, string(res)) 3838 } 3839 } 3840 3841 func TestServerConnState(t *testing.T) { 3842 setParallel(t) 3843 defer afterTest(t) 3844 handler := map[string]func(w ResponseWriter, r *Request){ 3845 "/": func(w ResponseWriter, r *Request) { 3846 fmt.Fprintf(w, "Hello.") 3847 }, 3848 "/close": func(w ResponseWriter, r *Request) { 3849 w.Header().Set("Connection", "close") 3850 fmt.Fprintf(w, "Hello.") 3851 }, 3852 "/hijack": func(w ResponseWriter, r *Request) { 3853 c, _, _ := w.(Hijacker).Hijack() 3854 c.Write([]byte("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\nHello.")) 3855 c.Close() 3856 }, 3857 "/hijack-panic": func(w ResponseWriter, r *Request) { 3858 c, _, _ := w.(Hijacker).Hijack() 3859 c.Write([]byte("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\nHello.")) 3860 c.Close() 3861 panic("intentional panic") 3862 }, 3863 } 3864 ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) { 3865 handler[r.URL.Path](w, r) 3866 })) 3867 defer ts.Close() 3868 3869 var mu sync.Mutex // guard stateLog and connID 3870 var stateLog = map[int][]ConnState{} 3871 var connID = map[net.Conn]int{} 3872 3873 ts.Config.ErrorLog = log.New(ioutil.Discard, "", 0) 3874 ts.Config.ConnState = func(c net.Conn, state ConnState) { 3875 if c == nil { 3876 t.Errorf("nil conn seen in state %s", state) 3877 return 3878 } 3879 mu.Lock() 3880 defer mu.Unlock() 3881 id, ok := connID[c] 3882 if !ok { 3883 id = len(connID) + 1 3884 connID[c] = id 3885 } 3886 stateLog[id] = append(stateLog[id], state) 3887 } 3888 ts.Start() 3889 3890 c := ts.Client() 3891 3892 mustGet := func(url string, headers ...string) { 3893 req, err := NewRequest("GET", url, nil) 3894 if err != nil { 3895 t.Fatal(err) 3896 } 3897 for len(headers) > 0 { 3898 req.Header.Add(headers[0], headers[1]) 3899 headers = headers[2:] 3900 } 3901 res, err := c.Do(req) 3902 if err != nil { 3903 t.Errorf("Error fetching %s: %v", url, err) 3904 return 3905 } 3906 _, err = ioutil.ReadAll(res.Body) 3907 defer res.Body.Close() 3908 if err != nil { 3909 t.Errorf("Error reading %s: %v", url, err) 3910 } 3911 } 3912 3913 mustGet(ts.URL + "/") 3914 mustGet(ts.URL + "/close") 3915 3916 mustGet(ts.URL + "/") 3917 mustGet(ts.URL+"/", "Connection", "close") 3918 3919 mustGet(ts.URL + "/hijack") 3920 mustGet(ts.URL + "/hijack-panic") 3921 3922 // New->Closed 3923 { 3924 c, err := net.Dial("tcp", ts.Listener.Addr().String()) 3925 if err != nil { 3926 t.Fatal(err) 3927 } 3928 c.Close() 3929 } 3930 3931 // New->Active->Closed 3932 { 3933 c, err := net.Dial("tcp", ts.Listener.Addr().String()) 3934 if err != nil { 3935 t.Fatal(err) 3936 } 3937 if _, err := io.WriteString(c, "BOGUS REQUEST\r\n\r\n"); err != nil { 3938 t.Fatal(err) 3939 } 3940 c.Read(make([]byte, 1)) // block until server hangs up on us 3941 c.Close() 3942 } 3943 3944 // New->Idle->Closed 3945 { 3946 c, err := net.Dial("tcp", ts.Listener.Addr().String()) 3947 if err != nil { 3948 t.Fatal(err) 3949 } 3950 if _, err := io.WriteString(c, "GET / HTTP/1.1\r\nHost: foo\r\n\r\n"); err != nil { 3951 t.Fatal(err) 3952 } 3953 res, err := ReadResponse(bufio.NewReader(c), nil) 3954 if err != nil { 3955 t.Fatal(err) 3956 } 3957 if _, err := io.Copy(ioutil.Discard, res.Body); err != nil { 3958 t.Fatal(err) 3959 } 3960 c.Close() 3961 } 3962 3963 want := map[int][]ConnState{ 3964 1: {StateNew, StateActive, StateIdle, StateActive, StateClosed}, 3965 2: {StateNew, StateActive, StateIdle, StateActive, StateClosed}, 3966 3: {StateNew, StateActive, StateHijacked}, 3967 4: {StateNew, StateActive, StateHijacked}, 3968 5: {StateNew, StateClosed}, 3969 6: {StateNew, StateActive, StateClosed}, 3970 7: {StateNew, StateActive, StateIdle, StateClosed}, 3971 } 3972 logString := func(m map[int][]ConnState) string { 3973 var b bytes.Buffer 3974 var keys []int 3975 for id := range m { 3976 keys = append(keys, id) 3977 } 3978 sort.Ints(keys) 3979 for _, id := range keys { 3980 fmt.Fprintf(&b, "Conn %d: ", id) 3981 for _, s := range m[id] { 3982 fmt.Fprintf(&b, "%s ", s) 3983 } 3984 b.WriteString("\n") 3985 } 3986 return b.String() 3987 } 3988 3989 for i := 0; i < 5; i++ { 3990 time.Sleep(time.Duration(i) * 50 * time.Millisecond) 3991 mu.Lock() 3992 match := reflect.DeepEqual(stateLog, want) 3993 mu.Unlock() 3994 if match { 3995 return 3996 } 3997 } 3998 3999 mu.Lock() 4000 t.Errorf("Unexpected events.\nGot log:\n%s\n Want:\n%s\n", logString(stateLog), logString(want)) 4001 mu.Unlock() 4002 } 4003 4004 func TestServerKeepAlivesEnabled(t *testing.T) { 4005 defer afterTest(t) 4006 ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) {})) 4007 ts.Config.SetKeepAlivesEnabled(false) 4008 ts.Start() 4009 defer ts.Close() 4010 res, err := Get(ts.URL) 4011 if err != nil { 4012 t.Fatal(err) 4013 } 4014 defer res.Body.Close() 4015 if !res.Close { 4016 t.Errorf("Body.Close == false; want true") 4017 } 4018 } 4019 4020 // golang.org/issue/7856 4021 func TestServerEmptyBodyRace_h1(t *testing.T) { testServerEmptyBodyRace(t, h1Mode) } 4022 func TestServerEmptyBodyRace_h2(t *testing.T) { testServerEmptyBodyRace(t, h2Mode) } 4023 func testServerEmptyBodyRace(t *testing.T, h2 bool) { 4024 setParallel(t) 4025 defer afterTest(t) 4026 var n int32 4027 cst := newClientServerTest(t, h2, HandlerFunc(func(rw ResponseWriter, req *Request) { 4028 atomic.AddInt32(&n, 1) 4029 })) 4030 defer cst.close() 4031 var wg sync.WaitGroup 4032 const reqs = 20 4033 for i := 0; i < reqs; i++ { 4034 wg.Add(1) 4035 go func() { 4036 defer wg.Done() 4037 res, err := cst.c.Get(cst.ts.URL) 4038 if err != nil { 4039 t.Error(err) 4040 return 4041 } 4042 defer res.Body.Close() 4043 _, err = io.Copy(ioutil.Discard, res.Body) 4044 if err != nil { 4045 t.Error(err) 4046 return 4047 } 4048 }() 4049 } 4050 wg.Wait() 4051 if got := atomic.LoadInt32(&n); got != reqs { 4052 t.Errorf("handler ran %d times; want %d", got, reqs) 4053 } 4054 } 4055 4056 func TestServerConnStateNew(t *testing.T) { 4057 sawNew := false // if the test is buggy, we'll race on this variable. 4058 srv := &Server{ 4059 ConnState: func(c net.Conn, state ConnState) { 4060 if state == StateNew { 4061 sawNew = true // testing that this write isn't racy 4062 } 4063 }, 4064 Handler: HandlerFunc(func(w ResponseWriter, r *Request) {}), // irrelevant 4065 } 4066 srv.Serve(&oneConnListener{ 4067 conn: &rwTestConn{ 4068 Reader: strings.NewReader("GET / HTTP/1.1\r\nHost: foo\r\n\r\n"), 4069 Writer: ioutil.Discard, 4070 }, 4071 }) 4072 if !sawNew { // testing that this read isn't racy 4073 t.Error("StateNew not seen") 4074 } 4075 } 4076 4077 type closeWriteTestConn struct { 4078 rwTestConn 4079 didCloseWrite bool 4080 } 4081 4082 func (c *closeWriteTestConn) CloseWrite() error { 4083 c.didCloseWrite = true 4084 return nil 4085 } 4086 4087 func TestCloseWrite(t *testing.T) { 4088 setParallel(t) 4089 var srv Server 4090 var testConn closeWriteTestConn 4091 c := ExportServerNewConn(&srv, &testConn) 4092 ExportCloseWriteAndWait(c) 4093 if !testConn.didCloseWrite { 4094 t.Error("didn't see CloseWrite call") 4095 } 4096 } 4097 4098 // This verifies that a handler can Flush and then Hijack. 4099 // 4100 // An similar test crashed once during development, but it was only 4101 // testing this tangentially and temporarily until another TODO was 4102 // fixed. 4103 // 4104 // So add an explicit test for this. 4105 func TestServerFlushAndHijack(t *testing.T) { 4106 defer afterTest(t) 4107 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { 4108 io.WriteString(w, "Hello, ") 4109 w.(Flusher).Flush() 4110 conn, buf, _ := w.(Hijacker).Hijack() 4111 buf.WriteString("6\r\nworld!\r\n0\r\n\r\n") 4112 if err := buf.Flush(); err != nil { 4113 t.Error(err) 4114 } 4115 if err := conn.Close(); err != nil { 4116 t.Error(err) 4117 } 4118 })) 4119 defer ts.Close() 4120 res, err := Get(ts.URL) 4121 if err != nil { 4122 t.Fatal(err) 4123 } 4124 defer res.Body.Close() 4125 all, err := ioutil.ReadAll(res.Body) 4126 if err != nil { 4127 t.Fatal(err) 4128 } 4129 if want := "Hello, world!"; string(all) != want { 4130 t.Errorf("Got %q; want %q", all, want) 4131 } 4132 } 4133 4134 // golang.org/issue/8534 -- the Server shouldn't reuse a connection 4135 // for keep-alive after it's seen any Write error (e.g. a timeout) on 4136 // that net.Conn. 4137 // 4138 // To test, verify we don't timeout or see fewer unique client 4139 // addresses (== unique connections) than requests. 4140 func TestServerKeepAliveAfterWriteError(t *testing.T) { 4141 if testing.Short() { 4142 t.Skip("skipping in -short mode") 4143 } 4144 defer afterTest(t) 4145 const numReq = 3 4146 addrc := make(chan string, numReq) 4147 ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) { 4148 addrc <- r.RemoteAddr 4149 time.Sleep(500 * time.Millisecond) 4150 w.(Flusher).Flush() 4151 })) 4152 ts.Config.WriteTimeout = 250 * time.Millisecond 4153 ts.Start() 4154 defer ts.Close() 4155 4156 errc := make(chan error, numReq) 4157 go func() { 4158 defer close(errc) 4159 for i := 0; i < numReq; i++ { 4160 res, err := Get(ts.URL) 4161 if res != nil { 4162 res.Body.Close() 4163 } 4164 errc <- err 4165 } 4166 }() 4167 4168 timeout := time.NewTimer(numReq * 2 * time.Second) // 4x overkill 4169 defer timeout.Stop() 4170 addrSeen := map[string]bool{} 4171 numOkay := 0 4172 for { 4173 select { 4174 case v := <-addrc: 4175 addrSeen[v] = true 4176 case err, ok := <-errc: 4177 if !ok { 4178 if len(addrSeen) != numReq { 4179 t.Errorf("saw %d unique client addresses; want %d", len(addrSeen), numReq) 4180 } 4181 if numOkay != 0 { 4182 t.Errorf("got %d successful client requests; want 0", numOkay) 4183 } 4184 return 4185 } 4186 if err == nil { 4187 numOkay++ 4188 } 4189 case <-timeout.C: 4190 t.Fatal("timeout waiting for requests to complete") 4191 } 4192 } 4193 } 4194 4195 // Issue 9987: shouldn't add automatic Content-Length (or 4196 // Content-Type) if a Transfer-Encoding was set by the handler. 4197 func TestNoContentLengthIfTransferEncoding(t *testing.T) { 4198 defer afterTest(t) 4199 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { 4200 w.Header().Set("Transfer-Encoding", "foo") 4201 io.WriteString(w, "<html>") 4202 })) 4203 defer ts.Close() 4204 c, err := net.Dial("tcp", ts.Listener.Addr().String()) 4205 if err != nil { 4206 t.Fatalf("Dial: %v", err) 4207 } 4208 defer c.Close() 4209 if _, err := io.WriteString(c, "GET / HTTP/1.1\r\nHost: foo\r\n\r\n"); err != nil { 4210 t.Fatal(err) 4211 } 4212 bs := bufio.NewScanner(c) 4213 var got bytes.Buffer 4214 for bs.Scan() { 4215 if strings.TrimSpace(bs.Text()) == "" { 4216 break 4217 } 4218 got.WriteString(bs.Text()) 4219 got.WriteByte('\n') 4220 } 4221 if err := bs.Err(); err != nil { 4222 t.Fatal(err) 4223 } 4224 if strings.Contains(got.String(), "Content-Length") { 4225 t.Errorf("Unexpected Content-Length in response headers: %s", got.String()) 4226 } 4227 if strings.Contains(got.String(), "Content-Type") { 4228 t.Errorf("Unexpected Content-Type in response headers: %s", got.String()) 4229 } 4230 } 4231 4232 // tolerate extra CRLF(s) before Request-Line on subsequent requests on a conn 4233 // Issue 10876. 4234 func TestTolerateCRLFBeforeRequestLine(t *testing.T) { 4235 req := []byte("POST / HTTP/1.1\r\nHost: golang.org\r\nContent-Length: 3\r\n\r\nABC" + 4236 "\r\n\r\n" + // <-- this stuff is bogus, but we'll ignore it 4237 "GET / HTTP/1.1\r\nHost: golang.org\r\n\r\n") 4238 var buf bytes.Buffer 4239 conn := &rwTestConn{ 4240 Reader: bytes.NewReader(req), 4241 Writer: &buf, 4242 closec: make(chan bool, 1), 4243 } 4244 ln := &oneConnListener{conn: conn} 4245 numReq := 0 4246 go Serve(ln, HandlerFunc(func(rw ResponseWriter, r *Request) { 4247 numReq++ 4248 })) 4249 <-conn.closec 4250 if numReq != 2 { 4251 t.Errorf("num requests = %d; want 2", numReq) 4252 t.Logf("Res: %s", buf.Bytes()) 4253 } 4254 } 4255 4256 func TestIssue13893_Expect100(t *testing.T) { 4257 // test that the Server doesn't filter out Expect headers. 4258 req := reqBytes(`PUT /readbody HTTP/1.1 4259 User-Agent: PycURL/7.22.0 4260 Host: 127.0.0.1:9000 4261 Accept: */* 4262 Expect: 100-continue 4263 Content-Length: 10 4264 4265 HelloWorld 4266 4267 `) 4268 var buf bytes.Buffer 4269 conn := &rwTestConn{ 4270 Reader: bytes.NewReader(req), 4271 Writer: &buf, 4272 closec: make(chan bool, 1), 4273 } 4274 ln := &oneConnListener{conn: conn} 4275 go Serve(ln, HandlerFunc(func(w ResponseWriter, r *Request) { 4276 if _, ok := r.Header["Expect"]; !ok { 4277 t.Error("Expect header should not be filtered out") 4278 } 4279 })) 4280 <-conn.closec 4281 } 4282 4283 func TestIssue11549_Expect100(t *testing.T) { 4284 req := reqBytes(`PUT /readbody HTTP/1.1 4285 User-Agent: PycURL/7.22.0 4286 Host: 127.0.0.1:9000 4287 Accept: */* 4288 Expect: 100-continue 4289 Content-Length: 10 4290 4291 HelloWorldPUT /noreadbody HTTP/1.1 4292 User-Agent: PycURL/7.22.0 4293 Host: 127.0.0.1:9000 4294 Accept: */* 4295 Expect: 100-continue 4296 Content-Length: 10 4297 4298 GET /should-be-ignored HTTP/1.1 4299 Host: foo 4300 4301 `) 4302 var buf bytes.Buffer 4303 conn := &rwTestConn{ 4304 Reader: bytes.NewReader(req), 4305 Writer: &buf, 4306 closec: make(chan bool, 1), 4307 } 4308 ln := &oneConnListener{conn: conn} 4309 numReq := 0 4310 go Serve(ln, HandlerFunc(func(w ResponseWriter, r *Request) { 4311 numReq++ 4312 if r.URL.Path == "/readbody" { 4313 ioutil.ReadAll(r.Body) 4314 } 4315 io.WriteString(w, "Hello world!") 4316 })) 4317 <-conn.closec 4318 if numReq != 2 { 4319 t.Errorf("num requests = %d; want 2", numReq) 4320 } 4321 if !strings.Contains(buf.String(), "Connection: close\r\n") { 4322 t.Errorf("expected 'Connection: close' in response; got: %s", buf.String()) 4323 } 4324 } 4325 4326 // If a Handler finishes and there's an unread request body, 4327 // verify the server try to do implicit read on it before replying. 4328 func TestHandlerFinishSkipBigContentLengthRead(t *testing.T) { 4329 setParallel(t) 4330 conn := &testConn{closec: make(chan bool)} 4331 conn.readBuf.Write([]byte(fmt.Sprintf( 4332 "POST / HTTP/1.1\r\n" + 4333 "Host: test\r\n" + 4334 "Content-Length: 9999999999\r\n" + 4335 "\r\n" + strings.Repeat("a", 1<<20)))) 4336 4337 ls := &oneConnListener{conn} 4338 var inHandlerLen int 4339 go Serve(ls, HandlerFunc(func(rw ResponseWriter, req *Request) { 4340 inHandlerLen = conn.readBuf.Len() 4341 rw.WriteHeader(404) 4342 })) 4343 <-conn.closec 4344 afterHandlerLen := conn.readBuf.Len() 4345 4346 if afterHandlerLen != inHandlerLen { 4347 t.Errorf("unexpected implicit read. Read buffer went from %d -> %d", inHandlerLen, afterHandlerLen) 4348 } 4349 } 4350 4351 func TestHandlerSetsBodyNil_h1(t *testing.T) { testHandlerSetsBodyNil(t, h1Mode) } 4352 func TestHandlerSetsBodyNil_h2(t *testing.T) { testHandlerSetsBodyNil(t, h2Mode) } 4353 func testHandlerSetsBodyNil(t *testing.T, h2 bool) { 4354 defer afterTest(t) 4355 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 4356 r.Body = nil 4357 fmt.Fprintf(w, "%v", r.RemoteAddr) 4358 })) 4359 defer cst.close() 4360 get := func() string { 4361 res, err := cst.c.Get(cst.ts.URL) 4362 if err != nil { 4363 t.Fatal(err) 4364 } 4365 defer res.Body.Close() 4366 slurp, err := ioutil.ReadAll(res.Body) 4367 if err != nil { 4368 t.Fatal(err) 4369 } 4370 return string(slurp) 4371 } 4372 a, b := get(), get() 4373 if a != b { 4374 t.Errorf("Failed to reuse connections between requests: %v vs %v", a, b) 4375 } 4376 } 4377 4378 // Test that we validate the Host header. 4379 // Issue 11206 (invalid bytes in Host) and 13624 (Host present in HTTP/1.1) 4380 func TestServerValidatesHostHeader(t *testing.T) { 4381 tests := []struct { 4382 proto string 4383 host string 4384 want int 4385 }{ 4386 {"HTTP/0.9", "", 400}, 4387 4388 {"HTTP/1.1", "", 400}, 4389 {"HTTP/1.1", "Host: \r\n", 200}, 4390 {"HTTP/1.1", "Host: 1.2.3.4\r\n", 200}, 4391 {"HTTP/1.1", "Host: foo.com\r\n", 200}, 4392 {"HTTP/1.1", "Host: foo-bar_baz.com\r\n", 200}, 4393 {"HTTP/1.1", "Host: foo.com:80\r\n", 200}, 4394 {"HTTP/1.1", "Host: ::1\r\n", 200}, 4395 {"HTTP/1.1", "Host: [::1]\r\n", 200}, // questionable without port, but accept it 4396 {"HTTP/1.1", "Host: [::1]:80\r\n", 200}, 4397 {"HTTP/1.1", "Host: [::1%25en0]:80\r\n", 200}, 4398 {"HTTP/1.1", "Host: 1.2.3.4\r\n", 200}, 4399 {"HTTP/1.1", "Host: \x06\r\n", 400}, 4400 {"HTTP/1.1", "Host: \xff\r\n", 400}, 4401 {"HTTP/1.1", "Host: {\r\n", 400}, 4402 {"HTTP/1.1", "Host: }\r\n", 400}, 4403 {"HTTP/1.1", "Host: first\r\nHost: second\r\n", 400}, 4404 4405 // HTTP/1.0 can lack a host header, but if present 4406 // must play by the rules too: 4407 {"HTTP/1.0", "", 200}, 4408 {"HTTP/1.0", "Host: first\r\nHost: second\r\n", 400}, 4409 {"HTTP/1.0", "Host: \xff\r\n", 400}, 4410 4411 // Make an exception for HTTP upgrade requests: 4412 {"PRI * HTTP/2.0", "", 200}, 4413 4414 // Also an exception for CONNECT requests: (Issue 18215) 4415 {"CONNECT golang.org:443 HTTP/1.1", "", 200}, 4416 4417 // But not other HTTP/2 stuff: 4418 {"PRI / HTTP/2.0", "", 400}, 4419 {"GET / HTTP/2.0", "", 400}, 4420 {"GET / HTTP/3.0", "", 400}, 4421 } 4422 for _, tt := range tests { 4423 conn := &testConn{closec: make(chan bool, 1)} 4424 methodTarget := "GET / " 4425 if !strings.HasPrefix(tt.proto, "HTTP/") { 4426 methodTarget = "" 4427 } 4428 io.WriteString(&conn.readBuf, methodTarget+tt.proto+"\r\n"+tt.host+"\r\n") 4429 4430 ln := &oneConnListener{conn} 4431 srv := Server{ 4432 ErrorLog: quietLog, 4433 Handler: HandlerFunc(func(ResponseWriter, *Request) {}), 4434 } 4435 go srv.Serve(ln) 4436 <-conn.closec 4437 res, err := ReadResponse(bufio.NewReader(&conn.writeBuf), nil) 4438 if err != nil { 4439 t.Errorf("For %s %q, ReadResponse: %v", tt.proto, tt.host, res) 4440 continue 4441 } 4442 if res.StatusCode != tt.want { 4443 t.Errorf("For %s %q, Status = %d; want %d", tt.proto, tt.host, res.StatusCode, tt.want) 4444 } 4445 } 4446 } 4447 4448 func TestServerHandlersCanHandleH2PRI(t *testing.T) { 4449 const upgradeResponse = "upgrade here" 4450 defer afterTest(t) 4451 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { 4452 conn, br, err := w.(Hijacker).Hijack() 4453 defer conn.Close() 4454 if r.Method != "PRI" || r.RequestURI != "*" { 4455 t.Errorf("Got method/target %q %q; want PRI *", r.Method, r.RequestURI) 4456 return 4457 } 4458 if !r.Close { 4459 t.Errorf("Request.Close = true; want false") 4460 } 4461 const want = "SM\r\n\r\n" 4462 buf := make([]byte, len(want)) 4463 n, err := io.ReadFull(br, buf) 4464 if err != nil || string(buf[:n]) != want { 4465 t.Errorf("Read = %v, %v (%q), want %q", n, err, buf[:n], want) 4466 return 4467 } 4468 io.WriteString(conn, upgradeResponse) 4469 })) 4470 defer ts.Close() 4471 4472 c, err := net.Dial("tcp", ts.Listener.Addr().String()) 4473 if err != nil { 4474 t.Fatalf("Dial: %v", err) 4475 } 4476 defer c.Close() 4477 io.WriteString(c, "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n") 4478 slurp, err := ioutil.ReadAll(c) 4479 if err != nil { 4480 t.Fatal(err) 4481 } 4482 if string(slurp) != upgradeResponse { 4483 t.Errorf("Handler response = %q; want %q", slurp, upgradeResponse) 4484 } 4485 } 4486 4487 // Test that we validate the valid bytes in HTTP/1 headers. 4488 // Issue 11207. 4489 func TestServerValidatesHeaders(t *testing.T) { 4490 setParallel(t) 4491 tests := []struct { 4492 header string 4493 want int 4494 }{ 4495 {"", 200}, 4496 {"Foo: bar\r\n", 200}, 4497 {"X-Foo: bar\r\n", 200}, 4498 {"Foo: a space\r\n", 200}, 4499 4500 {"A space: foo\r\n", 400}, // space in header 4501 {"foo\xffbar: foo\r\n", 400}, // binary in header 4502 {"foo\x00bar: foo\r\n", 400}, // binary in header 4503 {"Foo: " + strings.Repeat("x", 1<<21) + "\r\n", 431}, // header too large 4504 4505 {"foo: foo foo\r\n", 200}, // LWS space is okay 4506 {"foo: foo\tfoo\r\n", 200}, // LWS tab is okay 4507 {"foo: foo\x00foo\r\n", 400}, // CTL 0x00 in value is bad 4508 {"foo: foo\x7ffoo\r\n", 400}, // CTL 0x7f in value is bad 4509 {"foo: foo\xfffoo\r\n", 200}, // non-ASCII high octets in value are fine 4510 } 4511 for _, tt := range tests { 4512 conn := &testConn{closec: make(chan bool, 1)} 4513 io.WriteString(&conn.readBuf, "GET / HTTP/1.1\r\nHost: foo\r\n"+tt.header+"\r\n") 4514 4515 ln := &oneConnListener{conn} 4516 srv := Server{ 4517 ErrorLog: quietLog, 4518 Handler: HandlerFunc(func(ResponseWriter, *Request) {}), 4519 } 4520 go srv.Serve(ln) 4521 <-conn.closec 4522 res, err := ReadResponse(bufio.NewReader(&conn.writeBuf), nil) 4523 if err != nil { 4524 t.Errorf("For %q, ReadResponse: %v", tt.header, res) 4525 continue 4526 } 4527 if res.StatusCode != tt.want { 4528 t.Errorf("For %q, Status = %d; want %d", tt.header, res.StatusCode, tt.want) 4529 } 4530 } 4531 } 4532 4533 func TestServerRequestContextCancel_ServeHTTPDone_h1(t *testing.T) { 4534 testServerRequestContextCancel_ServeHTTPDone(t, h1Mode) 4535 } 4536 func TestServerRequestContextCancel_ServeHTTPDone_h2(t *testing.T) { 4537 testServerRequestContextCancel_ServeHTTPDone(t, h2Mode) 4538 } 4539 func testServerRequestContextCancel_ServeHTTPDone(t *testing.T, h2 bool) { 4540 setParallel(t) 4541 defer afterTest(t) 4542 ctxc := make(chan context.Context, 1) 4543 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 4544 ctx := r.Context() 4545 select { 4546 case <-ctx.Done(): 4547 t.Error("should not be Done in ServeHTTP") 4548 default: 4549 } 4550 ctxc <- ctx 4551 })) 4552 defer cst.close() 4553 res, err := cst.c.Get(cst.ts.URL) 4554 if err != nil { 4555 t.Fatal(err) 4556 } 4557 res.Body.Close() 4558 ctx := <-ctxc 4559 select { 4560 case <-ctx.Done(): 4561 default: 4562 t.Error("context should be done after ServeHTTP completes") 4563 } 4564 } 4565 4566 // Tests that the Request.Context available to the Handler is canceled 4567 // if the peer closes their TCP connection. This requires that the server 4568 // is always blocked in a Read call so it notices the EOF from the client. 4569 // See issues 15927 and 15224. 4570 func TestServerRequestContextCancel_ConnClose(t *testing.T) { 4571 setParallel(t) 4572 defer afterTest(t) 4573 inHandler := make(chan struct{}) 4574 handlerDone := make(chan struct{}) 4575 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { 4576 close(inHandler) 4577 select { 4578 case <-r.Context().Done(): 4579 case <-time.After(3 * time.Second): 4580 t.Errorf("timeout waiting for context to be done") 4581 } 4582 close(handlerDone) 4583 })) 4584 defer ts.Close() 4585 c, err := net.Dial("tcp", ts.Listener.Addr().String()) 4586 if err != nil { 4587 t.Fatal(err) 4588 } 4589 defer c.Close() 4590 io.WriteString(c, "GET / HTTP/1.1\r\nHost: foo\r\n\r\n") 4591 select { 4592 case <-inHandler: 4593 case <-time.After(3 * time.Second): 4594 t.Fatalf("timeout waiting to see ServeHTTP get called") 4595 } 4596 c.Close() // this should trigger the context being done 4597 4598 select { 4599 case <-handlerDone: 4600 case <-time.After(4 * time.Second): 4601 t.Fatalf("timeout waiting to see ServeHTTP exit") 4602 } 4603 } 4604 4605 func TestServerContext_ServerContextKey_h1(t *testing.T) { 4606 testServerContext_ServerContextKey(t, h1Mode) 4607 } 4608 func TestServerContext_ServerContextKey_h2(t *testing.T) { 4609 testServerContext_ServerContextKey(t, h2Mode) 4610 } 4611 func testServerContext_ServerContextKey(t *testing.T, h2 bool) { 4612 setParallel(t) 4613 defer afterTest(t) 4614 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 4615 ctx := r.Context() 4616 got := ctx.Value(ServerContextKey) 4617 if _, ok := got.(*Server); !ok { 4618 t.Errorf("context value = %T; want *http.Server", got) 4619 } 4620 })) 4621 defer cst.close() 4622 res, err := cst.c.Get(cst.ts.URL) 4623 if err != nil { 4624 t.Fatal(err) 4625 } 4626 res.Body.Close() 4627 } 4628 4629 func TestServerContext_LocalAddrContextKey_h1(t *testing.T) { 4630 testServerContext_LocalAddrContextKey(t, h1Mode) 4631 } 4632 func TestServerContext_LocalAddrContextKey_h2(t *testing.T) { 4633 testServerContext_LocalAddrContextKey(t, h2Mode) 4634 } 4635 func testServerContext_LocalAddrContextKey(t *testing.T, h2 bool) { 4636 setParallel(t) 4637 defer afterTest(t) 4638 ch := make(chan interface{}, 1) 4639 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 4640 ch <- r.Context().Value(LocalAddrContextKey) 4641 })) 4642 defer cst.close() 4643 if _, err := cst.c.Head(cst.ts.URL); err != nil { 4644 t.Fatal(err) 4645 } 4646 4647 host := cst.ts.Listener.Addr().String() 4648 select { 4649 case got := <-ch: 4650 if addr, ok := got.(net.Addr); !ok { 4651 t.Errorf("local addr value = %T; want net.Addr", got) 4652 } else if fmt.Sprint(addr) != host { 4653 t.Errorf("local addr = %v; want %v", addr, host) 4654 } 4655 case <-time.After(5 * time.Second): 4656 t.Error("timed out") 4657 } 4658 } 4659 4660 // https://golang.org/issue/15960 4661 func TestHandlerSetTransferEncodingChunked(t *testing.T) { 4662 setParallel(t) 4663 defer afterTest(t) 4664 ht := newHandlerTest(HandlerFunc(func(w ResponseWriter, r *Request) { 4665 w.Header().Set("Transfer-Encoding", "chunked") 4666 w.Write([]byte("hello")) 4667 })) 4668 resp := ht.rawResponse("GET / HTTP/1.1\nHost: foo") 4669 const hdr = "Transfer-Encoding: chunked" 4670 if n := strings.Count(resp, hdr); n != 1 { 4671 t.Errorf("want 1 occurrence of %q in response, got %v\nresponse: %v", hdr, n, resp) 4672 } 4673 } 4674 4675 // https://golang.org/issue/16063 4676 func TestHandlerSetTransferEncodingGzip(t *testing.T) { 4677 setParallel(t) 4678 defer afterTest(t) 4679 ht := newHandlerTest(HandlerFunc(func(w ResponseWriter, r *Request) { 4680 w.Header().Set("Transfer-Encoding", "gzip") 4681 gz := gzip.NewWriter(w) 4682 gz.Write([]byte("hello")) 4683 gz.Close() 4684 })) 4685 resp := ht.rawResponse("GET / HTTP/1.1\nHost: foo") 4686 for _, v := range []string{"gzip", "chunked"} { 4687 hdr := "Transfer-Encoding: " + v 4688 if n := strings.Count(resp, hdr); n != 1 { 4689 t.Errorf("want 1 occurrence of %q in response, got %v\nresponse: %v", hdr, n, resp) 4690 } 4691 } 4692 } 4693 4694 func BenchmarkClientServer(b *testing.B) { 4695 b.ReportAllocs() 4696 b.StopTimer() 4697 ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) { 4698 fmt.Fprintf(rw, "Hello world.\n") 4699 })) 4700 defer ts.Close() 4701 b.StartTimer() 4702 4703 for i := 0; i < b.N; i++ { 4704 res, err := Get(ts.URL) 4705 if err != nil { 4706 b.Fatal("Get:", err) 4707 } 4708 all, err := ioutil.ReadAll(res.Body) 4709 res.Body.Close() 4710 if err != nil { 4711 b.Fatal("ReadAll:", err) 4712 } 4713 body := string(all) 4714 if body != "Hello world.\n" { 4715 b.Fatal("Got body:", body) 4716 } 4717 } 4718 4719 b.StopTimer() 4720 } 4721 4722 func BenchmarkClientServerParallel4(b *testing.B) { 4723 benchmarkClientServerParallel(b, 4, false) 4724 } 4725 4726 func BenchmarkClientServerParallel64(b *testing.B) { 4727 benchmarkClientServerParallel(b, 64, false) 4728 } 4729 4730 func BenchmarkClientServerParallelTLS4(b *testing.B) { 4731 benchmarkClientServerParallel(b, 4, true) 4732 } 4733 4734 func BenchmarkClientServerParallelTLS64(b *testing.B) { 4735 benchmarkClientServerParallel(b, 64, true) 4736 } 4737 4738 func benchmarkClientServerParallel(b *testing.B, parallelism int, useTLS bool) { 4739 b.ReportAllocs() 4740 ts := httptest.NewUnstartedServer(HandlerFunc(func(rw ResponseWriter, r *Request) { 4741 fmt.Fprintf(rw, "Hello world.\n") 4742 })) 4743 if useTLS { 4744 ts.StartTLS() 4745 } else { 4746 ts.Start() 4747 } 4748 defer ts.Close() 4749 b.ResetTimer() 4750 b.SetParallelism(parallelism) 4751 b.RunParallel(func(pb *testing.PB) { 4752 c := ts.Client() 4753 for pb.Next() { 4754 res, err := c.Get(ts.URL) 4755 if err != nil { 4756 b.Logf("Get: %v", err) 4757 continue 4758 } 4759 all, err := ioutil.ReadAll(res.Body) 4760 res.Body.Close() 4761 if err != nil { 4762 b.Logf("ReadAll: %v", err) 4763 continue 4764 } 4765 body := string(all) 4766 if body != "Hello world.\n" { 4767 panic("Got body: " + body) 4768 } 4769 } 4770 }) 4771 } 4772 4773 // A benchmark for profiling the server without the HTTP client code. 4774 // The client code runs in a subprocess. 4775 // 4776 // For use like: 4777 // $ go test -c 4778 // $ ./http.test -test.run=XX -test.bench=BenchmarkServer -test.benchtime=15s -test.cpuprofile=http.prof 4779 // $ go tool pprof http.test http.prof 4780 // (pprof) web 4781 func BenchmarkServer(b *testing.B) { 4782 b.ReportAllocs() 4783 // Child process mode; 4784 if url := os.Getenv("TEST_BENCH_SERVER_URL"); url != "" { 4785 n, err := strconv.Atoi(os.Getenv("TEST_BENCH_CLIENT_N")) 4786 if err != nil { 4787 panic(err) 4788 } 4789 for i := 0; i < n; i++ { 4790 res, err := Get(url) 4791 if err != nil { 4792 log.Panicf("Get: %v", err) 4793 } 4794 all, err := ioutil.ReadAll(res.Body) 4795 res.Body.Close() 4796 if err != nil { 4797 log.Panicf("ReadAll: %v", err) 4798 } 4799 body := string(all) 4800 if body != "Hello world.\n" { 4801 log.Panicf("Got body: %q", body) 4802 } 4803 } 4804 os.Exit(0) 4805 return 4806 } 4807 4808 var res = []byte("Hello world.\n") 4809 b.StopTimer() 4810 ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) { 4811 rw.Header().Set("Content-Type", "text/html; charset=utf-8") 4812 rw.Write(res) 4813 })) 4814 defer ts.Close() 4815 b.StartTimer() 4816 4817 cmd := exec.Command(os.Args[0], "-test.run=XXXX", "-test.bench=BenchmarkServer$") 4818 cmd.Env = append([]string{ 4819 fmt.Sprintf("TEST_BENCH_CLIENT_N=%d", b.N), 4820 fmt.Sprintf("TEST_BENCH_SERVER_URL=%s", ts.URL), 4821 }, os.Environ()...) 4822 out, err := cmd.CombinedOutput() 4823 if err != nil { 4824 b.Errorf("Test failure: %v, with output: %s", err, out) 4825 } 4826 } 4827 4828 // getNoBody wraps Get but closes any Response.Body before returning the response. 4829 func getNoBody(urlStr string) (*Response, error) { 4830 res, err := Get(urlStr) 4831 if err != nil { 4832 return nil, err 4833 } 4834 res.Body.Close() 4835 return res, nil 4836 } 4837 4838 // A benchmark for profiling the client without the HTTP server code. 4839 // The server code runs in a subprocess. 4840 func BenchmarkClient(b *testing.B) { 4841 b.ReportAllocs() 4842 b.StopTimer() 4843 defer afterTest(b) 4844 4845 var data = []byte("Hello world.\n") 4846 if server := os.Getenv("TEST_BENCH_SERVER"); server != "" { 4847 // Server process mode. 4848 port := os.Getenv("TEST_BENCH_SERVER_PORT") // can be set by user 4849 if port == "" { 4850 port = "0" 4851 } 4852 ln, err := net.Listen("tcp", "localhost:"+port) 4853 if err != nil { 4854 fmt.Fprintln(os.Stderr, err.Error()) 4855 os.Exit(1) 4856 } 4857 fmt.Println(ln.Addr().String()) 4858 HandleFunc("/", func(w ResponseWriter, r *Request) { 4859 r.ParseForm() 4860 if r.Form.Get("stop") != "" { 4861 os.Exit(0) 4862 } 4863 w.Header().Set("Content-Type", "text/html; charset=utf-8") 4864 w.Write(data) 4865 }) 4866 var srv Server 4867 log.Fatal(srv.Serve(ln)) 4868 } 4869 4870 // Start server process. 4871 cmd := exec.Command(os.Args[0], "-test.run=XXXX", "-test.bench=BenchmarkClient$") 4872 cmd.Env = append(os.Environ(), "TEST_BENCH_SERVER=yes") 4873 cmd.Stderr = os.Stderr 4874 stdout, err := cmd.StdoutPipe() 4875 if err != nil { 4876 b.Fatal(err) 4877 } 4878 if err := cmd.Start(); err != nil { 4879 b.Fatalf("subprocess failed to start: %v", err) 4880 } 4881 defer cmd.Process.Kill() 4882 4883 // Wait for the server in the child process to respond and tell us 4884 // its listening address, once it's started listening: 4885 timer := time.AfterFunc(10*time.Second, func() { 4886 cmd.Process.Kill() 4887 }) 4888 defer timer.Stop() 4889 bs := bufio.NewScanner(stdout) 4890 if !bs.Scan() { 4891 b.Fatalf("failed to read listening URL from child: %v", bs.Err()) 4892 } 4893 url := "http://" + strings.TrimSpace(bs.Text()) + "/" 4894 timer.Stop() 4895 if _, err := getNoBody(url); err != nil { 4896 b.Fatalf("initial probe of child process failed: %v", err) 4897 } 4898 4899 done := make(chan error) 4900 go func() { 4901 done <- cmd.Wait() 4902 }() 4903 4904 // Do b.N requests to the server. 4905 b.StartTimer() 4906 for i := 0; i < b.N; i++ { 4907 res, err := Get(url) 4908 if err != nil { 4909 b.Fatalf("Get: %v", err) 4910 } 4911 body, err := ioutil.ReadAll(res.Body) 4912 res.Body.Close() 4913 if err != nil { 4914 b.Fatalf("ReadAll: %v", err) 4915 } 4916 if !bytes.Equal(body, data) { 4917 b.Fatalf("Got body: %q", body) 4918 } 4919 } 4920 b.StopTimer() 4921 4922 // Instruct server process to stop. 4923 getNoBody(url + "?stop=yes") 4924 select { 4925 case err := <-done: 4926 if err != nil { 4927 b.Fatalf("subprocess failed: %v", err) 4928 } 4929 case <-time.After(5 * time.Second): 4930 b.Fatalf("subprocess did not stop") 4931 } 4932 } 4933 4934 func BenchmarkServerFakeConnNoKeepAlive(b *testing.B) { 4935 b.ReportAllocs() 4936 req := reqBytes(`GET / HTTP/1.0 4937 Host: golang.org 4938 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 4939 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17 4940 Accept-Encoding: gzip,deflate,sdch 4941 Accept-Language: en-US,en;q=0.8 4942 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 4943 `) 4944 res := []byte("Hello world!\n") 4945 4946 conn := &testConn{ 4947 // testConn.Close will not push into the channel 4948 // if it's full. 4949 closec: make(chan bool, 1), 4950 } 4951 handler := HandlerFunc(func(rw ResponseWriter, r *Request) { 4952 rw.Header().Set("Content-Type", "text/html; charset=utf-8") 4953 rw.Write(res) 4954 }) 4955 ln := new(oneConnListener) 4956 for i := 0; i < b.N; i++ { 4957 conn.readBuf.Reset() 4958 conn.writeBuf.Reset() 4959 conn.readBuf.Write(req) 4960 ln.conn = conn 4961 Serve(ln, handler) 4962 <-conn.closec 4963 } 4964 } 4965 4966 // repeatReader reads content count times, then EOFs. 4967 type repeatReader struct { 4968 content []byte 4969 count int 4970 off int 4971 } 4972 4973 func (r *repeatReader) Read(p []byte) (n int, err error) { 4974 if r.count <= 0 { 4975 return 0, io.EOF 4976 } 4977 n = copy(p, r.content[r.off:]) 4978 r.off += n 4979 if r.off == len(r.content) { 4980 r.count-- 4981 r.off = 0 4982 } 4983 return 4984 } 4985 4986 func BenchmarkServerFakeConnWithKeepAlive(b *testing.B) { 4987 b.ReportAllocs() 4988 4989 req := reqBytes(`GET / HTTP/1.1 4990 Host: golang.org 4991 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 4992 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17 4993 Accept-Encoding: gzip,deflate,sdch 4994 Accept-Language: en-US,en;q=0.8 4995 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 4996 `) 4997 res := []byte("Hello world!\n") 4998 4999 conn := &rwTestConn{ 5000 Reader: &repeatReader{content: req, count: b.N}, 5001 Writer: ioutil.Discard, 5002 closec: make(chan bool, 1), 5003 } 5004 handled := 0 5005 handler := HandlerFunc(func(rw ResponseWriter, r *Request) { 5006 handled++ 5007 rw.Header().Set("Content-Type", "text/html; charset=utf-8") 5008 rw.Write(res) 5009 }) 5010 ln := &oneConnListener{conn: conn} 5011 go Serve(ln, handler) 5012 <-conn.closec 5013 if b.N != handled { 5014 b.Errorf("b.N=%d but handled %d", b.N, handled) 5015 } 5016 } 5017 5018 // same as above, but representing the most simple possible request 5019 // and handler. Notably: the handler does not call rw.Header(). 5020 func BenchmarkServerFakeConnWithKeepAliveLite(b *testing.B) { 5021 b.ReportAllocs() 5022 5023 req := reqBytes(`GET / HTTP/1.1 5024 Host: golang.org 5025 `) 5026 res := []byte("Hello world!\n") 5027 5028 conn := &rwTestConn{ 5029 Reader: &repeatReader{content: req, count: b.N}, 5030 Writer: ioutil.Discard, 5031 closec: make(chan bool, 1), 5032 } 5033 handled := 0 5034 handler := HandlerFunc(func(rw ResponseWriter, r *Request) { 5035 handled++ 5036 rw.Write(res) 5037 }) 5038 ln := &oneConnListener{conn: conn} 5039 go Serve(ln, handler) 5040 <-conn.closec 5041 if b.N != handled { 5042 b.Errorf("b.N=%d but handled %d", b.N, handled) 5043 } 5044 } 5045 5046 const someResponse = "<html>some response</html>" 5047 5048 // A Response that's just no bigger than 2KB, the buffer-before-chunking threshold. 5049 var response = bytes.Repeat([]byte(someResponse), 2<<10/len(someResponse)) 5050 5051 // Both Content-Type and Content-Length set. Should be no buffering. 5052 func BenchmarkServerHandlerTypeLen(b *testing.B) { 5053 benchmarkHandler(b, HandlerFunc(func(w ResponseWriter, r *Request) { 5054 w.Header().Set("Content-Type", "text/html") 5055 w.Header().Set("Content-Length", strconv.Itoa(len(response))) 5056 w.Write(response) 5057 })) 5058 } 5059 5060 // A Content-Type is set, but no length. No sniffing, but will count the Content-Length. 5061 func BenchmarkServerHandlerNoLen(b *testing.B) { 5062 benchmarkHandler(b, HandlerFunc(func(w ResponseWriter, r *Request) { 5063 w.Header().Set("Content-Type", "text/html") 5064 w.Write(response) 5065 })) 5066 } 5067 5068 // A Content-Length is set, but the Content-Type will be sniffed. 5069 func BenchmarkServerHandlerNoType(b *testing.B) { 5070 benchmarkHandler(b, HandlerFunc(func(w ResponseWriter, r *Request) { 5071 w.Header().Set("Content-Length", strconv.Itoa(len(response))) 5072 w.Write(response) 5073 })) 5074 } 5075 5076 // Neither a Content-Type or Content-Length, so sniffed and counted. 5077 func BenchmarkServerHandlerNoHeader(b *testing.B) { 5078 benchmarkHandler(b, HandlerFunc(func(w ResponseWriter, r *Request) { 5079 w.Write(response) 5080 })) 5081 } 5082 5083 func benchmarkHandler(b *testing.B, h Handler) { 5084 b.ReportAllocs() 5085 req := reqBytes(`GET / HTTP/1.1 5086 Host: golang.org 5087 `) 5088 conn := &rwTestConn{ 5089 Reader: &repeatReader{content: req, count: b.N}, 5090 Writer: ioutil.Discard, 5091 closec: make(chan bool, 1), 5092 } 5093 handled := 0 5094 handler := HandlerFunc(func(rw ResponseWriter, r *Request) { 5095 handled++ 5096 h.ServeHTTP(rw, r) 5097 }) 5098 ln := &oneConnListener{conn: conn} 5099 go Serve(ln, handler) 5100 <-conn.closec 5101 if b.N != handled { 5102 b.Errorf("b.N=%d but handled %d", b.N, handled) 5103 } 5104 } 5105 5106 func BenchmarkServerHijack(b *testing.B) { 5107 b.ReportAllocs() 5108 req := reqBytes(`GET / HTTP/1.1 5109 Host: golang.org 5110 `) 5111 h := HandlerFunc(func(w ResponseWriter, r *Request) { 5112 conn, _, err := w.(Hijacker).Hijack() 5113 if err != nil { 5114 panic(err) 5115 } 5116 conn.Close() 5117 }) 5118 conn := &rwTestConn{ 5119 Writer: ioutil.Discard, 5120 closec: make(chan bool, 1), 5121 } 5122 ln := &oneConnListener{conn: conn} 5123 for i := 0; i < b.N; i++ { 5124 conn.Reader = bytes.NewReader(req) 5125 ln.conn = conn 5126 Serve(ln, h) 5127 <-conn.closec 5128 } 5129 } 5130 5131 func BenchmarkCloseNotifier(b *testing.B) { 5132 b.ReportAllocs() 5133 b.StopTimer() 5134 sawClose := make(chan bool) 5135 ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) { 5136 <-rw.(CloseNotifier).CloseNotify() 5137 sawClose <- true 5138 })) 5139 defer ts.Close() 5140 tot := time.NewTimer(5 * time.Second) 5141 defer tot.Stop() 5142 b.StartTimer() 5143 for i := 0; i < b.N; i++ { 5144 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 5145 if err != nil { 5146 b.Fatalf("error dialing: %v", err) 5147 } 5148 _, err = fmt.Fprintf(conn, "GET / HTTP/1.1\r\nConnection: keep-alive\r\nHost: foo\r\n\r\n") 5149 if err != nil { 5150 b.Fatal(err) 5151 } 5152 conn.Close() 5153 tot.Reset(5 * time.Second) 5154 select { 5155 case <-sawClose: 5156 case <-tot.C: 5157 b.Fatal("timeout") 5158 } 5159 } 5160 b.StopTimer() 5161 } 5162 5163 // Verify this doesn't race (Issue 16505) 5164 func TestConcurrentServerServe(t *testing.T) { 5165 setParallel(t) 5166 for i := 0; i < 100; i++ { 5167 ln1 := &oneConnListener{conn: nil} 5168 ln2 := &oneConnListener{conn: nil} 5169 srv := Server{} 5170 go func() { srv.Serve(ln1) }() 5171 go func() { srv.Serve(ln2) }() 5172 } 5173 } 5174 5175 func TestServerIdleTimeout(t *testing.T) { 5176 if testing.Short() { 5177 t.Skip("skipping in short mode") 5178 } 5179 setParallel(t) 5180 defer afterTest(t) 5181 ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) { 5182 io.Copy(ioutil.Discard, r.Body) 5183 io.WriteString(w, r.RemoteAddr) 5184 })) 5185 ts.Config.ReadHeaderTimeout = 1 * time.Second 5186 ts.Config.IdleTimeout = 2 * time.Second 5187 ts.Start() 5188 defer ts.Close() 5189 c := ts.Client() 5190 5191 get := func() string { 5192 res, err := c.Get(ts.URL) 5193 if err != nil { 5194 t.Fatal(err) 5195 } 5196 defer res.Body.Close() 5197 slurp, err := ioutil.ReadAll(res.Body) 5198 if err != nil { 5199 t.Fatal(err) 5200 } 5201 return string(slurp) 5202 } 5203 5204 a1, a2 := get(), get() 5205 if a1 != a2 { 5206 t.Fatalf("did requests on different connections") 5207 } 5208 time.Sleep(3 * time.Second) 5209 a3 := get() 5210 if a2 == a3 { 5211 t.Fatal("request three unexpectedly on same connection") 5212 } 5213 5214 // And test that ReadHeaderTimeout still works: 5215 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 5216 if err != nil { 5217 t.Fatal(err) 5218 } 5219 defer conn.Close() 5220 conn.Write([]byte("GET / HTTP/1.1\r\nHost: foo.com\r\n")) 5221 time.Sleep(2 * time.Second) 5222 if _, err := io.CopyN(ioutil.Discard, conn, 1); err == nil { 5223 t.Fatal("copy byte succeeded; want err") 5224 } 5225 } 5226 5227 func get(t *testing.T, c *Client, url string) string { 5228 res, err := c.Get(url) 5229 if err != nil { 5230 t.Fatal(err) 5231 } 5232 defer res.Body.Close() 5233 slurp, err := ioutil.ReadAll(res.Body) 5234 if err != nil { 5235 t.Fatal(err) 5236 } 5237 return string(slurp) 5238 } 5239 5240 // Tests that calls to Server.SetKeepAlivesEnabled(false) closes any 5241 // currently-open connections. 5242 func TestServerSetKeepAlivesEnabledClosesConns(t *testing.T) { 5243 setParallel(t) 5244 defer afterTest(t) 5245 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { 5246 io.WriteString(w, r.RemoteAddr) 5247 })) 5248 defer ts.Close() 5249 5250 c := ts.Client() 5251 tr := c.Transport.(*Transport) 5252 5253 get := func() string { return get(t, c, ts.URL) } 5254 5255 a1, a2 := get(), get() 5256 if a1 != a2 { 5257 t.Fatal("expected first two requests on same connection") 5258 } 5259 var idle0 int 5260 if !waitCondition(2*time.Second, 10*time.Millisecond, func() bool { 5261 idle0 = tr.IdleConnKeyCountForTesting() 5262 return idle0 == 1 5263 }) { 5264 t.Fatalf("idle count before SetKeepAlivesEnabled called = %v; want 1", idle0) 5265 } 5266 5267 ts.Config.SetKeepAlivesEnabled(false) 5268 5269 var idle1 int 5270 if !waitCondition(2*time.Second, 10*time.Millisecond, func() bool { 5271 idle1 = tr.IdleConnKeyCountForTesting() 5272 return idle1 == 0 5273 }) { 5274 t.Fatalf("idle count after SetKeepAlivesEnabled called = %v; want 0", idle1) 5275 } 5276 5277 a3 := get() 5278 if a3 == a2 { 5279 t.Fatal("expected third request on new connection") 5280 } 5281 } 5282 5283 func TestServerShutdown_h1(t *testing.T) { testServerShutdown(t, h1Mode) } 5284 func TestServerShutdown_h2(t *testing.T) { testServerShutdown(t, h2Mode) } 5285 5286 func testServerShutdown(t *testing.T, h2 bool) { 5287 setParallel(t) 5288 defer afterTest(t) 5289 var doShutdown func() // set later 5290 var shutdownRes = make(chan error, 1) 5291 var gotOnShutdown = make(chan struct{}, 1) 5292 handler := HandlerFunc(func(w ResponseWriter, r *Request) { 5293 go doShutdown() 5294 // Shutdown is graceful, so it should not interrupt 5295 // this in-flight response. Add a tiny sleep here to 5296 // increase the odds of a failure if shutdown has 5297 // bugs. 5298 time.Sleep(20 * time.Millisecond) 5299 io.WriteString(w, r.RemoteAddr) 5300 }) 5301 cst := newClientServerTest(t, h2, handler, func(srv *httptest.Server) { 5302 srv.Config.RegisterOnShutdown(func() { gotOnShutdown <- struct{}{} }) 5303 }) 5304 defer cst.close() 5305 5306 doShutdown = func() { 5307 shutdownRes <- cst.ts.Config.Shutdown(context.Background()) 5308 } 5309 get(t, cst.c, cst.ts.URL) // calls t.Fail on failure 5310 5311 if err := <-shutdownRes; err != nil { 5312 t.Fatalf("Shutdown: %v", err) 5313 } 5314 select { 5315 case <-gotOnShutdown: 5316 case <-time.After(5 * time.Second): 5317 t.Errorf("onShutdown callback not called, RegisterOnShutdown broken?") 5318 } 5319 5320 res, err := cst.c.Get(cst.ts.URL) 5321 if err == nil { 5322 res.Body.Close() 5323 t.Fatal("second request should fail. server should be shut down") 5324 } 5325 } 5326 5327 // Issue 17878: tests that we can call Close twice. 5328 func TestServerCloseDeadlock(t *testing.T) { 5329 var s Server 5330 s.Close() 5331 s.Close() 5332 } 5333 5334 // Issue 17717: tests that Server.SetKeepAlivesEnabled is respected by 5335 // both HTTP/1 and HTTP/2. 5336 func TestServerKeepAlivesEnabled_h1(t *testing.T) { testServerKeepAlivesEnabled(t, h1Mode) } 5337 func TestServerKeepAlivesEnabled_h2(t *testing.T) { testServerKeepAlivesEnabled(t, h2Mode) } 5338 func testServerKeepAlivesEnabled(t *testing.T, h2 bool) { 5339 setParallel(t) 5340 defer afterTest(t) 5341 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 5342 fmt.Fprintf(w, "%v", r.RemoteAddr) 5343 })) 5344 defer cst.close() 5345 srv := cst.ts.Config 5346 srv.SetKeepAlivesEnabled(false) 5347 a := cst.getURL(cst.ts.URL) 5348 if !waitCondition(2*time.Second, 10*time.Millisecond, srv.ExportAllConnsIdle) { 5349 t.Fatalf("test server has active conns") 5350 } 5351 b := cst.getURL(cst.ts.URL) 5352 if a == b { 5353 t.Errorf("got same connection between first and second requests") 5354 } 5355 if !waitCondition(2*time.Second, 10*time.Millisecond, srv.ExportAllConnsIdle) { 5356 t.Fatalf("test server has active conns") 5357 } 5358 } 5359 5360 // Issue 18447: test that the Server's ReadTimeout is stopped while 5361 // the server's doing its 1-byte background read between requests, 5362 // waiting for the connection to maybe close. 5363 func TestServerCancelsReadTimeoutWhenIdle(t *testing.T) { 5364 setParallel(t) 5365 defer afterTest(t) 5366 const timeout = 250 * time.Millisecond 5367 ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) { 5368 select { 5369 case <-time.After(2 * timeout): 5370 fmt.Fprint(w, "ok") 5371 case <-r.Context().Done(): 5372 fmt.Fprint(w, r.Context().Err()) 5373 } 5374 })) 5375 ts.Config.ReadTimeout = timeout 5376 ts.Start() 5377 defer ts.Close() 5378 5379 c := ts.Client() 5380 5381 res, err := c.Get(ts.URL) 5382 if err != nil { 5383 t.Fatal(err) 5384 } 5385 slurp, err := ioutil.ReadAll(res.Body) 5386 res.Body.Close() 5387 if err != nil { 5388 t.Fatal(err) 5389 } 5390 if string(slurp) != "ok" { 5391 t.Fatalf("Got: %q, want ok", slurp) 5392 } 5393 } 5394 5395 // Issue 18535: test that the Server doesn't try to do a background 5396 // read if it's already done one. 5397 func TestServerDuplicateBackgroundRead(t *testing.T) { 5398 setParallel(t) 5399 defer afterTest(t) 5400 5401 const goroutines = 5 5402 const requests = 2000 5403 5404 hts := httptest.NewServer(HandlerFunc(NotFound)) 5405 defer hts.Close() 5406 5407 reqBytes := []byte("GET / HTTP/1.1\r\nHost: e.com\r\n\r\n") 5408 5409 var wg sync.WaitGroup 5410 for i := 0; i < goroutines; i++ { 5411 wg.Add(1) 5412 go func() { 5413 defer wg.Done() 5414 cn, err := net.Dial("tcp", hts.Listener.Addr().String()) 5415 if err != nil { 5416 t.Error(err) 5417 return 5418 } 5419 defer cn.Close() 5420 5421 wg.Add(1) 5422 go func() { 5423 defer wg.Done() 5424 io.Copy(ioutil.Discard, cn) 5425 }() 5426 5427 for j := 0; j < requests; j++ { 5428 if t.Failed() { 5429 return 5430 } 5431 _, err := cn.Write(reqBytes) 5432 if err != nil { 5433 t.Error(err) 5434 return 5435 } 5436 } 5437 }() 5438 } 5439 wg.Wait() 5440 } 5441 5442 // Test that the bufio.Reader returned by Hijack includes any buffered 5443 // byte (from the Server's backgroundRead) in its buffer. We want the 5444 // Handler code to be able to tell that a byte is available via 5445 // bufio.Reader.Buffered(), without resorting to Reading it 5446 // (potentially blocking) to get at it. 5447 func TestServerHijackGetsBackgroundByte(t *testing.T) { 5448 if runtime.GOOS == "plan9" { 5449 t.Skip("skipping test; see https://golang.org/issue/18657") 5450 } 5451 setParallel(t) 5452 defer afterTest(t) 5453 done := make(chan struct{}) 5454 inHandler := make(chan bool, 1) 5455 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { 5456 defer close(done) 5457 5458 // Tell the client to send more data after the GET request. 5459 inHandler <- true 5460 5461 // Wait until the HTTP server sees the extra data 5462 // after the GET request. The HTTP server fires the 5463 // close notifier here, assuming it's a pipelined 5464 // request, as documented. 5465 select { 5466 case <-w.(CloseNotifier).CloseNotify(): 5467 case <-time.After(5 * time.Second): 5468 t.Error("timeout") 5469 return 5470 } 5471 5472 conn, buf, err := w.(Hijacker).Hijack() 5473 if err != nil { 5474 t.Error(err) 5475 return 5476 } 5477 defer conn.Close() 5478 n := buf.Reader.Buffered() 5479 if n != 1 { 5480 t.Errorf("buffered data = %d; want 1", n) 5481 } 5482 peek, err := buf.Reader.Peek(3) 5483 if string(peek) != "foo" || err != nil { 5484 t.Errorf("Peek = %q, %v; want foo, nil", peek, err) 5485 } 5486 })) 5487 defer ts.Close() 5488 5489 cn, err := net.Dial("tcp", ts.Listener.Addr().String()) 5490 if err != nil { 5491 t.Fatal(err) 5492 } 5493 defer cn.Close() 5494 if _, err := cn.Write([]byte("GET / HTTP/1.1\r\nHost: e.com\r\n\r\n")); err != nil { 5495 t.Fatal(err) 5496 } 5497 <-inHandler 5498 if _, err := cn.Write([]byte("foo")); err != nil { 5499 t.Fatal(err) 5500 } 5501 5502 if err := cn.(*net.TCPConn).CloseWrite(); err != nil { 5503 t.Fatal(err) 5504 } 5505 select { 5506 case <-done: 5507 case <-time.After(2 * time.Second): 5508 t.Error("timeout") 5509 } 5510 } 5511 5512 // Like TestServerHijackGetsBackgroundByte above but sending a 5513 // immediate 1MB of data to the server to fill up the server's 4KB 5514 // buffer. 5515 func TestServerHijackGetsBackgroundByte_big(t *testing.T) { 5516 if runtime.GOOS == "plan9" { 5517 t.Skip("skipping test; see https://golang.org/issue/18657") 5518 } 5519 setParallel(t) 5520 defer afterTest(t) 5521 done := make(chan struct{}) 5522 const size = 8 << 10 5523 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { 5524 defer close(done) 5525 5526 // Wait until the HTTP server sees the extra data 5527 // after the GET request. The HTTP server fires the 5528 // close notifier here, assuming it's a pipelined 5529 // request, as documented. 5530 select { 5531 case <-w.(CloseNotifier).CloseNotify(): 5532 case <-time.After(5 * time.Second): 5533 t.Error("timeout") 5534 return 5535 } 5536 5537 conn, buf, err := w.(Hijacker).Hijack() 5538 if err != nil { 5539 t.Error(err) 5540 return 5541 } 5542 defer conn.Close() 5543 slurp, err := ioutil.ReadAll(buf.Reader) 5544 if err != nil { 5545 t.Errorf("Copy: %v", err) 5546 } 5547 allX := true 5548 for _, v := range slurp { 5549 if v != 'x' { 5550 allX = false 5551 } 5552 } 5553 if len(slurp) != size { 5554 t.Errorf("read %d; want %d", len(slurp), size) 5555 } else if !allX { 5556 t.Errorf("read %q; want %d 'x'", slurp, size) 5557 } 5558 })) 5559 defer ts.Close() 5560 5561 cn, err := net.Dial("tcp", ts.Listener.Addr().String()) 5562 if err != nil { 5563 t.Fatal(err) 5564 } 5565 defer cn.Close() 5566 if _, err := fmt.Fprintf(cn, "GET / HTTP/1.1\r\nHost: e.com\r\n\r\n%s", 5567 strings.Repeat("x", size)); err != nil { 5568 t.Fatal(err) 5569 } 5570 if err := cn.(*net.TCPConn).CloseWrite(); err != nil { 5571 t.Fatal(err) 5572 } 5573 5574 select { 5575 case <-done: 5576 case <-time.After(2 * time.Second): 5577 t.Error("timeout") 5578 } 5579 } 5580 5581 // Issue 18319: test that the Server validates the request method. 5582 func TestServerValidatesMethod(t *testing.T) { 5583 tests := []struct { 5584 method string 5585 want int 5586 }{ 5587 {"GET", 200}, 5588 {"GE(T", 400}, 5589 } 5590 for _, tt := range tests { 5591 conn := &testConn{closec: make(chan bool, 1)} 5592 io.WriteString(&conn.readBuf, tt.method+" / HTTP/1.1\r\nHost: foo.example\r\n\r\n") 5593 5594 ln := &oneConnListener{conn} 5595 go Serve(ln, serve(200)) 5596 <-conn.closec 5597 res, err := ReadResponse(bufio.NewReader(&conn.writeBuf), nil) 5598 if err != nil { 5599 t.Errorf("For %s, ReadResponse: %v", tt.method, res) 5600 continue 5601 } 5602 if res.StatusCode != tt.want { 5603 t.Errorf("For %s, Status = %d; want %d", tt.method, res.StatusCode, tt.want) 5604 } 5605 } 5606 } 5607 5608 func BenchmarkResponseStatusLine(b *testing.B) { 5609 b.ReportAllocs() 5610 b.RunParallel(func(pb *testing.PB) { 5611 bw := bufio.NewWriter(ioutil.Discard) 5612 var buf3 [3]byte 5613 for pb.Next() { 5614 Export_writeStatusLine(bw, true, 200, buf3[:]) 5615 } 5616 }) 5617 }