github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/src/net/http/h2_bundle.go (about)

     1  // Code generated by golang.org/x/tools/cmd/bundle.
     2  //go:generate bundle -o h2_bundle.go -prefix http2 -underscore golang.org/x/net/http2
     3  
     4  // Package http2 implements the HTTP/2 protocol.
     5  //
     6  // This package is low-level and intended to be used directly by very
     7  // few people. Most users will use it indirectly through the automatic
     8  // use by the net/http package (from Go 1.6 and later).
     9  // For use in earlier Go versions see ConfigureServer. (Transport support
    10  // requires Go 1.6 or later)
    11  //
    12  // See https://http2.github.io/ for more information on HTTP/2.
    13  //
    14  // See https://http2.golang.org/ for a test server running this code.
    15  //
    16  
    17  package http
    18  
    19  import (
    20  	"bufio"
    21  	"bytes"
    22  	"compress/gzip"
    23  	"context"
    24  	"crypto/rand"
    25  	"crypto/tls"
    26  	"encoding/binary"
    27  	"errors"
    28  	"fmt"
    29  	"io"
    30  	"io/ioutil"
    31  	"log"
    32  	"math"
    33  	"net"
    34  	"net/http/httptrace"
    35  	"net/textproto"
    36  	"net/url"
    37  	"os"
    38  	"reflect"
    39  	"runtime"
    40  	"sort"
    41  	"strconv"
    42  	"strings"
    43  	"sync"
    44  	"time"
    45  
    46  	"golang_org/x/net/http2/hpack"
    47  	"golang_org/x/net/idna"
    48  	"golang_org/x/net/lex/httplex"
    49  )
    50  
    51  // ClientConnPool manages a pool of HTTP/2 client connections.
    52  type http2ClientConnPool interface {
    53  	GetClientConn(req *Request, addr string) (*http2ClientConn, error)
    54  	MarkDead(*http2ClientConn)
    55  }
    56  
    57  // clientConnPoolIdleCloser is the interface implemented by ClientConnPool
    58  // implementations which can close their idle connections.
    59  type http2clientConnPoolIdleCloser interface {
    60  	http2ClientConnPool
    61  	closeIdleConnections()
    62  }
    63  
    64  var (
    65  	_ http2clientConnPoolIdleCloser = (*http2clientConnPool)(nil)
    66  	_ http2clientConnPoolIdleCloser = http2noDialClientConnPool{}
    67  )
    68  
    69  // TODO: use singleflight for dialing and addConnCalls?
    70  type http2clientConnPool struct {
    71  	t *http2Transport
    72  
    73  	mu sync.Mutex // TODO: maybe switch to RWMutex
    74  	// TODO: add support for sharing conns based on cert names
    75  	// (e.g. share conn for googleapis.com and appspot.com)
    76  	conns        map[string][]*http2ClientConn // key is host:port
    77  	dialing      map[string]*http2dialCall     // currently in-flight dials
    78  	keys         map[*http2ClientConn][]string
    79  	addConnCalls map[string]*http2addConnCall // in-flight addConnIfNeede calls
    80  }
    81  
    82  func (p *http2clientConnPool) GetClientConn(req *Request, addr string) (*http2ClientConn, error) {
    83  	return p.getClientConn(req, addr, http2dialOnMiss)
    84  }
    85  
    86  const (
    87  	http2dialOnMiss   = true
    88  	http2noDialOnMiss = false
    89  )
    90  
    91  func (p *http2clientConnPool) getClientConn(req *Request, addr string, dialOnMiss bool) (*http2ClientConn, error) {
    92  	if http2isConnectionCloseRequest(req) && dialOnMiss {
    93  		// It gets its own connection.
    94  		const singleUse = true
    95  		cc, err := p.t.dialClientConn(addr, singleUse)
    96  		if err != nil {
    97  			return nil, err
    98  		}
    99  		return cc, nil
   100  	}
   101  	p.mu.Lock()
   102  	for _, cc := range p.conns[addr] {
   103  		if cc.CanTakeNewRequest() {
   104  			p.mu.Unlock()
   105  			return cc, nil
   106  		}
   107  	}
   108  	if !dialOnMiss {
   109  		p.mu.Unlock()
   110  		return nil, http2ErrNoCachedConn
   111  	}
   112  	call := p.getStartDialLocked(addr)
   113  	p.mu.Unlock()
   114  	<-call.done
   115  	return call.res, call.err
   116  }
   117  
   118  // dialCall is an in-flight Transport dial call to a host.
   119  type http2dialCall struct {
   120  	p    *http2clientConnPool
   121  	done chan struct{}    // closed when done
   122  	res  *http2ClientConn // valid after done is closed
   123  	err  error            // valid after done is closed
   124  }
   125  
   126  // requires p.mu is held.
   127  func (p *http2clientConnPool) getStartDialLocked(addr string) *http2dialCall {
   128  	if call, ok := p.dialing[addr]; ok {
   129  
   130  		return call
   131  	}
   132  	call := &http2dialCall{p: p, done: make(chan struct{})}
   133  	if p.dialing == nil {
   134  		p.dialing = make(map[string]*http2dialCall)
   135  	}
   136  	p.dialing[addr] = call
   137  	go call.dial(addr)
   138  	return call
   139  }
   140  
   141  // run in its own goroutine.
   142  func (c *http2dialCall) dial(addr string) {
   143  	const singleUse = false // shared conn
   144  	c.res, c.err = c.p.t.dialClientConn(addr, singleUse)
   145  	close(c.done)
   146  
   147  	c.p.mu.Lock()
   148  	delete(c.p.dialing, addr)
   149  	if c.err == nil {
   150  		c.p.addConnLocked(addr, c.res)
   151  	}
   152  	c.p.mu.Unlock()
   153  }
   154  
   155  // addConnIfNeeded makes a NewClientConn out of c if a connection for key doesn't
   156  // already exist. It coalesces concurrent calls with the same key.
   157  // This is used by the http1 Transport code when it creates a new connection. Because
   158  // the http1 Transport doesn't de-dup TCP dials to outbound hosts (because it doesn't know
   159  // the protocol), it can get into a situation where it has multiple TLS connections.
   160  // This code decides which ones live or die.
   161  // The return value used is whether c was used.
   162  // c is never closed.
   163  func (p *http2clientConnPool) addConnIfNeeded(key string, t *http2Transport, c *tls.Conn) (used bool, err error) {
   164  	p.mu.Lock()
   165  	for _, cc := range p.conns[key] {
   166  		if cc.CanTakeNewRequest() {
   167  			p.mu.Unlock()
   168  			return false, nil
   169  		}
   170  	}
   171  	call, dup := p.addConnCalls[key]
   172  	if !dup {
   173  		if p.addConnCalls == nil {
   174  			p.addConnCalls = make(map[string]*http2addConnCall)
   175  		}
   176  		call = &http2addConnCall{
   177  			p:    p,
   178  			done: make(chan struct{}),
   179  		}
   180  		p.addConnCalls[key] = call
   181  		go call.run(t, key, c)
   182  	}
   183  	p.mu.Unlock()
   184  
   185  	<-call.done
   186  	if call.err != nil {
   187  		return false, call.err
   188  	}
   189  	return !dup, nil
   190  }
   191  
   192  type http2addConnCall struct {
   193  	p    *http2clientConnPool
   194  	done chan struct{} // closed when done
   195  	err  error
   196  }
   197  
   198  func (c *http2addConnCall) run(t *http2Transport, key string, tc *tls.Conn) {
   199  	cc, err := t.NewClientConn(tc)
   200  
   201  	p := c.p
   202  	p.mu.Lock()
   203  	if err != nil {
   204  		c.err = err
   205  	} else {
   206  		p.addConnLocked(key, cc)
   207  	}
   208  	delete(p.addConnCalls, key)
   209  	p.mu.Unlock()
   210  	close(c.done)
   211  }
   212  
   213  func (p *http2clientConnPool) addConn(key string, cc *http2ClientConn) {
   214  	p.mu.Lock()
   215  	p.addConnLocked(key, cc)
   216  	p.mu.Unlock()
   217  }
   218  
   219  // p.mu must be held
   220  func (p *http2clientConnPool) addConnLocked(key string, cc *http2ClientConn) {
   221  	for _, v := range p.conns[key] {
   222  		if v == cc {
   223  			return
   224  		}
   225  	}
   226  	if p.conns == nil {
   227  		p.conns = make(map[string][]*http2ClientConn)
   228  	}
   229  	if p.keys == nil {
   230  		p.keys = make(map[*http2ClientConn][]string)
   231  	}
   232  	p.conns[key] = append(p.conns[key], cc)
   233  	p.keys[cc] = append(p.keys[cc], key)
   234  }
   235  
   236  func (p *http2clientConnPool) MarkDead(cc *http2ClientConn) {
   237  	p.mu.Lock()
   238  	defer p.mu.Unlock()
   239  	for _, key := range p.keys[cc] {
   240  		vv, ok := p.conns[key]
   241  		if !ok {
   242  			continue
   243  		}
   244  		newList := http2filterOutClientConn(vv, cc)
   245  		if len(newList) > 0 {
   246  			p.conns[key] = newList
   247  		} else {
   248  			delete(p.conns, key)
   249  		}
   250  	}
   251  	delete(p.keys, cc)
   252  }
   253  
   254  func (p *http2clientConnPool) closeIdleConnections() {
   255  	p.mu.Lock()
   256  	defer p.mu.Unlock()
   257  
   258  	for _, vv := range p.conns {
   259  		for _, cc := range vv {
   260  			cc.closeIfIdle()
   261  		}
   262  	}
   263  }
   264  
   265  func http2filterOutClientConn(in []*http2ClientConn, exclude *http2ClientConn) []*http2ClientConn {
   266  	out := in[:0]
   267  	for _, v := range in {
   268  		if v != exclude {
   269  			out = append(out, v)
   270  		}
   271  	}
   272  
   273  	if len(in) != len(out) {
   274  		in[len(in)-1] = nil
   275  	}
   276  	return out
   277  }
   278  
   279  // noDialClientConnPool is an implementation of http2.ClientConnPool
   280  // which never dials. We let the HTTP/1.1 client dial and use its TLS
   281  // connection instead.
   282  type http2noDialClientConnPool struct{ *http2clientConnPool }
   283  
   284  func (p http2noDialClientConnPool) GetClientConn(req *Request, addr string) (*http2ClientConn, error) {
   285  	return p.getClientConn(req, addr, http2noDialOnMiss)
   286  }
   287  
   288  func http2configureTransport(t1 *Transport) (*http2Transport, error) {
   289  	connPool := new(http2clientConnPool)
   290  	t2 := &http2Transport{
   291  		ConnPool: http2noDialClientConnPool{connPool},
   292  		t1:       t1,
   293  	}
   294  	connPool.t = t2
   295  	if err := http2registerHTTPSProtocol(t1, http2noDialH2RoundTripper{t2}); err != nil {
   296  		return nil, err
   297  	}
   298  	if t1.TLSClientConfig == nil {
   299  		t1.TLSClientConfig = new(tls.Config)
   300  	}
   301  	if !http2strSliceContains(t1.TLSClientConfig.NextProtos, "h2") {
   302  		t1.TLSClientConfig.NextProtos = append([]string{"h2"}, t1.TLSClientConfig.NextProtos...)
   303  	}
   304  	if !http2strSliceContains(t1.TLSClientConfig.NextProtos, "http/1.1") {
   305  		t1.TLSClientConfig.NextProtos = append(t1.TLSClientConfig.NextProtos, "http/1.1")
   306  	}
   307  	upgradeFn := func(authority string, c *tls.Conn) RoundTripper {
   308  		addr := http2authorityAddr("https", authority)
   309  		if used, err := connPool.addConnIfNeeded(addr, t2, c); err != nil {
   310  			go c.Close()
   311  			return http2erringRoundTripper{err}
   312  		} else if !used {
   313  
   314  			go c.Close()
   315  		}
   316  		return t2
   317  	}
   318  	if m := t1.TLSNextProto; len(m) == 0 {
   319  		t1.TLSNextProto = map[string]func(string, *tls.Conn) RoundTripper{
   320  			"h2": upgradeFn,
   321  		}
   322  	} else {
   323  		m["h2"] = upgradeFn
   324  	}
   325  	return t2, nil
   326  }
   327  
   328  // registerHTTPSProtocol calls Transport.RegisterProtocol but
   329  // convering panics into errors.
   330  func http2registerHTTPSProtocol(t *Transport, rt RoundTripper) (err error) {
   331  	defer func() {
   332  		if e := recover(); e != nil {
   333  			err = fmt.Errorf("%v", e)
   334  		}
   335  	}()
   336  	t.RegisterProtocol("https", rt)
   337  	return nil
   338  }
   339  
   340  // noDialH2RoundTripper is a RoundTripper which only tries to complete the request
   341  // if there's already has a cached connection to the host.
   342  type http2noDialH2RoundTripper struct{ t *http2Transport }
   343  
   344  func (rt http2noDialH2RoundTripper) RoundTrip(req *Request) (*Response, error) {
   345  	res, err := rt.t.RoundTrip(req)
   346  	if err == http2ErrNoCachedConn {
   347  		return nil, ErrSkipAltProtocol
   348  	}
   349  	return res, err
   350  }
   351  
   352  // Buffer chunks are allocated from a pool to reduce pressure on GC.
   353  // The maximum wasted space per dataBuffer is 2x the largest size class,
   354  // which happens when the dataBuffer has multiple chunks and there is
   355  // one unread byte in both the first and last chunks. We use a few size
   356  // classes to minimize overheads for servers that typically receive very
   357  // small request bodies.
   358  //
   359  // TODO: Benchmark to determine if the pools are necessary. The GC may have
   360  // improved enough that we can instead allocate chunks like this:
   361  // make([]byte, max(16<<10, expectedBytesRemaining))
   362  var (
   363  	http2dataChunkSizeClasses = []int{
   364  		1 << 10,
   365  		2 << 10,
   366  		4 << 10,
   367  		8 << 10,
   368  		16 << 10,
   369  	}
   370  	http2dataChunkPools = [...]sync.Pool{
   371  		{New: func() interface{} { return make([]byte, 1<<10) }},
   372  		{New: func() interface{} { return make([]byte, 2<<10) }},
   373  		{New: func() interface{} { return make([]byte, 4<<10) }},
   374  		{New: func() interface{} { return make([]byte, 8<<10) }},
   375  		{New: func() interface{} { return make([]byte, 16<<10) }},
   376  	}
   377  )
   378  
   379  func http2getDataBufferChunk(size int64) []byte {
   380  	i := 0
   381  	for ; i < len(http2dataChunkSizeClasses)-1; i++ {
   382  		if size <= int64(http2dataChunkSizeClasses[i]) {
   383  			break
   384  		}
   385  	}
   386  	return http2dataChunkPools[i].Get().([]byte)
   387  }
   388  
   389  func http2putDataBufferChunk(p []byte) {
   390  	for i, n := range http2dataChunkSizeClasses {
   391  		if len(p) == n {
   392  			http2dataChunkPools[i].Put(p)
   393  			return
   394  		}
   395  	}
   396  	panic(fmt.Sprintf("unexpected buffer len=%v", len(p)))
   397  }
   398  
   399  // dataBuffer is an io.ReadWriter backed by a list of data chunks.
   400  // Each dataBuffer is used to read DATA frames on a single stream.
   401  // The buffer is divided into chunks so the server can limit the
   402  // total memory used by a single connection without limiting the
   403  // request body size on any single stream.
   404  type http2dataBuffer struct {
   405  	chunks   [][]byte
   406  	r        int   // next byte to read is chunks[0][r]
   407  	w        int   // next byte to write is chunks[len(chunks)-1][w]
   408  	size     int   // total buffered bytes
   409  	expected int64 // we expect at least this many bytes in future Write calls (ignored if <= 0)
   410  }
   411  
   412  var http2errReadEmpty = errors.New("read from empty dataBuffer")
   413  
   414  // Read copies bytes from the buffer into p.
   415  // It is an error to read when no data is available.
   416  func (b *http2dataBuffer) Read(p []byte) (int, error) {
   417  	if b.size == 0 {
   418  		return 0, http2errReadEmpty
   419  	}
   420  	var ntotal int
   421  	for len(p) > 0 && b.size > 0 {
   422  		readFrom := b.bytesFromFirstChunk()
   423  		n := copy(p, readFrom)
   424  		p = p[n:]
   425  		ntotal += n
   426  		b.r += n
   427  		b.size -= n
   428  
   429  		if b.r == len(b.chunks[0]) {
   430  			http2putDataBufferChunk(b.chunks[0])
   431  			end := len(b.chunks) - 1
   432  			copy(b.chunks[:end], b.chunks[1:])
   433  			b.chunks[end] = nil
   434  			b.chunks = b.chunks[:end]
   435  			b.r = 0
   436  		}
   437  	}
   438  	return ntotal, nil
   439  }
   440  
   441  func (b *http2dataBuffer) bytesFromFirstChunk() []byte {
   442  	if len(b.chunks) == 1 {
   443  		return b.chunks[0][b.r:b.w]
   444  	}
   445  	return b.chunks[0][b.r:]
   446  }
   447  
   448  // Len returns the number of bytes of the unread portion of the buffer.
   449  func (b *http2dataBuffer) Len() int {
   450  	return b.size
   451  }
   452  
   453  // Write appends p to the buffer.
   454  func (b *http2dataBuffer) Write(p []byte) (int, error) {
   455  	ntotal := len(p)
   456  	for len(p) > 0 {
   457  
   458  		want := int64(len(p))
   459  		if b.expected > want {
   460  			want = b.expected
   461  		}
   462  		chunk := b.lastChunkOrAlloc(want)
   463  		n := copy(chunk[b.w:], p)
   464  		p = p[n:]
   465  		b.w += n
   466  		b.size += n
   467  		b.expected -= int64(n)
   468  	}
   469  	return ntotal, nil
   470  }
   471  
   472  func (b *http2dataBuffer) lastChunkOrAlloc(want int64) []byte {
   473  	if len(b.chunks) != 0 {
   474  		last := b.chunks[len(b.chunks)-1]
   475  		if b.w < len(last) {
   476  			return last
   477  		}
   478  	}
   479  	chunk := http2getDataBufferChunk(want)
   480  	b.chunks = append(b.chunks, chunk)
   481  	b.w = 0
   482  	return chunk
   483  }
   484  
   485  // An ErrCode is an unsigned 32-bit error code as defined in the HTTP/2 spec.
   486  type http2ErrCode uint32
   487  
   488  const (
   489  	http2ErrCodeNo                 http2ErrCode = 0x0
   490  	http2ErrCodeProtocol           http2ErrCode = 0x1
   491  	http2ErrCodeInternal           http2ErrCode = 0x2
   492  	http2ErrCodeFlowControl        http2ErrCode = 0x3
   493  	http2ErrCodeSettingsTimeout    http2ErrCode = 0x4
   494  	http2ErrCodeStreamClosed       http2ErrCode = 0x5
   495  	http2ErrCodeFrameSize          http2ErrCode = 0x6
   496  	http2ErrCodeRefusedStream      http2ErrCode = 0x7
   497  	http2ErrCodeCancel             http2ErrCode = 0x8
   498  	http2ErrCodeCompression        http2ErrCode = 0x9
   499  	http2ErrCodeConnect            http2ErrCode = 0xa
   500  	http2ErrCodeEnhanceYourCalm    http2ErrCode = 0xb
   501  	http2ErrCodeInadequateSecurity http2ErrCode = 0xc
   502  	http2ErrCodeHTTP11Required     http2ErrCode = 0xd
   503  )
   504  
   505  var http2errCodeName = map[http2ErrCode]string{
   506  	http2ErrCodeNo:                 "NO_ERROR",
   507  	http2ErrCodeProtocol:           "PROTOCOL_ERROR",
   508  	http2ErrCodeInternal:           "INTERNAL_ERROR",
   509  	http2ErrCodeFlowControl:        "FLOW_CONTROL_ERROR",
   510  	http2ErrCodeSettingsTimeout:    "SETTINGS_TIMEOUT",
   511  	http2ErrCodeStreamClosed:       "STREAM_CLOSED",
   512  	http2ErrCodeFrameSize:          "FRAME_SIZE_ERROR",
   513  	http2ErrCodeRefusedStream:      "REFUSED_STREAM",
   514  	http2ErrCodeCancel:             "CANCEL",
   515  	http2ErrCodeCompression:        "COMPRESSION_ERROR",
   516  	http2ErrCodeConnect:            "CONNECT_ERROR",
   517  	http2ErrCodeEnhanceYourCalm:    "ENHANCE_YOUR_CALM",
   518  	http2ErrCodeInadequateSecurity: "INADEQUATE_SECURITY",
   519  	http2ErrCodeHTTP11Required:     "HTTP_1_1_REQUIRED",
   520  }
   521  
   522  func (e http2ErrCode) String() string {
   523  	if s, ok := http2errCodeName[e]; ok {
   524  		return s
   525  	}
   526  	return fmt.Sprintf("unknown error code 0x%x", uint32(e))
   527  }
   528  
   529  // ConnectionError is an error that results in the termination of the
   530  // entire connection.
   531  type http2ConnectionError http2ErrCode
   532  
   533  func (e http2ConnectionError) Error() string {
   534  	return fmt.Sprintf("connection error: %s", http2ErrCode(e))
   535  }
   536  
   537  // StreamError is an error that only affects one stream within an
   538  // HTTP/2 connection.
   539  type http2StreamError struct {
   540  	StreamID uint32
   541  	Code     http2ErrCode
   542  	Cause    error // optional additional detail
   543  }
   544  
   545  func http2streamError(id uint32, code http2ErrCode) http2StreamError {
   546  	return http2StreamError{StreamID: id, Code: code}
   547  }
   548  
   549  func (e http2StreamError) Error() string {
   550  	if e.Cause != nil {
   551  		return fmt.Sprintf("stream error: stream ID %d; %v; %v", e.StreamID, e.Code, e.Cause)
   552  	}
   553  	return fmt.Sprintf("stream error: stream ID %d; %v", e.StreamID, e.Code)
   554  }
   555  
   556  // 6.9.1 The Flow Control Window
   557  // "If a sender receives a WINDOW_UPDATE that causes a flow control
   558  // window to exceed this maximum it MUST terminate either the stream
   559  // or the connection, as appropriate. For streams, [...]; for the
   560  // connection, a GOAWAY frame with a FLOW_CONTROL_ERROR code."
   561  type http2goAwayFlowError struct{}
   562  
   563  func (http2goAwayFlowError) Error() string { return "connection exceeded flow control window size" }
   564  
   565  // Errors of this type are only returned by the frame parser functions
   566  // and converted into ConnectionError(ErrCodeProtocol).
   567  type http2connError struct {
   568  	Code   http2ErrCode
   569  	Reason string
   570  }
   571  
   572  func (e http2connError) Error() string {
   573  	return fmt.Sprintf("http2: connection error: %v: %v", e.Code, e.Reason)
   574  }
   575  
   576  type http2pseudoHeaderError string
   577  
   578  func (e http2pseudoHeaderError) Error() string {
   579  	return fmt.Sprintf("invalid pseudo-header %q", string(e))
   580  }
   581  
   582  type http2duplicatePseudoHeaderError string
   583  
   584  func (e http2duplicatePseudoHeaderError) Error() string {
   585  	return fmt.Sprintf("duplicate pseudo-header %q", string(e))
   586  }
   587  
   588  type http2headerFieldNameError string
   589  
   590  func (e http2headerFieldNameError) Error() string {
   591  	return fmt.Sprintf("invalid header field name %q", string(e))
   592  }
   593  
   594  type http2headerFieldValueError string
   595  
   596  func (e http2headerFieldValueError) Error() string {
   597  	return fmt.Sprintf("invalid header field value %q", string(e))
   598  }
   599  
   600  var (
   601  	http2errMixPseudoHeaderTypes = errors.New("mix of request and response pseudo headers")
   602  	http2errPseudoAfterRegular   = errors.New("pseudo header field after regular")
   603  )
   604  
   605  // flow is the flow control window's size.
   606  type http2flow struct {
   607  	// n is the number of DATA bytes we're allowed to send.
   608  	// A flow is kept both on a conn and a per-stream.
   609  	n int32
   610  
   611  	// conn points to the shared connection-level flow that is
   612  	// shared by all streams on that conn. It is nil for the flow
   613  	// that's on the conn directly.
   614  	conn *http2flow
   615  }
   616  
   617  func (f *http2flow) setConnFlow(cf *http2flow) { f.conn = cf }
   618  
   619  func (f *http2flow) available() int32 {
   620  	n := f.n
   621  	if f.conn != nil && f.conn.n < n {
   622  		n = f.conn.n
   623  	}
   624  	return n
   625  }
   626  
   627  func (f *http2flow) take(n int32) {
   628  	if n > f.available() {
   629  		panic("internal error: took too much")
   630  	}
   631  	f.n -= n
   632  	if f.conn != nil {
   633  		f.conn.n -= n
   634  	}
   635  }
   636  
   637  // add adds n bytes (positive or negative) to the flow control window.
   638  // It returns false if the sum would exceed 2^31-1.
   639  func (f *http2flow) add(n int32) bool {
   640  	remain := (1<<31 - 1) - f.n
   641  	if n > remain {
   642  		return false
   643  	}
   644  	f.n += n
   645  	return true
   646  }
   647  
   648  const http2frameHeaderLen = 9
   649  
   650  var http2padZeros = make([]byte, 255) // zeros for padding
   651  
   652  // A FrameType is a registered frame type as defined in
   653  // http://http2.github.io/http2-spec/#rfc.section.11.2
   654  type http2FrameType uint8
   655  
   656  const (
   657  	http2FrameData         http2FrameType = 0x0
   658  	http2FrameHeaders      http2FrameType = 0x1
   659  	http2FramePriority     http2FrameType = 0x2
   660  	http2FrameRSTStream    http2FrameType = 0x3
   661  	http2FrameSettings     http2FrameType = 0x4
   662  	http2FramePushPromise  http2FrameType = 0x5
   663  	http2FramePing         http2FrameType = 0x6
   664  	http2FrameGoAway       http2FrameType = 0x7
   665  	http2FrameWindowUpdate http2FrameType = 0x8
   666  	http2FrameContinuation http2FrameType = 0x9
   667  )
   668  
   669  var http2frameName = map[http2FrameType]string{
   670  	http2FrameData:         "DATA",
   671  	http2FrameHeaders:      "HEADERS",
   672  	http2FramePriority:     "PRIORITY",
   673  	http2FrameRSTStream:    "RST_STREAM",
   674  	http2FrameSettings:     "SETTINGS",
   675  	http2FramePushPromise:  "PUSH_PROMISE",
   676  	http2FramePing:         "PING",
   677  	http2FrameGoAway:       "GOAWAY",
   678  	http2FrameWindowUpdate: "WINDOW_UPDATE",
   679  	http2FrameContinuation: "CONTINUATION",
   680  }
   681  
   682  func (t http2FrameType) String() string {
   683  	if s, ok := http2frameName[t]; ok {
   684  		return s
   685  	}
   686  	return fmt.Sprintf("UNKNOWN_FRAME_TYPE_%d", uint8(t))
   687  }
   688  
   689  // Flags is a bitmask of HTTP/2 flags.
   690  // The meaning of flags varies depending on the frame type.
   691  type http2Flags uint8
   692  
   693  // Has reports whether f contains all (0 or more) flags in v.
   694  func (f http2Flags) Has(v http2Flags) bool {
   695  	return (f & v) == v
   696  }
   697  
   698  // Frame-specific FrameHeader flag bits.
   699  const (
   700  	// Data Frame
   701  	http2FlagDataEndStream http2Flags = 0x1
   702  	http2FlagDataPadded    http2Flags = 0x8
   703  
   704  	// Headers Frame
   705  	http2FlagHeadersEndStream  http2Flags = 0x1
   706  	http2FlagHeadersEndHeaders http2Flags = 0x4
   707  	http2FlagHeadersPadded     http2Flags = 0x8
   708  	http2FlagHeadersPriority   http2Flags = 0x20
   709  
   710  	// Settings Frame
   711  	http2FlagSettingsAck http2Flags = 0x1
   712  
   713  	// Ping Frame
   714  	http2FlagPingAck http2Flags = 0x1
   715  
   716  	// Continuation Frame
   717  	http2FlagContinuationEndHeaders http2Flags = 0x4
   718  
   719  	http2FlagPushPromiseEndHeaders http2Flags = 0x4
   720  	http2FlagPushPromisePadded     http2Flags = 0x8
   721  )
   722  
   723  var http2flagName = map[http2FrameType]map[http2Flags]string{
   724  	http2FrameData: {
   725  		http2FlagDataEndStream: "END_STREAM",
   726  		http2FlagDataPadded:    "PADDED",
   727  	},
   728  	http2FrameHeaders: {
   729  		http2FlagHeadersEndStream:  "END_STREAM",
   730  		http2FlagHeadersEndHeaders: "END_HEADERS",
   731  		http2FlagHeadersPadded:     "PADDED",
   732  		http2FlagHeadersPriority:   "PRIORITY",
   733  	},
   734  	http2FrameSettings: {
   735  		http2FlagSettingsAck: "ACK",
   736  	},
   737  	http2FramePing: {
   738  		http2FlagPingAck: "ACK",
   739  	},
   740  	http2FrameContinuation: {
   741  		http2FlagContinuationEndHeaders: "END_HEADERS",
   742  	},
   743  	http2FramePushPromise: {
   744  		http2FlagPushPromiseEndHeaders: "END_HEADERS",
   745  		http2FlagPushPromisePadded:     "PADDED",
   746  	},
   747  }
   748  
   749  // a frameParser parses a frame given its FrameHeader and payload
   750  // bytes. The length of payload will always equal fh.Length (which
   751  // might be 0).
   752  type http2frameParser func(fc *http2frameCache, fh http2FrameHeader, payload []byte) (http2Frame, error)
   753  
   754  var http2frameParsers = map[http2FrameType]http2frameParser{
   755  	http2FrameData:         http2parseDataFrame,
   756  	http2FrameHeaders:      http2parseHeadersFrame,
   757  	http2FramePriority:     http2parsePriorityFrame,
   758  	http2FrameRSTStream:    http2parseRSTStreamFrame,
   759  	http2FrameSettings:     http2parseSettingsFrame,
   760  	http2FramePushPromise:  http2parsePushPromise,
   761  	http2FramePing:         http2parsePingFrame,
   762  	http2FrameGoAway:       http2parseGoAwayFrame,
   763  	http2FrameWindowUpdate: http2parseWindowUpdateFrame,
   764  	http2FrameContinuation: http2parseContinuationFrame,
   765  }
   766  
   767  func http2typeFrameParser(t http2FrameType) http2frameParser {
   768  	if f := http2frameParsers[t]; f != nil {
   769  		return f
   770  	}
   771  	return http2parseUnknownFrame
   772  }
   773  
   774  // A FrameHeader is the 9 byte header of all HTTP/2 frames.
   775  //
   776  // See http://http2.github.io/http2-spec/#FrameHeader
   777  type http2FrameHeader struct {
   778  	valid bool // caller can access []byte fields in the Frame
   779  
   780  	// Type is the 1 byte frame type. There are ten standard frame
   781  	// types, but extension frame types may be written by WriteRawFrame
   782  	// and will be returned by ReadFrame (as UnknownFrame).
   783  	Type http2FrameType
   784  
   785  	// Flags are the 1 byte of 8 potential bit flags per frame.
   786  	// They are specific to the frame type.
   787  	Flags http2Flags
   788  
   789  	// Length is the length of the frame, not including the 9 byte header.
   790  	// The maximum size is one byte less than 16MB (uint24), but only
   791  	// frames up to 16KB are allowed without peer agreement.
   792  	Length uint32
   793  
   794  	// StreamID is which stream this frame is for. Certain frames
   795  	// are not stream-specific, in which case this field is 0.
   796  	StreamID uint32
   797  }
   798  
   799  // Header returns h. It exists so FrameHeaders can be embedded in other
   800  // specific frame types and implement the Frame interface.
   801  func (h http2FrameHeader) Header() http2FrameHeader { return h }
   802  
   803  func (h http2FrameHeader) String() string {
   804  	var buf bytes.Buffer
   805  	buf.WriteString("[FrameHeader ")
   806  	h.writeDebug(&buf)
   807  	buf.WriteByte(']')
   808  	return buf.String()
   809  }
   810  
   811  func (h http2FrameHeader) writeDebug(buf *bytes.Buffer) {
   812  	buf.WriteString(h.Type.String())
   813  	if h.Flags != 0 {
   814  		buf.WriteString(" flags=")
   815  		set := 0
   816  		for i := uint8(0); i < 8; i++ {
   817  			if h.Flags&(1<<i) == 0 {
   818  				continue
   819  			}
   820  			set++
   821  			if set > 1 {
   822  				buf.WriteByte('|')
   823  			}
   824  			name := http2flagName[h.Type][http2Flags(1<<i)]
   825  			if name != "" {
   826  				buf.WriteString(name)
   827  			} else {
   828  				fmt.Fprintf(buf, "0x%x", 1<<i)
   829  			}
   830  		}
   831  	}
   832  	if h.StreamID != 0 {
   833  		fmt.Fprintf(buf, " stream=%d", h.StreamID)
   834  	}
   835  	fmt.Fprintf(buf, " len=%d", h.Length)
   836  }
   837  
   838  func (h *http2FrameHeader) checkValid() {
   839  	if !h.valid {
   840  		panic("Frame accessor called on non-owned Frame")
   841  	}
   842  }
   843  
   844  func (h *http2FrameHeader) invalidate() { h.valid = false }
   845  
   846  // frame header bytes.
   847  // Used only by ReadFrameHeader.
   848  var http2fhBytes = sync.Pool{
   849  	New: func() interface{} {
   850  		buf := make([]byte, http2frameHeaderLen)
   851  		return &buf
   852  	},
   853  }
   854  
   855  // ReadFrameHeader reads 9 bytes from r and returns a FrameHeader.
   856  // Most users should use Framer.ReadFrame instead.
   857  func http2ReadFrameHeader(r io.Reader) (http2FrameHeader, error) {
   858  	bufp := http2fhBytes.Get().(*[]byte)
   859  	defer http2fhBytes.Put(bufp)
   860  	return http2readFrameHeader(*bufp, r)
   861  }
   862  
   863  func http2readFrameHeader(buf []byte, r io.Reader) (http2FrameHeader, error) {
   864  	_, err := io.ReadFull(r, buf[:http2frameHeaderLen])
   865  	if err != nil {
   866  		return http2FrameHeader{}, err
   867  	}
   868  	return http2FrameHeader{
   869  		Length:   (uint32(buf[0])<<16 | uint32(buf[1])<<8 | uint32(buf[2])),
   870  		Type:     http2FrameType(buf[3]),
   871  		Flags:    http2Flags(buf[4]),
   872  		StreamID: binary.BigEndian.Uint32(buf[5:]) & (1<<31 - 1),
   873  		valid:    true,
   874  	}, nil
   875  }
   876  
   877  // A Frame is the base interface implemented by all frame types.
   878  // Callers will generally type-assert the specific frame type:
   879  // *HeadersFrame, *SettingsFrame, *WindowUpdateFrame, etc.
   880  //
   881  // Frames are only valid until the next call to Framer.ReadFrame.
   882  type http2Frame interface {
   883  	Header() http2FrameHeader
   884  
   885  	// invalidate is called by Framer.ReadFrame to make this
   886  	// frame's buffers as being invalid, since the subsequent
   887  	// frame will reuse them.
   888  	invalidate()
   889  }
   890  
   891  // A Framer reads and writes Frames.
   892  type http2Framer struct {
   893  	r         io.Reader
   894  	lastFrame http2Frame
   895  	errDetail error
   896  
   897  	// lastHeaderStream is non-zero if the last frame was an
   898  	// unfinished HEADERS/CONTINUATION.
   899  	lastHeaderStream uint32
   900  
   901  	maxReadSize uint32
   902  	headerBuf   [http2frameHeaderLen]byte
   903  
   904  	// TODO: let getReadBuf be configurable, and use a less memory-pinning
   905  	// allocator in server.go to minimize memory pinned for many idle conns.
   906  	// Will probably also need to make frame invalidation have a hook too.
   907  	getReadBuf func(size uint32) []byte
   908  	readBuf    []byte // cache for default getReadBuf
   909  
   910  	maxWriteSize uint32 // zero means unlimited; TODO: implement
   911  
   912  	w    io.Writer
   913  	wbuf []byte
   914  
   915  	// AllowIllegalWrites permits the Framer's Write methods to
   916  	// write frames that do not conform to the HTTP/2 spec. This
   917  	// permits using the Framer to test other HTTP/2
   918  	// implementations' conformance to the spec.
   919  	// If false, the Write methods will prefer to return an error
   920  	// rather than comply.
   921  	AllowIllegalWrites bool
   922  
   923  	// AllowIllegalReads permits the Framer's ReadFrame method
   924  	// to return non-compliant frames or frame orders.
   925  	// This is for testing and permits using the Framer to test
   926  	// other HTTP/2 implementations' conformance to the spec.
   927  	// It is not compatible with ReadMetaHeaders.
   928  	AllowIllegalReads bool
   929  
   930  	// ReadMetaHeaders if non-nil causes ReadFrame to merge
   931  	// HEADERS and CONTINUATION frames together and return
   932  	// MetaHeadersFrame instead.
   933  	ReadMetaHeaders *hpack.Decoder
   934  
   935  	// MaxHeaderListSize is the http2 MAX_HEADER_LIST_SIZE.
   936  	// It's used only if ReadMetaHeaders is set; 0 means a sane default
   937  	// (currently 16MB)
   938  	// If the limit is hit, MetaHeadersFrame.Truncated is set true.
   939  	MaxHeaderListSize uint32
   940  
   941  	logReads, logWrites bool
   942  
   943  	debugFramer       *http2Framer // only use for logging written writes
   944  	debugFramerBuf    *bytes.Buffer
   945  	debugReadLoggerf  func(string, ...interface{})
   946  	debugWriteLoggerf func(string, ...interface{})
   947  
   948  	frameCache *http2frameCache // nil if frames aren't reused (default)
   949  }
   950  
   951  func (fr *http2Framer) maxHeaderListSize() uint32 {
   952  	if fr.MaxHeaderListSize == 0 {
   953  		return 16 << 20
   954  	}
   955  	return fr.MaxHeaderListSize
   956  }
   957  
   958  func (f *http2Framer) startWrite(ftype http2FrameType, flags http2Flags, streamID uint32) {
   959  
   960  	f.wbuf = append(f.wbuf[:0],
   961  		0,
   962  		0,
   963  		0,
   964  		byte(ftype),
   965  		byte(flags),
   966  		byte(streamID>>24),
   967  		byte(streamID>>16),
   968  		byte(streamID>>8),
   969  		byte(streamID))
   970  }
   971  
   972  func (f *http2Framer) endWrite() error {
   973  
   974  	length := len(f.wbuf) - http2frameHeaderLen
   975  	if length >= (1 << 24) {
   976  		return http2ErrFrameTooLarge
   977  	}
   978  	_ = append(f.wbuf[:0],
   979  		byte(length>>16),
   980  		byte(length>>8),
   981  		byte(length))
   982  	if f.logWrites {
   983  		f.logWrite()
   984  	}
   985  
   986  	n, err := f.w.Write(f.wbuf)
   987  	if err == nil && n != len(f.wbuf) {
   988  		err = io.ErrShortWrite
   989  	}
   990  	return err
   991  }
   992  
   993  func (f *http2Framer) logWrite() {
   994  	if f.debugFramer == nil {
   995  		f.debugFramerBuf = new(bytes.Buffer)
   996  		f.debugFramer = http2NewFramer(nil, f.debugFramerBuf)
   997  		f.debugFramer.logReads = false
   998  
   999  		f.debugFramer.AllowIllegalReads = true
  1000  	}
  1001  	f.debugFramerBuf.Write(f.wbuf)
  1002  	fr, err := f.debugFramer.ReadFrame()
  1003  	if err != nil {
  1004  		f.debugWriteLoggerf("http2: Framer %p: failed to decode just-written frame", f)
  1005  		return
  1006  	}
  1007  	f.debugWriteLoggerf("http2: Framer %p: wrote %v", f, http2summarizeFrame(fr))
  1008  }
  1009  
  1010  func (f *http2Framer) writeByte(v byte) { f.wbuf = append(f.wbuf, v) }
  1011  
  1012  func (f *http2Framer) writeBytes(v []byte) { f.wbuf = append(f.wbuf, v...) }
  1013  
  1014  func (f *http2Framer) writeUint16(v uint16) { f.wbuf = append(f.wbuf, byte(v>>8), byte(v)) }
  1015  
  1016  func (f *http2Framer) writeUint32(v uint32) {
  1017  	f.wbuf = append(f.wbuf, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
  1018  }
  1019  
  1020  const (
  1021  	http2minMaxFrameSize = 1 << 14
  1022  	http2maxFrameSize    = 1<<24 - 1
  1023  )
  1024  
  1025  // SetReuseFrames allows the Framer to reuse Frames.
  1026  // If called on a Framer, Frames returned by calls to ReadFrame are only
  1027  // valid until the next call to ReadFrame.
  1028  func (fr *http2Framer) SetReuseFrames() {
  1029  	if fr.frameCache != nil {
  1030  		return
  1031  	}
  1032  	fr.frameCache = &http2frameCache{}
  1033  }
  1034  
  1035  type http2frameCache struct {
  1036  	dataFrame http2DataFrame
  1037  }
  1038  
  1039  func (fc *http2frameCache) getDataFrame() *http2DataFrame {
  1040  	if fc == nil {
  1041  		return &http2DataFrame{}
  1042  	}
  1043  	return &fc.dataFrame
  1044  }
  1045  
  1046  // NewFramer returns a Framer that writes frames to w and reads them from r.
  1047  func http2NewFramer(w io.Writer, r io.Reader) *http2Framer {
  1048  	fr := &http2Framer{
  1049  		w:                 w,
  1050  		r:                 r,
  1051  		logReads:          http2logFrameReads,
  1052  		logWrites:         http2logFrameWrites,
  1053  		debugReadLoggerf:  log.Printf,
  1054  		debugWriteLoggerf: log.Printf,
  1055  	}
  1056  	fr.getReadBuf = func(size uint32) []byte {
  1057  		if cap(fr.readBuf) >= int(size) {
  1058  			return fr.readBuf[:size]
  1059  		}
  1060  		fr.readBuf = make([]byte, size)
  1061  		return fr.readBuf
  1062  	}
  1063  	fr.SetMaxReadFrameSize(http2maxFrameSize)
  1064  	return fr
  1065  }
  1066  
  1067  // SetMaxReadFrameSize sets the maximum size of a frame
  1068  // that will be read by a subsequent call to ReadFrame.
  1069  // It is the caller's responsibility to advertise this
  1070  // limit with a SETTINGS frame.
  1071  func (fr *http2Framer) SetMaxReadFrameSize(v uint32) {
  1072  	if v > http2maxFrameSize {
  1073  		v = http2maxFrameSize
  1074  	}
  1075  	fr.maxReadSize = v
  1076  }
  1077  
  1078  // ErrorDetail returns a more detailed error of the last error
  1079  // returned by Framer.ReadFrame. For instance, if ReadFrame
  1080  // returns a StreamError with code PROTOCOL_ERROR, ErrorDetail
  1081  // will say exactly what was invalid. ErrorDetail is not guaranteed
  1082  // to return a non-nil value and like the rest of the http2 package,
  1083  // its return value is not protected by an API compatibility promise.
  1084  // ErrorDetail is reset after the next call to ReadFrame.
  1085  func (fr *http2Framer) ErrorDetail() error {
  1086  	return fr.errDetail
  1087  }
  1088  
  1089  // ErrFrameTooLarge is returned from Framer.ReadFrame when the peer
  1090  // sends a frame that is larger than declared with SetMaxReadFrameSize.
  1091  var http2ErrFrameTooLarge = errors.New("http2: frame too large")
  1092  
  1093  // terminalReadFrameError reports whether err is an unrecoverable
  1094  // error from ReadFrame and no other frames should be read.
  1095  func http2terminalReadFrameError(err error) bool {
  1096  	if _, ok := err.(http2StreamError); ok {
  1097  		return false
  1098  	}
  1099  	return err != nil
  1100  }
  1101  
  1102  // ReadFrame reads a single frame. The returned Frame is only valid
  1103  // until the next call to ReadFrame.
  1104  //
  1105  // If the frame is larger than previously set with SetMaxReadFrameSize, the
  1106  // returned error is ErrFrameTooLarge. Other errors may be of type
  1107  // ConnectionError, StreamError, or anything else from the underlying
  1108  // reader.
  1109  func (fr *http2Framer) ReadFrame() (http2Frame, error) {
  1110  	fr.errDetail = nil
  1111  	if fr.lastFrame != nil {
  1112  		fr.lastFrame.invalidate()
  1113  	}
  1114  	fh, err := http2readFrameHeader(fr.headerBuf[:], fr.r)
  1115  	if err != nil {
  1116  		return nil, err
  1117  	}
  1118  	if fh.Length > fr.maxReadSize {
  1119  		return nil, http2ErrFrameTooLarge
  1120  	}
  1121  	payload := fr.getReadBuf(fh.Length)
  1122  	if _, err := io.ReadFull(fr.r, payload); err != nil {
  1123  		return nil, err
  1124  	}
  1125  	f, err := http2typeFrameParser(fh.Type)(fr.frameCache, fh, payload)
  1126  	if err != nil {
  1127  		if ce, ok := err.(http2connError); ok {
  1128  			return nil, fr.connError(ce.Code, ce.Reason)
  1129  		}
  1130  		return nil, err
  1131  	}
  1132  	if err := fr.checkFrameOrder(f); err != nil {
  1133  		return nil, err
  1134  	}
  1135  	if fr.logReads {
  1136  		fr.debugReadLoggerf("http2: Framer %p: read %v", fr, http2summarizeFrame(f))
  1137  	}
  1138  	if fh.Type == http2FrameHeaders && fr.ReadMetaHeaders != nil {
  1139  		return fr.readMetaFrame(f.(*http2HeadersFrame))
  1140  	}
  1141  	return f, nil
  1142  }
  1143  
  1144  // connError returns ConnectionError(code) but first
  1145  // stashes away a public reason to the caller can optionally relay it
  1146  // to the peer before hanging up on them. This might help others debug
  1147  // their implementations.
  1148  func (fr *http2Framer) connError(code http2ErrCode, reason string) error {
  1149  	fr.errDetail = errors.New(reason)
  1150  	return http2ConnectionError(code)
  1151  }
  1152  
  1153  // checkFrameOrder reports an error if f is an invalid frame to return
  1154  // next from ReadFrame. Mostly it checks whether HEADERS and
  1155  // CONTINUATION frames are contiguous.
  1156  func (fr *http2Framer) checkFrameOrder(f http2Frame) error {
  1157  	last := fr.lastFrame
  1158  	fr.lastFrame = f
  1159  	if fr.AllowIllegalReads {
  1160  		return nil
  1161  	}
  1162  
  1163  	fh := f.Header()
  1164  	if fr.lastHeaderStream != 0 {
  1165  		if fh.Type != http2FrameContinuation {
  1166  			return fr.connError(http2ErrCodeProtocol,
  1167  				fmt.Sprintf("got %s for stream %d; expected CONTINUATION following %s for stream %d",
  1168  					fh.Type, fh.StreamID,
  1169  					last.Header().Type, fr.lastHeaderStream))
  1170  		}
  1171  		if fh.StreamID != fr.lastHeaderStream {
  1172  			return fr.connError(http2ErrCodeProtocol,
  1173  				fmt.Sprintf("got CONTINUATION for stream %d; expected stream %d",
  1174  					fh.StreamID, fr.lastHeaderStream))
  1175  		}
  1176  	} else if fh.Type == http2FrameContinuation {
  1177  		return fr.connError(http2ErrCodeProtocol, fmt.Sprintf("unexpected CONTINUATION for stream %d", fh.StreamID))
  1178  	}
  1179  
  1180  	switch fh.Type {
  1181  	case http2FrameHeaders, http2FrameContinuation:
  1182  		if fh.Flags.Has(http2FlagHeadersEndHeaders) {
  1183  			fr.lastHeaderStream = 0
  1184  		} else {
  1185  			fr.lastHeaderStream = fh.StreamID
  1186  		}
  1187  	}
  1188  
  1189  	return nil
  1190  }
  1191  
  1192  // A DataFrame conveys arbitrary, variable-length sequences of octets
  1193  // associated with a stream.
  1194  // See http://http2.github.io/http2-spec/#rfc.section.6.1
  1195  type http2DataFrame struct {
  1196  	http2FrameHeader
  1197  	data []byte
  1198  }
  1199  
  1200  func (f *http2DataFrame) StreamEnded() bool {
  1201  	return f.http2FrameHeader.Flags.Has(http2FlagDataEndStream)
  1202  }
  1203  
  1204  // Data returns the frame's data octets, not including any padding
  1205  // size byte or padding suffix bytes.
  1206  // The caller must not retain the returned memory past the next
  1207  // call to ReadFrame.
  1208  func (f *http2DataFrame) Data() []byte {
  1209  	f.checkValid()
  1210  	return f.data
  1211  }
  1212  
  1213  func http2parseDataFrame(fc *http2frameCache, fh http2FrameHeader, payload []byte) (http2Frame, error) {
  1214  	if fh.StreamID == 0 {
  1215  
  1216  		return nil, http2connError{http2ErrCodeProtocol, "DATA frame with stream ID 0"}
  1217  	}
  1218  	f := fc.getDataFrame()
  1219  	f.http2FrameHeader = fh
  1220  
  1221  	var padSize byte
  1222  	if fh.Flags.Has(http2FlagDataPadded) {
  1223  		var err error
  1224  		payload, padSize, err = http2readByte(payload)
  1225  		if err != nil {
  1226  			return nil, err
  1227  		}
  1228  	}
  1229  	if int(padSize) > len(payload) {
  1230  
  1231  		return nil, http2connError{http2ErrCodeProtocol, "pad size larger than data payload"}
  1232  	}
  1233  	f.data = payload[:len(payload)-int(padSize)]
  1234  	return f, nil
  1235  }
  1236  
  1237  var (
  1238  	http2errStreamID    = errors.New("invalid stream ID")
  1239  	http2errDepStreamID = errors.New("invalid dependent stream ID")
  1240  	http2errPadLength   = errors.New("pad length too large")
  1241  	http2errPadBytes    = errors.New("padding bytes must all be zeros unless AllowIllegalWrites is enabled")
  1242  )
  1243  
  1244  func http2validStreamIDOrZero(streamID uint32) bool {
  1245  	return streamID&(1<<31) == 0
  1246  }
  1247  
  1248  func http2validStreamID(streamID uint32) bool {
  1249  	return streamID != 0 && streamID&(1<<31) == 0
  1250  }
  1251  
  1252  // WriteData writes a DATA frame.
  1253  //
  1254  // It will perform exactly one Write to the underlying Writer.
  1255  // It is the caller's responsibility not to violate the maximum frame size
  1256  // and to not call other Write methods concurrently.
  1257  func (f *http2Framer) WriteData(streamID uint32, endStream bool, data []byte) error {
  1258  	return f.WriteDataPadded(streamID, endStream, data, nil)
  1259  }
  1260  
  1261  // WriteData writes a DATA frame with optional padding.
  1262  //
  1263  // If pad is nil, the padding bit is not sent.
  1264  // The length of pad must not exceed 255 bytes.
  1265  // The bytes of pad must all be zero, unless f.AllowIllegalWrites is set.
  1266  //
  1267  // It will perform exactly one Write to the underlying Writer.
  1268  // It is the caller's responsibility not to violate the maximum frame size
  1269  // and to not call other Write methods concurrently.
  1270  func (f *http2Framer) WriteDataPadded(streamID uint32, endStream bool, data, pad []byte) error {
  1271  	if !http2validStreamID(streamID) && !f.AllowIllegalWrites {
  1272  		return http2errStreamID
  1273  	}
  1274  	if len(pad) > 0 {
  1275  		if len(pad) > 255 {
  1276  			return http2errPadLength
  1277  		}
  1278  		if !f.AllowIllegalWrites {
  1279  			for _, b := range pad {
  1280  				if b != 0 {
  1281  
  1282  					return http2errPadBytes
  1283  				}
  1284  			}
  1285  		}
  1286  	}
  1287  	var flags http2Flags
  1288  	if endStream {
  1289  		flags |= http2FlagDataEndStream
  1290  	}
  1291  	if pad != nil {
  1292  		flags |= http2FlagDataPadded
  1293  	}
  1294  	f.startWrite(http2FrameData, flags, streamID)
  1295  	if pad != nil {
  1296  		f.wbuf = append(f.wbuf, byte(len(pad)))
  1297  	}
  1298  	f.wbuf = append(f.wbuf, data...)
  1299  	f.wbuf = append(f.wbuf, pad...)
  1300  	return f.endWrite()
  1301  }
  1302  
  1303  // A SettingsFrame conveys configuration parameters that affect how
  1304  // endpoints communicate, such as preferences and constraints on peer
  1305  // behavior.
  1306  //
  1307  // See http://http2.github.io/http2-spec/#SETTINGS
  1308  type http2SettingsFrame struct {
  1309  	http2FrameHeader
  1310  	p []byte
  1311  }
  1312  
  1313  func http2parseSettingsFrame(_ *http2frameCache, fh http2FrameHeader, p []byte) (http2Frame, error) {
  1314  	if fh.Flags.Has(http2FlagSettingsAck) && fh.Length > 0 {
  1315  
  1316  		return nil, http2ConnectionError(http2ErrCodeFrameSize)
  1317  	}
  1318  	if fh.StreamID != 0 {
  1319  
  1320  		return nil, http2ConnectionError(http2ErrCodeProtocol)
  1321  	}
  1322  	if len(p)%6 != 0 {
  1323  
  1324  		return nil, http2ConnectionError(http2ErrCodeFrameSize)
  1325  	}
  1326  	f := &http2SettingsFrame{http2FrameHeader: fh, p: p}
  1327  	if v, ok := f.Value(http2SettingInitialWindowSize); ok && v > (1<<31)-1 {
  1328  
  1329  		return nil, http2ConnectionError(http2ErrCodeFlowControl)
  1330  	}
  1331  	return f, nil
  1332  }
  1333  
  1334  func (f *http2SettingsFrame) IsAck() bool {
  1335  	return f.http2FrameHeader.Flags.Has(http2FlagSettingsAck)
  1336  }
  1337  
  1338  func (f *http2SettingsFrame) Value(s http2SettingID) (v uint32, ok bool) {
  1339  	f.checkValid()
  1340  	buf := f.p
  1341  	for len(buf) > 0 {
  1342  		settingID := http2SettingID(binary.BigEndian.Uint16(buf[:2]))
  1343  		if settingID == s {
  1344  			return binary.BigEndian.Uint32(buf[2:6]), true
  1345  		}
  1346  		buf = buf[6:]
  1347  	}
  1348  	return 0, false
  1349  }
  1350  
  1351  // ForeachSetting runs fn for each setting.
  1352  // It stops and returns the first error.
  1353  func (f *http2SettingsFrame) ForeachSetting(fn func(http2Setting) error) error {
  1354  	f.checkValid()
  1355  	buf := f.p
  1356  	for len(buf) > 0 {
  1357  		if err := fn(http2Setting{
  1358  			http2SettingID(binary.BigEndian.Uint16(buf[:2])),
  1359  			binary.BigEndian.Uint32(buf[2:6]),
  1360  		}); err != nil {
  1361  			return err
  1362  		}
  1363  		buf = buf[6:]
  1364  	}
  1365  	return nil
  1366  }
  1367  
  1368  // WriteSettings writes a SETTINGS frame with zero or more settings
  1369  // specified and the ACK bit not set.
  1370  //
  1371  // It will perform exactly one Write to the underlying Writer.
  1372  // It is the caller's responsibility to not call other Write methods concurrently.
  1373  func (f *http2Framer) WriteSettings(settings ...http2Setting) error {
  1374  	f.startWrite(http2FrameSettings, 0, 0)
  1375  	for _, s := range settings {
  1376  		f.writeUint16(uint16(s.ID))
  1377  		f.writeUint32(s.Val)
  1378  	}
  1379  	return f.endWrite()
  1380  }
  1381  
  1382  // WriteSettingsAck writes an empty SETTINGS frame with the ACK bit set.
  1383  //
  1384  // It will perform exactly one Write to the underlying Writer.
  1385  // It is the caller's responsibility to not call other Write methods concurrently.
  1386  func (f *http2Framer) WriteSettingsAck() error {
  1387  	f.startWrite(http2FrameSettings, http2FlagSettingsAck, 0)
  1388  	return f.endWrite()
  1389  }
  1390  
  1391  // A PingFrame is a mechanism for measuring a minimal round trip time
  1392  // from the sender, as well as determining whether an idle connection
  1393  // is still functional.
  1394  // See http://http2.github.io/http2-spec/#rfc.section.6.7
  1395  type http2PingFrame struct {
  1396  	http2FrameHeader
  1397  	Data [8]byte
  1398  }
  1399  
  1400  func (f *http2PingFrame) IsAck() bool { return f.Flags.Has(http2FlagPingAck) }
  1401  
  1402  func http2parsePingFrame(_ *http2frameCache, fh http2FrameHeader, payload []byte) (http2Frame, error) {
  1403  	if len(payload) != 8 {
  1404  		return nil, http2ConnectionError(http2ErrCodeFrameSize)
  1405  	}
  1406  	if fh.StreamID != 0 {
  1407  		return nil, http2ConnectionError(http2ErrCodeProtocol)
  1408  	}
  1409  	f := &http2PingFrame{http2FrameHeader: fh}
  1410  	copy(f.Data[:], payload)
  1411  	return f, nil
  1412  }
  1413  
  1414  func (f *http2Framer) WritePing(ack bool, data [8]byte) error {
  1415  	var flags http2Flags
  1416  	if ack {
  1417  		flags = http2FlagPingAck
  1418  	}
  1419  	f.startWrite(http2FramePing, flags, 0)
  1420  	f.writeBytes(data[:])
  1421  	return f.endWrite()
  1422  }
  1423  
  1424  // A GoAwayFrame informs the remote peer to stop creating streams on this connection.
  1425  // See http://http2.github.io/http2-spec/#rfc.section.6.8
  1426  type http2GoAwayFrame struct {
  1427  	http2FrameHeader
  1428  	LastStreamID uint32
  1429  	ErrCode      http2ErrCode
  1430  	debugData    []byte
  1431  }
  1432  
  1433  // DebugData returns any debug data in the GOAWAY frame. Its contents
  1434  // are not defined.
  1435  // The caller must not retain the returned memory past the next
  1436  // call to ReadFrame.
  1437  func (f *http2GoAwayFrame) DebugData() []byte {
  1438  	f.checkValid()
  1439  	return f.debugData
  1440  }
  1441  
  1442  func http2parseGoAwayFrame(_ *http2frameCache, fh http2FrameHeader, p []byte) (http2Frame, error) {
  1443  	if fh.StreamID != 0 {
  1444  		return nil, http2ConnectionError(http2ErrCodeProtocol)
  1445  	}
  1446  	if len(p) < 8 {
  1447  		return nil, http2ConnectionError(http2ErrCodeFrameSize)
  1448  	}
  1449  	return &http2GoAwayFrame{
  1450  		http2FrameHeader: fh,
  1451  		LastStreamID:     binary.BigEndian.Uint32(p[:4]) & (1<<31 - 1),
  1452  		ErrCode:          http2ErrCode(binary.BigEndian.Uint32(p[4:8])),
  1453  		debugData:        p[8:],
  1454  	}, nil
  1455  }
  1456  
  1457  func (f *http2Framer) WriteGoAway(maxStreamID uint32, code http2ErrCode, debugData []byte) error {
  1458  	f.startWrite(http2FrameGoAway, 0, 0)
  1459  	f.writeUint32(maxStreamID & (1<<31 - 1))
  1460  	f.writeUint32(uint32(code))
  1461  	f.writeBytes(debugData)
  1462  	return f.endWrite()
  1463  }
  1464  
  1465  // An UnknownFrame is the frame type returned when the frame type is unknown
  1466  // or no specific frame type parser exists.
  1467  type http2UnknownFrame struct {
  1468  	http2FrameHeader
  1469  	p []byte
  1470  }
  1471  
  1472  // Payload returns the frame's payload (after the header).  It is not
  1473  // valid to call this method after a subsequent call to
  1474  // Framer.ReadFrame, nor is it valid to retain the returned slice.
  1475  // The memory is owned by the Framer and is invalidated when the next
  1476  // frame is read.
  1477  func (f *http2UnknownFrame) Payload() []byte {
  1478  	f.checkValid()
  1479  	return f.p
  1480  }
  1481  
  1482  func http2parseUnknownFrame(_ *http2frameCache, fh http2FrameHeader, p []byte) (http2Frame, error) {
  1483  	return &http2UnknownFrame{fh, p}, nil
  1484  }
  1485  
  1486  // A WindowUpdateFrame is used to implement flow control.
  1487  // See http://http2.github.io/http2-spec/#rfc.section.6.9
  1488  type http2WindowUpdateFrame struct {
  1489  	http2FrameHeader
  1490  	Increment uint32 // never read with high bit set
  1491  }
  1492  
  1493  func http2parseWindowUpdateFrame(_ *http2frameCache, fh http2FrameHeader, p []byte) (http2Frame, error) {
  1494  	if len(p) != 4 {
  1495  		return nil, http2ConnectionError(http2ErrCodeFrameSize)
  1496  	}
  1497  	inc := binary.BigEndian.Uint32(p[:4]) & 0x7fffffff
  1498  	if inc == 0 {
  1499  
  1500  		if fh.StreamID == 0 {
  1501  			return nil, http2ConnectionError(http2ErrCodeProtocol)
  1502  		}
  1503  		return nil, http2streamError(fh.StreamID, http2ErrCodeProtocol)
  1504  	}
  1505  	return &http2WindowUpdateFrame{
  1506  		http2FrameHeader: fh,
  1507  		Increment:        inc,
  1508  	}, nil
  1509  }
  1510  
  1511  // WriteWindowUpdate writes a WINDOW_UPDATE frame.
  1512  // The increment value must be between 1 and 2,147,483,647, inclusive.
  1513  // If the Stream ID is zero, the window update applies to the
  1514  // connection as a whole.
  1515  func (f *http2Framer) WriteWindowUpdate(streamID, incr uint32) error {
  1516  
  1517  	if (incr < 1 || incr > 2147483647) && !f.AllowIllegalWrites {
  1518  		return errors.New("illegal window increment value")
  1519  	}
  1520  	f.startWrite(http2FrameWindowUpdate, 0, streamID)
  1521  	f.writeUint32(incr)
  1522  	return f.endWrite()
  1523  }
  1524  
  1525  // A HeadersFrame is used to open a stream and additionally carries a
  1526  // header block fragment.
  1527  type http2HeadersFrame struct {
  1528  	http2FrameHeader
  1529  
  1530  	// Priority is set if FlagHeadersPriority is set in the FrameHeader.
  1531  	Priority http2PriorityParam
  1532  
  1533  	headerFragBuf []byte // not owned
  1534  }
  1535  
  1536  func (f *http2HeadersFrame) HeaderBlockFragment() []byte {
  1537  	f.checkValid()
  1538  	return f.headerFragBuf
  1539  }
  1540  
  1541  func (f *http2HeadersFrame) HeadersEnded() bool {
  1542  	return f.http2FrameHeader.Flags.Has(http2FlagHeadersEndHeaders)
  1543  }
  1544  
  1545  func (f *http2HeadersFrame) StreamEnded() bool {
  1546  	return f.http2FrameHeader.Flags.Has(http2FlagHeadersEndStream)
  1547  }
  1548  
  1549  func (f *http2HeadersFrame) HasPriority() bool {
  1550  	return f.http2FrameHeader.Flags.Has(http2FlagHeadersPriority)
  1551  }
  1552  
  1553  func http2parseHeadersFrame(_ *http2frameCache, fh http2FrameHeader, p []byte) (_ http2Frame, err error) {
  1554  	hf := &http2HeadersFrame{
  1555  		http2FrameHeader: fh,
  1556  	}
  1557  	if fh.StreamID == 0 {
  1558  
  1559  		return nil, http2connError{http2ErrCodeProtocol, "HEADERS frame with stream ID 0"}
  1560  	}
  1561  	var padLength uint8
  1562  	if fh.Flags.Has(http2FlagHeadersPadded) {
  1563  		if p, padLength, err = http2readByte(p); err != nil {
  1564  			return
  1565  		}
  1566  	}
  1567  	if fh.Flags.Has(http2FlagHeadersPriority) {
  1568  		var v uint32
  1569  		p, v, err = http2readUint32(p)
  1570  		if err != nil {
  1571  			return nil, err
  1572  		}
  1573  		hf.Priority.StreamDep = v & 0x7fffffff
  1574  		hf.Priority.Exclusive = (v != hf.Priority.StreamDep)
  1575  		p, hf.Priority.Weight, err = http2readByte(p)
  1576  		if err != nil {
  1577  			return nil, err
  1578  		}
  1579  	}
  1580  	if len(p)-int(padLength) <= 0 {
  1581  		return nil, http2streamError(fh.StreamID, http2ErrCodeProtocol)
  1582  	}
  1583  	hf.headerFragBuf = p[:len(p)-int(padLength)]
  1584  	return hf, nil
  1585  }
  1586  
  1587  // HeadersFrameParam are the parameters for writing a HEADERS frame.
  1588  type http2HeadersFrameParam struct {
  1589  	// StreamID is the required Stream ID to initiate.
  1590  	StreamID uint32
  1591  	// BlockFragment is part (or all) of a Header Block.
  1592  	BlockFragment []byte
  1593  
  1594  	// EndStream indicates that the header block is the last that
  1595  	// the endpoint will send for the identified stream. Setting
  1596  	// this flag causes the stream to enter one of "half closed"
  1597  	// states.
  1598  	EndStream bool
  1599  
  1600  	// EndHeaders indicates that this frame contains an entire
  1601  	// header block and is not followed by any
  1602  	// CONTINUATION frames.
  1603  	EndHeaders bool
  1604  
  1605  	// PadLength is the optional number of bytes of zeros to add
  1606  	// to this frame.
  1607  	PadLength uint8
  1608  
  1609  	// Priority, if non-zero, includes stream priority information
  1610  	// in the HEADER frame.
  1611  	Priority http2PriorityParam
  1612  }
  1613  
  1614  // WriteHeaders writes a single HEADERS frame.
  1615  //
  1616  // This is a low-level header writing method. Encoding headers and
  1617  // splitting them into any necessary CONTINUATION frames is handled
  1618  // elsewhere.
  1619  //
  1620  // It will perform exactly one Write to the underlying Writer.
  1621  // It is the caller's responsibility to not call other Write methods concurrently.
  1622  func (f *http2Framer) WriteHeaders(p http2HeadersFrameParam) error {
  1623  	if !http2validStreamID(p.StreamID) && !f.AllowIllegalWrites {
  1624  		return http2errStreamID
  1625  	}
  1626  	var flags http2Flags
  1627  	if p.PadLength != 0 {
  1628  		flags |= http2FlagHeadersPadded
  1629  	}
  1630  	if p.EndStream {
  1631  		flags |= http2FlagHeadersEndStream
  1632  	}
  1633  	if p.EndHeaders {
  1634  		flags |= http2FlagHeadersEndHeaders
  1635  	}
  1636  	if !p.Priority.IsZero() {
  1637  		flags |= http2FlagHeadersPriority
  1638  	}
  1639  	f.startWrite(http2FrameHeaders, flags, p.StreamID)
  1640  	if p.PadLength != 0 {
  1641  		f.writeByte(p.PadLength)
  1642  	}
  1643  	if !p.Priority.IsZero() {
  1644  		v := p.Priority.StreamDep
  1645  		if !http2validStreamIDOrZero(v) && !f.AllowIllegalWrites {
  1646  			return http2errDepStreamID
  1647  		}
  1648  		if p.Priority.Exclusive {
  1649  			v |= 1 << 31
  1650  		}
  1651  		f.writeUint32(v)
  1652  		f.writeByte(p.Priority.Weight)
  1653  	}
  1654  	f.wbuf = append(f.wbuf, p.BlockFragment...)
  1655  	f.wbuf = append(f.wbuf, http2padZeros[:p.PadLength]...)
  1656  	return f.endWrite()
  1657  }
  1658  
  1659  // A PriorityFrame specifies the sender-advised priority of a stream.
  1660  // See http://http2.github.io/http2-spec/#rfc.section.6.3
  1661  type http2PriorityFrame struct {
  1662  	http2FrameHeader
  1663  	http2PriorityParam
  1664  }
  1665  
  1666  // PriorityParam are the stream prioritzation parameters.
  1667  type http2PriorityParam struct {
  1668  	// StreamDep is a 31-bit stream identifier for the
  1669  	// stream that this stream depends on. Zero means no
  1670  	// dependency.
  1671  	StreamDep uint32
  1672  
  1673  	// Exclusive is whether the dependency is exclusive.
  1674  	Exclusive bool
  1675  
  1676  	// Weight is the stream's zero-indexed weight. It should be
  1677  	// set together with StreamDep, or neither should be set. Per
  1678  	// the spec, "Add one to the value to obtain a weight between
  1679  	// 1 and 256."
  1680  	Weight uint8
  1681  }
  1682  
  1683  func (p http2PriorityParam) IsZero() bool {
  1684  	return p == http2PriorityParam{}
  1685  }
  1686  
  1687  func http2parsePriorityFrame(_ *http2frameCache, fh http2FrameHeader, payload []byte) (http2Frame, error) {
  1688  	if fh.StreamID == 0 {
  1689  		return nil, http2connError{http2ErrCodeProtocol, "PRIORITY frame with stream ID 0"}
  1690  	}
  1691  	if len(payload) != 5 {
  1692  		return nil, http2connError{http2ErrCodeFrameSize, fmt.Sprintf("PRIORITY frame payload size was %d; want 5", len(payload))}
  1693  	}
  1694  	v := binary.BigEndian.Uint32(payload[:4])
  1695  	streamID := v & 0x7fffffff
  1696  	return &http2PriorityFrame{
  1697  		http2FrameHeader: fh,
  1698  		http2PriorityParam: http2PriorityParam{
  1699  			Weight:    payload[4],
  1700  			StreamDep: streamID,
  1701  			Exclusive: streamID != v,
  1702  		},
  1703  	}, nil
  1704  }
  1705  
  1706  // WritePriority writes a PRIORITY frame.
  1707  //
  1708  // It will perform exactly one Write to the underlying Writer.
  1709  // It is the caller's responsibility to not call other Write methods concurrently.
  1710  func (f *http2Framer) WritePriority(streamID uint32, p http2PriorityParam) error {
  1711  	if !http2validStreamID(streamID) && !f.AllowIllegalWrites {
  1712  		return http2errStreamID
  1713  	}
  1714  	if !http2validStreamIDOrZero(p.StreamDep) {
  1715  		return http2errDepStreamID
  1716  	}
  1717  	f.startWrite(http2FramePriority, 0, streamID)
  1718  	v := p.StreamDep
  1719  	if p.Exclusive {
  1720  		v |= 1 << 31
  1721  	}
  1722  	f.writeUint32(v)
  1723  	f.writeByte(p.Weight)
  1724  	return f.endWrite()
  1725  }
  1726  
  1727  // A RSTStreamFrame allows for abnormal termination of a stream.
  1728  // See http://http2.github.io/http2-spec/#rfc.section.6.4
  1729  type http2RSTStreamFrame struct {
  1730  	http2FrameHeader
  1731  	ErrCode http2ErrCode
  1732  }
  1733  
  1734  func http2parseRSTStreamFrame(_ *http2frameCache, fh http2FrameHeader, p []byte) (http2Frame, error) {
  1735  	if len(p) != 4 {
  1736  		return nil, http2ConnectionError(http2ErrCodeFrameSize)
  1737  	}
  1738  	if fh.StreamID == 0 {
  1739  		return nil, http2ConnectionError(http2ErrCodeProtocol)
  1740  	}
  1741  	return &http2RSTStreamFrame{fh, http2ErrCode(binary.BigEndian.Uint32(p[:4]))}, nil
  1742  }
  1743  
  1744  // WriteRSTStream writes a RST_STREAM frame.
  1745  //
  1746  // It will perform exactly one Write to the underlying Writer.
  1747  // It is the caller's responsibility to not call other Write methods concurrently.
  1748  func (f *http2Framer) WriteRSTStream(streamID uint32, code http2ErrCode) error {
  1749  	if !http2validStreamID(streamID) && !f.AllowIllegalWrites {
  1750  		return http2errStreamID
  1751  	}
  1752  	f.startWrite(http2FrameRSTStream, 0, streamID)
  1753  	f.writeUint32(uint32(code))
  1754  	return f.endWrite()
  1755  }
  1756  
  1757  // A ContinuationFrame is used to continue a sequence of header block fragments.
  1758  // See http://http2.github.io/http2-spec/#rfc.section.6.10
  1759  type http2ContinuationFrame struct {
  1760  	http2FrameHeader
  1761  	headerFragBuf []byte
  1762  }
  1763  
  1764  func http2parseContinuationFrame(_ *http2frameCache, fh http2FrameHeader, p []byte) (http2Frame, error) {
  1765  	if fh.StreamID == 0 {
  1766  		return nil, http2connError{http2ErrCodeProtocol, "CONTINUATION frame with stream ID 0"}
  1767  	}
  1768  	return &http2ContinuationFrame{fh, p}, nil
  1769  }
  1770  
  1771  func (f *http2ContinuationFrame) HeaderBlockFragment() []byte {
  1772  	f.checkValid()
  1773  	return f.headerFragBuf
  1774  }
  1775  
  1776  func (f *http2ContinuationFrame) HeadersEnded() bool {
  1777  	return f.http2FrameHeader.Flags.Has(http2FlagContinuationEndHeaders)
  1778  }
  1779  
  1780  // WriteContinuation writes a CONTINUATION frame.
  1781  //
  1782  // It will perform exactly one Write to the underlying Writer.
  1783  // It is the caller's responsibility to not call other Write methods concurrently.
  1784  func (f *http2Framer) WriteContinuation(streamID uint32, endHeaders bool, headerBlockFragment []byte) error {
  1785  	if !http2validStreamID(streamID) && !f.AllowIllegalWrites {
  1786  		return http2errStreamID
  1787  	}
  1788  	var flags http2Flags
  1789  	if endHeaders {
  1790  		flags |= http2FlagContinuationEndHeaders
  1791  	}
  1792  	f.startWrite(http2FrameContinuation, flags, streamID)
  1793  	f.wbuf = append(f.wbuf, headerBlockFragment...)
  1794  	return f.endWrite()
  1795  }
  1796  
  1797  // A PushPromiseFrame is used to initiate a server stream.
  1798  // See http://http2.github.io/http2-spec/#rfc.section.6.6
  1799  type http2PushPromiseFrame struct {
  1800  	http2FrameHeader
  1801  	PromiseID     uint32
  1802  	headerFragBuf []byte // not owned
  1803  }
  1804  
  1805  func (f *http2PushPromiseFrame) HeaderBlockFragment() []byte {
  1806  	f.checkValid()
  1807  	return f.headerFragBuf
  1808  }
  1809  
  1810  func (f *http2PushPromiseFrame) HeadersEnded() bool {
  1811  	return f.http2FrameHeader.Flags.Has(http2FlagPushPromiseEndHeaders)
  1812  }
  1813  
  1814  func http2parsePushPromise(_ *http2frameCache, fh http2FrameHeader, p []byte) (_ http2Frame, err error) {
  1815  	pp := &http2PushPromiseFrame{
  1816  		http2FrameHeader: fh,
  1817  	}
  1818  	if pp.StreamID == 0 {
  1819  
  1820  		return nil, http2ConnectionError(http2ErrCodeProtocol)
  1821  	}
  1822  	// The PUSH_PROMISE frame includes optional padding.
  1823  	// Padding fields and flags are identical to those defined for DATA frames
  1824  	var padLength uint8
  1825  	if fh.Flags.Has(http2FlagPushPromisePadded) {
  1826  		if p, padLength, err = http2readByte(p); err != nil {
  1827  			return
  1828  		}
  1829  	}
  1830  
  1831  	p, pp.PromiseID, err = http2readUint32(p)
  1832  	if err != nil {
  1833  		return
  1834  	}
  1835  	pp.PromiseID = pp.PromiseID & (1<<31 - 1)
  1836  
  1837  	if int(padLength) > len(p) {
  1838  
  1839  		return nil, http2ConnectionError(http2ErrCodeProtocol)
  1840  	}
  1841  	pp.headerFragBuf = p[:len(p)-int(padLength)]
  1842  	return pp, nil
  1843  }
  1844  
  1845  // PushPromiseParam are the parameters for writing a PUSH_PROMISE frame.
  1846  type http2PushPromiseParam struct {
  1847  	// StreamID is the required Stream ID to initiate.
  1848  	StreamID uint32
  1849  
  1850  	// PromiseID is the required Stream ID which this
  1851  	// Push Promises
  1852  	PromiseID uint32
  1853  
  1854  	// BlockFragment is part (or all) of a Header Block.
  1855  	BlockFragment []byte
  1856  
  1857  	// EndHeaders indicates that this frame contains an entire
  1858  	// header block and is not followed by any
  1859  	// CONTINUATION frames.
  1860  	EndHeaders bool
  1861  
  1862  	// PadLength is the optional number of bytes of zeros to add
  1863  	// to this frame.
  1864  	PadLength uint8
  1865  }
  1866  
  1867  // WritePushPromise writes a single PushPromise Frame.
  1868  //
  1869  // As with Header Frames, This is the low level call for writing
  1870  // individual frames. Continuation frames are handled elsewhere.
  1871  //
  1872  // It will perform exactly one Write to the underlying Writer.
  1873  // It is the caller's responsibility to not call other Write methods concurrently.
  1874  func (f *http2Framer) WritePushPromise(p http2PushPromiseParam) error {
  1875  	if !http2validStreamID(p.StreamID) && !f.AllowIllegalWrites {
  1876  		return http2errStreamID
  1877  	}
  1878  	var flags http2Flags
  1879  	if p.PadLength != 0 {
  1880  		flags |= http2FlagPushPromisePadded
  1881  	}
  1882  	if p.EndHeaders {
  1883  		flags |= http2FlagPushPromiseEndHeaders
  1884  	}
  1885  	f.startWrite(http2FramePushPromise, flags, p.StreamID)
  1886  	if p.PadLength != 0 {
  1887  		f.writeByte(p.PadLength)
  1888  	}
  1889  	if !http2validStreamID(p.PromiseID) && !f.AllowIllegalWrites {
  1890  		return http2errStreamID
  1891  	}
  1892  	f.writeUint32(p.PromiseID)
  1893  	f.wbuf = append(f.wbuf, p.BlockFragment...)
  1894  	f.wbuf = append(f.wbuf, http2padZeros[:p.PadLength]...)
  1895  	return f.endWrite()
  1896  }
  1897  
  1898  // WriteRawFrame writes a raw frame. This can be used to write
  1899  // extension frames unknown to this package.
  1900  func (f *http2Framer) WriteRawFrame(t http2FrameType, flags http2Flags, streamID uint32, payload []byte) error {
  1901  	f.startWrite(t, flags, streamID)
  1902  	f.writeBytes(payload)
  1903  	return f.endWrite()
  1904  }
  1905  
  1906  func http2readByte(p []byte) (remain []byte, b byte, err error) {
  1907  	if len(p) == 0 {
  1908  		return nil, 0, io.ErrUnexpectedEOF
  1909  	}
  1910  	return p[1:], p[0], nil
  1911  }
  1912  
  1913  func http2readUint32(p []byte) (remain []byte, v uint32, err error) {
  1914  	if len(p) < 4 {
  1915  		return nil, 0, io.ErrUnexpectedEOF
  1916  	}
  1917  	return p[4:], binary.BigEndian.Uint32(p[:4]), nil
  1918  }
  1919  
  1920  type http2streamEnder interface {
  1921  	StreamEnded() bool
  1922  }
  1923  
  1924  type http2headersEnder interface {
  1925  	HeadersEnded() bool
  1926  }
  1927  
  1928  type http2headersOrContinuation interface {
  1929  	http2headersEnder
  1930  	HeaderBlockFragment() []byte
  1931  }
  1932  
  1933  // A MetaHeadersFrame is the representation of one HEADERS frame and
  1934  // zero or more contiguous CONTINUATION frames and the decoding of
  1935  // their HPACK-encoded contents.
  1936  //
  1937  // This type of frame does not appear on the wire and is only returned
  1938  // by the Framer when Framer.ReadMetaHeaders is set.
  1939  type http2MetaHeadersFrame struct {
  1940  	*http2HeadersFrame
  1941  
  1942  	// Fields are the fields contained in the HEADERS and
  1943  	// CONTINUATION frames. The underlying slice is owned by the
  1944  	// Framer and must not be retained after the next call to
  1945  	// ReadFrame.
  1946  	//
  1947  	// Fields are guaranteed to be in the correct http2 order and
  1948  	// not have unknown pseudo header fields or invalid header
  1949  	// field names or values. Required pseudo header fields may be
  1950  	// missing, however. Use the MetaHeadersFrame.Pseudo accessor
  1951  	// method access pseudo headers.
  1952  	Fields []hpack.HeaderField
  1953  
  1954  	// Truncated is whether the max header list size limit was hit
  1955  	// and Fields is incomplete. The hpack decoder state is still
  1956  	// valid, however.
  1957  	Truncated bool
  1958  }
  1959  
  1960  // PseudoValue returns the given pseudo header field's value.
  1961  // The provided pseudo field should not contain the leading colon.
  1962  func (mh *http2MetaHeadersFrame) PseudoValue(pseudo string) string {
  1963  	for _, hf := range mh.Fields {
  1964  		if !hf.IsPseudo() {
  1965  			return ""
  1966  		}
  1967  		if hf.Name[1:] == pseudo {
  1968  			return hf.Value
  1969  		}
  1970  	}
  1971  	return ""
  1972  }
  1973  
  1974  // RegularFields returns the regular (non-pseudo) header fields of mh.
  1975  // The caller does not own the returned slice.
  1976  func (mh *http2MetaHeadersFrame) RegularFields() []hpack.HeaderField {
  1977  	for i, hf := range mh.Fields {
  1978  		if !hf.IsPseudo() {
  1979  			return mh.Fields[i:]
  1980  		}
  1981  	}
  1982  	return nil
  1983  }
  1984  
  1985  // PseudoFields returns the pseudo header fields of mh.
  1986  // The caller does not own the returned slice.
  1987  func (mh *http2MetaHeadersFrame) PseudoFields() []hpack.HeaderField {
  1988  	for i, hf := range mh.Fields {
  1989  		if !hf.IsPseudo() {
  1990  			return mh.Fields[:i]
  1991  		}
  1992  	}
  1993  	return mh.Fields
  1994  }
  1995  
  1996  func (mh *http2MetaHeadersFrame) checkPseudos() error {
  1997  	var isRequest, isResponse bool
  1998  	pf := mh.PseudoFields()
  1999  	for i, hf := range pf {
  2000  		switch hf.Name {
  2001  		case ":method", ":path", ":scheme", ":authority":
  2002  			isRequest = true
  2003  		case ":status":
  2004  			isResponse = true
  2005  		default:
  2006  			return http2pseudoHeaderError(hf.Name)
  2007  		}
  2008  
  2009  		for _, hf2 := range pf[:i] {
  2010  			if hf.Name == hf2.Name {
  2011  				return http2duplicatePseudoHeaderError(hf.Name)
  2012  			}
  2013  		}
  2014  	}
  2015  	if isRequest && isResponse {
  2016  		return http2errMixPseudoHeaderTypes
  2017  	}
  2018  	return nil
  2019  }
  2020  
  2021  func (fr *http2Framer) maxHeaderStringLen() int {
  2022  	v := fr.maxHeaderListSize()
  2023  	if uint32(int(v)) == v {
  2024  		return int(v)
  2025  	}
  2026  
  2027  	return 0
  2028  }
  2029  
  2030  // readMetaFrame returns 0 or more CONTINUATION frames from fr and
  2031  // merge them into into the provided hf and returns a MetaHeadersFrame
  2032  // with the decoded hpack values.
  2033  func (fr *http2Framer) readMetaFrame(hf *http2HeadersFrame) (*http2MetaHeadersFrame, error) {
  2034  	if fr.AllowIllegalReads {
  2035  		return nil, errors.New("illegal use of AllowIllegalReads with ReadMetaHeaders")
  2036  	}
  2037  	mh := &http2MetaHeadersFrame{
  2038  		http2HeadersFrame: hf,
  2039  	}
  2040  	var remainSize = fr.maxHeaderListSize()
  2041  	var sawRegular bool
  2042  
  2043  	var invalid error // pseudo header field errors
  2044  	hdec := fr.ReadMetaHeaders
  2045  	hdec.SetEmitEnabled(true)
  2046  	hdec.SetMaxStringLength(fr.maxHeaderStringLen())
  2047  	hdec.SetEmitFunc(func(hf hpack.HeaderField) {
  2048  		if http2VerboseLogs && fr.logReads {
  2049  			fr.debugReadLoggerf("http2: decoded hpack field %+v", hf)
  2050  		}
  2051  		if !httplex.ValidHeaderFieldValue(hf.Value) {
  2052  			invalid = http2headerFieldValueError(hf.Value)
  2053  		}
  2054  		isPseudo := strings.HasPrefix(hf.Name, ":")
  2055  		if isPseudo {
  2056  			if sawRegular {
  2057  				invalid = http2errPseudoAfterRegular
  2058  			}
  2059  		} else {
  2060  			sawRegular = true
  2061  			if !http2validWireHeaderFieldName(hf.Name) {
  2062  				invalid = http2headerFieldNameError(hf.Name)
  2063  			}
  2064  		}
  2065  
  2066  		if invalid != nil {
  2067  			hdec.SetEmitEnabled(false)
  2068  			return
  2069  		}
  2070  
  2071  		size := hf.Size()
  2072  		if size > remainSize {
  2073  			hdec.SetEmitEnabled(false)
  2074  			mh.Truncated = true
  2075  			return
  2076  		}
  2077  		remainSize -= size
  2078  
  2079  		mh.Fields = append(mh.Fields, hf)
  2080  	})
  2081  
  2082  	defer hdec.SetEmitFunc(func(hf hpack.HeaderField) {})
  2083  
  2084  	var hc http2headersOrContinuation = hf
  2085  	for {
  2086  		frag := hc.HeaderBlockFragment()
  2087  		if _, err := hdec.Write(frag); err != nil {
  2088  			return nil, http2ConnectionError(http2ErrCodeCompression)
  2089  		}
  2090  
  2091  		if hc.HeadersEnded() {
  2092  			break
  2093  		}
  2094  		if f, err := fr.ReadFrame(); err != nil {
  2095  			return nil, err
  2096  		} else {
  2097  			hc = f.(*http2ContinuationFrame)
  2098  		}
  2099  	}
  2100  
  2101  	mh.http2HeadersFrame.headerFragBuf = nil
  2102  	mh.http2HeadersFrame.invalidate()
  2103  
  2104  	if err := hdec.Close(); err != nil {
  2105  		return nil, http2ConnectionError(http2ErrCodeCompression)
  2106  	}
  2107  	if invalid != nil {
  2108  		fr.errDetail = invalid
  2109  		if http2VerboseLogs {
  2110  			log.Printf("http2: invalid header: %v", invalid)
  2111  		}
  2112  		return nil, http2StreamError{mh.StreamID, http2ErrCodeProtocol, invalid}
  2113  	}
  2114  	if err := mh.checkPseudos(); err != nil {
  2115  		fr.errDetail = err
  2116  		if http2VerboseLogs {
  2117  			log.Printf("http2: invalid pseudo headers: %v", err)
  2118  		}
  2119  		return nil, http2StreamError{mh.StreamID, http2ErrCodeProtocol, err}
  2120  	}
  2121  	return mh, nil
  2122  }
  2123  
  2124  func http2summarizeFrame(f http2Frame) string {
  2125  	var buf bytes.Buffer
  2126  	f.Header().writeDebug(&buf)
  2127  	switch f := f.(type) {
  2128  	case *http2SettingsFrame:
  2129  		n := 0
  2130  		f.ForeachSetting(func(s http2Setting) error {
  2131  			n++
  2132  			if n == 1 {
  2133  				buf.WriteString(", settings:")
  2134  			}
  2135  			fmt.Fprintf(&buf, " %v=%v,", s.ID, s.Val)
  2136  			return nil
  2137  		})
  2138  		if n > 0 {
  2139  			buf.Truncate(buf.Len() - 1)
  2140  		}
  2141  	case *http2DataFrame:
  2142  		data := f.Data()
  2143  		const max = 256
  2144  		if len(data) > max {
  2145  			data = data[:max]
  2146  		}
  2147  		fmt.Fprintf(&buf, " data=%q", data)
  2148  		if len(f.Data()) > max {
  2149  			fmt.Fprintf(&buf, " (%d bytes omitted)", len(f.Data())-max)
  2150  		}
  2151  	case *http2WindowUpdateFrame:
  2152  		if f.StreamID == 0 {
  2153  			buf.WriteString(" (conn)")
  2154  		}
  2155  		fmt.Fprintf(&buf, " incr=%v", f.Increment)
  2156  	case *http2PingFrame:
  2157  		fmt.Fprintf(&buf, " ping=%q", f.Data[:])
  2158  	case *http2GoAwayFrame:
  2159  		fmt.Fprintf(&buf, " LastStreamID=%v ErrCode=%v Debug=%q",
  2160  			f.LastStreamID, f.ErrCode, f.debugData)
  2161  	case *http2RSTStreamFrame:
  2162  		fmt.Fprintf(&buf, " ErrCode=%v", f.ErrCode)
  2163  	}
  2164  	return buf.String()
  2165  }
  2166  
  2167  func http2transportExpectContinueTimeout(t1 *Transport) time.Duration {
  2168  	return t1.ExpectContinueTimeout
  2169  }
  2170  
  2171  // isBadCipher reports whether the cipher is blacklisted by the HTTP/2 spec.
  2172  func http2isBadCipher(cipher uint16) bool {
  2173  	switch cipher {
  2174  	case tls.TLS_RSA_WITH_RC4_128_SHA,
  2175  		tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
  2176  		tls.TLS_RSA_WITH_AES_128_CBC_SHA,
  2177  		tls.TLS_RSA_WITH_AES_256_CBC_SHA,
  2178  		tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
  2179  		tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
  2180  		tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
  2181  		tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  2182  		tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  2183  		tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
  2184  		tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
  2185  		tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  2186  		tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
  2187  
  2188  		return true
  2189  	default:
  2190  		return false
  2191  	}
  2192  }
  2193  
  2194  type http2contextContext interface {
  2195  	context.Context
  2196  }
  2197  
  2198  func http2serverConnBaseContext(c net.Conn, opts *http2ServeConnOpts) (ctx http2contextContext, cancel func()) {
  2199  	ctx, cancel = context.WithCancel(context.Background())
  2200  	ctx = context.WithValue(ctx, LocalAddrContextKey, c.LocalAddr())
  2201  	if hs := opts.baseConfig(); hs != nil {
  2202  		ctx = context.WithValue(ctx, ServerContextKey, hs)
  2203  	}
  2204  	return
  2205  }
  2206  
  2207  func http2contextWithCancel(ctx http2contextContext) (_ http2contextContext, cancel func()) {
  2208  	return context.WithCancel(ctx)
  2209  }
  2210  
  2211  func http2requestWithContext(req *Request, ctx http2contextContext) *Request {
  2212  	return req.WithContext(ctx)
  2213  }
  2214  
  2215  type http2clientTrace httptrace.ClientTrace
  2216  
  2217  func http2reqContext(r *Request) context.Context { return r.Context() }
  2218  
  2219  func (t *http2Transport) idleConnTimeout() time.Duration {
  2220  	if t.t1 != nil {
  2221  		return t.t1.IdleConnTimeout
  2222  	}
  2223  	return 0
  2224  }
  2225  
  2226  func http2setResponseUncompressed(res *Response) { res.Uncompressed = true }
  2227  
  2228  func http2traceGotConn(req *Request, cc *http2ClientConn) {
  2229  	trace := httptrace.ContextClientTrace(req.Context())
  2230  	if trace == nil || trace.GotConn == nil {
  2231  		return
  2232  	}
  2233  	ci := httptrace.GotConnInfo{Conn: cc.tconn}
  2234  	cc.mu.Lock()
  2235  	ci.Reused = cc.nextStreamID > 1
  2236  	ci.WasIdle = len(cc.streams) == 0 && ci.Reused
  2237  	if ci.WasIdle && !cc.lastActive.IsZero() {
  2238  		ci.IdleTime = time.Now().Sub(cc.lastActive)
  2239  	}
  2240  	cc.mu.Unlock()
  2241  
  2242  	trace.GotConn(ci)
  2243  }
  2244  
  2245  func http2traceWroteHeaders(trace *http2clientTrace) {
  2246  	if trace != nil && trace.WroteHeaders != nil {
  2247  		trace.WroteHeaders()
  2248  	}
  2249  }
  2250  
  2251  func http2traceGot100Continue(trace *http2clientTrace) {
  2252  	if trace != nil && trace.Got100Continue != nil {
  2253  		trace.Got100Continue()
  2254  	}
  2255  }
  2256  
  2257  func http2traceWait100Continue(trace *http2clientTrace) {
  2258  	if trace != nil && trace.Wait100Continue != nil {
  2259  		trace.Wait100Continue()
  2260  	}
  2261  }
  2262  
  2263  func http2traceWroteRequest(trace *http2clientTrace, err error) {
  2264  	if trace != nil && trace.WroteRequest != nil {
  2265  		trace.WroteRequest(httptrace.WroteRequestInfo{Err: err})
  2266  	}
  2267  }
  2268  
  2269  func http2traceFirstResponseByte(trace *http2clientTrace) {
  2270  	if trace != nil && trace.GotFirstResponseByte != nil {
  2271  		trace.GotFirstResponseByte()
  2272  	}
  2273  }
  2274  
  2275  func http2requestTrace(req *Request) *http2clientTrace {
  2276  	trace := httptrace.ContextClientTrace(req.Context())
  2277  	return (*http2clientTrace)(trace)
  2278  }
  2279  
  2280  // Ping sends a PING frame to the server and waits for the ack.
  2281  func (cc *http2ClientConn) Ping(ctx context.Context) error {
  2282  	return cc.ping(ctx)
  2283  }
  2284  
  2285  func http2cloneTLSConfig(c *tls.Config) *tls.Config { return c.Clone() }
  2286  
  2287  var _ Pusher = (*http2responseWriter)(nil)
  2288  
  2289  // Push implements http.Pusher.
  2290  func (w *http2responseWriter) Push(target string, opts *PushOptions) error {
  2291  	internalOpts := http2pushOptions{}
  2292  	if opts != nil {
  2293  		internalOpts.Method = opts.Method
  2294  		internalOpts.Header = opts.Header
  2295  	}
  2296  	return w.push(target, internalOpts)
  2297  }
  2298  
  2299  func http2configureServer18(h1 *Server, h2 *http2Server) error {
  2300  	if h2.IdleTimeout == 0 {
  2301  		if h1.IdleTimeout != 0 {
  2302  			h2.IdleTimeout = h1.IdleTimeout
  2303  		} else {
  2304  			h2.IdleTimeout = h1.ReadTimeout
  2305  		}
  2306  	}
  2307  	return nil
  2308  }
  2309  
  2310  func http2shouldLogPanic(panicValue interface{}) bool {
  2311  	return panicValue != nil && panicValue != ErrAbortHandler
  2312  }
  2313  
  2314  func http2reqGetBody(req *Request) func() (io.ReadCloser, error) {
  2315  	return req.GetBody
  2316  }
  2317  
  2318  func http2reqBodyIsNoBody(body io.ReadCloser) bool {
  2319  	return body == NoBody
  2320  }
  2321  
  2322  var http2DebugGoroutines = os.Getenv("DEBUG_HTTP2_GOROUTINES") == "1"
  2323  
  2324  type http2goroutineLock uint64
  2325  
  2326  func http2newGoroutineLock() http2goroutineLock {
  2327  	if !http2DebugGoroutines {
  2328  		return 0
  2329  	}
  2330  	return http2goroutineLock(http2curGoroutineID())
  2331  }
  2332  
  2333  func (g http2goroutineLock) check() {
  2334  	if !http2DebugGoroutines {
  2335  		return
  2336  	}
  2337  	if http2curGoroutineID() != uint64(g) {
  2338  		panic("running on the wrong goroutine")
  2339  	}
  2340  }
  2341  
  2342  func (g http2goroutineLock) checkNotOn() {
  2343  	if !http2DebugGoroutines {
  2344  		return
  2345  	}
  2346  	if http2curGoroutineID() == uint64(g) {
  2347  		panic("running on the wrong goroutine")
  2348  	}
  2349  }
  2350  
  2351  var http2goroutineSpace = []byte("goroutine ")
  2352  
  2353  func http2curGoroutineID() uint64 {
  2354  	bp := http2littleBuf.Get().(*[]byte)
  2355  	defer http2littleBuf.Put(bp)
  2356  	b := *bp
  2357  	b = b[:runtime.Stack(b, false)]
  2358  
  2359  	b = bytes.TrimPrefix(b, http2goroutineSpace)
  2360  	i := bytes.IndexByte(b, ' ')
  2361  	if i < 0 {
  2362  		panic(fmt.Sprintf("No space found in %q", b))
  2363  	}
  2364  	b = b[:i]
  2365  	n, err := http2parseUintBytes(b, 10, 64)
  2366  	if err != nil {
  2367  		panic(fmt.Sprintf("Failed to parse goroutine ID out of %q: %v", b, err))
  2368  	}
  2369  	return n
  2370  }
  2371  
  2372  var http2littleBuf = sync.Pool{
  2373  	New: func() interface{} {
  2374  		buf := make([]byte, 64)
  2375  		return &buf
  2376  	},
  2377  }
  2378  
  2379  // parseUintBytes is like strconv.ParseUint, but using a []byte.
  2380  func http2parseUintBytes(s []byte, base int, bitSize int) (n uint64, err error) {
  2381  	var cutoff, maxVal uint64
  2382  
  2383  	if bitSize == 0 {
  2384  		bitSize = int(strconv.IntSize)
  2385  	}
  2386  
  2387  	s0 := s
  2388  	switch {
  2389  	case len(s) < 1:
  2390  		err = strconv.ErrSyntax
  2391  		goto Error
  2392  
  2393  	case 2 <= base && base <= 36:
  2394  
  2395  	case base == 0:
  2396  
  2397  		switch {
  2398  		case s[0] == '0' && len(s) > 1 && (s[1] == 'x' || s[1] == 'X'):
  2399  			base = 16
  2400  			s = s[2:]
  2401  			if len(s) < 1 {
  2402  				err = strconv.ErrSyntax
  2403  				goto Error
  2404  			}
  2405  		case s[0] == '0':
  2406  			base = 8
  2407  		default:
  2408  			base = 10
  2409  		}
  2410  
  2411  	default:
  2412  		err = errors.New("invalid base " + strconv.Itoa(base))
  2413  		goto Error
  2414  	}
  2415  
  2416  	n = 0
  2417  	cutoff = http2cutoff64(base)
  2418  	maxVal = 1<<uint(bitSize) - 1
  2419  
  2420  	for i := 0; i < len(s); i++ {
  2421  		var v byte
  2422  		d := s[i]
  2423  		switch {
  2424  		case '0' <= d && d <= '9':
  2425  			v = d - '0'
  2426  		case 'a' <= d && d <= 'z':
  2427  			v = d - 'a' + 10
  2428  		case 'A' <= d && d <= 'Z':
  2429  			v = d - 'A' + 10
  2430  		default:
  2431  			n = 0
  2432  			err = strconv.ErrSyntax
  2433  			goto Error
  2434  		}
  2435  		if int(v) >= base {
  2436  			n = 0
  2437  			err = strconv.ErrSyntax
  2438  			goto Error
  2439  		}
  2440  
  2441  		if n >= cutoff {
  2442  
  2443  			n = 1<<64 - 1
  2444  			err = strconv.ErrRange
  2445  			goto Error
  2446  		}
  2447  		n *= uint64(base)
  2448  
  2449  		n1 := n + uint64(v)
  2450  		if n1 < n || n1 > maxVal {
  2451  
  2452  			n = 1<<64 - 1
  2453  			err = strconv.ErrRange
  2454  			goto Error
  2455  		}
  2456  		n = n1
  2457  	}
  2458  
  2459  	return n, nil
  2460  
  2461  Error:
  2462  	return n, &strconv.NumError{Func: "ParseUint", Num: string(s0), Err: err}
  2463  }
  2464  
  2465  // Return the first number n such that n*base >= 1<<64.
  2466  func http2cutoff64(base int) uint64 {
  2467  	if base < 2 {
  2468  		return 0
  2469  	}
  2470  	return (1<<64-1)/uint64(base) + 1
  2471  }
  2472  
  2473  var (
  2474  	http2commonLowerHeader = map[string]string{} // Go-Canonical-Case -> lower-case
  2475  	http2commonCanonHeader = map[string]string{} // lower-case -> Go-Canonical-Case
  2476  )
  2477  
  2478  func init() {
  2479  	for _, v := range []string{
  2480  		"accept",
  2481  		"accept-charset",
  2482  		"accept-encoding",
  2483  		"accept-language",
  2484  		"accept-ranges",
  2485  		"age",
  2486  		"access-control-allow-origin",
  2487  		"allow",
  2488  		"authorization",
  2489  		"cache-control",
  2490  		"content-disposition",
  2491  		"content-encoding",
  2492  		"content-language",
  2493  		"content-length",
  2494  		"content-location",
  2495  		"content-range",
  2496  		"content-type",
  2497  		"cookie",
  2498  		"date",
  2499  		"etag",
  2500  		"expect",
  2501  		"expires",
  2502  		"from",
  2503  		"host",
  2504  		"if-match",
  2505  		"if-modified-since",
  2506  		"if-none-match",
  2507  		"if-unmodified-since",
  2508  		"last-modified",
  2509  		"link",
  2510  		"location",
  2511  		"max-forwards",
  2512  		"proxy-authenticate",
  2513  		"proxy-authorization",
  2514  		"range",
  2515  		"referer",
  2516  		"refresh",
  2517  		"retry-after",
  2518  		"server",
  2519  		"set-cookie",
  2520  		"strict-transport-security",
  2521  		"trailer",
  2522  		"transfer-encoding",
  2523  		"user-agent",
  2524  		"vary",
  2525  		"via",
  2526  		"www-authenticate",
  2527  	} {
  2528  		chk := CanonicalHeaderKey(v)
  2529  		http2commonLowerHeader[chk] = v
  2530  		http2commonCanonHeader[v] = chk
  2531  	}
  2532  }
  2533  
  2534  func http2lowerHeader(v string) string {
  2535  	if s, ok := http2commonLowerHeader[v]; ok {
  2536  		return s
  2537  	}
  2538  	return strings.ToLower(v)
  2539  }
  2540  
  2541  var (
  2542  	http2VerboseLogs    bool
  2543  	http2logFrameWrites bool
  2544  	http2logFrameReads  bool
  2545  	http2inTests        bool
  2546  )
  2547  
  2548  func init() {
  2549  	e := os.Getenv("GODEBUG")
  2550  	if strings.Contains(e, "http2debug=1") {
  2551  		http2VerboseLogs = true
  2552  	}
  2553  	if strings.Contains(e, "http2debug=2") {
  2554  		http2VerboseLogs = true
  2555  		http2logFrameWrites = true
  2556  		http2logFrameReads = true
  2557  	}
  2558  }
  2559  
  2560  const (
  2561  	// ClientPreface is the string that must be sent by new
  2562  	// connections from clients.
  2563  	http2ClientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
  2564  
  2565  	// SETTINGS_MAX_FRAME_SIZE default
  2566  	// http://http2.github.io/http2-spec/#rfc.section.6.5.2
  2567  	http2initialMaxFrameSize = 16384
  2568  
  2569  	// NextProtoTLS is the NPN/ALPN protocol negotiated during
  2570  	// HTTP/2's TLS setup.
  2571  	http2NextProtoTLS = "h2"
  2572  
  2573  	// http://http2.github.io/http2-spec/#SettingValues
  2574  	http2initialHeaderTableSize = 4096
  2575  
  2576  	http2initialWindowSize = 65535 // 6.9.2 Initial Flow Control Window Size
  2577  
  2578  	http2defaultMaxReadFrameSize = 1 << 20
  2579  )
  2580  
  2581  var (
  2582  	http2clientPreface = []byte(http2ClientPreface)
  2583  )
  2584  
  2585  type http2streamState int
  2586  
  2587  // HTTP/2 stream states.
  2588  //
  2589  // See http://tools.ietf.org/html/rfc7540#section-5.1.
  2590  //
  2591  // For simplicity, the server code merges "reserved (local)" into
  2592  // "half-closed (remote)". This is one less state transition to track.
  2593  // The only downside is that we send PUSH_PROMISEs slightly less
  2594  // liberally than allowable. More discussion here:
  2595  // https://lists.w3.org/Archives/Public/ietf-http-wg/2016JulSep/0599.html
  2596  //
  2597  // "reserved (remote)" is omitted since the client code does not
  2598  // support server push.
  2599  const (
  2600  	http2stateIdle http2streamState = iota
  2601  	http2stateOpen
  2602  	http2stateHalfClosedLocal
  2603  	http2stateHalfClosedRemote
  2604  	http2stateClosed
  2605  )
  2606  
  2607  var http2stateName = [...]string{
  2608  	http2stateIdle:             "Idle",
  2609  	http2stateOpen:             "Open",
  2610  	http2stateHalfClosedLocal:  "HalfClosedLocal",
  2611  	http2stateHalfClosedRemote: "HalfClosedRemote",
  2612  	http2stateClosed:           "Closed",
  2613  }
  2614  
  2615  func (st http2streamState) String() string {
  2616  	return http2stateName[st]
  2617  }
  2618  
  2619  // Setting is a setting parameter: which setting it is, and its value.
  2620  type http2Setting struct {
  2621  	// ID is which setting is being set.
  2622  	// See http://http2.github.io/http2-spec/#SettingValues
  2623  	ID http2SettingID
  2624  
  2625  	// Val is the value.
  2626  	Val uint32
  2627  }
  2628  
  2629  func (s http2Setting) String() string {
  2630  	return fmt.Sprintf("[%v = %d]", s.ID, s.Val)
  2631  }
  2632  
  2633  // Valid reports whether the setting is valid.
  2634  func (s http2Setting) Valid() error {
  2635  
  2636  	switch s.ID {
  2637  	case http2SettingEnablePush:
  2638  		if s.Val != 1 && s.Val != 0 {
  2639  			return http2ConnectionError(http2ErrCodeProtocol)
  2640  		}
  2641  	case http2SettingInitialWindowSize:
  2642  		if s.Val > 1<<31-1 {
  2643  			return http2ConnectionError(http2ErrCodeFlowControl)
  2644  		}
  2645  	case http2SettingMaxFrameSize:
  2646  		if s.Val < 16384 || s.Val > 1<<24-1 {
  2647  			return http2ConnectionError(http2ErrCodeProtocol)
  2648  		}
  2649  	}
  2650  	return nil
  2651  }
  2652  
  2653  // A SettingID is an HTTP/2 setting as defined in
  2654  // http://http2.github.io/http2-spec/#iana-settings
  2655  type http2SettingID uint16
  2656  
  2657  const (
  2658  	http2SettingHeaderTableSize      http2SettingID = 0x1
  2659  	http2SettingEnablePush           http2SettingID = 0x2
  2660  	http2SettingMaxConcurrentStreams http2SettingID = 0x3
  2661  	http2SettingInitialWindowSize    http2SettingID = 0x4
  2662  	http2SettingMaxFrameSize         http2SettingID = 0x5
  2663  	http2SettingMaxHeaderListSize    http2SettingID = 0x6
  2664  )
  2665  
  2666  var http2settingName = map[http2SettingID]string{
  2667  	http2SettingHeaderTableSize:      "HEADER_TABLE_SIZE",
  2668  	http2SettingEnablePush:           "ENABLE_PUSH",
  2669  	http2SettingMaxConcurrentStreams: "MAX_CONCURRENT_STREAMS",
  2670  	http2SettingInitialWindowSize:    "INITIAL_WINDOW_SIZE",
  2671  	http2SettingMaxFrameSize:         "MAX_FRAME_SIZE",
  2672  	http2SettingMaxHeaderListSize:    "MAX_HEADER_LIST_SIZE",
  2673  }
  2674  
  2675  func (s http2SettingID) String() string {
  2676  	if v, ok := http2settingName[s]; ok {
  2677  		return v
  2678  	}
  2679  	return fmt.Sprintf("UNKNOWN_SETTING_%d", uint16(s))
  2680  }
  2681  
  2682  var (
  2683  	http2errInvalidHeaderFieldName  = errors.New("http2: invalid header field name")
  2684  	http2errInvalidHeaderFieldValue = errors.New("http2: invalid header field value")
  2685  )
  2686  
  2687  // validWireHeaderFieldName reports whether v is a valid header field
  2688  // name (key). See httplex.ValidHeaderName for the base rules.
  2689  //
  2690  // Further, http2 says:
  2691  //   "Just as in HTTP/1.x, header field names are strings of ASCII
  2692  //   characters that are compared in a case-insensitive
  2693  //   fashion. However, header field names MUST be converted to
  2694  //   lowercase prior to their encoding in HTTP/2. "
  2695  func http2validWireHeaderFieldName(v string) bool {
  2696  	if len(v) == 0 {
  2697  		return false
  2698  	}
  2699  	for _, r := range v {
  2700  		if !httplex.IsTokenRune(r) {
  2701  			return false
  2702  		}
  2703  		if 'A' <= r && r <= 'Z' {
  2704  			return false
  2705  		}
  2706  	}
  2707  	return true
  2708  }
  2709  
  2710  var http2httpCodeStringCommon = map[int]string{} // n -> strconv.Itoa(n)
  2711  
  2712  func init() {
  2713  	for i := 100; i <= 999; i++ {
  2714  		if v := StatusText(i); v != "" {
  2715  			http2httpCodeStringCommon[i] = strconv.Itoa(i)
  2716  		}
  2717  	}
  2718  }
  2719  
  2720  func http2httpCodeString(code int) string {
  2721  	if s, ok := http2httpCodeStringCommon[code]; ok {
  2722  		return s
  2723  	}
  2724  	return strconv.Itoa(code)
  2725  }
  2726  
  2727  // from pkg io
  2728  type http2stringWriter interface {
  2729  	WriteString(s string) (n int, err error)
  2730  }
  2731  
  2732  // A gate lets two goroutines coordinate their activities.
  2733  type http2gate chan struct{}
  2734  
  2735  func (g http2gate) Done() { g <- struct{}{} }
  2736  
  2737  func (g http2gate) Wait() { <-g }
  2738  
  2739  // A closeWaiter is like a sync.WaitGroup but only goes 1 to 0 (open to closed).
  2740  type http2closeWaiter chan struct{}
  2741  
  2742  // Init makes a closeWaiter usable.
  2743  // It exists because so a closeWaiter value can be placed inside a
  2744  // larger struct and have the Mutex and Cond's memory in the same
  2745  // allocation.
  2746  func (cw *http2closeWaiter) Init() {
  2747  	*cw = make(chan struct{})
  2748  }
  2749  
  2750  // Close marks the closeWaiter as closed and unblocks any waiters.
  2751  func (cw http2closeWaiter) Close() {
  2752  	close(cw)
  2753  }
  2754  
  2755  // Wait waits for the closeWaiter to become closed.
  2756  func (cw http2closeWaiter) Wait() {
  2757  	<-cw
  2758  }
  2759  
  2760  // bufferedWriter is a buffered writer that writes to w.
  2761  // Its buffered writer is lazily allocated as needed, to minimize
  2762  // idle memory usage with many connections.
  2763  type http2bufferedWriter struct {
  2764  	w  io.Writer     // immutable
  2765  	bw *bufio.Writer // non-nil when data is buffered
  2766  }
  2767  
  2768  func http2newBufferedWriter(w io.Writer) *http2bufferedWriter {
  2769  	return &http2bufferedWriter{w: w}
  2770  }
  2771  
  2772  // bufWriterPoolBufferSize is the size of bufio.Writer's
  2773  // buffers created using bufWriterPool.
  2774  //
  2775  // TODO: pick a less arbitrary value? this is a bit under
  2776  // (3 x typical 1500 byte MTU) at least. Other than that,
  2777  // not much thought went into it.
  2778  const http2bufWriterPoolBufferSize = 4 << 10
  2779  
  2780  var http2bufWriterPool = sync.Pool{
  2781  	New: func() interface{} {
  2782  		return bufio.NewWriterSize(nil, http2bufWriterPoolBufferSize)
  2783  	},
  2784  }
  2785  
  2786  func (w *http2bufferedWriter) Available() int {
  2787  	if w.bw == nil {
  2788  		return http2bufWriterPoolBufferSize
  2789  	}
  2790  	return w.bw.Available()
  2791  }
  2792  
  2793  func (w *http2bufferedWriter) Write(p []byte) (n int, err error) {
  2794  	if w.bw == nil {
  2795  		bw := http2bufWriterPool.Get().(*bufio.Writer)
  2796  		bw.Reset(w.w)
  2797  		w.bw = bw
  2798  	}
  2799  	return w.bw.Write(p)
  2800  }
  2801  
  2802  func (w *http2bufferedWriter) Flush() error {
  2803  	bw := w.bw
  2804  	if bw == nil {
  2805  		return nil
  2806  	}
  2807  	err := bw.Flush()
  2808  	bw.Reset(nil)
  2809  	http2bufWriterPool.Put(bw)
  2810  	w.bw = nil
  2811  	return err
  2812  }
  2813  
  2814  func http2mustUint31(v int32) uint32 {
  2815  	if v < 0 || v > 2147483647 {
  2816  		panic("out of range")
  2817  	}
  2818  	return uint32(v)
  2819  }
  2820  
  2821  // bodyAllowedForStatus reports whether a given response status code
  2822  // permits a body. See RFC 2616, section 4.4.
  2823  func http2bodyAllowedForStatus(status int) bool {
  2824  	switch {
  2825  	case status >= 100 && status <= 199:
  2826  		return false
  2827  	case status == 204:
  2828  		return false
  2829  	case status == 304:
  2830  		return false
  2831  	}
  2832  	return true
  2833  }
  2834  
  2835  type http2httpError struct {
  2836  	msg     string
  2837  	timeout bool
  2838  }
  2839  
  2840  func (e *http2httpError) Error() string { return e.msg }
  2841  
  2842  func (e *http2httpError) Timeout() bool { return e.timeout }
  2843  
  2844  func (e *http2httpError) Temporary() bool { return true }
  2845  
  2846  var http2errTimeout error = &http2httpError{msg: "http2: timeout awaiting response headers", timeout: true}
  2847  
  2848  type http2connectionStater interface {
  2849  	ConnectionState() tls.ConnectionState
  2850  }
  2851  
  2852  var http2sorterPool = sync.Pool{New: func() interface{} { return new(http2sorter) }}
  2853  
  2854  type http2sorter struct {
  2855  	v []string // owned by sorter
  2856  }
  2857  
  2858  func (s *http2sorter) Len() int { return len(s.v) }
  2859  
  2860  func (s *http2sorter) Swap(i, j int) { s.v[i], s.v[j] = s.v[j], s.v[i] }
  2861  
  2862  func (s *http2sorter) Less(i, j int) bool { return s.v[i] < s.v[j] }
  2863  
  2864  // Keys returns the sorted keys of h.
  2865  //
  2866  // The returned slice is only valid until s used again or returned to
  2867  // its pool.
  2868  func (s *http2sorter) Keys(h Header) []string {
  2869  	keys := s.v[:0]
  2870  	for k := range h {
  2871  		keys = append(keys, k)
  2872  	}
  2873  	s.v = keys
  2874  	sort.Sort(s)
  2875  	return keys
  2876  }
  2877  
  2878  func (s *http2sorter) SortStrings(ss []string) {
  2879  
  2880  	save := s.v
  2881  	s.v = ss
  2882  	sort.Sort(s)
  2883  	s.v = save
  2884  }
  2885  
  2886  // validPseudoPath reports whether v is a valid :path pseudo-header
  2887  // value. It must be either:
  2888  //
  2889  //     *) a non-empty string starting with '/', but not with with "//",
  2890  //     *) the string '*', for OPTIONS requests.
  2891  //
  2892  // For now this is only used a quick check for deciding when to clean
  2893  // up Opaque URLs before sending requests from the Transport.
  2894  // See golang.org/issue/16847
  2895  func http2validPseudoPath(v string) bool {
  2896  	return (len(v) > 0 && v[0] == '/' && (len(v) == 1 || v[1] != '/')) || v == "*"
  2897  }
  2898  
  2899  // pipe is a goroutine-safe io.Reader/io.Writer pair. It's like
  2900  // io.Pipe except there are no PipeReader/PipeWriter halves, and the
  2901  // underlying buffer is an interface. (io.Pipe is always unbuffered)
  2902  type http2pipe struct {
  2903  	mu       sync.Mutex
  2904  	c        sync.Cond // c.L lazily initialized to &p.mu
  2905  	b        http2pipeBuffer
  2906  	err      error         // read error once empty. non-nil means closed.
  2907  	breakErr error         // immediate read error (caller doesn't see rest of b)
  2908  	donec    chan struct{} // closed on error
  2909  	readFn   func()        // optional code to run in Read before error
  2910  }
  2911  
  2912  type http2pipeBuffer interface {
  2913  	Len() int
  2914  	io.Writer
  2915  	io.Reader
  2916  }
  2917  
  2918  func (p *http2pipe) Len() int {
  2919  	p.mu.Lock()
  2920  	defer p.mu.Unlock()
  2921  	return p.b.Len()
  2922  }
  2923  
  2924  // Read waits until data is available and copies bytes
  2925  // from the buffer into p.
  2926  func (p *http2pipe) Read(d []byte) (n int, err error) {
  2927  	p.mu.Lock()
  2928  	defer p.mu.Unlock()
  2929  	if p.c.L == nil {
  2930  		p.c.L = &p.mu
  2931  	}
  2932  	for {
  2933  		if p.breakErr != nil {
  2934  			return 0, p.breakErr
  2935  		}
  2936  		if p.b.Len() > 0 {
  2937  			return p.b.Read(d)
  2938  		}
  2939  		if p.err != nil {
  2940  			if p.readFn != nil {
  2941  				p.readFn()
  2942  				p.readFn = nil
  2943  			}
  2944  			return 0, p.err
  2945  		}
  2946  		p.c.Wait()
  2947  	}
  2948  }
  2949  
  2950  var http2errClosedPipeWrite = errors.New("write on closed buffer")
  2951  
  2952  // Write copies bytes from p into the buffer and wakes a reader.
  2953  // It is an error to write more data than the buffer can hold.
  2954  func (p *http2pipe) Write(d []byte) (n int, err error) {
  2955  	p.mu.Lock()
  2956  	defer p.mu.Unlock()
  2957  	if p.c.L == nil {
  2958  		p.c.L = &p.mu
  2959  	}
  2960  	defer p.c.Signal()
  2961  	if p.err != nil {
  2962  		return 0, http2errClosedPipeWrite
  2963  	}
  2964  	return p.b.Write(d)
  2965  }
  2966  
  2967  // CloseWithError causes the next Read (waking up a current blocked
  2968  // Read if needed) to return the provided err after all data has been
  2969  // read.
  2970  //
  2971  // The error must be non-nil.
  2972  func (p *http2pipe) CloseWithError(err error) { p.closeWithError(&p.err, err, nil) }
  2973  
  2974  // BreakWithError causes the next Read (waking up a current blocked
  2975  // Read if needed) to return the provided err immediately, without
  2976  // waiting for unread data.
  2977  func (p *http2pipe) BreakWithError(err error) { p.closeWithError(&p.breakErr, err, nil) }
  2978  
  2979  // closeWithErrorAndCode is like CloseWithError but also sets some code to run
  2980  // in the caller's goroutine before returning the error.
  2981  func (p *http2pipe) closeWithErrorAndCode(err error, fn func()) { p.closeWithError(&p.err, err, fn) }
  2982  
  2983  func (p *http2pipe) closeWithError(dst *error, err error, fn func()) {
  2984  	if err == nil {
  2985  		panic("err must be non-nil")
  2986  	}
  2987  	p.mu.Lock()
  2988  	defer p.mu.Unlock()
  2989  	if p.c.L == nil {
  2990  		p.c.L = &p.mu
  2991  	}
  2992  	defer p.c.Signal()
  2993  	if *dst != nil {
  2994  
  2995  		return
  2996  	}
  2997  	p.readFn = fn
  2998  	*dst = err
  2999  	p.closeDoneLocked()
  3000  }
  3001  
  3002  // requires p.mu be held.
  3003  func (p *http2pipe) closeDoneLocked() {
  3004  	if p.donec == nil {
  3005  		return
  3006  	}
  3007  
  3008  	select {
  3009  	case <-p.donec:
  3010  	default:
  3011  		close(p.donec)
  3012  	}
  3013  }
  3014  
  3015  // Err returns the error (if any) first set by BreakWithError or CloseWithError.
  3016  func (p *http2pipe) Err() error {
  3017  	p.mu.Lock()
  3018  	defer p.mu.Unlock()
  3019  	if p.breakErr != nil {
  3020  		return p.breakErr
  3021  	}
  3022  	return p.err
  3023  }
  3024  
  3025  // Done returns a channel which is closed if and when this pipe is closed
  3026  // with CloseWithError.
  3027  func (p *http2pipe) Done() <-chan struct{} {
  3028  	p.mu.Lock()
  3029  	defer p.mu.Unlock()
  3030  	if p.donec == nil {
  3031  		p.donec = make(chan struct{})
  3032  		if p.err != nil || p.breakErr != nil {
  3033  
  3034  			p.closeDoneLocked()
  3035  		}
  3036  	}
  3037  	return p.donec
  3038  }
  3039  
  3040  const (
  3041  	http2prefaceTimeout        = 10 * time.Second
  3042  	http2firstSettingsTimeout  = 2 * time.Second // should be in-flight with preface anyway
  3043  	http2handlerChunkWriteSize = 4 << 10
  3044  	http2defaultMaxStreams     = 250 // TODO: make this 100 as the GFE seems to?
  3045  )
  3046  
  3047  var (
  3048  	http2errClientDisconnected = errors.New("client disconnected")
  3049  	http2errClosedBody         = errors.New("body closed by handler")
  3050  	http2errHandlerComplete    = errors.New("http2: request body closed due to handler exiting")
  3051  	http2errStreamClosed       = errors.New("http2: stream closed")
  3052  )
  3053  
  3054  var http2responseWriterStatePool = sync.Pool{
  3055  	New: func() interface{} {
  3056  		rws := &http2responseWriterState{}
  3057  		rws.bw = bufio.NewWriterSize(http2chunkWriter{rws}, http2handlerChunkWriteSize)
  3058  		return rws
  3059  	},
  3060  }
  3061  
  3062  // Test hooks.
  3063  var (
  3064  	http2testHookOnConn        func()
  3065  	http2testHookGetServerConn func(*http2serverConn)
  3066  	http2testHookOnPanicMu     *sync.Mutex // nil except in tests
  3067  	http2testHookOnPanic       func(sc *http2serverConn, panicVal interface{}) (rePanic bool)
  3068  )
  3069  
  3070  // Server is an HTTP/2 server.
  3071  type http2Server struct {
  3072  	// MaxHandlers limits the number of http.Handler ServeHTTP goroutines
  3073  	// which may run at a time over all connections.
  3074  	// Negative or zero no limit.
  3075  	// TODO: implement
  3076  	MaxHandlers int
  3077  
  3078  	// MaxConcurrentStreams optionally specifies the number of
  3079  	// concurrent streams that each client may have open at a
  3080  	// time. This is unrelated to the number of http.Handler goroutines
  3081  	// which may be active globally, which is MaxHandlers.
  3082  	// If zero, MaxConcurrentStreams defaults to at least 100, per
  3083  	// the HTTP/2 spec's recommendations.
  3084  	MaxConcurrentStreams uint32
  3085  
  3086  	// MaxReadFrameSize optionally specifies the largest frame
  3087  	// this server is willing to read. A valid value is between
  3088  	// 16k and 16M, inclusive. If zero or otherwise invalid, a
  3089  	// default value is used.
  3090  	MaxReadFrameSize uint32
  3091  
  3092  	// PermitProhibitedCipherSuites, if true, permits the use of
  3093  	// cipher suites prohibited by the HTTP/2 spec.
  3094  	PermitProhibitedCipherSuites bool
  3095  
  3096  	// IdleTimeout specifies how long until idle clients should be
  3097  	// closed with a GOAWAY frame. PING frames are not considered
  3098  	// activity for the purposes of IdleTimeout.
  3099  	IdleTimeout time.Duration
  3100  
  3101  	// MaxUploadBufferPerConnection is the size of the initial flow
  3102  	// control window for each connections. The HTTP/2 spec does not
  3103  	// allow this to be smaller than 65535 or larger than 2^32-1.
  3104  	// If the value is outside this range, a default value will be
  3105  	// used instead.
  3106  	MaxUploadBufferPerConnection int32
  3107  
  3108  	// MaxUploadBufferPerStream is the size of the initial flow control
  3109  	// window for each stream. The HTTP/2 spec does not allow this to
  3110  	// be larger than 2^32-1. If the value is zero or larger than the
  3111  	// maximum, a default value will be used instead.
  3112  	MaxUploadBufferPerStream int32
  3113  
  3114  	// NewWriteScheduler constructs a write scheduler for a connection.
  3115  	// If nil, a default scheduler is chosen.
  3116  	NewWriteScheduler func() http2WriteScheduler
  3117  }
  3118  
  3119  func (s *http2Server) initialConnRecvWindowSize() int32 {
  3120  	if s.MaxUploadBufferPerConnection > http2initialWindowSize {
  3121  		return s.MaxUploadBufferPerConnection
  3122  	}
  3123  	return 1 << 20
  3124  }
  3125  
  3126  func (s *http2Server) initialStreamRecvWindowSize() int32 {
  3127  	if s.MaxUploadBufferPerStream > 0 {
  3128  		return s.MaxUploadBufferPerStream
  3129  	}
  3130  	return 1 << 20
  3131  }
  3132  
  3133  func (s *http2Server) maxReadFrameSize() uint32 {
  3134  	if v := s.MaxReadFrameSize; v >= http2minMaxFrameSize && v <= http2maxFrameSize {
  3135  		return v
  3136  	}
  3137  	return http2defaultMaxReadFrameSize
  3138  }
  3139  
  3140  func (s *http2Server) maxConcurrentStreams() uint32 {
  3141  	if v := s.MaxConcurrentStreams; v > 0 {
  3142  		return v
  3143  	}
  3144  	return http2defaultMaxStreams
  3145  }
  3146  
  3147  // ConfigureServer adds HTTP/2 support to a net/http Server.
  3148  //
  3149  // The configuration conf may be nil.
  3150  //
  3151  // ConfigureServer must be called before s begins serving.
  3152  func http2ConfigureServer(s *Server, conf *http2Server) error {
  3153  	if s == nil {
  3154  		panic("nil *http.Server")
  3155  	}
  3156  	if conf == nil {
  3157  		conf = new(http2Server)
  3158  	}
  3159  	if err := http2configureServer18(s, conf); err != nil {
  3160  		return err
  3161  	}
  3162  
  3163  	if s.TLSConfig == nil {
  3164  		s.TLSConfig = new(tls.Config)
  3165  	} else if s.TLSConfig.CipherSuites != nil {
  3166  		// If they already provided a CipherSuite list, return
  3167  		// an error if it has a bad order or is missing
  3168  		// ECDHE_RSA_WITH_AES_128_GCM_SHA256.
  3169  		const requiredCipher = tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  3170  		haveRequired := false
  3171  		sawBad := false
  3172  		for i, cs := range s.TLSConfig.CipherSuites {
  3173  			if cs == requiredCipher {
  3174  				haveRequired = true
  3175  			}
  3176  			if http2isBadCipher(cs) {
  3177  				sawBad = true
  3178  			} else if sawBad {
  3179  				return fmt.Errorf("http2: TLSConfig.CipherSuites index %d contains an HTTP/2-approved cipher suite (%#04x), but it comes after unapproved cipher suites. With this configuration, clients that don't support previous, approved cipher suites may be given an unapproved one and reject the connection.", i, cs)
  3180  			}
  3181  		}
  3182  		if !haveRequired {
  3183  			return fmt.Errorf("http2: TLSConfig.CipherSuites is missing HTTP/2-required TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256")
  3184  		}
  3185  	}
  3186  
  3187  	s.TLSConfig.PreferServerCipherSuites = true
  3188  
  3189  	haveNPN := false
  3190  	for _, p := range s.TLSConfig.NextProtos {
  3191  		if p == http2NextProtoTLS {
  3192  			haveNPN = true
  3193  			break
  3194  		}
  3195  	}
  3196  	if !haveNPN {
  3197  		s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, http2NextProtoTLS)
  3198  	}
  3199  
  3200  	if s.TLSNextProto == nil {
  3201  		s.TLSNextProto = map[string]func(*Server, *tls.Conn, Handler){}
  3202  	}
  3203  	protoHandler := func(hs *Server, c *tls.Conn, h Handler) {
  3204  		if http2testHookOnConn != nil {
  3205  			http2testHookOnConn()
  3206  		}
  3207  		conf.ServeConn(c, &http2ServeConnOpts{
  3208  			Handler:    h,
  3209  			BaseConfig: hs,
  3210  		})
  3211  	}
  3212  	s.TLSNextProto[http2NextProtoTLS] = protoHandler
  3213  	return nil
  3214  }
  3215  
  3216  // ServeConnOpts are options for the Server.ServeConn method.
  3217  type http2ServeConnOpts struct {
  3218  	// BaseConfig optionally sets the base configuration
  3219  	// for values. If nil, defaults are used.
  3220  	BaseConfig *Server
  3221  
  3222  	// Handler specifies which handler to use for processing
  3223  	// requests. If nil, BaseConfig.Handler is used. If BaseConfig
  3224  	// or BaseConfig.Handler is nil, http.DefaultServeMux is used.
  3225  	Handler Handler
  3226  }
  3227  
  3228  func (o *http2ServeConnOpts) baseConfig() *Server {
  3229  	if o != nil && o.BaseConfig != nil {
  3230  		return o.BaseConfig
  3231  	}
  3232  	return new(Server)
  3233  }
  3234  
  3235  func (o *http2ServeConnOpts) handler() Handler {
  3236  	if o != nil {
  3237  		if o.Handler != nil {
  3238  			return o.Handler
  3239  		}
  3240  		if o.BaseConfig != nil && o.BaseConfig.Handler != nil {
  3241  			return o.BaseConfig.Handler
  3242  		}
  3243  	}
  3244  	return DefaultServeMux
  3245  }
  3246  
  3247  // ServeConn serves HTTP/2 requests on the provided connection and
  3248  // blocks until the connection is no longer readable.
  3249  //
  3250  // ServeConn starts speaking HTTP/2 assuming that c has not had any
  3251  // reads or writes. It writes its initial settings frame and expects
  3252  // to be able to read the preface and settings frame from the
  3253  // client. If c has a ConnectionState method like a *tls.Conn, the
  3254  // ConnectionState is used to verify the TLS ciphersuite and to set
  3255  // the Request.TLS field in Handlers.
  3256  //
  3257  // ServeConn does not support h2c by itself. Any h2c support must be
  3258  // implemented in terms of providing a suitably-behaving net.Conn.
  3259  //
  3260  // The opts parameter is optional. If nil, default values are used.
  3261  func (s *http2Server) ServeConn(c net.Conn, opts *http2ServeConnOpts) {
  3262  	baseCtx, cancel := http2serverConnBaseContext(c, opts)
  3263  	defer cancel()
  3264  
  3265  	sc := &http2serverConn{
  3266  		srv:                         s,
  3267  		hs:                          opts.baseConfig(),
  3268  		conn:                        c,
  3269  		baseCtx:                     baseCtx,
  3270  		remoteAddrStr:               c.RemoteAddr().String(),
  3271  		bw:                          http2newBufferedWriter(c),
  3272  		handler:                     opts.handler(),
  3273  		streams:                     make(map[uint32]*http2stream),
  3274  		readFrameCh:                 make(chan http2readFrameResult),
  3275  		wantWriteFrameCh:            make(chan http2FrameWriteRequest, 8),
  3276  		wantStartPushCh:             make(chan http2startPushRequest, 8),
  3277  		wroteFrameCh:                make(chan http2frameWriteResult, 1),
  3278  		bodyReadCh:                  make(chan http2bodyReadMsg),
  3279  		doneServing:                 make(chan struct{}),
  3280  		clientMaxStreams:            math.MaxUint32,
  3281  		advMaxStreams:               s.maxConcurrentStreams(),
  3282  		initialStreamSendWindowSize: http2initialWindowSize,
  3283  		maxFrameSize:                http2initialMaxFrameSize,
  3284  		headerTableSize:             http2initialHeaderTableSize,
  3285  		serveG:                      http2newGoroutineLock(),
  3286  		pushEnabled:                 true,
  3287  	}
  3288  
  3289  	if sc.hs.WriteTimeout != 0 {
  3290  		sc.conn.SetWriteDeadline(time.Time{})
  3291  	}
  3292  
  3293  	if s.NewWriteScheduler != nil {
  3294  		sc.writeSched = s.NewWriteScheduler()
  3295  	} else {
  3296  		sc.writeSched = http2NewRandomWriteScheduler()
  3297  	}
  3298  
  3299  	sc.flow.add(http2initialWindowSize)
  3300  	sc.inflow.add(http2initialWindowSize)
  3301  	sc.hpackEncoder = hpack.NewEncoder(&sc.headerWriteBuf)
  3302  
  3303  	fr := http2NewFramer(sc.bw, c)
  3304  	fr.ReadMetaHeaders = hpack.NewDecoder(http2initialHeaderTableSize, nil)
  3305  	fr.MaxHeaderListSize = sc.maxHeaderListSize()
  3306  	fr.SetMaxReadFrameSize(s.maxReadFrameSize())
  3307  	sc.framer = fr
  3308  
  3309  	if tc, ok := c.(http2connectionStater); ok {
  3310  		sc.tlsState = new(tls.ConnectionState)
  3311  		*sc.tlsState = tc.ConnectionState()
  3312  
  3313  		if sc.tlsState.Version < tls.VersionTLS12 {
  3314  			sc.rejectConn(http2ErrCodeInadequateSecurity, "TLS version too low")
  3315  			return
  3316  		}
  3317  
  3318  		if sc.tlsState.ServerName == "" {
  3319  
  3320  		}
  3321  
  3322  		if !s.PermitProhibitedCipherSuites && http2isBadCipher(sc.tlsState.CipherSuite) {
  3323  
  3324  			sc.rejectConn(http2ErrCodeInadequateSecurity, fmt.Sprintf("Prohibited TLS 1.2 Cipher Suite: %x", sc.tlsState.CipherSuite))
  3325  			return
  3326  		}
  3327  	}
  3328  
  3329  	if hook := http2testHookGetServerConn; hook != nil {
  3330  		hook(sc)
  3331  	}
  3332  	sc.serve()
  3333  }
  3334  
  3335  func (sc *http2serverConn) rejectConn(err http2ErrCode, debug string) {
  3336  	sc.vlogf("http2: server rejecting conn: %v, %s", err, debug)
  3337  
  3338  	sc.framer.WriteGoAway(0, err, []byte(debug))
  3339  	sc.bw.Flush()
  3340  	sc.conn.Close()
  3341  }
  3342  
  3343  type http2serverConn struct {
  3344  	// Immutable:
  3345  	srv              *http2Server
  3346  	hs               *Server
  3347  	conn             net.Conn
  3348  	bw               *http2bufferedWriter // writing to conn
  3349  	handler          Handler
  3350  	baseCtx          http2contextContext
  3351  	framer           *http2Framer
  3352  	doneServing      chan struct{}               // closed when serverConn.serve ends
  3353  	readFrameCh      chan http2readFrameResult   // written by serverConn.readFrames
  3354  	wantWriteFrameCh chan http2FrameWriteRequest // from handlers -> serve
  3355  	wantStartPushCh  chan http2startPushRequest  // from handlers -> serve
  3356  	wroteFrameCh     chan http2frameWriteResult  // from writeFrameAsync -> serve, tickles more frame writes
  3357  	bodyReadCh       chan http2bodyReadMsg       // from handlers -> serve
  3358  	testHookCh       chan func(int)              // code to run on the serve loop
  3359  	flow             http2flow                   // conn-wide (not stream-specific) outbound flow control
  3360  	inflow           http2flow                   // conn-wide inbound flow control
  3361  	tlsState         *tls.ConnectionState        // shared by all handlers, like net/http
  3362  	remoteAddrStr    string
  3363  	writeSched       http2WriteScheduler
  3364  
  3365  	// Everything following is owned by the serve loop; use serveG.check():
  3366  	serveG                      http2goroutineLock // used to verify funcs are on serve()
  3367  	pushEnabled                 bool
  3368  	sawFirstSettings            bool // got the initial SETTINGS frame after the preface
  3369  	needToSendSettingsAck       bool
  3370  	unackedSettings             int    // how many SETTINGS have we sent without ACKs?
  3371  	clientMaxStreams            uint32 // SETTINGS_MAX_CONCURRENT_STREAMS from client (our PUSH_PROMISE limit)
  3372  	advMaxStreams               uint32 // our SETTINGS_MAX_CONCURRENT_STREAMS advertised the client
  3373  	curClientStreams            uint32 // number of open streams initiated by the client
  3374  	curPushedStreams            uint32 // number of open streams initiated by server push
  3375  	maxClientStreamID           uint32 // max ever seen from client (odd), or 0 if there have been no client requests
  3376  	maxPushPromiseID            uint32 // ID of the last push promise (even), or 0 if there have been no pushes
  3377  	streams                     map[uint32]*http2stream
  3378  	initialStreamSendWindowSize int32
  3379  	maxFrameSize                int32
  3380  	headerTableSize             uint32
  3381  	peerMaxHeaderListSize       uint32            // zero means unknown (default)
  3382  	canonHeader                 map[string]string // http2-lower-case -> Go-Canonical-Case
  3383  	writingFrame                bool              // started writing a frame (on serve goroutine or separate)
  3384  	writingFrameAsync           bool              // started a frame on its own goroutine but haven't heard back on wroteFrameCh
  3385  	needsFrameFlush             bool              // last frame write wasn't a flush
  3386  	inGoAway                    bool              // we've started to or sent GOAWAY
  3387  	inFrameScheduleLoop         bool              // whether we're in the scheduleFrameWrite loop
  3388  	needToSendGoAway            bool              // we need to schedule a GOAWAY frame write
  3389  	goAwayCode                  http2ErrCode
  3390  	shutdownTimerCh             <-chan time.Time // nil until used
  3391  	shutdownTimer               *time.Timer      // nil until used
  3392  	idleTimer                   *time.Timer      // nil if unused
  3393  	idleTimerCh                 <-chan time.Time // nil if unused
  3394  
  3395  	// Owned by the writeFrameAsync goroutine:
  3396  	headerWriteBuf bytes.Buffer
  3397  	hpackEncoder   *hpack.Encoder
  3398  }
  3399  
  3400  func (sc *http2serverConn) maxHeaderListSize() uint32 {
  3401  	n := sc.hs.MaxHeaderBytes
  3402  	if n <= 0 {
  3403  		n = DefaultMaxHeaderBytes
  3404  	}
  3405  	// http2's count is in a slightly different unit and includes 32 bytes per pair.
  3406  	// So, take the net/http.Server value and pad it up a bit, assuming 10 headers.
  3407  	const perFieldOverhead = 32 // per http2 spec
  3408  	const typicalHeaders = 10   // conservative
  3409  	return uint32(n + typicalHeaders*perFieldOverhead)
  3410  }
  3411  
  3412  func (sc *http2serverConn) curOpenStreams() uint32 {
  3413  	sc.serveG.check()
  3414  	return sc.curClientStreams + sc.curPushedStreams
  3415  }
  3416  
  3417  // stream represents a stream. This is the minimal metadata needed by
  3418  // the serve goroutine. Most of the actual stream state is owned by
  3419  // the http.Handler's goroutine in the responseWriter. Because the
  3420  // responseWriter's responseWriterState is recycled at the end of a
  3421  // handler, this struct intentionally has no pointer to the
  3422  // *responseWriter{,State} itself, as the Handler ending nils out the
  3423  // responseWriter's state field.
  3424  type http2stream struct {
  3425  	// immutable:
  3426  	sc        *http2serverConn
  3427  	id        uint32
  3428  	body      *http2pipe       // non-nil if expecting DATA frames
  3429  	cw        http2closeWaiter // closed wait stream transitions to closed state
  3430  	ctx       http2contextContext
  3431  	cancelCtx func()
  3432  
  3433  	// owned by serverConn's serve loop:
  3434  	bodyBytes        int64        // body bytes seen so far
  3435  	declBodyBytes    int64        // or -1 if undeclared
  3436  	flow             http2flow    // limits writing from Handler to client
  3437  	inflow           http2flow    // what the client is allowed to POST/etc to us
  3438  	parent           *http2stream // or nil
  3439  	numTrailerValues int64
  3440  	weight           uint8
  3441  	state            http2streamState
  3442  	resetQueued      bool // RST_STREAM queued for write; set by sc.resetStream
  3443  	gotTrailerHeader bool // HEADER frame for trailers was seen
  3444  	wroteHeaders     bool // whether we wrote headers (not status 100)
  3445  
  3446  	trailer    Header // accumulated trailers
  3447  	reqTrailer Header // handler's Request.Trailer
  3448  }
  3449  
  3450  func (sc *http2serverConn) Framer() *http2Framer { return sc.framer }
  3451  
  3452  func (sc *http2serverConn) CloseConn() error { return sc.conn.Close() }
  3453  
  3454  func (sc *http2serverConn) Flush() error { return sc.bw.Flush() }
  3455  
  3456  func (sc *http2serverConn) HeaderEncoder() (*hpack.Encoder, *bytes.Buffer) {
  3457  	return sc.hpackEncoder, &sc.headerWriteBuf
  3458  }
  3459  
  3460  func (sc *http2serverConn) state(streamID uint32) (http2streamState, *http2stream) {
  3461  	sc.serveG.check()
  3462  
  3463  	if st, ok := sc.streams[streamID]; ok {
  3464  		return st.state, st
  3465  	}
  3466  
  3467  	if streamID%2 == 1 {
  3468  		if streamID <= sc.maxClientStreamID {
  3469  			return http2stateClosed, nil
  3470  		}
  3471  	} else {
  3472  		if streamID <= sc.maxPushPromiseID {
  3473  			return http2stateClosed, nil
  3474  		}
  3475  	}
  3476  	return http2stateIdle, nil
  3477  }
  3478  
  3479  // setConnState calls the net/http ConnState hook for this connection, if configured.
  3480  // Note that the net/http package does StateNew and StateClosed for us.
  3481  // There is currently no plan for StateHijacked or hijacking HTTP/2 connections.
  3482  func (sc *http2serverConn) setConnState(state ConnState) {
  3483  	if sc.hs.ConnState != nil {
  3484  		sc.hs.ConnState(sc.conn, state)
  3485  	}
  3486  }
  3487  
  3488  func (sc *http2serverConn) vlogf(format string, args ...interface{}) {
  3489  	if http2VerboseLogs {
  3490  		sc.logf(format, args...)
  3491  	}
  3492  }
  3493  
  3494  func (sc *http2serverConn) logf(format string, args ...interface{}) {
  3495  	if lg := sc.hs.ErrorLog; lg != nil {
  3496  		lg.Printf(format, args...)
  3497  	} else {
  3498  		log.Printf(format, args...)
  3499  	}
  3500  }
  3501  
  3502  // errno returns v's underlying uintptr, else 0.
  3503  //
  3504  // TODO: remove this helper function once http2 can use build
  3505  // tags. See comment in isClosedConnError.
  3506  func http2errno(v error) uintptr {
  3507  	if rv := reflect.ValueOf(v); rv.Kind() == reflect.Uintptr {
  3508  		return uintptr(rv.Uint())
  3509  	}
  3510  	return 0
  3511  }
  3512  
  3513  // isClosedConnError reports whether err is an error from use of a closed
  3514  // network connection.
  3515  func http2isClosedConnError(err error) bool {
  3516  	if err == nil {
  3517  		return false
  3518  	}
  3519  
  3520  	str := err.Error()
  3521  	if strings.Contains(str, "use of closed network connection") {
  3522  		return true
  3523  	}
  3524  
  3525  	if runtime.GOOS == "windows" {
  3526  		if oe, ok := err.(*net.OpError); ok && oe.Op == "read" {
  3527  			if se, ok := oe.Err.(*os.SyscallError); ok && se.Syscall == "wsarecv" {
  3528  				const WSAECONNABORTED = 10053
  3529  				const WSAECONNRESET = 10054
  3530  				if n := http2errno(se.Err); n == WSAECONNRESET || n == WSAECONNABORTED {
  3531  					return true
  3532  				}
  3533  			}
  3534  		}
  3535  	}
  3536  	return false
  3537  }
  3538  
  3539  func (sc *http2serverConn) condlogf(err error, format string, args ...interface{}) {
  3540  	if err == nil {
  3541  		return
  3542  	}
  3543  	if err == io.EOF || err == io.ErrUnexpectedEOF || http2isClosedConnError(err) {
  3544  
  3545  		sc.vlogf(format, args...)
  3546  	} else {
  3547  		sc.logf(format, args...)
  3548  	}
  3549  }
  3550  
  3551  func (sc *http2serverConn) canonicalHeader(v string) string {
  3552  	sc.serveG.check()
  3553  	cv, ok := http2commonCanonHeader[v]
  3554  	if ok {
  3555  		return cv
  3556  	}
  3557  	cv, ok = sc.canonHeader[v]
  3558  	if ok {
  3559  		return cv
  3560  	}
  3561  	if sc.canonHeader == nil {
  3562  		sc.canonHeader = make(map[string]string)
  3563  	}
  3564  	cv = CanonicalHeaderKey(v)
  3565  	sc.canonHeader[v] = cv
  3566  	return cv
  3567  }
  3568  
  3569  type http2readFrameResult struct {
  3570  	f   http2Frame // valid until readMore is called
  3571  	err error
  3572  
  3573  	// readMore should be called once the consumer no longer needs or
  3574  	// retains f. After readMore, f is invalid and more frames can be
  3575  	// read.
  3576  	readMore func()
  3577  }
  3578  
  3579  // readFrames is the loop that reads incoming frames.
  3580  // It takes care to only read one frame at a time, blocking until the
  3581  // consumer is done with the frame.
  3582  // It's run on its own goroutine.
  3583  func (sc *http2serverConn) readFrames() {
  3584  	gate := make(http2gate)
  3585  	gateDone := gate.Done
  3586  	for {
  3587  		f, err := sc.framer.ReadFrame()
  3588  		select {
  3589  		case sc.readFrameCh <- http2readFrameResult{f, err, gateDone}:
  3590  		case <-sc.doneServing:
  3591  			return
  3592  		}
  3593  		select {
  3594  		case <-gate:
  3595  		case <-sc.doneServing:
  3596  			return
  3597  		}
  3598  		if http2terminalReadFrameError(err) {
  3599  			return
  3600  		}
  3601  	}
  3602  }
  3603  
  3604  // frameWriteResult is the message passed from writeFrameAsync to the serve goroutine.
  3605  type http2frameWriteResult struct {
  3606  	wr  http2FrameWriteRequest // what was written (or attempted)
  3607  	err error                  // result of the writeFrame call
  3608  }
  3609  
  3610  // writeFrameAsync runs in its own goroutine and writes a single frame
  3611  // and then reports when it's done.
  3612  // At most one goroutine can be running writeFrameAsync at a time per
  3613  // serverConn.
  3614  func (sc *http2serverConn) writeFrameAsync(wr http2FrameWriteRequest) {
  3615  	err := wr.write.writeFrame(sc)
  3616  	sc.wroteFrameCh <- http2frameWriteResult{wr, err}
  3617  }
  3618  
  3619  func (sc *http2serverConn) closeAllStreamsOnConnClose() {
  3620  	sc.serveG.check()
  3621  	for _, st := range sc.streams {
  3622  		sc.closeStream(st, http2errClientDisconnected)
  3623  	}
  3624  }
  3625  
  3626  func (sc *http2serverConn) stopShutdownTimer() {
  3627  	sc.serveG.check()
  3628  	if t := sc.shutdownTimer; t != nil {
  3629  		t.Stop()
  3630  	}
  3631  }
  3632  
  3633  func (sc *http2serverConn) notePanic() {
  3634  
  3635  	if http2testHookOnPanicMu != nil {
  3636  		http2testHookOnPanicMu.Lock()
  3637  		defer http2testHookOnPanicMu.Unlock()
  3638  	}
  3639  	if http2testHookOnPanic != nil {
  3640  		if e := recover(); e != nil {
  3641  			if http2testHookOnPanic(sc, e) {
  3642  				panic(e)
  3643  			}
  3644  		}
  3645  	}
  3646  }
  3647  
  3648  func (sc *http2serverConn) serve() {
  3649  	sc.serveG.check()
  3650  	defer sc.notePanic()
  3651  	defer sc.conn.Close()
  3652  	defer sc.closeAllStreamsOnConnClose()
  3653  	defer sc.stopShutdownTimer()
  3654  	defer close(sc.doneServing)
  3655  
  3656  	if http2VerboseLogs {
  3657  		sc.vlogf("http2: server connection from %v on %p", sc.conn.RemoteAddr(), sc.hs)
  3658  	}
  3659  
  3660  	sc.writeFrame(http2FrameWriteRequest{
  3661  		write: http2writeSettings{
  3662  			{http2SettingMaxFrameSize, sc.srv.maxReadFrameSize()},
  3663  			{http2SettingMaxConcurrentStreams, sc.advMaxStreams},
  3664  			{http2SettingMaxHeaderListSize, sc.maxHeaderListSize()},
  3665  			{http2SettingInitialWindowSize, uint32(sc.srv.initialStreamRecvWindowSize())},
  3666  		},
  3667  	})
  3668  	sc.unackedSettings++
  3669  
  3670  	if diff := sc.srv.initialConnRecvWindowSize() - http2initialWindowSize; diff > 0 {
  3671  		sc.sendWindowUpdate(nil, int(diff))
  3672  	}
  3673  
  3674  	if err := sc.readPreface(); err != nil {
  3675  		sc.condlogf(err, "http2: server: error reading preface from client %v: %v", sc.conn.RemoteAddr(), err)
  3676  		return
  3677  	}
  3678  
  3679  	sc.setConnState(StateActive)
  3680  	sc.setConnState(StateIdle)
  3681  
  3682  	if sc.srv.IdleTimeout != 0 {
  3683  		sc.idleTimer = time.NewTimer(sc.srv.IdleTimeout)
  3684  		defer sc.idleTimer.Stop()
  3685  		sc.idleTimerCh = sc.idleTimer.C
  3686  	}
  3687  
  3688  	var gracefulShutdownCh <-chan struct{}
  3689  	if sc.hs != nil {
  3690  		gracefulShutdownCh = http2h1ServerShutdownChan(sc.hs)
  3691  	}
  3692  
  3693  	go sc.readFrames()
  3694  
  3695  	settingsTimer := time.NewTimer(http2firstSettingsTimeout)
  3696  	loopNum := 0
  3697  	for {
  3698  		loopNum++
  3699  		select {
  3700  		case wr := <-sc.wantWriteFrameCh:
  3701  			sc.writeFrame(wr)
  3702  		case spr := <-sc.wantStartPushCh:
  3703  			sc.startPush(spr)
  3704  		case res := <-sc.wroteFrameCh:
  3705  			sc.wroteFrame(res)
  3706  		case res := <-sc.readFrameCh:
  3707  			if !sc.processFrameFromReader(res) {
  3708  				return
  3709  			}
  3710  			res.readMore()
  3711  			if settingsTimer.C != nil {
  3712  				settingsTimer.Stop()
  3713  				settingsTimer.C = nil
  3714  			}
  3715  		case m := <-sc.bodyReadCh:
  3716  			sc.noteBodyRead(m.st, m.n)
  3717  		case <-settingsTimer.C:
  3718  			sc.logf("timeout waiting for SETTINGS frames from %v", sc.conn.RemoteAddr())
  3719  			return
  3720  		case <-gracefulShutdownCh:
  3721  			gracefulShutdownCh = nil
  3722  			sc.startGracefulShutdown()
  3723  		case <-sc.shutdownTimerCh:
  3724  			sc.vlogf("GOAWAY close timer fired; closing conn from %v", sc.conn.RemoteAddr())
  3725  			return
  3726  		case <-sc.idleTimerCh:
  3727  			sc.vlogf("connection is idle")
  3728  			sc.goAway(http2ErrCodeNo)
  3729  		case fn := <-sc.testHookCh:
  3730  			fn(loopNum)
  3731  		}
  3732  
  3733  		if sc.inGoAway && sc.curOpenStreams() == 0 && !sc.needToSendGoAway && !sc.writingFrame {
  3734  			return
  3735  		}
  3736  	}
  3737  }
  3738  
  3739  // readPreface reads the ClientPreface greeting from the peer
  3740  // or returns an error on timeout or an invalid greeting.
  3741  func (sc *http2serverConn) readPreface() error {
  3742  	errc := make(chan error, 1)
  3743  	go func() {
  3744  
  3745  		buf := make([]byte, len(http2ClientPreface))
  3746  		if _, err := io.ReadFull(sc.conn, buf); err != nil {
  3747  			errc <- err
  3748  		} else if !bytes.Equal(buf, http2clientPreface) {
  3749  			errc <- fmt.Errorf("bogus greeting %q", buf)
  3750  		} else {
  3751  			errc <- nil
  3752  		}
  3753  	}()
  3754  	timer := time.NewTimer(http2prefaceTimeout)
  3755  	defer timer.Stop()
  3756  	select {
  3757  	case <-timer.C:
  3758  		return errors.New("timeout waiting for client preface")
  3759  	case err := <-errc:
  3760  		if err == nil {
  3761  			if http2VerboseLogs {
  3762  				sc.vlogf("http2: server: client %v said hello", sc.conn.RemoteAddr())
  3763  			}
  3764  		}
  3765  		return err
  3766  	}
  3767  }
  3768  
  3769  var http2errChanPool = sync.Pool{
  3770  	New: func() interface{} { return make(chan error, 1) },
  3771  }
  3772  
  3773  var http2writeDataPool = sync.Pool{
  3774  	New: func() interface{} { return new(http2writeData) },
  3775  }
  3776  
  3777  // writeDataFromHandler writes DATA response frames from a handler on
  3778  // the given stream.
  3779  func (sc *http2serverConn) writeDataFromHandler(stream *http2stream, data []byte, endStream bool) error {
  3780  	ch := http2errChanPool.Get().(chan error)
  3781  	writeArg := http2writeDataPool.Get().(*http2writeData)
  3782  	*writeArg = http2writeData{stream.id, data, endStream}
  3783  	err := sc.writeFrameFromHandler(http2FrameWriteRequest{
  3784  		write:  writeArg,
  3785  		stream: stream,
  3786  		done:   ch,
  3787  	})
  3788  	if err != nil {
  3789  		return err
  3790  	}
  3791  	var frameWriteDone bool // the frame write is done (successfully or not)
  3792  	select {
  3793  	case err = <-ch:
  3794  		frameWriteDone = true
  3795  	case <-sc.doneServing:
  3796  		return http2errClientDisconnected
  3797  	case <-stream.cw:
  3798  
  3799  		select {
  3800  		case err = <-ch:
  3801  			frameWriteDone = true
  3802  		default:
  3803  			return http2errStreamClosed
  3804  		}
  3805  	}
  3806  	http2errChanPool.Put(ch)
  3807  	if frameWriteDone {
  3808  		http2writeDataPool.Put(writeArg)
  3809  	}
  3810  	return err
  3811  }
  3812  
  3813  // writeFrameFromHandler sends wr to sc.wantWriteFrameCh, but aborts
  3814  // if the connection has gone away.
  3815  //
  3816  // This must not be run from the serve goroutine itself, else it might
  3817  // deadlock writing to sc.wantWriteFrameCh (which is only mildly
  3818  // buffered and is read by serve itself). If you're on the serve
  3819  // goroutine, call writeFrame instead.
  3820  func (sc *http2serverConn) writeFrameFromHandler(wr http2FrameWriteRequest) error {
  3821  	sc.serveG.checkNotOn()
  3822  	select {
  3823  	case sc.wantWriteFrameCh <- wr:
  3824  		return nil
  3825  	case <-sc.doneServing:
  3826  
  3827  		return http2errClientDisconnected
  3828  	}
  3829  }
  3830  
  3831  // writeFrame schedules a frame to write and sends it if there's nothing
  3832  // already being written.
  3833  //
  3834  // There is no pushback here (the serve goroutine never blocks). It's
  3835  // the http.Handlers that block, waiting for their previous frames to
  3836  // make it onto the wire
  3837  //
  3838  // If you're not on the serve goroutine, use writeFrameFromHandler instead.
  3839  func (sc *http2serverConn) writeFrame(wr http2FrameWriteRequest) {
  3840  	sc.serveG.check()
  3841  
  3842  	// If true, wr will not be written and wr.done will not be signaled.
  3843  	var ignoreWrite bool
  3844  
  3845  	if wr.StreamID() != 0 {
  3846  		_, isReset := wr.write.(http2StreamError)
  3847  		if state, _ := sc.state(wr.StreamID()); state == http2stateClosed && !isReset {
  3848  			ignoreWrite = true
  3849  		}
  3850  	}
  3851  
  3852  	switch wr.write.(type) {
  3853  	case *http2writeResHeaders:
  3854  		wr.stream.wroteHeaders = true
  3855  	case http2write100ContinueHeadersFrame:
  3856  		if wr.stream.wroteHeaders {
  3857  
  3858  			if wr.done != nil {
  3859  				panic("wr.done != nil for write100ContinueHeadersFrame")
  3860  			}
  3861  			ignoreWrite = true
  3862  		}
  3863  	}
  3864  
  3865  	if !ignoreWrite {
  3866  		sc.writeSched.Push(wr)
  3867  	}
  3868  	sc.scheduleFrameWrite()
  3869  }
  3870  
  3871  // startFrameWrite starts a goroutine to write wr (in a separate
  3872  // goroutine since that might block on the network), and updates the
  3873  // serve goroutine's state about the world, updated from info in wr.
  3874  func (sc *http2serverConn) startFrameWrite(wr http2FrameWriteRequest) {
  3875  	sc.serveG.check()
  3876  	if sc.writingFrame {
  3877  		panic("internal error: can only be writing one frame at a time")
  3878  	}
  3879  
  3880  	st := wr.stream
  3881  	if st != nil {
  3882  		switch st.state {
  3883  		case http2stateHalfClosedLocal:
  3884  			switch wr.write.(type) {
  3885  			case http2StreamError, http2handlerPanicRST, http2writeWindowUpdate:
  3886  
  3887  			default:
  3888  				panic(fmt.Sprintf("internal error: attempt to send frame on a half-closed-local stream: %v", wr))
  3889  			}
  3890  		case http2stateClosed:
  3891  			panic(fmt.Sprintf("internal error: attempt to send frame on a closed stream: %v", wr))
  3892  		}
  3893  	}
  3894  	if wpp, ok := wr.write.(*http2writePushPromise); ok {
  3895  		var err error
  3896  		wpp.promisedID, err = wpp.allocatePromisedID()
  3897  		if err != nil {
  3898  			sc.writingFrameAsync = false
  3899  			wr.replyToWriter(err)
  3900  			return
  3901  		}
  3902  	}
  3903  
  3904  	sc.writingFrame = true
  3905  	sc.needsFrameFlush = true
  3906  	if wr.write.staysWithinBuffer(sc.bw.Available()) {
  3907  		sc.writingFrameAsync = false
  3908  		err := wr.write.writeFrame(sc)
  3909  		sc.wroteFrame(http2frameWriteResult{wr, err})
  3910  	} else {
  3911  		sc.writingFrameAsync = true
  3912  		go sc.writeFrameAsync(wr)
  3913  	}
  3914  }
  3915  
  3916  // errHandlerPanicked is the error given to any callers blocked in a read from
  3917  // Request.Body when the main goroutine panics. Since most handlers read in the
  3918  // the main ServeHTTP goroutine, this will show up rarely.
  3919  var http2errHandlerPanicked = errors.New("http2: handler panicked")
  3920  
  3921  // wroteFrame is called on the serve goroutine with the result of
  3922  // whatever happened on writeFrameAsync.
  3923  func (sc *http2serverConn) wroteFrame(res http2frameWriteResult) {
  3924  	sc.serveG.check()
  3925  	if !sc.writingFrame {
  3926  		panic("internal error: expected to be already writing a frame")
  3927  	}
  3928  	sc.writingFrame = false
  3929  	sc.writingFrameAsync = false
  3930  
  3931  	wr := res.wr
  3932  
  3933  	if http2writeEndsStream(wr.write) {
  3934  		st := wr.stream
  3935  		if st == nil {
  3936  			panic("internal error: expecting non-nil stream")
  3937  		}
  3938  		switch st.state {
  3939  		case http2stateOpen:
  3940  
  3941  			st.state = http2stateHalfClosedLocal
  3942  			sc.resetStream(http2streamError(st.id, http2ErrCodeCancel))
  3943  		case http2stateHalfClosedRemote:
  3944  			sc.closeStream(st, http2errHandlerComplete)
  3945  		}
  3946  	} else {
  3947  		switch v := wr.write.(type) {
  3948  		case http2StreamError:
  3949  
  3950  			if st, ok := sc.streams[v.StreamID]; ok {
  3951  				sc.closeStream(st, v)
  3952  			}
  3953  		case http2handlerPanicRST:
  3954  			sc.closeStream(wr.stream, http2errHandlerPanicked)
  3955  		}
  3956  	}
  3957  
  3958  	wr.replyToWriter(res.err)
  3959  
  3960  	sc.scheduleFrameWrite()
  3961  }
  3962  
  3963  // scheduleFrameWrite tickles the frame writing scheduler.
  3964  //
  3965  // If a frame is already being written, nothing happens. This will be called again
  3966  // when the frame is done being written.
  3967  //
  3968  // If a frame isn't being written we need to send one, the best frame
  3969  // to send is selected, preferring first things that aren't
  3970  // stream-specific (e.g. ACKing settings), and then finding the
  3971  // highest priority stream.
  3972  //
  3973  // If a frame isn't being written and there's nothing else to send, we
  3974  // flush the write buffer.
  3975  func (sc *http2serverConn) scheduleFrameWrite() {
  3976  	sc.serveG.check()
  3977  	if sc.writingFrame || sc.inFrameScheduleLoop {
  3978  		return
  3979  	}
  3980  	sc.inFrameScheduleLoop = true
  3981  	for !sc.writingFrameAsync {
  3982  		if sc.needToSendGoAway {
  3983  			sc.needToSendGoAway = false
  3984  			sc.startFrameWrite(http2FrameWriteRequest{
  3985  				write: &http2writeGoAway{
  3986  					maxStreamID: sc.maxClientStreamID,
  3987  					code:        sc.goAwayCode,
  3988  				},
  3989  			})
  3990  			continue
  3991  		}
  3992  		if sc.needToSendSettingsAck {
  3993  			sc.needToSendSettingsAck = false
  3994  			sc.startFrameWrite(http2FrameWriteRequest{write: http2writeSettingsAck{}})
  3995  			continue
  3996  		}
  3997  		if !sc.inGoAway || sc.goAwayCode == http2ErrCodeNo {
  3998  			if wr, ok := sc.writeSched.Pop(); ok {
  3999  				sc.startFrameWrite(wr)
  4000  				continue
  4001  			}
  4002  		}
  4003  		if sc.needsFrameFlush {
  4004  			sc.startFrameWrite(http2FrameWriteRequest{write: http2flushFrameWriter{}})
  4005  			sc.needsFrameFlush = false
  4006  			continue
  4007  		}
  4008  		break
  4009  	}
  4010  	sc.inFrameScheduleLoop = false
  4011  }
  4012  
  4013  // startGracefulShutdown sends a GOAWAY with ErrCodeNo to tell the
  4014  // client we're gracefully shutting down. The connection isn't closed
  4015  // until all current streams are done.
  4016  func (sc *http2serverConn) startGracefulShutdown() {
  4017  	sc.goAwayIn(http2ErrCodeNo, 0)
  4018  }
  4019  
  4020  func (sc *http2serverConn) goAway(code http2ErrCode) {
  4021  	sc.serveG.check()
  4022  	var forceCloseIn time.Duration
  4023  	if code != http2ErrCodeNo {
  4024  		forceCloseIn = 250 * time.Millisecond
  4025  	} else {
  4026  
  4027  		forceCloseIn = 1 * time.Second
  4028  	}
  4029  	sc.goAwayIn(code, forceCloseIn)
  4030  }
  4031  
  4032  func (sc *http2serverConn) goAwayIn(code http2ErrCode, forceCloseIn time.Duration) {
  4033  	sc.serveG.check()
  4034  	if sc.inGoAway {
  4035  		return
  4036  	}
  4037  	if forceCloseIn != 0 {
  4038  		sc.shutDownIn(forceCloseIn)
  4039  	}
  4040  	sc.inGoAway = true
  4041  	sc.needToSendGoAway = true
  4042  	sc.goAwayCode = code
  4043  	sc.scheduleFrameWrite()
  4044  }
  4045  
  4046  func (sc *http2serverConn) shutDownIn(d time.Duration) {
  4047  	sc.serveG.check()
  4048  	sc.shutdownTimer = time.NewTimer(d)
  4049  	sc.shutdownTimerCh = sc.shutdownTimer.C
  4050  }
  4051  
  4052  func (sc *http2serverConn) resetStream(se http2StreamError) {
  4053  	sc.serveG.check()
  4054  	sc.writeFrame(http2FrameWriteRequest{write: se})
  4055  	if st, ok := sc.streams[se.StreamID]; ok {
  4056  		st.resetQueued = true
  4057  	}
  4058  }
  4059  
  4060  // processFrameFromReader processes the serve loop's read from readFrameCh from the
  4061  // frame-reading goroutine.
  4062  // processFrameFromReader returns whether the connection should be kept open.
  4063  func (sc *http2serverConn) processFrameFromReader(res http2readFrameResult) bool {
  4064  	sc.serveG.check()
  4065  	err := res.err
  4066  	if err != nil {
  4067  		if err == http2ErrFrameTooLarge {
  4068  			sc.goAway(http2ErrCodeFrameSize)
  4069  			return true
  4070  		}
  4071  		clientGone := err == io.EOF || err == io.ErrUnexpectedEOF || http2isClosedConnError(err)
  4072  		if clientGone {
  4073  
  4074  			return false
  4075  		}
  4076  	} else {
  4077  		f := res.f
  4078  		if http2VerboseLogs {
  4079  			sc.vlogf("http2: server read frame %v", http2summarizeFrame(f))
  4080  		}
  4081  		err = sc.processFrame(f)
  4082  		if err == nil {
  4083  			return true
  4084  		}
  4085  	}
  4086  
  4087  	switch ev := err.(type) {
  4088  	case http2StreamError:
  4089  		sc.resetStream(ev)
  4090  		return true
  4091  	case http2goAwayFlowError:
  4092  		sc.goAway(http2ErrCodeFlowControl)
  4093  		return true
  4094  	case http2ConnectionError:
  4095  		sc.logf("http2: server connection error from %v: %v", sc.conn.RemoteAddr(), ev)
  4096  		sc.goAway(http2ErrCode(ev))
  4097  		return true
  4098  	default:
  4099  		if res.err != nil {
  4100  			sc.vlogf("http2: server closing client connection; error reading frame from client %s: %v", sc.conn.RemoteAddr(), err)
  4101  		} else {
  4102  			sc.logf("http2: server closing client connection: %v", err)
  4103  		}
  4104  		return false
  4105  	}
  4106  }
  4107  
  4108  func (sc *http2serverConn) processFrame(f http2Frame) error {
  4109  	sc.serveG.check()
  4110  
  4111  	if !sc.sawFirstSettings {
  4112  		if _, ok := f.(*http2SettingsFrame); !ok {
  4113  			return http2ConnectionError(http2ErrCodeProtocol)
  4114  		}
  4115  		sc.sawFirstSettings = true
  4116  	}
  4117  
  4118  	switch f := f.(type) {
  4119  	case *http2SettingsFrame:
  4120  		return sc.processSettings(f)
  4121  	case *http2MetaHeadersFrame:
  4122  		return sc.processHeaders(f)
  4123  	case *http2WindowUpdateFrame:
  4124  		return sc.processWindowUpdate(f)
  4125  	case *http2PingFrame:
  4126  		return sc.processPing(f)
  4127  	case *http2DataFrame:
  4128  		return sc.processData(f)
  4129  	case *http2RSTStreamFrame:
  4130  		return sc.processResetStream(f)
  4131  	case *http2PriorityFrame:
  4132  		return sc.processPriority(f)
  4133  	case *http2GoAwayFrame:
  4134  		return sc.processGoAway(f)
  4135  	case *http2PushPromiseFrame:
  4136  
  4137  		return http2ConnectionError(http2ErrCodeProtocol)
  4138  	default:
  4139  		sc.vlogf("http2: server ignoring frame: %v", f.Header())
  4140  		return nil
  4141  	}
  4142  }
  4143  
  4144  func (sc *http2serverConn) processPing(f *http2PingFrame) error {
  4145  	sc.serveG.check()
  4146  	if f.IsAck() {
  4147  
  4148  		return nil
  4149  	}
  4150  	if f.StreamID != 0 {
  4151  
  4152  		return http2ConnectionError(http2ErrCodeProtocol)
  4153  	}
  4154  	if sc.inGoAway && sc.goAwayCode != http2ErrCodeNo {
  4155  		return nil
  4156  	}
  4157  	sc.writeFrame(http2FrameWriteRequest{write: http2writePingAck{f}})
  4158  	return nil
  4159  }
  4160  
  4161  func (sc *http2serverConn) processWindowUpdate(f *http2WindowUpdateFrame) error {
  4162  	sc.serveG.check()
  4163  	switch {
  4164  	case f.StreamID != 0:
  4165  		state, st := sc.state(f.StreamID)
  4166  		if state == http2stateIdle {
  4167  
  4168  			return http2ConnectionError(http2ErrCodeProtocol)
  4169  		}
  4170  		if st == nil {
  4171  
  4172  			return nil
  4173  		}
  4174  		if !st.flow.add(int32(f.Increment)) {
  4175  			return http2streamError(f.StreamID, http2ErrCodeFlowControl)
  4176  		}
  4177  	default:
  4178  		if !sc.flow.add(int32(f.Increment)) {
  4179  			return http2goAwayFlowError{}
  4180  		}
  4181  	}
  4182  	sc.scheduleFrameWrite()
  4183  	return nil
  4184  }
  4185  
  4186  func (sc *http2serverConn) processResetStream(f *http2RSTStreamFrame) error {
  4187  	sc.serveG.check()
  4188  
  4189  	state, st := sc.state(f.StreamID)
  4190  	if state == http2stateIdle {
  4191  
  4192  		return http2ConnectionError(http2ErrCodeProtocol)
  4193  	}
  4194  	if st != nil {
  4195  		st.cancelCtx()
  4196  		sc.closeStream(st, http2streamError(f.StreamID, f.ErrCode))
  4197  	}
  4198  	return nil
  4199  }
  4200  
  4201  func (sc *http2serverConn) closeStream(st *http2stream, err error) {
  4202  	sc.serveG.check()
  4203  	if st.state == http2stateIdle || st.state == http2stateClosed {
  4204  		panic(fmt.Sprintf("invariant; can't close stream in state %v", st.state))
  4205  	}
  4206  	st.state = http2stateClosed
  4207  	if st.isPushed() {
  4208  		sc.curPushedStreams--
  4209  	} else {
  4210  		sc.curClientStreams--
  4211  	}
  4212  	delete(sc.streams, st.id)
  4213  	if len(sc.streams) == 0 {
  4214  		sc.setConnState(StateIdle)
  4215  		if sc.srv.IdleTimeout != 0 {
  4216  			sc.idleTimer.Reset(sc.srv.IdleTimeout)
  4217  		}
  4218  		if http2h1ServerKeepAlivesDisabled(sc.hs) {
  4219  			sc.startGracefulShutdown()
  4220  		}
  4221  	}
  4222  	if p := st.body; p != nil {
  4223  
  4224  		sc.sendWindowUpdate(nil, p.Len())
  4225  
  4226  		p.CloseWithError(err)
  4227  	}
  4228  	st.cw.Close()
  4229  	sc.writeSched.CloseStream(st.id)
  4230  }
  4231  
  4232  func (sc *http2serverConn) processSettings(f *http2SettingsFrame) error {
  4233  	sc.serveG.check()
  4234  	if f.IsAck() {
  4235  		sc.unackedSettings--
  4236  		if sc.unackedSettings < 0 {
  4237  
  4238  			return http2ConnectionError(http2ErrCodeProtocol)
  4239  		}
  4240  		return nil
  4241  	}
  4242  	if err := f.ForeachSetting(sc.processSetting); err != nil {
  4243  		return err
  4244  	}
  4245  	sc.needToSendSettingsAck = true
  4246  	sc.scheduleFrameWrite()
  4247  	return nil
  4248  }
  4249  
  4250  func (sc *http2serverConn) processSetting(s http2Setting) error {
  4251  	sc.serveG.check()
  4252  	if err := s.Valid(); err != nil {
  4253  		return err
  4254  	}
  4255  	if http2VerboseLogs {
  4256  		sc.vlogf("http2: server processing setting %v", s)
  4257  	}
  4258  	switch s.ID {
  4259  	case http2SettingHeaderTableSize:
  4260  		sc.headerTableSize = s.Val
  4261  		sc.hpackEncoder.SetMaxDynamicTableSize(s.Val)
  4262  	case http2SettingEnablePush:
  4263  		sc.pushEnabled = s.Val != 0
  4264  	case http2SettingMaxConcurrentStreams:
  4265  		sc.clientMaxStreams = s.Val
  4266  	case http2SettingInitialWindowSize:
  4267  		return sc.processSettingInitialWindowSize(s.Val)
  4268  	case http2SettingMaxFrameSize:
  4269  		sc.maxFrameSize = int32(s.Val)
  4270  	case http2SettingMaxHeaderListSize:
  4271  		sc.peerMaxHeaderListSize = s.Val
  4272  	default:
  4273  
  4274  		if http2VerboseLogs {
  4275  			sc.vlogf("http2: server ignoring unknown setting %v", s)
  4276  		}
  4277  	}
  4278  	return nil
  4279  }
  4280  
  4281  func (sc *http2serverConn) processSettingInitialWindowSize(val uint32) error {
  4282  	sc.serveG.check()
  4283  
  4284  	old := sc.initialStreamSendWindowSize
  4285  	sc.initialStreamSendWindowSize = int32(val)
  4286  	growth := int32(val) - old
  4287  	for _, st := range sc.streams {
  4288  		if !st.flow.add(growth) {
  4289  
  4290  			return http2ConnectionError(http2ErrCodeFlowControl)
  4291  		}
  4292  	}
  4293  	return nil
  4294  }
  4295  
  4296  func (sc *http2serverConn) processData(f *http2DataFrame) error {
  4297  	sc.serveG.check()
  4298  	if sc.inGoAway && sc.goAwayCode != http2ErrCodeNo {
  4299  		return nil
  4300  	}
  4301  	data := f.Data()
  4302  
  4303  	id := f.Header().StreamID
  4304  	state, st := sc.state(id)
  4305  	if id == 0 || state == http2stateIdle {
  4306  
  4307  		return http2ConnectionError(http2ErrCodeProtocol)
  4308  	}
  4309  	if st == nil || state != http2stateOpen || st.gotTrailerHeader || st.resetQueued {
  4310  
  4311  		if sc.inflow.available() < int32(f.Length) {
  4312  			return http2streamError(id, http2ErrCodeFlowControl)
  4313  		}
  4314  
  4315  		sc.inflow.take(int32(f.Length))
  4316  		sc.sendWindowUpdate(nil, int(f.Length))
  4317  
  4318  		if st != nil && st.resetQueued {
  4319  
  4320  			return nil
  4321  		}
  4322  		return http2streamError(id, http2ErrCodeStreamClosed)
  4323  	}
  4324  	if st.body == nil {
  4325  		panic("internal error: should have a body in this state")
  4326  	}
  4327  
  4328  	if st.declBodyBytes != -1 && st.bodyBytes+int64(len(data)) > st.declBodyBytes {
  4329  		st.body.CloseWithError(fmt.Errorf("sender tried to send more than declared Content-Length of %d bytes", st.declBodyBytes))
  4330  		return http2streamError(id, http2ErrCodeStreamClosed)
  4331  	}
  4332  	if f.Length > 0 {
  4333  
  4334  		if st.inflow.available() < int32(f.Length) {
  4335  			return http2streamError(id, http2ErrCodeFlowControl)
  4336  		}
  4337  		st.inflow.take(int32(f.Length))
  4338  
  4339  		if len(data) > 0 {
  4340  			wrote, err := st.body.Write(data)
  4341  			if err != nil {
  4342  				return http2streamError(id, http2ErrCodeStreamClosed)
  4343  			}
  4344  			if wrote != len(data) {
  4345  				panic("internal error: bad Writer")
  4346  			}
  4347  			st.bodyBytes += int64(len(data))
  4348  		}
  4349  
  4350  		if pad := int32(f.Length) - int32(len(data)); pad > 0 {
  4351  			sc.sendWindowUpdate32(nil, pad)
  4352  			sc.sendWindowUpdate32(st, pad)
  4353  		}
  4354  	}
  4355  	if f.StreamEnded() {
  4356  		st.endStream()
  4357  	}
  4358  	return nil
  4359  }
  4360  
  4361  func (sc *http2serverConn) processGoAway(f *http2GoAwayFrame) error {
  4362  	sc.serveG.check()
  4363  	if f.ErrCode != http2ErrCodeNo {
  4364  		sc.logf("http2: received GOAWAY %+v, starting graceful shutdown", f)
  4365  	} else {
  4366  		sc.vlogf("http2: received GOAWAY %+v, starting graceful shutdown", f)
  4367  	}
  4368  	sc.startGracefulShutdown()
  4369  
  4370  	sc.pushEnabled = false
  4371  	return nil
  4372  }
  4373  
  4374  // isPushed reports whether the stream is server-initiated.
  4375  func (st *http2stream) isPushed() bool {
  4376  	return st.id%2 == 0
  4377  }
  4378  
  4379  // endStream closes a Request.Body's pipe. It is called when a DATA
  4380  // frame says a request body is over (or after trailers).
  4381  func (st *http2stream) endStream() {
  4382  	sc := st.sc
  4383  	sc.serveG.check()
  4384  
  4385  	if st.declBodyBytes != -1 && st.declBodyBytes != st.bodyBytes {
  4386  		st.body.CloseWithError(fmt.Errorf("request declared a Content-Length of %d but only wrote %d bytes",
  4387  			st.declBodyBytes, st.bodyBytes))
  4388  	} else {
  4389  		st.body.closeWithErrorAndCode(io.EOF, st.copyTrailersToHandlerRequest)
  4390  		st.body.CloseWithError(io.EOF)
  4391  	}
  4392  	st.state = http2stateHalfClosedRemote
  4393  }
  4394  
  4395  // copyTrailersToHandlerRequest is run in the Handler's goroutine in
  4396  // its Request.Body.Read just before it gets io.EOF.
  4397  func (st *http2stream) copyTrailersToHandlerRequest() {
  4398  	for k, vv := range st.trailer {
  4399  		if _, ok := st.reqTrailer[k]; ok {
  4400  
  4401  			st.reqTrailer[k] = vv
  4402  		}
  4403  	}
  4404  }
  4405  
  4406  func (sc *http2serverConn) processHeaders(f *http2MetaHeadersFrame) error {
  4407  	sc.serveG.check()
  4408  	id := f.StreamID
  4409  	if sc.inGoAway {
  4410  
  4411  		return nil
  4412  	}
  4413  
  4414  	if id%2 != 1 {
  4415  		return http2ConnectionError(http2ErrCodeProtocol)
  4416  	}
  4417  
  4418  	if st := sc.streams[f.StreamID]; st != nil {
  4419  		if st.resetQueued {
  4420  
  4421  			return nil
  4422  		}
  4423  		return st.processTrailerHeaders(f)
  4424  	}
  4425  
  4426  	if id <= sc.maxClientStreamID {
  4427  		return http2ConnectionError(http2ErrCodeProtocol)
  4428  	}
  4429  	sc.maxClientStreamID = id
  4430  
  4431  	if sc.idleTimer != nil {
  4432  		sc.idleTimer.Stop()
  4433  	}
  4434  
  4435  	if sc.curClientStreams+1 > sc.advMaxStreams {
  4436  		if sc.unackedSettings == 0 {
  4437  
  4438  			return http2streamError(id, http2ErrCodeProtocol)
  4439  		}
  4440  
  4441  		return http2streamError(id, http2ErrCodeRefusedStream)
  4442  	}
  4443  
  4444  	initialState := http2stateOpen
  4445  	if f.StreamEnded() {
  4446  		initialState = http2stateHalfClosedRemote
  4447  	}
  4448  	st := sc.newStream(id, 0, initialState)
  4449  
  4450  	if f.HasPriority() {
  4451  		if err := http2checkPriority(f.StreamID, f.Priority); err != nil {
  4452  			return err
  4453  		}
  4454  		sc.writeSched.AdjustStream(st.id, f.Priority)
  4455  	}
  4456  
  4457  	rw, req, err := sc.newWriterAndRequest(st, f)
  4458  	if err != nil {
  4459  		return err
  4460  	}
  4461  	st.reqTrailer = req.Trailer
  4462  	if st.reqTrailer != nil {
  4463  		st.trailer = make(Header)
  4464  	}
  4465  	st.body = req.Body.(*http2requestBody).pipe
  4466  	st.declBodyBytes = req.ContentLength
  4467  
  4468  	handler := sc.handler.ServeHTTP
  4469  	if f.Truncated {
  4470  
  4471  		handler = http2handleHeaderListTooLong
  4472  	} else if err := http2checkValidHTTP2RequestHeaders(req.Header); err != nil {
  4473  		handler = http2new400Handler(err)
  4474  	}
  4475  
  4476  	if sc.hs.ReadTimeout != 0 {
  4477  		sc.conn.SetReadDeadline(time.Time{})
  4478  	}
  4479  
  4480  	go sc.runHandler(rw, req, handler)
  4481  	return nil
  4482  }
  4483  
  4484  func (st *http2stream) processTrailerHeaders(f *http2MetaHeadersFrame) error {
  4485  	sc := st.sc
  4486  	sc.serveG.check()
  4487  	if st.gotTrailerHeader {
  4488  		return http2ConnectionError(http2ErrCodeProtocol)
  4489  	}
  4490  	st.gotTrailerHeader = true
  4491  	if !f.StreamEnded() {
  4492  		return http2streamError(st.id, http2ErrCodeProtocol)
  4493  	}
  4494  
  4495  	if len(f.PseudoFields()) > 0 {
  4496  		return http2streamError(st.id, http2ErrCodeProtocol)
  4497  	}
  4498  	if st.trailer != nil {
  4499  		for _, hf := range f.RegularFields() {
  4500  			key := sc.canonicalHeader(hf.Name)
  4501  			if !http2ValidTrailerHeader(key) {
  4502  
  4503  				return http2streamError(st.id, http2ErrCodeProtocol)
  4504  			}
  4505  			st.trailer[key] = append(st.trailer[key], hf.Value)
  4506  		}
  4507  	}
  4508  	st.endStream()
  4509  	return nil
  4510  }
  4511  
  4512  func http2checkPriority(streamID uint32, p http2PriorityParam) error {
  4513  	if streamID == p.StreamDep {
  4514  
  4515  		return http2streamError(streamID, http2ErrCodeProtocol)
  4516  	}
  4517  	return nil
  4518  }
  4519  
  4520  func (sc *http2serverConn) processPriority(f *http2PriorityFrame) error {
  4521  	if sc.inGoAway {
  4522  		return nil
  4523  	}
  4524  	if err := http2checkPriority(f.StreamID, f.http2PriorityParam); err != nil {
  4525  		return err
  4526  	}
  4527  	sc.writeSched.AdjustStream(f.StreamID, f.http2PriorityParam)
  4528  	return nil
  4529  }
  4530  
  4531  func (sc *http2serverConn) newStream(id, pusherID uint32, state http2streamState) *http2stream {
  4532  	sc.serveG.check()
  4533  	if id == 0 {
  4534  		panic("internal error: cannot create stream with id 0")
  4535  	}
  4536  
  4537  	ctx, cancelCtx := http2contextWithCancel(sc.baseCtx)
  4538  	st := &http2stream{
  4539  		sc:        sc,
  4540  		id:        id,
  4541  		state:     state,
  4542  		ctx:       ctx,
  4543  		cancelCtx: cancelCtx,
  4544  	}
  4545  	st.cw.Init()
  4546  	st.flow.conn = &sc.flow
  4547  	st.flow.add(sc.initialStreamSendWindowSize)
  4548  	st.inflow.conn = &sc.inflow
  4549  	st.inflow.add(sc.srv.initialStreamRecvWindowSize())
  4550  
  4551  	sc.streams[id] = st
  4552  	sc.writeSched.OpenStream(st.id, http2OpenStreamOptions{PusherID: pusherID})
  4553  	if st.isPushed() {
  4554  		sc.curPushedStreams++
  4555  	} else {
  4556  		sc.curClientStreams++
  4557  	}
  4558  	if sc.curOpenStreams() == 1 {
  4559  		sc.setConnState(StateActive)
  4560  	}
  4561  
  4562  	return st
  4563  }
  4564  
  4565  func (sc *http2serverConn) newWriterAndRequest(st *http2stream, f *http2MetaHeadersFrame) (*http2responseWriter, *Request, error) {
  4566  	sc.serveG.check()
  4567  
  4568  	rp := http2requestParam{
  4569  		method:    f.PseudoValue("method"),
  4570  		scheme:    f.PseudoValue("scheme"),
  4571  		authority: f.PseudoValue("authority"),
  4572  		path:      f.PseudoValue("path"),
  4573  	}
  4574  
  4575  	isConnect := rp.method == "CONNECT"
  4576  	if isConnect {
  4577  		if rp.path != "" || rp.scheme != "" || rp.authority == "" {
  4578  			return nil, nil, http2streamError(f.StreamID, http2ErrCodeProtocol)
  4579  		}
  4580  	} else if rp.method == "" || rp.path == "" || (rp.scheme != "https" && rp.scheme != "http") {
  4581  
  4582  		return nil, nil, http2streamError(f.StreamID, http2ErrCodeProtocol)
  4583  	}
  4584  
  4585  	bodyOpen := !f.StreamEnded()
  4586  	if rp.method == "HEAD" && bodyOpen {
  4587  
  4588  		return nil, nil, http2streamError(f.StreamID, http2ErrCodeProtocol)
  4589  	}
  4590  
  4591  	rp.header = make(Header)
  4592  	for _, hf := range f.RegularFields() {
  4593  		rp.header.Add(sc.canonicalHeader(hf.Name), hf.Value)
  4594  	}
  4595  	if rp.authority == "" {
  4596  		rp.authority = rp.header.Get("Host")
  4597  	}
  4598  
  4599  	rw, req, err := sc.newWriterAndRequestNoBody(st, rp)
  4600  	if err != nil {
  4601  		return nil, nil, err
  4602  	}
  4603  	if bodyOpen {
  4604  		if vv, ok := rp.header["Content-Length"]; ok {
  4605  			req.ContentLength, _ = strconv.ParseInt(vv[0], 10, 64)
  4606  		} else {
  4607  			req.ContentLength = -1
  4608  		}
  4609  		req.Body.(*http2requestBody).pipe = &http2pipe{
  4610  			b: &http2dataBuffer{expected: req.ContentLength},
  4611  		}
  4612  	}
  4613  	return rw, req, nil
  4614  }
  4615  
  4616  type http2requestParam struct {
  4617  	method                  string
  4618  	scheme, authority, path string
  4619  	header                  Header
  4620  }
  4621  
  4622  func (sc *http2serverConn) newWriterAndRequestNoBody(st *http2stream, rp http2requestParam) (*http2responseWriter, *Request, error) {
  4623  	sc.serveG.check()
  4624  
  4625  	var tlsState *tls.ConnectionState // nil if not scheme https
  4626  	if rp.scheme == "https" {
  4627  		tlsState = sc.tlsState
  4628  	}
  4629  
  4630  	needsContinue := rp.header.Get("Expect") == "100-continue"
  4631  	if needsContinue {
  4632  		rp.header.Del("Expect")
  4633  	}
  4634  
  4635  	if cookies := rp.header["Cookie"]; len(cookies) > 1 {
  4636  		rp.header.Set("Cookie", strings.Join(cookies, "; "))
  4637  	}
  4638  
  4639  	// Setup Trailers
  4640  	var trailer Header
  4641  	for _, v := range rp.header["Trailer"] {
  4642  		for _, key := range strings.Split(v, ",") {
  4643  			key = CanonicalHeaderKey(strings.TrimSpace(key))
  4644  			switch key {
  4645  			case "Transfer-Encoding", "Trailer", "Content-Length":
  4646  
  4647  			default:
  4648  				if trailer == nil {
  4649  					trailer = make(Header)
  4650  				}
  4651  				trailer[key] = nil
  4652  			}
  4653  		}
  4654  	}
  4655  	delete(rp.header, "Trailer")
  4656  
  4657  	var url_ *url.URL
  4658  	var requestURI string
  4659  	if rp.method == "CONNECT" {
  4660  		url_ = &url.URL{Host: rp.authority}
  4661  		requestURI = rp.authority
  4662  	} else {
  4663  		var err error
  4664  		url_, err = url.ParseRequestURI(rp.path)
  4665  		if err != nil {
  4666  			return nil, nil, http2streamError(st.id, http2ErrCodeProtocol)
  4667  		}
  4668  		requestURI = rp.path
  4669  	}
  4670  
  4671  	body := &http2requestBody{
  4672  		conn:          sc,
  4673  		stream:        st,
  4674  		needsContinue: needsContinue,
  4675  	}
  4676  	req := &Request{
  4677  		Method:     rp.method,
  4678  		URL:        url_,
  4679  		RemoteAddr: sc.remoteAddrStr,
  4680  		Header:     rp.header,
  4681  		RequestURI: requestURI,
  4682  		Proto:      "HTTP/2.0",
  4683  		ProtoMajor: 2,
  4684  		ProtoMinor: 0,
  4685  		TLS:        tlsState,
  4686  		Host:       rp.authority,
  4687  		Body:       body,
  4688  		Trailer:    trailer,
  4689  	}
  4690  	req = http2requestWithContext(req, st.ctx)
  4691  
  4692  	rws := http2responseWriterStatePool.Get().(*http2responseWriterState)
  4693  	bwSave := rws.bw
  4694  	*rws = http2responseWriterState{}
  4695  	rws.conn = sc
  4696  	rws.bw = bwSave
  4697  	rws.bw.Reset(http2chunkWriter{rws})
  4698  	rws.stream = st
  4699  	rws.req = req
  4700  	rws.body = body
  4701  
  4702  	rw := &http2responseWriter{rws: rws}
  4703  	return rw, req, nil
  4704  }
  4705  
  4706  // Run on its own goroutine.
  4707  func (sc *http2serverConn) runHandler(rw *http2responseWriter, req *Request, handler func(ResponseWriter, *Request)) {
  4708  	didPanic := true
  4709  	defer func() {
  4710  		rw.rws.stream.cancelCtx()
  4711  		if didPanic {
  4712  			e := recover()
  4713  			sc.writeFrameFromHandler(http2FrameWriteRequest{
  4714  				write:  http2handlerPanicRST{rw.rws.stream.id},
  4715  				stream: rw.rws.stream,
  4716  			})
  4717  
  4718  			if http2shouldLogPanic(e) {
  4719  				const size = 64 << 10
  4720  				buf := make([]byte, size)
  4721  				buf = buf[:runtime.Stack(buf, false)]
  4722  				sc.logf("http2: panic serving %v: %v\n%s", sc.conn.RemoteAddr(), e, buf)
  4723  			}
  4724  			return
  4725  		}
  4726  		rw.handlerDone()
  4727  	}()
  4728  	handler(rw, req)
  4729  	didPanic = false
  4730  }
  4731  
  4732  func http2handleHeaderListTooLong(w ResponseWriter, r *Request) {
  4733  	// 10.5.1 Limits on Header Block Size:
  4734  	// .. "A server that receives a larger header block than it is
  4735  	// willing to handle can send an HTTP 431 (Request Header Fields Too
  4736  	// Large) status code"
  4737  	const statusRequestHeaderFieldsTooLarge = 431 // only in Go 1.6+
  4738  	w.WriteHeader(statusRequestHeaderFieldsTooLarge)
  4739  	io.WriteString(w, "<h1>HTTP Error 431</h1><p>Request Header Field(s) Too Large</p>")
  4740  }
  4741  
  4742  // called from handler goroutines.
  4743  // h may be nil.
  4744  func (sc *http2serverConn) writeHeaders(st *http2stream, headerData *http2writeResHeaders) error {
  4745  	sc.serveG.checkNotOn()
  4746  	var errc chan error
  4747  	if headerData.h != nil {
  4748  
  4749  		errc = http2errChanPool.Get().(chan error)
  4750  	}
  4751  	if err := sc.writeFrameFromHandler(http2FrameWriteRequest{
  4752  		write:  headerData,
  4753  		stream: st,
  4754  		done:   errc,
  4755  	}); err != nil {
  4756  		return err
  4757  	}
  4758  	if errc != nil {
  4759  		select {
  4760  		case err := <-errc:
  4761  			http2errChanPool.Put(errc)
  4762  			return err
  4763  		case <-sc.doneServing:
  4764  			return http2errClientDisconnected
  4765  		case <-st.cw:
  4766  			return http2errStreamClosed
  4767  		}
  4768  	}
  4769  	return nil
  4770  }
  4771  
  4772  // called from handler goroutines.
  4773  func (sc *http2serverConn) write100ContinueHeaders(st *http2stream) {
  4774  	sc.writeFrameFromHandler(http2FrameWriteRequest{
  4775  		write:  http2write100ContinueHeadersFrame{st.id},
  4776  		stream: st,
  4777  	})
  4778  }
  4779  
  4780  // A bodyReadMsg tells the server loop that the http.Handler read n
  4781  // bytes of the DATA from the client on the given stream.
  4782  type http2bodyReadMsg struct {
  4783  	st *http2stream
  4784  	n  int
  4785  }
  4786  
  4787  // called from handler goroutines.
  4788  // Notes that the handler for the given stream ID read n bytes of its body
  4789  // and schedules flow control tokens to be sent.
  4790  func (sc *http2serverConn) noteBodyReadFromHandler(st *http2stream, n int, err error) {
  4791  	sc.serveG.checkNotOn()
  4792  	if n > 0 {
  4793  		select {
  4794  		case sc.bodyReadCh <- http2bodyReadMsg{st, n}:
  4795  		case <-sc.doneServing:
  4796  		}
  4797  	}
  4798  }
  4799  
  4800  func (sc *http2serverConn) noteBodyRead(st *http2stream, n int) {
  4801  	sc.serveG.check()
  4802  	sc.sendWindowUpdate(nil, n)
  4803  	if st.state != http2stateHalfClosedRemote && st.state != http2stateClosed {
  4804  
  4805  		sc.sendWindowUpdate(st, n)
  4806  	}
  4807  }
  4808  
  4809  // st may be nil for conn-level
  4810  func (sc *http2serverConn) sendWindowUpdate(st *http2stream, n int) {
  4811  	sc.serveG.check()
  4812  	// "The legal range for the increment to the flow control
  4813  	// window is 1 to 2^31-1 (2,147,483,647) octets."
  4814  	// A Go Read call on 64-bit machines could in theory read
  4815  	// a larger Read than this. Very unlikely, but we handle it here
  4816  	// rather than elsewhere for now.
  4817  	const maxUint31 = 1<<31 - 1
  4818  	for n >= maxUint31 {
  4819  		sc.sendWindowUpdate32(st, maxUint31)
  4820  		n -= maxUint31
  4821  	}
  4822  	sc.sendWindowUpdate32(st, int32(n))
  4823  }
  4824  
  4825  // st may be nil for conn-level
  4826  func (sc *http2serverConn) sendWindowUpdate32(st *http2stream, n int32) {
  4827  	sc.serveG.check()
  4828  	if n == 0 {
  4829  		return
  4830  	}
  4831  	if n < 0 {
  4832  		panic("negative update")
  4833  	}
  4834  	var streamID uint32
  4835  	if st != nil {
  4836  		streamID = st.id
  4837  	}
  4838  	sc.writeFrame(http2FrameWriteRequest{
  4839  		write:  http2writeWindowUpdate{streamID: streamID, n: uint32(n)},
  4840  		stream: st,
  4841  	})
  4842  	var ok bool
  4843  	if st == nil {
  4844  		ok = sc.inflow.add(n)
  4845  	} else {
  4846  		ok = st.inflow.add(n)
  4847  	}
  4848  	if !ok {
  4849  		panic("internal error; sent too many window updates without decrements?")
  4850  	}
  4851  }
  4852  
  4853  // requestBody is the Handler's Request.Body type.
  4854  // Read and Close may be called concurrently.
  4855  type http2requestBody struct {
  4856  	stream        *http2stream
  4857  	conn          *http2serverConn
  4858  	closed        bool       // for use by Close only
  4859  	sawEOF        bool       // for use by Read only
  4860  	pipe          *http2pipe // non-nil if we have a HTTP entity message body
  4861  	needsContinue bool       // need to send a 100-continue
  4862  }
  4863  
  4864  func (b *http2requestBody) Close() error {
  4865  	if b.pipe != nil && !b.closed {
  4866  		b.pipe.BreakWithError(http2errClosedBody)
  4867  	}
  4868  	b.closed = true
  4869  	return nil
  4870  }
  4871  
  4872  func (b *http2requestBody) Read(p []byte) (n int, err error) {
  4873  	if b.needsContinue {
  4874  		b.needsContinue = false
  4875  		b.conn.write100ContinueHeaders(b.stream)
  4876  	}
  4877  	if b.pipe == nil || b.sawEOF {
  4878  		return 0, io.EOF
  4879  	}
  4880  	n, err = b.pipe.Read(p)
  4881  	if err == io.EOF {
  4882  		b.sawEOF = true
  4883  	}
  4884  	if b.conn == nil && http2inTests {
  4885  		return
  4886  	}
  4887  	b.conn.noteBodyReadFromHandler(b.stream, n, err)
  4888  	return
  4889  }
  4890  
  4891  // responseWriter is the http.ResponseWriter implementation. It's
  4892  // intentionally small (1 pointer wide) to minimize garbage. The
  4893  // responseWriterState pointer inside is zeroed at the end of a
  4894  // request (in handlerDone) and calls on the responseWriter thereafter
  4895  // simply crash (caller's mistake), but the much larger responseWriterState
  4896  // and buffers are reused between multiple requests.
  4897  type http2responseWriter struct {
  4898  	rws *http2responseWriterState
  4899  }
  4900  
  4901  // Optional http.ResponseWriter interfaces implemented.
  4902  var (
  4903  	_ CloseNotifier     = (*http2responseWriter)(nil)
  4904  	_ Flusher           = (*http2responseWriter)(nil)
  4905  	_ http2stringWriter = (*http2responseWriter)(nil)
  4906  )
  4907  
  4908  type http2responseWriterState struct {
  4909  	// immutable within a request:
  4910  	stream *http2stream
  4911  	req    *Request
  4912  	body   *http2requestBody // to close at end of request, if DATA frames didn't
  4913  	conn   *http2serverConn
  4914  
  4915  	// TODO: adjust buffer writing sizes based on server config, frame size updates from peer, etc
  4916  	bw *bufio.Writer // writing to a chunkWriter{this *responseWriterState}
  4917  
  4918  	// mutated by http.Handler goroutine:
  4919  	handlerHeader Header   // nil until called
  4920  	snapHeader    Header   // snapshot of handlerHeader at WriteHeader time
  4921  	trailers      []string // set in writeChunk
  4922  	status        int      // status code passed to WriteHeader
  4923  	wroteHeader   bool     // WriteHeader called (explicitly or implicitly). Not necessarily sent to user yet.
  4924  	sentHeader    bool     // have we sent the header frame?
  4925  	handlerDone   bool     // handler has finished
  4926  
  4927  	sentContentLen int64 // non-zero if handler set a Content-Length header
  4928  	wroteBytes     int64
  4929  
  4930  	closeNotifierMu sync.Mutex // guards closeNotifierCh
  4931  	closeNotifierCh chan bool  // nil until first used
  4932  }
  4933  
  4934  type http2chunkWriter struct{ rws *http2responseWriterState }
  4935  
  4936  func (cw http2chunkWriter) Write(p []byte) (n int, err error) { return cw.rws.writeChunk(p) }
  4937  
  4938  func (rws *http2responseWriterState) hasTrailers() bool { return len(rws.trailers) != 0 }
  4939  
  4940  // declareTrailer is called for each Trailer header when the
  4941  // response header is written. It notes that a header will need to be
  4942  // written in the trailers at the end of the response.
  4943  func (rws *http2responseWriterState) declareTrailer(k string) {
  4944  	k = CanonicalHeaderKey(k)
  4945  	if !http2ValidTrailerHeader(k) {
  4946  
  4947  		rws.conn.logf("ignoring invalid trailer %q", k)
  4948  		return
  4949  	}
  4950  	if !http2strSliceContains(rws.trailers, k) {
  4951  		rws.trailers = append(rws.trailers, k)
  4952  	}
  4953  }
  4954  
  4955  // writeChunk writes chunks from the bufio.Writer. But because
  4956  // bufio.Writer may bypass its chunking, sometimes p may be
  4957  // arbitrarily large.
  4958  //
  4959  // writeChunk is also responsible (on the first chunk) for sending the
  4960  // HEADER response.
  4961  func (rws *http2responseWriterState) writeChunk(p []byte) (n int, err error) {
  4962  	if !rws.wroteHeader {
  4963  		rws.writeHeader(200)
  4964  	}
  4965  
  4966  	isHeadResp := rws.req.Method == "HEAD"
  4967  	if !rws.sentHeader {
  4968  		rws.sentHeader = true
  4969  		var ctype, clen string
  4970  		if clen = rws.snapHeader.Get("Content-Length"); clen != "" {
  4971  			rws.snapHeader.Del("Content-Length")
  4972  			clen64, err := strconv.ParseInt(clen, 10, 64)
  4973  			if err == nil && clen64 >= 0 {
  4974  				rws.sentContentLen = clen64
  4975  			} else {
  4976  				clen = ""
  4977  			}
  4978  		}
  4979  		if clen == "" && rws.handlerDone && http2bodyAllowedForStatus(rws.status) && (len(p) > 0 || !isHeadResp) {
  4980  			clen = strconv.Itoa(len(p))
  4981  		}
  4982  		_, hasContentType := rws.snapHeader["Content-Type"]
  4983  		if !hasContentType && http2bodyAllowedForStatus(rws.status) {
  4984  			ctype = DetectContentType(p)
  4985  		}
  4986  		var date string
  4987  		if _, ok := rws.snapHeader["Date"]; !ok {
  4988  
  4989  			date = time.Now().UTC().Format(TimeFormat)
  4990  		}
  4991  
  4992  		for _, v := range rws.snapHeader["Trailer"] {
  4993  			http2foreachHeaderElement(v, rws.declareTrailer)
  4994  		}
  4995  
  4996  		endStream := (rws.handlerDone && !rws.hasTrailers() && len(p) == 0) || isHeadResp
  4997  		err = rws.conn.writeHeaders(rws.stream, &http2writeResHeaders{
  4998  			streamID:      rws.stream.id,
  4999  			httpResCode:   rws.status,
  5000  			h:             rws.snapHeader,
  5001  			endStream:     endStream,
  5002  			contentType:   ctype,
  5003  			contentLength: clen,
  5004  			date:          date,
  5005  		})
  5006  		if err != nil {
  5007  			return 0, err
  5008  		}
  5009  		if endStream {
  5010  			return 0, nil
  5011  		}
  5012  	}
  5013  	if isHeadResp {
  5014  		return len(p), nil
  5015  	}
  5016  	if len(p) == 0 && !rws.handlerDone {
  5017  		return 0, nil
  5018  	}
  5019  
  5020  	if rws.handlerDone {
  5021  		rws.promoteUndeclaredTrailers()
  5022  	}
  5023  
  5024  	endStream := rws.handlerDone && !rws.hasTrailers()
  5025  	if len(p) > 0 || endStream {
  5026  
  5027  		if err := rws.conn.writeDataFromHandler(rws.stream, p, endStream); err != nil {
  5028  			return 0, err
  5029  		}
  5030  	}
  5031  
  5032  	if rws.handlerDone && rws.hasTrailers() {
  5033  		err = rws.conn.writeHeaders(rws.stream, &http2writeResHeaders{
  5034  			streamID:  rws.stream.id,
  5035  			h:         rws.handlerHeader,
  5036  			trailers:  rws.trailers,
  5037  			endStream: true,
  5038  		})
  5039  		return len(p), err
  5040  	}
  5041  	return len(p), nil
  5042  }
  5043  
  5044  // TrailerPrefix is a magic prefix for ResponseWriter.Header map keys
  5045  // that, if present, signals that the map entry is actually for
  5046  // the response trailers, and not the response headers. The prefix
  5047  // is stripped after the ServeHTTP call finishes and the values are
  5048  // sent in the trailers.
  5049  //
  5050  // This mechanism is intended only for trailers that are not known
  5051  // prior to the headers being written. If the set of trailers is fixed
  5052  // or known before the header is written, the normal Go trailers mechanism
  5053  // is preferred:
  5054  //    https://golang.org/pkg/net/http/#ResponseWriter
  5055  //    https://golang.org/pkg/net/http/#example_ResponseWriter_trailers
  5056  const http2TrailerPrefix = "Trailer:"
  5057  
  5058  // promoteUndeclaredTrailers permits http.Handlers to set trailers
  5059  // after the header has already been flushed. Because the Go
  5060  // ResponseWriter interface has no way to set Trailers (only the
  5061  // Header), and because we didn't want to expand the ResponseWriter
  5062  // interface, and because nobody used trailers, and because RFC 2616
  5063  // says you SHOULD (but not must) predeclare any trailers in the
  5064  // header, the official ResponseWriter rules said trailers in Go must
  5065  // be predeclared, and then we reuse the same ResponseWriter.Header()
  5066  // map to mean both Headers and Trailers. When it's time to write the
  5067  // Trailers, we pick out the fields of Headers that were declared as
  5068  // trailers. That worked for a while, until we found the first major
  5069  // user of Trailers in the wild: gRPC (using them only over http2),
  5070  // and gRPC libraries permit setting trailers mid-stream without
  5071  // predeclarnig them. So: change of plans. We still permit the old
  5072  // way, but we also permit this hack: if a Header() key begins with
  5073  // "Trailer:", the suffix of that key is a Trailer. Because ':' is an
  5074  // invalid token byte anyway, there is no ambiguity. (And it's already
  5075  // filtered out) It's mildly hacky, but not terrible.
  5076  //
  5077  // This method runs after the Handler is done and promotes any Header
  5078  // fields to be trailers.
  5079  func (rws *http2responseWriterState) promoteUndeclaredTrailers() {
  5080  	for k, vv := range rws.handlerHeader {
  5081  		if !strings.HasPrefix(k, http2TrailerPrefix) {
  5082  			continue
  5083  		}
  5084  		trailerKey := strings.TrimPrefix(k, http2TrailerPrefix)
  5085  		rws.declareTrailer(trailerKey)
  5086  		rws.handlerHeader[CanonicalHeaderKey(trailerKey)] = vv
  5087  	}
  5088  
  5089  	if len(rws.trailers) > 1 {
  5090  		sorter := http2sorterPool.Get().(*http2sorter)
  5091  		sorter.SortStrings(rws.trailers)
  5092  		http2sorterPool.Put(sorter)
  5093  	}
  5094  }
  5095  
  5096  func (w *http2responseWriter) Flush() {
  5097  	rws := w.rws
  5098  	if rws == nil {
  5099  		panic("Header called after Handler finished")
  5100  	}
  5101  	if rws.bw.Buffered() > 0 {
  5102  		if err := rws.bw.Flush(); err != nil {
  5103  
  5104  			return
  5105  		}
  5106  	} else {
  5107  
  5108  		rws.writeChunk(nil)
  5109  	}
  5110  }
  5111  
  5112  func (w *http2responseWriter) CloseNotify() <-chan bool {
  5113  	rws := w.rws
  5114  	if rws == nil {
  5115  		panic("CloseNotify called after Handler finished")
  5116  	}
  5117  	rws.closeNotifierMu.Lock()
  5118  	ch := rws.closeNotifierCh
  5119  	if ch == nil {
  5120  		ch = make(chan bool, 1)
  5121  		rws.closeNotifierCh = ch
  5122  		cw := rws.stream.cw
  5123  		go func() {
  5124  			cw.Wait()
  5125  			ch <- true
  5126  		}()
  5127  	}
  5128  	rws.closeNotifierMu.Unlock()
  5129  	return ch
  5130  }
  5131  
  5132  func (w *http2responseWriter) Header() Header {
  5133  	rws := w.rws
  5134  	if rws == nil {
  5135  		panic("Header called after Handler finished")
  5136  	}
  5137  	if rws.handlerHeader == nil {
  5138  		rws.handlerHeader = make(Header)
  5139  	}
  5140  	return rws.handlerHeader
  5141  }
  5142  
  5143  func (w *http2responseWriter) WriteHeader(code int) {
  5144  	rws := w.rws
  5145  	if rws == nil {
  5146  		panic("WriteHeader called after Handler finished")
  5147  	}
  5148  	rws.writeHeader(code)
  5149  }
  5150  
  5151  func (rws *http2responseWriterState) writeHeader(code int) {
  5152  	if !rws.wroteHeader {
  5153  		rws.wroteHeader = true
  5154  		rws.status = code
  5155  		if len(rws.handlerHeader) > 0 {
  5156  			rws.snapHeader = http2cloneHeader(rws.handlerHeader)
  5157  		}
  5158  	}
  5159  }
  5160  
  5161  func http2cloneHeader(h Header) Header {
  5162  	h2 := make(Header, len(h))
  5163  	for k, vv := range h {
  5164  		vv2 := make([]string, len(vv))
  5165  		copy(vv2, vv)
  5166  		h2[k] = vv2
  5167  	}
  5168  	return h2
  5169  }
  5170  
  5171  // The Life Of A Write is like this:
  5172  //
  5173  // * Handler calls w.Write or w.WriteString ->
  5174  // * -> rws.bw (*bufio.Writer) ->
  5175  // * (Handler migth call Flush)
  5176  // * -> chunkWriter{rws}
  5177  // * -> responseWriterState.writeChunk(p []byte)
  5178  // * -> responseWriterState.writeChunk (most of the magic; see comment there)
  5179  func (w *http2responseWriter) Write(p []byte) (n int, err error) {
  5180  	return w.write(len(p), p, "")
  5181  }
  5182  
  5183  func (w *http2responseWriter) WriteString(s string) (n int, err error) {
  5184  	return w.write(len(s), nil, s)
  5185  }
  5186  
  5187  // either dataB or dataS is non-zero.
  5188  func (w *http2responseWriter) write(lenData int, dataB []byte, dataS string) (n int, err error) {
  5189  	rws := w.rws
  5190  	if rws == nil {
  5191  		panic("Write called after Handler finished")
  5192  	}
  5193  	if !rws.wroteHeader {
  5194  		w.WriteHeader(200)
  5195  	}
  5196  	if !http2bodyAllowedForStatus(rws.status) {
  5197  		return 0, ErrBodyNotAllowed
  5198  	}
  5199  	rws.wroteBytes += int64(len(dataB)) + int64(len(dataS))
  5200  	if rws.sentContentLen != 0 && rws.wroteBytes > rws.sentContentLen {
  5201  
  5202  		return 0, errors.New("http2: handler wrote more than declared Content-Length")
  5203  	}
  5204  
  5205  	if dataB != nil {
  5206  		return rws.bw.Write(dataB)
  5207  	} else {
  5208  		return rws.bw.WriteString(dataS)
  5209  	}
  5210  }
  5211  
  5212  func (w *http2responseWriter) handlerDone() {
  5213  	rws := w.rws
  5214  	rws.handlerDone = true
  5215  	w.Flush()
  5216  	w.rws = nil
  5217  	http2responseWriterStatePool.Put(rws)
  5218  }
  5219  
  5220  // Push errors.
  5221  var (
  5222  	http2ErrRecursivePush    = errors.New("http2: recursive push not allowed")
  5223  	http2ErrPushLimitReached = errors.New("http2: push would exceed peer's SETTINGS_MAX_CONCURRENT_STREAMS")
  5224  )
  5225  
  5226  // pushOptions is the internal version of http.PushOptions, which we
  5227  // cannot include here because it's only defined in Go 1.8 and later.
  5228  type http2pushOptions struct {
  5229  	Method string
  5230  	Header Header
  5231  }
  5232  
  5233  func (w *http2responseWriter) push(target string, opts http2pushOptions) error {
  5234  	st := w.rws.stream
  5235  	sc := st.sc
  5236  	sc.serveG.checkNotOn()
  5237  
  5238  	if st.isPushed() {
  5239  		return http2ErrRecursivePush
  5240  	}
  5241  
  5242  	if opts.Method == "" {
  5243  		opts.Method = "GET"
  5244  	}
  5245  	if opts.Header == nil {
  5246  		opts.Header = Header{}
  5247  	}
  5248  	wantScheme := "http"
  5249  	if w.rws.req.TLS != nil {
  5250  		wantScheme = "https"
  5251  	}
  5252  
  5253  	u, err := url.Parse(target)
  5254  	if err != nil {
  5255  		return err
  5256  	}
  5257  	if u.Scheme == "" {
  5258  		if !strings.HasPrefix(target, "/") {
  5259  			return fmt.Errorf("target must be an absolute URL or an absolute path: %q", target)
  5260  		}
  5261  		u.Scheme = wantScheme
  5262  		u.Host = w.rws.req.Host
  5263  	} else {
  5264  		if u.Scheme != wantScheme {
  5265  			return fmt.Errorf("cannot push URL with scheme %q from request with scheme %q", u.Scheme, wantScheme)
  5266  		}
  5267  		if u.Host == "" {
  5268  			return errors.New("URL must have a host")
  5269  		}
  5270  	}
  5271  	for k := range opts.Header {
  5272  		if strings.HasPrefix(k, ":") {
  5273  			return fmt.Errorf("promised request headers cannot include pseudo header %q", k)
  5274  		}
  5275  
  5276  		switch strings.ToLower(k) {
  5277  		case "content-length", "content-encoding", "trailer", "te", "expect", "host":
  5278  			return fmt.Errorf("promised request headers cannot include %q", k)
  5279  		}
  5280  	}
  5281  	if err := http2checkValidHTTP2RequestHeaders(opts.Header); err != nil {
  5282  		return err
  5283  	}
  5284  
  5285  	if opts.Method != "GET" && opts.Method != "HEAD" {
  5286  		return fmt.Errorf("method %q must be GET or HEAD", opts.Method)
  5287  	}
  5288  
  5289  	msg := http2startPushRequest{
  5290  		parent: st,
  5291  		method: opts.Method,
  5292  		url:    u,
  5293  		header: http2cloneHeader(opts.Header),
  5294  		done:   http2errChanPool.Get().(chan error),
  5295  	}
  5296  
  5297  	select {
  5298  	case <-sc.doneServing:
  5299  		return http2errClientDisconnected
  5300  	case <-st.cw:
  5301  		return http2errStreamClosed
  5302  	case sc.wantStartPushCh <- msg:
  5303  	}
  5304  
  5305  	select {
  5306  	case <-sc.doneServing:
  5307  		return http2errClientDisconnected
  5308  	case <-st.cw:
  5309  		return http2errStreamClosed
  5310  	case err := <-msg.done:
  5311  		http2errChanPool.Put(msg.done)
  5312  		return err
  5313  	}
  5314  }
  5315  
  5316  type http2startPushRequest struct {
  5317  	parent *http2stream
  5318  	method string
  5319  	url    *url.URL
  5320  	header Header
  5321  	done   chan error
  5322  }
  5323  
  5324  func (sc *http2serverConn) startPush(msg http2startPushRequest) {
  5325  	sc.serveG.check()
  5326  
  5327  	if msg.parent.state != http2stateOpen && msg.parent.state != http2stateHalfClosedRemote {
  5328  
  5329  		msg.done <- http2errStreamClosed
  5330  		return
  5331  	}
  5332  
  5333  	if !sc.pushEnabled {
  5334  		msg.done <- ErrNotSupported
  5335  		return
  5336  	}
  5337  
  5338  	allocatePromisedID := func() (uint32, error) {
  5339  		sc.serveG.check()
  5340  
  5341  		if !sc.pushEnabled {
  5342  			return 0, ErrNotSupported
  5343  		}
  5344  
  5345  		if sc.curPushedStreams+1 > sc.clientMaxStreams {
  5346  			return 0, http2ErrPushLimitReached
  5347  		}
  5348  
  5349  		if sc.maxPushPromiseID+2 >= 1<<31 {
  5350  			sc.startGracefulShutdown()
  5351  			return 0, http2ErrPushLimitReached
  5352  		}
  5353  		sc.maxPushPromiseID += 2
  5354  		promisedID := sc.maxPushPromiseID
  5355  
  5356  		promised := sc.newStream(promisedID, msg.parent.id, http2stateHalfClosedRemote)
  5357  		rw, req, err := sc.newWriterAndRequestNoBody(promised, http2requestParam{
  5358  			method:    msg.method,
  5359  			scheme:    msg.url.Scheme,
  5360  			authority: msg.url.Host,
  5361  			path:      msg.url.RequestURI(),
  5362  			header:    http2cloneHeader(msg.header),
  5363  		})
  5364  		if err != nil {
  5365  
  5366  			panic(fmt.Sprintf("newWriterAndRequestNoBody(%+v): %v", msg.url, err))
  5367  		}
  5368  
  5369  		go sc.runHandler(rw, req, sc.handler.ServeHTTP)
  5370  		return promisedID, nil
  5371  	}
  5372  
  5373  	sc.writeFrame(http2FrameWriteRequest{
  5374  		write: &http2writePushPromise{
  5375  			streamID:           msg.parent.id,
  5376  			method:             msg.method,
  5377  			url:                msg.url,
  5378  			h:                  msg.header,
  5379  			allocatePromisedID: allocatePromisedID,
  5380  		},
  5381  		stream: msg.parent,
  5382  		done:   msg.done,
  5383  	})
  5384  }
  5385  
  5386  // foreachHeaderElement splits v according to the "#rule" construction
  5387  // in RFC 2616 section 2.1 and calls fn for each non-empty element.
  5388  func http2foreachHeaderElement(v string, fn func(string)) {
  5389  	v = textproto.TrimString(v)
  5390  	if v == "" {
  5391  		return
  5392  	}
  5393  	if !strings.Contains(v, ",") {
  5394  		fn(v)
  5395  		return
  5396  	}
  5397  	for _, f := range strings.Split(v, ",") {
  5398  		if f = textproto.TrimString(f); f != "" {
  5399  			fn(f)
  5400  		}
  5401  	}
  5402  }
  5403  
  5404  // From http://httpwg.org/specs/rfc7540.html#rfc.section.8.1.2.2
  5405  var http2connHeaders = []string{
  5406  	"Connection",
  5407  	"Keep-Alive",
  5408  	"Proxy-Connection",
  5409  	"Transfer-Encoding",
  5410  	"Upgrade",
  5411  }
  5412  
  5413  // checkValidHTTP2RequestHeaders checks whether h is a valid HTTP/2 request,
  5414  // per RFC 7540 Section 8.1.2.2.
  5415  // The returned error is reported to users.
  5416  func http2checkValidHTTP2RequestHeaders(h Header) error {
  5417  	for _, k := range http2connHeaders {
  5418  		if _, ok := h[k]; ok {
  5419  			return fmt.Errorf("request header %q is not valid in HTTP/2", k)
  5420  		}
  5421  	}
  5422  	te := h["Te"]
  5423  	if len(te) > 0 && (len(te) > 1 || (te[0] != "trailers" && te[0] != "")) {
  5424  		return errors.New(`request header "TE" may only be "trailers" in HTTP/2`)
  5425  	}
  5426  	return nil
  5427  }
  5428  
  5429  func http2new400Handler(err error) HandlerFunc {
  5430  	return func(w ResponseWriter, r *Request) {
  5431  		Error(w, err.Error(), StatusBadRequest)
  5432  	}
  5433  }
  5434  
  5435  // ValidTrailerHeader reports whether name is a valid header field name to appear
  5436  // in trailers.
  5437  // See: http://tools.ietf.org/html/rfc7230#section-4.1.2
  5438  func http2ValidTrailerHeader(name string) bool {
  5439  	name = CanonicalHeaderKey(name)
  5440  	if strings.HasPrefix(name, "If-") || http2badTrailer[name] {
  5441  		return false
  5442  	}
  5443  	return true
  5444  }
  5445  
  5446  var http2badTrailer = map[string]bool{
  5447  	"Authorization":       true,
  5448  	"Cache-Control":       true,
  5449  	"Connection":          true,
  5450  	"Content-Encoding":    true,
  5451  	"Content-Length":      true,
  5452  	"Content-Range":       true,
  5453  	"Content-Type":        true,
  5454  	"Expect":              true,
  5455  	"Host":                true,
  5456  	"Keep-Alive":          true,
  5457  	"Max-Forwards":        true,
  5458  	"Pragma":              true,
  5459  	"Proxy-Authenticate":  true,
  5460  	"Proxy-Authorization": true,
  5461  	"Proxy-Connection":    true,
  5462  	"Range":               true,
  5463  	"Realm":               true,
  5464  	"Te":                  true,
  5465  	"Trailer":             true,
  5466  	"Transfer-Encoding":   true,
  5467  	"Www-Authenticate":    true,
  5468  }
  5469  
  5470  // h1ServerShutdownChan returns a channel that will be closed when the
  5471  // provided *http.Server wants to shut down.
  5472  //
  5473  // This is a somewhat hacky way to get at http1 innards. It works
  5474  // when the http2 code is bundled into the net/http package in the
  5475  // standard library. The alternatives ended up making the cmd/go tool
  5476  // depend on http Servers. This is the lightest option for now.
  5477  // This is tested via the TestServeShutdown* tests in net/http.
  5478  func http2h1ServerShutdownChan(hs *Server) <-chan struct{} {
  5479  	if fn := http2testh1ServerShutdownChan; fn != nil {
  5480  		return fn(hs)
  5481  	}
  5482  	var x interface{} = hs
  5483  	type I interface {
  5484  		getDoneChan() <-chan struct{}
  5485  	}
  5486  	if hs, ok := x.(I); ok {
  5487  		return hs.getDoneChan()
  5488  	}
  5489  	return nil
  5490  }
  5491  
  5492  // optional test hook for h1ServerShutdownChan.
  5493  var http2testh1ServerShutdownChan func(hs *Server) <-chan struct{}
  5494  
  5495  // h1ServerKeepAlivesDisabled reports whether hs has its keep-alives
  5496  // disabled. See comments on h1ServerShutdownChan above for why
  5497  // the code is written this way.
  5498  func http2h1ServerKeepAlivesDisabled(hs *Server) bool {
  5499  	var x interface{} = hs
  5500  	type I interface {
  5501  		doKeepAlives() bool
  5502  	}
  5503  	if hs, ok := x.(I); ok {
  5504  		return !hs.doKeepAlives()
  5505  	}
  5506  	return false
  5507  }
  5508  
  5509  const (
  5510  	// transportDefaultConnFlow is how many connection-level flow control
  5511  	// tokens we give the server at start-up, past the default 64k.
  5512  	http2transportDefaultConnFlow = 1 << 30
  5513  
  5514  	// transportDefaultStreamFlow is how many stream-level flow
  5515  	// control tokens we announce to the peer, and how many bytes
  5516  	// we buffer per stream.
  5517  	http2transportDefaultStreamFlow = 4 << 20
  5518  
  5519  	// transportDefaultStreamMinRefresh is the minimum number of bytes we'll send
  5520  	// a stream-level WINDOW_UPDATE for at a time.
  5521  	http2transportDefaultStreamMinRefresh = 4 << 10
  5522  
  5523  	http2defaultUserAgent = "Go-http-client/2.0"
  5524  )
  5525  
  5526  // Transport is an HTTP/2 Transport.
  5527  //
  5528  // A Transport internally caches connections to servers. It is safe
  5529  // for concurrent use by multiple goroutines.
  5530  type http2Transport struct {
  5531  	// DialTLS specifies an optional dial function for creating
  5532  	// TLS connections for requests.
  5533  	//
  5534  	// If DialTLS is nil, tls.Dial is used.
  5535  	//
  5536  	// If the returned net.Conn has a ConnectionState method like tls.Conn,
  5537  	// it will be used to set http.Response.TLS.
  5538  	DialTLS func(network, addr string, cfg *tls.Config) (net.Conn, error)
  5539  
  5540  	// TLSClientConfig specifies the TLS configuration to use with
  5541  	// tls.Client. If nil, the default configuration is used.
  5542  	TLSClientConfig *tls.Config
  5543  
  5544  	// ConnPool optionally specifies an alternate connection pool to use.
  5545  	// If nil, the default is used.
  5546  	ConnPool http2ClientConnPool
  5547  
  5548  	// DisableCompression, if true, prevents the Transport from
  5549  	// requesting compression with an "Accept-Encoding: gzip"
  5550  	// request header when the Request contains no existing
  5551  	// Accept-Encoding value. If the Transport requests gzip on
  5552  	// its own and gets a gzipped response, it's transparently
  5553  	// decoded in the Response.Body. However, if the user
  5554  	// explicitly requested gzip it is not automatically
  5555  	// uncompressed.
  5556  	DisableCompression bool
  5557  
  5558  	// AllowHTTP, if true, permits HTTP/2 requests using the insecure,
  5559  	// plain-text "http" scheme. Note that this does not enable h2c support.
  5560  	AllowHTTP bool
  5561  
  5562  	// MaxHeaderListSize is the http2 SETTINGS_MAX_HEADER_LIST_SIZE to
  5563  	// send in the initial settings frame. It is how many bytes
  5564  	// of response headers are allow. Unlike the http2 spec, zero here
  5565  	// means to use a default limit (currently 10MB). If you actually
  5566  	// want to advertise an ulimited value to the peer, Transport
  5567  	// interprets the highest possible value here (0xffffffff or 1<<32-1)
  5568  	// to mean no limit.
  5569  	MaxHeaderListSize uint32
  5570  
  5571  	// t1, if non-nil, is the standard library Transport using
  5572  	// this transport. Its settings are used (but not its
  5573  	// RoundTrip method, etc).
  5574  	t1 *Transport
  5575  
  5576  	connPoolOnce  sync.Once
  5577  	connPoolOrDef http2ClientConnPool // non-nil version of ConnPool
  5578  }
  5579  
  5580  func (t *http2Transport) maxHeaderListSize() uint32 {
  5581  	if t.MaxHeaderListSize == 0 {
  5582  		return 10 << 20
  5583  	}
  5584  	if t.MaxHeaderListSize == 0xffffffff {
  5585  		return 0
  5586  	}
  5587  	return t.MaxHeaderListSize
  5588  }
  5589  
  5590  func (t *http2Transport) disableCompression() bool {
  5591  	return t.DisableCompression || (t.t1 != nil && t.t1.DisableCompression)
  5592  }
  5593  
  5594  var http2errTransportVersion = errors.New("http2: ConfigureTransport is only supported starting at Go 1.6")
  5595  
  5596  // ConfigureTransport configures a net/http HTTP/1 Transport to use HTTP/2.
  5597  // It requires Go 1.6 or later and returns an error if the net/http package is too old
  5598  // or if t1 has already been HTTP/2-enabled.
  5599  func http2ConfigureTransport(t1 *Transport) error {
  5600  	_, err := http2configureTransport(t1)
  5601  	return err
  5602  }
  5603  
  5604  func (t *http2Transport) connPool() http2ClientConnPool {
  5605  	t.connPoolOnce.Do(t.initConnPool)
  5606  	return t.connPoolOrDef
  5607  }
  5608  
  5609  func (t *http2Transport) initConnPool() {
  5610  	if t.ConnPool != nil {
  5611  		t.connPoolOrDef = t.ConnPool
  5612  	} else {
  5613  		t.connPoolOrDef = &http2clientConnPool{t: t}
  5614  	}
  5615  }
  5616  
  5617  // ClientConn is the state of a single HTTP/2 client connection to an
  5618  // HTTP/2 server.
  5619  type http2ClientConn struct {
  5620  	t         *http2Transport
  5621  	tconn     net.Conn             // usually *tls.Conn, except specialized impls
  5622  	tlsState  *tls.ConnectionState // nil only for specialized impls
  5623  	singleUse bool                 // whether being used for a single http.Request
  5624  
  5625  	// readLoop goroutine fields:
  5626  	readerDone chan struct{} // closed on error
  5627  	readerErr  error         // set before readerDone is closed
  5628  
  5629  	idleTimeout time.Duration // or 0 for never
  5630  	idleTimer   *time.Timer
  5631  
  5632  	mu              sync.Mutex // guards following
  5633  	cond            *sync.Cond // hold mu; broadcast on flow/closed changes
  5634  	flow            http2flow  // our conn-level flow control quota (cs.flow is per stream)
  5635  	inflow          http2flow  // peer's conn-level flow control
  5636  	closed          bool
  5637  	wantSettingsAck bool                          // we sent a SETTINGS frame and haven't heard back
  5638  	goAway          *http2GoAwayFrame             // if non-nil, the GoAwayFrame we received
  5639  	goAwayDebug     string                        // goAway frame's debug data, retained as a string
  5640  	streams         map[uint32]*http2clientStream // client-initiated
  5641  	nextStreamID    uint32
  5642  	pings           map[[8]byte]chan struct{} // in flight ping data to notification channel
  5643  	bw              *bufio.Writer
  5644  	br              *bufio.Reader
  5645  	fr              *http2Framer
  5646  	lastActive      time.Time
  5647  	// Settings from peer: (also guarded by mu)
  5648  	maxFrameSize         uint32
  5649  	maxConcurrentStreams uint32
  5650  	initialWindowSize    uint32
  5651  
  5652  	hbuf    bytes.Buffer // HPACK encoder writes into this
  5653  	henc    *hpack.Encoder
  5654  	freeBuf [][]byte
  5655  
  5656  	wmu  sync.Mutex // held while writing; acquire AFTER mu if holding both
  5657  	werr error      // first write error that has occurred
  5658  }
  5659  
  5660  // clientStream is the state for a single HTTP/2 stream. One of these
  5661  // is created for each Transport.RoundTrip call.
  5662  type http2clientStream struct {
  5663  	cc            *http2ClientConn
  5664  	req           *Request
  5665  	trace         *http2clientTrace // or nil
  5666  	ID            uint32
  5667  	resc          chan http2resAndError
  5668  	bufPipe       http2pipe // buffered pipe with the flow-controlled response payload
  5669  	startedWrite  bool      // started request body write; guarded by cc.mu
  5670  	requestedGzip bool
  5671  	on100         func() // optional code to run if get a 100 continue response
  5672  
  5673  	flow        http2flow // guarded by cc.mu
  5674  	inflow      http2flow // guarded by cc.mu
  5675  	bytesRemain int64     // -1 means unknown; owned by transportResponseBody.Read
  5676  	readErr     error     // sticky read error; owned by transportResponseBody.Read
  5677  	stopReqBody error     // if non-nil, stop writing req body; guarded by cc.mu
  5678  	didReset    bool      // whether we sent a RST_STREAM to the server; guarded by cc.mu
  5679  
  5680  	peerReset chan struct{} // closed on peer reset
  5681  	resetErr  error         // populated before peerReset is closed
  5682  
  5683  	done chan struct{} // closed when stream remove from cc.streams map; close calls guarded by cc.mu
  5684  
  5685  	// owned by clientConnReadLoop:
  5686  	firstByte    bool // got the first response byte
  5687  	pastHeaders  bool // got first MetaHeadersFrame (actual headers)
  5688  	pastTrailers bool // got optional second MetaHeadersFrame (trailers)
  5689  
  5690  	trailer    Header  // accumulated trailers
  5691  	resTrailer *Header // client's Response.Trailer
  5692  }
  5693  
  5694  // awaitRequestCancel runs in its own goroutine and waits for the user
  5695  // to cancel a RoundTrip request, its context to expire, or for the
  5696  // request to be done (any way it might be removed from the cc.streams
  5697  // map: peer reset, successful completion, TCP connection breakage,
  5698  // etc)
  5699  func (cs *http2clientStream) awaitRequestCancel(req *Request) {
  5700  	ctx := http2reqContext(req)
  5701  	if req.Cancel == nil && ctx.Done() == nil {
  5702  		return
  5703  	}
  5704  	select {
  5705  	case <-req.Cancel:
  5706  		cs.cancelStream()
  5707  		cs.bufPipe.CloseWithError(http2errRequestCanceled)
  5708  	case <-ctx.Done():
  5709  		cs.cancelStream()
  5710  		cs.bufPipe.CloseWithError(ctx.Err())
  5711  	case <-cs.done:
  5712  	}
  5713  }
  5714  
  5715  func (cs *http2clientStream) cancelStream() {
  5716  	cs.cc.mu.Lock()
  5717  	didReset := cs.didReset
  5718  	cs.didReset = true
  5719  	cs.cc.mu.Unlock()
  5720  
  5721  	if !didReset {
  5722  		cs.cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil)
  5723  	}
  5724  }
  5725  
  5726  // checkResetOrDone reports any error sent in a RST_STREAM frame by the
  5727  // server, or errStreamClosed if the stream is complete.
  5728  func (cs *http2clientStream) checkResetOrDone() error {
  5729  	select {
  5730  	case <-cs.peerReset:
  5731  		return cs.resetErr
  5732  	case <-cs.done:
  5733  		return http2errStreamClosed
  5734  	default:
  5735  		return nil
  5736  	}
  5737  }
  5738  
  5739  func (cs *http2clientStream) abortRequestBodyWrite(err error) {
  5740  	if err == nil {
  5741  		panic("nil error")
  5742  	}
  5743  	cc := cs.cc
  5744  	cc.mu.Lock()
  5745  	cs.stopReqBody = err
  5746  	cc.cond.Broadcast()
  5747  	cc.mu.Unlock()
  5748  }
  5749  
  5750  type http2stickyErrWriter struct {
  5751  	w   io.Writer
  5752  	err *error
  5753  }
  5754  
  5755  func (sew http2stickyErrWriter) Write(p []byte) (n int, err error) {
  5756  	if *sew.err != nil {
  5757  		return 0, *sew.err
  5758  	}
  5759  	n, err = sew.w.Write(p)
  5760  	*sew.err = err
  5761  	return
  5762  }
  5763  
  5764  var http2ErrNoCachedConn = errors.New("http2: no cached connection was available")
  5765  
  5766  // RoundTripOpt are options for the Transport.RoundTripOpt method.
  5767  type http2RoundTripOpt struct {
  5768  	// OnlyCachedConn controls whether RoundTripOpt may
  5769  	// create a new TCP connection. If set true and
  5770  	// no cached connection is available, RoundTripOpt
  5771  	// will return ErrNoCachedConn.
  5772  	OnlyCachedConn bool
  5773  }
  5774  
  5775  func (t *http2Transport) RoundTrip(req *Request) (*Response, error) {
  5776  	return t.RoundTripOpt(req, http2RoundTripOpt{})
  5777  }
  5778  
  5779  // authorityAddr returns a given authority (a host/IP, or host:port / ip:port)
  5780  // and returns a host:port. The port 443 is added if needed.
  5781  func http2authorityAddr(scheme string, authority string) (addr string) {
  5782  	host, port, err := net.SplitHostPort(authority)
  5783  	if err != nil {
  5784  		port = "443"
  5785  		if scheme == "http" {
  5786  			port = "80"
  5787  		}
  5788  		host = authority
  5789  	}
  5790  	if a, err := idna.ToASCII(host); err == nil {
  5791  		host = a
  5792  	}
  5793  
  5794  	if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
  5795  		return host + ":" + port
  5796  	}
  5797  	return net.JoinHostPort(host, port)
  5798  }
  5799  
  5800  // RoundTripOpt is like RoundTrip, but takes options.
  5801  func (t *http2Transport) RoundTripOpt(req *Request, opt http2RoundTripOpt) (*Response, error) {
  5802  	if !(req.URL.Scheme == "https" || (req.URL.Scheme == "http" && t.AllowHTTP)) {
  5803  		return nil, errors.New("http2: unsupported scheme")
  5804  	}
  5805  
  5806  	addr := http2authorityAddr(req.URL.Scheme, req.URL.Host)
  5807  	for {
  5808  		cc, err := t.connPool().GetClientConn(req, addr)
  5809  		if err != nil {
  5810  			t.vlogf("http2: Transport failed to get client conn for %s: %v", addr, err)
  5811  			return nil, err
  5812  		}
  5813  		http2traceGotConn(req, cc)
  5814  		res, err := cc.RoundTrip(req)
  5815  		if err != nil {
  5816  			if req, err = http2shouldRetryRequest(req, err); err == nil {
  5817  				continue
  5818  			}
  5819  		}
  5820  		if err != nil {
  5821  			t.vlogf("RoundTrip failure: %v", err)
  5822  			return nil, err
  5823  		}
  5824  		return res, nil
  5825  	}
  5826  }
  5827  
  5828  // CloseIdleConnections closes any connections which were previously
  5829  // connected from previous requests but are now sitting idle.
  5830  // It does not interrupt any connections currently in use.
  5831  func (t *http2Transport) CloseIdleConnections() {
  5832  	if cp, ok := t.connPool().(http2clientConnPoolIdleCloser); ok {
  5833  		cp.closeIdleConnections()
  5834  	}
  5835  }
  5836  
  5837  var (
  5838  	http2errClientConnClosed   = errors.New("http2: client conn is closed")
  5839  	http2errClientConnUnusable = errors.New("http2: client conn not usable")
  5840  
  5841  	http2errClientConnGotGoAway                 = errors.New("http2: Transport received Server's graceful shutdown GOAWAY")
  5842  	http2errClientConnGotGoAwayAfterSomeReqBody = errors.New("http2: Transport received Server's graceful shutdown GOAWAY; some request body already written")
  5843  )
  5844  
  5845  // shouldRetryRequest is called by RoundTrip when a request fails to get
  5846  // response headers. It is always called with a non-nil error.
  5847  // It returns either a request to retry (either the same request, or a
  5848  // modified clone), or an error if the request can't be replayed.
  5849  func http2shouldRetryRequest(req *Request, err error) (*Request, error) {
  5850  	switch err {
  5851  	default:
  5852  		return nil, err
  5853  	case http2errClientConnUnusable, http2errClientConnGotGoAway:
  5854  		return req, nil
  5855  	case http2errClientConnGotGoAwayAfterSomeReqBody:
  5856  
  5857  		if req.Body == nil || http2reqBodyIsNoBody(req.Body) {
  5858  			return req, nil
  5859  		}
  5860  
  5861  		getBody := http2reqGetBody(req)
  5862  		if getBody == nil {
  5863  			return nil, errors.New("http2: Transport: peer server initiated graceful shutdown after some of Request.Body was written; define Request.GetBody to avoid this error")
  5864  		}
  5865  		body, err := getBody()
  5866  		if err != nil {
  5867  			return nil, err
  5868  		}
  5869  		newReq := *req
  5870  		newReq.Body = body
  5871  		return &newReq, nil
  5872  	}
  5873  }
  5874  
  5875  func (t *http2Transport) dialClientConn(addr string, singleUse bool) (*http2ClientConn, error) {
  5876  	host, _, err := net.SplitHostPort(addr)
  5877  	if err != nil {
  5878  		return nil, err
  5879  	}
  5880  	tconn, err := t.dialTLS()("tcp", addr, t.newTLSConfig(host))
  5881  	if err != nil {
  5882  		return nil, err
  5883  	}
  5884  	return t.newClientConn(tconn, singleUse)
  5885  }
  5886  
  5887  func (t *http2Transport) newTLSConfig(host string) *tls.Config {
  5888  	cfg := new(tls.Config)
  5889  	if t.TLSClientConfig != nil {
  5890  		*cfg = *http2cloneTLSConfig(t.TLSClientConfig)
  5891  	}
  5892  	if !http2strSliceContains(cfg.NextProtos, http2NextProtoTLS) {
  5893  		cfg.NextProtos = append([]string{http2NextProtoTLS}, cfg.NextProtos...)
  5894  	}
  5895  	if cfg.ServerName == "" {
  5896  		cfg.ServerName = host
  5897  	}
  5898  	return cfg
  5899  }
  5900  
  5901  func (t *http2Transport) dialTLS() func(string, string, *tls.Config) (net.Conn, error) {
  5902  	if t.DialTLS != nil {
  5903  		return t.DialTLS
  5904  	}
  5905  	return t.dialTLSDefault
  5906  }
  5907  
  5908  func (t *http2Transport) dialTLSDefault(network, addr string, cfg *tls.Config) (net.Conn, error) {
  5909  	cn, err := tls.Dial(network, addr, cfg)
  5910  	if err != nil {
  5911  		return nil, err
  5912  	}
  5913  	if err := cn.Handshake(); err != nil {
  5914  		return nil, err
  5915  	}
  5916  	if !cfg.InsecureSkipVerify {
  5917  		if err := cn.VerifyHostname(cfg.ServerName); err != nil {
  5918  			return nil, err
  5919  		}
  5920  	}
  5921  	state := cn.ConnectionState()
  5922  	if p := state.NegotiatedProtocol; p != http2NextProtoTLS {
  5923  		return nil, fmt.Errorf("http2: unexpected ALPN protocol %q; want %q", p, http2NextProtoTLS)
  5924  	}
  5925  	if !state.NegotiatedProtocolIsMutual {
  5926  		return nil, errors.New("http2: could not negotiate protocol mutually")
  5927  	}
  5928  	return cn, nil
  5929  }
  5930  
  5931  // disableKeepAlives reports whether connections should be closed as
  5932  // soon as possible after handling the first request.
  5933  func (t *http2Transport) disableKeepAlives() bool {
  5934  	return t.t1 != nil && t.t1.DisableKeepAlives
  5935  }
  5936  
  5937  func (t *http2Transport) expectContinueTimeout() time.Duration {
  5938  	if t.t1 == nil {
  5939  		return 0
  5940  	}
  5941  	return http2transportExpectContinueTimeout(t.t1)
  5942  }
  5943  
  5944  func (t *http2Transport) NewClientConn(c net.Conn) (*http2ClientConn, error) {
  5945  	return t.newClientConn(c, false)
  5946  }
  5947  
  5948  func (t *http2Transport) newClientConn(c net.Conn, singleUse bool) (*http2ClientConn, error) {
  5949  	cc := &http2ClientConn{
  5950  		t:                    t,
  5951  		tconn:                c,
  5952  		readerDone:           make(chan struct{}),
  5953  		nextStreamID:         1,
  5954  		maxFrameSize:         16 << 10,
  5955  		initialWindowSize:    65535,
  5956  		maxConcurrentStreams: 1000,
  5957  		streams:              make(map[uint32]*http2clientStream),
  5958  		singleUse:            singleUse,
  5959  		wantSettingsAck:      true,
  5960  		pings:                make(map[[8]byte]chan struct{}),
  5961  	}
  5962  	if d := t.idleConnTimeout(); d != 0 {
  5963  		cc.idleTimeout = d
  5964  		cc.idleTimer = time.AfterFunc(d, cc.onIdleTimeout)
  5965  	}
  5966  	if http2VerboseLogs {
  5967  		t.vlogf("http2: Transport creating client conn %p to %v", cc, c.RemoteAddr())
  5968  	}
  5969  
  5970  	cc.cond = sync.NewCond(&cc.mu)
  5971  	cc.flow.add(int32(http2initialWindowSize))
  5972  
  5973  	cc.bw = bufio.NewWriter(http2stickyErrWriter{c, &cc.werr})
  5974  	cc.br = bufio.NewReader(c)
  5975  	cc.fr = http2NewFramer(cc.bw, cc.br)
  5976  	cc.fr.ReadMetaHeaders = hpack.NewDecoder(http2initialHeaderTableSize, nil)
  5977  	cc.fr.MaxHeaderListSize = t.maxHeaderListSize()
  5978  
  5979  	cc.henc = hpack.NewEncoder(&cc.hbuf)
  5980  
  5981  	if cs, ok := c.(http2connectionStater); ok {
  5982  		state := cs.ConnectionState()
  5983  		cc.tlsState = &state
  5984  	}
  5985  
  5986  	initialSettings := []http2Setting{
  5987  		{ID: http2SettingEnablePush, Val: 0},
  5988  		{ID: http2SettingInitialWindowSize, Val: http2transportDefaultStreamFlow},
  5989  	}
  5990  	if max := t.maxHeaderListSize(); max != 0 {
  5991  		initialSettings = append(initialSettings, http2Setting{ID: http2SettingMaxHeaderListSize, Val: max})
  5992  	}
  5993  
  5994  	cc.bw.Write(http2clientPreface)
  5995  	cc.fr.WriteSettings(initialSettings...)
  5996  	cc.fr.WriteWindowUpdate(0, http2transportDefaultConnFlow)
  5997  	cc.inflow.add(http2transportDefaultConnFlow + http2initialWindowSize)
  5998  	cc.bw.Flush()
  5999  	if cc.werr != nil {
  6000  		return nil, cc.werr
  6001  	}
  6002  
  6003  	go cc.readLoop()
  6004  	return cc, nil
  6005  }
  6006  
  6007  func (cc *http2ClientConn) setGoAway(f *http2GoAwayFrame) {
  6008  	cc.mu.Lock()
  6009  	defer cc.mu.Unlock()
  6010  
  6011  	old := cc.goAway
  6012  	cc.goAway = f
  6013  
  6014  	if cc.goAwayDebug == "" {
  6015  		cc.goAwayDebug = string(f.DebugData())
  6016  	}
  6017  	if old != nil && old.ErrCode != http2ErrCodeNo {
  6018  		cc.goAway.ErrCode = old.ErrCode
  6019  	}
  6020  	last := f.LastStreamID
  6021  	for streamID, cs := range cc.streams {
  6022  		if streamID > last {
  6023  			select {
  6024  			case cs.resc <- http2resAndError{err: http2errClientConnGotGoAway}:
  6025  			default:
  6026  			}
  6027  		}
  6028  	}
  6029  }
  6030  
  6031  func (cc *http2ClientConn) CanTakeNewRequest() bool {
  6032  	cc.mu.Lock()
  6033  	defer cc.mu.Unlock()
  6034  	return cc.canTakeNewRequestLocked()
  6035  }
  6036  
  6037  func (cc *http2ClientConn) canTakeNewRequestLocked() bool {
  6038  	if cc.singleUse && cc.nextStreamID > 1 {
  6039  		return false
  6040  	}
  6041  	return cc.goAway == nil && !cc.closed &&
  6042  		int64(len(cc.streams)+1) < int64(cc.maxConcurrentStreams) &&
  6043  		cc.nextStreamID < math.MaxInt32
  6044  }
  6045  
  6046  // onIdleTimeout is called from a time.AfterFunc goroutine. It will
  6047  // only be called when we're idle, but because we're coming from a new
  6048  // goroutine, there could be a new request coming in at the same time,
  6049  // so this simply calls the synchronized closeIfIdle to shut down this
  6050  // connection. The timer could just call closeIfIdle, but this is more
  6051  // clear.
  6052  func (cc *http2ClientConn) onIdleTimeout() {
  6053  	cc.closeIfIdle()
  6054  }
  6055  
  6056  func (cc *http2ClientConn) closeIfIdle() {
  6057  	cc.mu.Lock()
  6058  	if len(cc.streams) > 0 {
  6059  		cc.mu.Unlock()
  6060  		return
  6061  	}
  6062  	cc.closed = true
  6063  	nextID := cc.nextStreamID
  6064  
  6065  	cc.mu.Unlock()
  6066  
  6067  	if http2VerboseLogs {
  6068  		cc.vlogf("http2: Transport closing idle conn %p (forSingleUse=%v, maxStream=%v)", cc, cc.singleUse, nextID-2)
  6069  	}
  6070  	cc.tconn.Close()
  6071  }
  6072  
  6073  const http2maxAllocFrameSize = 512 << 10
  6074  
  6075  // frameBuffer returns a scratch buffer suitable for writing DATA frames.
  6076  // They're capped at the min of the peer's max frame size or 512KB
  6077  // (kinda arbitrarily), but definitely capped so we don't allocate 4GB
  6078  // bufers.
  6079  func (cc *http2ClientConn) frameScratchBuffer() []byte {
  6080  	cc.mu.Lock()
  6081  	size := cc.maxFrameSize
  6082  	if size > http2maxAllocFrameSize {
  6083  		size = http2maxAllocFrameSize
  6084  	}
  6085  	for i, buf := range cc.freeBuf {
  6086  		if len(buf) >= int(size) {
  6087  			cc.freeBuf[i] = nil
  6088  			cc.mu.Unlock()
  6089  			return buf[:size]
  6090  		}
  6091  	}
  6092  	cc.mu.Unlock()
  6093  	return make([]byte, size)
  6094  }
  6095  
  6096  func (cc *http2ClientConn) putFrameScratchBuffer(buf []byte) {
  6097  	cc.mu.Lock()
  6098  	defer cc.mu.Unlock()
  6099  	const maxBufs = 4 // arbitrary; 4 concurrent requests per conn? investigate.
  6100  	if len(cc.freeBuf) < maxBufs {
  6101  		cc.freeBuf = append(cc.freeBuf, buf)
  6102  		return
  6103  	}
  6104  	for i, old := range cc.freeBuf {
  6105  		if old == nil {
  6106  			cc.freeBuf[i] = buf
  6107  			return
  6108  		}
  6109  	}
  6110  
  6111  }
  6112  
  6113  // errRequestCanceled is a copy of net/http's errRequestCanceled because it's not
  6114  // exported. At least they'll be DeepEqual for h1-vs-h2 comparisons tests.
  6115  var http2errRequestCanceled = errors.New("net/http: request canceled")
  6116  
  6117  func http2commaSeparatedTrailers(req *Request) (string, error) {
  6118  	keys := make([]string, 0, len(req.Trailer))
  6119  	for k := range req.Trailer {
  6120  		k = CanonicalHeaderKey(k)
  6121  		switch k {
  6122  		case "Transfer-Encoding", "Trailer", "Content-Length":
  6123  			return "", &http2badStringError{"invalid Trailer key", k}
  6124  		}
  6125  		keys = append(keys, k)
  6126  	}
  6127  	if len(keys) > 0 {
  6128  		sort.Strings(keys)
  6129  		return strings.Join(keys, ","), nil
  6130  	}
  6131  	return "", nil
  6132  }
  6133  
  6134  func (cc *http2ClientConn) responseHeaderTimeout() time.Duration {
  6135  	if cc.t.t1 != nil {
  6136  		return cc.t.t1.ResponseHeaderTimeout
  6137  	}
  6138  
  6139  	return 0
  6140  }
  6141  
  6142  // checkConnHeaders checks whether req has any invalid connection-level headers.
  6143  // per RFC 7540 section 8.1.2.2: Connection-Specific Header Fields.
  6144  // Certain headers are special-cased as okay but not transmitted later.
  6145  func http2checkConnHeaders(req *Request) error {
  6146  	if v := req.Header.Get("Upgrade"); v != "" {
  6147  		return fmt.Errorf("http2: invalid Upgrade request header: %q", req.Header["Upgrade"])
  6148  	}
  6149  	if vv := req.Header["Transfer-Encoding"]; len(vv) > 0 && (len(vv) > 1 || vv[0] != "" && vv[0] != "chunked") {
  6150  		return fmt.Errorf("http2: invalid Transfer-Encoding request header: %q", vv)
  6151  	}
  6152  	if vv := req.Header["Connection"]; len(vv) > 0 && (len(vv) > 1 || vv[0] != "" && vv[0] != "close" && vv[0] != "keep-alive") {
  6153  		return fmt.Errorf("http2: invalid Connection request header: %q", vv)
  6154  	}
  6155  	return nil
  6156  }
  6157  
  6158  // actualContentLength returns a sanitized version of
  6159  // req.ContentLength, where 0 actually means zero (not unknown) and -1
  6160  // means unknown.
  6161  func http2actualContentLength(req *Request) int64 {
  6162  	if req.Body == nil {
  6163  		return 0
  6164  	}
  6165  	if req.ContentLength != 0 {
  6166  		return req.ContentLength
  6167  	}
  6168  	return -1
  6169  }
  6170  
  6171  func (cc *http2ClientConn) RoundTrip(req *Request) (*Response, error) {
  6172  	if err := http2checkConnHeaders(req); err != nil {
  6173  		return nil, err
  6174  	}
  6175  	if cc.idleTimer != nil {
  6176  		cc.idleTimer.Stop()
  6177  	}
  6178  
  6179  	trailers, err := http2commaSeparatedTrailers(req)
  6180  	if err != nil {
  6181  		return nil, err
  6182  	}
  6183  	hasTrailers := trailers != ""
  6184  
  6185  	cc.mu.Lock()
  6186  	cc.lastActive = time.Now()
  6187  	if cc.closed || !cc.canTakeNewRequestLocked() {
  6188  		cc.mu.Unlock()
  6189  		return nil, http2errClientConnUnusable
  6190  	}
  6191  
  6192  	body := req.Body
  6193  	hasBody := body != nil
  6194  	contentLen := http2actualContentLength(req)
  6195  
  6196  	// TODO(bradfitz): this is a copy of the logic in net/http. Unify somewhere?
  6197  	var requestedGzip bool
  6198  	if !cc.t.disableCompression() &&
  6199  		req.Header.Get("Accept-Encoding") == "" &&
  6200  		req.Header.Get("Range") == "" &&
  6201  		req.Method != "HEAD" {
  6202  
  6203  		requestedGzip = true
  6204  	}
  6205  
  6206  	hdrs, err := cc.encodeHeaders(req, requestedGzip, trailers, contentLen)
  6207  	if err != nil {
  6208  		cc.mu.Unlock()
  6209  		return nil, err
  6210  	}
  6211  
  6212  	cs := cc.newStream()
  6213  	cs.req = req
  6214  	cs.trace = http2requestTrace(req)
  6215  	cs.requestedGzip = requestedGzip
  6216  	bodyWriter := cc.t.getBodyWriterState(cs, body)
  6217  	cs.on100 = bodyWriter.on100
  6218  
  6219  	cc.wmu.Lock()
  6220  	endStream := !hasBody && !hasTrailers
  6221  	werr := cc.writeHeaders(cs.ID, endStream, hdrs)
  6222  	cc.wmu.Unlock()
  6223  	http2traceWroteHeaders(cs.trace)
  6224  	cc.mu.Unlock()
  6225  
  6226  	if werr != nil {
  6227  		if hasBody {
  6228  			req.Body.Close()
  6229  			bodyWriter.cancel()
  6230  		}
  6231  		cc.forgetStreamID(cs.ID)
  6232  
  6233  		http2traceWroteRequest(cs.trace, werr)
  6234  		return nil, werr
  6235  	}
  6236  
  6237  	var respHeaderTimer <-chan time.Time
  6238  	if hasBody {
  6239  		bodyWriter.scheduleBodyWrite()
  6240  	} else {
  6241  		http2traceWroteRequest(cs.trace, nil)
  6242  		if d := cc.responseHeaderTimeout(); d != 0 {
  6243  			timer := time.NewTimer(d)
  6244  			defer timer.Stop()
  6245  			respHeaderTimer = timer.C
  6246  		}
  6247  	}
  6248  
  6249  	readLoopResCh := cs.resc
  6250  	bodyWritten := false
  6251  	ctx := http2reqContext(req)
  6252  
  6253  	handleReadLoopResponse := func(re http2resAndError) (*Response, error) {
  6254  		res := re.res
  6255  		if re.err != nil || res.StatusCode > 299 {
  6256  
  6257  			bodyWriter.cancel()
  6258  			cs.abortRequestBodyWrite(http2errStopReqBodyWrite)
  6259  		}
  6260  		if re.err != nil {
  6261  			if re.err == http2errClientConnGotGoAway {
  6262  				cc.mu.Lock()
  6263  				if cs.startedWrite {
  6264  					re.err = http2errClientConnGotGoAwayAfterSomeReqBody
  6265  				}
  6266  				cc.mu.Unlock()
  6267  			}
  6268  			cc.forgetStreamID(cs.ID)
  6269  			return nil, re.err
  6270  		}
  6271  		res.Request = req
  6272  		res.TLS = cc.tlsState
  6273  		return res, nil
  6274  	}
  6275  
  6276  	for {
  6277  		select {
  6278  		case re := <-readLoopResCh:
  6279  			return handleReadLoopResponse(re)
  6280  		case <-respHeaderTimer:
  6281  			cc.forgetStreamID(cs.ID)
  6282  			if !hasBody || bodyWritten {
  6283  				cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil)
  6284  			} else {
  6285  				bodyWriter.cancel()
  6286  				cs.abortRequestBodyWrite(http2errStopReqBodyWriteAndCancel)
  6287  			}
  6288  			return nil, http2errTimeout
  6289  		case <-ctx.Done():
  6290  			cc.forgetStreamID(cs.ID)
  6291  			if !hasBody || bodyWritten {
  6292  				cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil)
  6293  			} else {
  6294  				bodyWriter.cancel()
  6295  				cs.abortRequestBodyWrite(http2errStopReqBodyWriteAndCancel)
  6296  			}
  6297  			return nil, ctx.Err()
  6298  		case <-req.Cancel:
  6299  			cc.forgetStreamID(cs.ID)
  6300  			if !hasBody || bodyWritten {
  6301  				cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil)
  6302  			} else {
  6303  				bodyWriter.cancel()
  6304  				cs.abortRequestBodyWrite(http2errStopReqBodyWriteAndCancel)
  6305  			}
  6306  			return nil, http2errRequestCanceled
  6307  		case <-cs.peerReset:
  6308  
  6309  			return nil, cs.resetErr
  6310  		case err := <-bodyWriter.resc:
  6311  
  6312  			select {
  6313  			case re := <-readLoopResCh:
  6314  				return handleReadLoopResponse(re)
  6315  			default:
  6316  			}
  6317  			if err != nil {
  6318  				return nil, err
  6319  			}
  6320  			bodyWritten = true
  6321  			if d := cc.responseHeaderTimeout(); d != 0 {
  6322  				timer := time.NewTimer(d)
  6323  				defer timer.Stop()
  6324  				respHeaderTimer = timer.C
  6325  			}
  6326  		}
  6327  	}
  6328  }
  6329  
  6330  // requires cc.wmu be held
  6331  func (cc *http2ClientConn) writeHeaders(streamID uint32, endStream bool, hdrs []byte) error {
  6332  	first := true
  6333  	frameSize := int(cc.maxFrameSize)
  6334  	for len(hdrs) > 0 && cc.werr == nil {
  6335  		chunk := hdrs
  6336  		if len(chunk) > frameSize {
  6337  			chunk = chunk[:frameSize]
  6338  		}
  6339  		hdrs = hdrs[len(chunk):]
  6340  		endHeaders := len(hdrs) == 0
  6341  		if first {
  6342  			cc.fr.WriteHeaders(http2HeadersFrameParam{
  6343  				StreamID:      streamID,
  6344  				BlockFragment: chunk,
  6345  				EndStream:     endStream,
  6346  				EndHeaders:    endHeaders,
  6347  			})
  6348  			first = false
  6349  		} else {
  6350  			cc.fr.WriteContinuation(streamID, endHeaders, chunk)
  6351  		}
  6352  	}
  6353  
  6354  	cc.bw.Flush()
  6355  	return cc.werr
  6356  }
  6357  
  6358  // internal error values; they don't escape to callers
  6359  var (
  6360  	// abort request body write; don't send cancel
  6361  	http2errStopReqBodyWrite = errors.New("http2: aborting request body write")
  6362  
  6363  	// abort request body write, but send stream reset of cancel.
  6364  	http2errStopReqBodyWriteAndCancel = errors.New("http2: canceling request")
  6365  )
  6366  
  6367  func (cs *http2clientStream) writeRequestBody(body io.Reader, bodyCloser io.Closer) (err error) {
  6368  	cc := cs.cc
  6369  	sentEnd := false
  6370  	buf := cc.frameScratchBuffer()
  6371  	defer cc.putFrameScratchBuffer(buf)
  6372  
  6373  	defer func() {
  6374  		http2traceWroteRequest(cs.trace, err)
  6375  
  6376  		cerr := bodyCloser.Close()
  6377  		if err == nil {
  6378  			err = cerr
  6379  		}
  6380  	}()
  6381  
  6382  	req := cs.req
  6383  	hasTrailers := req.Trailer != nil
  6384  
  6385  	var sawEOF bool
  6386  	for !sawEOF {
  6387  		n, err := body.Read(buf)
  6388  		if err == io.EOF {
  6389  			sawEOF = true
  6390  			err = nil
  6391  		} else if err != nil {
  6392  			return err
  6393  		}
  6394  
  6395  		remain := buf[:n]
  6396  		for len(remain) > 0 && err == nil {
  6397  			var allowed int32
  6398  			allowed, err = cs.awaitFlowControl(len(remain))
  6399  			switch {
  6400  			case err == http2errStopReqBodyWrite:
  6401  				return err
  6402  			case err == http2errStopReqBodyWriteAndCancel:
  6403  				cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil)
  6404  				return err
  6405  			case err != nil:
  6406  				return err
  6407  			}
  6408  			cc.wmu.Lock()
  6409  			data := remain[:allowed]
  6410  			remain = remain[allowed:]
  6411  			sentEnd = sawEOF && len(remain) == 0 && !hasTrailers
  6412  			err = cc.fr.WriteData(cs.ID, sentEnd, data)
  6413  			if err == nil {
  6414  
  6415  				err = cc.bw.Flush()
  6416  			}
  6417  			cc.wmu.Unlock()
  6418  		}
  6419  		if err != nil {
  6420  			return err
  6421  		}
  6422  	}
  6423  
  6424  	if sentEnd {
  6425  
  6426  		return nil
  6427  	}
  6428  
  6429  	var trls []byte
  6430  	if hasTrailers {
  6431  		cc.mu.Lock()
  6432  		defer cc.mu.Unlock()
  6433  		trls = cc.encodeTrailers(req)
  6434  	}
  6435  
  6436  	cc.wmu.Lock()
  6437  	defer cc.wmu.Unlock()
  6438  
  6439  	if len(trls) > 0 {
  6440  		err = cc.writeHeaders(cs.ID, true, trls)
  6441  	} else {
  6442  		err = cc.fr.WriteData(cs.ID, true, nil)
  6443  	}
  6444  	if ferr := cc.bw.Flush(); ferr != nil && err == nil {
  6445  		err = ferr
  6446  	}
  6447  	return err
  6448  }
  6449  
  6450  // awaitFlowControl waits for [1, min(maxBytes, cc.cs.maxFrameSize)] flow
  6451  // control tokens from the server.
  6452  // It returns either the non-zero number of tokens taken or an error
  6453  // if the stream is dead.
  6454  func (cs *http2clientStream) awaitFlowControl(maxBytes int) (taken int32, err error) {
  6455  	cc := cs.cc
  6456  	cc.mu.Lock()
  6457  	defer cc.mu.Unlock()
  6458  	for {
  6459  		if cc.closed {
  6460  			return 0, http2errClientConnClosed
  6461  		}
  6462  		if cs.stopReqBody != nil {
  6463  			return 0, cs.stopReqBody
  6464  		}
  6465  		if err := cs.checkResetOrDone(); err != nil {
  6466  			return 0, err
  6467  		}
  6468  		if a := cs.flow.available(); a > 0 {
  6469  			take := a
  6470  			if int(take) > maxBytes {
  6471  
  6472  				take = int32(maxBytes)
  6473  			}
  6474  			if take > int32(cc.maxFrameSize) {
  6475  				take = int32(cc.maxFrameSize)
  6476  			}
  6477  			cs.flow.take(take)
  6478  			return take, nil
  6479  		}
  6480  		cc.cond.Wait()
  6481  	}
  6482  }
  6483  
  6484  type http2badStringError struct {
  6485  	what string
  6486  	str  string
  6487  }
  6488  
  6489  func (e *http2badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) }
  6490  
  6491  // requires cc.mu be held.
  6492  func (cc *http2ClientConn) encodeHeaders(req *Request, addGzipHeader bool, trailers string, contentLength int64) ([]byte, error) {
  6493  	cc.hbuf.Reset()
  6494  
  6495  	host := req.Host
  6496  	if host == "" {
  6497  		host = req.URL.Host
  6498  	}
  6499  	host, err := httplex.PunycodeHostPort(host)
  6500  	if err != nil {
  6501  		return nil, err
  6502  	}
  6503  
  6504  	var path string
  6505  	if req.Method != "CONNECT" {
  6506  		path = req.URL.RequestURI()
  6507  		if !http2validPseudoPath(path) {
  6508  			orig := path
  6509  			path = strings.TrimPrefix(path, req.URL.Scheme+"://"+host)
  6510  			if !http2validPseudoPath(path) {
  6511  				if req.URL.Opaque != "" {
  6512  					return nil, fmt.Errorf("invalid request :path %q from URL.Opaque = %q", orig, req.URL.Opaque)
  6513  				} else {
  6514  					return nil, fmt.Errorf("invalid request :path %q", orig)
  6515  				}
  6516  			}
  6517  		}
  6518  	}
  6519  
  6520  	for k, vv := range req.Header {
  6521  		if !httplex.ValidHeaderFieldName(k) {
  6522  			return nil, fmt.Errorf("invalid HTTP header name %q", k)
  6523  		}
  6524  		for _, v := range vv {
  6525  			if !httplex.ValidHeaderFieldValue(v) {
  6526  				return nil, fmt.Errorf("invalid HTTP header value %q for header %q", v, k)
  6527  			}
  6528  		}
  6529  	}
  6530  
  6531  	cc.writeHeader(":authority", host)
  6532  	cc.writeHeader(":method", req.Method)
  6533  	if req.Method != "CONNECT" {
  6534  		cc.writeHeader(":path", path)
  6535  		cc.writeHeader(":scheme", req.URL.Scheme)
  6536  	}
  6537  	if trailers != "" {
  6538  		cc.writeHeader("trailer", trailers)
  6539  	}
  6540  
  6541  	var didUA bool
  6542  	for k, vv := range req.Header {
  6543  		lowKey := strings.ToLower(k)
  6544  		switch lowKey {
  6545  		case "host", "content-length":
  6546  
  6547  			continue
  6548  		case "connection", "proxy-connection", "transfer-encoding", "upgrade", "keep-alive":
  6549  
  6550  			continue
  6551  		case "user-agent":
  6552  
  6553  			didUA = true
  6554  			if len(vv) < 1 {
  6555  				continue
  6556  			}
  6557  			vv = vv[:1]
  6558  			if vv[0] == "" {
  6559  				continue
  6560  			}
  6561  		}
  6562  		for _, v := range vv {
  6563  			cc.writeHeader(lowKey, v)
  6564  		}
  6565  	}
  6566  	if http2shouldSendReqContentLength(req.Method, contentLength) {
  6567  		cc.writeHeader("content-length", strconv.FormatInt(contentLength, 10))
  6568  	}
  6569  	if addGzipHeader {
  6570  		cc.writeHeader("accept-encoding", "gzip")
  6571  	}
  6572  	if !didUA {
  6573  		cc.writeHeader("user-agent", http2defaultUserAgent)
  6574  	}
  6575  	return cc.hbuf.Bytes(), nil
  6576  }
  6577  
  6578  // shouldSendReqContentLength reports whether the http2.Transport should send
  6579  // a "content-length" request header. This logic is basically a copy of the net/http
  6580  // transferWriter.shouldSendContentLength.
  6581  // The contentLength is the corrected contentLength (so 0 means actually 0, not unknown).
  6582  // -1 means unknown.
  6583  func http2shouldSendReqContentLength(method string, contentLength int64) bool {
  6584  	if contentLength > 0 {
  6585  		return true
  6586  	}
  6587  	if contentLength < 0 {
  6588  		return false
  6589  	}
  6590  
  6591  	switch method {
  6592  	case "POST", "PUT", "PATCH":
  6593  		return true
  6594  	default:
  6595  		return false
  6596  	}
  6597  }
  6598  
  6599  // requires cc.mu be held.
  6600  func (cc *http2ClientConn) encodeTrailers(req *Request) []byte {
  6601  	cc.hbuf.Reset()
  6602  	for k, vv := range req.Trailer {
  6603  
  6604  		lowKey := strings.ToLower(k)
  6605  		for _, v := range vv {
  6606  			cc.writeHeader(lowKey, v)
  6607  		}
  6608  	}
  6609  	return cc.hbuf.Bytes()
  6610  }
  6611  
  6612  func (cc *http2ClientConn) writeHeader(name, value string) {
  6613  	if http2VerboseLogs {
  6614  		log.Printf("http2: Transport encoding header %q = %q", name, value)
  6615  	}
  6616  	cc.henc.WriteField(hpack.HeaderField{Name: name, Value: value})
  6617  }
  6618  
  6619  type http2resAndError struct {
  6620  	res *Response
  6621  	err error
  6622  }
  6623  
  6624  // requires cc.mu be held.
  6625  func (cc *http2ClientConn) newStream() *http2clientStream {
  6626  	cs := &http2clientStream{
  6627  		cc:        cc,
  6628  		ID:        cc.nextStreamID,
  6629  		resc:      make(chan http2resAndError, 1),
  6630  		peerReset: make(chan struct{}),
  6631  		done:      make(chan struct{}),
  6632  	}
  6633  	cs.flow.add(int32(cc.initialWindowSize))
  6634  	cs.flow.setConnFlow(&cc.flow)
  6635  	cs.inflow.add(http2transportDefaultStreamFlow)
  6636  	cs.inflow.setConnFlow(&cc.inflow)
  6637  	cc.nextStreamID += 2
  6638  	cc.streams[cs.ID] = cs
  6639  	return cs
  6640  }
  6641  
  6642  func (cc *http2ClientConn) forgetStreamID(id uint32) {
  6643  	cc.streamByID(id, true)
  6644  }
  6645  
  6646  func (cc *http2ClientConn) streamByID(id uint32, andRemove bool) *http2clientStream {
  6647  	cc.mu.Lock()
  6648  	defer cc.mu.Unlock()
  6649  	cs := cc.streams[id]
  6650  	if andRemove && cs != nil && !cc.closed {
  6651  		cc.lastActive = time.Now()
  6652  		delete(cc.streams, id)
  6653  		if len(cc.streams) == 0 && cc.idleTimer != nil {
  6654  			cc.idleTimer.Reset(cc.idleTimeout)
  6655  		}
  6656  		close(cs.done)
  6657  		cc.cond.Broadcast()
  6658  	}
  6659  	return cs
  6660  }
  6661  
  6662  // clientConnReadLoop is the state owned by the clientConn's frame-reading readLoop.
  6663  type http2clientConnReadLoop struct {
  6664  	cc            *http2ClientConn
  6665  	activeRes     map[uint32]*http2clientStream // keyed by streamID
  6666  	closeWhenIdle bool
  6667  }
  6668  
  6669  // readLoop runs in its own goroutine and reads and dispatches frames.
  6670  func (cc *http2ClientConn) readLoop() {
  6671  	rl := &http2clientConnReadLoop{
  6672  		cc:        cc,
  6673  		activeRes: make(map[uint32]*http2clientStream),
  6674  	}
  6675  
  6676  	defer rl.cleanup()
  6677  	cc.readerErr = rl.run()
  6678  	if ce, ok := cc.readerErr.(http2ConnectionError); ok {
  6679  		cc.wmu.Lock()
  6680  		cc.fr.WriteGoAway(0, http2ErrCode(ce), nil)
  6681  		cc.wmu.Unlock()
  6682  	}
  6683  }
  6684  
  6685  // GoAwayError is returned by the Transport when the server closes the
  6686  // TCP connection after sending a GOAWAY frame.
  6687  type http2GoAwayError struct {
  6688  	LastStreamID uint32
  6689  	ErrCode      http2ErrCode
  6690  	DebugData    string
  6691  }
  6692  
  6693  func (e http2GoAwayError) Error() string {
  6694  	return fmt.Sprintf("http2: server sent GOAWAY and closed the connection; LastStreamID=%v, ErrCode=%v, debug=%q",
  6695  		e.LastStreamID, e.ErrCode, e.DebugData)
  6696  }
  6697  
  6698  func http2isEOFOrNetReadError(err error) bool {
  6699  	if err == io.EOF {
  6700  		return true
  6701  	}
  6702  	ne, ok := err.(*net.OpError)
  6703  	return ok && ne.Op == "read"
  6704  }
  6705  
  6706  func (rl *http2clientConnReadLoop) cleanup() {
  6707  	cc := rl.cc
  6708  	defer cc.tconn.Close()
  6709  	defer cc.t.connPool().MarkDead(cc)
  6710  	defer close(cc.readerDone)
  6711  
  6712  	if cc.idleTimer != nil {
  6713  		cc.idleTimer.Stop()
  6714  	}
  6715  
  6716  	err := cc.readerErr
  6717  	cc.mu.Lock()
  6718  	if cc.goAway != nil && http2isEOFOrNetReadError(err) {
  6719  		err = http2GoAwayError{
  6720  			LastStreamID: cc.goAway.LastStreamID,
  6721  			ErrCode:      cc.goAway.ErrCode,
  6722  			DebugData:    cc.goAwayDebug,
  6723  		}
  6724  	} else if err == io.EOF {
  6725  		err = io.ErrUnexpectedEOF
  6726  	}
  6727  	for _, cs := range rl.activeRes {
  6728  		cs.bufPipe.CloseWithError(err)
  6729  	}
  6730  	for _, cs := range cc.streams {
  6731  		select {
  6732  		case cs.resc <- http2resAndError{err: err}:
  6733  		default:
  6734  		}
  6735  		close(cs.done)
  6736  	}
  6737  	cc.closed = true
  6738  	cc.cond.Broadcast()
  6739  	cc.mu.Unlock()
  6740  }
  6741  
  6742  func (rl *http2clientConnReadLoop) run() error {
  6743  	cc := rl.cc
  6744  	rl.closeWhenIdle = cc.t.disableKeepAlives() || cc.singleUse
  6745  	gotReply := false
  6746  	gotSettings := false
  6747  	for {
  6748  		f, err := cc.fr.ReadFrame()
  6749  		if err != nil {
  6750  			cc.vlogf("http2: Transport readFrame error on conn %p: (%T) %v", cc, err, err)
  6751  		}
  6752  		if se, ok := err.(http2StreamError); ok {
  6753  			if cs := cc.streamByID(se.StreamID, true); cs != nil {
  6754  				cs.cc.writeStreamReset(cs.ID, se.Code, err)
  6755  				if se.Cause == nil {
  6756  					se.Cause = cc.fr.errDetail
  6757  				}
  6758  				rl.endStreamError(cs, se)
  6759  			}
  6760  			continue
  6761  		} else if err != nil {
  6762  			return err
  6763  		}
  6764  		if http2VerboseLogs {
  6765  			cc.vlogf("http2: Transport received %s", http2summarizeFrame(f))
  6766  		}
  6767  		if !gotSettings {
  6768  			if _, ok := f.(*http2SettingsFrame); !ok {
  6769  				cc.logf("protocol error: received %T before a SETTINGS frame", f)
  6770  				return http2ConnectionError(http2ErrCodeProtocol)
  6771  			}
  6772  			gotSettings = true
  6773  		}
  6774  		maybeIdle := false
  6775  
  6776  		switch f := f.(type) {
  6777  		case *http2MetaHeadersFrame:
  6778  			err = rl.processHeaders(f)
  6779  			maybeIdle = true
  6780  			gotReply = true
  6781  		case *http2DataFrame:
  6782  			err = rl.processData(f)
  6783  			maybeIdle = true
  6784  		case *http2GoAwayFrame:
  6785  			err = rl.processGoAway(f)
  6786  			maybeIdle = true
  6787  		case *http2RSTStreamFrame:
  6788  			err = rl.processResetStream(f)
  6789  			maybeIdle = true
  6790  		case *http2SettingsFrame:
  6791  			err = rl.processSettings(f)
  6792  		case *http2PushPromiseFrame:
  6793  			err = rl.processPushPromise(f)
  6794  		case *http2WindowUpdateFrame:
  6795  			err = rl.processWindowUpdate(f)
  6796  		case *http2PingFrame:
  6797  			err = rl.processPing(f)
  6798  		default:
  6799  			cc.logf("Transport: unhandled response frame type %T", f)
  6800  		}
  6801  		if err != nil {
  6802  			if http2VerboseLogs {
  6803  				cc.vlogf("http2: Transport conn %p received error from processing frame %v: %v", cc, http2summarizeFrame(f), err)
  6804  			}
  6805  			return err
  6806  		}
  6807  		if rl.closeWhenIdle && gotReply && maybeIdle && len(rl.activeRes) == 0 {
  6808  			cc.closeIfIdle()
  6809  		}
  6810  	}
  6811  }
  6812  
  6813  func (rl *http2clientConnReadLoop) processHeaders(f *http2MetaHeadersFrame) error {
  6814  	cc := rl.cc
  6815  	cs := cc.streamByID(f.StreamID, f.StreamEnded())
  6816  	if cs == nil {
  6817  
  6818  		return nil
  6819  	}
  6820  	if !cs.firstByte {
  6821  		if cs.trace != nil {
  6822  
  6823  			http2traceFirstResponseByte(cs.trace)
  6824  		}
  6825  		cs.firstByte = true
  6826  	}
  6827  	if !cs.pastHeaders {
  6828  		cs.pastHeaders = true
  6829  	} else {
  6830  		return rl.processTrailers(cs, f)
  6831  	}
  6832  
  6833  	res, err := rl.handleResponse(cs, f)
  6834  	if err != nil {
  6835  		if _, ok := err.(http2ConnectionError); ok {
  6836  			return err
  6837  		}
  6838  
  6839  		cs.cc.writeStreamReset(f.StreamID, http2ErrCodeProtocol, err)
  6840  		cs.resc <- http2resAndError{err: err}
  6841  		return nil
  6842  	}
  6843  	if res == nil {
  6844  
  6845  		return nil
  6846  	}
  6847  	if res.Body != http2noBody {
  6848  		rl.activeRes[cs.ID] = cs
  6849  	}
  6850  	cs.resTrailer = &res.Trailer
  6851  	cs.resc <- http2resAndError{res: res}
  6852  	return nil
  6853  }
  6854  
  6855  // may return error types nil, or ConnectionError. Any other error value
  6856  // is a StreamError of type ErrCodeProtocol. The returned error in that case
  6857  // is the detail.
  6858  //
  6859  // As a special case, handleResponse may return (nil, nil) to skip the
  6860  // frame (currently only used for 100 expect continue). This special
  6861  // case is going away after Issue 13851 is fixed.
  6862  func (rl *http2clientConnReadLoop) handleResponse(cs *http2clientStream, f *http2MetaHeadersFrame) (*Response, error) {
  6863  	if f.Truncated {
  6864  		return nil, http2errResponseHeaderListSize
  6865  	}
  6866  
  6867  	status := f.PseudoValue("status")
  6868  	if status == "" {
  6869  		return nil, errors.New("missing status pseudo header")
  6870  	}
  6871  	statusCode, err := strconv.Atoi(status)
  6872  	if err != nil {
  6873  		return nil, errors.New("malformed non-numeric status pseudo header")
  6874  	}
  6875  
  6876  	if statusCode == 100 {
  6877  		http2traceGot100Continue(cs.trace)
  6878  		if cs.on100 != nil {
  6879  			cs.on100()
  6880  		}
  6881  		cs.pastHeaders = false
  6882  		return nil, nil
  6883  	}
  6884  
  6885  	header := make(Header)
  6886  	res := &Response{
  6887  		Proto:      "HTTP/2.0",
  6888  		ProtoMajor: 2,
  6889  		Header:     header,
  6890  		StatusCode: statusCode,
  6891  		Status:     status + " " + StatusText(statusCode),
  6892  	}
  6893  	for _, hf := range f.RegularFields() {
  6894  		key := CanonicalHeaderKey(hf.Name)
  6895  		if key == "Trailer" {
  6896  			t := res.Trailer
  6897  			if t == nil {
  6898  				t = make(Header)
  6899  				res.Trailer = t
  6900  			}
  6901  			http2foreachHeaderElement(hf.Value, func(v string) {
  6902  				t[CanonicalHeaderKey(v)] = nil
  6903  			})
  6904  		} else {
  6905  			header[key] = append(header[key], hf.Value)
  6906  		}
  6907  	}
  6908  
  6909  	streamEnded := f.StreamEnded()
  6910  	isHead := cs.req.Method == "HEAD"
  6911  	if !streamEnded || isHead {
  6912  		res.ContentLength = -1
  6913  		if clens := res.Header["Content-Length"]; len(clens) == 1 {
  6914  			if clen64, err := strconv.ParseInt(clens[0], 10, 64); err == nil {
  6915  				res.ContentLength = clen64
  6916  			} else {
  6917  
  6918  			}
  6919  		} else if len(clens) > 1 {
  6920  
  6921  		}
  6922  	}
  6923  
  6924  	if streamEnded || isHead {
  6925  		res.Body = http2noBody
  6926  		return res, nil
  6927  	}
  6928  
  6929  	cs.bufPipe = http2pipe{b: &http2dataBuffer{expected: res.ContentLength}}
  6930  	cs.bytesRemain = res.ContentLength
  6931  	res.Body = http2transportResponseBody{cs}
  6932  	go cs.awaitRequestCancel(cs.req)
  6933  
  6934  	if cs.requestedGzip && res.Header.Get("Content-Encoding") == "gzip" {
  6935  		res.Header.Del("Content-Encoding")
  6936  		res.Header.Del("Content-Length")
  6937  		res.ContentLength = -1
  6938  		res.Body = &http2gzipReader{body: res.Body}
  6939  		http2setResponseUncompressed(res)
  6940  	}
  6941  	return res, nil
  6942  }
  6943  
  6944  func (rl *http2clientConnReadLoop) processTrailers(cs *http2clientStream, f *http2MetaHeadersFrame) error {
  6945  	if cs.pastTrailers {
  6946  
  6947  		return http2ConnectionError(http2ErrCodeProtocol)
  6948  	}
  6949  	cs.pastTrailers = true
  6950  	if !f.StreamEnded() {
  6951  
  6952  		return http2ConnectionError(http2ErrCodeProtocol)
  6953  	}
  6954  	if len(f.PseudoFields()) > 0 {
  6955  
  6956  		return http2ConnectionError(http2ErrCodeProtocol)
  6957  	}
  6958  
  6959  	trailer := make(Header)
  6960  	for _, hf := range f.RegularFields() {
  6961  		key := CanonicalHeaderKey(hf.Name)
  6962  		trailer[key] = append(trailer[key], hf.Value)
  6963  	}
  6964  	cs.trailer = trailer
  6965  
  6966  	rl.endStream(cs)
  6967  	return nil
  6968  }
  6969  
  6970  // transportResponseBody is the concrete type of Transport.RoundTrip's
  6971  // Response.Body. It is an io.ReadCloser. On Read, it reads from cs.body.
  6972  // On Close it sends RST_STREAM if EOF wasn't already seen.
  6973  type http2transportResponseBody struct {
  6974  	cs *http2clientStream
  6975  }
  6976  
  6977  func (b http2transportResponseBody) Read(p []byte) (n int, err error) {
  6978  	cs := b.cs
  6979  	cc := cs.cc
  6980  
  6981  	if cs.readErr != nil {
  6982  		return 0, cs.readErr
  6983  	}
  6984  	n, err = b.cs.bufPipe.Read(p)
  6985  	if cs.bytesRemain != -1 {
  6986  		if int64(n) > cs.bytesRemain {
  6987  			n = int(cs.bytesRemain)
  6988  			if err == nil {
  6989  				err = errors.New("net/http: server replied with more than declared Content-Length; truncated")
  6990  				cc.writeStreamReset(cs.ID, http2ErrCodeProtocol, err)
  6991  			}
  6992  			cs.readErr = err
  6993  			return int(cs.bytesRemain), err
  6994  		}
  6995  		cs.bytesRemain -= int64(n)
  6996  		if err == io.EOF && cs.bytesRemain > 0 {
  6997  			err = io.ErrUnexpectedEOF
  6998  			cs.readErr = err
  6999  			return n, err
  7000  		}
  7001  	}
  7002  	if n == 0 {
  7003  
  7004  		return
  7005  	}
  7006  
  7007  	cc.mu.Lock()
  7008  	defer cc.mu.Unlock()
  7009  
  7010  	var connAdd, streamAdd int32
  7011  
  7012  	if v := cc.inflow.available(); v < http2transportDefaultConnFlow/2 {
  7013  		connAdd = http2transportDefaultConnFlow - v
  7014  		cc.inflow.add(connAdd)
  7015  	}
  7016  	if err == nil {
  7017  
  7018  		v := int(cs.inflow.available()) + cs.bufPipe.Len()
  7019  		if v < http2transportDefaultStreamFlow-http2transportDefaultStreamMinRefresh {
  7020  			streamAdd = int32(http2transportDefaultStreamFlow - v)
  7021  			cs.inflow.add(streamAdd)
  7022  		}
  7023  	}
  7024  	if connAdd != 0 || streamAdd != 0 {
  7025  		cc.wmu.Lock()
  7026  		defer cc.wmu.Unlock()
  7027  		if connAdd != 0 {
  7028  			cc.fr.WriteWindowUpdate(0, http2mustUint31(connAdd))
  7029  		}
  7030  		if streamAdd != 0 {
  7031  			cc.fr.WriteWindowUpdate(cs.ID, http2mustUint31(streamAdd))
  7032  		}
  7033  		cc.bw.Flush()
  7034  	}
  7035  	return
  7036  }
  7037  
  7038  var http2errClosedResponseBody = errors.New("http2: response body closed")
  7039  
  7040  func (b http2transportResponseBody) Close() error {
  7041  	cs := b.cs
  7042  	cc := cs.cc
  7043  
  7044  	serverSentStreamEnd := cs.bufPipe.Err() == io.EOF
  7045  	unread := cs.bufPipe.Len()
  7046  
  7047  	if unread > 0 || !serverSentStreamEnd {
  7048  		cc.mu.Lock()
  7049  		cc.wmu.Lock()
  7050  		if !serverSentStreamEnd {
  7051  			cc.fr.WriteRSTStream(cs.ID, http2ErrCodeCancel)
  7052  		}
  7053  
  7054  		if unread > 0 {
  7055  			cc.inflow.add(int32(unread))
  7056  			cc.fr.WriteWindowUpdate(0, uint32(unread))
  7057  		}
  7058  		cc.bw.Flush()
  7059  		cc.wmu.Unlock()
  7060  		cc.mu.Unlock()
  7061  	}
  7062  
  7063  	cs.bufPipe.BreakWithError(http2errClosedResponseBody)
  7064  	return nil
  7065  }
  7066  
  7067  func (rl *http2clientConnReadLoop) processData(f *http2DataFrame) error {
  7068  	cc := rl.cc
  7069  	cs := cc.streamByID(f.StreamID, f.StreamEnded())
  7070  	data := f.Data()
  7071  	if cs == nil {
  7072  		cc.mu.Lock()
  7073  		neverSent := cc.nextStreamID
  7074  		cc.mu.Unlock()
  7075  		if f.StreamID >= neverSent {
  7076  
  7077  			cc.logf("http2: Transport received unsolicited DATA frame; closing connection")
  7078  			return http2ConnectionError(http2ErrCodeProtocol)
  7079  		}
  7080  
  7081  		if f.Length > 0 {
  7082  			cc.mu.Lock()
  7083  			cc.inflow.add(int32(f.Length))
  7084  			cc.mu.Unlock()
  7085  
  7086  			cc.wmu.Lock()
  7087  			cc.fr.WriteWindowUpdate(0, uint32(f.Length))
  7088  			cc.bw.Flush()
  7089  			cc.wmu.Unlock()
  7090  		}
  7091  		return nil
  7092  	}
  7093  	if f.Length > 0 {
  7094  		if len(data) > 0 && cs.bufPipe.b == nil {
  7095  
  7096  			cc.logf("http2: Transport received DATA frame for closed stream; closing connection")
  7097  			return http2ConnectionError(http2ErrCodeProtocol)
  7098  		}
  7099  
  7100  		cc.mu.Lock()
  7101  		if cs.inflow.available() >= int32(f.Length) {
  7102  			cs.inflow.take(int32(f.Length))
  7103  		} else {
  7104  			cc.mu.Unlock()
  7105  			return http2ConnectionError(http2ErrCodeFlowControl)
  7106  		}
  7107  
  7108  		if pad := int32(f.Length) - int32(len(data)); pad > 0 {
  7109  			cs.inflow.add(pad)
  7110  			cc.inflow.add(pad)
  7111  			cc.wmu.Lock()
  7112  			cc.fr.WriteWindowUpdate(0, uint32(pad))
  7113  			cc.fr.WriteWindowUpdate(cs.ID, uint32(pad))
  7114  			cc.bw.Flush()
  7115  			cc.wmu.Unlock()
  7116  		}
  7117  		didReset := cs.didReset
  7118  		cc.mu.Unlock()
  7119  
  7120  		if len(data) > 0 && !didReset {
  7121  			if _, err := cs.bufPipe.Write(data); err != nil {
  7122  				rl.endStreamError(cs, err)
  7123  				return err
  7124  			}
  7125  		}
  7126  	}
  7127  
  7128  	if f.StreamEnded() {
  7129  		rl.endStream(cs)
  7130  	}
  7131  	return nil
  7132  }
  7133  
  7134  var http2errInvalidTrailers = errors.New("http2: invalid trailers")
  7135  
  7136  func (rl *http2clientConnReadLoop) endStream(cs *http2clientStream) {
  7137  
  7138  	rl.endStreamError(cs, nil)
  7139  }
  7140  
  7141  func (rl *http2clientConnReadLoop) endStreamError(cs *http2clientStream, err error) {
  7142  	var code func()
  7143  	if err == nil {
  7144  		err = io.EOF
  7145  		code = cs.copyTrailers
  7146  	}
  7147  	cs.bufPipe.closeWithErrorAndCode(err, code)
  7148  	delete(rl.activeRes, cs.ID)
  7149  	if http2isConnectionCloseRequest(cs.req) {
  7150  		rl.closeWhenIdle = true
  7151  	}
  7152  
  7153  	select {
  7154  	case cs.resc <- http2resAndError{err: err}:
  7155  	default:
  7156  	}
  7157  }
  7158  
  7159  func (cs *http2clientStream) copyTrailers() {
  7160  	for k, vv := range cs.trailer {
  7161  		t := cs.resTrailer
  7162  		if *t == nil {
  7163  			*t = make(Header)
  7164  		}
  7165  		(*t)[k] = vv
  7166  	}
  7167  }
  7168  
  7169  func (rl *http2clientConnReadLoop) processGoAway(f *http2GoAwayFrame) error {
  7170  	cc := rl.cc
  7171  	cc.t.connPool().MarkDead(cc)
  7172  	if f.ErrCode != 0 {
  7173  
  7174  		cc.vlogf("transport got GOAWAY with error code = %v", f.ErrCode)
  7175  	}
  7176  	cc.setGoAway(f)
  7177  	return nil
  7178  }
  7179  
  7180  func (rl *http2clientConnReadLoop) processSettings(f *http2SettingsFrame) error {
  7181  	cc := rl.cc
  7182  	cc.mu.Lock()
  7183  	defer cc.mu.Unlock()
  7184  
  7185  	if f.IsAck() {
  7186  		if cc.wantSettingsAck {
  7187  			cc.wantSettingsAck = false
  7188  			return nil
  7189  		}
  7190  		return http2ConnectionError(http2ErrCodeProtocol)
  7191  	}
  7192  
  7193  	err := f.ForeachSetting(func(s http2Setting) error {
  7194  		switch s.ID {
  7195  		case http2SettingMaxFrameSize:
  7196  			cc.maxFrameSize = s.Val
  7197  		case http2SettingMaxConcurrentStreams:
  7198  			cc.maxConcurrentStreams = s.Val
  7199  		case http2SettingInitialWindowSize:
  7200  
  7201  			if s.Val > math.MaxInt32 {
  7202  				return http2ConnectionError(http2ErrCodeFlowControl)
  7203  			}
  7204  
  7205  			delta := int32(s.Val) - int32(cc.initialWindowSize)
  7206  			for _, cs := range cc.streams {
  7207  				cs.flow.add(delta)
  7208  			}
  7209  			cc.cond.Broadcast()
  7210  
  7211  			cc.initialWindowSize = s.Val
  7212  		default:
  7213  
  7214  			cc.vlogf("Unhandled Setting: %v", s)
  7215  		}
  7216  		return nil
  7217  	})
  7218  	if err != nil {
  7219  		return err
  7220  	}
  7221  
  7222  	cc.wmu.Lock()
  7223  	defer cc.wmu.Unlock()
  7224  
  7225  	cc.fr.WriteSettingsAck()
  7226  	cc.bw.Flush()
  7227  	return cc.werr
  7228  }
  7229  
  7230  func (rl *http2clientConnReadLoop) processWindowUpdate(f *http2WindowUpdateFrame) error {
  7231  	cc := rl.cc
  7232  	cs := cc.streamByID(f.StreamID, false)
  7233  	if f.StreamID != 0 && cs == nil {
  7234  		return nil
  7235  	}
  7236  
  7237  	cc.mu.Lock()
  7238  	defer cc.mu.Unlock()
  7239  
  7240  	fl := &cc.flow
  7241  	if cs != nil {
  7242  		fl = &cs.flow
  7243  	}
  7244  	if !fl.add(int32(f.Increment)) {
  7245  		return http2ConnectionError(http2ErrCodeFlowControl)
  7246  	}
  7247  	cc.cond.Broadcast()
  7248  	return nil
  7249  }
  7250  
  7251  func (rl *http2clientConnReadLoop) processResetStream(f *http2RSTStreamFrame) error {
  7252  	cs := rl.cc.streamByID(f.StreamID, true)
  7253  	if cs == nil {
  7254  
  7255  		return nil
  7256  	}
  7257  	select {
  7258  	case <-cs.peerReset:
  7259  
  7260  	default:
  7261  		err := http2streamError(cs.ID, f.ErrCode)
  7262  		cs.resetErr = err
  7263  		close(cs.peerReset)
  7264  		cs.bufPipe.CloseWithError(err)
  7265  		cs.cc.cond.Broadcast()
  7266  	}
  7267  	delete(rl.activeRes, cs.ID)
  7268  	return nil
  7269  }
  7270  
  7271  // Ping sends a PING frame to the server and waits for the ack.
  7272  // Public implementation is in go17.go and not_go17.go
  7273  func (cc *http2ClientConn) ping(ctx http2contextContext) error {
  7274  	c := make(chan struct{})
  7275  	// Generate a random payload
  7276  	var p [8]byte
  7277  	for {
  7278  		if _, err := rand.Read(p[:]); err != nil {
  7279  			return err
  7280  		}
  7281  		cc.mu.Lock()
  7282  
  7283  		if _, found := cc.pings[p]; !found {
  7284  			cc.pings[p] = c
  7285  			cc.mu.Unlock()
  7286  			break
  7287  		}
  7288  		cc.mu.Unlock()
  7289  	}
  7290  	cc.wmu.Lock()
  7291  	if err := cc.fr.WritePing(false, p); err != nil {
  7292  		cc.wmu.Unlock()
  7293  		return err
  7294  	}
  7295  	if err := cc.bw.Flush(); err != nil {
  7296  		cc.wmu.Unlock()
  7297  		return err
  7298  	}
  7299  	cc.wmu.Unlock()
  7300  	select {
  7301  	case <-c:
  7302  		return nil
  7303  	case <-ctx.Done():
  7304  		return ctx.Err()
  7305  	case <-cc.readerDone:
  7306  
  7307  		return cc.readerErr
  7308  	}
  7309  }
  7310  
  7311  func (rl *http2clientConnReadLoop) processPing(f *http2PingFrame) error {
  7312  	if f.IsAck() {
  7313  		cc := rl.cc
  7314  		cc.mu.Lock()
  7315  		defer cc.mu.Unlock()
  7316  
  7317  		if c, ok := cc.pings[f.Data]; ok {
  7318  			close(c)
  7319  			delete(cc.pings, f.Data)
  7320  		}
  7321  		return nil
  7322  	}
  7323  	cc := rl.cc
  7324  	cc.wmu.Lock()
  7325  	defer cc.wmu.Unlock()
  7326  	if err := cc.fr.WritePing(true, f.Data); err != nil {
  7327  		return err
  7328  	}
  7329  	return cc.bw.Flush()
  7330  }
  7331  
  7332  func (rl *http2clientConnReadLoop) processPushPromise(f *http2PushPromiseFrame) error {
  7333  
  7334  	return http2ConnectionError(http2ErrCodeProtocol)
  7335  }
  7336  
  7337  func (cc *http2ClientConn) writeStreamReset(streamID uint32, code http2ErrCode, err error) {
  7338  
  7339  	cc.wmu.Lock()
  7340  	cc.fr.WriteRSTStream(streamID, code)
  7341  	cc.bw.Flush()
  7342  	cc.wmu.Unlock()
  7343  }
  7344  
  7345  var (
  7346  	http2errResponseHeaderListSize = errors.New("http2: response header list larger than advertised limit")
  7347  	http2errPseudoTrailers         = errors.New("http2: invalid pseudo header in trailers")
  7348  )
  7349  
  7350  func (cc *http2ClientConn) logf(format string, args ...interface{}) {
  7351  	cc.t.logf(format, args...)
  7352  }
  7353  
  7354  func (cc *http2ClientConn) vlogf(format string, args ...interface{}) {
  7355  	cc.t.vlogf(format, args...)
  7356  }
  7357  
  7358  func (t *http2Transport) vlogf(format string, args ...interface{}) {
  7359  	if http2VerboseLogs {
  7360  		t.logf(format, args...)
  7361  	}
  7362  }
  7363  
  7364  func (t *http2Transport) logf(format string, args ...interface{}) {
  7365  	log.Printf(format, args...)
  7366  }
  7367  
  7368  var http2noBody io.ReadCloser = ioutil.NopCloser(bytes.NewReader(nil))
  7369  
  7370  func http2strSliceContains(ss []string, s string) bool {
  7371  	for _, v := range ss {
  7372  		if v == s {
  7373  			return true
  7374  		}
  7375  	}
  7376  	return false
  7377  }
  7378  
  7379  type http2erringRoundTripper struct{ err error }
  7380  
  7381  func (rt http2erringRoundTripper) RoundTrip(*Request) (*Response, error) { return nil, rt.err }
  7382  
  7383  // gzipReader wraps a response body so it can lazily
  7384  // call gzip.NewReader on the first call to Read
  7385  type http2gzipReader struct {
  7386  	body io.ReadCloser // underlying Response.Body
  7387  	zr   *gzip.Reader  // lazily-initialized gzip reader
  7388  	zerr error         // sticky error
  7389  }
  7390  
  7391  func (gz *http2gzipReader) Read(p []byte) (n int, err error) {
  7392  	if gz.zerr != nil {
  7393  		return 0, gz.zerr
  7394  	}
  7395  	if gz.zr == nil {
  7396  		gz.zr, err = gzip.NewReader(gz.body)
  7397  		if err != nil {
  7398  			gz.zerr = err
  7399  			return 0, err
  7400  		}
  7401  	}
  7402  	return gz.zr.Read(p)
  7403  }
  7404  
  7405  func (gz *http2gzipReader) Close() error {
  7406  	return gz.body.Close()
  7407  }
  7408  
  7409  type http2errorReader struct{ err error }
  7410  
  7411  func (r http2errorReader) Read(p []byte) (int, error) { return 0, r.err }
  7412  
  7413  // bodyWriterState encapsulates various state around the Transport's writing
  7414  // of the request body, particularly regarding doing delayed writes of the body
  7415  // when the request contains "Expect: 100-continue".
  7416  type http2bodyWriterState struct {
  7417  	cs     *http2clientStream
  7418  	timer  *time.Timer   // if non-nil, we're doing a delayed write
  7419  	fnonce *sync.Once    // to call fn with
  7420  	fn     func()        // the code to run in the goroutine, writing the body
  7421  	resc   chan error    // result of fn's execution
  7422  	delay  time.Duration // how long we should delay a delayed write for
  7423  }
  7424  
  7425  func (t *http2Transport) getBodyWriterState(cs *http2clientStream, body io.Reader) (s http2bodyWriterState) {
  7426  	s.cs = cs
  7427  	if body == nil {
  7428  		return
  7429  	}
  7430  	resc := make(chan error, 1)
  7431  	s.resc = resc
  7432  	s.fn = func() {
  7433  		cs.cc.mu.Lock()
  7434  		cs.startedWrite = true
  7435  		cs.cc.mu.Unlock()
  7436  		resc <- cs.writeRequestBody(body, cs.req.Body)
  7437  	}
  7438  	s.delay = t.expectContinueTimeout()
  7439  	if s.delay == 0 ||
  7440  		!httplex.HeaderValuesContainsToken(
  7441  			cs.req.Header["Expect"],
  7442  			"100-continue") {
  7443  		return
  7444  	}
  7445  	s.fnonce = new(sync.Once)
  7446  
  7447  	// Arm the timer with a very large duration, which we'll
  7448  	// intentionally lower later. It has to be large now because
  7449  	// we need a handle to it before writing the headers, but the
  7450  	// s.delay value is defined to not start until after the
  7451  	// request headers were written.
  7452  	const hugeDuration = 365 * 24 * time.Hour
  7453  	s.timer = time.AfterFunc(hugeDuration, func() {
  7454  		s.fnonce.Do(s.fn)
  7455  	})
  7456  	return
  7457  }
  7458  
  7459  func (s http2bodyWriterState) cancel() {
  7460  	if s.timer != nil {
  7461  		s.timer.Stop()
  7462  	}
  7463  }
  7464  
  7465  func (s http2bodyWriterState) on100() {
  7466  	if s.timer == nil {
  7467  
  7468  		return
  7469  	}
  7470  	s.timer.Stop()
  7471  	go func() { s.fnonce.Do(s.fn) }()
  7472  }
  7473  
  7474  // scheduleBodyWrite starts writing the body, either immediately (in
  7475  // the common case) or after the delay timeout. It should not be
  7476  // called until after the headers have been written.
  7477  func (s http2bodyWriterState) scheduleBodyWrite() {
  7478  	if s.timer == nil {
  7479  
  7480  		go s.fn()
  7481  		return
  7482  	}
  7483  	http2traceWait100Continue(s.cs.trace)
  7484  	if s.timer.Stop() {
  7485  		s.timer.Reset(s.delay)
  7486  	}
  7487  }
  7488  
  7489  // isConnectionCloseRequest reports whether req should use its own
  7490  // connection for a single request and then close the connection.
  7491  func http2isConnectionCloseRequest(req *Request) bool {
  7492  	return req.Close || httplex.HeaderValuesContainsToken(req.Header["Connection"], "close")
  7493  }
  7494  
  7495  // writeFramer is implemented by any type that is used to write frames.
  7496  type http2writeFramer interface {
  7497  	writeFrame(http2writeContext) error
  7498  
  7499  	// staysWithinBuffer reports whether this writer promises that
  7500  	// it will only write less than or equal to size bytes, and it
  7501  	// won't Flush the write context.
  7502  	staysWithinBuffer(size int) bool
  7503  }
  7504  
  7505  // writeContext is the interface needed by the various frame writer
  7506  // types below. All the writeFrame methods below are scheduled via the
  7507  // frame writing scheduler (see writeScheduler in writesched.go).
  7508  //
  7509  // This interface is implemented by *serverConn.
  7510  //
  7511  // TODO: decide whether to a) use this in the client code (which didn't
  7512  // end up using this yet, because it has a simpler design, not
  7513  // currently implementing priorities), or b) delete this and
  7514  // make the server code a bit more concrete.
  7515  type http2writeContext interface {
  7516  	Framer() *http2Framer
  7517  	Flush() error
  7518  	CloseConn() error
  7519  	// HeaderEncoder returns an HPACK encoder that writes to the
  7520  	// returned buffer.
  7521  	HeaderEncoder() (*hpack.Encoder, *bytes.Buffer)
  7522  }
  7523  
  7524  // writeEndsStream reports whether w writes a frame that will transition
  7525  // the stream to a half-closed local state. This returns false for RST_STREAM,
  7526  // which closes the entire stream (not just the local half).
  7527  func http2writeEndsStream(w http2writeFramer) bool {
  7528  	switch v := w.(type) {
  7529  	case *http2writeData:
  7530  		return v.endStream
  7531  	case *http2writeResHeaders:
  7532  		return v.endStream
  7533  	case nil:
  7534  
  7535  		panic("writeEndsStream called on nil writeFramer")
  7536  	}
  7537  	return false
  7538  }
  7539  
  7540  type http2flushFrameWriter struct{}
  7541  
  7542  func (http2flushFrameWriter) writeFrame(ctx http2writeContext) error {
  7543  	return ctx.Flush()
  7544  }
  7545  
  7546  func (http2flushFrameWriter) staysWithinBuffer(max int) bool { return false }
  7547  
  7548  type http2writeSettings []http2Setting
  7549  
  7550  func (s http2writeSettings) staysWithinBuffer(max int) bool {
  7551  	const settingSize = 6 // uint16 + uint32
  7552  	return http2frameHeaderLen+settingSize*len(s) <= max
  7553  
  7554  }
  7555  
  7556  func (s http2writeSettings) writeFrame(ctx http2writeContext) error {
  7557  	return ctx.Framer().WriteSettings([]http2Setting(s)...)
  7558  }
  7559  
  7560  type http2writeGoAway struct {
  7561  	maxStreamID uint32
  7562  	code        http2ErrCode
  7563  }
  7564  
  7565  func (p *http2writeGoAway) writeFrame(ctx http2writeContext) error {
  7566  	err := ctx.Framer().WriteGoAway(p.maxStreamID, p.code, nil)
  7567  	if p.code != 0 {
  7568  		ctx.Flush()
  7569  		time.Sleep(50 * time.Millisecond)
  7570  		ctx.CloseConn()
  7571  	}
  7572  	return err
  7573  }
  7574  
  7575  func (*http2writeGoAway) staysWithinBuffer(max int) bool { return false }
  7576  
  7577  type http2writeData struct {
  7578  	streamID  uint32
  7579  	p         []byte
  7580  	endStream bool
  7581  }
  7582  
  7583  func (w *http2writeData) String() string {
  7584  	return fmt.Sprintf("writeData(stream=%d, p=%d, endStream=%v)", w.streamID, len(w.p), w.endStream)
  7585  }
  7586  
  7587  func (w *http2writeData) writeFrame(ctx http2writeContext) error {
  7588  	return ctx.Framer().WriteData(w.streamID, w.endStream, w.p)
  7589  }
  7590  
  7591  func (w *http2writeData) staysWithinBuffer(max int) bool {
  7592  	return http2frameHeaderLen+len(w.p) <= max
  7593  }
  7594  
  7595  // handlerPanicRST is the message sent from handler goroutines when
  7596  // the handler panics.
  7597  type http2handlerPanicRST struct {
  7598  	StreamID uint32
  7599  }
  7600  
  7601  func (hp http2handlerPanicRST) writeFrame(ctx http2writeContext) error {
  7602  	return ctx.Framer().WriteRSTStream(hp.StreamID, http2ErrCodeInternal)
  7603  }
  7604  
  7605  func (hp http2handlerPanicRST) staysWithinBuffer(max int) bool { return http2frameHeaderLen+4 <= max }
  7606  
  7607  func (se http2StreamError) writeFrame(ctx http2writeContext) error {
  7608  	return ctx.Framer().WriteRSTStream(se.StreamID, se.Code)
  7609  }
  7610  
  7611  func (se http2StreamError) staysWithinBuffer(max int) bool { return http2frameHeaderLen+4 <= max }
  7612  
  7613  type http2writePingAck struct{ pf *http2PingFrame }
  7614  
  7615  func (w http2writePingAck) writeFrame(ctx http2writeContext) error {
  7616  	return ctx.Framer().WritePing(true, w.pf.Data)
  7617  }
  7618  
  7619  func (w http2writePingAck) staysWithinBuffer(max int) bool {
  7620  	return http2frameHeaderLen+len(w.pf.Data) <= max
  7621  }
  7622  
  7623  type http2writeSettingsAck struct{}
  7624  
  7625  func (http2writeSettingsAck) writeFrame(ctx http2writeContext) error {
  7626  	return ctx.Framer().WriteSettingsAck()
  7627  }
  7628  
  7629  func (http2writeSettingsAck) staysWithinBuffer(max int) bool { return http2frameHeaderLen <= max }
  7630  
  7631  // splitHeaderBlock splits headerBlock into fragments so that each fragment fits
  7632  // in a single frame, then calls fn for each fragment. firstFrag/lastFrag are true
  7633  // for the first/last fragment, respectively.
  7634  func http2splitHeaderBlock(ctx http2writeContext, headerBlock []byte, fn func(ctx http2writeContext, frag []byte, firstFrag, lastFrag bool) error) error {
  7635  	// For now we're lazy and just pick the minimum MAX_FRAME_SIZE
  7636  	// that all peers must support (16KB). Later we could care
  7637  	// more and send larger frames if the peer advertised it, but
  7638  	// there's little point. Most headers are small anyway (so we
  7639  	// generally won't have CONTINUATION frames), and extra frames
  7640  	// only waste 9 bytes anyway.
  7641  	const maxFrameSize = 16384
  7642  
  7643  	first := true
  7644  	for len(headerBlock) > 0 {
  7645  		frag := headerBlock
  7646  		if len(frag) > maxFrameSize {
  7647  			frag = frag[:maxFrameSize]
  7648  		}
  7649  		headerBlock = headerBlock[len(frag):]
  7650  		if err := fn(ctx, frag, first, len(headerBlock) == 0); err != nil {
  7651  			return err
  7652  		}
  7653  		first = false
  7654  	}
  7655  	return nil
  7656  }
  7657  
  7658  // writeResHeaders is a request to write a HEADERS and 0+ CONTINUATION frames
  7659  // for HTTP response headers or trailers from a server handler.
  7660  type http2writeResHeaders struct {
  7661  	streamID    uint32
  7662  	httpResCode int      // 0 means no ":status" line
  7663  	h           Header   // may be nil
  7664  	trailers    []string // if non-nil, which keys of h to write. nil means all.
  7665  	endStream   bool
  7666  
  7667  	date          string
  7668  	contentType   string
  7669  	contentLength string
  7670  }
  7671  
  7672  func http2encKV(enc *hpack.Encoder, k, v string) {
  7673  	if http2VerboseLogs {
  7674  		log.Printf("http2: server encoding header %q = %q", k, v)
  7675  	}
  7676  	enc.WriteField(hpack.HeaderField{Name: k, Value: v})
  7677  }
  7678  
  7679  func (w *http2writeResHeaders) staysWithinBuffer(max int) bool {
  7680  
  7681  	return false
  7682  }
  7683  
  7684  func (w *http2writeResHeaders) writeFrame(ctx http2writeContext) error {
  7685  	enc, buf := ctx.HeaderEncoder()
  7686  	buf.Reset()
  7687  
  7688  	if w.httpResCode != 0 {
  7689  		http2encKV(enc, ":status", http2httpCodeString(w.httpResCode))
  7690  	}
  7691  
  7692  	http2encodeHeaders(enc, w.h, w.trailers)
  7693  
  7694  	if w.contentType != "" {
  7695  		http2encKV(enc, "content-type", w.contentType)
  7696  	}
  7697  	if w.contentLength != "" {
  7698  		http2encKV(enc, "content-length", w.contentLength)
  7699  	}
  7700  	if w.date != "" {
  7701  		http2encKV(enc, "date", w.date)
  7702  	}
  7703  
  7704  	headerBlock := buf.Bytes()
  7705  	if len(headerBlock) == 0 && w.trailers == nil {
  7706  		panic("unexpected empty hpack")
  7707  	}
  7708  
  7709  	return http2splitHeaderBlock(ctx, headerBlock, w.writeHeaderBlock)
  7710  }
  7711  
  7712  func (w *http2writeResHeaders) writeHeaderBlock(ctx http2writeContext, frag []byte, firstFrag, lastFrag bool) error {
  7713  	if firstFrag {
  7714  		return ctx.Framer().WriteHeaders(http2HeadersFrameParam{
  7715  			StreamID:      w.streamID,
  7716  			BlockFragment: frag,
  7717  			EndStream:     w.endStream,
  7718  			EndHeaders:    lastFrag,
  7719  		})
  7720  	} else {
  7721  		return ctx.Framer().WriteContinuation(w.streamID, lastFrag, frag)
  7722  	}
  7723  }
  7724  
  7725  // writePushPromise is a request to write a PUSH_PROMISE and 0+ CONTINUATION frames.
  7726  type http2writePushPromise struct {
  7727  	streamID uint32   // pusher stream
  7728  	method   string   // for :method
  7729  	url      *url.URL // for :scheme, :authority, :path
  7730  	h        Header
  7731  
  7732  	// Creates an ID for a pushed stream. This runs on serveG just before
  7733  	// the frame is written. The returned ID is copied to promisedID.
  7734  	allocatePromisedID func() (uint32, error)
  7735  	promisedID         uint32
  7736  }
  7737  
  7738  func (w *http2writePushPromise) staysWithinBuffer(max int) bool {
  7739  
  7740  	return false
  7741  }
  7742  
  7743  func (w *http2writePushPromise) writeFrame(ctx http2writeContext) error {
  7744  	enc, buf := ctx.HeaderEncoder()
  7745  	buf.Reset()
  7746  
  7747  	http2encKV(enc, ":method", w.method)
  7748  	http2encKV(enc, ":scheme", w.url.Scheme)
  7749  	http2encKV(enc, ":authority", w.url.Host)
  7750  	http2encKV(enc, ":path", w.url.RequestURI())
  7751  	http2encodeHeaders(enc, w.h, nil)
  7752  
  7753  	headerBlock := buf.Bytes()
  7754  	if len(headerBlock) == 0 {
  7755  		panic("unexpected empty hpack")
  7756  	}
  7757  
  7758  	return http2splitHeaderBlock(ctx, headerBlock, w.writeHeaderBlock)
  7759  }
  7760  
  7761  func (w *http2writePushPromise) writeHeaderBlock(ctx http2writeContext, frag []byte, firstFrag, lastFrag bool) error {
  7762  	if firstFrag {
  7763  		return ctx.Framer().WritePushPromise(http2PushPromiseParam{
  7764  			StreamID:      w.streamID,
  7765  			PromiseID:     w.promisedID,
  7766  			BlockFragment: frag,
  7767  			EndHeaders:    lastFrag,
  7768  		})
  7769  	} else {
  7770  		return ctx.Framer().WriteContinuation(w.streamID, lastFrag, frag)
  7771  	}
  7772  }
  7773  
  7774  type http2write100ContinueHeadersFrame struct {
  7775  	streamID uint32
  7776  }
  7777  
  7778  func (w http2write100ContinueHeadersFrame) writeFrame(ctx http2writeContext) error {
  7779  	enc, buf := ctx.HeaderEncoder()
  7780  	buf.Reset()
  7781  	http2encKV(enc, ":status", "100")
  7782  	return ctx.Framer().WriteHeaders(http2HeadersFrameParam{
  7783  		StreamID:      w.streamID,
  7784  		BlockFragment: buf.Bytes(),
  7785  		EndStream:     false,
  7786  		EndHeaders:    true,
  7787  	})
  7788  }
  7789  
  7790  func (w http2write100ContinueHeadersFrame) staysWithinBuffer(max int) bool {
  7791  
  7792  	return 9+2*(len(":status")+len("100")) <= max
  7793  }
  7794  
  7795  type http2writeWindowUpdate struct {
  7796  	streamID uint32 // or 0 for conn-level
  7797  	n        uint32
  7798  }
  7799  
  7800  func (wu http2writeWindowUpdate) staysWithinBuffer(max int) bool { return http2frameHeaderLen+4 <= max }
  7801  
  7802  func (wu http2writeWindowUpdate) writeFrame(ctx http2writeContext) error {
  7803  	return ctx.Framer().WriteWindowUpdate(wu.streamID, wu.n)
  7804  }
  7805  
  7806  // encodeHeaders encodes an http.Header. If keys is not nil, then (k, h[k])
  7807  // is encoded only only if k is in keys.
  7808  func http2encodeHeaders(enc *hpack.Encoder, h Header, keys []string) {
  7809  	if keys == nil {
  7810  		sorter := http2sorterPool.Get().(*http2sorter)
  7811  
  7812  		defer http2sorterPool.Put(sorter)
  7813  		keys = sorter.Keys(h)
  7814  	}
  7815  	for _, k := range keys {
  7816  		vv := h[k]
  7817  		k = http2lowerHeader(k)
  7818  		if !http2validWireHeaderFieldName(k) {
  7819  
  7820  			continue
  7821  		}
  7822  		isTE := k == "transfer-encoding"
  7823  		for _, v := range vv {
  7824  			if !httplex.ValidHeaderFieldValue(v) {
  7825  
  7826  				continue
  7827  			}
  7828  
  7829  			if isTE && v != "trailers" {
  7830  				continue
  7831  			}
  7832  			http2encKV(enc, k, v)
  7833  		}
  7834  	}
  7835  }
  7836  
  7837  // WriteScheduler is the interface implemented by HTTP/2 write schedulers.
  7838  // Methods are never called concurrently.
  7839  type http2WriteScheduler interface {
  7840  	// OpenStream opens a new stream in the write scheduler.
  7841  	// It is illegal to call this with streamID=0 or with a streamID that is
  7842  	// already open -- the call may panic.
  7843  	OpenStream(streamID uint32, options http2OpenStreamOptions)
  7844  
  7845  	// CloseStream closes a stream in the write scheduler. Any frames queued on
  7846  	// this stream should be discarded. It is illegal to call this on a stream
  7847  	// that is not open -- the call may panic.
  7848  	CloseStream(streamID uint32)
  7849  
  7850  	// AdjustStream adjusts the priority of the given stream. This may be called
  7851  	// on a stream that has not yet been opened or has been closed. Note that
  7852  	// RFC 7540 allows PRIORITY frames to be sent on streams in any state. See:
  7853  	// https://tools.ietf.org/html/rfc7540#section-5.1
  7854  	AdjustStream(streamID uint32, priority http2PriorityParam)
  7855  
  7856  	// Push queues a frame in the scheduler. In most cases, this will not be
  7857  	// called with wr.StreamID()!=0 unless that stream is currently open. The one
  7858  	// exception is RST_STREAM frames, which may be sent on idle or closed streams.
  7859  	Push(wr http2FrameWriteRequest)
  7860  
  7861  	// Pop dequeues the next frame to write. Returns false if no frames can
  7862  	// be written. Frames with a given wr.StreamID() are Pop'd in the same
  7863  	// order they are Push'd.
  7864  	Pop() (wr http2FrameWriteRequest, ok bool)
  7865  }
  7866  
  7867  // OpenStreamOptions specifies extra options for WriteScheduler.OpenStream.
  7868  type http2OpenStreamOptions struct {
  7869  	// PusherID is zero if the stream was initiated by the client. Otherwise,
  7870  	// PusherID names the stream that pushed the newly opened stream.
  7871  	PusherID uint32
  7872  }
  7873  
  7874  // FrameWriteRequest is a request to write a frame.
  7875  type http2FrameWriteRequest struct {
  7876  	// write is the interface value that does the writing, once the
  7877  	// WriteScheduler has selected this frame to write. The write
  7878  	// functions are all defined in write.go.
  7879  	write http2writeFramer
  7880  
  7881  	// stream is the stream on which this frame will be written.
  7882  	// nil for non-stream frames like PING and SETTINGS.
  7883  	stream *http2stream
  7884  
  7885  	// done, if non-nil, must be a buffered channel with space for
  7886  	// 1 message and is sent the return value from write (or an
  7887  	// earlier error) when the frame has been written.
  7888  	done chan error
  7889  }
  7890  
  7891  // StreamID returns the id of the stream this frame will be written to.
  7892  // 0 is used for non-stream frames such as PING and SETTINGS.
  7893  func (wr http2FrameWriteRequest) StreamID() uint32 {
  7894  	if wr.stream == nil {
  7895  		if se, ok := wr.write.(http2StreamError); ok {
  7896  
  7897  			return se.StreamID
  7898  		}
  7899  		return 0
  7900  	}
  7901  	return wr.stream.id
  7902  }
  7903  
  7904  // DataSize returns the number of flow control bytes that must be consumed
  7905  // to write this entire frame. This is 0 for non-DATA frames.
  7906  func (wr http2FrameWriteRequest) DataSize() int {
  7907  	if wd, ok := wr.write.(*http2writeData); ok {
  7908  		return len(wd.p)
  7909  	}
  7910  	return 0
  7911  }
  7912  
  7913  // Consume consumes min(n, available) bytes from this frame, where available
  7914  // is the number of flow control bytes available on the stream. Consume returns
  7915  // 0, 1, or 2 frames, where the integer return value gives the number of frames
  7916  // returned.
  7917  //
  7918  // If flow control prevents consuming any bytes, this returns (_, _, 0). If
  7919  // the entire frame was consumed, this returns (wr, _, 1). Otherwise, this
  7920  // returns (consumed, rest, 2), where 'consumed' contains the consumed bytes and
  7921  // 'rest' contains the remaining bytes. The consumed bytes are deducted from the
  7922  // underlying stream's flow control budget.
  7923  func (wr http2FrameWriteRequest) Consume(n int32) (http2FrameWriteRequest, http2FrameWriteRequest, int) {
  7924  	var empty http2FrameWriteRequest
  7925  
  7926  	wd, ok := wr.write.(*http2writeData)
  7927  	if !ok || len(wd.p) == 0 {
  7928  		return wr, empty, 1
  7929  	}
  7930  
  7931  	allowed := wr.stream.flow.available()
  7932  	if n < allowed {
  7933  		allowed = n
  7934  	}
  7935  	if wr.stream.sc.maxFrameSize < allowed {
  7936  		allowed = wr.stream.sc.maxFrameSize
  7937  	}
  7938  	if allowed <= 0 {
  7939  		return empty, empty, 0
  7940  	}
  7941  	if len(wd.p) > int(allowed) {
  7942  		wr.stream.flow.take(allowed)
  7943  		consumed := http2FrameWriteRequest{
  7944  			stream: wr.stream,
  7945  			write: &http2writeData{
  7946  				streamID: wd.streamID,
  7947  				p:        wd.p[:allowed],
  7948  
  7949  				endStream: false,
  7950  			},
  7951  
  7952  			done: nil,
  7953  		}
  7954  		rest := http2FrameWriteRequest{
  7955  			stream: wr.stream,
  7956  			write: &http2writeData{
  7957  				streamID:  wd.streamID,
  7958  				p:         wd.p[allowed:],
  7959  				endStream: wd.endStream,
  7960  			},
  7961  			done: wr.done,
  7962  		}
  7963  		return consumed, rest, 2
  7964  	}
  7965  
  7966  	wr.stream.flow.take(int32(len(wd.p)))
  7967  	return wr, empty, 1
  7968  }
  7969  
  7970  // String is for debugging only.
  7971  func (wr http2FrameWriteRequest) String() string {
  7972  	var des string
  7973  	if s, ok := wr.write.(fmt.Stringer); ok {
  7974  		des = s.String()
  7975  	} else {
  7976  		des = fmt.Sprintf("%T", wr.write)
  7977  	}
  7978  	return fmt.Sprintf("[FrameWriteRequest stream=%d, ch=%v, writer=%v]", wr.StreamID(), wr.done != nil, des)
  7979  }
  7980  
  7981  // replyToWriter sends err to wr.done and panics if the send must block
  7982  // This does nothing if wr.done is nil.
  7983  func (wr *http2FrameWriteRequest) replyToWriter(err error) {
  7984  	if wr.done == nil {
  7985  		return
  7986  	}
  7987  	select {
  7988  	case wr.done <- err:
  7989  	default:
  7990  		panic(fmt.Sprintf("unbuffered done channel passed in for type %T", wr.write))
  7991  	}
  7992  	wr.write = nil
  7993  }
  7994  
  7995  // writeQueue is used by implementations of WriteScheduler.
  7996  type http2writeQueue struct {
  7997  	s []http2FrameWriteRequest
  7998  }
  7999  
  8000  func (q *http2writeQueue) empty() bool { return len(q.s) == 0 }
  8001  
  8002  func (q *http2writeQueue) push(wr http2FrameWriteRequest) {
  8003  	q.s = append(q.s, wr)
  8004  }
  8005  
  8006  func (q *http2writeQueue) shift() http2FrameWriteRequest {
  8007  	if len(q.s) == 0 {
  8008  		panic("invalid use of queue")
  8009  	}
  8010  	wr := q.s[0]
  8011  
  8012  	copy(q.s, q.s[1:])
  8013  	q.s[len(q.s)-1] = http2FrameWriteRequest{}
  8014  	q.s = q.s[:len(q.s)-1]
  8015  	return wr
  8016  }
  8017  
  8018  // consume consumes up to n bytes from q.s[0]. If the frame is
  8019  // entirely consumed, it is removed from the queue. If the frame
  8020  // is partially consumed, the frame is kept with the consumed
  8021  // bytes removed. Returns true iff any bytes were consumed.
  8022  func (q *http2writeQueue) consume(n int32) (http2FrameWriteRequest, bool) {
  8023  	if len(q.s) == 0 {
  8024  		return http2FrameWriteRequest{}, false
  8025  	}
  8026  	consumed, rest, numresult := q.s[0].Consume(n)
  8027  	switch numresult {
  8028  	case 0:
  8029  		return http2FrameWriteRequest{}, false
  8030  	case 1:
  8031  		q.shift()
  8032  	case 2:
  8033  		q.s[0] = rest
  8034  	}
  8035  	return consumed, true
  8036  }
  8037  
  8038  type http2writeQueuePool []*http2writeQueue
  8039  
  8040  // put inserts an unused writeQueue into the pool.
  8041  func (p *http2writeQueuePool) put(q *http2writeQueue) {
  8042  	for i := range q.s {
  8043  		q.s[i] = http2FrameWriteRequest{}
  8044  	}
  8045  	q.s = q.s[:0]
  8046  	*p = append(*p, q)
  8047  }
  8048  
  8049  // get returns an empty writeQueue.
  8050  func (p *http2writeQueuePool) get() *http2writeQueue {
  8051  	ln := len(*p)
  8052  	if ln == 0 {
  8053  		return new(http2writeQueue)
  8054  	}
  8055  	x := ln - 1
  8056  	q := (*p)[x]
  8057  	(*p)[x] = nil
  8058  	*p = (*p)[:x]
  8059  	return q
  8060  }
  8061  
  8062  // RFC 7540, Section 5.3.5: the default weight is 16.
  8063  const http2priorityDefaultWeight = 15 // 16 = 15 + 1
  8064  
  8065  // PriorityWriteSchedulerConfig configures a priorityWriteScheduler.
  8066  type http2PriorityWriteSchedulerConfig struct {
  8067  	// MaxClosedNodesInTree controls the maximum number of closed streams to
  8068  	// retain in the priority tree. Setting this to zero saves a small amount
  8069  	// of memory at the cost of performance.
  8070  	//
  8071  	// See RFC 7540, Section 5.3.4:
  8072  	//   "It is possible for a stream to become closed while prioritization
  8073  	//   information ... is in transit. ... This potentially creates suboptimal
  8074  	//   prioritization, since the stream could be given a priority that is
  8075  	//   different from what is intended. To avoid these problems, an endpoint
  8076  	//   SHOULD retain stream prioritization state for a period after streams
  8077  	//   become closed. The longer state is retained, the lower the chance that
  8078  	//   streams are assigned incorrect or default priority values."
  8079  	MaxClosedNodesInTree int
  8080  
  8081  	// MaxIdleNodesInTree controls the maximum number of idle streams to
  8082  	// retain in the priority tree. Setting this to zero saves a small amount
  8083  	// of memory at the cost of performance.
  8084  	//
  8085  	// See RFC 7540, Section 5.3.4:
  8086  	//   Similarly, streams that are in the "idle" state can be assigned
  8087  	//   priority or become a parent of other streams. This allows for the
  8088  	//   creation of a grouping node in the dependency tree, which enables
  8089  	//   more flexible expressions of priority. Idle streams begin with a
  8090  	//   default priority (Section 5.3.5).
  8091  	MaxIdleNodesInTree int
  8092  
  8093  	// ThrottleOutOfOrderWrites enables write throttling to help ensure that
  8094  	// data is delivered in priority order. This works around a race where
  8095  	// stream B depends on stream A and both streams are about to call Write
  8096  	// to queue DATA frames. If B wins the race, a naive scheduler would eagerly
  8097  	// write as much data from B as possible, but this is suboptimal because A
  8098  	// is a higher-priority stream. With throttling enabled, we write a small
  8099  	// amount of data from B to minimize the amount of bandwidth that B can
  8100  	// steal from A.
  8101  	ThrottleOutOfOrderWrites bool
  8102  }
  8103  
  8104  // NewPriorityWriteScheduler constructs a WriteScheduler that schedules
  8105  // frames by following HTTP/2 priorities as described in RFC 7340 Section 5.3.
  8106  // If cfg is nil, default options are used.
  8107  func http2NewPriorityWriteScheduler(cfg *http2PriorityWriteSchedulerConfig) http2WriteScheduler {
  8108  	if cfg == nil {
  8109  
  8110  		cfg = &http2PriorityWriteSchedulerConfig{
  8111  			MaxClosedNodesInTree:     10,
  8112  			MaxIdleNodesInTree:       10,
  8113  			ThrottleOutOfOrderWrites: false,
  8114  		}
  8115  	}
  8116  
  8117  	ws := &http2priorityWriteScheduler{
  8118  		nodes:                make(map[uint32]*http2priorityNode),
  8119  		maxClosedNodesInTree: cfg.MaxClosedNodesInTree,
  8120  		maxIdleNodesInTree:   cfg.MaxIdleNodesInTree,
  8121  		enableWriteThrottle:  cfg.ThrottleOutOfOrderWrites,
  8122  	}
  8123  	ws.nodes[0] = &ws.root
  8124  	if cfg.ThrottleOutOfOrderWrites {
  8125  		ws.writeThrottleLimit = 1024
  8126  	} else {
  8127  		ws.writeThrottleLimit = math.MaxInt32
  8128  	}
  8129  	return ws
  8130  }
  8131  
  8132  type http2priorityNodeState int
  8133  
  8134  const (
  8135  	http2priorityNodeOpen http2priorityNodeState = iota
  8136  	http2priorityNodeClosed
  8137  	http2priorityNodeIdle
  8138  )
  8139  
  8140  // priorityNode is a node in an HTTP/2 priority tree.
  8141  // Each node is associated with a single stream ID.
  8142  // See RFC 7540, Section 5.3.
  8143  type http2priorityNode struct {
  8144  	q            http2writeQueue        // queue of pending frames to write
  8145  	id           uint32                 // id of the stream, or 0 for the root of the tree
  8146  	weight       uint8                  // the actual weight is weight+1, so the value is in [1,256]
  8147  	state        http2priorityNodeState // open | closed | idle
  8148  	bytes        int64                  // number of bytes written by this node, or 0 if closed
  8149  	subtreeBytes int64                  // sum(node.bytes) of all nodes in this subtree
  8150  
  8151  	// These links form the priority tree.
  8152  	parent     *http2priorityNode
  8153  	kids       *http2priorityNode // start of the kids list
  8154  	prev, next *http2priorityNode // doubly-linked list of siblings
  8155  }
  8156  
  8157  func (n *http2priorityNode) setParent(parent *http2priorityNode) {
  8158  	if n == parent {
  8159  		panic("setParent to self")
  8160  	}
  8161  	if n.parent == parent {
  8162  		return
  8163  	}
  8164  
  8165  	if parent := n.parent; parent != nil {
  8166  		if n.prev == nil {
  8167  			parent.kids = n.next
  8168  		} else {
  8169  			n.prev.next = n.next
  8170  		}
  8171  		if n.next != nil {
  8172  			n.next.prev = n.prev
  8173  		}
  8174  	}
  8175  
  8176  	n.parent = parent
  8177  	if parent == nil {
  8178  		n.next = nil
  8179  		n.prev = nil
  8180  	} else {
  8181  		n.next = parent.kids
  8182  		n.prev = nil
  8183  		if n.next != nil {
  8184  			n.next.prev = n
  8185  		}
  8186  		parent.kids = n
  8187  	}
  8188  }
  8189  
  8190  func (n *http2priorityNode) addBytes(b int64) {
  8191  	n.bytes += b
  8192  	for ; n != nil; n = n.parent {
  8193  		n.subtreeBytes += b
  8194  	}
  8195  }
  8196  
  8197  // walkReadyInOrder iterates over the tree in priority order, calling f for each node
  8198  // with a non-empty write queue. When f returns true, this funcion returns true and the
  8199  // walk halts. tmp is used as scratch space for sorting.
  8200  //
  8201  // f(n, openParent) takes two arguments: the node to visit, n, and a bool that is true
  8202  // if any ancestor p of n is still open (ignoring the root node).
  8203  func (n *http2priorityNode) walkReadyInOrder(openParent bool, tmp *[]*http2priorityNode, f func(*http2priorityNode, bool) bool) bool {
  8204  	if !n.q.empty() && f(n, openParent) {
  8205  		return true
  8206  	}
  8207  	if n.kids == nil {
  8208  		return false
  8209  	}
  8210  
  8211  	if n.id != 0 {
  8212  		openParent = openParent || (n.state == http2priorityNodeOpen)
  8213  	}
  8214  
  8215  	w := n.kids.weight
  8216  	needSort := false
  8217  	for k := n.kids.next; k != nil; k = k.next {
  8218  		if k.weight != w {
  8219  			needSort = true
  8220  			break
  8221  		}
  8222  	}
  8223  	if !needSort {
  8224  		for k := n.kids; k != nil; k = k.next {
  8225  			if k.walkReadyInOrder(openParent, tmp, f) {
  8226  				return true
  8227  			}
  8228  		}
  8229  		return false
  8230  	}
  8231  
  8232  	*tmp = (*tmp)[:0]
  8233  	for n.kids != nil {
  8234  		*tmp = append(*tmp, n.kids)
  8235  		n.kids.setParent(nil)
  8236  	}
  8237  	sort.Sort(http2sortPriorityNodeSiblings(*tmp))
  8238  	for i := len(*tmp) - 1; i >= 0; i-- {
  8239  		(*tmp)[i].setParent(n)
  8240  	}
  8241  	for k := n.kids; k != nil; k = k.next {
  8242  		if k.walkReadyInOrder(openParent, tmp, f) {
  8243  			return true
  8244  		}
  8245  	}
  8246  	return false
  8247  }
  8248  
  8249  type http2sortPriorityNodeSiblings []*http2priorityNode
  8250  
  8251  func (z http2sortPriorityNodeSiblings) Len() int { return len(z) }
  8252  
  8253  func (z http2sortPriorityNodeSiblings) Swap(i, k int) { z[i], z[k] = z[k], z[i] }
  8254  
  8255  func (z http2sortPriorityNodeSiblings) Less(i, k int) bool {
  8256  
  8257  	wi, bi := float64(z[i].weight+1), float64(z[i].subtreeBytes)
  8258  	wk, bk := float64(z[k].weight+1), float64(z[k].subtreeBytes)
  8259  	if bi == 0 && bk == 0 {
  8260  		return wi >= wk
  8261  	}
  8262  	if bk == 0 {
  8263  		return false
  8264  	}
  8265  	return bi/bk <= wi/wk
  8266  }
  8267  
  8268  type http2priorityWriteScheduler struct {
  8269  	// root is the root of the priority tree, where root.id = 0.
  8270  	// The root queues control frames that are not associated with any stream.
  8271  	root http2priorityNode
  8272  
  8273  	// nodes maps stream ids to priority tree nodes.
  8274  	nodes map[uint32]*http2priorityNode
  8275  
  8276  	// maxID is the maximum stream id in nodes.
  8277  	maxID uint32
  8278  
  8279  	// lists of nodes that have been closed or are idle, but are kept in
  8280  	// the tree for improved prioritization. When the lengths exceed either
  8281  	// maxClosedNodesInTree or maxIdleNodesInTree, old nodes are discarded.
  8282  	closedNodes, idleNodes []*http2priorityNode
  8283  
  8284  	// From the config.
  8285  	maxClosedNodesInTree int
  8286  	maxIdleNodesInTree   int
  8287  	writeThrottleLimit   int32
  8288  	enableWriteThrottle  bool
  8289  
  8290  	// tmp is scratch space for priorityNode.walkReadyInOrder to reduce allocations.
  8291  	tmp []*http2priorityNode
  8292  
  8293  	// pool of empty queues for reuse.
  8294  	queuePool http2writeQueuePool
  8295  }
  8296  
  8297  func (ws *http2priorityWriteScheduler) OpenStream(streamID uint32, options http2OpenStreamOptions) {
  8298  
  8299  	if curr := ws.nodes[streamID]; curr != nil {
  8300  		if curr.state != http2priorityNodeIdle {
  8301  			panic(fmt.Sprintf("stream %d already opened", streamID))
  8302  		}
  8303  		curr.state = http2priorityNodeOpen
  8304  		return
  8305  	}
  8306  
  8307  	parent := ws.nodes[options.PusherID]
  8308  	if parent == nil {
  8309  		parent = &ws.root
  8310  	}
  8311  	n := &http2priorityNode{
  8312  		q:      *ws.queuePool.get(),
  8313  		id:     streamID,
  8314  		weight: http2priorityDefaultWeight,
  8315  		state:  http2priorityNodeOpen,
  8316  	}
  8317  	n.setParent(parent)
  8318  	ws.nodes[streamID] = n
  8319  	if streamID > ws.maxID {
  8320  		ws.maxID = streamID
  8321  	}
  8322  }
  8323  
  8324  func (ws *http2priorityWriteScheduler) CloseStream(streamID uint32) {
  8325  	if streamID == 0 {
  8326  		panic("violation of WriteScheduler interface: cannot close stream 0")
  8327  	}
  8328  	if ws.nodes[streamID] == nil {
  8329  		panic(fmt.Sprintf("violation of WriteScheduler interface: unknown stream %d", streamID))
  8330  	}
  8331  	if ws.nodes[streamID].state != http2priorityNodeOpen {
  8332  		panic(fmt.Sprintf("violation of WriteScheduler interface: stream %d already closed", streamID))
  8333  	}
  8334  
  8335  	n := ws.nodes[streamID]
  8336  	n.state = http2priorityNodeClosed
  8337  	n.addBytes(-n.bytes)
  8338  
  8339  	q := n.q
  8340  	ws.queuePool.put(&q)
  8341  	n.q.s = nil
  8342  	if ws.maxClosedNodesInTree > 0 {
  8343  		ws.addClosedOrIdleNode(&ws.closedNodes, ws.maxClosedNodesInTree, n)
  8344  	} else {
  8345  		ws.removeNode(n)
  8346  	}
  8347  }
  8348  
  8349  func (ws *http2priorityWriteScheduler) AdjustStream(streamID uint32, priority http2PriorityParam) {
  8350  	if streamID == 0 {
  8351  		panic("adjustPriority on root")
  8352  	}
  8353  
  8354  	n := ws.nodes[streamID]
  8355  	if n == nil {
  8356  		if streamID <= ws.maxID || ws.maxIdleNodesInTree == 0 {
  8357  			return
  8358  		}
  8359  		ws.maxID = streamID
  8360  		n = &http2priorityNode{
  8361  			q:      *ws.queuePool.get(),
  8362  			id:     streamID,
  8363  			weight: http2priorityDefaultWeight,
  8364  			state:  http2priorityNodeIdle,
  8365  		}
  8366  		n.setParent(&ws.root)
  8367  		ws.nodes[streamID] = n
  8368  		ws.addClosedOrIdleNode(&ws.idleNodes, ws.maxIdleNodesInTree, n)
  8369  	}
  8370  
  8371  	parent := ws.nodes[priority.StreamDep]
  8372  	if parent == nil {
  8373  		n.setParent(&ws.root)
  8374  		n.weight = http2priorityDefaultWeight
  8375  		return
  8376  	}
  8377  
  8378  	if n == parent {
  8379  		return
  8380  	}
  8381  
  8382  	for x := parent.parent; x != nil; x = x.parent {
  8383  		if x == n {
  8384  			parent.setParent(n.parent)
  8385  			break
  8386  		}
  8387  	}
  8388  
  8389  	if priority.Exclusive {
  8390  		k := parent.kids
  8391  		for k != nil {
  8392  			next := k.next
  8393  			if k != n {
  8394  				k.setParent(n)
  8395  			}
  8396  			k = next
  8397  		}
  8398  	}
  8399  
  8400  	n.setParent(parent)
  8401  	n.weight = priority.Weight
  8402  }
  8403  
  8404  func (ws *http2priorityWriteScheduler) Push(wr http2FrameWriteRequest) {
  8405  	var n *http2priorityNode
  8406  	if id := wr.StreamID(); id == 0 {
  8407  		n = &ws.root
  8408  	} else {
  8409  		n = ws.nodes[id]
  8410  		if n == nil {
  8411  
  8412  			if wr.DataSize() > 0 {
  8413  				panic("add DATA on non-open stream")
  8414  			}
  8415  			n = &ws.root
  8416  		}
  8417  	}
  8418  	n.q.push(wr)
  8419  }
  8420  
  8421  func (ws *http2priorityWriteScheduler) Pop() (wr http2FrameWriteRequest, ok bool) {
  8422  	ws.root.walkReadyInOrder(false, &ws.tmp, func(n *http2priorityNode, openParent bool) bool {
  8423  		limit := int32(math.MaxInt32)
  8424  		if openParent {
  8425  			limit = ws.writeThrottleLimit
  8426  		}
  8427  		wr, ok = n.q.consume(limit)
  8428  		if !ok {
  8429  			return false
  8430  		}
  8431  		n.addBytes(int64(wr.DataSize()))
  8432  
  8433  		if openParent {
  8434  			ws.writeThrottleLimit += 1024
  8435  			if ws.writeThrottleLimit < 0 {
  8436  				ws.writeThrottleLimit = math.MaxInt32
  8437  			}
  8438  		} else if ws.enableWriteThrottle {
  8439  			ws.writeThrottleLimit = 1024
  8440  		}
  8441  		return true
  8442  	})
  8443  	return wr, ok
  8444  }
  8445  
  8446  func (ws *http2priorityWriteScheduler) addClosedOrIdleNode(list *[]*http2priorityNode, maxSize int, n *http2priorityNode) {
  8447  	if maxSize == 0 {
  8448  		return
  8449  	}
  8450  	if len(*list) == maxSize {
  8451  
  8452  		ws.removeNode((*list)[0])
  8453  		x := (*list)[1:]
  8454  		copy(*list, x)
  8455  		*list = (*list)[:len(x)]
  8456  	}
  8457  	*list = append(*list, n)
  8458  }
  8459  
  8460  func (ws *http2priorityWriteScheduler) removeNode(n *http2priorityNode) {
  8461  	for k := n.kids; k != nil; k = k.next {
  8462  		k.setParent(n.parent)
  8463  	}
  8464  	n.setParent(nil)
  8465  	delete(ws.nodes, n.id)
  8466  }
  8467  
  8468  // NewRandomWriteScheduler constructs a WriteScheduler that ignores HTTP/2
  8469  // priorities. Control frames like SETTINGS and PING are written before DATA
  8470  // frames, but if no control frames are queued and multiple streams have queued
  8471  // HEADERS or DATA frames, Pop selects a ready stream arbitrarily.
  8472  func http2NewRandomWriteScheduler() http2WriteScheduler {
  8473  	return &http2randomWriteScheduler{sq: make(map[uint32]*http2writeQueue)}
  8474  }
  8475  
  8476  type http2randomWriteScheduler struct {
  8477  	// zero are frames not associated with a specific stream.
  8478  	zero http2writeQueue
  8479  
  8480  	// sq contains the stream-specific queues, keyed by stream ID.
  8481  	// When a stream is idle or closed, it's deleted from the map.
  8482  	sq map[uint32]*http2writeQueue
  8483  
  8484  	// pool of empty queues for reuse.
  8485  	queuePool http2writeQueuePool
  8486  }
  8487  
  8488  func (ws *http2randomWriteScheduler) OpenStream(streamID uint32, options http2OpenStreamOptions) {
  8489  
  8490  }
  8491  
  8492  func (ws *http2randomWriteScheduler) CloseStream(streamID uint32) {
  8493  	q, ok := ws.sq[streamID]
  8494  	if !ok {
  8495  		return
  8496  	}
  8497  	delete(ws.sq, streamID)
  8498  	ws.queuePool.put(q)
  8499  }
  8500  
  8501  func (ws *http2randomWriteScheduler) AdjustStream(streamID uint32, priority http2PriorityParam) {
  8502  
  8503  }
  8504  
  8505  func (ws *http2randomWriteScheduler) Push(wr http2FrameWriteRequest) {
  8506  	id := wr.StreamID()
  8507  	if id == 0 {
  8508  		ws.zero.push(wr)
  8509  		return
  8510  	}
  8511  	q, ok := ws.sq[id]
  8512  	if !ok {
  8513  		q = ws.queuePool.get()
  8514  		ws.sq[id] = q
  8515  	}
  8516  	q.push(wr)
  8517  }
  8518  
  8519  func (ws *http2randomWriteScheduler) Pop() (http2FrameWriteRequest, bool) {
  8520  
  8521  	if !ws.zero.empty() {
  8522  		return ws.zero.shift(), true
  8523  	}
  8524  
  8525  	for _, q := range ws.sq {
  8526  		if wr, ok := q.consume(math.MaxInt32); ok {
  8527  			return wr, true
  8528  		}
  8529  	}
  8530  	return http2FrameWriteRequest{}, false
  8531  }