github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/net/http2/server_test.go (about) 1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package http2 6 7 import ( 8 "bytes" 9 "compress/gzip" 10 "compress/zlib" 11 "context" 12 "errors" 13 "flag" 14 "fmt" 15 "io" 16 "io/ioutil" 17 "log" 18 "net" 19 "os" 20 "os/exec" 21 "reflect" 22 "runtime" 23 "strconv" 24 "strings" 25 "sync" 26 "sync/atomic" 27 "testing" 28 "time" 29 30 http "github.com/hxx258456/ccgo/gmhttp" 31 "github.com/hxx258456/ccgo/gmhttp/httptest" 32 tls "github.com/hxx258456/ccgo/gmtls" 33 "github.com/hxx258456/ccgo/net/http2/hpack" 34 ) 35 36 var stderrVerbose = flag.Bool("stderr_verbose", false, "Mirror verbosity to stderr, unbuffered") 37 38 func stderrv() io.Writer { 39 if *stderrVerbose { 40 return os.Stderr 41 } 42 43 return ioutil.Discard 44 } 45 46 type safeBuffer struct { 47 b bytes.Buffer 48 m sync.Mutex 49 } 50 51 func (sb *safeBuffer) Write(d []byte) (int, error) { 52 sb.m.Lock() 53 defer sb.m.Unlock() 54 return sb.b.Write(d) 55 } 56 57 func (sb *safeBuffer) Bytes() []byte { 58 sb.m.Lock() 59 defer sb.m.Unlock() 60 return sb.b.Bytes() 61 } 62 63 func (sb *safeBuffer) Len() int { 64 sb.m.Lock() 65 defer sb.m.Unlock() 66 return sb.b.Len() 67 } 68 69 type serverTester struct { 70 cc net.Conn // client conn 71 t testing.TB 72 ts *httptest.Server 73 fr *Framer 74 serverLogBuf safeBuffer // logger for httptest.Server 75 logFilter []string // substrings to filter out 76 scMu sync.Mutex // guards sc 77 sc *serverConn 78 hpackDec *hpack.Decoder 79 decodedHeaders [][2]string 80 81 // If http2debug!=2, then we capture Frame debug logs that will be written 82 // to t.Log after a test fails. The read and write logs use separate locks 83 // and buffers so we don't accidentally introduce synchronization between 84 // the read and write goroutines, which may hide data races. 85 frameReadLogMu sync.Mutex 86 frameReadLogBuf bytes.Buffer 87 frameWriteLogMu sync.Mutex 88 frameWriteLogBuf bytes.Buffer 89 90 // writing headers: 91 headerBuf bytes.Buffer 92 hpackEnc *hpack.Encoder 93 } 94 95 func init() { 96 testHookOnPanicMu = new(sync.Mutex) 97 goAwayTimeout = 25 * time.Millisecond 98 } 99 100 func resetHooks() { 101 testHookOnPanicMu.Lock() 102 testHookOnPanic = nil 103 testHookOnPanicMu.Unlock() 104 } 105 106 type serverTesterOpt string 107 108 var optOnlyServer = serverTesterOpt("only_server") 109 var optQuiet = serverTesterOpt("quiet_logging") 110 var optFramerReuseFrames = serverTesterOpt("frame_reuse_frames") 111 112 func newServerTester(t testing.TB, handler http.HandlerFunc, opts ...interface{}) *serverTester { 113 resetHooks() 114 115 ts := httptest.NewUnstartedServer(handler) 116 117 tlsConfig := &tls.Config{ 118 InsecureSkipVerify: true, 119 NextProtos: []string{NextProtoTLS}, 120 } 121 122 var onlyServer, quiet, framerReuseFrames bool 123 h2server := new(Server) 124 for _, opt := range opts { 125 switch v := opt.(type) { 126 case func(*tls.Config): 127 v(tlsConfig) 128 case func(*httptest.Server): 129 v(ts) 130 case func(*Server): 131 v(h2server) 132 case serverTesterOpt: 133 switch v { 134 case optOnlyServer: 135 onlyServer = true 136 case optQuiet: 137 quiet = true 138 case optFramerReuseFrames: 139 framerReuseFrames = true 140 } 141 case func(net.Conn, http.ConnState): 142 ts.Config.ConnState = v 143 default: 144 t.Fatalf("unknown newServerTester option type %T", v) 145 } 146 } 147 148 ConfigureServer(ts.Config, h2server) 149 150 st := &serverTester{ 151 t: t, 152 ts: ts, 153 } 154 st.hpackEnc = hpack.NewEncoder(&st.headerBuf) 155 st.hpackDec = hpack.NewDecoder(initialHeaderTableSize, st.onHeaderField) 156 157 ts.TLS = ts.Config.TLSConfig // the httptest.Server has its own copy of this TLS config 158 if quiet { 159 ts.Config.ErrorLog = log.New(ioutil.Discard, "", 0) 160 } else { 161 ts.Config.ErrorLog = log.New(io.MultiWriter(stderrv(), twriter{t: t, st: st}, &st.serverLogBuf), "", log.LstdFlags) 162 } 163 ts.StartTLS() 164 165 if VerboseLogs { 166 t.Logf("Running test server at: %s", ts.URL) 167 } 168 testHookGetServerConn = func(v *serverConn) { 169 st.scMu.Lock() 170 defer st.scMu.Unlock() 171 st.sc = v 172 } 173 log.SetOutput(io.MultiWriter(stderrv(), twriter{t: t, st: st})) 174 if !onlyServer { 175 cc, err := tls.Dial("tcp", ts.Listener.Addr().String(), tlsConfig) 176 if err != nil { 177 t.Fatal(err) 178 } 179 st.cc = cc 180 st.fr = NewFramer(cc, cc) 181 if framerReuseFrames { 182 st.fr.SetReuseFrames() 183 } 184 if !logFrameReads && !logFrameWrites { 185 st.fr.debugReadLoggerf = func(m string, v ...interface{}) { 186 m = time.Now().Format("2006-01-02 15:04:05.999999999 ") + strings.TrimPrefix(m, "http2: ") + "\n" 187 st.frameReadLogMu.Lock() 188 fmt.Fprintf(&st.frameReadLogBuf, m, v...) 189 st.frameReadLogMu.Unlock() 190 } 191 st.fr.debugWriteLoggerf = func(m string, v ...interface{}) { 192 m = time.Now().Format("2006-01-02 15:04:05.999999999 ") + strings.TrimPrefix(m, "http2: ") + "\n" 193 st.frameWriteLogMu.Lock() 194 fmt.Fprintf(&st.frameWriteLogBuf, m, v...) 195 st.frameWriteLogMu.Unlock() 196 } 197 st.fr.logReads = true 198 st.fr.logWrites = true 199 } 200 } 201 return st 202 } 203 204 func (st *serverTester) closeConn() { 205 st.scMu.Lock() 206 defer st.scMu.Unlock() 207 st.sc.conn.Close() 208 } 209 210 func (st *serverTester) addLogFilter(phrase string) { 211 st.logFilter = append(st.logFilter, phrase) 212 } 213 214 func (st *serverTester) stream(id uint32) *stream { 215 ch := make(chan *stream, 1) 216 st.sc.serveMsgCh <- func(int) { 217 ch <- st.sc.streams[id] 218 } 219 return <-ch 220 } 221 222 func (st *serverTester) streamState(id uint32) streamState { 223 ch := make(chan streamState, 1) 224 st.sc.serveMsgCh <- func(int) { 225 state, _ := st.sc.state(id) 226 ch <- state 227 } 228 return <-ch 229 } 230 231 // loopNum reports how many times this conn's select loop has gone around. 232 func (st *serverTester) loopNum() int { 233 lastc := make(chan int, 1) 234 st.sc.serveMsgCh <- func(loopNum int) { 235 lastc <- loopNum 236 } 237 return <-lastc 238 } 239 240 // awaitIdle heuristically awaits for the server conn's select loop to be idle. 241 // The heuristic is that the server connection's serve loop must schedule 242 // 50 times in a row without any channel sends or receives occurring. 243 func (st *serverTester) awaitIdle() { 244 remain := 50 245 last := st.loopNum() 246 for remain > 0 { 247 n := st.loopNum() 248 if n == last+1 { 249 remain-- 250 } else { 251 remain = 50 252 } 253 last = n 254 } 255 } 256 257 func (st *serverTester) Close() { 258 if st.t.Failed() { 259 st.frameReadLogMu.Lock() 260 if st.frameReadLogBuf.Len() > 0 { 261 st.t.Logf("Framer read log:\n%s", st.frameReadLogBuf.String()) 262 } 263 st.frameReadLogMu.Unlock() 264 265 st.frameWriteLogMu.Lock() 266 if st.frameWriteLogBuf.Len() > 0 { 267 st.t.Logf("Framer write log:\n%s", st.frameWriteLogBuf.String()) 268 } 269 st.frameWriteLogMu.Unlock() 270 271 // If we failed already (and are likely in a Fatal, 272 // unwindowing), force close the connection, so the 273 // httptest.Server doesn't wait forever for the conn 274 // to close. 275 if st.cc != nil { 276 st.cc.Close() 277 } 278 } 279 st.ts.Close() 280 if st.cc != nil { 281 st.cc.Close() 282 } 283 log.SetOutput(os.Stderr) 284 } 285 286 // greet initiates the client's HTTP/2 connection into a state where 287 // frames may be sent. 288 func (st *serverTester) greet() { 289 st.greetAndCheckSettings(func(Setting) error { return nil }) 290 } 291 292 func (st *serverTester) greetAndCheckSettings(checkSetting func(s Setting) error) { 293 st.writePreface() 294 st.writeInitialSettings() 295 st.wantSettings().ForeachSetting(checkSetting) 296 st.writeSettingsAck() 297 298 // The initial WINDOW_UPDATE and SETTINGS ACK can come in any order. 299 var gotSettingsAck bool 300 var gotWindowUpdate bool 301 302 for i := 0; i < 2; i++ { 303 f, err := st.readFrame() 304 if err != nil { 305 st.t.Fatal(err) 306 } 307 switch f := f.(type) { 308 case *SettingsFrame: 309 if !f.Header().Flags.Has(FlagSettingsAck) { 310 st.t.Fatal("Settings Frame didn't have ACK set") 311 } 312 gotSettingsAck = true 313 314 case *WindowUpdateFrame: 315 if f.FrameHeader.StreamID != 0 { 316 st.t.Fatalf("WindowUpdate StreamID = %d; want 0", f.FrameHeader.StreamID) 317 } 318 incr := uint32((&Server{}).initialConnRecvWindowSize() - initialWindowSize) 319 if f.Increment != incr { 320 st.t.Fatalf("WindowUpdate increment = %d; want %d", f.Increment, incr) 321 } 322 gotWindowUpdate = true 323 324 default: 325 st.t.Fatalf("Wanting a settings ACK or window update, received a %T", f) 326 } 327 } 328 329 if !gotSettingsAck { 330 st.t.Fatalf("Didn't get a settings ACK") 331 } 332 if !gotWindowUpdate { 333 st.t.Fatalf("Didn't get a window update") 334 } 335 } 336 337 func (st *serverTester) writePreface() { 338 n, err := st.cc.Write(clientPreface) 339 if err != nil { 340 st.t.Fatalf("Error writing client preface: %v", err) 341 } 342 if n != len(clientPreface) { 343 st.t.Fatalf("Writing client preface, wrote %d bytes; want %d", n, len(clientPreface)) 344 } 345 } 346 347 func (st *serverTester) writeInitialSettings() { 348 if err := st.fr.WriteSettings(); err != nil { 349 st.t.Fatalf("Error writing initial SETTINGS frame from client to server: %v", err) 350 } 351 } 352 353 func (st *serverTester) writeSettingsAck() { 354 if err := st.fr.WriteSettingsAck(); err != nil { 355 st.t.Fatalf("Error writing ACK of server's SETTINGS: %v", err) 356 } 357 } 358 359 func (st *serverTester) writeHeaders(p HeadersFrameParam) { 360 if err := st.fr.WriteHeaders(p); err != nil { 361 st.t.Fatalf("Error writing HEADERS: %v", err) 362 } 363 } 364 365 func (st *serverTester) writePriority(id uint32, p PriorityParam) { 366 if err := st.fr.WritePriority(id, p); err != nil { 367 st.t.Fatalf("Error writing PRIORITY: %v", err) 368 } 369 } 370 371 func (st *serverTester) encodeHeaderField(k, v string) { 372 err := st.hpackEnc.WriteField(hpack.HeaderField{Name: k, Value: v}) 373 if err != nil { 374 st.t.Fatalf("HPACK encoding error for %q/%q: %v", k, v, err) 375 } 376 } 377 378 // encodeHeaderRaw is the magic-free version of encodeHeader. 379 // It takes 0 or more (k, v) pairs and encodes them. 380 func (st *serverTester) encodeHeaderRaw(headers ...string) []byte { 381 if len(headers)%2 == 1 { 382 panic("odd number of kv args") 383 } 384 st.headerBuf.Reset() 385 for len(headers) > 0 { 386 k, v := headers[0], headers[1] 387 st.encodeHeaderField(k, v) 388 headers = headers[2:] 389 } 390 return st.headerBuf.Bytes() 391 } 392 393 // encodeHeader encodes headers and returns their HPACK bytes. headers 394 // must contain an even number of key/value pairs. There may be 395 // multiple pairs for keys (e.g. "cookie"). The :method, :path, and 396 // :scheme headers default to GET, / and https. The :authority header 397 // defaults to st.ts.Listener.Addr(). 398 func (st *serverTester) encodeHeader(headers ...string) []byte { 399 if len(headers)%2 == 1 { 400 panic("odd number of kv args") 401 } 402 403 st.headerBuf.Reset() 404 defaultAuthority := st.ts.Listener.Addr().String() 405 406 if len(headers) == 0 { 407 // Fast path, mostly for benchmarks, so test code doesn't pollute 408 // profiles when we're looking to improve server allocations. 409 st.encodeHeaderField(":method", "GET") 410 st.encodeHeaderField(":scheme", "https") 411 st.encodeHeaderField(":authority", defaultAuthority) 412 st.encodeHeaderField(":path", "/") 413 return st.headerBuf.Bytes() 414 } 415 416 if len(headers) == 2 && headers[0] == ":method" { 417 // Another fast path for benchmarks. 418 st.encodeHeaderField(":method", headers[1]) 419 st.encodeHeaderField(":scheme", "https") 420 st.encodeHeaderField(":authority", defaultAuthority) 421 st.encodeHeaderField(":path", "/") 422 return st.headerBuf.Bytes() 423 } 424 425 pseudoCount := map[string]int{} 426 keys := []string{":method", ":scheme", ":authority", ":path"} 427 vals := map[string][]string{ 428 ":method": {"GET"}, 429 ":scheme": {"https"}, 430 ":authority": {defaultAuthority}, 431 ":path": {"/"}, 432 } 433 for len(headers) > 0 { 434 k, v := headers[0], headers[1] 435 headers = headers[2:] 436 if _, ok := vals[k]; !ok { 437 keys = append(keys, k) 438 } 439 if strings.HasPrefix(k, ":") { 440 pseudoCount[k]++ 441 if pseudoCount[k] == 1 { 442 vals[k] = []string{v} 443 } else { 444 // Allows testing of invalid headers w/ dup pseudo fields. 445 vals[k] = append(vals[k], v) 446 } 447 } else { 448 vals[k] = append(vals[k], v) 449 } 450 } 451 for _, k := range keys { 452 for _, v := range vals[k] { 453 st.encodeHeaderField(k, v) 454 } 455 } 456 return st.headerBuf.Bytes() 457 } 458 459 // bodylessReq1 writes a HEADERS frames with StreamID 1 and EndStream and EndHeaders set. 460 func (st *serverTester) bodylessReq1(headers ...string) { 461 st.writeHeaders(HeadersFrameParam{ 462 StreamID: 1, // clients send odd numbers 463 BlockFragment: st.encodeHeader(headers...), 464 EndStream: true, 465 EndHeaders: true, 466 }) 467 } 468 469 func (st *serverTester) writeData(streamID uint32, endStream bool, data []byte) { 470 if err := st.fr.WriteData(streamID, endStream, data); err != nil { 471 st.t.Fatalf("Error writing DATA: %v", err) 472 } 473 } 474 475 func (st *serverTester) writeDataPadded(streamID uint32, endStream bool, data, pad []byte) { 476 if err := st.fr.WriteDataPadded(streamID, endStream, data, pad); err != nil { 477 st.t.Fatalf("Error writing DATA: %v", err) 478 } 479 } 480 481 func (st *serverTester) readFrame() (Frame, error) { 482 return st.fr.ReadFrame() 483 } 484 485 func (st *serverTester) wantHeaders() *HeadersFrame { 486 f, err := st.readFrame() 487 if err != nil { 488 st.t.Fatalf("Error while expecting a HEADERS frame: %v", err) 489 } 490 hf, ok := f.(*HeadersFrame) 491 if !ok { 492 st.t.Fatalf("got a %T; want *HeadersFrame", f) 493 } 494 return hf 495 } 496 497 func (st *serverTester) wantContinuation() *ContinuationFrame { 498 f, err := st.readFrame() 499 if err != nil { 500 st.t.Fatalf("Error while expecting a CONTINUATION frame: %v", err) 501 } 502 cf, ok := f.(*ContinuationFrame) 503 if !ok { 504 st.t.Fatalf("got a %T; want *ContinuationFrame", f) 505 } 506 return cf 507 } 508 509 func (st *serverTester) wantData() *DataFrame { 510 f, err := st.readFrame() 511 if err != nil { 512 st.t.Fatalf("Error while expecting a DATA frame: %v", err) 513 } 514 df, ok := f.(*DataFrame) 515 if !ok { 516 st.t.Fatalf("got a %T; want *DataFrame", f) 517 } 518 return df 519 } 520 521 func (st *serverTester) wantSettings() *SettingsFrame { 522 f, err := st.readFrame() 523 if err != nil { 524 st.t.Fatalf("Error while expecting a SETTINGS frame: %v", err) 525 } 526 sf, ok := f.(*SettingsFrame) 527 if !ok { 528 st.t.Fatalf("got a %T; want *SettingsFrame", f) 529 } 530 return sf 531 } 532 533 func (st *serverTester) wantPing() *PingFrame { 534 f, err := st.readFrame() 535 if err != nil { 536 st.t.Fatalf("Error while expecting a PING frame: %v", err) 537 } 538 pf, ok := f.(*PingFrame) 539 if !ok { 540 st.t.Fatalf("got a %T; want *PingFrame", f) 541 } 542 return pf 543 } 544 545 func (st *serverTester) wantGoAway() *GoAwayFrame { 546 f, err := st.readFrame() 547 if err != nil { 548 st.t.Fatalf("Error while expecting a GOAWAY frame: %v", err) 549 } 550 gf, ok := f.(*GoAwayFrame) 551 if !ok { 552 st.t.Fatalf("got a %T; want *GoAwayFrame", f) 553 } 554 return gf 555 } 556 557 func (st *serverTester) wantRSTStream(streamID uint32, errCode ErrCode) { 558 f, err := st.readFrame() 559 if err != nil { 560 st.t.Fatalf("Error while expecting an RSTStream frame: %v", err) 561 } 562 rs, ok := f.(*RSTStreamFrame) 563 if !ok { 564 st.t.Fatalf("got a %T; want *RSTStreamFrame", f) 565 } 566 if rs.FrameHeader.StreamID != streamID { 567 st.t.Fatalf("RSTStream StreamID = %d; want %d", rs.FrameHeader.StreamID, streamID) 568 } 569 if rs.ErrCode != errCode { 570 st.t.Fatalf("RSTStream ErrCode = %d (%s); want %d (%s)", rs.ErrCode, rs.ErrCode, errCode, errCode) 571 } 572 } 573 574 func (st *serverTester) wantWindowUpdate(streamID, incr uint32) { 575 f, err := st.readFrame() 576 if err != nil { 577 st.t.Fatalf("Error while expecting a WINDOW_UPDATE frame: %v", err) 578 } 579 wu, ok := f.(*WindowUpdateFrame) 580 if !ok { 581 st.t.Fatalf("got a %T; want *WindowUpdateFrame", f) 582 } 583 if wu.FrameHeader.StreamID != streamID { 584 st.t.Fatalf("WindowUpdate StreamID = %d; want %d", wu.FrameHeader.StreamID, streamID) 585 } 586 if wu.Increment != incr { 587 st.t.Fatalf("WindowUpdate increment = %d; want %d", wu.Increment, incr) 588 } 589 } 590 591 func (st *serverTester) wantSettingsAck() { 592 f, err := st.readFrame() 593 if err != nil { 594 st.t.Fatal(err) 595 } 596 sf, ok := f.(*SettingsFrame) 597 if !ok { 598 st.t.Fatalf("Wanting a settings ACK, received a %T", f) 599 } 600 if !sf.Header().Flags.Has(FlagSettingsAck) { 601 st.t.Fatal("Settings Frame didn't have ACK set") 602 } 603 } 604 605 func (st *serverTester) wantPushPromise() *PushPromiseFrame { 606 f, err := st.readFrame() 607 if err != nil { 608 st.t.Fatal(err) 609 } 610 ppf, ok := f.(*PushPromiseFrame) 611 if !ok { 612 st.t.Fatalf("Wanted PushPromise, received %T", ppf) 613 } 614 return ppf 615 } 616 617 func TestServer(t *testing.T) { 618 gotReq := make(chan bool, 1) 619 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 620 w.Header().Set("Foo", "Bar") 621 gotReq <- true 622 }) 623 defer st.Close() 624 625 covers("3.5", ` 626 The server connection preface consists of a potentially empty 627 SETTINGS frame ([SETTINGS]) that MUST be the first frame the 628 server sends in the HTTP/2 connection. 629 `) 630 631 st.greet() 632 st.writeHeaders(HeadersFrameParam{ 633 StreamID: 1, // clients send odd numbers 634 BlockFragment: st.encodeHeader(), 635 EndStream: true, // no DATA frames 636 EndHeaders: true, 637 }) 638 639 select { 640 case <-gotReq: 641 case <-time.After(2 * time.Second): 642 t.Error("timeout waiting for request") 643 } 644 } 645 646 func TestServer_Request_Get(t *testing.T) { 647 testServerRequest(t, func(st *serverTester) { 648 st.writeHeaders(HeadersFrameParam{ 649 StreamID: 1, // clients send odd numbers 650 BlockFragment: st.encodeHeader("foo-bar", "some-value"), 651 EndStream: true, // no DATA frames 652 EndHeaders: true, 653 }) 654 }, func(r *http.Request) { 655 if r.Method != "GET" { 656 t.Errorf("Method = %q; want GET", r.Method) 657 } 658 if r.URL.Path != "/" { 659 t.Errorf("URL.Path = %q; want /", r.URL.Path) 660 } 661 if r.ContentLength != 0 { 662 t.Errorf("ContentLength = %v; want 0", r.ContentLength) 663 } 664 if r.Close { 665 t.Error("Close = true; want false") 666 } 667 if !strings.Contains(r.RemoteAddr, ":") { 668 t.Errorf("RemoteAddr = %q; want something with a colon", r.RemoteAddr) 669 } 670 if r.Proto != "HTTP/2.0" || r.ProtoMajor != 2 || r.ProtoMinor != 0 { 671 t.Errorf("Proto = %q Major=%v,Minor=%v; want HTTP/2.0", r.Proto, r.ProtoMajor, r.ProtoMinor) 672 } 673 wantHeader := http.Header{ 674 "Foo-Bar": []string{"some-value"}, 675 } 676 if !reflect.DeepEqual(r.Header, wantHeader) { 677 t.Errorf("Header = %#v; want %#v", r.Header, wantHeader) 678 } 679 if n, err := r.Body.Read([]byte(" ")); err != io.EOF || n != 0 { 680 t.Errorf("Read = %d, %v; want 0, EOF", n, err) 681 } 682 }) 683 } 684 685 func TestServer_Request_Get_PathSlashes(t *testing.T) { 686 testServerRequest(t, func(st *serverTester) { 687 st.writeHeaders(HeadersFrameParam{ 688 StreamID: 1, // clients send odd numbers 689 BlockFragment: st.encodeHeader(":path", "/%2f/"), 690 EndStream: true, // no DATA frames 691 EndHeaders: true, 692 }) 693 }, func(r *http.Request) { 694 if r.RequestURI != "/%2f/" { 695 t.Errorf("RequestURI = %q; want /%%2f/", r.RequestURI) 696 } 697 if r.URL.Path != "///" { 698 t.Errorf("URL.Path = %q; want ///", r.URL.Path) 699 } 700 }) 701 } 702 703 // TODO: add a test with EndStream=true on the HEADERS but setting a 704 // Content-Length anyway. Should we just omit it and force it to 705 // zero? 706 707 func TestServer_Request_Post_NoContentLength_EndStream(t *testing.T) { 708 testServerRequest(t, func(st *serverTester) { 709 st.writeHeaders(HeadersFrameParam{ 710 StreamID: 1, // clients send odd numbers 711 BlockFragment: st.encodeHeader(":method", "POST"), 712 EndStream: true, 713 EndHeaders: true, 714 }) 715 }, func(r *http.Request) { 716 if r.Method != "POST" { 717 t.Errorf("Method = %q; want POST", r.Method) 718 } 719 if r.ContentLength != 0 { 720 t.Errorf("ContentLength = %v; want 0", r.ContentLength) 721 } 722 if n, err := r.Body.Read([]byte(" ")); err != io.EOF || n != 0 { 723 t.Errorf("Read = %d, %v; want 0, EOF", n, err) 724 } 725 }) 726 } 727 728 func TestServer_Request_Post_Body_ImmediateEOF(t *testing.T) { 729 testBodyContents(t, -1, "", func(st *serverTester) { 730 st.writeHeaders(HeadersFrameParam{ 731 StreamID: 1, // clients send odd numbers 732 BlockFragment: st.encodeHeader(":method", "POST"), 733 EndStream: false, // to say DATA frames are coming 734 EndHeaders: true, 735 }) 736 st.writeData(1, true, nil) // just kidding. empty body. 737 }) 738 } 739 740 func TestServer_Request_Post_Body_OneData(t *testing.T) { 741 const content = "Some content" 742 testBodyContents(t, -1, content, func(st *serverTester) { 743 st.writeHeaders(HeadersFrameParam{ 744 StreamID: 1, // clients send odd numbers 745 BlockFragment: st.encodeHeader(":method", "POST"), 746 EndStream: false, // to say DATA frames are coming 747 EndHeaders: true, 748 }) 749 st.writeData(1, true, []byte(content)) 750 }) 751 } 752 753 func TestServer_Request_Post_Body_TwoData(t *testing.T) { 754 const content = "Some content" 755 testBodyContents(t, -1, content, func(st *serverTester) { 756 st.writeHeaders(HeadersFrameParam{ 757 StreamID: 1, // clients send odd numbers 758 BlockFragment: st.encodeHeader(":method", "POST"), 759 EndStream: false, // to say DATA frames are coming 760 EndHeaders: true, 761 }) 762 st.writeData(1, false, []byte(content[:5])) 763 st.writeData(1, true, []byte(content[5:])) 764 }) 765 } 766 767 func TestServer_Request_Post_Body_ContentLength_Correct(t *testing.T) { 768 const content = "Some content" 769 testBodyContents(t, int64(len(content)), content, func(st *serverTester) { 770 st.writeHeaders(HeadersFrameParam{ 771 StreamID: 1, // clients send odd numbers 772 BlockFragment: st.encodeHeader( 773 ":method", "POST", 774 "content-length", strconv.Itoa(len(content)), 775 ), 776 EndStream: false, // to say DATA frames are coming 777 EndHeaders: true, 778 }) 779 st.writeData(1, true, []byte(content)) 780 }) 781 } 782 783 func TestServer_Request_Post_Body_ContentLength_TooLarge(t *testing.T) { 784 testBodyContentsFail(t, 3, "request declared a Content-Length of 3 but only wrote 2 bytes", 785 func(st *serverTester) { 786 st.writeHeaders(HeadersFrameParam{ 787 StreamID: 1, // clients send odd numbers 788 BlockFragment: st.encodeHeader( 789 ":method", "POST", 790 "content-length", "3", 791 ), 792 EndStream: false, // to say DATA frames are coming 793 EndHeaders: true, 794 }) 795 st.writeData(1, true, []byte("12")) 796 }) 797 } 798 799 func TestServer_Request_Post_Body_ContentLength_TooSmall(t *testing.T) { 800 testBodyContentsFail(t, 4, "sender tried to send more than declared Content-Length of 4 bytes", 801 func(st *serverTester) { 802 st.writeHeaders(HeadersFrameParam{ 803 StreamID: 1, // clients send odd numbers 804 BlockFragment: st.encodeHeader( 805 ":method", "POST", 806 "content-length", "4", 807 ), 808 EndStream: false, // to say DATA frames are coming 809 EndHeaders: true, 810 }) 811 st.writeData(1, true, []byte("12345")) 812 }) 813 } 814 815 func testBodyContents(t *testing.T, wantContentLength int64, wantBody string, write func(st *serverTester)) { 816 testServerRequest(t, write, func(r *http.Request) { 817 if r.Method != "POST" { 818 t.Errorf("Method = %q; want POST", r.Method) 819 } 820 if r.ContentLength != wantContentLength { 821 t.Errorf("ContentLength = %v; want %d", r.ContentLength, wantContentLength) 822 } 823 all, err := ioutil.ReadAll(r.Body) 824 if err != nil { 825 t.Fatal(err) 826 } 827 if string(all) != wantBody { 828 t.Errorf("Read = %q; want %q", all, wantBody) 829 } 830 if err := r.Body.Close(); err != nil { 831 t.Fatalf("Close: %v", err) 832 } 833 }) 834 } 835 836 func testBodyContentsFail(t *testing.T, wantContentLength int64, wantReadError string, write func(st *serverTester)) { 837 testServerRequest(t, write, func(r *http.Request) { 838 if r.Method != "POST" { 839 t.Errorf("Method = %q; want POST", r.Method) 840 } 841 if r.ContentLength != wantContentLength { 842 t.Errorf("ContentLength = %v; want %d", r.ContentLength, wantContentLength) 843 } 844 all, err := ioutil.ReadAll(r.Body) 845 if err == nil { 846 t.Fatalf("expected an error (%q) reading from the body. Successfully read %q instead.", 847 wantReadError, all) 848 } 849 if !strings.Contains(err.Error(), wantReadError) { 850 t.Fatalf("Body.Read = %v; want substring %q", err, wantReadError) 851 } 852 if err := r.Body.Close(); err != nil { 853 t.Fatalf("Close: %v", err) 854 } 855 }) 856 } 857 858 // Using a Host header, instead of :authority 859 func TestServer_Request_Get_Host(t *testing.T) { 860 const host = "example.com" 861 testServerRequest(t, func(st *serverTester) { 862 st.writeHeaders(HeadersFrameParam{ 863 StreamID: 1, // clients send odd numbers 864 BlockFragment: st.encodeHeader(":authority", "", "host", host), 865 EndStream: true, 866 EndHeaders: true, 867 }) 868 }, func(r *http.Request) { 869 if r.Host != host { 870 t.Errorf("Host = %q; want %q", r.Host, host) 871 } 872 }) 873 } 874 875 // Using an :authority pseudo-header, instead of Host 876 func TestServer_Request_Get_Authority(t *testing.T) { 877 const host = "example.com" 878 testServerRequest(t, func(st *serverTester) { 879 st.writeHeaders(HeadersFrameParam{ 880 StreamID: 1, // clients send odd numbers 881 BlockFragment: st.encodeHeader(":authority", host), 882 EndStream: true, 883 EndHeaders: true, 884 }) 885 }, func(r *http.Request) { 886 if r.Host != host { 887 t.Errorf("Host = %q; want %q", r.Host, host) 888 } 889 }) 890 } 891 892 func TestServer_Request_WithContinuation(t *testing.T) { 893 wantHeader := http.Header{ 894 "Foo-One": []string{"value-one"}, 895 "Foo-Two": []string{"value-two"}, 896 "Foo-Three": []string{"value-three"}, 897 } 898 testServerRequest(t, func(st *serverTester) { 899 fullHeaders := st.encodeHeader( 900 "foo-one", "value-one", 901 "foo-two", "value-two", 902 "foo-three", "value-three", 903 ) 904 remain := fullHeaders 905 chunks := 0 906 for len(remain) > 0 { 907 const maxChunkSize = 5 908 chunk := remain 909 if len(chunk) > maxChunkSize { 910 chunk = chunk[:maxChunkSize] 911 } 912 remain = remain[len(chunk):] 913 914 if chunks == 0 { 915 st.writeHeaders(HeadersFrameParam{ 916 StreamID: 1, // clients send odd numbers 917 BlockFragment: chunk, 918 EndStream: true, // no DATA frames 919 EndHeaders: false, // we'll have continuation frames 920 }) 921 } else { 922 err := st.fr.WriteContinuation(1, len(remain) == 0, chunk) 923 if err != nil { 924 t.Fatal(err) 925 } 926 } 927 chunks++ 928 } 929 if chunks < 2 { 930 t.Fatal("too few chunks") 931 } 932 }, func(r *http.Request) { 933 if !reflect.DeepEqual(r.Header, wantHeader) { 934 t.Errorf("Header = %#v; want %#v", r.Header, wantHeader) 935 } 936 }) 937 } 938 939 // Concatenated cookie headers. ("8.1.2.5 Compressing the Cookie Header Field") 940 func TestServer_Request_CookieConcat(t *testing.T) { 941 const host = "example.com" 942 testServerRequest(t, func(st *serverTester) { 943 st.bodylessReq1( 944 ":authority", host, 945 "cookie", "a=b", 946 "cookie", "c=d", 947 "cookie", "e=f", 948 ) 949 }, func(r *http.Request) { 950 const want = "a=b; c=d; e=f" 951 if got := r.Header.Get("Cookie"); got != want { 952 t.Errorf("Cookie = %q; want %q", got, want) 953 } 954 }) 955 } 956 957 func TestServer_Request_Reject_CapitalHeader(t *testing.T) { 958 testRejectRequest(t, func(st *serverTester) { st.bodylessReq1("UPPER", "v") }) 959 } 960 961 func TestServer_Request_Reject_HeaderFieldNameColon(t *testing.T) { 962 testRejectRequest(t, func(st *serverTester) { st.bodylessReq1("has:colon", "v") }) 963 } 964 965 func TestServer_Request_Reject_HeaderFieldNameNULL(t *testing.T) { 966 testRejectRequest(t, func(st *serverTester) { st.bodylessReq1("has\x00null", "v") }) 967 } 968 969 func TestServer_Request_Reject_HeaderFieldNameEmpty(t *testing.T) { 970 testRejectRequest(t, func(st *serverTester) { st.bodylessReq1("", "v") }) 971 } 972 973 func TestServer_Request_Reject_HeaderFieldValueNewline(t *testing.T) { 974 testRejectRequest(t, func(st *serverTester) { st.bodylessReq1("foo", "has\nnewline") }) 975 } 976 977 func TestServer_Request_Reject_HeaderFieldValueCR(t *testing.T) { 978 testRejectRequest(t, func(st *serverTester) { st.bodylessReq1("foo", "has\rcarriage") }) 979 } 980 981 func TestServer_Request_Reject_HeaderFieldValueDEL(t *testing.T) { 982 testRejectRequest(t, func(st *serverTester) { st.bodylessReq1("foo", "has\x7fdel") }) 983 } 984 985 func TestServer_Request_Reject_Pseudo_Missing_method(t *testing.T) { 986 testRejectRequest(t, func(st *serverTester) { st.bodylessReq1(":method", "") }) 987 } 988 989 func TestServer_Request_Reject_Pseudo_ExactlyOne(t *testing.T) { 990 // 8.1.2.3 Request Pseudo-Header Fields 991 // "All HTTP/2 requests MUST include exactly one valid value" ... 992 testRejectRequest(t, func(st *serverTester) { 993 st.addLogFilter("duplicate pseudo-header") 994 st.bodylessReq1(":method", "GET", ":method", "POST") 995 }) 996 } 997 998 func TestServer_Request_Reject_Pseudo_AfterRegular(t *testing.T) { 999 // 8.1.2.3 Request Pseudo-Header Fields 1000 // "All pseudo-header fields MUST appear in the header block 1001 // before regular header fields. Any request or response that 1002 // contains a pseudo-header field that appears in a header 1003 // block after a regular header field MUST be treated as 1004 // malformed (Section 8.1.2.6)." 1005 testRejectRequest(t, func(st *serverTester) { 1006 st.addLogFilter("pseudo-header after regular header") 1007 var buf bytes.Buffer 1008 enc := hpack.NewEncoder(&buf) 1009 enc.WriteField(hpack.HeaderField{Name: ":method", Value: "GET"}) 1010 enc.WriteField(hpack.HeaderField{Name: "regular", Value: "foobar"}) 1011 enc.WriteField(hpack.HeaderField{Name: ":path", Value: "/"}) 1012 enc.WriteField(hpack.HeaderField{Name: ":scheme", Value: "https"}) 1013 st.writeHeaders(HeadersFrameParam{ 1014 StreamID: 1, // clients send odd numbers 1015 BlockFragment: buf.Bytes(), 1016 EndStream: true, 1017 EndHeaders: true, 1018 }) 1019 }) 1020 } 1021 1022 func TestServer_Request_Reject_Pseudo_Missing_path(t *testing.T) { 1023 testRejectRequest(t, func(st *serverTester) { st.bodylessReq1(":path", "") }) 1024 } 1025 1026 func TestServer_Request_Reject_Pseudo_Missing_scheme(t *testing.T) { 1027 testRejectRequest(t, func(st *serverTester) { st.bodylessReq1(":scheme", "") }) 1028 } 1029 1030 func TestServer_Request_Reject_Pseudo_scheme_invalid(t *testing.T) { 1031 testRejectRequest(t, func(st *serverTester) { st.bodylessReq1(":scheme", "bogus") }) 1032 } 1033 1034 func TestServer_Request_Reject_Pseudo_Unknown(t *testing.T) { 1035 testRejectRequest(t, func(st *serverTester) { 1036 st.addLogFilter(`invalid pseudo-header ":unknown_thing"`) 1037 st.bodylessReq1(":unknown_thing", "") 1038 }) 1039 } 1040 1041 func testRejectRequest(t *testing.T, send func(*serverTester)) { 1042 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 1043 t.Error("server request made it to handler; should've been rejected") 1044 }) 1045 defer st.Close() 1046 1047 st.greet() 1048 send(st) 1049 st.wantRSTStream(1, ErrCodeProtocol) 1050 } 1051 1052 func testRejectRequestWithProtocolError(t *testing.T, send func(*serverTester)) { 1053 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 1054 t.Error("server request made it to handler; should've been rejected") 1055 }, optQuiet) 1056 defer st.Close() 1057 1058 st.greet() 1059 send(st) 1060 gf := st.wantGoAway() 1061 if gf.ErrCode != ErrCodeProtocol { 1062 t.Errorf("err code = %v; want %v", gf.ErrCode, ErrCodeProtocol) 1063 } 1064 } 1065 1066 // Section 5.1, on idle connections: "Receiving any frame other than 1067 // HEADERS or PRIORITY on a stream in this state MUST be treated as a 1068 // connection error (Section 5.4.1) of type PROTOCOL_ERROR." 1069 func TestRejectFrameOnIdle_WindowUpdate(t *testing.T) { 1070 testRejectRequestWithProtocolError(t, func(st *serverTester) { 1071 st.fr.WriteWindowUpdate(123, 456) 1072 }) 1073 } 1074 func TestRejectFrameOnIdle_Data(t *testing.T) { 1075 testRejectRequestWithProtocolError(t, func(st *serverTester) { 1076 st.fr.WriteData(123, true, nil) 1077 }) 1078 } 1079 func TestRejectFrameOnIdle_RSTStream(t *testing.T) { 1080 testRejectRequestWithProtocolError(t, func(st *serverTester) { 1081 st.fr.WriteRSTStream(123, ErrCodeCancel) 1082 }) 1083 } 1084 1085 func TestServer_Request_Connect(t *testing.T) { 1086 testServerRequest(t, func(st *serverTester) { 1087 st.writeHeaders(HeadersFrameParam{ 1088 StreamID: 1, 1089 BlockFragment: st.encodeHeaderRaw( 1090 ":method", "CONNECT", 1091 ":authority", "example.com:123", 1092 ), 1093 EndStream: true, 1094 EndHeaders: true, 1095 }) 1096 }, func(r *http.Request) { 1097 if g, w := r.Method, "CONNECT"; g != w { 1098 t.Errorf("Method = %q; want %q", g, w) 1099 } 1100 if g, w := r.RequestURI, "example.com:123"; g != w { 1101 t.Errorf("RequestURI = %q; want %q", g, w) 1102 } 1103 if g, w := r.URL.Host, "example.com:123"; g != w { 1104 t.Errorf("URL.Host = %q; want %q", g, w) 1105 } 1106 }) 1107 } 1108 1109 func TestServer_Request_Connect_InvalidPath(t *testing.T) { 1110 testServerRejectsStream(t, ErrCodeProtocol, func(st *serverTester) { 1111 st.writeHeaders(HeadersFrameParam{ 1112 StreamID: 1, 1113 BlockFragment: st.encodeHeaderRaw( 1114 ":method", "CONNECT", 1115 ":authority", "example.com:123", 1116 ":path", "/bogus", 1117 ), 1118 EndStream: true, 1119 EndHeaders: true, 1120 }) 1121 }) 1122 } 1123 1124 func TestServer_Request_Connect_InvalidScheme(t *testing.T) { 1125 testServerRejectsStream(t, ErrCodeProtocol, func(st *serverTester) { 1126 st.writeHeaders(HeadersFrameParam{ 1127 StreamID: 1, 1128 BlockFragment: st.encodeHeaderRaw( 1129 ":method", "CONNECT", 1130 ":authority", "example.com:123", 1131 ":scheme", "https", 1132 ), 1133 EndStream: true, 1134 EndHeaders: true, 1135 }) 1136 }) 1137 } 1138 1139 func TestServer_Ping(t *testing.T) { 1140 st := newServerTester(t, nil) 1141 defer st.Close() 1142 st.greet() 1143 1144 // Server should ignore this one, since it has ACK set. 1145 ackPingData := [8]byte{1, 2, 4, 8, 16, 32, 64, 128} 1146 if err := st.fr.WritePing(true, ackPingData); err != nil { 1147 t.Fatal(err) 1148 } 1149 1150 // But the server should reply to this one, since ACK is false. 1151 pingData := [8]byte{1, 2, 3, 4, 5, 6, 7, 8} 1152 if err := st.fr.WritePing(false, pingData); err != nil { 1153 t.Fatal(err) 1154 } 1155 1156 pf := st.wantPing() 1157 if !pf.Flags.Has(FlagPingAck) { 1158 t.Error("response ping doesn't have ACK set") 1159 } 1160 if pf.Data != pingData { 1161 t.Errorf("response ping has data %q; want %q", pf.Data, pingData) 1162 } 1163 } 1164 1165 type filterListener struct { 1166 net.Listener 1167 accept func(conn net.Conn) (net.Conn, error) 1168 } 1169 1170 func (l *filterListener) Accept() (net.Conn, error) { 1171 c, err := l.Listener.Accept() 1172 if err != nil { 1173 return nil, err 1174 } 1175 return l.accept(c) 1176 } 1177 1178 func TestServer_MaxQueuedControlFrames(t *testing.T) { 1179 if testing.Short() { 1180 t.Skip("skipping in short mode") 1181 } 1182 1183 st := newServerTester(t, nil, func(ts *httptest.Server) { 1184 // TCP buffer sizes on test systems aren't under our control and can be large. 1185 // Create a conn that blocks after 10000 bytes written. 1186 ts.Listener = &filterListener{ 1187 Listener: ts.Listener, 1188 accept: func(conn net.Conn) (net.Conn, error) { 1189 return newBlockingWriteConn(conn, 10000), nil 1190 }, 1191 } 1192 }) 1193 defer st.Close() 1194 st.greet() 1195 1196 const extraPings = 500000 // enough to fill the TCP buffers 1197 1198 for i := 0; i < maxQueuedControlFrames+extraPings; i++ { 1199 pingData := [8]byte{1, 2, 3, 4, 5, 6, 7, 8} 1200 if err := st.fr.WritePing(false, pingData); err != nil { 1201 if i == 0 { 1202 t.Fatal(err) 1203 } 1204 // We expect the connection to get closed by the server when the TCP 1205 // buffer fills up and the write queue reaches MaxQueuedControlFrames. 1206 t.Logf("sent %d PING frames", i) 1207 return 1208 } 1209 } 1210 t.Errorf("unexpected success sending all PING frames") 1211 } 1212 1213 func TestServer_RejectsLargeFrames(t *testing.T) { 1214 if runtime.GOOS == "windows" || runtime.GOOS == "plan9" || runtime.GOOS == "zos" { 1215 t.Skip("see golang.org/issue/13434, golang.org/issue/37321") 1216 } 1217 st := newServerTester(t, nil) 1218 defer st.Close() 1219 st.greet() 1220 1221 // Write too large of a frame (too large by one byte) 1222 // We ignore the return value because it's expected that the server 1223 // will only read the first 9 bytes (the headre) and then disconnect. 1224 st.fr.WriteRawFrame(0xff, 0, 0, make([]byte, defaultMaxReadFrameSize+1)) 1225 1226 gf := st.wantGoAway() 1227 if gf.ErrCode != ErrCodeFrameSize { 1228 t.Errorf("GOAWAY err = %v; want %v", gf.ErrCode, ErrCodeFrameSize) 1229 } 1230 if st.serverLogBuf.Len() != 0 { 1231 // Previously we spun here for a bit until the GOAWAY disconnect 1232 // timer fired, logging while we fired. 1233 t.Errorf("unexpected server output: %.500s\n", st.serverLogBuf.Bytes()) 1234 } 1235 } 1236 1237 func TestServer_Handler_Sends_WindowUpdate(t *testing.T) { 1238 puppet := newHandlerPuppet() 1239 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 1240 puppet.act(w, r) 1241 }) 1242 defer st.Close() 1243 defer puppet.done() 1244 1245 st.greet() 1246 1247 st.writeHeaders(HeadersFrameParam{ 1248 StreamID: 1, // clients send odd numbers 1249 BlockFragment: st.encodeHeader(":method", "POST"), 1250 EndStream: false, // data coming 1251 EndHeaders: true, 1252 }) 1253 st.writeData(1, false, []byte("abcdef")) 1254 puppet.do(readBodyHandler(t, "abc")) 1255 st.wantWindowUpdate(0, 3) 1256 st.wantWindowUpdate(1, 3) 1257 1258 puppet.do(readBodyHandler(t, "def")) 1259 st.wantWindowUpdate(0, 3) 1260 st.wantWindowUpdate(1, 3) 1261 1262 st.writeData(1, true, []byte("ghijkl")) // END_STREAM here 1263 puppet.do(readBodyHandler(t, "ghi")) 1264 puppet.do(readBodyHandler(t, "jkl")) 1265 st.wantWindowUpdate(0, 3) 1266 st.wantWindowUpdate(0, 3) // no more stream-level, since END_STREAM 1267 } 1268 1269 // the version of the TestServer_Handler_Sends_WindowUpdate with padding. 1270 // See golang.org/issue/16556 1271 func TestServer_Handler_Sends_WindowUpdate_Padding(t *testing.T) { 1272 puppet := newHandlerPuppet() 1273 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 1274 puppet.act(w, r) 1275 }) 1276 defer st.Close() 1277 defer puppet.done() 1278 1279 st.greet() 1280 1281 st.writeHeaders(HeadersFrameParam{ 1282 StreamID: 1, 1283 BlockFragment: st.encodeHeader(":method", "POST"), 1284 EndStream: false, 1285 EndHeaders: true, 1286 }) 1287 st.writeDataPadded(1, false, []byte("abcdef"), []byte{0, 0, 0, 0}) 1288 1289 // Expect to immediately get our 5 bytes of padding back for 1290 // both the connection and stream (4 bytes of padding + 1 byte of length) 1291 st.wantWindowUpdate(0, 5) 1292 st.wantWindowUpdate(1, 5) 1293 1294 puppet.do(readBodyHandler(t, "abc")) 1295 st.wantWindowUpdate(0, 3) 1296 st.wantWindowUpdate(1, 3) 1297 1298 puppet.do(readBodyHandler(t, "def")) 1299 st.wantWindowUpdate(0, 3) 1300 st.wantWindowUpdate(1, 3) 1301 } 1302 1303 func TestServer_Send_GoAway_After_Bogus_WindowUpdate(t *testing.T) { 1304 st := newServerTester(t, nil) 1305 defer st.Close() 1306 st.greet() 1307 if err := st.fr.WriteWindowUpdate(0, 1<<31-1); err != nil { 1308 t.Fatal(err) 1309 } 1310 gf := st.wantGoAway() 1311 if gf.ErrCode != ErrCodeFlowControl { 1312 t.Errorf("GOAWAY err = %v; want %v", gf.ErrCode, ErrCodeFlowControl) 1313 } 1314 if gf.LastStreamID != 0 { 1315 t.Errorf("GOAWAY last stream ID = %v; want %v", gf.LastStreamID, 0) 1316 } 1317 } 1318 1319 func TestServer_Send_RstStream_After_Bogus_WindowUpdate(t *testing.T) { 1320 inHandler := make(chan bool) 1321 blockHandler := make(chan bool) 1322 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 1323 inHandler <- true 1324 <-blockHandler 1325 }) 1326 defer st.Close() 1327 defer close(blockHandler) 1328 st.greet() 1329 st.writeHeaders(HeadersFrameParam{ 1330 StreamID: 1, 1331 BlockFragment: st.encodeHeader(":method", "POST"), 1332 EndStream: false, // keep it open 1333 EndHeaders: true, 1334 }) 1335 <-inHandler 1336 // Send a bogus window update: 1337 if err := st.fr.WriteWindowUpdate(1, 1<<31-1); err != nil { 1338 t.Fatal(err) 1339 } 1340 st.wantRSTStream(1, ErrCodeFlowControl) 1341 } 1342 1343 // testServerPostUnblock sends a hanging POST with unsent data to handler, 1344 // then runs fn once in the handler, and verifies that the error returned from 1345 // handler is acceptable. It fails if takes over 5 seconds for handler to exit. 1346 func testServerPostUnblock(t *testing.T, 1347 handler func(http.ResponseWriter, *http.Request) error, 1348 fn func(*serverTester), 1349 checkErr func(error), 1350 otherHeaders ...string) { 1351 inHandler := make(chan bool) 1352 errc := make(chan error, 1) 1353 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 1354 inHandler <- true 1355 errc <- handler(w, r) 1356 }) 1357 defer st.Close() 1358 st.greet() 1359 st.writeHeaders(HeadersFrameParam{ 1360 StreamID: 1, 1361 BlockFragment: st.encodeHeader(append([]string{":method", "POST"}, otherHeaders...)...), 1362 EndStream: false, // keep it open 1363 EndHeaders: true, 1364 }) 1365 <-inHandler 1366 fn(st) 1367 select { 1368 case err := <-errc: 1369 if checkErr != nil { 1370 checkErr(err) 1371 } 1372 case <-time.After(5 * time.Second): 1373 t.Fatal("timeout waiting for Handler to return") 1374 } 1375 } 1376 1377 func TestServer_RSTStream_Unblocks_Read(t *testing.T) { 1378 testServerPostUnblock(t, 1379 func(w http.ResponseWriter, r *http.Request) (err error) { 1380 _, err = r.Body.Read(make([]byte, 1)) 1381 return 1382 }, 1383 func(st *serverTester) { 1384 if err := st.fr.WriteRSTStream(1, ErrCodeCancel); err != nil { 1385 t.Fatal(err) 1386 } 1387 }, 1388 func(err error) { 1389 want := StreamError{StreamID: 0x1, Code: 0x8} 1390 if !reflect.DeepEqual(err, want) { 1391 t.Errorf("Read error = %v; want %v", err, want) 1392 } 1393 }, 1394 ) 1395 } 1396 1397 func TestServer_RSTStream_Unblocks_Header_Write(t *testing.T) { 1398 // Run this test a bunch, because it doesn't always 1399 // deadlock. But with a bunch, it did. 1400 n := 50 1401 if testing.Short() { 1402 n = 5 1403 } 1404 for i := 0; i < n; i++ { 1405 testServer_RSTStream_Unblocks_Header_Write(t) 1406 } 1407 } 1408 1409 func testServer_RSTStream_Unblocks_Header_Write(t *testing.T) { 1410 inHandler := make(chan bool, 1) 1411 unblockHandler := make(chan bool, 1) 1412 headerWritten := make(chan bool, 1) 1413 wroteRST := make(chan bool, 1) 1414 1415 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 1416 inHandler <- true 1417 <-wroteRST 1418 w.Header().Set("foo", "bar") 1419 w.WriteHeader(200) 1420 w.(http.Flusher).Flush() 1421 headerWritten <- true 1422 <-unblockHandler 1423 }) 1424 defer st.Close() 1425 1426 st.greet() 1427 st.writeHeaders(HeadersFrameParam{ 1428 StreamID: 1, 1429 BlockFragment: st.encodeHeader(":method", "POST"), 1430 EndStream: false, // keep it open 1431 EndHeaders: true, 1432 }) 1433 <-inHandler 1434 if err := st.fr.WriteRSTStream(1, ErrCodeCancel); err != nil { 1435 t.Fatal(err) 1436 } 1437 wroteRST <- true 1438 st.awaitIdle() 1439 select { 1440 case <-headerWritten: 1441 case <-time.After(2 * time.Second): 1442 t.Error("timeout waiting for header write") 1443 } 1444 unblockHandler <- true 1445 } 1446 1447 func TestServer_DeadConn_Unblocks_Read(t *testing.T) { 1448 testServerPostUnblock(t, 1449 func(w http.ResponseWriter, r *http.Request) (err error) { 1450 _, err = r.Body.Read(make([]byte, 1)) 1451 return 1452 }, 1453 func(st *serverTester) { st.cc.Close() }, 1454 func(err error) { 1455 if err == nil { 1456 t.Error("unexpected nil error from Request.Body.Read") 1457 } 1458 }, 1459 ) 1460 } 1461 1462 var blockUntilClosed = func(w http.ResponseWriter, r *http.Request) error { 1463 <-w.(http.CloseNotifier).CloseNotify() 1464 return nil 1465 } 1466 1467 func TestServer_CloseNotify_After_RSTStream(t *testing.T) { 1468 testServerPostUnblock(t, blockUntilClosed, func(st *serverTester) { 1469 if err := st.fr.WriteRSTStream(1, ErrCodeCancel); err != nil { 1470 t.Fatal(err) 1471 } 1472 }, nil) 1473 } 1474 1475 func TestServer_CloseNotify_After_ConnClose(t *testing.T) { 1476 testServerPostUnblock(t, blockUntilClosed, func(st *serverTester) { st.cc.Close() }, nil) 1477 } 1478 1479 // that CloseNotify unblocks after a stream error due to the client's 1480 // problem that's unrelated to them explicitly canceling it (which is 1481 // TestServer_CloseNotify_After_RSTStream above) 1482 func TestServer_CloseNotify_After_StreamError(t *testing.T) { 1483 testServerPostUnblock(t, blockUntilClosed, func(st *serverTester) { 1484 // data longer than declared Content-Length => stream error 1485 st.writeData(1, true, []byte("1234")) 1486 }, nil, "content-length", "3") 1487 } 1488 1489 func TestServer_StateTransitions(t *testing.T) { 1490 var st *serverTester 1491 inHandler := make(chan bool) 1492 writeData := make(chan bool) 1493 leaveHandler := make(chan bool) 1494 st = newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 1495 inHandler <- true 1496 if st.stream(1) == nil { 1497 t.Errorf("nil stream 1 in handler") 1498 } 1499 if got, want := st.streamState(1), stateOpen; got != want { 1500 t.Errorf("in handler, state is %v; want %v", got, want) 1501 } 1502 writeData <- true 1503 if n, err := r.Body.Read(make([]byte, 1)); n != 0 || err != io.EOF { 1504 t.Errorf("body read = %d, %v; want 0, EOF", n, err) 1505 } 1506 if got, want := st.streamState(1), stateHalfClosedRemote; got != want { 1507 t.Errorf("in handler, state is %v; want %v", got, want) 1508 } 1509 1510 <-leaveHandler 1511 }) 1512 st.greet() 1513 if st.stream(1) != nil { 1514 t.Fatal("stream 1 should be empty") 1515 } 1516 if got := st.streamState(1); got != stateIdle { 1517 t.Fatalf("stream 1 should be idle; got %v", got) 1518 } 1519 1520 st.writeHeaders(HeadersFrameParam{ 1521 StreamID: 1, 1522 BlockFragment: st.encodeHeader(":method", "POST"), 1523 EndStream: false, // keep it open 1524 EndHeaders: true, 1525 }) 1526 <-inHandler 1527 <-writeData 1528 st.writeData(1, true, nil) 1529 1530 leaveHandler <- true 1531 hf := st.wantHeaders() 1532 if !hf.StreamEnded() { 1533 t.Fatal("expected END_STREAM flag") 1534 } 1535 1536 if got, want := st.streamState(1), stateClosed; got != want { 1537 t.Errorf("at end, state is %v; want %v", got, want) 1538 } 1539 if st.stream(1) != nil { 1540 t.Fatal("at end, stream 1 should be gone") 1541 } 1542 } 1543 1544 // test HEADERS w/o EndHeaders + another HEADERS (should get rejected) 1545 func TestServer_Rejects_HeadersNoEnd_Then_Headers(t *testing.T) { 1546 testServerRejectsConn(t, func(st *serverTester) { 1547 st.writeHeaders(HeadersFrameParam{ 1548 StreamID: 1, 1549 BlockFragment: st.encodeHeader(), 1550 EndStream: true, 1551 EndHeaders: false, 1552 }) 1553 st.writeHeaders(HeadersFrameParam{ // Not a continuation. 1554 StreamID: 3, // different stream. 1555 BlockFragment: st.encodeHeader(), 1556 EndStream: true, 1557 EndHeaders: true, 1558 }) 1559 }) 1560 } 1561 1562 // test HEADERS w/o EndHeaders + PING (should get rejected) 1563 func TestServer_Rejects_HeadersNoEnd_Then_Ping(t *testing.T) { 1564 testServerRejectsConn(t, func(st *serverTester) { 1565 st.writeHeaders(HeadersFrameParam{ 1566 StreamID: 1, 1567 BlockFragment: st.encodeHeader(), 1568 EndStream: true, 1569 EndHeaders: false, 1570 }) 1571 if err := st.fr.WritePing(false, [8]byte{}); err != nil { 1572 t.Fatal(err) 1573 } 1574 }) 1575 } 1576 1577 // test HEADERS w/ EndHeaders + a continuation HEADERS (should get rejected) 1578 func TestServer_Rejects_HeadersEnd_Then_Continuation(t *testing.T) { 1579 testServerRejectsConn(t, func(st *serverTester) { 1580 st.writeHeaders(HeadersFrameParam{ 1581 StreamID: 1, 1582 BlockFragment: st.encodeHeader(), 1583 EndStream: true, 1584 EndHeaders: true, 1585 }) 1586 st.wantHeaders() 1587 if err := st.fr.WriteContinuation(1, true, encodeHeaderNoImplicit(t, "foo", "bar")); err != nil { 1588 t.Fatal(err) 1589 } 1590 }) 1591 } 1592 1593 // test HEADERS w/o EndHeaders + a continuation HEADERS on wrong stream ID 1594 func TestServer_Rejects_HeadersNoEnd_Then_ContinuationWrongStream(t *testing.T) { 1595 testServerRejectsConn(t, func(st *serverTester) { 1596 st.writeHeaders(HeadersFrameParam{ 1597 StreamID: 1, 1598 BlockFragment: st.encodeHeader(), 1599 EndStream: true, 1600 EndHeaders: false, 1601 }) 1602 if err := st.fr.WriteContinuation(3, true, encodeHeaderNoImplicit(t, "foo", "bar")); err != nil { 1603 t.Fatal(err) 1604 } 1605 }) 1606 } 1607 1608 // No HEADERS on stream 0. 1609 func TestServer_Rejects_Headers0(t *testing.T) { 1610 testServerRejectsConn(t, func(st *serverTester) { 1611 st.fr.AllowIllegalWrites = true 1612 st.writeHeaders(HeadersFrameParam{ 1613 StreamID: 0, 1614 BlockFragment: st.encodeHeader(), 1615 EndStream: true, 1616 EndHeaders: true, 1617 }) 1618 }) 1619 } 1620 1621 // No CONTINUATION on stream 0. 1622 func TestServer_Rejects_Continuation0(t *testing.T) { 1623 testServerRejectsConn(t, func(st *serverTester) { 1624 st.fr.AllowIllegalWrites = true 1625 if err := st.fr.WriteContinuation(0, true, st.encodeHeader()); err != nil { 1626 t.Fatal(err) 1627 } 1628 }) 1629 } 1630 1631 // No PRIORITY on stream 0. 1632 func TestServer_Rejects_Priority0(t *testing.T) { 1633 testServerRejectsConn(t, func(st *serverTester) { 1634 st.fr.AllowIllegalWrites = true 1635 st.writePriority(0, PriorityParam{StreamDep: 1}) 1636 }) 1637 } 1638 1639 // No HEADERS frame with a self-dependence. 1640 func TestServer_Rejects_HeadersSelfDependence(t *testing.T) { 1641 testServerRejectsStream(t, ErrCodeProtocol, func(st *serverTester) { 1642 st.fr.AllowIllegalWrites = true 1643 st.writeHeaders(HeadersFrameParam{ 1644 StreamID: 1, 1645 BlockFragment: st.encodeHeader(), 1646 EndStream: true, 1647 EndHeaders: true, 1648 Priority: PriorityParam{StreamDep: 1}, 1649 }) 1650 }) 1651 } 1652 1653 // No PRIORTY frame with a self-dependence. 1654 func TestServer_Rejects_PrioritySelfDependence(t *testing.T) { 1655 testServerRejectsStream(t, ErrCodeProtocol, func(st *serverTester) { 1656 st.fr.AllowIllegalWrites = true 1657 st.writePriority(1, PriorityParam{StreamDep: 1}) 1658 }) 1659 } 1660 1661 func TestServer_Rejects_PushPromise(t *testing.T) { 1662 testServerRejectsConn(t, func(st *serverTester) { 1663 pp := PushPromiseParam{ 1664 StreamID: 1, 1665 PromiseID: 3, 1666 } 1667 if err := st.fr.WritePushPromise(pp); err != nil { 1668 t.Fatal(err) 1669 } 1670 }) 1671 } 1672 1673 // testServerRejectsConn tests that the server hangs up with a GOAWAY 1674 // frame and a server close after the client does something 1675 // deserving a CONNECTION_ERROR. 1676 func testServerRejectsConn(t *testing.T, writeReq func(*serverTester)) { 1677 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {}) 1678 st.addLogFilter("connection error: PROTOCOL_ERROR") 1679 defer st.Close() 1680 st.greet() 1681 writeReq(st) 1682 1683 st.wantGoAway() 1684 errc := make(chan error, 1) 1685 go func() { 1686 fr, err := st.fr.ReadFrame() 1687 if err == nil { 1688 err = fmt.Errorf("got frame of type %T", fr) 1689 } 1690 errc <- err 1691 }() 1692 select { 1693 case err := <-errc: 1694 if err != io.EOF { 1695 t.Errorf("ReadFrame = %v; want io.EOF", err) 1696 } 1697 case <-time.After(2 * time.Second): 1698 t.Error("timeout waiting for disconnect") 1699 } 1700 } 1701 1702 // testServerRejectsStream tests that the server sends a RST_STREAM with the provided 1703 // error code after a client sends a bogus request. 1704 func testServerRejectsStream(t *testing.T, code ErrCode, writeReq func(*serverTester)) { 1705 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {}) 1706 defer st.Close() 1707 st.greet() 1708 writeReq(st) 1709 st.wantRSTStream(1, code) 1710 } 1711 1712 // testServerRequest sets up an idle HTTP/2 connection and lets you 1713 // write a single request with writeReq, and then verify that the 1714 // *http.Request is built correctly in checkReq. 1715 func testServerRequest(t *testing.T, writeReq func(*serverTester), checkReq func(*http.Request)) { 1716 gotReq := make(chan bool, 1) 1717 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 1718 if r.Body == nil { 1719 t.Fatal("nil Body") 1720 } 1721 checkReq(r) 1722 gotReq <- true 1723 }) 1724 defer st.Close() 1725 1726 st.greet() 1727 writeReq(st) 1728 1729 select { 1730 case <-gotReq: 1731 case <-time.After(2 * time.Second): 1732 t.Error("timeout waiting for request") 1733 } 1734 } 1735 1736 func getSlash(st *serverTester) { st.bodylessReq1() } 1737 1738 func TestServer_Response_NoData(t *testing.T) { 1739 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 1740 // Nothing. 1741 return nil 1742 }, func(st *serverTester) { 1743 getSlash(st) 1744 hf := st.wantHeaders() 1745 if !hf.StreamEnded() { 1746 t.Fatal("want END_STREAM flag") 1747 } 1748 if !hf.HeadersEnded() { 1749 t.Fatal("want END_HEADERS flag") 1750 } 1751 }) 1752 } 1753 1754 func TestServer_Response_NoData_Header_FooBar(t *testing.T) { 1755 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 1756 w.Header().Set("Foo-Bar", "some-value") 1757 return nil 1758 }, func(st *serverTester) { 1759 getSlash(st) 1760 hf := st.wantHeaders() 1761 if !hf.StreamEnded() { 1762 t.Fatal("want END_STREAM flag") 1763 } 1764 if !hf.HeadersEnded() { 1765 t.Fatal("want END_HEADERS flag") 1766 } 1767 goth := st.decodeHeader(hf.HeaderBlockFragment()) 1768 wanth := [][2]string{ 1769 {":status", "200"}, 1770 {"foo-bar", "some-value"}, 1771 {"content-length", "0"}, 1772 } 1773 if !reflect.DeepEqual(goth, wanth) { 1774 t.Errorf("Got headers %v; want %v", goth, wanth) 1775 } 1776 }) 1777 } 1778 1779 // Reject content-length headers containing a sign. 1780 // See https://golang.org/issue/39017 1781 func TestServerIgnoresContentLengthSignWhenWritingChunks(t *testing.T) { 1782 tests := []struct { 1783 name string 1784 cl string 1785 wantCL string 1786 }{ 1787 { 1788 name: "proper content-length", 1789 cl: "3", 1790 wantCL: "3", 1791 }, 1792 { 1793 name: "ignore cl with plus sign", 1794 cl: "+3", 1795 wantCL: "0", 1796 }, 1797 { 1798 name: "ignore cl with minus sign", 1799 cl: "-3", 1800 wantCL: "0", 1801 }, 1802 { 1803 name: "max int64, for safe uint64->int64 conversion", 1804 cl: "9223372036854775807", 1805 wantCL: "9223372036854775807", 1806 }, 1807 { 1808 name: "overflows int64, so ignored", 1809 cl: "9223372036854775808", 1810 wantCL: "0", 1811 }, 1812 } 1813 1814 for _, tt := range tests { 1815 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 1816 w.Header().Set("content-length", tt.cl) 1817 return nil 1818 }, func(st *serverTester) { 1819 getSlash(st) 1820 hf := st.wantHeaders() 1821 goth := st.decodeHeader(hf.HeaderBlockFragment()) 1822 wanth := [][2]string{ 1823 {":status", "200"}, 1824 {"content-length", tt.wantCL}, 1825 } 1826 if !reflect.DeepEqual(goth, wanth) { 1827 t.Errorf("For case %q, value %q, got = %q; want %q", tt.name, tt.cl, goth, wanth) 1828 } 1829 }) 1830 } 1831 } 1832 1833 // Reject content-length headers containing a sign. 1834 // See https://golang.org/issue/39017 1835 func TestServerRejectsContentLengthWithSignNewRequests(t *testing.T) { 1836 tests := []struct { 1837 name string 1838 cl string 1839 wantCL int64 1840 }{ 1841 { 1842 name: "proper content-length", 1843 cl: "3", 1844 wantCL: 3, 1845 }, 1846 { 1847 name: "ignore cl with plus sign", 1848 cl: "+3", 1849 wantCL: 0, 1850 }, 1851 { 1852 name: "ignore cl with minus sign", 1853 cl: "-3", 1854 wantCL: 0, 1855 }, 1856 { 1857 name: "max int64, for safe uint64->int64 conversion", 1858 cl: "9223372036854775807", 1859 wantCL: 9223372036854775807, 1860 }, 1861 { 1862 name: "overflows int64, so ignored", 1863 cl: "9223372036854775808", 1864 wantCL: 0, 1865 }, 1866 } 1867 1868 for _, tt := range tests { 1869 tt := tt 1870 t.Run(tt.name, func(t *testing.T) { 1871 writeReq := func(st *serverTester) { 1872 st.writeHeaders(HeadersFrameParam{ 1873 StreamID: 1, // clients send odd numbers 1874 BlockFragment: st.encodeHeader("content-length", tt.cl), 1875 EndStream: false, 1876 EndHeaders: true, 1877 }) 1878 st.writeData(1, false, []byte("")) 1879 } 1880 checkReq := func(r *http.Request) { 1881 if r.ContentLength != tt.wantCL { 1882 t.Fatalf("Got: %q\nWant: %q", r.ContentLength, tt.wantCL) 1883 } 1884 } 1885 testServerRequest(t, writeReq, checkReq) 1886 }) 1887 } 1888 } 1889 1890 func TestServer_Response_Data_Sniff_DoesntOverride(t *testing.T) { 1891 const msg = "<html>this is HTML." 1892 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 1893 w.Header().Set("Content-Type", "foo/bar") 1894 io.WriteString(w, msg) 1895 return nil 1896 }, func(st *serverTester) { 1897 getSlash(st) 1898 hf := st.wantHeaders() 1899 if hf.StreamEnded() { 1900 t.Fatal("don't want END_STREAM, expecting data") 1901 } 1902 if !hf.HeadersEnded() { 1903 t.Fatal("want END_HEADERS flag") 1904 } 1905 goth := st.decodeHeader(hf.HeaderBlockFragment()) 1906 wanth := [][2]string{ 1907 {":status", "200"}, 1908 {"content-type", "foo/bar"}, 1909 {"content-length", strconv.Itoa(len(msg))}, 1910 } 1911 if !reflect.DeepEqual(goth, wanth) { 1912 t.Errorf("Got headers %v; want %v", goth, wanth) 1913 } 1914 df := st.wantData() 1915 if !df.StreamEnded() { 1916 t.Error("expected DATA to have END_STREAM flag") 1917 } 1918 if got := string(df.Data()); got != msg { 1919 t.Errorf("got DATA %q; want %q", got, msg) 1920 } 1921 }) 1922 } 1923 1924 func TestServer_Response_TransferEncoding_chunked(t *testing.T) { 1925 const msg = "hi" 1926 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 1927 w.Header().Set("Transfer-Encoding", "chunked") // should be stripped 1928 io.WriteString(w, msg) 1929 return nil 1930 }, func(st *serverTester) { 1931 getSlash(st) 1932 hf := st.wantHeaders() 1933 goth := st.decodeHeader(hf.HeaderBlockFragment()) 1934 wanth := [][2]string{ 1935 {":status", "200"}, 1936 {"content-type", "text/plain; charset=utf-8"}, 1937 {"content-length", strconv.Itoa(len(msg))}, 1938 } 1939 if !reflect.DeepEqual(goth, wanth) { 1940 t.Errorf("Got headers %v; want %v", goth, wanth) 1941 } 1942 }) 1943 } 1944 1945 // Header accessed only after the initial write. 1946 func TestServer_Response_Data_IgnoreHeaderAfterWrite_After(t *testing.T) { 1947 const msg = "<html>this is HTML." 1948 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 1949 io.WriteString(w, msg) 1950 w.Header().Set("foo", "should be ignored") 1951 return nil 1952 }, func(st *serverTester) { 1953 getSlash(st) 1954 hf := st.wantHeaders() 1955 if hf.StreamEnded() { 1956 t.Fatal("unexpected END_STREAM") 1957 } 1958 if !hf.HeadersEnded() { 1959 t.Fatal("want END_HEADERS flag") 1960 } 1961 goth := st.decodeHeader(hf.HeaderBlockFragment()) 1962 wanth := [][2]string{ 1963 {":status", "200"}, 1964 {"content-type", "text/html; charset=utf-8"}, 1965 {"content-length", strconv.Itoa(len(msg))}, 1966 } 1967 if !reflect.DeepEqual(goth, wanth) { 1968 t.Errorf("Got headers %v; want %v", goth, wanth) 1969 } 1970 }) 1971 } 1972 1973 // Header accessed before the initial write and later mutated. 1974 func TestServer_Response_Data_IgnoreHeaderAfterWrite_Overwrite(t *testing.T) { 1975 const msg = "<html>this is HTML." 1976 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 1977 w.Header().Set("foo", "proper value") 1978 io.WriteString(w, msg) 1979 w.Header().Set("foo", "should be ignored") 1980 return nil 1981 }, func(st *serverTester) { 1982 getSlash(st) 1983 hf := st.wantHeaders() 1984 if hf.StreamEnded() { 1985 t.Fatal("unexpected END_STREAM") 1986 } 1987 if !hf.HeadersEnded() { 1988 t.Fatal("want END_HEADERS flag") 1989 } 1990 goth := st.decodeHeader(hf.HeaderBlockFragment()) 1991 wanth := [][2]string{ 1992 {":status", "200"}, 1993 {"foo", "proper value"}, 1994 {"content-type", "text/html; charset=utf-8"}, 1995 {"content-length", strconv.Itoa(len(msg))}, 1996 } 1997 if !reflect.DeepEqual(goth, wanth) { 1998 t.Errorf("Got headers %v; want %v", goth, wanth) 1999 } 2000 }) 2001 } 2002 2003 func TestServer_Response_Data_SniffLenType(t *testing.T) { 2004 const msg = "<html>this is HTML." 2005 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 2006 io.WriteString(w, msg) 2007 return nil 2008 }, func(st *serverTester) { 2009 getSlash(st) 2010 hf := st.wantHeaders() 2011 if hf.StreamEnded() { 2012 t.Fatal("don't want END_STREAM, expecting data") 2013 } 2014 if !hf.HeadersEnded() { 2015 t.Fatal("want END_HEADERS flag") 2016 } 2017 goth := st.decodeHeader(hf.HeaderBlockFragment()) 2018 wanth := [][2]string{ 2019 {":status", "200"}, 2020 {"content-type", "text/html; charset=utf-8"}, 2021 {"content-length", strconv.Itoa(len(msg))}, 2022 } 2023 if !reflect.DeepEqual(goth, wanth) { 2024 t.Errorf("Got headers %v; want %v", goth, wanth) 2025 } 2026 df := st.wantData() 2027 if !df.StreamEnded() { 2028 t.Error("expected DATA to have END_STREAM flag") 2029 } 2030 if got := string(df.Data()); got != msg { 2031 t.Errorf("got DATA %q; want %q", got, msg) 2032 } 2033 }) 2034 } 2035 2036 func TestServer_Response_Header_Flush_MidWrite(t *testing.T) { 2037 const msg = "<html>this is HTML" 2038 const msg2 = ", and this is the next chunk" 2039 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 2040 io.WriteString(w, msg) 2041 w.(http.Flusher).Flush() 2042 io.WriteString(w, msg2) 2043 return nil 2044 }, func(st *serverTester) { 2045 getSlash(st) 2046 hf := st.wantHeaders() 2047 if hf.StreamEnded() { 2048 t.Fatal("unexpected END_STREAM flag") 2049 } 2050 if !hf.HeadersEnded() { 2051 t.Fatal("want END_HEADERS flag") 2052 } 2053 goth := st.decodeHeader(hf.HeaderBlockFragment()) 2054 wanth := [][2]string{ 2055 {":status", "200"}, 2056 {"content-type", "text/html; charset=utf-8"}, // sniffed 2057 // and no content-length 2058 } 2059 if !reflect.DeepEqual(goth, wanth) { 2060 t.Errorf("Got headers %v; want %v", goth, wanth) 2061 } 2062 { 2063 df := st.wantData() 2064 if df.StreamEnded() { 2065 t.Error("unexpected END_STREAM flag") 2066 } 2067 if got := string(df.Data()); got != msg { 2068 t.Errorf("got DATA %q; want %q", got, msg) 2069 } 2070 } 2071 { 2072 df := st.wantData() 2073 if !df.StreamEnded() { 2074 t.Error("wanted END_STREAM flag on last data chunk") 2075 } 2076 if got := string(df.Data()); got != msg2 { 2077 t.Errorf("got DATA %q; want %q", got, msg2) 2078 } 2079 } 2080 }) 2081 } 2082 2083 func TestServer_Response_LargeWrite(t *testing.T) { 2084 const size = 1 << 20 2085 const maxFrameSize = 16 << 10 2086 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 2087 n, err := w.Write(bytes.Repeat([]byte("a"), size)) 2088 if err != nil { 2089 return fmt.Errorf("Write error: %v", err) 2090 } 2091 if n != size { 2092 return fmt.Errorf("wrong size %d from Write", n) 2093 } 2094 return nil 2095 }, func(st *serverTester) { 2096 if err := st.fr.WriteSettings( 2097 Setting{SettingInitialWindowSize, 0}, 2098 Setting{SettingMaxFrameSize, maxFrameSize}, 2099 ); err != nil { 2100 t.Fatal(err) 2101 } 2102 st.wantSettingsAck() 2103 2104 getSlash(st) // make the single request 2105 2106 // Give the handler quota to write: 2107 if err := st.fr.WriteWindowUpdate(1, size); err != nil { 2108 t.Fatal(err) 2109 } 2110 // Give the handler quota to write to connection-level 2111 // window as well 2112 if err := st.fr.WriteWindowUpdate(0, size); err != nil { 2113 t.Fatal(err) 2114 } 2115 hf := st.wantHeaders() 2116 if hf.StreamEnded() { 2117 t.Fatal("unexpected END_STREAM flag") 2118 } 2119 if !hf.HeadersEnded() { 2120 t.Fatal("want END_HEADERS flag") 2121 } 2122 goth := st.decodeHeader(hf.HeaderBlockFragment()) 2123 wanth := [][2]string{ 2124 {":status", "200"}, 2125 {"content-type", "text/plain; charset=utf-8"}, // sniffed 2126 // and no content-length 2127 } 2128 if !reflect.DeepEqual(goth, wanth) { 2129 t.Errorf("Got headers %v; want %v", goth, wanth) 2130 } 2131 var bytes, frames int 2132 for { 2133 df := st.wantData() 2134 bytes += len(df.Data()) 2135 frames++ 2136 for _, b := range df.Data() { 2137 if b != 'a' { 2138 t.Fatal("non-'a' byte seen in DATA") 2139 } 2140 } 2141 if df.StreamEnded() { 2142 break 2143 } 2144 } 2145 if bytes != size { 2146 t.Errorf("Got %d bytes; want %d", bytes, size) 2147 } 2148 if want := int(size / maxFrameSize); frames < want || frames > want*2 { 2149 t.Errorf("Got %d frames; want %d", frames, size) 2150 } 2151 }) 2152 } 2153 2154 // Test that the handler can't write more than the client allows 2155 func TestServer_Response_LargeWrite_FlowControlled(t *testing.T) { 2156 // Make these reads. Before each read, the client adds exactly enough 2157 // flow-control to satisfy the read. Numbers chosen arbitrarily. 2158 reads := []int{123, 1, 13, 127} 2159 size := 0 2160 for _, n := range reads { 2161 size += n 2162 } 2163 2164 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 2165 w.(http.Flusher).Flush() 2166 n, err := w.Write(bytes.Repeat([]byte("a"), size)) 2167 if err != nil { 2168 return fmt.Errorf("Write error: %v", err) 2169 } 2170 if n != size { 2171 return fmt.Errorf("wrong size %d from Write", n) 2172 } 2173 return nil 2174 }, func(st *serverTester) { 2175 // Set the window size to something explicit for this test. 2176 // It's also how much initial data we expect. 2177 if err := st.fr.WriteSettings(Setting{SettingInitialWindowSize, uint32(reads[0])}); err != nil { 2178 t.Fatal(err) 2179 } 2180 st.wantSettingsAck() 2181 2182 getSlash(st) // make the single request 2183 2184 hf := st.wantHeaders() 2185 if hf.StreamEnded() { 2186 t.Fatal("unexpected END_STREAM flag") 2187 } 2188 if !hf.HeadersEnded() { 2189 t.Fatal("want END_HEADERS flag") 2190 } 2191 2192 df := st.wantData() 2193 if got := len(df.Data()); got != reads[0] { 2194 t.Fatalf("Initial window size = %d but got DATA with %d bytes", reads[0], got) 2195 } 2196 2197 for _, quota := range reads[1:] { 2198 if err := st.fr.WriteWindowUpdate(1, uint32(quota)); err != nil { 2199 t.Fatal(err) 2200 } 2201 df := st.wantData() 2202 if int(quota) != len(df.Data()) { 2203 t.Fatalf("read %d bytes after giving %d quota", len(df.Data()), quota) 2204 } 2205 } 2206 }) 2207 } 2208 2209 // Test that the handler blocked in a Write is unblocked if the server sends a RST_STREAM. 2210 func TestServer_Response_RST_Unblocks_LargeWrite(t *testing.T) { 2211 const size = 1 << 20 2212 const maxFrameSize = 16 << 10 2213 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 2214 w.(http.Flusher).Flush() 2215 errc := make(chan error, 1) 2216 go func() { 2217 _, err := w.Write(bytes.Repeat([]byte("a"), size)) 2218 errc <- err 2219 }() 2220 select { 2221 case err := <-errc: 2222 if err == nil { 2223 return errors.New("unexpected nil error from Write in handler") 2224 } 2225 return nil 2226 case <-time.After(2 * time.Second): 2227 return errors.New("timeout waiting for Write in handler") 2228 } 2229 }, func(st *serverTester) { 2230 if err := st.fr.WriteSettings( 2231 Setting{SettingInitialWindowSize, 0}, 2232 Setting{SettingMaxFrameSize, maxFrameSize}, 2233 ); err != nil { 2234 t.Fatal(err) 2235 } 2236 st.wantSettingsAck() 2237 2238 getSlash(st) // make the single request 2239 2240 hf := st.wantHeaders() 2241 if hf.StreamEnded() { 2242 t.Fatal("unexpected END_STREAM flag") 2243 } 2244 if !hf.HeadersEnded() { 2245 t.Fatal("want END_HEADERS flag") 2246 } 2247 2248 if err := st.fr.WriteRSTStream(1, ErrCodeCancel); err != nil { 2249 t.Fatal(err) 2250 } 2251 }) 2252 } 2253 2254 func TestServer_Response_Empty_Data_Not_FlowControlled(t *testing.T) { 2255 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 2256 w.(http.Flusher).Flush() 2257 // Nothing; send empty DATA 2258 return nil 2259 }, func(st *serverTester) { 2260 // Handler gets no data quota: 2261 if err := st.fr.WriteSettings(Setting{SettingInitialWindowSize, 0}); err != nil { 2262 t.Fatal(err) 2263 } 2264 st.wantSettingsAck() 2265 2266 getSlash(st) // make the single request 2267 2268 hf := st.wantHeaders() 2269 if hf.StreamEnded() { 2270 t.Fatal("unexpected END_STREAM flag") 2271 } 2272 if !hf.HeadersEnded() { 2273 t.Fatal("want END_HEADERS flag") 2274 } 2275 2276 df := st.wantData() 2277 if got := len(df.Data()); got != 0 { 2278 t.Fatalf("unexpected %d DATA bytes; want 0", got) 2279 } 2280 if !df.StreamEnded() { 2281 t.Fatal("DATA didn't have END_STREAM") 2282 } 2283 }) 2284 } 2285 2286 func TestServer_Response_Automatic100Continue(t *testing.T) { 2287 const msg = "foo" 2288 const reply = "bar" 2289 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 2290 if v := r.Header.Get("Expect"); v != "" { 2291 t.Errorf("Expect header = %q; want empty", v) 2292 } 2293 buf := make([]byte, len(msg)) 2294 // This read should trigger the 100-continue being sent. 2295 if n, err := io.ReadFull(r.Body, buf); err != nil || n != len(msg) || string(buf) != msg { 2296 return fmt.Errorf("ReadFull = %q, %v; want %q, nil", buf[:n], err, msg) 2297 } 2298 _, err := io.WriteString(w, reply) 2299 return err 2300 }, func(st *serverTester) { 2301 st.writeHeaders(HeadersFrameParam{ 2302 StreamID: 1, // clients send odd numbers 2303 BlockFragment: st.encodeHeader(":method", "POST", "expect", "100-continue"), 2304 EndStream: false, 2305 EndHeaders: true, 2306 }) 2307 hf := st.wantHeaders() 2308 if hf.StreamEnded() { 2309 t.Fatal("unexpected END_STREAM flag") 2310 } 2311 if !hf.HeadersEnded() { 2312 t.Fatal("want END_HEADERS flag") 2313 } 2314 goth := st.decodeHeader(hf.HeaderBlockFragment()) 2315 wanth := [][2]string{ 2316 {":status", "100"}, 2317 } 2318 if !reflect.DeepEqual(goth, wanth) { 2319 t.Fatalf("Got headers %v; want %v", goth, wanth) 2320 } 2321 2322 // Okay, they sent status 100, so we can send our 2323 // gigantic and/or sensitive "foo" payload now. 2324 st.writeData(1, true, []byte(msg)) 2325 2326 st.wantWindowUpdate(0, uint32(len(msg))) 2327 2328 hf = st.wantHeaders() 2329 if hf.StreamEnded() { 2330 t.Fatal("expected data to follow") 2331 } 2332 if !hf.HeadersEnded() { 2333 t.Fatal("want END_HEADERS flag") 2334 } 2335 goth = st.decodeHeader(hf.HeaderBlockFragment()) 2336 wanth = [][2]string{ 2337 {":status", "200"}, 2338 {"content-type", "text/plain; charset=utf-8"}, 2339 {"content-length", strconv.Itoa(len(reply))}, 2340 } 2341 if !reflect.DeepEqual(goth, wanth) { 2342 t.Errorf("Got headers %v; want %v", goth, wanth) 2343 } 2344 2345 df := st.wantData() 2346 if string(df.Data()) != reply { 2347 t.Errorf("Client read %q; want %q", df.Data(), reply) 2348 } 2349 if !df.StreamEnded() { 2350 t.Errorf("expect data stream end") 2351 } 2352 }) 2353 } 2354 2355 func TestServer_HandlerWriteErrorOnDisconnect(t *testing.T) { 2356 errc := make(chan error, 1) 2357 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 2358 p := []byte("some data.\n") 2359 for { 2360 _, err := w.Write(p) 2361 if err != nil { 2362 errc <- err 2363 return nil 2364 } 2365 } 2366 }, func(st *serverTester) { 2367 st.writeHeaders(HeadersFrameParam{ 2368 StreamID: 1, 2369 BlockFragment: st.encodeHeader(), 2370 EndStream: false, 2371 EndHeaders: true, 2372 }) 2373 hf := st.wantHeaders() 2374 if hf.StreamEnded() { 2375 t.Fatal("unexpected END_STREAM flag") 2376 } 2377 if !hf.HeadersEnded() { 2378 t.Fatal("want END_HEADERS flag") 2379 } 2380 // Close the connection and wait for the handler to (hopefully) notice. 2381 st.cc.Close() 2382 select { 2383 case <-errc: 2384 case <-time.After(5 * time.Second): 2385 t.Error("timeout") 2386 } 2387 }) 2388 } 2389 2390 func TestServer_Rejects_Too_Many_Streams(t *testing.T) { 2391 const testPath = "/some/path" 2392 2393 inHandler := make(chan uint32) 2394 leaveHandler := make(chan bool) 2395 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 2396 id := w.(*responseWriter).rws.stream.id 2397 inHandler <- id 2398 if id == 1+(defaultMaxStreams+1)*2 && r.URL.Path != testPath { 2399 t.Errorf("decoded final path as %q; want %q", r.URL.Path, testPath) 2400 } 2401 <-leaveHandler 2402 }) 2403 defer st.Close() 2404 st.greet() 2405 nextStreamID := uint32(1) 2406 streamID := func() uint32 { 2407 defer func() { nextStreamID += 2 }() 2408 return nextStreamID 2409 } 2410 sendReq := func(id uint32, headers ...string) { 2411 st.writeHeaders(HeadersFrameParam{ 2412 StreamID: id, 2413 BlockFragment: st.encodeHeader(headers...), 2414 EndStream: true, 2415 EndHeaders: true, 2416 }) 2417 } 2418 for i := 0; i < defaultMaxStreams; i++ { 2419 sendReq(streamID()) 2420 <-inHandler 2421 } 2422 defer func() { 2423 for i := 0; i < defaultMaxStreams; i++ { 2424 leaveHandler <- true 2425 } 2426 }() 2427 2428 // And this one should cross the limit: 2429 // (It's also sent as a CONTINUATION, to verify we still track the decoder context, 2430 // even if we're rejecting it) 2431 rejectID := streamID() 2432 headerBlock := st.encodeHeader(":path", testPath) 2433 frag1, frag2 := headerBlock[:3], headerBlock[3:] 2434 st.writeHeaders(HeadersFrameParam{ 2435 StreamID: rejectID, 2436 BlockFragment: frag1, 2437 EndStream: true, 2438 EndHeaders: false, // CONTINUATION coming 2439 }) 2440 if err := st.fr.WriteContinuation(rejectID, true, frag2); err != nil { 2441 t.Fatal(err) 2442 } 2443 st.wantRSTStream(rejectID, ErrCodeProtocol) 2444 2445 // But let a handler finish: 2446 leaveHandler <- true 2447 st.wantHeaders() 2448 2449 // And now another stream should be able to start: 2450 goodID := streamID() 2451 sendReq(goodID, ":path", testPath) 2452 select { 2453 case got := <-inHandler: 2454 if got != goodID { 2455 t.Errorf("Got stream %d; want %d", got, goodID) 2456 } 2457 case <-time.After(3 * time.Second): 2458 t.Error("timeout waiting for handler") 2459 } 2460 } 2461 2462 // So many response headers that the server needs to use CONTINUATION frames: 2463 func TestServer_Response_ManyHeaders_With_Continuation(t *testing.T) { 2464 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 2465 h := w.Header() 2466 for i := 0; i < 5000; i++ { 2467 h.Set(fmt.Sprintf("x-header-%d", i), fmt.Sprintf("x-value-%d", i)) 2468 } 2469 return nil 2470 }, func(st *serverTester) { 2471 getSlash(st) 2472 hf := st.wantHeaders() 2473 if hf.HeadersEnded() { 2474 t.Fatal("got unwanted END_HEADERS flag") 2475 } 2476 n := 0 2477 for { 2478 n++ 2479 cf := st.wantContinuation() 2480 if cf.HeadersEnded() { 2481 break 2482 } 2483 } 2484 if n < 5 { 2485 t.Errorf("Only got %d CONTINUATION frames; expected 5+ (currently 6)", n) 2486 } 2487 }) 2488 } 2489 2490 // This previously crashed (reported by Mathieu Lonjaret as observed 2491 // while using Camlistore) because we got a DATA frame from the client 2492 // after the handler exited and our logic at the time was wrong, 2493 // keeping a stream in the map in stateClosed, which tickled an 2494 // invariant check later when we tried to remove that stream (via 2495 // defer sc.closeAllStreamsOnConnClose) when the serverConn serve loop 2496 // ended. 2497 func TestServer_NoCrash_HandlerClose_Then_ClientClose(t *testing.T) { 2498 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 2499 // nothing 2500 return nil 2501 }, func(st *serverTester) { 2502 st.writeHeaders(HeadersFrameParam{ 2503 StreamID: 1, 2504 BlockFragment: st.encodeHeader(), 2505 EndStream: false, // DATA is coming 2506 EndHeaders: true, 2507 }) 2508 hf := st.wantHeaders() 2509 if !hf.HeadersEnded() || !hf.StreamEnded() { 2510 t.Fatalf("want END_HEADERS+END_STREAM, got %v", hf) 2511 } 2512 2513 // Sent when the a Handler closes while a client has 2514 // indicated it's still sending DATA: 2515 st.wantRSTStream(1, ErrCodeNo) 2516 2517 // Now the handler has ended, so it's ended its 2518 // stream, but the client hasn't closed its side 2519 // (stateClosedLocal). So send more data and verify 2520 // it doesn't crash with an internal invariant panic, like 2521 // it did before. 2522 st.writeData(1, true, []byte("foo")) 2523 2524 // Get our flow control bytes back, since the handler didn't get them. 2525 st.wantWindowUpdate(0, uint32(len("foo"))) 2526 2527 // Sent after a peer sends data anyway (admittedly the 2528 // previous RST_STREAM might've still been in-flight), 2529 // but they'll get the more friendly 'cancel' code 2530 // first. 2531 st.wantRSTStream(1, ErrCodeStreamClosed) 2532 2533 // Set up a bunch of machinery to record the panic we saw 2534 // previously. 2535 var ( 2536 panMu sync.Mutex 2537 panicVal interface{} 2538 ) 2539 2540 testHookOnPanicMu.Lock() 2541 testHookOnPanic = func(sc *serverConn, pv interface{}) bool { 2542 panMu.Lock() 2543 panicVal = pv 2544 panMu.Unlock() 2545 return true 2546 } 2547 testHookOnPanicMu.Unlock() 2548 2549 // Now force the serve loop to end, via closing the connection. 2550 st.cc.Close() 2551 select { 2552 case <-st.sc.doneServing: 2553 // Loop has exited. 2554 panMu.Lock() 2555 got := panicVal 2556 panMu.Unlock() 2557 if got != nil { 2558 t.Errorf("Got panic: %v", got) 2559 } 2560 case <-time.After(5 * time.Second): 2561 t.Error("timeout") 2562 } 2563 }) 2564 } 2565 2566 func TestServer_Rejects_TLS10(t *testing.T) { testRejectTLS(t, tls.VersionTLS10) } 2567 func TestServer_Rejects_TLS11(t *testing.T) { testRejectTLS(t, tls.VersionTLS11) } 2568 2569 func testRejectTLS(t *testing.T, max uint16) { 2570 st := newServerTester(t, nil, func(c *tls.Config) { 2571 // As of 1.18 the default minimum Go TLS version is 2572 // 1.2. In order to test rejection of lower versions, 2573 // manually set the minimum version to 1.0 2574 c.MinVersion = tls.VersionTLS10 2575 c.MaxVersion = max 2576 }) 2577 defer st.Close() 2578 gf := st.wantGoAway() 2579 if got, want := gf.ErrCode, ErrCodeInadequateSecurity; got != want { 2580 t.Errorf("Got error code %v; want %v", got, want) 2581 } 2582 } 2583 2584 func TestServer_Rejects_TLSBadCipher(t *testing.T) { 2585 st := newServerTester(t, nil, func(c *tls.Config) { 2586 // All TLS 1.3 ciphers are good. Test with TLS 1.2. 2587 c.MaxVersion = tls.VersionTLS12 2588 // Only list bad ones: 2589 c.CipherSuites = []uint16{ 2590 tls.TLS_RSA_WITH_RC4_128_SHA, 2591 tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA, 2592 tls.TLS_RSA_WITH_AES_128_CBC_SHA, 2593 tls.TLS_RSA_WITH_AES_256_CBC_SHA, 2594 tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 2595 tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 2596 tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 2597 tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA, 2598 tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 2599 tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 2600 tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 2601 cipher_TLS_RSA_WITH_AES_128_CBC_SHA256, 2602 } 2603 }) 2604 defer st.Close() 2605 gf := st.wantGoAway() 2606 if got, want := gf.ErrCode, ErrCodeInadequateSecurity; got != want { 2607 t.Errorf("Got error code %v; want %v", got, want) 2608 } 2609 } 2610 2611 func TestServer_Advertises_Common_Cipher(t *testing.T) { 2612 const requiredSuite = tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 2613 st := newServerTester(t, nil, func(c *tls.Config) { 2614 // Have the client only support the one required by the spec. 2615 c.CipherSuites = []uint16{requiredSuite} 2616 }, func(ts *httptest.Server) { 2617 var srv *http.Server = ts.Config 2618 // Have the server configured with no specific cipher suites. 2619 // This tests that Go's defaults include the required one. 2620 srv.TLSConfig = nil 2621 }) 2622 defer st.Close() 2623 st.greet() 2624 } 2625 2626 func (st *serverTester) onHeaderField(f hpack.HeaderField) { 2627 if f.Name == "date" { 2628 return 2629 } 2630 st.decodedHeaders = append(st.decodedHeaders, [2]string{f.Name, f.Value}) 2631 } 2632 2633 func (st *serverTester) decodeHeader(headerBlock []byte) (pairs [][2]string) { 2634 st.decodedHeaders = nil 2635 if _, err := st.hpackDec.Write(headerBlock); err != nil { 2636 st.t.Fatalf("hpack decoding error: %v", err) 2637 } 2638 if err := st.hpackDec.Close(); err != nil { 2639 st.t.Fatalf("hpack decoding error: %v", err) 2640 } 2641 return st.decodedHeaders 2642 } 2643 2644 // testServerResponse sets up an idle HTTP/2 connection. The client function should 2645 // write a single request that must be handled by the handler. This waits up to 5s 2646 // for client to return, then up to an additional 2s for the handler to return. 2647 func testServerResponse(t testing.TB, 2648 handler func(http.ResponseWriter, *http.Request) error, 2649 client func(*serverTester), 2650 ) { 2651 errc := make(chan error, 1) 2652 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 2653 if r.Body == nil { 2654 t.Fatal("nil Body") 2655 } 2656 errc <- handler(w, r) 2657 }) 2658 defer st.Close() 2659 2660 donec := make(chan bool) 2661 go func() { 2662 defer close(donec) 2663 st.greet() 2664 client(st) 2665 }() 2666 2667 select { 2668 case <-donec: 2669 case <-time.After(5 * time.Second): 2670 t.Fatal("timeout in client") 2671 } 2672 2673 select { 2674 case err := <-errc: 2675 if err != nil { 2676 t.Fatalf("Error in handler: %v", err) 2677 } 2678 case <-time.After(2 * time.Second): 2679 t.Fatal("timeout in handler") 2680 } 2681 } 2682 2683 // readBodyHandler returns an http Handler func that reads len(want) 2684 // bytes from r.Body and fails t if the contents read were not 2685 // the value of want. 2686 func readBodyHandler(t *testing.T, want string) func(w http.ResponseWriter, r *http.Request) { 2687 return func(w http.ResponseWriter, r *http.Request) { 2688 buf := make([]byte, len(want)) 2689 _, err := io.ReadFull(r.Body, buf) 2690 if err != nil { 2691 t.Error(err) 2692 return 2693 } 2694 if string(buf) != want { 2695 t.Errorf("read %q; want %q", buf, want) 2696 } 2697 } 2698 } 2699 2700 // TestServerWithCurl currently fails, hence the LenientCipherSuites test. See: 2701 // https://github.com/tatsuhiro-t/nghttp2/issues/140 & 2702 // http://sourceforge.net/p/curl/bugs/1472/ 2703 func TestServerWithCurl(t *testing.T) { testServerWithCurl(t, false) } 2704 func TestServerWithCurl_LenientCipherSuites(t *testing.T) { testServerWithCurl(t, true) } 2705 2706 func testServerWithCurl(t *testing.T, permitProhibitedCipherSuites bool) { 2707 if runtime.GOOS != "linux" { 2708 t.Skip("skipping Docker test when not on Linux; requires --net which won't work with boot2docker anyway") 2709 } 2710 if testing.Short() { 2711 t.Skip("skipping curl test in short mode") 2712 } 2713 requireCurl(t) 2714 var gotConn int32 2715 testHookOnConn = func() { atomic.StoreInt32(&gotConn, 1) } 2716 2717 const msg = "Hello from curl!\n" 2718 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 2719 w.Header().Set("Foo", "Bar") 2720 w.Header().Set("Client-Proto", r.Proto) 2721 io.WriteString(w, msg) 2722 })) 2723 ConfigureServer(ts.Config, &Server{ 2724 PermitProhibitedCipherSuites: permitProhibitedCipherSuites, 2725 }) 2726 ts.TLS = ts.Config.TLSConfig // the httptest.Server has its own copy of this TLS config 2727 ts.StartTLS() 2728 defer ts.Close() 2729 2730 t.Logf("Running test server for curl to hit at: %s", ts.URL) 2731 container := curl(t, "--silent", "--http2", "--insecure", "-v", ts.URL) 2732 defer kill(container) 2733 resc := make(chan interface{}, 1) 2734 go func() { 2735 res, err := dockerLogs(container) 2736 if err != nil { 2737 resc <- err 2738 } else { 2739 resc <- res 2740 } 2741 }() 2742 select { 2743 case res := <-resc: 2744 if err, ok := res.(error); ok { 2745 t.Fatal(err) 2746 } 2747 body := string(res.([]byte)) 2748 // Search for both "key: value" and "key:value", since curl changed their format 2749 // Our Dockerfile contains the latest version (no space), but just in case people 2750 // didn't rebuild, check both. 2751 if !strings.Contains(body, "foo: Bar") && !strings.Contains(body, "foo:Bar") { 2752 t.Errorf("didn't see foo: Bar header") 2753 t.Logf("Got: %s", body) 2754 } 2755 if !strings.Contains(body, "client-proto: HTTP/2") && !strings.Contains(body, "client-proto:HTTP/2") { 2756 t.Errorf("didn't see client-proto: HTTP/2 header") 2757 t.Logf("Got: %s", res) 2758 } 2759 if !strings.Contains(string(res.([]byte)), msg) { 2760 t.Errorf("didn't see %q content", msg) 2761 t.Logf("Got: %s", res) 2762 } 2763 case <-time.After(3 * time.Second): 2764 t.Errorf("timeout waiting for curl") 2765 } 2766 2767 if atomic.LoadInt32(&gotConn) == 0 { 2768 t.Error("never saw an http2 connection") 2769 } 2770 } 2771 2772 var doh2load = flag.Bool("h2load", false, "Run h2load test") 2773 2774 func TestServerWithH2Load(t *testing.T) { 2775 if !*doh2load { 2776 t.Skip("Skipping without --h2load flag.") 2777 } 2778 if runtime.GOOS != "linux" { 2779 t.Skip("skipping Docker test when not on Linux; requires --net which won't work with boot2docker anyway") 2780 } 2781 requireH2load(t) 2782 2783 msg := strings.Repeat("Hello, h2load!\n", 5000) 2784 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 2785 io.WriteString(w, msg) 2786 w.(http.Flusher).Flush() 2787 io.WriteString(w, msg) 2788 })) 2789 ts.StartTLS() 2790 defer ts.Close() 2791 2792 cmd := exec.Command("docker", "run", "--net=host", "--entrypoint=/usr/local/bin/h2load", "gohttp2/curl", 2793 "-n100000", "-c100", "-m100", ts.URL) 2794 cmd.Stdout = os.Stdout 2795 cmd.Stderr = os.Stderr 2796 if err := cmd.Run(); err != nil { 2797 t.Fatal(err) 2798 } 2799 } 2800 2801 // Issue 12843 2802 func TestServerDoS_MaxHeaderListSize(t *testing.T) { 2803 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {}) 2804 defer st.Close() 2805 2806 // shake hands 2807 frameSize := defaultMaxReadFrameSize 2808 var advHeaderListSize *uint32 2809 st.greetAndCheckSettings(func(s Setting) error { 2810 switch s.ID { 2811 case SettingMaxFrameSize: 2812 if s.Val < minMaxFrameSize { 2813 frameSize = minMaxFrameSize 2814 } else if s.Val > maxFrameSize { 2815 frameSize = maxFrameSize 2816 } else { 2817 frameSize = int(s.Val) 2818 } 2819 case SettingMaxHeaderListSize: 2820 advHeaderListSize = &s.Val 2821 } 2822 return nil 2823 }) 2824 2825 if advHeaderListSize == nil { 2826 t.Errorf("server didn't advertise a max header list size") 2827 } else if *advHeaderListSize == 0 { 2828 t.Errorf("server advertised a max header list size of 0") 2829 } 2830 2831 st.encodeHeaderField(":method", "GET") 2832 st.encodeHeaderField(":path", "/") 2833 st.encodeHeaderField(":scheme", "https") 2834 cookie := strings.Repeat("*", 4058) 2835 st.encodeHeaderField("cookie", cookie) 2836 st.writeHeaders(HeadersFrameParam{ 2837 StreamID: 1, 2838 BlockFragment: st.headerBuf.Bytes(), 2839 EndStream: true, 2840 EndHeaders: false, 2841 }) 2842 2843 // Capture the short encoding of a duplicate ~4K cookie, now 2844 // that we've already sent it once. 2845 st.headerBuf.Reset() 2846 st.encodeHeaderField("cookie", cookie) 2847 2848 // Now send 1MB of it. 2849 const size = 1 << 20 2850 b := bytes.Repeat(st.headerBuf.Bytes(), size/st.headerBuf.Len()) 2851 for len(b) > 0 { 2852 chunk := b 2853 if len(chunk) > frameSize { 2854 chunk = chunk[:frameSize] 2855 } 2856 b = b[len(chunk):] 2857 st.fr.WriteContinuation(1, len(b) == 0, chunk) 2858 } 2859 2860 h := st.wantHeaders() 2861 if !h.HeadersEnded() { 2862 t.Fatalf("Got HEADERS without END_HEADERS set: %v", h) 2863 } 2864 headers := st.decodeHeader(h.HeaderBlockFragment()) 2865 want := [][2]string{ 2866 {":status", "431"}, 2867 {"content-type", "text/html; charset=utf-8"}, 2868 {"content-length", "63"}, 2869 } 2870 if !reflect.DeepEqual(headers, want) { 2871 t.Errorf("Headers mismatch.\n got: %q\nwant: %q\n", headers, want) 2872 } 2873 } 2874 2875 func TestServer_Response_Stream_With_Missing_Trailer(t *testing.T) { 2876 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 2877 w.Header().Set("Trailer", "test-trailer") 2878 return nil 2879 }, func(st *serverTester) { 2880 getSlash(st) 2881 hf := st.wantHeaders() 2882 if !hf.HeadersEnded() { 2883 t.Fatal("want END_HEADERS flag") 2884 } 2885 df := st.wantData() 2886 if len(df.data) != 0 { 2887 t.Fatal("did not want data") 2888 } 2889 if !df.StreamEnded() { 2890 t.Fatal("want END_STREAM flag") 2891 } 2892 }) 2893 } 2894 2895 func TestCompressionErrorOnWrite(t *testing.T) { 2896 const maxStrLen = 8 << 10 2897 var serverConfig *http.Server 2898 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 2899 // No response body. 2900 }, func(ts *httptest.Server) { 2901 serverConfig = ts.Config 2902 serverConfig.MaxHeaderBytes = maxStrLen 2903 }) 2904 st.addLogFilter("connection error: COMPRESSION_ERROR") 2905 defer st.Close() 2906 st.greet() 2907 2908 maxAllowed := st.sc.framer.maxHeaderStringLen() 2909 2910 // Crank this up, now that we have a conn connected with the 2911 // hpack.Decoder's max string length set has been initialized 2912 // from the earlier low ~8K value. We want this higher so don't 2913 // hit the max header list size. We only want to test hitting 2914 // the max string size. 2915 serverConfig.MaxHeaderBytes = 1 << 20 2916 2917 // First a request with a header that's exactly the max allowed size 2918 // for the hpack compression. It's still too long for the header list 2919 // size, so we'll get the 431 error, but that keeps the compression 2920 // context still valid. 2921 hbf := st.encodeHeader("foo", strings.Repeat("a", maxAllowed)) 2922 2923 st.writeHeaders(HeadersFrameParam{ 2924 StreamID: 1, 2925 BlockFragment: hbf, 2926 EndStream: true, 2927 EndHeaders: true, 2928 }) 2929 h := st.wantHeaders() 2930 if !h.HeadersEnded() { 2931 t.Fatalf("Got HEADERS without END_HEADERS set: %v", h) 2932 } 2933 headers := st.decodeHeader(h.HeaderBlockFragment()) 2934 want := [][2]string{ 2935 {":status", "431"}, 2936 {"content-type", "text/html; charset=utf-8"}, 2937 {"content-length", "63"}, 2938 } 2939 if !reflect.DeepEqual(headers, want) { 2940 t.Errorf("Headers mismatch.\n got: %q\nwant: %q\n", headers, want) 2941 } 2942 df := st.wantData() 2943 if !strings.Contains(string(df.Data()), "HTTP Error 431") { 2944 t.Errorf("Unexpected data body: %q", df.Data()) 2945 } 2946 if !df.StreamEnded() { 2947 t.Fatalf("expect data stream end") 2948 } 2949 2950 // And now send one that's just one byte too big. 2951 hbf = st.encodeHeader("bar", strings.Repeat("b", maxAllowed+1)) 2952 st.writeHeaders(HeadersFrameParam{ 2953 StreamID: 3, 2954 BlockFragment: hbf, 2955 EndStream: true, 2956 EndHeaders: true, 2957 }) 2958 ga := st.wantGoAway() 2959 if ga.ErrCode != ErrCodeCompression { 2960 t.Errorf("GOAWAY err = %v; want ErrCodeCompression", ga.ErrCode) 2961 } 2962 } 2963 2964 func TestCompressionErrorOnClose(t *testing.T) { 2965 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 2966 // No response body. 2967 }) 2968 st.addLogFilter("connection error: COMPRESSION_ERROR") 2969 defer st.Close() 2970 st.greet() 2971 2972 hbf := st.encodeHeader("foo", "bar") 2973 hbf = hbf[:len(hbf)-1] // truncate one byte from the end, so hpack.Decoder.Close fails. 2974 st.writeHeaders(HeadersFrameParam{ 2975 StreamID: 1, 2976 BlockFragment: hbf, 2977 EndStream: true, 2978 EndHeaders: true, 2979 }) 2980 ga := st.wantGoAway() 2981 if ga.ErrCode != ErrCodeCompression { 2982 t.Errorf("GOAWAY err = %v; want ErrCodeCompression", ga.ErrCode) 2983 } 2984 } 2985 2986 // test that a server handler can read trailers from a client 2987 func TestServerReadsTrailers(t *testing.T) { 2988 const testBody = "some test body" 2989 writeReq := func(st *serverTester) { 2990 st.writeHeaders(HeadersFrameParam{ 2991 StreamID: 1, // clients send odd numbers 2992 BlockFragment: st.encodeHeader("trailer", "Foo, Bar", "trailer", "Baz"), 2993 EndStream: false, 2994 EndHeaders: true, 2995 }) 2996 st.writeData(1, false, []byte(testBody)) 2997 st.writeHeaders(HeadersFrameParam{ 2998 StreamID: 1, // clients send odd numbers 2999 BlockFragment: st.encodeHeaderRaw( 3000 "foo", "foov", 3001 "bar", "barv", 3002 "baz", "bazv", 3003 "surprise", "wasn't declared; shouldn't show up", 3004 ), 3005 EndStream: true, 3006 EndHeaders: true, 3007 }) 3008 } 3009 checkReq := func(r *http.Request) { 3010 wantTrailer := http.Header{ 3011 "Foo": nil, 3012 "Bar": nil, 3013 "Baz": nil, 3014 } 3015 if !reflect.DeepEqual(r.Trailer, wantTrailer) { 3016 t.Errorf("initial Trailer = %v; want %v", r.Trailer, wantTrailer) 3017 } 3018 slurp, err := ioutil.ReadAll(r.Body) 3019 if string(slurp) != testBody { 3020 t.Errorf("read body %q; want %q", slurp, testBody) 3021 } 3022 if err != nil { 3023 t.Fatalf("Body slurp: %v", err) 3024 } 3025 wantTrailerAfter := http.Header{ 3026 "Foo": {"foov"}, 3027 "Bar": {"barv"}, 3028 "Baz": {"bazv"}, 3029 } 3030 if !reflect.DeepEqual(r.Trailer, wantTrailerAfter) { 3031 t.Errorf("final Trailer = %v; want %v", r.Trailer, wantTrailerAfter) 3032 } 3033 } 3034 testServerRequest(t, writeReq, checkReq) 3035 } 3036 3037 // test that a server handler can send trailers 3038 func TestServerWritesTrailers_WithFlush(t *testing.T) { testServerWritesTrailers(t, true) } 3039 func TestServerWritesTrailers_WithoutFlush(t *testing.T) { testServerWritesTrailers(t, false) } 3040 3041 func testServerWritesTrailers(t *testing.T, withFlush bool) { 3042 // See https://httpwg.github.io/specs/rfc7540.html#rfc.section.8.1.3 3043 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 3044 w.Header().Set("Trailer", "Server-Trailer-A, Server-Trailer-B") 3045 w.Header().Add("Trailer", "Server-Trailer-C") 3046 w.Header().Add("Trailer", "Transfer-Encoding, Content-Length, Trailer") // filtered 3047 3048 // Regular headers: 3049 w.Header().Set("Foo", "Bar") 3050 w.Header().Set("Content-Length", "5") // len("Hello") 3051 3052 io.WriteString(w, "Hello") 3053 if withFlush { 3054 w.(http.Flusher).Flush() 3055 } 3056 w.Header().Set("Server-Trailer-A", "valuea") 3057 w.Header().Set("Server-Trailer-C", "valuec") // skipping B 3058 // After a flush, random keys like Server-Surprise shouldn't show up: 3059 w.Header().Set("Server-Surpise", "surprise! this isn't predeclared!") 3060 // But we do permit promoting keys to trailers after a 3061 // flush if they start with the magic 3062 // otherwise-invalid "Trailer:" prefix: 3063 w.Header().Set("Trailer:Post-Header-Trailer", "hi1") 3064 w.Header().Set("Trailer:post-header-trailer2", "hi2") 3065 w.Header().Set("Trailer:Range", "invalid") 3066 w.Header().Set("Trailer:Foo\x01Bogus", "invalid") 3067 w.Header().Set("Transfer-Encoding", "should not be included; Forbidden by RFC 7230 4.1.2") 3068 w.Header().Set("Content-Length", "should not be included; Forbidden by RFC 7230 4.1.2") 3069 w.Header().Set("Trailer", "should not be included; Forbidden by RFC 7230 4.1.2") 3070 return nil 3071 }, func(st *serverTester) { 3072 getSlash(st) 3073 hf := st.wantHeaders() 3074 if hf.StreamEnded() { 3075 t.Fatal("response HEADERS had END_STREAM") 3076 } 3077 if !hf.HeadersEnded() { 3078 t.Fatal("response HEADERS didn't have END_HEADERS") 3079 } 3080 goth := st.decodeHeader(hf.HeaderBlockFragment()) 3081 wanth := [][2]string{ 3082 {":status", "200"}, 3083 {"foo", "Bar"}, 3084 {"trailer", "Server-Trailer-A, Server-Trailer-B"}, 3085 {"trailer", "Server-Trailer-C"}, 3086 {"trailer", "Transfer-Encoding, Content-Length, Trailer"}, 3087 {"content-type", "text/plain; charset=utf-8"}, 3088 {"content-length", "5"}, 3089 } 3090 if !reflect.DeepEqual(goth, wanth) { 3091 t.Errorf("Header mismatch.\n got: %v\nwant: %v", goth, wanth) 3092 } 3093 df := st.wantData() 3094 if string(df.Data()) != "Hello" { 3095 t.Fatalf("Client read %q; want Hello", df.Data()) 3096 } 3097 if df.StreamEnded() { 3098 t.Fatalf("data frame had STREAM_ENDED") 3099 } 3100 tf := st.wantHeaders() // for the trailers 3101 if !tf.StreamEnded() { 3102 t.Fatalf("trailers HEADERS lacked END_STREAM") 3103 } 3104 if !tf.HeadersEnded() { 3105 t.Fatalf("trailers HEADERS lacked END_HEADERS") 3106 } 3107 wanth = [][2]string{ 3108 {"post-header-trailer", "hi1"}, 3109 {"post-header-trailer2", "hi2"}, 3110 {"server-trailer-a", "valuea"}, 3111 {"server-trailer-c", "valuec"}, 3112 } 3113 goth = st.decodeHeader(tf.HeaderBlockFragment()) 3114 if !reflect.DeepEqual(goth, wanth) { 3115 t.Errorf("Header mismatch.\n got: %v\nwant: %v", goth, wanth) 3116 } 3117 }) 3118 } 3119 3120 // validate transmitted header field names & values 3121 // golang.org/issue/14048 3122 func TestServerDoesntWriteInvalidHeaders(t *testing.T) { 3123 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 3124 w.Header().Add("OK1", "x") 3125 w.Header().Add("Bad:Colon", "x") // colon (non-token byte) in key 3126 w.Header().Add("Bad1\x00", "x") // null in key 3127 w.Header().Add("Bad2", "x\x00y") // null in value 3128 return nil 3129 }, func(st *serverTester) { 3130 getSlash(st) 3131 hf := st.wantHeaders() 3132 if !hf.StreamEnded() { 3133 t.Error("response HEADERS lacked END_STREAM") 3134 } 3135 if !hf.HeadersEnded() { 3136 t.Fatal("response HEADERS didn't have END_HEADERS") 3137 } 3138 goth := st.decodeHeader(hf.HeaderBlockFragment()) 3139 wanth := [][2]string{ 3140 {":status", "200"}, 3141 {"ok1", "x"}, 3142 {"content-length", "0"}, 3143 } 3144 if !reflect.DeepEqual(goth, wanth) { 3145 t.Errorf("Header mismatch.\n got: %v\nwant: %v", goth, wanth) 3146 } 3147 }) 3148 } 3149 3150 func BenchmarkServerGets(b *testing.B) { 3151 defer disableGoroutineTracking()() 3152 b.ReportAllocs() 3153 3154 const msg = "Hello, world" 3155 st := newServerTester(b, func(w http.ResponseWriter, r *http.Request) { 3156 io.WriteString(w, msg) 3157 }) 3158 defer st.Close() 3159 st.greet() 3160 3161 // Give the server quota to reply. (plus it has the 64KB) 3162 if err := st.fr.WriteWindowUpdate(0, uint32(b.N*len(msg))); err != nil { 3163 b.Fatal(err) 3164 } 3165 3166 for i := 0; i < b.N; i++ { 3167 id := 1 + uint32(i)*2 3168 st.writeHeaders(HeadersFrameParam{ 3169 StreamID: id, 3170 BlockFragment: st.encodeHeader(), 3171 EndStream: true, 3172 EndHeaders: true, 3173 }) 3174 st.wantHeaders() 3175 df := st.wantData() 3176 if !df.StreamEnded() { 3177 b.Fatalf("DATA didn't have END_STREAM; got %v", df) 3178 } 3179 } 3180 } 3181 3182 func BenchmarkServerPosts(b *testing.B) { 3183 defer disableGoroutineTracking()() 3184 b.ReportAllocs() 3185 3186 const msg = "Hello, world" 3187 st := newServerTester(b, func(w http.ResponseWriter, r *http.Request) { 3188 // Consume the (empty) body from th peer before replying, otherwise 3189 // the server will sometimes (depending on scheduling) send the peer a 3190 // a RST_STREAM with the CANCEL error code. 3191 if n, err := io.Copy(ioutil.Discard, r.Body); n != 0 || err != nil { 3192 b.Errorf("Copy error; got %v, %v; want 0, nil", n, err) 3193 } 3194 io.WriteString(w, msg) 3195 }) 3196 defer st.Close() 3197 st.greet() 3198 3199 // Give the server quota to reply. (plus it has the 64KB) 3200 if err := st.fr.WriteWindowUpdate(0, uint32(b.N*len(msg))); err != nil { 3201 b.Fatal(err) 3202 } 3203 3204 for i := 0; i < b.N; i++ { 3205 id := 1 + uint32(i)*2 3206 st.writeHeaders(HeadersFrameParam{ 3207 StreamID: id, 3208 BlockFragment: st.encodeHeader(":method", "POST"), 3209 EndStream: false, 3210 EndHeaders: true, 3211 }) 3212 st.writeData(id, true, nil) 3213 st.wantHeaders() 3214 df := st.wantData() 3215 if !df.StreamEnded() { 3216 b.Fatalf("DATA didn't have END_STREAM; got %v", df) 3217 } 3218 } 3219 } 3220 3221 // Send a stream of messages from server to client in separate data frames. 3222 // Brings up performance issues seen in long streams. 3223 // Created to show problem in go issue #18502 3224 func BenchmarkServerToClientStreamDefaultOptions(b *testing.B) { 3225 benchmarkServerToClientStream(b) 3226 } 3227 3228 // Justification for Change-Id: Iad93420ef6c3918f54249d867098f1dadfa324d8 3229 // Expect to see memory/alloc reduction by opting in to Frame reuse with the Framer. 3230 func BenchmarkServerToClientStreamReuseFrames(b *testing.B) { 3231 benchmarkServerToClientStream(b, optFramerReuseFrames) 3232 } 3233 3234 func benchmarkServerToClientStream(b *testing.B, newServerOpts ...interface{}) { 3235 defer disableGoroutineTracking()() 3236 b.ReportAllocs() 3237 const msgLen = 1 3238 // default window size 3239 const windowSize = 1<<16 - 1 3240 3241 // next message to send from the server and for the client to expect 3242 nextMsg := func(i int) []byte { 3243 msg := make([]byte, msgLen) 3244 msg[0] = byte(i) 3245 if len(msg) != msgLen { 3246 panic("invalid test setup msg length") 3247 } 3248 return msg 3249 } 3250 3251 st := newServerTester(b, func(w http.ResponseWriter, r *http.Request) { 3252 // Consume the (empty) body from th peer before replying, otherwise 3253 // the server will sometimes (depending on scheduling) send the peer a 3254 // a RST_STREAM with the CANCEL error code. 3255 if n, err := io.Copy(ioutil.Discard, r.Body); n != 0 || err != nil { 3256 b.Errorf("Copy error; got %v, %v; want 0, nil", n, err) 3257 } 3258 for i := 0; i < b.N; i += 1 { 3259 w.Write(nextMsg(i)) 3260 w.(http.Flusher).Flush() 3261 } 3262 }, newServerOpts...) 3263 defer st.Close() 3264 st.greet() 3265 3266 const id = uint32(1) 3267 3268 st.writeHeaders(HeadersFrameParam{ 3269 StreamID: id, 3270 BlockFragment: st.encodeHeader(":method", "POST"), 3271 EndStream: false, 3272 EndHeaders: true, 3273 }) 3274 3275 st.writeData(id, true, nil) 3276 st.wantHeaders() 3277 3278 var pendingWindowUpdate = uint32(0) 3279 3280 for i := 0; i < b.N; i += 1 { 3281 expected := nextMsg(i) 3282 df := st.wantData() 3283 if bytes.Compare(expected, df.data) != 0 { 3284 b.Fatalf("Bad message received; want %v; got %v", expected, df.data) 3285 } 3286 // try to send infrequent but large window updates so they don't overwhelm the test 3287 pendingWindowUpdate += uint32(len(df.data)) 3288 if pendingWindowUpdate >= windowSize/2 { 3289 if err := st.fr.WriteWindowUpdate(0, pendingWindowUpdate); err != nil { 3290 b.Fatal(err) 3291 } 3292 if err := st.fr.WriteWindowUpdate(id, pendingWindowUpdate); err != nil { 3293 b.Fatal(err) 3294 } 3295 pendingWindowUpdate = 0 3296 } 3297 } 3298 df := st.wantData() 3299 if !df.StreamEnded() { 3300 b.Fatalf("DATA didn't have END_STREAM; got %v", df) 3301 } 3302 } 3303 3304 // go-fuzz bug, originally reported at https://github.com/bradfitz/http2/issues/53 3305 // Verify we don't hang. 3306 func TestIssue53(t *testing.T) { 3307 const data = "PRI * HTTP/2.0\r\n\r\nSM" + 3308 "\r\n\r\n\x00\x00\x00\x01\ainfinfin\ad" 3309 s := &http.Server{ 3310 ErrorLog: log.New(io.MultiWriter(stderrv(), twriter{t: t}), "", log.LstdFlags), 3311 Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 3312 w.Write([]byte("hello")) 3313 }), 3314 } 3315 s2 := &Server{ 3316 MaxReadFrameSize: 1 << 16, 3317 PermitProhibitedCipherSuites: true, 3318 } 3319 c := &issue53Conn{[]byte(data), false, false} 3320 s2.ServeConn(c, &ServeConnOpts{BaseConfig: s}) 3321 if !c.closed { 3322 t.Fatal("connection is not closed") 3323 } 3324 } 3325 3326 type issue53Conn struct { 3327 data []byte 3328 closed bool 3329 written bool 3330 } 3331 3332 func (c *issue53Conn) Read(b []byte) (n int, err error) { 3333 if len(c.data) == 0 { 3334 return 0, io.EOF 3335 } 3336 n = copy(b, c.data) 3337 c.data = c.data[n:] 3338 return 3339 } 3340 3341 func (c *issue53Conn) Write(b []byte) (n int, err error) { 3342 c.written = true 3343 return len(b), nil 3344 } 3345 3346 func (c *issue53Conn) Close() error { 3347 c.closed = true 3348 return nil 3349 } 3350 3351 func (c *issue53Conn) LocalAddr() net.Addr { 3352 return &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 49706} 3353 } 3354 func (c *issue53Conn) RemoteAddr() net.Addr { 3355 return &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 49706} 3356 } 3357 func (c *issue53Conn) SetDeadline(t time.Time) error { return nil } 3358 func (c *issue53Conn) SetReadDeadline(t time.Time) error { return nil } 3359 func (c *issue53Conn) SetWriteDeadline(t time.Time) error { return nil } 3360 3361 // golang.org/issue/33839 3362 func TestServeConnOptsNilReceiverBehavior(t *testing.T) { 3363 defer func() { 3364 if r := recover(); r != nil { 3365 t.Errorf("got a panic that should not happen: %v", r) 3366 } 3367 }() 3368 3369 var o *ServeConnOpts 3370 if o.context() == nil { 3371 t.Error("o.context should not return nil") 3372 } 3373 if o.baseConfig() == nil { 3374 t.Error("o.baseConfig should not return nil") 3375 } 3376 if o.handler() == nil { 3377 t.Error("o.handler should not return nil") 3378 } 3379 } 3380 3381 // golang.org/issue/12895 3382 func TestConfigureServer(t *testing.T) { 3383 tests := []struct { 3384 name string 3385 tlsConfig *tls.Config 3386 wantErr string 3387 }{ 3388 { 3389 name: "empty server", 3390 }, 3391 { 3392 name: "empty CipherSuites", 3393 tlsConfig: &tls.Config{}, 3394 }, 3395 { 3396 name: "bad CipherSuites but MinVersion TLS 1.3", 3397 tlsConfig: &tls.Config{ 3398 MinVersion: tls.VersionTLS13, 3399 CipherSuites: []uint16{tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384}, 3400 }, 3401 }, 3402 { 3403 name: "just the required cipher suite", 3404 tlsConfig: &tls.Config{ 3405 CipherSuites: []uint16{tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256}, 3406 }, 3407 }, 3408 { 3409 name: "just the alternative required cipher suite", 3410 tlsConfig: &tls.Config{ 3411 CipherSuites: []uint16{tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256}, 3412 }, 3413 }, 3414 { 3415 name: "missing required cipher suite", 3416 tlsConfig: &tls.Config{ 3417 CipherSuites: []uint16{tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384}, 3418 }, 3419 wantErr: "is missing an HTTP/2-required", 3420 }, 3421 { 3422 name: "required after bad", 3423 tlsConfig: &tls.Config{ 3424 CipherSuites: []uint16{tls.TLS_RSA_WITH_RC4_128_SHA, tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256}, 3425 }, 3426 }, 3427 { 3428 name: "bad after required", 3429 tlsConfig: &tls.Config{ 3430 CipherSuites: []uint16{tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, tls.TLS_RSA_WITH_RC4_128_SHA}, 3431 }, 3432 }, 3433 } 3434 for _, tt := range tests { 3435 srv := &http.Server{TLSConfig: tt.tlsConfig} 3436 err := ConfigureServer(srv, nil) 3437 if (err != nil) != (tt.wantErr != "") { 3438 if tt.wantErr != "" { 3439 t.Errorf("%s: success, but want error", tt.name) 3440 } else { 3441 t.Errorf("%s: unexpected error: %v", tt.name, err) 3442 } 3443 } 3444 if err != nil && tt.wantErr != "" && !strings.Contains(err.Error(), tt.wantErr) { 3445 t.Errorf("%s: err = %v; want substring %q", tt.name, err, tt.wantErr) 3446 } 3447 if err == nil && !srv.TLSConfig.PreferServerCipherSuites { 3448 t.Errorf("%s: PreferServerCipherSuite is false; want true", tt.name) 3449 } 3450 } 3451 } 3452 3453 func TestServerRejectHeadWithBody(t *testing.T) { 3454 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 3455 // No response body. 3456 }) 3457 defer st.Close() 3458 st.greet() 3459 st.writeHeaders(HeadersFrameParam{ 3460 StreamID: 1, // clients send odd numbers 3461 BlockFragment: st.encodeHeader(":method", "HEAD"), 3462 EndStream: false, // what we're testing, a bogus HEAD request with body 3463 EndHeaders: true, 3464 }) 3465 st.wantRSTStream(1, ErrCodeProtocol) 3466 } 3467 3468 func TestServerNoAutoContentLengthOnHead(t *testing.T) { 3469 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 3470 // No response body. (or smaller than one frame) 3471 }) 3472 defer st.Close() 3473 st.greet() 3474 st.writeHeaders(HeadersFrameParam{ 3475 StreamID: 1, // clients send odd numbers 3476 BlockFragment: st.encodeHeader(":method", "HEAD"), 3477 EndStream: true, 3478 EndHeaders: true, 3479 }) 3480 h := st.wantHeaders() 3481 headers := st.decodeHeader(h.HeaderBlockFragment()) 3482 want := [][2]string{ 3483 {":status", "200"}, 3484 } 3485 if !reflect.DeepEqual(headers, want) { 3486 t.Errorf("Headers mismatch.\n got: %q\nwant: %q\n", headers, want) 3487 } 3488 } 3489 3490 // golang.org/issue/13495 3491 func TestServerNoDuplicateContentType(t *testing.T) { 3492 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 3493 w.Header()["Content-Type"] = []string{""} 3494 fmt.Fprintf(w, "<html><head></head><body>hi</body></html>") 3495 }) 3496 defer st.Close() 3497 st.greet() 3498 st.writeHeaders(HeadersFrameParam{ 3499 StreamID: 1, 3500 BlockFragment: st.encodeHeader(), 3501 EndStream: true, 3502 EndHeaders: true, 3503 }) 3504 h := st.wantHeaders() 3505 headers := st.decodeHeader(h.HeaderBlockFragment()) 3506 want := [][2]string{ 3507 {":status", "200"}, 3508 {"content-type", ""}, 3509 {"content-length", "41"}, 3510 } 3511 if !reflect.DeepEqual(headers, want) { 3512 t.Errorf("Headers mismatch.\n got: %q\nwant: %q\n", headers, want) 3513 } 3514 } 3515 3516 func disableGoroutineTracking() (restore func()) { 3517 old := DebugGoroutines 3518 DebugGoroutines = false 3519 return func() { DebugGoroutines = old } 3520 } 3521 3522 func BenchmarkServer_GetRequest(b *testing.B) { 3523 defer disableGoroutineTracking()() 3524 b.ReportAllocs() 3525 const msg = "Hello, world." 3526 st := newServerTester(b, func(w http.ResponseWriter, r *http.Request) { 3527 n, err := io.Copy(ioutil.Discard, r.Body) 3528 if err != nil || n > 0 { 3529 b.Errorf("Read %d bytes, error %v; want 0 bytes.", n, err) 3530 } 3531 io.WriteString(w, msg) 3532 }) 3533 defer st.Close() 3534 3535 st.greet() 3536 // Give the server quota to reply. (plus it has the 64KB) 3537 if err := st.fr.WriteWindowUpdate(0, uint32(b.N*len(msg))); err != nil { 3538 b.Fatal(err) 3539 } 3540 hbf := st.encodeHeader(":method", "GET") 3541 for i := 0; i < b.N; i++ { 3542 streamID := uint32(1 + 2*i) 3543 st.writeHeaders(HeadersFrameParam{ 3544 StreamID: streamID, 3545 BlockFragment: hbf, 3546 EndStream: true, 3547 EndHeaders: true, 3548 }) 3549 st.wantHeaders() 3550 st.wantData() 3551 } 3552 } 3553 3554 func BenchmarkServer_PostRequest(b *testing.B) { 3555 defer disableGoroutineTracking()() 3556 b.ReportAllocs() 3557 const msg = "Hello, world." 3558 st := newServerTester(b, func(w http.ResponseWriter, r *http.Request) { 3559 n, err := io.Copy(ioutil.Discard, r.Body) 3560 if err != nil || n > 0 { 3561 b.Errorf("Read %d bytes, error %v; want 0 bytes.", n, err) 3562 } 3563 io.WriteString(w, msg) 3564 }) 3565 defer st.Close() 3566 st.greet() 3567 // Give the server quota to reply. (plus it has the 64KB) 3568 if err := st.fr.WriteWindowUpdate(0, uint32(b.N*len(msg))); err != nil { 3569 b.Fatal(err) 3570 } 3571 hbf := st.encodeHeader(":method", "POST") 3572 for i := 0; i < b.N; i++ { 3573 streamID := uint32(1 + 2*i) 3574 st.writeHeaders(HeadersFrameParam{ 3575 StreamID: streamID, 3576 BlockFragment: hbf, 3577 EndStream: false, 3578 EndHeaders: true, 3579 }) 3580 st.writeData(streamID, true, nil) 3581 st.wantHeaders() 3582 st.wantData() 3583 } 3584 } 3585 3586 type connStateConn struct { 3587 net.Conn 3588 cs tls.ConnectionState 3589 } 3590 3591 func (c connStateConn) ConnectionState() tls.ConnectionState { return c.cs } 3592 3593 // golang.org/issue/12737 -- handle any net.Conn, not just 3594 // *tls.Conn. 3595 func TestServerHandleCustomConn(t *testing.T) { 3596 var s Server 3597 c1, c2 := net.Pipe() 3598 clientDone := make(chan struct{}) 3599 handlerDone := make(chan struct{}) 3600 var req *http.Request 3601 go func() { 3602 defer close(clientDone) 3603 defer c2.Close() 3604 fr := NewFramer(c2, c2) 3605 io.WriteString(c2, ClientPreface) 3606 fr.WriteSettings() 3607 fr.WriteSettingsAck() 3608 f, err := fr.ReadFrame() 3609 if err != nil { 3610 t.Error(err) 3611 return 3612 } 3613 if sf, ok := f.(*SettingsFrame); !ok || sf.IsAck() { 3614 t.Errorf("Got %v; want non-ACK SettingsFrame", summarizeFrame(f)) 3615 return 3616 } 3617 f, err = fr.ReadFrame() 3618 if err != nil { 3619 t.Error(err) 3620 return 3621 } 3622 if sf, ok := f.(*SettingsFrame); !ok || !sf.IsAck() { 3623 t.Errorf("Got %v; want ACK SettingsFrame", summarizeFrame(f)) 3624 return 3625 } 3626 var henc hpackEncoder 3627 fr.WriteHeaders(HeadersFrameParam{ 3628 StreamID: 1, 3629 BlockFragment: henc.encodeHeaderRaw(t, ":method", "GET", ":path", "/", ":scheme", "https", ":authority", "foo.com"), 3630 EndStream: true, 3631 EndHeaders: true, 3632 }) 3633 go io.Copy(ioutil.Discard, c2) 3634 <-handlerDone 3635 }() 3636 const testString = "my custom ConnectionState" 3637 fakeConnState := tls.ConnectionState{ 3638 ServerName: testString, 3639 Version: tls.VersionTLS12, 3640 CipherSuite: cipher_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 3641 } 3642 go s.ServeConn(connStateConn{c1, fakeConnState}, &ServeConnOpts{ 3643 BaseConfig: &http.Server{ 3644 Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 3645 defer close(handlerDone) 3646 req = r 3647 }), 3648 }}) 3649 select { 3650 case <-clientDone: 3651 case <-time.After(5 * time.Second): 3652 t.Fatal("timeout waiting for handler") 3653 } 3654 if req.TLS == nil { 3655 t.Fatalf("Request.TLS is nil. Got: %#v", req) 3656 } 3657 if req.TLS.ServerName != testString { 3658 t.Fatalf("Request.TLS = %+v; want ServerName of %q", req.TLS, testString) 3659 } 3660 } 3661 3662 // golang.org/issue/14214 3663 func TestServer_Rejects_ConnHeaders(t *testing.T) { 3664 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 3665 t.Error("should not get to Handler") 3666 }) 3667 defer st.Close() 3668 st.greet() 3669 st.bodylessReq1("connection", "foo") 3670 hf := st.wantHeaders() 3671 goth := st.decodeHeader(hf.HeaderBlockFragment()) 3672 wanth := [][2]string{ 3673 {":status", "400"}, 3674 {"content-type", "text/plain; charset=utf-8"}, 3675 {"x-content-type-options", "nosniff"}, 3676 {"content-length", "51"}, 3677 } 3678 if !reflect.DeepEqual(goth, wanth) { 3679 t.Errorf("Got headers %v; want %v", goth, wanth) 3680 } 3681 } 3682 3683 type hpackEncoder struct { 3684 enc *hpack.Encoder 3685 buf bytes.Buffer 3686 } 3687 3688 func (he *hpackEncoder) encodeHeaderRaw(t *testing.T, headers ...string) []byte { 3689 if len(headers)%2 == 1 { 3690 panic("odd number of kv args") 3691 } 3692 he.buf.Reset() 3693 if he.enc == nil { 3694 he.enc = hpack.NewEncoder(&he.buf) 3695 } 3696 for len(headers) > 0 { 3697 k, v := headers[0], headers[1] 3698 err := he.enc.WriteField(hpack.HeaderField{Name: k, Value: v}) 3699 if err != nil { 3700 t.Fatalf("HPACK encoding error for %q/%q: %v", k, v, err) 3701 } 3702 headers = headers[2:] 3703 } 3704 return he.buf.Bytes() 3705 } 3706 3707 func TestCheckValidHTTP2Request(t *testing.T) { 3708 tests := []struct { 3709 h http.Header 3710 want error 3711 }{ 3712 { 3713 h: http.Header{"Te": {"trailers"}}, 3714 want: nil, 3715 }, 3716 { 3717 h: http.Header{"Te": {"trailers", "bogus"}}, 3718 want: errors.New(`request header "TE" may only be "trailers" in HTTP/2`), 3719 }, 3720 { 3721 h: http.Header{"Foo": {""}}, 3722 want: nil, 3723 }, 3724 { 3725 h: http.Header{"Connection": {""}}, 3726 want: errors.New(`request header "Connection" is not valid in HTTP/2`), 3727 }, 3728 { 3729 h: http.Header{"Proxy-Connection": {""}}, 3730 want: errors.New(`request header "Proxy-Connection" is not valid in HTTP/2`), 3731 }, 3732 { 3733 h: http.Header{"Keep-Alive": {""}}, 3734 want: errors.New(`request header "Keep-Alive" is not valid in HTTP/2`), 3735 }, 3736 { 3737 h: http.Header{"Upgrade": {""}}, 3738 want: errors.New(`request header "Upgrade" is not valid in HTTP/2`), 3739 }, 3740 } 3741 for i, tt := range tests { 3742 got := checkValidHTTP2RequestHeaders(tt.h) 3743 if !equalError(got, tt.want) { 3744 t.Errorf("%d. checkValidHTTP2Request = %v; want %v", i, got, tt.want) 3745 } 3746 } 3747 } 3748 3749 // golang.org/issue/14030 3750 func TestExpect100ContinueAfterHandlerWrites(t *testing.T) { 3751 const msg = "Hello" 3752 const msg2 = "World" 3753 3754 doRead := make(chan bool, 1) 3755 defer close(doRead) // fallback cleanup 3756 3757 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 3758 io.WriteString(w, msg) 3759 w.(http.Flusher).Flush() 3760 3761 // Do a read, which might force a 100-continue status to be sent. 3762 <-doRead 3763 r.Body.Read(make([]byte, 10)) 3764 3765 io.WriteString(w, msg2) 3766 3767 }, optOnlyServer) 3768 defer st.Close() 3769 3770 tr := &Transport{TLSClientConfig: tlsConfigInsecure} 3771 defer tr.CloseIdleConnections() 3772 3773 req, _ := http.NewRequest("POST", st.ts.URL, io.LimitReader(neverEnding('A'), 2<<20)) 3774 req.Header.Set("Expect", "100-continue") 3775 3776 res, err := tr.RoundTrip(req) 3777 if err != nil { 3778 t.Fatal(err) 3779 } 3780 defer res.Body.Close() 3781 3782 buf := make([]byte, len(msg)) 3783 if _, err := io.ReadFull(res.Body, buf); err != nil { 3784 t.Fatal(err) 3785 } 3786 if string(buf) != msg { 3787 t.Fatalf("msg = %q; want %q", buf, msg) 3788 } 3789 3790 doRead <- true 3791 3792 if _, err := io.ReadFull(res.Body, buf); err != nil { 3793 t.Fatal(err) 3794 } 3795 if string(buf) != msg2 { 3796 t.Fatalf("second msg = %q; want %q", buf, msg2) 3797 } 3798 } 3799 3800 type funcReader func([]byte) (n int, err error) 3801 3802 func (f funcReader) Read(p []byte) (n int, err error) { return f(p) } 3803 3804 // golang.org/issue/16481 -- return flow control when streams close with unread data. 3805 // (The Server version of the bug. See also TestUnreadFlowControlReturned_Transport) 3806 func TestUnreadFlowControlReturned_Server(t *testing.T) { 3807 for _, tt := range []struct { 3808 name string 3809 reqFn func(r *http.Request) 3810 }{ 3811 { 3812 "body-open", 3813 func(r *http.Request) {}, 3814 }, 3815 { 3816 "body-closed", 3817 func(r *http.Request) { 3818 r.Body.Close() 3819 }, 3820 }, 3821 { 3822 "read-1-byte-and-close", 3823 func(r *http.Request) { 3824 b := make([]byte, 1) 3825 r.Body.Read(b) 3826 r.Body.Close() 3827 }, 3828 }, 3829 } { 3830 t.Run(tt.name, func(t *testing.T) { 3831 unblock := make(chan bool, 1) 3832 defer close(unblock) 3833 3834 timeOut := time.NewTimer(5 * time.Second) 3835 defer timeOut.Stop() 3836 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 3837 // Don't read the 16KB request body. Wait until the client's 3838 // done sending it and then return. This should cause the Server 3839 // to then return those 16KB of flow control to the client. 3840 tt.reqFn(r) 3841 select { 3842 case <-unblock: 3843 case <-timeOut.C: 3844 t.Fatal(tt.name, "timedout") 3845 } 3846 }, optOnlyServer) 3847 defer st.Close() 3848 3849 tr := &Transport{TLSClientConfig: tlsConfigInsecure} 3850 defer tr.CloseIdleConnections() 3851 3852 // This previously hung on the 4th iteration. 3853 iters := 100 3854 if testing.Short() { 3855 iters = 20 3856 } 3857 for i := 0; i < iters; i++ { 3858 body := io.MultiReader( 3859 io.LimitReader(neverEnding('A'), 16<<10), 3860 funcReader(func([]byte) (n int, err error) { 3861 unblock <- true 3862 return 0, io.EOF 3863 }), 3864 ) 3865 req, _ := http.NewRequest("POST", st.ts.URL, body) 3866 res, err := tr.RoundTrip(req) 3867 if err != nil { 3868 t.Fatal(tt.name, err) 3869 } 3870 res.Body.Close() 3871 } 3872 }) 3873 } 3874 } 3875 3876 func TestServerIdleTimeout(t *testing.T) { 3877 if testing.Short() { 3878 t.Skip("skipping in short mode") 3879 } 3880 3881 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 3882 }, func(h2s *Server) { 3883 h2s.IdleTimeout = 500 * time.Millisecond 3884 }) 3885 defer st.Close() 3886 3887 st.greet() 3888 ga := st.wantGoAway() 3889 if ga.ErrCode != ErrCodeNo { 3890 t.Errorf("GOAWAY error = %v; want ErrCodeNo", ga.ErrCode) 3891 } 3892 } 3893 3894 func TestServerIdleTimeout_AfterRequest(t *testing.T) { 3895 if testing.Short() { 3896 t.Skip("skipping in short mode") 3897 } 3898 const timeout = 250 * time.Millisecond 3899 3900 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 3901 time.Sleep(timeout * 2) 3902 }, func(h2s *Server) { 3903 h2s.IdleTimeout = timeout 3904 }) 3905 defer st.Close() 3906 3907 st.greet() 3908 3909 // Send a request which takes twice the timeout. Verifies the 3910 // idle timeout doesn't fire while we're in a request: 3911 st.bodylessReq1() 3912 st.wantHeaders() 3913 3914 // But the idle timeout should be rearmed after the request 3915 // is done: 3916 ga := st.wantGoAway() 3917 if ga.ErrCode != ErrCodeNo { 3918 t.Errorf("GOAWAY error = %v; want ErrCodeNo", ga.ErrCode) 3919 } 3920 } 3921 3922 // grpc-go closes the Request.Body currently with a Read. 3923 // Verify that it doesn't race. 3924 // See https://github.com/grpc/grpc-go/pull/938 3925 func TestRequestBodyReadCloseRace(t *testing.T) { 3926 for i := 0; i < 100; i++ { 3927 body := &requestBody{ 3928 pipe: &pipe{ 3929 b: new(bytes.Buffer), 3930 }, 3931 } 3932 body.pipe.CloseWithError(io.EOF) 3933 3934 done := make(chan bool, 1) 3935 buf := make([]byte, 10) 3936 go func() { 3937 time.Sleep(1 * time.Millisecond) 3938 body.Close() 3939 done <- true 3940 }() 3941 body.Read(buf) 3942 <-done 3943 } 3944 } 3945 3946 func TestIssue20704Race(t *testing.T) { 3947 if testing.Short() && os.Getenv("GO_BUILDER_NAME") == "" { 3948 t.Skip("skipping in short mode") 3949 } 3950 const ( 3951 itemSize = 1 << 10 3952 itemCount = 100 3953 ) 3954 3955 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 3956 for i := 0; i < itemCount; i++ { 3957 _, err := w.Write(make([]byte, itemSize)) 3958 if err != nil { 3959 return 3960 } 3961 } 3962 }, optOnlyServer) 3963 defer st.Close() 3964 3965 tr := &Transport{TLSClientConfig: tlsConfigInsecure} 3966 defer tr.CloseIdleConnections() 3967 cl := &http.Client{Transport: tr} 3968 3969 for i := 0; i < 1000; i++ { 3970 resp, err := cl.Get(st.ts.URL) 3971 if err != nil { 3972 t.Fatal(err) 3973 } 3974 // Force a RST stream to the server by closing without 3975 // reading the body: 3976 resp.Body.Close() 3977 } 3978 } 3979 3980 func TestServer_Rejects_TooSmall(t *testing.T) { 3981 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 3982 ioutil.ReadAll(r.Body) 3983 return nil 3984 }, func(st *serverTester) { 3985 st.writeHeaders(HeadersFrameParam{ 3986 StreamID: 1, // clients send odd numbers 3987 BlockFragment: st.encodeHeader( 3988 ":method", "POST", 3989 "content-length", "4", 3990 ), 3991 EndStream: false, // to say DATA frames are coming 3992 EndHeaders: true, 3993 }) 3994 st.writeData(1, true, []byte("12345")) 3995 3996 st.wantRSTStream(1, ErrCodeProtocol) 3997 }) 3998 } 3999 4000 // Tests that a handler setting "Connection: close" results in a GOAWAY being sent, 4001 // and the connection still completing. 4002 func TestServerHandlerConnectionClose(t *testing.T) { 4003 unblockHandler := make(chan bool, 1) 4004 defer close(unblockHandler) // backup; in case of errors 4005 testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error { 4006 w.Header().Set("Connection", "close") 4007 w.Header().Set("Foo", "bar") 4008 w.(http.Flusher).Flush() 4009 <-unblockHandler 4010 return nil 4011 }, func(st *serverTester) { 4012 st.writeHeaders(HeadersFrameParam{ 4013 StreamID: 1, 4014 BlockFragment: st.encodeHeader(), 4015 EndStream: true, 4016 EndHeaders: true, 4017 }) 4018 var sawGoAway bool 4019 var sawRes bool 4020 for { 4021 f, err := st.readFrame() 4022 if err == io.EOF { 4023 break 4024 } 4025 if err != nil { 4026 t.Fatal(err) 4027 } 4028 switch f := f.(type) { 4029 case *GoAwayFrame: 4030 sawGoAway = true 4031 unblockHandler <- true 4032 if f.LastStreamID != 1 || f.ErrCode != ErrCodeNo { 4033 t.Errorf("unexpected GOAWAY frame: %v", summarizeFrame(f)) 4034 } 4035 case *HeadersFrame: 4036 goth := st.decodeHeader(f.HeaderBlockFragment()) 4037 wanth := [][2]string{ 4038 {":status", "200"}, 4039 {"foo", "bar"}, 4040 } 4041 if !reflect.DeepEqual(goth, wanth) { 4042 t.Errorf("got headers %v; want %v", goth, wanth) 4043 } 4044 sawRes = true 4045 case *DataFrame: 4046 if f.StreamID != 1 || !f.StreamEnded() || len(f.Data()) != 0 { 4047 t.Errorf("unexpected DATA frame: %v", summarizeFrame(f)) 4048 } 4049 default: 4050 t.Logf("unexpected frame: %v", summarizeFrame(f)) 4051 } 4052 } 4053 if !sawGoAway { 4054 t.Errorf("didn't see GOAWAY") 4055 } 4056 if !sawRes { 4057 t.Errorf("didn't see response") 4058 } 4059 }) 4060 } 4061 4062 func TestServer_Headers_HalfCloseRemote(t *testing.T) { 4063 var st *serverTester 4064 writeData := make(chan bool) 4065 writeHeaders := make(chan bool) 4066 leaveHandler := make(chan bool) 4067 st = newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 4068 if st.stream(1) == nil { 4069 t.Errorf("nil stream 1 in handler") 4070 } 4071 if got, want := st.streamState(1), stateOpen; got != want { 4072 t.Errorf("in handler, state is %v; want %v", got, want) 4073 } 4074 writeData <- true 4075 if n, err := r.Body.Read(make([]byte, 1)); n != 0 || err != io.EOF { 4076 t.Errorf("body read = %d, %v; want 0, EOF", n, err) 4077 } 4078 if got, want := st.streamState(1), stateHalfClosedRemote; got != want { 4079 t.Errorf("in handler, state is %v; want %v", got, want) 4080 } 4081 writeHeaders <- true 4082 4083 <-leaveHandler 4084 }) 4085 st.greet() 4086 4087 st.writeHeaders(HeadersFrameParam{ 4088 StreamID: 1, 4089 BlockFragment: st.encodeHeader(), 4090 EndStream: false, // keep it open 4091 EndHeaders: true, 4092 }) 4093 <-writeData 4094 st.writeData(1, true, nil) 4095 4096 <-writeHeaders 4097 4098 st.writeHeaders(HeadersFrameParam{ 4099 StreamID: 1, 4100 BlockFragment: st.encodeHeader(), 4101 EndStream: false, // keep it open 4102 EndHeaders: true, 4103 }) 4104 4105 defer close(leaveHandler) 4106 4107 st.wantRSTStream(1, ErrCodeStreamClosed) 4108 } 4109 4110 func TestServerGracefulShutdown(t *testing.T) { 4111 var st *serverTester 4112 handlerDone := make(chan struct{}) 4113 st = newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 4114 defer close(handlerDone) 4115 go st.ts.Config.Shutdown(context.Background()) 4116 4117 ga := st.wantGoAway() 4118 if ga.ErrCode != ErrCodeNo { 4119 t.Errorf("GOAWAY error = %v; want ErrCodeNo", ga.ErrCode) 4120 } 4121 if ga.LastStreamID != 1 { 4122 t.Errorf("GOAWAY LastStreamID = %v; want 1", ga.LastStreamID) 4123 } 4124 4125 w.Header().Set("x-foo", "bar") 4126 }) 4127 defer st.Close() 4128 4129 st.greet() 4130 st.bodylessReq1() 4131 4132 select { 4133 case <-handlerDone: 4134 case <-time.After(5 * time.Second): 4135 t.Fatalf("server did not shutdown?") 4136 } 4137 hf := st.wantHeaders() 4138 goth := st.decodeHeader(hf.HeaderBlockFragment()) 4139 wanth := [][2]string{ 4140 {":status", "200"}, 4141 {"x-foo", "bar"}, 4142 {"content-length", "0"}, 4143 } 4144 if !reflect.DeepEqual(goth, wanth) { 4145 t.Errorf("Got headers %v; want %v", goth, wanth) 4146 } 4147 4148 n, err := st.cc.Read([]byte{0}) 4149 if n != 0 || err == nil { 4150 t.Errorf("Read = %v, %v; want 0, non-nil", n, err) 4151 } 4152 } 4153 4154 // Issue 31753: don't sniff when Content-Encoding is set 4155 func TestContentEncodingNoSniffing(t *testing.T) { 4156 type resp struct { 4157 name string 4158 body []byte 4159 // setting Content-Encoding as an interface instead of a string 4160 // directly, so as to differentiate between 3 states: 4161 // unset, empty string "" and set string "foo/bar". 4162 contentEncoding interface{} 4163 wantContentType string 4164 } 4165 4166 resps := []*resp{ 4167 { 4168 name: "gzip content-encoding, gzipped", // don't sniff. 4169 contentEncoding: "application/gzip", 4170 wantContentType: "", 4171 body: func() []byte { 4172 buf := new(bytes.Buffer) 4173 gzw := gzip.NewWriter(buf) 4174 gzw.Write([]byte("doctype html><p>Hello</p>")) 4175 gzw.Close() 4176 return buf.Bytes() 4177 }(), 4178 }, 4179 { 4180 name: "zlib content-encoding, zlibbed", // don't sniff. 4181 contentEncoding: "application/zlib", 4182 wantContentType: "", 4183 body: func() []byte { 4184 buf := new(bytes.Buffer) 4185 zw := zlib.NewWriter(buf) 4186 zw.Write([]byte("doctype html><p>Hello</p>")) 4187 zw.Close() 4188 return buf.Bytes() 4189 }(), 4190 }, 4191 { 4192 name: "no content-encoding", // must sniff. 4193 wantContentType: "application/x-gzip", 4194 body: func() []byte { 4195 buf := new(bytes.Buffer) 4196 gzw := gzip.NewWriter(buf) 4197 gzw.Write([]byte("doctype html><p>Hello</p>")) 4198 gzw.Close() 4199 return buf.Bytes() 4200 }(), 4201 }, 4202 { 4203 name: "phony content-encoding", // don't sniff. 4204 contentEncoding: "foo/bar", 4205 body: []byte("doctype html><p>Hello</p>"), 4206 }, 4207 { 4208 name: "empty but set content-encoding", 4209 contentEncoding: "", 4210 wantContentType: "audio/mpeg", 4211 body: []byte("ID3"), 4212 }, 4213 } 4214 4215 for _, tt := range resps { 4216 t.Run(tt.name, func(t *testing.T) { 4217 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 4218 if tt.contentEncoding != nil { 4219 w.Header().Set("Content-Encoding", tt.contentEncoding.(string)) 4220 } 4221 w.Write(tt.body) 4222 }, optOnlyServer) 4223 defer st.Close() 4224 4225 tr := &Transport{TLSClientConfig: tlsConfigInsecure} 4226 defer tr.CloseIdleConnections() 4227 4228 req, _ := http.NewRequest("GET", st.ts.URL, nil) 4229 res, err := tr.RoundTrip(req) 4230 if err != nil { 4231 t.Fatalf("GET %s: %v", st.ts.URL, err) 4232 } 4233 defer res.Body.Close() 4234 4235 g := res.Header.Get("Content-Encoding") 4236 t.Logf("%s: Content-Encoding: %s", st.ts.URL, g) 4237 4238 if w := tt.contentEncoding; g != w { 4239 if w != nil { // The case where contentEncoding was set explicitly. 4240 t.Errorf("Content-Encoding mismatch\n\tgot: %q\n\twant: %q", g, w) 4241 } else if g != "" { // "" should be the equivalent when the contentEncoding is unset. 4242 t.Errorf("Unexpected Content-Encoding %q", g) 4243 } 4244 } 4245 4246 g = res.Header.Get("Content-Type") 4247 if w := tt.wantContentType; g != w { 4248 t.Errorf("Content-Type mismatch\n\tgot: %q\n\twant: %q", g, w) 4249 } 4250 t.Logf("%s: Content-Type: %s", st.ts.URL, g) 4251 }) 4252 } 4253 } 4254 4255 func TestServerWindowUpdateOnBodyClose(t *testing.T) { 4256 const content = "12345678" 4257 blockCh := make(chan bool) 4258 errc := make(chan error, 1) 4259 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) { 4260 buf := make([]byte, 4) 4261 n, err := io.ReadFull(r.Body, buf) 4262 if err != nil { 4263 errc <- err 4264 return 4265 } 4266 if n != len(buf) { 4267 errc <- fmt.Errorf("too few bytes read: %d", n) 4268 return 4269 } 4270 blockCh <- true 4271 <-blockCh 4272 errc <- nil 4273 }) 4274 defer st.Close() 4275 4276 st.greet() 4277 st.writeHeaders(HeadersFrameParam{ 4278 StreamID: 1, // clients send odd numbers 4279 BlockFragment: st.encodeHeader( 4280 ":method", "POST", 4281 "content-length", strconv.Itoa(len(content)), 4282 ), 4283 EndStream: false, // to say DATA frames are coming 4284 EndHeaders: true, 4285 }) 4286 st.writeData(1, false, []byte(content[:5])) 4287 <-blockCh 4288 st.stream(1).body.CloseWithError(io.EOF) 4289 st.writeData(1, false, []byte(content[5:])) 4290 blockCh <- true 4291 4292 increments := len(content) 4293 for { 4294 f, err := st.readFrame() 4295 if err == io.EOF { 4296 break 4297 } 4298 if err != nil { 4299 t.Fatal(err) 4300 } 4301 if wu, ok := f.(*WindowUpdateFrame); ok && wu.StreamID == 0 { 4302 increments -= int(wu.Increment) 4303 if increments == 0 { 4304 break 4305 } 4306 } 4307 } 4308 4309 if err := <-errc; err != nil { 4310 t.Error(err) 4311 } 4312 } 4313 4314 func TestNoErrorLoggedOnPostAfterGOAWAY(t *testing.T) { 4315 st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {}) 4316 defer st.Close() 4317 4318 st.greet() 4319 4320 content := "some content" 4321 st.writeHeaders(HeadersFrameParam{ 4322 StreamID: 1, 4323 BlockFragment: st.encodeHeader( 4324 ":method", "POST", 4325 "content-length", strconv.Itoa(len(content)), 4326 ), 4327 EndStream: false, 4328 EndHeaders: true, 4329 }) 4330 st.wantHeaders() 4331 4332 st.sc.startGracefulShutdown() 4333 for { 4334 f, err := st.readFrame() 4335 if err == io.EOF { 4336 st.t.Fatal("got a EOF; want *GoAwayFrame") 4337 } 4338 if err != nil { 4339 t.Fatal(err) 4340 } 4341 if gf, ok := f.(*GoAwayFrame); ok && gf.StreamID == 0 { 4342 break 4343 } 4344 } 4345 4346 st.writeData(1, true, []byte(content)) 4347 time.Sleep(200 * time.Millisecond) 4348 st.Close() 4349 4350 if bytes.Contains(st.serverLogBuf.Bytes(), []byte("PROTOCOL_ERROR")) { 4351 t.Error("got protocol error") 4352 } 4353 }