gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/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 "gitee.com/ks-custle/core-gm/gmhttp"
    31  	"gitee.com/ks-custle/core-gm/gmhttp/httptest"
    32  	tls "gitee.com/ks-custle/core-gm/gmtls"
    33  	"gitee.com/ks-custle/core-gm/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  //
  2702  //	https://github.com/tatsuhiro-t/nghttp2/issues/140 &
  2703  //	http://sourceforge.net/p/curl/bugs/1472/
  2704  func TestServerWithCurl(t *testing.T)                     { testServerWithCurl(t, false) }
  2705  func TestServerWithCurl_LenientCipherSuites(t *testing.T) { testServerWithCurl(t, true) }
  2706  
  2707  func testServerWithCurl(t *testing.T, permitProhibitedCipherSuites bool) {
  2708  	if runtime.GOOS != "linux" {
  2709  		t.Skip("skipping Docker test when not on Linux; requires --net which won't work with boot2docker anyway")
  2710  	}
  2711  	if testing.Short() {
  2712  		t.Skip("skipping curl test in short mode")
  2713  	}
  2714  	requireCurl(t)
  2715  	var gotConn int32
  2716  	testHookOnConn = func() { atomic.StoreInt32(&gotConn, 1) }
  2717  
  2718  	const msg = "Hello from curl!\n"
  2719  	ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  2720  		w.Header().Set("Foo", "Bar")
  2721  		w.Header().Set("Client-Proto", r.Proto)
  2722  		io.WriteString(w, msg)
  2723  	}))
  2724  	ConfigureServer(ts.Config, &Server{
  2725  		PermitProhibitedCipherSuites: permitProhibitedCipherSuites,
  2726  	})
  2727  	ts.TLS = ts.Config.TLSConfig // the httptest.Server has its own copy of this TLS config
  2728  	ts.StartTLS()
  2729  	defer ts.Close()
  2730  
  2731  	t.Logf("Running test server for curl to hit at: %s", ts.URL)
  2732  	container := curl(t, "--silent", "--http2", "--insecure", "-v", ts.URL)
  2733  	defer kill(container)
  2734  	resc := make(chan interface{}, 1)
  2735  	go func() {
  2736  		res, err := dockerLogs(container)
  2737  		if err != nil {
  2738  			resc <- err
  2739  		} else {
  2740  			resc <- res
  2741  		}
  2742  	}()
  2743  	select {
  2744  	case res := <-resc:
  2745  		if err, ok := res.(error); ok {
  2746  			t.Fatal(err)
  2747  		}
  2748  		body := string(res.([]byte))
  2749  		// Search for both "key: value" and "key:value", since curl changed their format
  2750  		// Our Dockerfile contains the latest version (no space), but just in case people
  2751  		// didn't rebuild, check both.
  2752  		if !strings.Contains(body, "foo: Bar") && !strings.Contains(body, "foo:Bar") {
  2753  			t.Errorf("didn't see foo: Bar header")
  2754  			t.Logf("Got: %s", body)
  2755  		}
  2756  		if !strings.Contains(body, "client-proto: HTTP/2") && !strings.Contains(body, "client-proto:HTTP/2") {
  2757  			t.Errorf("didn't see client-proto: HTTP/2 header")
  2758  			t.Logf("Got: %s", res)
  2759  		}
  2760  		if !strings.Contains(string(res.([]byte)), msg) {
  2761  			t.Errorf("didn't see %q content", msg)
  2762  			t.Logf("Got: %s", res)
  2763  		}
  2764  	case <-time.After(3 * time.Second):
  2765  		t.Errorf("timeout waiting for curl")
  2766  	}
  2767  
  2768  	if atomic.LoadInt32(&gotConn) == 0 {
  2769  		t.Error("never saw an http2 connection")
  2770  	}
  2771  }
  2772  
  2773  var doh2load = flag.Bool("h2load", false, "Run h2load test")
  2774  
  2775  func TestServerWithH2Load(t *testing.T) {
  2776  	if !*doh2load {
  2777  		t.Skip("Skipping without --h2load flag.")
  2778  	}
  2779  	if runtime.GOOS != "linux" {
  2780  		t.Skip("skipping Docker test when not on Linux; requires --net which won't work with boot2docker anyway")
  2781  	}
  2782  	requireH2load(t)
  2783  
  2784  	msg := strings.Repeat("Hello, h2load!\n", 5000)
  2785  	ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  2786  		io.WriteString(w, msg)
  2787  		w.(http.Flusher).Flush()
  2788  		io.WriteString(w, msg)
  2789  	}))
  2790  	ts.StartTLS()
  2791  	defer ts.Close()
  2792  
  2793  	cmd := exec.Command("docker", "run", "--net=host", "--entrypoint=/usr/local/bin/h2load", "gohttp2/curl",
  2794  		"-n100000", "-c100", "-m100", ts.URL)
  2795  	cmd.Stdout = os.Stdout
  2796  	cmd.Stderr = os.Stderr
  2797  	if err := cmd.Run(); err != nil {
  2798  		t.Fatal(err)
  2799  	}
  2800  }
  2801  
  2802  // Issue 12843
  2803  func TestServerDoS_MaxHeaderListSize(t *testing.T) {
  2804  	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {})
  2805  	defer st.Close()
  2806  
  2807  	// shake hands
  2808  	frameSize := defaultMaxReadFrameSize
  2809  	var advHeaderListSize *uint32
  2810  	st.greetAndCheckSettings(func(s Setting) error {
  2811  		switch s.ID {
  2812  		case SettingMaxFrameSize:
  2813  			if s.Val < minMaxFrameSize {
  2814  				frameSize = minMaxFrameSize
  2815  			} else if s.Val > maxFrameSize {
  2816  				frameSize = maxFrameSize
  2817  			} else {
  2818  				frameSize = int(s.Val)
  2819  			}
  2820  		case SettingMaxHeaderListSize:
  2821  			advHeaderListSize = &s.Val
  2822  		}
  2823  		return nil
  2824  	})
  2825  
  2826  	if advHeaderListSize == nil {
  2827  		t.Errorf("server didn't advertise a max header list size")
  2828  	} else if *advHeaderListSize == 0 {
  2829  		t.Errorf("server advertised a max header list size of 0")
  2830  	}
  2831  
  2832  	st.encodeHeaderField(":method", "GET")
  2833  	st.encodeHeaderField(":path", "/")
  2834  	st.encodeHeaderField(":scheme", "https")
  2835  	cookie := strings.Repeat("*", 4058)
  2836  	st.encodeHeaderField("cookie", cookie)
  2837  	st.writeHeaders(HeadersFrameParam{
  2838  		StreamID:      1,
  2839  		BlockFragment: st.headerBuf.Bytes(),
  2840  		EndStream:     true,
  2841  		EndHeaders:    false,
  2842  	})
  2843  
  2844  	// Capture the short encoding of a duplicate ~4K cookie, now
  2845  	// that we've already sent it once.
  2846  	st.headerBuf.Reset()
  2847  	st.encodeHeaderField("cookie", cookie)
  2848  
  2849  	// Now send 1MB of it.
  2850  	const size = 1 << 20
  2851  	b := bytes.Repeat(st.headerBuf.Bytes(), size/st.headerBuf.Len())
  2852  	for len(b) > 0 {
  2853  		chunk := b
  2854  		if len(chunk) > frameSize {
  2855  			chunk = chunk[:frameSize]
  2856  		}
  2857  		b = b[len(chunk):]
  2858  		st.fr.WriteContinuation(1, len(b) == 0, chunk)
  2859  	}
  2860  
  2861  	h := st.wantHeaders()
  2862  	if !h.HeadersEnded() {
  2863  		t.Fatalf("Got HEADERS without END_HEADERS set: %v", h)
  2864  	}
  2865  	headers := st.decodeHeader(h.HeaderBlockFragment())
  2866  	want := [][2]string{
  2867  		{":status", "431"},
  2868  		{"content-type", "text/html; charset=utf-8"},
  2869  		{"content-length", "63"},
  2870  	}
  2871  	if !reflect.DeepEqual(headers, want) {
  2872  		t.Errorf("Headers mismatch.\n got: %q\nwant: %q\n", headers, want)
  2873  	}
  2874  }
  2875  
  2876  func TestServer_Response_Stream_With_Missing_Trailer(t *testing.T) {
  2877  	testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error {
  2878  		w.Header().Set("Trailer", "test-trailer")
  2879  		return nil
  2880  	}, func(st *serverTester) {
  2881  		getSlash(st)
  2882  		hf := st.wantHeaders()
  2883  		if !hf.HeadersEnded() {
  2884  			t.Fatal("want END_HEADERS flag")
  2885  		}
  2886  		df := st.wantData()
  2887  		if len(df.data) != 0 {
  2888  			t.Fatal("did not want data")
  2889  		}
  2890  		if !df.StreamEnded() {
  2891  			t.Fatal("want END_STREAM flag")
  2892  		}
  2893  	})
  2894  }
  2895  
  2896  func TestCompressionErrorOnWrite(t *testing.T) {
  2897  	const maxStrLen = 8 << 10
  2898  	var serverConfig *http.Server
  2899  	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  2900  		// No response body.
  2901  	}, func(ts *httptest.Server) {
  2902  		serverConfig = ts.Config
  2903  		serverConfig.MaxHeaderBytes = maxStrLen
  2904  	})
  2905  	st.addLogFilter("connection error: COMPRESSION_ERROR")
  2906  	defer st.Close()
  2907  	st.greet()
  2908  
  2909  	maxAllowed := st.sc.framer.maxHeaderStringLen()
  2910  
  2911  	// Crank this up, now that we have a conn connected with the
  2912  	// hpack.Decoder's max string length set has been initialized
  2913  	// from the earlier low ~8K value. We want this higher so don't
  2914  	// hit the max header list size. We only want to test hitting
  2915  	// the max string size.
  2916  	serverConfig.MaxHeaderBytes = 1 << 20
  2917  
  2918  	// First a request with a header that's exactly the max allowed size
  2919  	// for the hpack compression. It's still too long for the header list
  2920  	// size, so we'll get the 431 error, but that keeps the compression
  2921  	// context still valid.
  2922  	hbf := st.encodeHeader("foo", strings.Repeat("a", maxAllowed))
  2923  
  2924  	st.writeHeaders(HeadersFrameParam{
  2925  		StreamID:      1,
  2926  		BlockFragment: hbf,
  2927  		EndStream:     true,
  2928  		EndHeaders:    true,
  2929  	})
  2930  	h := st.wantHeaders()
  2931  	if !h.HeadersEnded() {
  2932  		t.Fatalf("Got HEADERS without END_HEADERS set: %v", h)
  2933  	}
  2934  	headers := st.decodeHeader(h.HeaderBlockFragment())
  2935  	want := [][2]string{
  2936  		{":status", "431"},
  2937  		{"content-type", "text/html; charset=utf-8"},
  2938  		{"content-length", "63"},
  2939  	}
  2940  	if !reflect.DeepEqual(headers, want) {
  2941  		t.Errorf("Headers mismatch.\n got: %q\nwant: %q\n", headers, want)
  2942  	}
  2943  	df := st.wantData()
  2944  	if !strings.Contains(string(df.Data()), "HTTP Error 431") {
  2945  		t.Errorf("Unexpected data body: %q", df.Data())
  2946  	}
  2947  	if !df.StreamEnded() {
  2948  		t.Fatalf("expect data stream end")
  2949  	}
  2950  
  2951  	// And now send one that's just one byte too big.
  2952  	hbf = st.encodeHeader("bar", strings.Repeat("b", maxAllowed+1))
  2953  	st.writeHeaders(HeadersFrameParam{
  2954  		StreamID:      3,
  2955  		BlockFragment: hbf,
  2956  		EndStream:     true,
  2957  		EndHeaders:    true,
  2958  	})
  2959  	ga := st.wantGoAway()
  2960  	if ga.ErrCode != ErrCodeCompression {
  2961  		t.Errorf("GOAWAY err = %v; want ErrCodeCompression", ga.ErrCode)
  2962  	}
  2963  }
  2964  
  2965  func TestCompressionErrorOnClose(t *testing.T) {
  2966  	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  2967  		// No response body.
  2968  	})
  2969  	st.addLogFilter("connection error: COMPRESSION_ERROR")
  2970  	defer st.Close()
  2971  	st.greet()
  2972  
  2973  	hbf := st.encodeHeader("foo", "bar")
  2974  	hbf = hbf[:len(hbf)-1] // truncate one byte from the end, so hpack.Decoder.Close fails.
  2975  	st.writeHeaders(HeadersFrameParam{
  2976  		StreamID:      1,
  2977  		BlockFragment: hbf,
  2978  		EndStream:     true,
  2979  		EndHeaders:    true,
  2980  	})
  2981  	ga := st.wantGoAway()
  2982  	if ga.ErrCode != ErrCodeCompression {
  2983  		t.Errorf("GOAWAY err = %v; want ErrCodeCompression", ga.ErrCode)
  2984  	}
  2985  }
  2986  
  2987  // test that a server handler can read trailers from a client
  2988  func TestServerReadsTrailers(t *testing.T) {
  2989  	const testBody = "some test body"
  2990  	writeReq := func(st *serverTester) {
  2991  		st.writeHeaders(HeadersFrameParam{
  2992  			StreamID:      1, // clients send odd numbers
  2993  			BlockFragment: st.encodeHeader("trailer", "Foo, Bar", "trailer", "Baz"),
  2994  			EndStream:     false,
  2995  			EndHeaders:    true,
  2996  		})
  2997  		st.writeData(1, false, []byte(testBody))
  2998  		st.writeHeaders(HeadersFrameParam{
  2999  			StreamID: 1, // clients send odd numbers
  3000  			BlockFragment: st.encodeHeaderRaw(
  3001  				"foo", "foov",
  3002  				"bar", "barv",
  3003  				"baz", "bazv",
  3004  				"surprise", "wasn't declared; shouldn't show up",
  3005  			),
  3006  			EndStream:  true,
  3007  			EndHeaders: true,
  3008  		})
  3009  	}
  3010  	checkReq := func(r *http.Request) {
  3011  		wantTrailer := http.Header{
  3012  			"Foo": nil,
  3013  			"Bar": nil,
  3014  			"Baz": nil,
  3015  		}
  3016  		if !reflect.DeepEqual(r.Trailer, wantTrailer) {
  3017  			t.Errorf("initial Trailer = %v; want %v", r.Trailer, wantTrailer)
  3018  		}
  3019  		slurp, err := ioutil.ReadAll(r.Body)
  3020  		if string(slurp) != testBody {
  3021  			t.Errorf("read body %q; want %q", slurp, testBody)
  3022  		}
  3023  		if err != nil {
  3024  			t.Fatalf("Body slurp: %v", err)
  3025  		}
  3026  		wantTrailerAfter := http.Header{
  3027  			"Foo": {"foov"},
  3028  			"Bar": {"barv"},
  3029  			"Baz": {"bazv"},
  3030  		}
  3031  		if !reflect.DeepEqual(r.Trailer, wantTrailerAfter) {
  3032  			t.Errorf("final Trailer = %v; want %v", r.Trailer, wantTrailerAfter)
  3033  		}
  3034  	}
  3035  	testServerRequest(t, writeReq, checkReq)
  3036  }
  3037  
  3038  // test that a server handler can send trailers
  3039  func TestServerWritesTrailers_WithFlush(t *testing.T)    { testServerWritesTrailers(t, true) }
  3040  func TestServerWritesTrailers_WithoutFlush(t *testing.T) { testServerWritesTrailers(t, false) }
  3041  
  3042  func testServerWritesTrailers(t *testing.T, withFlush bool) {
  3043  	// See https://httpwg.github.io/specs/rfc7540.html#rfc.section.8.1.3
  3044  	testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error {
  3045  		w.Header().Set("Trailer", "Server-Trailer-A, Server-Trailer-B")
  3046  		w.Header().Add("Trailer", "Server-Trailer-C")
  3047  		w.Header().Add("Trailer", "Transfer-Encoding, Content-Length, Trailer") // filtered
  3048  
  3049  		// Regular headers:
  3050  		w.Header().Set("Foo", "Bar")
  3051  		w.Header().Set("Content-Length", "5") // len("Hello")
  3052  
  3053  		io.WriteString(w, "Hello")
  3054  		if withFlush {
  3055  			w.(http.Flusher).Flush()
  3056  		}
  3057  		w.Header().Set("Server-Trailer-A", "valuea")
  3058  		w.Header().Set("Server-Trailer-C", "valuec") // skipping B
  3059  		// After a flush, random keys like Server-Surprise shouldn't show up:
  3060  		w.Header().Set("Server-Surpise", "surprise! this isn't predeclared!")
  3061  		// But we do permit promoting keys to trailers after a
  3062  		// flush if they start with the magic
  3063  		// otherwise-invalid "Trailer:" prefix:
  3064  		w.Header().Set("Trailer:Post-Header-Trailer", "hi1")
  3065  		w.Header().Set("Trailer:post-header-trailer2", "hi2")
  3066  		w.Header().Set("Trailer:Range", "invalid")
  3067  		w.Header().Set("Trailer:Foo\x01Bogus", "invalid")
  3068  		w.Header().Set("Transfer-Encoding", "should not be included; Forbidden by RFC 7230 4.1.2")
  3069  		w.Header().Set("Content-Length", "should not be included; Forbidden by RFC 7230 4.1.2")
  3070  		w.Header().Set("Trailer", "should not be included; Forbidden by RFC 7230 4.1.2")
  3071  		return nil
  3072  	}, func(st *serverTester) {
  3073  		getSlash(st)
  3074  		hf := st.wantHeaders()
  3075  		if hf.StreamEnded() {
  3076  			t.Fatal("response HEADERS had END_STREAM")
  3077  		}
  3078  		if !hf.HeadersEnded() {
  3079  			t.Fatal("response HEADERS didn't have END_HEADERS")
  3080  		}
  3081  		goth := st.decodeHeader(hf.HeaderBlockFragment())
  3082  		wanth := [][2]string{
  3083  			{":status", "200"},
  3084  			{"foo", "Bar"},
  3085  			{"trailer", "Server-Trailer-A, Server-Trailer-B"},
  3086  			{"trailer", "Server-Trailer-C"},
  3087  			{"trailer", "Transfer-Encoding, Content-Length, Trailer"},
  3088  			{"content-type", "text/plain; charset=utf-8"},
  3089  			{"content-length", "5"},
  3090  		}
  3091  		if !reflect.DeepEqual(goth, wanth) {
  3092  			t.Errorf("Header mismatch.\n got: %v\nwant: %v", goth, wanth)
  3093  		}
  3094  		df := st.wantData()
  3095  		if string(df.Data()) != "Hello" {
  3096  			t.Fatalf("Client read %q; want Hello", df.Data())
  3097  		}
  3098  		if df.StreamEnded() {
  3099  			t.Fatalf("data frame had STREAM_ENDED")
  3100  		}
  3101  		tf := st.wantHeaders() // for the trailers
  3102  		if !tf.StreamEnded() {
  3103  			t.Fatalf("trailers HEADERS lacked END_STREAM")
  3104  		}
  3105  		if !tf.HeadersEnded() {
  3106  			t.Fatalf("trailers HEADERS lacked END_HEADERS")
  3107  		}
  3108  		wanth = [][2]string{
  3109  			{"post-header-trailer", "hi1"},
  3110  			{"post-header-trailer2", "hi2"},
  3111  			{"server-trailer-a", "valuea"},
  3112  			{"server-trailer-c", "valuec"},
  3113  		}
  3114  		goth = st.decodeHeader(tf.HeaderBlockFragment())
  3115  		if !reflect.DeepEqual(goth, wanth) {
  3116  			t.Errorf("Header mismatch.\n got: %v\nwant: %v", goth, wanth)
  3117  		}
  3118  	})
  3119  }
  3120  
  3121  // validate transmitted header field names & values
  3122  // golang.org/issue/14048
  3123  func TestServerDoesntWriteInvalidHeaders(t *testing.T) {
  3124  	testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error {
  3125  		w.Header().Add("OK1", "x")
  3126  		w.Header().Add("Bad:Colon", "x") // colon (non-token byte) in key
  3127  		w.Header().Add("Bad1\x00", "x")  // null in key
  3128  		w.Header().Add("Bad2", "x\x00y") // null in value
  3129  		return nil
  3130  	}, func(st *serverTester) {
  3131  		getSlash(st)
  3132  		hf := st.wantHeaders()
  3133  		if !hf.StreamEnded() {
  3134  			t.Error("response HEADERS lacked END_STREAM")
  3135  		}
  3136  		if !hf.HeadersEnded() {
  3137  			t.Fatal("response HEADERS didn't have END_HEADERS")
  3138  		}
  3139  		goth := st.decodeHeader(hf.HeaderBlockFragment())
  3140  		wanth := [][2]string{
  3141  			{":status", "200"},
  3142  			{"ok1", "x"},
  3143  			{"content-length", "0"},
  3144  		}
  3145  		if !reflect.DeepEqual(goth, wanth) {
  3146  			t.Errorf("Header mismatch.\n got: %v\nwant: %v", goth, wanth)
  3147  		}
  3148  	})
  3149  }
  3150  
  3151  func BenchmarkServerGets(b *testing.B) {
  3152  	defer disableGoroutineTracking()()
  3153  	b.ReportAllocs()
  3154  
  3155  	const msg = "Hello, world"
  3156  	st := newServerTester(b, func(w http.ResponseWriter, r *http.Request) {
  3157  		io.WriteString(w, msg)
  3158  	})
  3159  	defer st.Close()
  3160  	st.greet()
  3161  
  3162  	// Give the server quota to reply. (plus it has the 64KB)
  3163  	if err := st.fr.WriteWindowUpdate(0, uint32(b.N*len(msg))); err != nil {
  3164  		b.Fatal(err)
  3165  	}
  3166  
  3167  	for i := 0; i < b.N; i++ {
  3168  		id := 1 + uint32(i)*2
  3169  		st.writeHeaders(HeadersFrameParam{
  3170  			StreamID:      id,
  3171  			BlockFragment: st.encodeHeader(),
  3172  			EndStream:     true,
  3173  			EndHeaders:    true,
  3174  		})
  3175  		st.wantHeaders()
  3176  		df := st.wantData()
  3177  		if !df.StreamEnded() {
  3178  			b.Fatalf("DATA didn't have END_STREAM; got %v", df)
  3179  		}
  3180  	}
  3181  }
  3182  
  3183  func BenchmarkServerPosts(b *testing.B) {
  3184  	defer disableGoroutineTracking()()
  3185  	b.ReportAllocs()
  3186  
  3187  	const msg = "Hello, world"
  3188  	st := newServerTester(b, func(w http.ResponseWriter, r *http.Request) {
  3189  		// Consume the (empty) body from th peer before replying, otherwise
  3190  		// the server will sometimes (depending on scheduling) send the peer a
  3191  		// a RST_STREAM with the CANCEL error code.
  3192  		if n, err := io.Copy(ioutil.Discard, r.Body); n != 0 || err != nil {
  3193  			b.Errorf("Copy error; got %v, %v; want 0, nil", n, err)
  3194  		}
  3195  		io.WriteString(w, msg)
  3196  	})
  3197  	defer st.Close()
  3198  	st.greet()
  3199  
  3200  	// Give the server quota to reply. (plus it has the 64KB)
  3201  	if err := st.fr.WriteWindowUpdate(0, uint32(b.N*len(msg))); err != nil {
  3202  		b.Fatal(err)
  3203  	}
  3204  
  3205  	for i := 0; i < b.N; i++ {
  3206  		id := 1 + uint32(i)*2
  3207  		st.writeHeaders(HeadersFrameParam{
  3208  			StreamID:      id,
  3209  			BlockFragment: st.encodeHeader(":method", "POST"),
  3210  			EndStream:     false,
  3211  			EndHeaders:    true,
  3212  		})
  3213  		st.writeData(id, true, nil)
  3214  		st.wantHeaders()
  3215  		df := st.wantData()
  3216  		if !df.StreamEnded() {
  3217  			b.Fatalf("DATA didn't have END_STREAM; got %v", df)
  3218  		}
  3219  	}
  3220  }
  3221  
  3222  // Send a stream of messages from server to client in separate data frames.
  3223  // Brings up performance issues seen in long streams.
  3224  // Created to show problem in go issue #18502
  3225  func BenchmarkServerToClientStreamDefaultOptions(b *testing.B) {
  3226  	benchmarkServerToClientStream(b)
  3227  }
  3228  
  3229  // Justification for Change-Id: Iad93420ef6c3918f54249d867098f1dadfa324d8
  3230  // Expect to see memory/alloc reduction by opting in to Frame reuse with the Framer.
  3231  func BenchmarkServerToClientStreamReuseFrames(b *testing.B) {
  3232  	benchmarkServerToClientStream(b, optFramerReuseFrames)
  3233  }
  3234  
  3235  func benchmarkServerToClientStream(b *testing.B, newServerOpts ...interface{}) {
  3236  	defer disableGoroutineTracking()()
  3237  	b.ReportAllocs()
  3238  	const msgLen = 1
  3239  	// default window size
  3240  	const windowSize = 1<<16 - 1
  3241  
  3242  	// next message to send from the server and for the client to expect
  3243  	nextMsg := func(i int) []byte {
  3244  		msg := make([]byte, msgLen)
  3245  		msg[0] = byte(i)
  3246  		if len(msg) != msgLen {
  3247  			panic("invalid test setup msg length")
  3248  		}
  3249  		return msg
  3250  	}
  3251  
  3252  	st := newServerTester(b, func(w http.ResponseWriter, r *http.Request) {
  3253  		// Consume the (empty) body from th peer before replying, otherwise
  3254  		// the server will sometimes (depending on scheduling) send the peer a
  3255  		// a RST_STREAM with the CANCEL error code.
  3256  		if n, err := io.Copy(ioutil.Discard, r.Body); n != 0 || err != nil {
  3257  			b.Errorf("Copy error; got %v, %v; want 0, nil", n, err)
  3258  		}
  3259  		for i := 0; i < b.N; i += 1 {
  3260  			w.Write(nextMsg(i))
  3261  			w.(http.Flusher).Flush()
  3262  		}
  3263  	}, newServerOpts...)
  3264  	defer st.Close()
  3265  	st.greet()
  3266  
  3267  	const id = uint32(1)
  3268  
  3269  	st.writeHeaders(HeadersFrameParam{
  3270  		StreamID:      id,
  3271  		BlockFragment: st.encodeHeader(":method", "POST"),
  3272  		EndStream:     false,
  3273  		EndHeaders:    true,
  3274  	})
  3275  
  3276  	st.writeData(id, true, nil)
  3277  	st.wantHeaders()
  3278  
  3279  	var pendingWindowUpdate = uint32(0)
  3280  
  3281  	for i := 0; i < b.N; i += 1 {
  3282  		expected := nextMsg(i)
  3283  		df := st.wantData()
  3284  		if bytes.Compare(expected, df.data) != 0 {
  3285  			b.Fatalf("Bad message received; want %v; got %v", expected, df.data)
  3286  		}
  3287  		// try to send infrequent but large window updates so they don't overwhelm the test
  3288  		pendingWindowUpdate += uint32(len(df.data))
  3289  		if pendingWindowUpdate >= windowSize/2 {
  3290  			if err := st.fr.WriteWindowUpdate(0, pendingWindowUpdate); err != nil {
  3291  				b.Fatal(err)
  3292  			}
  3293  			if err := st.fr.WriteWindowUpdate(id, pendingWindowUpdate); err != nil {
  3294  				b.Fatal(err)
  3295  			}
  3296  			pendingWindowUpdate = 0
  3297  		}
  3298  	}
  3299  	df := st.wantData()
  3300  	if !df.StreamEnded() {
  3301  		b.Fatalf("DATA didn't have END_STREAM; got %v", df)
  3302  	}
  3303  }
  3304  
  3305  // go-fuzz bug, originally reported at https://github.com/bradfitz/http2/issues/53
  3306  // Verify we don't hang.
  3307  func TestIssue53(t *testing.T) {
  3308  	const data = "PRI * HTTP/2.0\r\n\r\nSM" +
  3309  		"\r\n\r\n\x00\x00\x00\x01\ainfinfin\ad"
  3310  	s := &http.Server{
  3311  		ErrorLog: log.New(io.MultiWriter(stderrv(), twriter{t: t}), "", log.LstdFlags),
  3312  		Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  3313  			w.Write([]byte("hello"))
  3314  		}),
  3315  	}
  3316  	s2 := &Server{
  3317  		MaxReadFrameSize:             1 << 16,
  3318  		PermitProhibitedCipherSuites: true,
  3319  	}
  3320  	c := &issue53Conn{[]byte(data), false, false}
  3321  	s2.ServeConn(c, &ServeConnOpts{BaseConfig: s})
  3322  	if !c.closed {
  3323  		t.Fatal("connection is not closed")
  3324  	}
  3325  }
  3326  
  3327  type issue53Conn struct {
  3328  	data    []byte
  3329  	closed  bool
  3330  	written bool
  3331  }
  3332  
  3333  func (c *issue53Conn) Read(b []byte) (n int, err error) {
  3334  	if len(c.data) == 0 {
  3335  		return 0, io.EOF
  3336  	}
  3337  	n = copy(b, c.data)
  3338  	c.data = c.data[n:]
  3339  	return
  3340  }
  3341  
  3342  func (c *issue53Conn) Write(b []byte) (n int, err error) {
  3343  	c.written = true
  3344  	return len(b), nil
  3345  }
  3346  
  3347  func (c *issue53Conn) Close() error {
  3348  	c.closed = true
  3349  	return nil
  3350  }
  3351  
  3352  func (c *issue53Conn) LocalAddr() net.Addr {
  3353  	return &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 49706}
  3354  }
  3355  func (c *issue53Conn) RemoteAddr() net.Addr {
  3356  	return &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 49706}
  3357  }
  3358  func (c *issue53Conn) SetDeadline(t time.Time) error      { return nil }
  3359  func (c *issue53Conn) SetReadDeadline(t time.Time) error  { return nil }
  3360  func (c *issue53Conn) SetWriteDeadline(t time.Time) error { return nil }
  3361  
  3362  // golang.org/issue/33839
  3363  func TestServeConnOptsNilReceiverBehavior(t *testing.T) {
  3364  	defer func() {
  3365  		if r := recover(); r != nil {
  3366  			t.Errorf("got a panic that should not happen: %v", r)
  3367  		}
  3368  	}()
  3369  
  3370  	var o *ServeConnOpts
  3371  	if o.context() == nil {
  3372  		t.Error("o.context should not return nil")
  3373  	}
  3374  	if o.baseConfig() == nil {
  3375  		t.Error("o.baseConfig should not return nil")
  3376  	}
  3377  	if o.handler() == nil {
  3378  		t.Error("o.handler should not return nil")
  3379  	}
  3380  }
  3381  
  3382  // golang.org/issue/12895
  3383  func TestConfigureServer(t *testing.T) {
  3384  	tests := []struct {
  3385  		name      string
  3386  		tlsConfig *tls.Config
  3387  		wantErr   string
  3388  	}{
  3389  		{
  3390  			name: "empty server",
  3391  		},
  3392  		{
  3393  			name:      "empty CipherSuites",
  3394  			tlsConfig: &tls.Config{},
  3395  		},
  3396  		{
  3397  			name: "bad CipherSuites but MinVersion TLS 1.3",
  3398  			tlsConfig: &tls.Config{
  3399  				MinVersion:   tls.VersionTLS13,
  3400  				CipherSuites: []uint16{tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
  3401  			},
  3402  		},
  3403  		{
  3404  			name: "just the required cipher suite",
  3405  			tlsConfig: &tls.Config{
  3406  				CipherSuites: []uint16{tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
  3407  			},
  3408  		},
  3409  		{
  3410  			name: "just the alternative required cipher suite",
  3411  			tlsConfig: &tls.Config{
  3412  				CipherSuites: []uint16{tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
  3413  			},
  3414  		},
  3415  		{
  3416  			name: "missing required cipher suite",
  3417  			tlsConfig: &tls.Config{
  3418  				CipherSuites: []uint16{tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
  3419  			},
  3420  			wantErr: "is missing an HTTP/2-required",
  3421  		},
  3422  		{
  3423  			name: "required after bad",
  3424  			tlsConfig: &tls.Config{
  3425  				CipherSuites: []uint16{tls.TLS_RSA_WITH_RC4_128_SHA, tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
  3426  			},
  3427  		},
  3428  		{
  3429  			name: "bad after required",
  3430  			tlsConfig: &tls.Config{
  3431  				CipherSuites: []uint16{tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, tls.TLS_RSA_WITH_RC4_128_SHA},
  3432  			},
  3433  		},
  3434  	}
  3435  	for _, tt := range tests {
  3436  		srv := &http.Server{TLSConfig: tt.tlsConfig}
  3437  		err := ConfigureServer(srv, nil)
  3438  		if (err != nil) != (tt.wantErr != "") {
  3439  			if tt.wantErr != "" {
  3440  				t.Errorf("%s: success, but want error", tt.name)
  3441  			} else {
  3442  				t.Errorf("%s: unexpected error: %v", tt.name, err)
  3443  			}
  3444  		}
  3445  		if err != nil && tt.wantErr != "" && !strings.Contains(err.Error(), tt.wantErr) {
  3446  			t.Errorf("%s: err = %v; want substring %q", tt.name, err, tt.wantErr)
  3447  		}
  3448  		if err == nil && !srv.TLSConfig.PreferServerCipherSuites {
  3449  			t.Errorf("%s: PreferServerCipherSuite is false; want true", tt.name)
  3450  		}
  3451  	}
  3452  }
  3453  
  3454  func TestServerRejectHeadWithBody(t *testing.T) {
  3455  	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  3456  		// No response body.
  3457  	})
  3458  	defer st.Close()
  3459  	st.greet()
  3460  	st.writeHeaders(HeadersFrameParam{
  3461  		StreamID:      1, // clients send odd numbers
  3462  		BlockFragment: st.encodeHeader(":method", "HEAD"),
  3463  		EndStream:     false, // what we're testing, a bogus HEAD request with body
  3464  		EndHeaders:    true,
  3465  	})
  3466  	st.wantRSTStream(1, ErrCodeProtocol)
  3467  }
  3468  
  3469  func TestServerNoAutoContentLengthOnHead(t *testing.T) {
  3470  	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  3471  		// No response body. (or smaller than one frame)
  3472  	})
  3473  	defer st.Close()
  3474  	st.greet()
  3475  	st.writeHeaders(HeadersFrameParam{
  3476  		StreamID:      1, // clients send odd numbers
  3477  		BlockFragment: st.encodeHeader(":method", "HEAD"),
  3478  		EndStream:     true,
  3479  		EndHeaders:    true,
  3480  	})
  3481  	h := st.wantHeaders()
  3482  	headers := st.decodeHeader(h.HeaderBlockFragment())
  3483  	want := [][2]string{
  3484  		{":status", "200"},
  3485  	}
  3486  	if !reflect.DeepEqual(headers, want) {
  3487  		t.Errorf("Headers mismatch.\n got: %q\nwant: %q\n", headers, want)
  3488  	}
  3489  }
  3490  
  3491  // golang.org/issue/13495
  3492  func TestServerNoDuplicateContentType(t *testing.T) {
  3493  	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  3494  		w.Header()["Content-Type"] = []string{""}
  3495  		fmt.Fprintf(w, "<html><head></head><body>hi</body></html>")
  3496  	})
  3497  	defer st.Close()
  3498  	st.greet()
  3499  	st.writeHeaders(HeadersFrameParam{
  3500  		StreamID:      1,
  3501  		BlockFragment: st.encodeHeader(),
  3502  		EndStream:     true,
  3503  		EndHeaders:    true,
  3504  	})
  3505  	h := st.wantHeaders()
  3506  	headers := st.decodeHeader(h.HeaderBlockFragment())
  3507  	want := [][2]string{
  3508  		{":status", "200"},
  3509  		{"content-type", ""},
  3510  		{"content-length", "41"},
  3511  	}
  3512  	if !reflect.DeepEqual(headers, want) {
  3513  		t.Errorf("Headers mismatch.\n got: %q\nwant: %q\n", headers, want)
  3514  	}
  3515  }
  3516  
  3517  func disableGoroutineTracking() (restore func()) {
  3518  	old := DebugGoroutines
  3519  	DebugGoroutines = false
  3520  	return func() { DebugGoroutines = old }
  3521  }
  3522  
  3523  func BenchmarkServer_GetRequest(b *testing.B) {
  3524  	defer disableGoroutineTracking()()
  3525  	b.ReportAllocs()
  3526  	const msg = "Hello, world."
  3527  	st := newServerTester(b, func(w http.ResponseWriter, r *http.Request) {
  3528  		n, err := io.Copy(ioutil.Discard, r.Body)
  3529  		if err != nil || n > 0 {
  3530  			b.Errorf("Read %d bytes, error %v; want 0 bytes.", n, err)
  3531  		}
  3532  		io.WriteString(w, msg)
  3533  	})
  3534  	defer st.Close()
  3535  
  3536  	st.greet()
  3537  	// Give the server quota to reply. (plus it has the 64KB)
  3538  	if err := st.fr.WriteWindowUpdate(0, uint32(b.N*len(msg))); err != nil {
  3539  		b.Fatal(err)
  3540  	}
  3541  	hbf := st.encodeHeader(":method", "GET")
  3542  	for i := 0; i < b.N; i++ {
  3543  		streamID := uint32(1 + 2*i)
  3544  		st.writeHeaders(HeadersFrameParam{
  3545  			StreamID:      streamID,
  3546  			BlockFragment: hbf,
  3547  			EndStream:     true,
  3548  			EndHeaders:    true,
  3549  		})
  3550  		st.wantHeaders()
  3551  		st.wantData()
  3552  	}
  3553  }
  3554  
  3555  func BenchmarkServer_PostRequest(b *testing.B) {
  3556  	defer disableGoroutineTracking()()
  3557  	b.ReportAllocs()
  3558  	const msg = "Hello, world."
  3559  	st := newServerTester(b, func(w http.ResponseWriter, r *http.Request) {
  3560  		n, err := io.Copy(ioutil.Discard, r.Body)
  3561  		if err != nil || n > 0 {
  3562  			b.Errorf("Read %d bytes, error %v; want 0 bytes.", n, err)
  3563  		}
  3564  		io.WriteString(w, msg)
  3565  	})
  3566  	defer st.Close()
  3567  	st.greet()
  3568  	// Give the server quota to reply. (plus it has the 64KB)
  3569  	if err := st.fr.WriteWindowUpdate(0, uint32(b.N*len(msg))); err != nil {
  3570  		b.Fatal(err)
  3571  	}
  3572  	hbf := st.encodeHeader(":method", "POST")
  3573  	for i := 0; i < b.N; i++ {
  3574  		streamID := uint32(1 + 2*i)
  3575  		st.writeHeaders(HeadersFrameParam{
  3576  			StreamID:      streamID,
  3577  			BlockFragment: hbf,
  3578  			EndStream:     false,
  3579  			EndHeaders:    true,
  3580  		})
  3581  		st.writeData(streamID, true, nil)
  3582  		st.wantHeaders()
  3583  		st.wantData()
  3584  	}
  3585  }
  3586  
  3587  type connStateConn struct {
  3588  	net.Conn
  3589  	cs tls.ConnectionState
  3590  }
  3591  
  3592  func (c connStateConn) ConnectionState() tls.ConnectionState { return c.cs }
  3593  
  3594  // golang.org/issue/12737 -- handle any net.Conn, not just
  3595  // *tls.Conn.
  3596  func TestServerHandleCustomConn(t *testing.T) {
  3597  	var s Server
  3598  	c1, c2 := net.Pipe()
  3599  	clientDone := make(chan struct{})
  3600  	handlerDone := make(chan struct{})
  3601  	var req *http.Request
  3602  	go func() {
  3603  		defer close(clientDone)
  3604  		defer c2.Close()
  3605  		fr := NewFramer(c2, c2)
  3606  		io.WriteString(c2, ClientPreface)
  3607  		fr.WriteSettings()
  3608  		fr.WriteSettingsAck()
  3609  		f, err := fr.ReadFrame()
  3610  		if err != nil {
  3611  			t.Error(err)
  3612  			return
  3613  		}
  3614  		if sf, ok := f.(*SettingsFrame); !ok || sf.IsAck() {
  3615  			t.Errorf("Got %v; want non-ACK SettingsFrame", summarizeFrame(f))
  3616  			return
  3617  		}
  3618  		f, err = fr.ReadFrame()
  3619  		if err != nil {
  3620  			t.Error(err)
  3621  			return
  3622  		}
  3623  		if sf, ok := f.(*SettingsFrame); !ok || !sf.IsAck() {
  3624  			t.Errorf("Got %v; want ACK SettingsFrame", summarizeFrame(f))
  3625  			return
  3626  		}
  3627  		var henc hpackEncoder
  3628  		fr.WriteHeaders(HeadersFrameParam{
  3629  			StreamID:      1,
  3630  			BlockFragment: henc.encodeHeaderRaw(t, ":method", "GET", ":path", "/", ":scheme", "https", ":authority", "foo.com"),
  3631  			EndStream:     true,
  3632  			EndHeaders:    true,
  3633  		})
  3634  		go io.Copy(ioutil.Discard, c2)
  3635  		<-handlerDone
  3636  	}()
  3637  	const testString = "my custom ConnectionState"
  3638  	fakeConnState := tls.ConnectionState{
  3639  		ServerName:  testString,
  3640  		Version:     tls.VersionTLS12,
  3641  		CipherSuite: cipher_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  3642  	}
  3643  	go s.ServeConn(connStateConn{c1, fakeConnState}, &ServeConnOpts{
  3644  		BaseConfig: &http.Server{
  3645  			Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  3646  				defer close(handlerDone)
  3647  				req = r
  3648  			}),
  3649  		}})
  3650  	select {
  3651  	case <-clientDone:
  3652  	case <-time.After(5 * time.Second):
  3653  		t.Fatal("timeout waiting for handler")
  3654  	}
  3655  	if req.TLS == nil {
  3656  		t.Fatalf("Request.TLS is nil. Got: %#v", req)
  3657  	}
  3658  	if req.TLS.ServerName != testString {
  3659  		t.Fatalf("Request.TLS = %+v; want ServerName of %q", req.TLS, testString)
  3660  	}
  3661  }
  3662  
  3663  // golang.org/issue/14214
  3664  func TestServer_Rejects_ConnHeaders(t *testing.T) {
  3665  	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  3666  		t.Error("should not get to Handler")
  3667  	})
  3668  	defer st.Close()
  3669  	st.greet()
  3670  	st.bodylessReq1("connection", "foo")
  3671  	hf := st.wantHeaders()
  3672  	goth := st.decodeHeader(hf.HeaderBlockFragment())
  3673  	wanth := [][2]string{
  3674  		{":status", "400"},
  3675  		{"content-type", "text/plain; charset=utf-8"},
  3676  		{"x-content-type-options", "nosniff"},
  3677  		{"content-length", "51"},
  3678  	}
  3679  	if !reflect.DeepEqual(goth, wanth) {
  3680  		t.Errorf("Got headers %v; want %v", goth, wanth)
  3681  	}
  3682  }
  3683  
  3684  type hpackEncoder struct {
  3685  	enc *hpack.Encoder
  3686  	buf bytes.Buffer
  3687  }
  3688  
  3689  func (he *hpackEncoder) encodeHeaderRaw(t *testing.T, headers ...string) []byte {
  3690  	if len(headers)%2 == 1 {
  3691  		panic("odd number of kv args")
  3692  	}
  3693  	he.buf.Reset()
  3694  	if he.enc == nil {
  3695  		he.enc = hpack.NewEncoder(&he.buf)
  3696  	}
  3697  	for len(headers) > 0 {
  3698  		k, v := headers[0], headers[1]
  3699  		err := he.enc.WriteField(hpack.HeaderField{Name: k, Value: v})
  3700  		if err != nil {
  3701  			t.Fatalf("HPACK encoding error for %q/%q: %v", k, v, err)
  3702  		}
  3703  		headers = headers[2:]
  3704  	}
  3705  	return he.buf.Bytes()
  3706  }
  3707  
  3708  func TestCheckValidHTTP2Request(t *testing.T) {
  3709  	tests := []struct {
  3710  		h    http.Header
  3711  		want error
  3712  	}{
  3713  		{
  3714  			h:    http.Header{"Te": {"trailers"}},
  3715  			want: nil,
  3716  		},
  3717  		{
  3718  			h:    http.Header{"Te": {"trailers", "bogus"}},
  3719  			want: errors.New(`request header "TE" may only be "trailers" in HTTP/2`),
  3720  		},
  3721  		{
  3722  			h:    http.Header{"Foo": {""}},
  3723  			want: nil,
  3724  		},
  3725  		{
  3726  			h:    http.Header{"Connection": {""}},
  3727  			want: errors.New(`request header "Connection" is not valid in HTTP/2`),
  3728  		},
  3729  		{
  3730  			h:    http.Header{"Proxy-Connection": {""}},
  3731  			want: errors.New(`request header "Proxy-Connection" is not valid in HTTP/2`),
  3732  		},
  3733  		{
  3734  			h:    http.Header{"Keep-Alive": {""}},
  3735  			want: errors.New(`request header "Keep-Alive" is not valid in HTTP/2`),
  3736  		},
  3737  		{
  3738  			h:    http.Header{"Upgrade": {""}},
  3739  			want: errors.New(`request header "Upgrade" is not valid in HTTP/2`),
  3740  		},
  3741  	}
  3742  	for i, tt := range tests {
  3743  		got := checkValidHTTP2RequestHeaders(tt.h)
  3744  		if !equalError(got, tt.want) {
  3745  			t.Errorf("%d. checkValidHTTP2Request = %v; want %v", i, got, tt.want)
  3746  		}
  3747  	}
  3748  }
  3749  
  3750  // golang.org/issue/14030
  3751  func TestExpect100ContinueAfterHandlerWrites(t *testing.T) {
  3752  	const msg = "Hello"
  3753  	const msg2 = "World"
  3754  
  3755  	doRead := make(chan bool, 1)
  3756  	defer close(doRead) // fallback cleanup
  3757  
  3758  	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  3759  		io.WriteString(w, msg)
  3760  		w.(http.Flusher).Flush()
  3761  
  3762  		// Do a read, which might force a 100-continue status to be sent.
  3763  		<-doRead
  3764  		r.Body.Read(make([]byte, 10))
  3765  
  3766  		io.WriteString(w, msg2)
  3767  
  3768  	}, optOnlyServer)
  3769  	defer st.Close()
  3770  
  3771  	tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  3772  	defer tr.CloseIdleConnections()
  3773  
  3774  	req, _ := http.NewRequest("POST", st.ts.URL, io.LimitReader(neverEnding('A'), 2<<20))
  3775  	req.Header.Set("Expect", "100-continue")
  3776  
  3777  	res, err := tr.RoundTrip(req)
  3778  	if err != nil {
  3779  		t.Fatal(err)
  3780  	}
  3781  	defer res.Body.Close()
  3782  
  3783  	buf := make([]byte, len(msg))
  3784  	if _, err := io.ReadFull(res.Body, buf); err != nil {
  3785  		t.Fatal(err)
  3786  	}
  3787  	if string(buf) != msg {
  3788  		t.Fatalf("msg = %q; want %q", buf, msg)
  3789  	}
  3790  
  3791  	doRead <- true
  3792  
  3793  	if _, err := io.ReadFull(res.Body, buf); err != nil {
  3794  		t.Fatal(err)
  3795  	}
  3796  	if string(buf) != msg2 {
  3797  		t.Fatalf("second msg = %q; want %q", buf, msg2)
  3798  	}
  3799  }
  3800  
  3801  type funcReader func([]byte) (n int, err error)
  3802  
  3803  func (f funcReader) Read(p []byte) (n int, err error) { return f(p) }
  3804  
  3805  // golang.org/issue/16481 -- return flow control when streams close with unread data.
  3806  // (The Server version of the bug. See also TestUnreadFlowControlReturned_Transport)
  3807  func TestUnreadFlowControlReturned_Server(t *testing.T) {
  3808  	for _, tt := range []struct {
  3809  		name  string
  3810  		reqFn func(r *http.Request)
  3811  	}{
  3812  		{
  3813  			"body-open",
  3814  			func(r *http.Request) {},
  3815  		},
  3816  		{
  3817  			"body-closed",
  3818  			func(r *http.Request) {
  3819  				r.Body.Close()
  3820  			},
  3821  		},
  3822  		{
  3823  			"read-1-byte-and-close",
  3824  			func(r *http.Request) {
  3825  				b := make([]byte, 1)
  3826  				r.Body.Read(b)
  3827  				r.Body.Close()
  3828  			},
  3829  		},
  3830  	} {
  3831  		t.Run(tt.name, func(t *testing.T) {
  3832  			unblock := make(chan bool, 1)
  3833  			defer close(unblock)
  3834  
  3835  			timeOut := time.NewTimer(5 * time.Second)
  3836  			defer timeOut.Stop()
  3837  			st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  3838  				// Don't read the 16KB request body. Wait until the client's
  3839  				// done sending it and then return. This should cause the Server
  3840  				// to then return those 16KB of flow control to the client.
  3841  				tt.reqFn(r)
  3842  				select {
  3843  				case <-unblock:
  3844  				case <-timeOut.C:
  3845  					t.Fatal(tt.name, "timedout")
  3846  				}
  3847  			}, optOnlyServer)
  3848  			defer st.Close()
  3849  
  3850  			tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  3851  			defer tr.CloseIdleConnections()
  3852  
  3853  			// This previously hung on the 4th iteration.
  3854  			iters := 100
  3855  			if testing.Short() {
  3856  				iters = 20
  3857  			}
  3858  			for i := 0; i < iters; i++ {
  3859  				body := io.MultiReader(
  3860  					io.LimitReader(neverEnding('A'), 16<<10),
  3861  					funcReader(func([]byte) (n int, err error) {
  3862  						unblock <- true
  3863  						return 0, io.EOF
  3864  					}),
  3865  				)
  3866  				req, _ := http.NewRequest("POST", st.ts.URL, body)
  3867  				res, err := tr.RoundTrip(req)
  3868  				if err != nil {
  3869  					t.Fatal(tt.name, err)
  3870  				}
  3871  				res.Body.Close()
  3872  			}
  3873  		})
  3874  	}
  3875  }
  3876  
  3877  func TestServerIdleTimeout(t *testing.T) {
  3878  	if testing.Short() {
  3879  		t.Skip("skipping in short mode")
  3880  	}
  3881  
  3882  	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  3883  	}, func(h2s *Server) {
  3884  		h2s.IdleTimeout = 500 * time.Millisecond
  3885  	})
  3886  	defer st.Close()
  3887  
  3888  	st.greet()
  3889  	ga := st.wantGoAway()
  3890  	if ga.ErrCode != ErrCodeNo {
  3891  		t.Errorf("GOAWAY error = %v; want ErrCodeNo", ga.ErrCode)
  3892  	}
  3893  }
  3894  
  3895  func TestServerIdleTimeout_AfterRequest(t *testing.T) {
  3896  	if testing.Short() {
  3897  		t.Skip("skipping in short mode")
  3898  	}
  3899  	const timeout = 250 * time.Millisecond
  3900  
  3901  	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  3902  		time.Sleep(timeout * 2)
  3903  	}, func(h2s *Server) {
  3904  		h2s.IdleTimeout = timeout
  3905  	})
  3906  	defer st.Close()
  3907  
  3908  	st.greet()
  3909  
  3910  	// Send a request which takes twice the timeout. Verifies the
  3911  	// idle timeout doesn't fire while we're in a request:
  3912  	st.bodylessReq1()
  3913  	st.wantHeaders()
  3914  
  3915  	// But the idle timeout should be rearmed after the request
  3916  	// is done:
  3917  	ga := st.wantGoAway()
  3918  	if ga.ErrCode != ErrCodeNo {
  3919  		t.Errorf("GOAWAY error = %v; want ErrCodeNo", ga.ErrCode)
  3920  	}
  3921  }
  3922  
  3923  // grpc-go closes the Request.Body currently with a Read.
  3924  // Verify that it doesn't race.
  3925  // See https://github.com/grpc/grpc-go/pull/938
  3926  func TestRequestBodyReadCloseRace(t *testing.T) {
  3927  	for i := 0; i < 100; i++ {
  3928  		body := &requestBody{
  3929  			pipe: &pipe{
  3930  				b: new(bytes.Buffer),
  3931  			},
  3932  		}
  3933  		body.pipe.CloseWithError(io.EOF)
  3934  
  3935  		done := make(chan bool, 1)
  3936  		buf := make([]byte, 10)
  3937  		go func() {
  3938  			time.Sleep(1 * time.Millisecond)
  3939  			body.Close()
  3940  			done <- true
  3941  		}()
  3942  		body.Read(buf)
  3943  		<-done
  3944  	}
  3945  }
  3946  
  3947  func TestIssue20704Race(t *testing.T) {
  3948  	if testing.Short() && os.Getenv("GO_BUILDER_NAME") == "" {
  3949  		t.Skip("skipping in short mode")
  3950  	}
  3951  	const (
  3952  		itemSize  = 1 << 10
  3953  		itemCount = 100
  3954  	)
  3955  
  3956  	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  3957  		for i := 0; i < itemCount; i++ {
  3958  			_, err := w.Write(make([]byte, itemSize))
  3959  			if err != nil {
  3960  				return
  3961  			}
  3962  		}
  3963  	}, optOnlyServer)
  3964  	defer st.Close()
  3965  
  3966  	tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  3967  	defer tr.CloseIdleConnections()
  3968  	cl := &http.Client{Transport: tr}
  3969  
  3970  	for i := 0; i < 1000; i++ {
  3971  		resp, err := cl.Get(st.ts.URL)
  3972  		if err != nil {
  3973  			t.Fatal(err)
  3974  		}
  3975  		// Force a RST stream to the server by closing without
  3976  		// reading the body:
  3977  		resp.Body.Close()
  3978  	}
  3979  }
  3980  
  3981  func TestServer_Rejects_TooSmall(t *testing.T) {
  3982  	testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error {
  3983  		ioutil.ReadAll(r.Body)
  3984  		return nil
  3985  	}, func(st *serverTester) {
  3986  		st.writeHeaders(HeadersFrameParam{
  3987  			StreamID: 1, // clients send odd numbers
  3988  			BlockFragment: st.encodeHeader(
  3989  				":method", "POST",
  3990  				"content-length", "4",
  3991  			),
  3992  			EndStream:  false, // to say DATA frames are coming
  3993  			EndHeaders: true,
  3994  		})
  3995  		st.writeData(1, true, []byte("12345"))
  3996  
  3997  		st.wantRSTStream(1, ErrCodeProtocol)
  3998  	})
  3999  }
  4000  
  4001  // Tests that a handler setting "Connection: close" results in a GOAWAY being sent,
  4002  // and the connection still completing.
  4003  func TestServerHandlerConnectionClose(t *testing.T) {
  4004  	unblockHandler := make(chan bool, 1)
  4005  	defer close(unblockHandler) // backup; in case of errors
  4006  	testServerResponse(t, func(w http.ResponseWriter, r *http.Request) error {
  4007  		w.Header().Set("Connection", "close")
  4008  		w.Header().Set("Foo", "bar")
  4009  		w.(http.Flusher).Flush()
  4010  		<-unblockHandler
  4011  		return nil
  4012  	}, func(st *serverTester) {
  4013  		st.writeHeaders(HeadersFrameParam{
  4014  			StreamID:      1,
  4015  			BlockFragment: st.encodeHeader(),
  4016  			EndStream:     true,
  4017  			EndHeaders:    true,
  4018  		})
  4019  		var sawGoAway bool
  4020  		var sawRes bool
  4021  		for {
  4022  			f, err := st.readFrame()
  4023  			if err == io.EOF {
  4024  				break
  4025  			}
  4026  			if err != nil {
  4027  				t.Fatal(err)
  4028  			}
  4029  			switch f := f.(type) {
  4030  			case *GoAwayFrame:
  4031  				sawGoAway = true
  4032  				unblockHandler <- true
  4033  				if f.LastStreamID != 1 || f.ErrCode != ErrCodeNo {
  4034  					t.Errorf("unexpected GOAWAY frame: %v", summarizeFrame(f))
  4035  				}
  4036  			case *HeadersFrame:
  4037  				goth := st.decodeHeader(f.HeaderBlockFragment())
  4038  				wanth := [][2]string{
  4039  					{":status", "200"},
  4040  					{"foo", "bar"},
  4041  				}
  4042  				if !reflect.DeepEqual(goth, wanth) {
  4043  					t.Errorf("got headers %v; want %v", goth, wanth)
  4044  				}
  4045  				sawRes = true
  4046  			case *DataFrame:
  4047  				if f.StreamID != 1 || !f.StreamEnded() || len(f.Data()) != 0 {
  4048  					t.Errorf("unexpected DATA frame: %v", summarizeFrame(f))
  4049  				}
  4050  			default:
  4051  				t.Logf("unexpected frame: %v", summarizeFrame(f))
  4052  			}
  4053  		}
  4054  		if !sawGoAway {
  4055  			t.Errorf("didn't see GOAWAY")
  4056  		}
  4057  		if !sawRes {
  4058  			t.Errorf("didn't see response")
  4059  		}
  4060  	})
  4061  }
  4062  
  4063  func TestServer_Headers_HalfCloseRemote(t *testing.T) {
  4064  	var st *serverTester
  4065  	writeData := make(chan bool)
  4066  	writeHeaders := make(chan bool)
  4067  	leaveHandler := make(chan bool)
  4068  	st = newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  4069  		if st.stream(1) == nil {
  4070  			t.Errorf("nil stream 1 in handler")
  4071  		}
  4072  		if got, want := st.streamState(1), stateOpen; got != want {
  4073  			t.Errorf("in handler, state is %v; want %v", got, want)
  4074  		}
  4075  		writeData <- true
  4076  		if n, err := r.Body.Read(make([]byte, 1)); n != 0 || err != io.EOF {
  4077  			t.Errorf("body read = %d, %v; want 0, EOF", n, err)
  4078  		}
  4079  		if got, want := st.streamState(1), stateHalfClosedRemote; got != want {
  4080  			t.Errorf("in handler, state is %v; want %v", got, want)
  4081  		}
  4082  		writeHeaders <- true
  4083  
  4084  		<-leaveHandler
  4085  	})
  4086  	st.greet()
  4087  
  4088  	st.writeHeaders(HeadersFrameParam{
  4089  		StreamID:      1,
  4090  		BlockFragment: st.encodeHeader(),
  4091  		EndStream:     false, // keep it open
  4092  		EndHeaders:    true,
  4093  	})
  4094  	<-writeData
  4095  	st.writeData(1, true, nil)
  4096  
  4097  	<-writeHeaders
  4098  
  4099  	st.writeHeaders(HeadersFrameParam{
  4100  		StreamID:      1,
  4101  		BlockFragment: st.encodeHeader(),
  4102  		EndStream:     false, // keep it open
  4103  		EndHeaders:    true,
  4104  	})
  4105  
  4106  	defer close(leaveHandler)
  4107  
  4108  	st.wantRSTStream(1, ErrCodeStreamClosed)
  4109  }
  4110  
  4111  func TestServerGracefulShutdown(t *testing.T) {
  4112  	var st *serverTester
  4113  	handlerDone := make(chan struct{})
  4114  	st = newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  4115  		defer close(handlerDone)
  4116  		go st.ts.Config.Shutdown(context.Background())
  4117  
  4118  		ga := st.wantGoAway()
  4119  		if ga.ErrCode != ErrCodeNo {
  4120  			t.Errorf("GOAWAY error = %v; want ErrCodeNo", ga.ErrCode)
  4121  		}
  4122  		if ga.LastStreamID != 1 {
  4123  			t.Errorf("GOAWAY LastStreamID = %v; want 1", ga.LastStreamID)
  4124  		}
  4125  
  4126  		w.Header().Set("x-foo", "bar")
  4127  	})
  4128  	defer st.Close()
  4129  
  4130  	st.greet()
  4131  	st.bodylessReq1()
  4132  
  4133  	select {
  4134  	case <-handlerDone:
  4135  	case <-time.After(5 * time.Second):
  4136  		t.Fatalf("server did not shutdown?")
  4137  	}
  4138  	hf := st.wantHeaders()
  4139  	goth := st.decodeHeader(hf.HeaderBlockFragment())
  4140  	wanth := [][2]string{
  4141  		{":status", "200"},
  4142  		{"x-foo", "bar"},
  4143  		{"content-length", "0"},
  4144  	}
  4145  	if !reflect.DeepEqual(goth, wanth) {
  4146  		t.Errorf("Got headers %v; want %v", goth, wanth)
  4147  	}
  4148  
  4149  	n, err := st.cc.Read([]byte{0})
  4150  	if n != 0 || err == nil {
  4151  		t.Errorf("Read = %v, %v; want 0, non-nil", n, err)
  4152  	}
  4153  }
  4154  
  4155  // Issue 31753: don't sniff when Content-Encoding is set
  4156  func TestContentEncodingNoSniffing(t *testing.T) {
  4157  	type resp struct {
  4158  		name string
  4159  		body []byte
  4160  		// setting Content-Encoding as an interface instead of a string
  4161  		// directly, so as to differentiate between 3 states:
  4162  		//    unset, empty string "" and set string "foo/bar".
  4163  		contentEncoding interface{}
  4164  		wantContentType string
  4165  	}
  4166  
  4167  	resps := []*resp{
  4168  		{
  4169  			name:            "gzip content-encoding, gzipped", // don't sniff.
  4170  			contentEncoding: "application/gzip",
  4171  			wantContentType: "",
  4172  			body: func() []byte {
  4173  				buf := new(bytes.Buffer)
  4174  				gzw := gzip.NewWriter(buf)
  4175  				gzw.Write([]byte("doctype html><p>Hello</p>"))
  4176  				gzw.Close()
  4177  				return buf.Bytes()
  4178  			}(),
  4179  		},
  4180  		{
  4181  			name:            "zlib content-encoding, zlibbed", // don't sniff.
  4182  			contentEncoding: "application/zlib",
  4183  			wantContentType: "",
  4184  			body: func() []byte {
  4185  				buf := new(bytes.Buffer)
  4186  				zw := zlib.NewWriter(buf)
  4187  				zw.Write([]byte("doctype html><p>Hello</p>"))
  4188  				zw.Close()
  4189  				return buf.Bytes()
  4190  			}(),
  4191  		},
  4192  		{
  4193  			name:            "no content-encoding", // must sniff.
  4194  			wantContentType: "application/x-gzip",
  4195  			body: func() []byte {
  4196  				buf := new(bytes.Buffer)
  4197  				gzw := gzip.NewWriter(buf)
  4198  				gzw.Write([]byte("doctype html><p>Hello</p>"))
  4199  				gzw.Close()
  4200  				return buf.Bytes()
  4201  			}(),
  4202  		},
  4203  		{
  4204  			name:            "phony content-encoding", // don't sniff.
  4205  			contentEncoding: "foo/bar",
  4206  			body:            []byte("doctype html><p>Hello</p>"),
  4207  		},
  4208  		{
  4209  			name:            "empty but set content-encoding",
  4210  			contentEncoding: "",
  4211  			wantContentType: "audio/mpeg",
  4212  			body:            []byte("ID3"),
  4213  		},
  4214  	}
  4215  
  4216  	for _, tt := range resps {
  4217  		t.Run(tt.name, func(t *testing.T) {
  4218  			st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  4219  				if tt.contentEncoding != nil {
  4220  					w.Header().Set("Content-Encoding", tt.contentEncoding.(string))
  4221  				}
  4222  				w.Write(tt.body)
  4223  			}, optOnlyServer)
  4224  			defer st.Close()
  4225  
  4226  			tr := &Transport{TLSClientConfig: tlsConfigInsecure}
  4227  			defer tr.CloseIdleConnections()
  4228  
  4229  			req, _ := http.NewRequest("GET", st.ts.URL, nil)
  4230  			res, err := tr.RoundTrip(req)
  4231  			if err != nil {
  4232  				t.Fatalf("GET %s: %v", st.ts.URL, err)
  4233  			}
  4234  			defer res.Body.Close()
  4235  
  4236  			g := res.Header.Get("Content-Encoding")
  4237  			t.Logf("%s: Content-Encoding: %s", st.ts.URL, g)
  4238  
  4239  			if w := tt.contentEncoding; g != w {
  4240  				if w != nil { // The case where contentEncoding was set explicitly.
  4241  					t.Errorf("Content-Encoding mismatch\n\tgot:  %q\n\twant: %q", g, w)
  4242  				} else if g != "" { // "" should be the equivalent when the contentEncoding is unset.
  4243  					t.Errorf("Unexpected Content-Encoding %q", g)
  4244  				}
  4245  			}
  4246  
  4247  			g = res.Header.Get("Content-Type")
  4248  			if w := tt.wantContentType; g != w {
  4249  				t.Errorf("Content-Type mismatch\n\tgot:  %q\n\twant: %q", g, w)
  4250  			}
  4251  			t.Logf("%s: Content-Type: %s", st.ts.URL, g)
  4252  		})
  4253  	}
  4254  }
  4255  
  4256  func TestServerWindowUpdateOnBodyClose(t *testing.T) {
  4257  	const content = "12345678"
  4258  	blockCh := make(chan bool)
  4259  	errc := make(chan error, 1)
  4260  	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  4261  		buf := make([]byte, 4)
  4262  		n, err := io.ReadFull(r.Body, buf)
  4263  		if err != nil {
  4264  			errc <- err
  4265  			return
  4266  		}
  4267  		if n != len(buf) {
  4268  			errc <- fmt.Errorf("too few bytes read: %d", n)
  4269  			return
  4270  		}
  4271  		blockCh <- true
  4272  		<-blockCh
  4273  		errc <- nil
  4274  	})
  4275  	defer st.Close()
  4276  
  4277  	st.greet()
  4278  	st.writeHeaders(HeadersFrameParam{
  4279  		StreamID: 1, // clients send odd numbers
  4280  		BlockFragment: st.encodeHeader(
  4281  			":method", "POST",
  4282  			"content-length", strconv.Itoa(len(content)),
  4283  		),
  4284  		EndStream:  false, // to say DATA frames are coming
  4285  		EndHeaders: true,
  4286  	})
  4287  	st.writeData(1, false, []byte(content[:5]))
  4288  	<-blockCh
  4289  	st.stream(1).body.CloseWithError(io.EOF)
  4290  	st.writeData(1, false, []byte(content[5:]))
  4291  	blockCh <- true
  4292  
  4293  	increments := len(content)
  4294  	for {
  4295  		f, err := st.readFrame()
  4296  		if err == io.EOF {
  4297  			break
  4298  		}
  4299  		if err != nil {
  4300  			t.Fatal(err)
  4301  		}
  4302  		if wu, ok := f.(*WindowUpdateFrame); ok && wu.StreamID == 0 {
  4303  			increments -= int(wu.Increment)
  4304  			if increments == 0 {
  4305  				break
  4306  			}
  4307  		}
  4308  	}
  4309  
  4310  	if err := <-errc; err != nil {
  4311  		t.Error(err)
  4312  	}
  4313  }
  4314  
  4315  func TestNoErrorLoggedOnPostAfterGOAWAY(t *testing.T) {
  4316  	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {})
  4317  	defer st.Close()
  4318  
  4319  	st.greet()
  4320  
  4321  	content := "some content"
  4322  	st.writeHeaders(HeadersFrameParam{
  4323  		StreamID: 1,
  4324  		BlockFragment: st.encodeHeader(
  4325  			":method", "POST",
  4326  			"content-length", strconv.Itoa(len(content)),
  4327  		),
  4328  		EndStream:  false,
  4329  		EndHeaders: true,
  4330  	})
  4331  	st.wantHeaders()
  4332  
  4333  	st.sc.startGracefulShutdown()
  4334  	for {
  4335  		f, err := st.readFrame()
  4336  		if err == io.EOF {
  4337  			st.t.Fatal("got a EOF; want *GoAwayFrame")
  4338  		}
  4339  		if err != nil {
  4340  			t.Fatal(err)
  4341  		}
  4342  		if gf, ok := f.(*GoAwayFrame); ok && gf.StreamID == 0 {
  4343  			break
  4344  		}
  4345  	}
  4346  
  4347  	st.writeData(1, true, []byte(content))
  4348  	time.Sleep(200 * time.Millisecond)
  4349  	st.Close()
  4350  
  4351  	if bytes.Contains(st.serverLogBuf.Bytes(), []byte("PROTOCOL_ERROR")) {
  4352  		t.Error("got protocol error")
  4353  	}
  4354  }