github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/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 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  	"crypto/tls"
    24  	"encoding/binary"
    25  	"errors"
    26  	"fmt"
    27  	"golang.org/x/net/http2/hpack"
    28  	"io"
    29  	"io/ioutil"
    30  	"log"
    31  	"net"
    32  	"net/textproto"
    33  	"net/url"
    34  	"os"
    35  	"reflect"
    36  	"runtime"
    37  	"sort"
    38  	"strconv"
    39  	"strings"
    40  	"sync"
    41  	"time"
    42  )
    43  
    44  // ClientConnPool manages a pool of HTTP/2 client connections.
    45  type http2ClientConnPool interface {
    46  	GetClientConn(req *Request, addr string) (*http2ClientConn, error)
    47  	MarkDead(*http2ClientConn)
    48  }
    49  
    50  // TODO: use singleflight for dialing and addConnCalls?
    51  type http2clientConnPool struct {
    52  	t *http2Transport
    53  
    54  	mu sync.Mutex // TODO: maybe switch to RWMutex
    55  	// TODO: add support for sharing conns based on cert names
    56  	// (e.g. share conn for googleapis.com and appspot.com)
    57  	conns        map[string][]*http2ClientConn // key is host:port
    58  	dialing      map[string]*http2dialCall     // currently in-flight dials
    59  	keys         map[*http2ClientConn][]string
    60  	addConnCalls map[string]*http2addConnCall // in-flight addConnIfNeede calls
    61  }
    62  
    63  func (p *http2clientConnPool) GetClientConn(req *Request, addr string) (*http2ClientConn, error) {
    64  	return p.getClientConn(req, addr, http2dialOnMiss)
    65  }
    66  
    67  const (
    68  	http2dialOnMiss   = true
    69  	http2noDialOnMiss = false
    70  )
    71  
    72  func (p *http2clientConnPool) getClientConn(_ *Request, addr string, dialOnMiss bool) (*http2ClientConn, error) {
    73  	p.mu.Lock()
    74  	for _, cc := range p.conns[addr] {
    75  		if cc.CanTakeNewRequest() {
    76  			p.mu.Unlock()
    77  			return cc, nil
    78  		}
    79  	}
    80  	if !dialOnMiss {
    81  		p.mu.Unlock()
    82  		return nil, http2ErrNoCachedConn
    83  	}
    84  	call := p.getStartDialLocked(addr)
    85  	p.mu.Unlock()
    86  	<-call.done
    87  	return call.res, call.err
    88  }
    89  
    90  // dialCall is an in-flight Transport dial call to a host.
    91  type http2dialCall struct {
    92  	p    *http2clientConnPool
    93  	done chan struct{}    // closed when done
    94  	res  *http2ClientConn // valid after done is closed
    95  	err  error            // valid after done is closed
    96  }
    97  
    98  // requires p.mu is held.
    99  func (p *http2clientConnPool) getStartDialLocked(addr string) *http2dialCall {
   100  	if call, ok := p.dialing[addr]; ok {
   101  
   102  		return call
   103  	}
   104  	call := &http2dialCall{p: p, done: make(chan struct{})}
   105  	if p.dialing == nil {
   106  		p.dialing = make(map[string]*http2dialCall)
   107  	}
   108  	p.dialing[addr] = call
   109  	go call.dial(addr)
   110  	return call
   111  }
   112  
   113  // run in its own goroutine.
   114  func (c *http2dialCall) dial(addr string) {
   115  	c.res, c.err = c.p.t.dialClientConn(addr)
   116  	close(c.done)
   117  
   118  	c.p.mu.Lock()
   119  	delete(c.p.dialing, addr)
   120  	if c.err == nil {
   121  		c.p.addConnLocked(addr, c.res)
   122  	}
   123  	c.p.mu.Unlock()
   124  }
   125  
   126  // addConnIfNeeded makes a NewClientConn out of c if a connection for key doesn't
   127  // already exist. It coalesces concurrent calls with the same key.
   128  // This is used by the http1 Transport code when it creates a new connection. Because
   129  // the http1 Transport doesn't de-dup TCP dials to outbound hosts (because it doesn't know
   130  // the protocol), it can get into a situation where it has multiple TLS connections.
   131  // This code decides which ones live or die.
   132  // The return value used is whether c was used.
   133  // c is never closed.
   134  func (p *http2clientConnPool) addConnIfNeeded(key string, t *http2Transport, c *tls.Conn) (used bool, err error) {
   135  	p.mu.Lock()
   136  	for _, cc := range p.conns[key] {
   137  		if cc.CanTakeNewRequest() {
   138  			p.mu.Unlock()
   139  			return false, nil
   140  		}
   141  	}
   142  	call, dup := p.addConnCalls[key]
   143  	if !dup {
   144  		if p.addConnCalls == nil {
   145  			p.addConnCalls = make(map[string]*http2addConnCall)
   146  		}
   147  		call = &http2addConnCall{
   148  			p:    p,
   149  			done: make(chan struct{}),
   150  		}
   151  		p.addConnCalls[key] = call
   152  		go call.run(t, key, c)
   153  	}
   154  	p.mu.Unlock()
   155  
   156  	<-call.done
   157  	if call.err != nil {
   158  		return false, call.err
   159  	}
   160  	return !dup, nil
   161  }
   162  
   163  type http2addConnCall struct {
   164  	p    *http2clientConnPool
   165  	done chan struct{} // closed when done
   166  	err  error
   167  }
   168  
   169  func (c *http2addConnCall) run(t *http2Transport, key string, tc *tls.Conn) {
   170  	cc, err := t.NewClientConn(tc)
   171  
   172  	p := c.p
   173  	p.mu.Lock()
   174  	if err != nil {
   175  		c.err = err
   176  	} else {
   177  		p.addConnLocked(key, cc)
   178  	}
   179  	delete(p.addConnCalls, key)
   180  	p.mu.Unlock()
   181  	close(c.done)
   182  }
   183  
   184  func (p *http2clientConnPool) addConn(key string, cc *http2ClientConn) {
   185  	p.mu.Lock()
   186  	p.addConnLocked(key, cc)
   187  	p.mu.Unlock()
   188  }
   189  
   190  // p.mu must be held
   191  func (p *http2clientConnPool) addConnLocked(key string, cc *http2ClientConn) {
   192  	for _, v := range p.conns[key] {
   193  		if v == cc {
   194  			return
   195  		}
   196  	}
   197  	if p.conns == nil {
   198  		p.conns = make(map[string][]*http2ClientConn)
   199  	}
   200  	if p.keys == nil {
   201  		p.keys = make(map[*http2ClientConn][]string)
   202  	}
   203  	p.conns[key] = append(p.conns[key], cc)
   204  	p.keys[cc] = append(p.keys[cc], key)
   205  }
   206  
   207  func (p *http2clientConnPool) MarkDead(cc *http2ClientConn) {
   208  	p.mu.Lock()
   209  	defer p.mu.Unlock()
   210  	for _, key := range p.keys[cc] {
   211  		vv, ok := p.conns[key]
   212  		if !ok {
   213  			continue
   214  		}
   215  		newList := http2filterOutClientConn(vv, cc)
   216  		if len(newList) > 0 {
   217  			p.conns[key] = newList
   218  		} else {
   219  			delete(p.conns, key)
   220  		}
   221  	}
   222  	delete(p.keys, cc)
   223  }
   224  
   225  func (p *http2clientConnPool) closeIdleConnections() {
   226  	p.mu.Lock()
   227  	defer p.mu.Unlock()
   228  
   229  	for _, vv := range p.conns {
   230  		for _, cc := range vv {
   231  			cc.closeIfIdle()
   232  		}
   233  	}
   234  }
   235  
   236  func http2filterOutClientConn(in []*http2ClientConn, exclude *http2ClientConn) []*http2ClientConn {
   237  	out := in[:0]
   238  	for _, v := range in {
   239  		if v != exclude {
   240  			out = append(out, v)
   241  		}
   242  	}
   243  
   244  	if len(in) != len(out) {
   245  		in[len(in)-1] = nil
   246  	}
   247  	return out
   248  }
   249  
   250  func http2configureTransport(t1 *Transport) (*http2Transport, error) {
   251  	connPool := new(http2clientConnPool)
   252  	t2 := &http2Transport{
   253  		ConnPool: http2noDialClientConnPool{connPool},
   254  		t1:       t1,
   255  	}
   256  	connPool.t = t2
   257  	if err := http2registerHTTPSProtocol(t1, http2noDialH2RoundTripper{t2}); err != nil {
   258  		return nil, err
   259  	}
   260  	if t1.TLSClientConfig == nil {
   261  		t1.TLSClientConfig = new(tls.Config)
   262  	}
   263  	if !http2strSliceContains(t1.TLSClientConfig.NextProtos, "h2") {
   264  		t1.TLSClientConfig.NextProtos = append([]string{"h2"}, t1.TLSClientConfig.NextProtos...)
   265  	}
   266  	if !http2strSliceContains(t1.TLSClientConfig.NextProtos, "http/1.1") {
   267  		t1.TLSClientConfig.NextProtos = append(t1.TLSClientConfig.NextProtos, "http/1.1")
   268  	}
   269  	upgradeFn := func(authority string, c *tls.Conn) RoundTripper {
   270  		addr := http2authorityAddr(authority)
   271  		if used, err := connPool.addConnIfNeeded(addr, t2, c); err != nil {
   272  			go c.Close()
   273  			return http2erringRoundTripper{err}
   274  		} else if !used {
   275  
   276  			go c.Close()
   277  		}
   278  		return t2
   279  	}
   280  	if m := t1.TLSNextProto; len(m) == 0 {
   281  		t1.TLSNextProto = map[string]func(string, *tls.Conn) RoundTripper{
   282  			"h2": upgradeFn,
   283  		}
   284  	} else {
   285  		m["h2"] = upgradeFn
   286  	}
   287  	return t2, nil
   288  }
   289  
   290  // registerHTTPSProtocol calls Transport.RegisterProtocol but
   291  // convering panics into errors.
   292  func http2registerHTTPSProtocol(t *Transport, rt RoundTripper) (err error) {
   293  	defer func() {
   294  		if e := recover(); e != nil {
   295  			err = fmt.Errorf("%v", e)
   296  		}
   297  	}()
   298  	t.RegisterProtocol("https", rt)
   299  	return nil
   300  }
   301  
   302  // noDialClientConnPool is an implementation of http2.ClientConnPool
   303  // which never dials.  We let the HTTP/1.1 client dial and use its TLS
   304  // connection instead.
   305  type http2noDialClientConnPool struct{ *http2clientConnPool }
   306  
   307  func (p http2noDialClientConnPool) GetClientConn(req *Request, addr string) (*http2ClientConn, error) {
   308  	return p.getClientConn(req, addr, http2noDialOnMiss)
   309  }
   310  
   311  // noDialH2RoundTripper is a RoundTripper which only tries to complete the request
   312  // if there's already has a cached connection to the host.
   313  type http2noDialH2RoundTripper struct{ t *http2Transport }
   314  
   315  func (rt http2noDialH2RoundTripper) RoundTrip(req *Request) (*Response, error) {
   316  	res, err := rt.t.RoundTrip(req)
   317  	if err == http2ErrNoCachedConn {
   318  		return nil, ErrSkipAltProtocol
   319  	}
   320  	return res, err
   321  }
   322  
   323  // An ErrCode is an unsigned 32-bit error code as defined in the HTTP/2 spec.
   324  type http2ErrCode uint32
   325  
   326  const (
   327  	http2ErrCodeNo                 http2ErrCode = 0x0
   328  	http2ErrCodeProtocol           http2ErrCode = 0x1
   329  	http2ErrCodeInternal           http2ErrCode = 0x2
   330  	http2ErrCodeFlowControl        http2ErrCode = 0x3
   331  	http2ErrCodeSettingsTimeout    http2ErrCode = 0x4
   332  	http2ErrCodeStreamClosed       http2ErrCode = 0x5
   333  	http2ErrCodeFrameSize          http2ErrCode = 0x6
   334  	http2ErrCodeRefusedStream      http2ErrCode = 0x7
   335  	http2ErrCodeCancel             http2ErrCode = 0x8
   336  	http2ErrCodeCompression        http2ErrCode = 0x9
   337  	http2ErrCodeConnect            http2ErrCode = 0xa
   338  	http2ErrCodeEnhanceYourCalm    http2ErrCode = 0xb
   339  	http2ErrCodeInadequateSecurity http2ErrCode = 0xc
   340  	http2ErrCodeHTTP11Required     http2ErrCode = 0xd
   341  )
   342  
   343  var http2errCodeName = map[http2ErrCode]string{
   344  	http2ErrCodeNo:                 "NO_ERROR",
   345  	http2ErrCodeProtocol:           "PROTOCOL_ERROR",
   346  	http2ErrCodeInternal:           "INTERNAL_ERROR",
   347  	http2ErrCodeFlowControl:        "FLOW_CONTROL_ERROR",
   348  	http2ErrCodeSettingsTimeout:    "SETTINGS_TIMEOUT",
   349  	http2ErrCodeStreamClosed:       "STREAM_CLOSED",
   350  	http2ErrCodeFrameSize:          "FRAME_SIZE_ERROR",
   351  	http2ErrCodeRefusedStream:      "REFUSED_STREAM",
   352  	http2ErrCodeCancel:             "CANCEL",
   353  	http2ErrCodeCompression:        "COMPRESSION_ERROR",
   354  	http2ErrCodeConnect:            "CONNECT_ERROR",
   355  	http2ErrCodeEnhanceYourCalm:    "ENHANCE_YOUR_CALM",
   356  	http2ErrCodeInadequateSecurity: "INADEQUATE_SECURITY",
   357  	http2ErrCodeHTTP11Required:     "HTTP_1_1_REQUIRED",
   358  }
   359  
   360  func (e http2ErrCode) String() string {
   361  	if s, ok := http2errCodeName[e]; ok {
   362  		return s
   363  	}
   364  	return fmt.Sprintf("unknown error code 0x%x", uint32(e))
   365  }
   366  
   367  // ConnectionError is an error that results in the termination of the
   368  // entire connection.
   369  type http2ConnectionError http2ErrCode
   370  
   371  func (e http2ConnectionError) Error() string {
   372  	return fmt.Sprintf("connection error: %s", http2ErrCode(e))
   373  }
   374  
   375  // StreamError is an error that only affects one stream within an
   376  // HTTP/2 connection.
   377  type http2StreamError struct {
   378  	StreamID uint32
   379  	Code     http2ErrCode
   380  }
   381  
   382  func (e http2StreamError) Error() string {
   383  	return fmt.Sprintf("stream error: stream ID %d; %v", e.StreamID, e.Code)
   384  }
   385  
   386  // 6.9.1 The Flow Control Window
   387  // "If a sender receives a WINDOW_UPDATE that causes a flow control
   388  // window to exceed this maximum it MUST terminate either the stream
   389  // or the connection, as appropriate. For streams, [...]; for the
   390  // connection, a GOAWAY frame with a FLOW_CONTROL_ERROR code."
   391  type http2goAwayFlowError struct{}
   392  
   393  func (http2goAwayFlowError) Error() string { return "connection exceeded flow control window size" }
   394  
   395  // Errors of this type are only returned by the frame parser functions
   396  // and converted into ConnectionError(ErrCodeProtocol).
   397  type http2connError struct {
   398  	Code   http2ErrCode
   399  	Reason string
   400  }
   401  
   402  func (e http2connError) Error() string {
   403  	return fmt.Sprintf("http2: connection error: %v: %v", e.Code, e.Reason)
   404  }
   405  
   406  type http2pseudoHeaderError string
   407  
   408  func (e http2pseudoHeaderError) Error() string {
   409  	return fmt.Sprintf("invalid pseudo-header %q", string(e))
   410  }
   411  
   412  type http2duplicatePseudoHeaderError string
   413  
   414  func (e http2duplicatePseudoHeaderError) Error() string {
   415  	return fmt.Sprintf("duplicate pseudo-header %q", string(e))
   416  }
   417  
   418  type http2headerFieldNameError string
   419  
   420  func (e http2headerFieldNameError) Error() string {
   421  	return fmt.Sprintf("invalid header field name %q", string(e))
   422  }
   423  
   424  type http2headerFieldValueError string
   425  
   426  func (e http2headerFieldValueError) Error() string {
   427  	return fmt.Sprintf("invalid header field value %q", string(e))
   428  }
   429  
   430  var (
   431  	http2errMixPseudoHeaderTypes = errors.New("mix of request and response pseudo headers")
   432  	http2errPseudoAfterRegular   = errors.New("pseudo header field after regular")
   433  )
   434  
   435  // fixedBuffer is an io.ReadWriter backed by a fixed size buffer.
   436  // It never allocates, but moves old data as new data is written.
   437  type http2fixedBuffer struct {
   438  	buf  []byte
   439  	r, w int
   440  }
   441  
   442  var (
   443  	http2errReadEmpty = errors.New("read from empty fixedBuffer")
   444  	http2errWriteFull = errors.New("write on full fixedBuffer")
   445  )
   446  
   447  // Read copies bytes from the buffer into p.
   448  // It is an error to read when no data is available.
   449  func (b *http2fixedBuffer) Read(p []byte) (n int, err error) {
   450  	if b.r == b.w {
   451  		return 0, http2errReadEmpty
   452  	}
   453  	n = copy(p, b.buf[b.r:b.w])
   454  	b.r += n
   455  	if b.r == b.w {
   456  		b.r = 0
   457  		b.w = 0
   458  	}
   459  	return n, nil
   460  }
   461  
   462  // Len returns the number of bytes of the unread portion of the buffer.
   463  func (b *http2fixedBuffer) Len() int {
   464  	return b.w - b.r
   465  }
   466  
   467  // Write copies bytes from p into the buffer.
   468  // It is an error to write more data than the buffer can hold.
   469  func (b *http2fixedBuffer) Write(p []byte) (n int, err error) {
   470  
   471  	if b.r > 0 && len(p) > len(b.buf)-b.w {
   472  		copy(b.buf, b.buf[b.r:b.w])
   473  		b.w -= b.r
   474  		b.r = 0
   475  	}
   476  
   477  	n = copy(b.buf[b.w:], p)
   478  	b.w += n
   479  	if n < len(p) {
   480  		err = http2errWriteFull
   481  	}
   482  	return n, err
   483  }
   484  
   485  // flow is the flow control window's size.
   486  type http2flow struct {
   487  	// n is the number of DATA bytes we're allowed to send.
   488  	// A flow is kept both on a conn and a per-stream.
   489  	n int32
   490  
   491  	// conn points to the shared connection-level flow that is
   492  	// shared by all streams on that conn. It is nil for the flow
   493  	// that's on the conn directly.
   494  	conn *http2flow
   495  }
   496  
   497  func (f *http2flow) setConnFlow(cf *http2flow) { f.conn = cf }
   498  
   499  func (f *http2flow) available() int32 {
   500  	n := f.n
   501  	if f.conn != nil && f.conn.n < n {
   502  		n = f.conn.n
   503  	}
   504  	return n
   505  }
   506  
   507  func (f *http2flow) take(n int32) {
   508  	if n > f.available() {
   509  		panic("internal error: took too much")
   510  	}
   511  	f.n -= n
   512  	if f.conn != nil {
   513  		f.conn.n -= n
   514  	}
   515  }
   516  
   517  // add adds n bytes (positive or negative) to the flow control window.
   518  // It returns false if the sum would exceed 2^31-1.
   519  func (f *http2flow) add(n int32) bool {
   520  	remain := (1<<31 - 1) - f.n
   521  	if n > remain {
   522  		return false
   523  	}
   524  	f.n += n
   525  	return true
   526  }
   527  
   528  const http2frameHeaderLen = 9
   529  
   530  var http2padZeros = make([]byte, 255) // zeros for padding
   531  
   532  // A FrameType is a registered frame type as defined in
   533  // http://http2.github.io/http2-spec/#rfc.section.11.2
   534  type http2FrameType uint8
   535  
   536  const (
   537  	http2FrameData         http2FrameType = 0x0
   538  	http2FrameHeaders      http2FrameType = 0x1
   539  	http2FramePriority     http2FrameType = 0x2
   540  	http2FrameRSTStream    http2FrameType = 0x3
   541  	http2FrameSettings     http2FrameType = 0x4
   542  	http2FramePushPromise  http2FrameType = 0x5
   543  	http2FramePing         http2FrameType = 0x6
   544  	http2FrameGoAway       http2FrameType = 0x7
   545  	http2FrameWindowUpdate http2FrameType = 0x8
   546  	http2FrameContinuation http2FrameType = 0x9
   547  )
   548  
   549  var http2frameName = map[http2FrameType]string{
   550  	http2FrameData:         "DATA",
   551  	http2FrameHeaders:      "HEADERS",
   552  	http2FramePriority:     "PRIORITY",
   553  	http2FrameRSTStream:    "RST_STREAM",
   554  	http2FrameSettings:     "SETTINGS",
   555  	http2FramePushPromise:  "PUSH_PROMISE",
   556  	http2FramePing:         "PING",
   557  	http2FrameGoAway:       "GOAWAY",
   558  	http2FrameWindowUpdate: "WINDOW_UPDATE",
   559  	http2FrameContinuation: "CONTINUATION",
   560  }
   561  
   562  func (t http2FrameType) String() string {
   563  	if s, ok := http2frameName[t]; ok {
   564  		return s
   565  	}
   566  	return fmt.Sprintf("UNKNOWN_FRAME_TYPE_%d", uint8(t))
   567  }
   568  
   569  // Flags is a bitmask of HTTP/2 flags.
   570  // The meaning of flags varies depending on the frame type.
   571  type http2Flags uint8
   572  
   573  // Has reports whether f contains all (0 or more) flags in v.
   574  func (f http2Flags) Has(v http2Flags) bool {
   575  	return (f & v) == v
   576  }
   577  
   578  // Frame-specific FrameHeader flag bits.
   579  const (
   580  	// Data Frame
   581  	http2FlagDataEndStream http2Flags = 0x1
   582  	http2FlagDataPadded    http2Flags = 0x8
   583  
   584  	// Headers Frame
   585  	http2FlagHeadersEndStream  http2Flags = 0x1
   586  	http2FlagHeadersEndHeaders http2Flags = 0x4
   587  	http2FlagHeadersPadded     http2Flags = 0x8
   588  	http2FlagHeadersPriority   http2Flags = 0x20
   589  
   590  	// Settings Frame
   591  	http2FlagSettingsAck http2Flags = 0x1
   592  
   593  	// Ping Frame
   594  	http2FlagPingAck http2Flags = 0x1
   595  
   596  	// Continuation Frame
   597  	http2FlagContinuationEndHeaders http2Flags = 0x4
   598  
   599  	http2FlagPushPromiseEndHeaders http2Flags = 0x4
   600  	http2FlagPushPromisePadded     http2Flags = 0x8
   601  )
   602  
   603  var http2flagName = map[http2FrameType]map[http2Flags]string{
   604  	http2FrameData: {
   605  		http2FlagDataEndStream: "END_STREAM",
   606  		http2FlagDataPadded:    "PADDED",
   607  	},
   608  	http2FrameHeaders: {
   609  		http2FlagHeadersEndStream:  "END_STREAM",
   610  		http2FlagHeadersEndHeaders: "END_HEADERS",
   611  		http2FlagHeadersPadded:     "PADDED",
   612  		http2FlagHeadersPriority:   "PRIORITY",
   613  	},
   614  	http2FrameSettings: {
   615  		http2FlagSettingsAck: "ACK",
   616  	},
   617  	http2FramePing: {
   618  		http2FlagPingAck: "ACK",
   619  	},
   620  	http2FrameContinuation: {
   621  		http2FlagContinuationEndHeaders: "END_HEADERS",
   622  	},
   623  	http2FramePushPromise: {
   624  		http2FlagPushPromiseEndHeaders: "END_HEADERS",
   625  		http2FlagPushPromisePadded:     "PADDED",
   626  	},
   627  }
   628  
   629  // a frameParser parses a frame given its FrameHeader and payload
   630  // bytes. The length of payload will always equal fh.Length (which
   631  // might be 0).
   632  type http2frameParser func(fh http2FrameHeader, payload []byte) (http2Frame, error)
   633  
   634  var http2frameParsers = map[http2FrameType]http2frameParser{
   635  	http2FrameData:         http2parseDataFrame,
   636  	http2FrameHeaders:      http2parseHeadersFrame,
   637  	http2FramePriority:     http2parsePriorityFrame,
   638  	http2FrameRSTStream:    http2parseRSTStreamFrame,
   639  	http2FrameSettings:     http2parseSettingsFrame,
   640  	http2FramePushPromise:  http2parsePushPromise,
   641  	http2FramePing:         http2parsePingFrame,
   642  	http2FrameGoAway:       http2parseGoAwayFrame,
   643  	http2FrameWindowUpdate: http2parseWindowUpdateFrame,
   644  	http2FrameContinuation: http2parseContinuationFrame,
   645  }
   646  
   647  func http2typeFrameParser(t http2FrameType) http2frameParser {
   648  	if f := http2frameParsers[t]; f != nil {
   649  		return f
   650  	}
   651  	return http2parseUnknownFrame
   652  }
   653  
   654  // A FrameHeader is the 9 byte header of all HTTP/2 frames.
   655  //
   656  // See http://http2.github.io/http2-spec/#FrameHeader
   657  type http2FrameHeader struct {
   658  	valid bool // caller can access []byte fields in the Frame
   659  
   660  	// Type is the 1 byte frame type. There are ten standard frame
   661  	// types, but extension frame types may be written by WriteRawFrame
   662  	// and will be returned by ReadFrame (as UnknownFrame).
   663  	Type http2FrameType
   664  
   665  	// Flags are the 1 byte of 8 potential bit flags per frame.
   666  	// They are specific to the frame type.
   667  	Flags http2Flags
   668  
   669  	// Length is the length of the frame, not including the 9 byte header.
   670  	// The maximum size is one byte less than 16MB (uint24), but only
   671  	// frames up to 16KB are allowed without peer agreement.
   672  	Length uint32
   673  
   674  	// StreamID is which stream this frame is for. Certain frames
   675  	// are not stream-specific, in which case this field is 0.
   676  	StreamID uint32
   677  }
   678  
   679  // Header returns h. It exists so FrameHeaders can be embedded in other
   680  // specific frame types and implement the Frame interface.
   681  func (h http2FrameHeader) Header() http2FrameHeader { return h }
   682  
   683  func (h http2FrameHeader) String() string {
   684  	var buf bytes.Buffer
   685  	buf.WriteString("[FrameHeader ")
   686  	h.writeDebug(&buf)
   687  	buf.WriteByte(']')
   688  	return buf.String()
   689  }
   690  
   691  func (h http2FrameHeader) writeDebug(buf *bytes.Buffer) {
   692  	buf.WriteString(h.Type.String())
   693  	if h.Flags != 0 {
   694  		buf.WriteString(" flags=")
   695  		set := 0
   696  		for i := uint8(0); i < 8; i++ {
   697  			if h.Flags&(1<<i) == 0 {
   698  				continue
   699  			}
   700  			set++
   701  			if set > 1 {
   702  				buf.WriteByte('|')
   703  			}
   704  			name := http2flagName[h.Type][http2Flags(1<<i)]
   705  			if name != "" {
   706  				buf.WriteString(name)
   707  			} else {
   708  				fmt.Fprintf(buf, "0x%x", 1<<i)
   709  			}
   710  		}
   711  	}
   712  	if h.StreamID != 0 {
   713  		fmt.Fprintf(buf, " stream=%d", h.StreamID)
   714  	}
   715  	fmt.Fprintf(buf, " len=%d", h.Length)
   716  }
   717  
   718  func (h *http2FrameHeader) checkValid() {
   719  	if !h.valid {
   720  		panic("Frame accessor called on non-owned Frame")
   721  	}
   722  }
   723  
   724  func (h *http2FrameHeader) invalidate() { h.valid = false }
   725  
   726  // frame header bytes.
   727  // Used only by ReadFrameHeader.
   728  var http2fhBytes = sync.Pool{
   729  	New: func() interface{} {
   730  		buf := make([]byte, http2frameHeaderLen)
   731  		return &buf
   732  	},
   733  }
   734  
   735  // ReadFrameHeader reads 9 bytes from r and returns a FrameHeader.
   736  // Most users should use Framer.ReadFrame instead.
   737  func http2ReadFrameHeader(r io.Reader) (http2FrameHeader, error) {
   738  	bufp := http2fhBytes.Get().(*[]byte)
   739  	defer http2fhBytes.Put(bufp)
   740  	return http2readFrameHeader(*bufp, r)
   741  }
   742  
   743  func http2readFrameHeader(buf []byte, r io.Reader) (http2FrameHeader, error) {
   744  	_, err := io.ReadFull(r, buf[:http2frameHeaderLen])
   745  	if err != nil {
   746  		return http2FrameHeader{}, err
   747  	}
   748  	return http2FrameHeader{
   749  		Length:   (uint32(buf[0])<<16 | uint32(buf[1])<<8 | uint32(buf[2])),
   750  		Type:     http2FrameType(buf[3]),
   751  		Flags:    http2Flags(buf[4]),
   752  		StreamID: binary.BigEndian.Uint32(buf[5:]) & (1<<31 - 1),
   753  		valid:    true,
   754  	}, nil
   755  }
   756  
   757  // A Frame is the base interface implemented by all frame types.
   758  // Callers will generally type-assert the specific frame type:
   759  // *HeadersFrame, *SettingsFrame, *WindowUpdateFrame, etc.
   760  //
   761  // Frames are only valid until the next call to Framer.ReadFrame.
   762  type http2Frame interface {
   763  	Header() http2FrameHeader
   764  
   765  	// invalidate is called by Framer.ReadFrame to make this
   766  	// frame's buffers as being invalid, since the subsequent
   767  	// frame will reuse them.
   768  	invalidate()
   769  }
   770  
   771  // A Framer reads and writes Frames.
   772  type http2Framer struct {
   773  	r         io.Reader
   774  	lastFrame http2Frame
   775  	errDetail error
   776  
   777  	// lastHeaderStream is non-zero if the last frame was an
   778  	// unfinished HEADERS/CONTINUATION.
   779  	lastHeaderStream uint32
   780  
   781  	maxReadSize uint32
   782  	headerBuf   [http2frameHeaderLen]byte
   783  
   784  	// TODO: let getReadBuf be configurable, and use a less memory-pinning
   785  	// allocator in server.go to minimize memory pinned for many idle conns.
   786  	// Will probably also need to make frame invalidation have a hook too.
   787  	getReadBuf func(size uint32) []byte
   788  	readBuf    []byte // cache for default getReadBuf
   789  
   790  	maxWriteSize uint32 // zero means unlimited; TODO: implement
   791  
   792  	w    io.Writer
   793  	wbuf []byte
   794  
   795  	// AllowIllegalWrites permits the Framer's Write methods to
   796  	// write frames that do not conform to the HTTP/2 spec. This
   797  	// permits using the Framer to test other HTTP/2
   798  	// implementations' conformance to the spec.
   799  	// If false, the Write methods will prefer to return an error
   800  	// rather than comply.
   801  	AllowIllegalWrites bool
   802  
   803  	// AllowIllegalReads permits the Framer's ReadFrame method
   804  	// to return non-compliant frames or frame orders.
   805  	// This is for testing and permits using the Framer to test
   806  	// other HTTP/2 implementations' conformance to the spec.
   807  	// It is not compatible with ReadMetaHeaders.
   808  	AllowIllegalReads bool
   809  
   810  	// ReadMetaHeaders if non-nil causes ReadFrame to merge
   811  	// HEADERS and CONTINUATION frames together and return
   812  	// MetaHeadersFrame instead.
   813  	ReadMetaHeaders *hpack.Decoder
   814  
   815  	// MaxHeaderListSize is the http2 MAX_HEADER_LIST_SIZE.
   816  	// It's used only if ReadMetaHeaders is set; 0 means a sane default
   817  	// (currently 16MB)
   818  	// If the limit is hit, MetaHeadersFrame.Truncated is set true.
   819  	MaxHeaderListSize uint32
   820  
   821  	logReads bool
   822  
   823  	debugFramer    *http2Framer // only use for logging written writes
   824  	debugFramerBuf *bytes.Buffer
   825  }
   826  
   827  func (fr *http2Framer) maxHeaderListSize() uint32 {
   828  	if fr.MaxHeaderListSize == 0 {
   829  		return 16 << 20
   830  	}
   831  	return fr.MaxHeaderListSize
   832  }
   833  
   834  func (f *http2Framer) startWrite(ftype http2FrameType, flags http2Flags, streamID uint32) {
   835  
   836  	f.wbuf = append(f.wbuf[:0],
   837  		0,
   838  		0,
   839  		0,
   840  		byte(ftype),
   841  		byte(flags),
   842  		byte(streamID>>24),
   843  		byte(streamID>>16),
   844  		byte(streamID>>8),
   845  		byte(streamID))
   846  }
   847  
   848  func (f *http2Framer) endWrite() error {
   849  
   850  	length := len(f.wbuf) - http2frameHeaderLen
   851  	if length >= (1 << 24) {
   852  		return http2ErrFrameTooLarge
   853  	}
   854  	_ = append(f.wbuf[:0],
   855  		byte(length>>16),
   856  		byte(length>>8),
   857  		byte(length))
   858  	if http2logFrameWrites {
   859  		f.logWrite()
   860  	}
   861  
   862  	n, err := f.w.Write(f.wbuf)
   863  	if err == nil && n != len(f.wbuf) {
   864  		err = io.ErrShortWrite
   865  	}
   866  	return err
   867  }
   868  
   869  func (f *http2Framer) logWrite() {
   870  	if f.debugFramer == nil {
   871  		f.debugFramerBuf = new(bytes.Buffer)
   872  		f.debugFramer = http2NewFramer(nil, f.debugFramerBuf)
   873  		f.debugFramer.logReads = false
   874  
   875  		f.debugFramer.AllowIllegalReads = true
   876  	}
   877  	f.debugFramerBuf.Write(f.wbuf)
   878  	fr, err := f.debugFramer.ReadFrame()
   879  	if err != nil {
   880  		log.Printf("http2: Framer %p: failed to decode just-written frame", f)
   881  		return
   882  	}
   883  	log.Printf("http2: Framer %p: wrote %v", f, http2summarizeFrame(fr))
   884  }
   885  
   886  func (f *http2Framer) writeByte(v byte) { f.wbuf = append(f.wbuf, v) }
   887  
   888  func (f *http2Framer) writeBytes(v []byte) { f.wbuf = append(f.wbuf, v...) }
   889  
   890  func (f *http2Framer) writeUint16(v uint16) { f.wbuf = append(f.wbuf, byte(v>>8), byte(v)) }
   891  
   892  func (f *http2Framer) writeUint32(v uint32) {
   893  	f.wbuf = append(f.wbuf, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
   894  }
   895  
   896  const (
   897  	http2minMaxFrameSize = 1 << 14
   898  	http2maxFrameSize    = 1<<24 - 1
   899  )
   900  
   901  // NewFramer returns a Framer that writes frames to w and reads them from r.
   902  func http2NewFramer(w io.Writer, r io.Reader) *http2Framer {
   903  	fr := &http2Framer{
   904  		w:        w,
   905  		r:        r,
   906  		logReads: http2logFrameReads,
   907  	}
   908  	fr.getReadBuf = func(size uint32) []byte {
   909  		if cap(fr.readBuf) >= int(size) {
   910  			return fr.readBuf[:size]
   911  		}
   912  		fr.readBuf = make([]byte, size)
   913  		return fr.readBuf
   914  	}
   915  	fr.SetMaxReadFrameSize(http2maxFrameSize)
   916  	return fr
   917  }
   918  
   919  // SetMaxReadFrameSize sets the maximum size of a frame
   920  // that will be read by a subsequent call to ReadFrame.
   921  // It is the caller's responsibility to advertise this
   922  // limit with a SETTINGS frame.
   923  func (fr *http2Framer) SetMaxReadFrameSize(v uint32) {
   924  	if v > http2maxFrameSize {
   925  		v = http2maxFrameSize
   926  	}
   927  	fr.maxReadSize = v
   928  }
   929  
   930  // ErrorDetail returns a more detailed error of the last error
   931  // returned by Framer.ReadFrame. For instance, if ReadFrame
   932  // returns a StreamError with code PROTOCOL_ERROR, ErrorDetail
   933  // will say exactly what was invalid. ErrorDetail is not guaranteed
   934  // to return a non-nil value and like the rest of the http2 package,
   935  // its return value is not protected by an API compatibility promise.
   936  // ErrorDetail is reset after the next call to ReadFrame.
   937  func (fr *http2Framer) ErrorDetail() error {
   938  	return fr.errDetail
   939  }
   940  
   941  // ErrFrameTooLarge is returned from Framer.ReadFrame when the peer
   942  // sends a frame that is larger than declared with SetMaxReadFrameSize.
   943  var http2ErrFrameTooLarge = errors.New("http2: frame too large")
   944  
   945  // terminalReadFrameError reports whether err is an unrecoverable
   946  // error from ReadFrame and no other frames should be read.
   947  func http2terminalReadFrameError(err error) bool {
   948  	if _, ok := err.(http2StreamError); ok {
   949  		return false
   950  	}
   951  	return err != nil
   952  }
   953  
   954  // ReadFrame reads a single frame. The returned Frame is only valid
   955  // until the next call to ReadFrame.
   956  //
   957  // If the frame is larger than previously set with SetMaxReadFrameSize, the
   958  // returned error is ErrFrameTooLarge. Other errors may be of type
   959  // ConnectionError, StreamError, or anything else from from the underlying
   960  // reader.
   961  func (fr *http2Framer) ReadFrame() (http2Frame, error) {
   962  	fr.errDetail = nil
   963  	if fr.lastFrame != nil {
   964  		fr.lastFrame.invalidate()
   965  	}
   966  	fh, err := http2readFrameHeader(fr.headerBuf[:], fr.r)
   967  	if err != nil {
   968  		return nil, err
   969  	}
   970  	if fh.Length > fr.maxReadSize {
   971  		return nil, http2ErrFrameTooLarge
   972  	}
   973  	payload := fr.getReadBuf(fh.Length)
   974  	if _, err := io.ReadFull(fr.r, payload); err != nil {
   975  		return nil, err
   976  	}
   977  	f, err := http2typeFrameParser(fh.Type)(fh, payload)
   978  	if err != nil {
   979  		if ce, ok := err.(http2connError); ok {
   980  			return nil, fr.connError(ce.Code, ce.Reason)
   981  		}
   982  		return nil, err
   983  	}
   984  	if err := fr.checkFrameOrder(f); err != nil {
   985  		return nil, err
   986  	}
   987  	if fr.logReads {
   988  		log.Printf("http2: Framer %p: read %v", fr, http2summarizeFrame(f))
   989  	}
   990  	if fh.Type == http2FrameHeaders && fr.ReadMetaHeaders != nil {
   991  		return fr.readMetaFrame(f.(*http2HeadersFrame))
   992  	}
   993  	return f, nil
   994  }
   995  
   996  // connError returns ConnectionError(code) but first
   997  // stashes away a public reason to the caller can optionally relay it
   998  // to the peer before hanging up on them. This might help others debug
   999  // their implementations.
  1000  func (fr *http2Framer) connError(code http2ErrCode, reason string) error {
  1001  	fr.errDetail = errors.New(reason)
  1002  	return http2ConnectionError(code)
  1003  }
  1004  
  1005  // checkFrameOrder reports an error if f is an invalid frame to return
  1006  // next from ReadFrame. Mostly it checks whether HEADERS and
  1007  // CONTINUATION frames are contiguous.
  1008  func (fr *http2Framer) checkFrameOrder(f http2Frame) error {
  1009  	last := fr.lastFrame
  1010  	fr.lastFrame = f
  1011  	if fr.AllowIllegalReads {
  1012  		return nil
  1013  	}
  1014  
  1015  	fh := f.Header()
  1016  	if fr.lastHeaderStream != 0 {
  1017  		if fh.Type != http2FrameContinuation {
  1018  			return fr.connError(http2ErrCodeProtocol,
  1019  				fmt.Sprintf("got %s for stream %d; expected CONTINUATION following %s for stream %d",
  1020  					fh.Type, fh.StreamID,
  1021  					last.Header().Type, fr.lastHeaderStream))
  1022  		}
  1023  		if fh.StreamID != fr.lastHeaderStream {
  1024  			return fr.connError(http2ErrCodeProtocol,
  1025  				fmt.Sprintf("got CONTINUATION for stream %d; expected stream %d",
  1026  					fh.StreamID, fr.lastHeaderStream))
  1027  		}
  1028  	} else if fh.Type == http2FrameContinuation {
  1029  		return fr.connError(http2ErrCodeProtocol, fmt.Sprintf("unexpected CONTINUATION for stream %d", fh.StreamID))
  1030  	}
  1031  
  1032  	switch fh.Type {
  1033  	case http2FrameHeaders, http2FrameContinuation:
  1034  		if fh.Flags.Has(http2FlagHeadersEndHeaders) {
  1035  			fr.lastHeaderStream = 0
  1036  		} else {
  1037  			fr.lastHeaderStream = fh.StreamID
  1038  		}
  1039  	}
  1040  
  1041  	return nil
  1042  }
  1043  
  1044  // A DataFrame conveys arbitrary, variable-length sequences of octets
  1045  // associated with a stream.
  1046  // See http://http2.github.io/http2-spec/#rfc.section.6.1
  1047  type http2DataFrame struct {
  1048  	http2FrameHeader
  1049  	data []byte
  1050  }
  1051  
  1052  func (f *http2DataFrame) StreamEnded() bool {
  1053  	return f.http2FrameHeader.Flags.Has(http2FlagDataEndStream)
  1054  }
  1055  
  1056  // Data returns the frame's data octets, not including any padding
  1057  // size byte or padding suffix bytes.
  1058  // The caller must not retain the returned memory past the next
  1059  // call to ReadFrame.
  1060  func (f *http2DataFrame) Data() []byte {
  1061  	f.checkValid()
  1062  	return f.data
  1063  }
  1064  
  1065  func http2parseDataFrame(fh http2FrameHeader, payload []byte) (http2Frame, error) {
  1066  	if fh.StreamID == 0 {
  1067  
  1068  		return nil, http2connError{http2ErrCodeProtocol, "DATA frame with stream ID 0"}
  1069  	}
  1070  	f := &http2DataFrame{
  1071  		http2FrameHeader: fh,
  1072  	}
  1073  	var padSize byte
  1074  	if fh.Flags.Has(http2FlagDataPadded) {
  1075  		var err error
  1076  		payload, padSize, err = http2readByte(payload)
  1077  		if err != nil {
  1078  			return nil, err
  1079  		}
  1080  	}
  1081  	if int(padSize) > len(payload) {
  1082  
  1083  		return nil, http2connError{http2ErrCodeProtocol, "pad size larger than data payload"}
  1084  	}
  1085  	f.data = payload[:len(payload)-int(padSize)]
  1086  	return f, nil
  1087  }
  1088  
  1089  var http2errStreamID = errors.New("invalid streamid")
  1090  
  1091  func http2validStreamID(streamID uint32) bool {
  1092  	return streamID != 0 && streamID&(1<<31) == 0
  1093  }
  1094  
  1095  // WriteData writes a DATA frame.
  1096  //
  1097  // It will perform exactly one Write to the underlying Writer.
  1098  // It is the caller's responsibility to not call other Write methods concurrently.
  1099  func (f *http2Framer) WriteData(streamID uint32, endStream bool, data []byte) error {
  1100  
  1101  	if !http2validStreamID(streamID) && !f.AllowIllegalWrites {
  1102  		return http2errStreamID
  1103  	}
  1104  	var flags http2Flags
  1105  	if endStream {
  1106  		flags |= http2FlagDataEndStream
  1107  	}
  1108  	f.startWrite(http2FrameData, flags, streamID)
  1109  	f.wbuf = append(f.wbuf, data...)
  1110  	return f.endWrite()
  1111  }
  1112  
  1113  // A SettingsFrame conveys configuration parameters that affect how
  1114  // endpoints communicate, such as preferences and constraints on peer
  1115  // behavior.
  1116  //
  1117  // See http://http2.github.io/http2-spec/#SETTINGS
  1118  type http2SettingsFrame struct {
  1119  	http2FrameHeader
  1120  	p []byte
  1121  }
  1122  
  1123  func http2parseSettingsFrame(fh http2FrameHeader, p []byte) (http2Frame, error) {
  1124  	if fh.Flags.Has(http2FlagSettingsAck) && fh.Length > 0 {
  1125  
  1126  		return nil, http2ConnectionError(http2ErrCodeFrameSize)
  1127  	}
  1128  	if fh.StreamID != 0 {
  1129  
  1130  		return nil, http2ConnectionError(http2ErrCodeProtocol)
  1131  	}
  1132  	if len(p)%6 != 0 {
  1133  
  1134  		return nil, http2ConnectionError(http2ErrCodeFrameSize)
  1135  	}
  1136  	f := &http2SettingsFrame{http2FrameHeader: fh, p: p}
  1137  	if v, ok := f.Value(http2SettingInitialWindowSize); ok && v > (1<<31)-1 {
  1138  
  1139  		return nil, http2ConnectionError(http2ErrCodeFlowControl)
  1140  	}
  1141  	return f, nil
  1142  }
  1143  
  1144  func (f *http2SettingsFrame) IsAck() bool {
  1145  	return f.http2FrameHeader.Flags.Has(http2FlagSettingsAck)
  1146  }
  1147  
  1148  func (f *http2SettingsFrame) Value(s http2SettingID) (v uint32, ok bool) {
  1149  	f.checkValid()
  1150  	buf := f.p
  1151  	for len(buf) > 0 {
  1152  		settingID := http2SettingID(binary.BigEndian.Uint16(buf[:2]))
  1153  		if settingID == s {
  1154  			return binary.BigEndian.Uint32(buf[2:6]), true
  1155  		}
  1156  		buf = buf[6:]
  1157  	}
  1158  	return 0, false
  1159  }
  1160  
  1161  // ForeachSetting runs fn for each setting.
  1162  // It stops and returns the first error.
  1163  func (f *http2SettingsFrame) ForeachSetting(fn func(http2Setting) error) error {
  1164  	f.checkValid()
  1165  	buf := f.p
  1166  	for len(buf) > 0 {
  1167  		if err := fn(http2Setting{
  1168  			http2SettingID(binary.BigEndian.Uint16(buf[:2])),
  1169  			binary.BigEndian.Uint32(buf[2:6]),
  1170  		}); err != nil {
  1171  			return err
  1172  		}
  1173  		buf = buf[6:]
  1174  	}
  1175  	return nil
  1176  }
  1177  
  1178  // WriteSettings writes a SETTINGS frame with zero or more settings
  1179  // specified and the ACK bit not set.
  1180  //
  1181  // It will perform exactly one Write to the underlying Writer.
  1182  // It is the caller's responsibility to not call other Write methods concurrently.
  1183  func (f *http2Framer) WriteSettings(settings ...http2Setting) error {
  1184  	f.startWrite(http2FrameSettings, 0, 0)
  1185  	for _, s := range settings {
  1186  		f.writeUint16(uint16(s.ID))
  1187  		f.writeUint32(s.Val)
  1188  	}
  1189  	return f.endWrite()
  1190  }
  1191  
  1192  // WriteSettings writes an empty SETTINGS frame with the ACK bit set.
  1193  //
  1194  // It will perform exactly one Write to the underlying Writer.
  1195  // It is the caller's responsibility to not call other Write methods concurrently.
  1196  func (f *http2Framer) WriteSettingsAck() error {
  1197  	f.startWrite(http2FrameSettings, http2FlagSettingsAck, 0)
  1198  	return f.endWrite()
  1199  }
  1200  
  1201  // A PingFrame is a mechanism for measuring a minimal round trip time
  1202  // from the sender, as well as determining whether an idle connection
  1203  // is still functional.
  1204  // See http://http2.github.io/http2-spec/#rfc.section.6.7
  1205  type http2PingFrame struct {
  1206  	http2FrameHeader
  1207  	Data [8]byte
  1208  }
  1209  
  1210  func (f *http2PingFrame) IsAck() bool { return f.Flags.Has(http2FlagPingAck) }
  1211  
  1212  func http2parsePingFrame(fh http2FrameHeader, payload []byte) (http2Frame, error) {
  1213  	if len(payload) != 8 {
  1214  		return nil, http2ConnectionError(http2ErrCodeFrameSize)
  1215  	}
  1216  	if fh.StreamID != 0 {
  1217  		return nil, http2ConnectionError(http2ErrCodeProtocol)
  1218  	}
  1219  	f := &http2PingFrame{http2FrameHeader: fh}
  1220  	copy(f.Data[:], payload)
  1221  	return f, nil
  1222  }
  1223  
  1224  func (f *http2Framer) WritePing(ack bool, data [8]byte) error {
  1225  	var flags http2Flags
  1226  	if ack {
  1227  		flags = http2FlagPingAck
  1228  	}
  1229  	f.startWrite(http2FramePing, flags, 0)
  1230  	f.writeBytes(data[:])
  1231  	return f.endWrite()
  1232  }
  1233  
  1234  // A GoAwayFrame informs the remote peer to stop creating streams on this connection.
  1235  // See http://http2.github.io/http2-spec/#rfc.section.6.8
  1236  type http2GoAwayFrame struct {
  1237  	http2FrameHeader
  1238  	LastStreamID uint32
  1239  	ErrCode      http2ErrCode
  1240  	debugData    []byte
  1241  }
  1242  
  1243  // DebugData returns any debug data in the GOAWAY frame. Its contents
  1244  // are not defined.
  1245  // The caller must not retain the returned memory past the next
  1246  // call to ReadFrame.
  1247  func (f *http2GoAwayFrame) DebugData() []byte {
  1248  	f.checkValid()
  1249  	return f.debugData
  1250  }
  1251  
  1252  func http2parseGoAwayFrame(fh http2FrameHeader, p []byte) (http2Frame, error) {
  1253  	if fh.StreamID != 0 {
  1254  		return nil, http2ConnectionError(http2ErrCodeProtocol)
  1255  	}
  1256  	if len(p) < 8 {
  1257  		return nil, http2ConnectionError(http2ErrCodeFrameSize)
  1258  	}
  1259  	return &http2GoAwayFrame{
  1260  		http2FrameHeader: fh,
  1261  		LastStreamID:     binary.BigEndian.Uint32(p[:4]) & (1<<31 - 1),
  1262  		ErrCode:          http2ErrCode(binary.BigEndian.Uint32(p[4:8])),
  1263  		debugData:        p[8:],
  1264  	}, nil
  1265  }
  1266  
  1267  func (f *http2Framer) WriteGoAway(maxStreamID uint32, code http2ErrCode, debugData []byte) error {
  1268  	f.startWrite(http2FrameGoAway, 0, 0)
  1269  	f.writeUint32(maxStreamID & (1<<31 - 1))
  1270  	f.writeUint32(uint32(code))
  1271  	f.writeBytes(debugData)
  1272  	return f.endWrite()
  1273  }
  1274  
  1275  // An UnknownFrame is the frame type returned when the frame type is unknown
  1276  // or no specific frame type parser exists.
  1277  type http2UnknownFrame struct {
  1278  	http2FrameHeader
  1279  	p []byte
  1280  }
  1281  
  1282  // Payload returns the frame's payload (after the header).  It is not
  1283  // valid to call this method after a subsequent call to
  1284  // Framer.ReadFrame, nor is it valid to retain the returned slice.
  1285  // The memory is owned by the Framer and is invalidated when the next
  1286  // frame is read.
  1287  func (f *http2UnknownFrame) Payload() []byte {
  1288  	f.checkValid()
  1289  	return f.p
  1290  }
  1291  
  1292  func http2parseUnknownFrame(fh http2FrameHeader, p []byte) (http2Frame, error) {
  1293  	return &http2UnknownFrame{fh, p}, nil
  1294  }
  1295  
  1296  // A WindowUpdateFrame is used to implement flow control.
  1297  // See http://http2.github.io/http2-spec/#rfc.section.6.9
  1298  type http2WindowUpdateFrame struct {
  1299  	http2FrameHeader
  1300  	Increment uint32 // never read with high bit set
  1301  }
  1302  
  1303  func http2parseWindowUpdateFrame(fh http2FrameHeader, p []byte) (http2Frame, error) {
  1304  	if len(p) != 4 {
  1305  		return nil, http2ConnectionError(http2ErrCodeFrameSize)
  1306  	}
  1307  	inc := binary.BigEndian.Uint32(p[:4]) & 0x7fffffff
  1308  	if inc == 0 {
  1309  
  1310  		if fh.StreamID == 0 {
  1311  			return nil, http2ConnectionError(http2ErrCodeProtocol)
  1312  		}
  1313  		return nil, http2StreamError{fh.StreamID, http2ErrCodeProtocol}
  1314  	}
  1315  	return &http2WindowUpdateFrame{
  1316  		http2FrameHeader: fh,
  1317  		Increment:        inc,
  1318  	}, nil
  1319  }
  1320  
  1321  // WriteWindowUpdate writes a WINDOW_UPDATE frame.
  1322  // The increment value must be between 1 and 2,147,483,647, inclusive.
  1323  // If the Stream ID is zero, the window update applies to the
  1324  // connection as a whole.
  1325  func (f *http2Framer) WriteWindowUpdate(streamID, incr uint32) error {
  1326  
  1327  	if (incr < 1 || incr > 2147483647) && !f.AllowIllegalWrites {
  1328  		return errors.New("illegal window increment value")
  1329  	}
  1330  	f.startWrite(http2FrameWindowUpdate, 0, streamID)
  1331  	f.writeUint32(incr)
  1332  	return f.endWrite()
  1333  }
  1334  
  1335  // A HeadersFrame is used to open a stream and additionally carries a
  1336  // header block fragment.
  1337  type http2HeadersFrame struct {
  1338  	http2FrameHeader
  1339  
  1340  	// Priority is set if FlagHeadersPriority is set in the FrameHeader.
  1341  	Priority http2PriorityParam
  1342  
  1343  	headerFragBuf []byte // not owned
  1344  }
  1345  
  1346  func (f *http2HeadersFrame) HeaderBlockFragment() []byte {
  1347  	f.checkValid()
  1348  	return f.headerFragBuf
  1349  }
  1350  
  1351  func (f *http2HeadersFrame) HeadersEnded() bool {
  1352  	return f.http2FrameHeader.Flags.Has(http2FlagHeadersEndHeaders)
  1353  }
  1354  
  1355  func (f *http2HeadersFrame) StreamEnded() bool {
  1356  	return f.http2FrameHeader.Flags.Has(http2FlagHeadersEndStream)
  1357  }
  1358  
  1359  func (f *http2HeadersFrame) HasPriority() bool {
  1360  	return f.http2FrameHeader.Flags.Has(http2FlagHeadersPriority)
  1361  }
  1362  
  1363  func http2parseHeadersFrame(fh http2FrameHeader, p []byte) (_ http2Frame, err error) {
  1364  	hf := &http2HeadersFrame{
  1365  		http2FrameHeader: fh,
  1366  	}
  1367  	if fh.StreamID == 0 {
  1368  
  1369  		return nil, http2connError{http2ErrCodeProtocol, "HEADERS frame with stream ID 0"}
  1370  	}
  1371  	var padLength uint8
  1372  	if fh.Flags.Has(http2FlagHeadersPadded) {
  1373  		if p, padLength, err = http2readByte(p); err != nil {
  1374  			return
  1375  		}
  1376  	}
  1377  	if fh.Flags.Has(http2FlagHeadersPriority) {
  1378  		var v uint32
  1379  		p, v, err = http2readUint32(p)
  1380  		if err != nil {
  1381  			return nil, err
  1382  		}
  1383  		hf.Priority.StreamDep = v & 0x7fffffff
  1384  		hf.Priority.Exclusive = (v != hf.Priority.StreamDep)
  1385  		p, hf.Priority.Weight, err = http2readByte(p)
  1386  		if err != nil {
  1387  			return nil, err
  1388  		}
  1389  	}
  1390  	if len(p)-int(padLength) <= 0 {
  1391  		return nil, http2StreamError{fh.StreamID, http2ErrCodeProtocol}
  1392  	}
  1393  	hf.headerFragBuf = p[:len(p)-int(padLength)]
  1394  	return hf, nil
  1395  }
  1396  
  1397  // HeadersFrameParam are the parameters for writing a HEADERS frame.
  1398  type http2HeadersFrameParam struct {
  1399  	// StreamID is the required Stream ID to initiate.
  1400  	StreamID uint32
  1401  	// BlockFragment is part (or all) of a Header Block.
  1402  	BlockFragment []byte
  1403  
  1404  	// EndStream indicates that the header block is the last that
  1405  	// the endpoint will send for the identified stream. Setting
  1406  	// this flag causes the stream to enter one of "half closed"
  1407  	// states.
  1408  	EndStream bool
  1409  
  1410  	// EndHeaders indicates that this frame contains an entire
  1411  	// header block and is not followed by any
  1412  	// CONTINUATION frames.
  1413  	EndHeaders bool
  1414  
  1415  	// PadLength is the optional number of bytes of zeros to add
  1416  	// to this frame.
  1417  	PadLength uint8
  1418  
  1419  	// Priority, if non-zero, includes stream priority information
  1420  	// in the HEADER frame.
  1421  	Priority http2PriorityParam
  1422  }
  1423  
  1424  // WriteHeaders writes a single HEADERS frame.
  1425  //
  1426  // This is a low-level header writing method. Encoding headers and
  1427  // splitting them into any necessary CONTINUATION frames is handled
  1428  // elsewhere.
  1429  //
  1430  // It will perform exactly one Write to the underlying Writer.
  1431  // It is the caller's responsibility to not call other Write methods concurrently.
  1432  func (f *http2Framer) WriteHeaders(p http2HeadersFrameParam) error {
  1433  	if !http2validStreamID(p.StreamID) && !f.AllowIllegalWrites {
  1434  		return http2errStreamID
  1435  	}
  1436  	var flags http2Flags
  1437  	if p.PadLength != 0 {
  1438  		flags |= http2FlagHeadersPadded
  1439  	}
  1440  	if p.EndStream {
  1441  		flags |= http2FlagHeadersEndStream
  1442  	}
  1443  	if p.EndHeaders {
  1444  		flags |= http2FlagHeadersEndHeaders
  1445  	}
  1446  	if !p.Priority.IsZero() {
  1447  		flags |= http2FlagHeadersPriority
  1448  	}
  1449  	f.startWrite(http2FrameHeaders, flags, p.StreamID)
  1450  	if p.PadLength != 0 {
  1451  		f.writeByte(p.PadLength)
  1452  	}
  1453  	if !p.Priority.IsZero() {
  1454  		v := p.Priority.StreamDep
  1455  		if !http2validStreamID(v) && !f.AllowIllegalWrites {
  1456  			return errors.New("invalid dependent stream id")
  1457  		}
  1458  		if p.Priority.Exclusive {
  1459  			v |= 1 << 31
  1460  		}
  1461  		f.writeUint32(v)
  1462  		f.writeByte(p.Priority.Weight)
  1463  	}
  1464  	f.wbuf = append(f.wbuf, p.BlockFragment...)
  1465  	f.wbuf = append(f.wbuf, http2padZeros[:p.PadLength]...)
  1466  	return f.endWrite()
  1467  }
  1468  
  1469  // A PriorityFrame specifies the sender-advised priority of a stream.
  1470  // See http://http2.github.io/http2-spec/#rfc.section.6.3
  1471  type http2PriorityFrame struct {
  1472  	http2FrameHeader
  1473  	http2PriorityParam
  1474  }
  1475  
  1476  // PriorityParam are the stream prioritzation parameters.
  1477  type http2PriorityParam struct {
  1478  	// StreamDep is a 31-bit stream identifier for the
  1479  	// stream that this stream depends on. Zero means no
  1480  	// dependency.
  1481  	StreamDep uint32
  1482  
  1483  	// Exclusive is whether the dependency is exclusive.
  1484  	Exclusive bool
  1485  
  1486  	// Weight is the stream's zero-indexed weight. It should be
  1487  	// set together with StreamDep, or neither should be set.  Per
  1488  	// the spec, "Add one to the value to obtain a weight between
  1489  	// 1 and 256."
  1490  	Weight uint8
  1491  }
  1492  
  1493  func (p http2PriorityParam) IsZero() bool {
  1494  	return p == http2PriorityParam{}
  1495  }
  1496  
  1497  func http2parsePriorityFrame(fh http2FrameHeader, payload []byte) (http2Frame, error) {
  1498  	if fh.StreamID == 0 {
  1499  		return nil, http2connError{http2ErrCodeProtocol, "PRIORITY frame with stream ID 0"}
  1500  	}
  1501  	if len(payload) != 5 {
  1502  		return nil, http2connError{http2ErrCodeFrameSize, fmt.Sprintf("PRIORITY frame payload size was %d; want 5", len(payload))}
  1503  	}
  1504  	v := binary.BigEndian.Uint32(payload[:4])
  1505  	streamID := v & 0x7fffffff
  1506  	return &http2PriorityFrame{
  1507  		http2FrameHeader: fh,
  1508  		http2PriorityParam: http2PriorityParam{
  1509  			Weight:    payload[4],
  1510  			StreamDep: streamID,
  1511  			Exclusive: streamID != v,
  1512  		},
  1513  	}, nil
  1514  }
  1515  
  1516  // WritePriority writes a PRIORITY frame.
  1517  //
  1518  // It will perform exactly one Write to the underlying Writer.
  1519  // It is the caller's responsibility to not call other Write methods concurrently.
  1520  func (f *http2Framer) WritePriority(streamID uint32, p http2PriorityParam) error {
  1521  	if !http2validStreamID(streamID) && !f.AllowIllegalWrites {
  1522  		return http2errStreamID
  1523  	}
  1524  	f.startWrite(http2FramePriority, 0, streamID)
  1525  	v := p.StreamDep
  1526  	if p.Exclusive {
  1527  		v |= 1 << 31
  1528  	}
  1529  	f.writeUint32(v)
  1530  	f.writeByte(p.Weight)
  1531  	return f.endWrite()
  1532  }
  1533  
  1534  // A RSTStreamFrame allows for abnormal termination of a stream.
  1535  // See http://http2.github.io/http2-spec/#rfc.section.6.4
  1536  type http2RSTStreamFrame struct {
  1537  	http2FrameHeader
  1538  	ErrCode http2ErrCode
  1539  }
  1540  
  1541  func http2parseRSTStreamFrame(fh http2FrameHeader, p []byte) (http2Frame, error) {
  1542  	if len(p) != 4 {
  1543  		return nil, http2ConnectionError(http2ErrCodeFrameSize)
  1544  	}
  1545  	if fh.StreamID == 0 {
  1546  		return nil, http2ConnectionError(http2ErrCodeProtocol)
  1547  	}
  1548  	return &http2RSTStreamFrame{fh, http2ErrCode(binary.BigEndian.Uint32(p[:4]))}, nil
  1549  }
  1550  
  1551  // WriteRSTStream writes a RST_STREAM frame.
  1552  //
  1553  // It will perform exactly one Write to the underlying Writer.
  1554  // It is the caller's responsibility to not call other Write methods concurrently.
  1555  func (f *http2Framer) WriteRSTStream(streamID uint32, code http2ErrCode) error {
  1556  	if !http2validStreamID(streamID) && !f.AllowIllegalWrites {
  1557  		return http2errStreamID
  1558  	}
  1559  	f.startWrite(http2FrameRSTStream, 0, streamID)
  1560  	f.writeUint32(uint32(code))
  1561  	return f.endWrite()
  1562  }
  1563  
  1564  // A ContinuationFrame is used to continue a sequence of header block fragments.
  1565  // See http://http2.github.io/http2-spec/#rfc.section.6.10
  1566  type http2ContinuationFrame struct {
  1567  	http2FrameHeader
  1568  	headerFragBuf []byte
  1569  }
  1570  
  1571  func http2parseContinuationFrame(fh http2FrameHeader, p []byte) (http2Frame, error) {
  1572  	if fh.StreamID == 0 {
  1573  		return nil, http2connError{http2ErrCodeProtocol, "CONTINUATION frame with stream ID 0"}
  1574  	}
  1575  	return &http2ContinuationFrame{fh, p}, nil
  1576  }
  1577  
  1578  func (f *http2ContinuationFrame) HeaderBlockFragment() []byte {
  1579  	f.checkValid()
  1580  	return f.headerFragBuf
  1581  }
  1582  
  1583  func (f *http2ContinuationFrame) HeadersEnded() bool {
  1584  	return f.http2FrameHeader.Flags.Has(http2FlagContinuationEndHeaders)
  1585  }
  1586  
  1587  // WriteContinuation writes a CONTINUATION frame.
  1588  //
  1589  // It will perform exactly one Write to the underlying Writer.
  1590  // It is the caller's responsibility to not call other Write methods concurrently.
  1591  func (f *http2Framer) WriteContinuation(streamID uint32, endHeaders bool, headerBlockFragment []byte) error {
  1592  	if !http2validStreamID(streamID) && !f.AllowIllegalWrites {
  1593  		return http2errStreamID
  1594  	}
  1595  	var flags http2Flags
  1596  	if endHeaders {
  1597  		flags |= http2FlagContinuationEndHeaders
  1598  	}
  1599  	f.startWrite(http2FrameContinuation, flags, streamID)
  1600  	f.wbuf = append(f.wbuf, headerBlockFragment...)
  1601  	return f.endWrite()
  1602  }
  1603  
  1604  // A PushPromiseFrame is used to initiate a server stream.
  1605  // See http://http2.github.io/http2-spec/#rfc.section.6.6
  1606  type http2PushPromiseFrame struct {
  1607  	http2FrameHeader
  1608  	PromiseID     uint32
  1609  	headerFragBuf []byte // not owned
  1610  }
  1611  
  1612  func (f *http2PushPromiseFrame) HeaderBlockFragment() []byte {
  1613  	f.checkValid()
  1614  	return f.headerFragBuf
  1615  }
  1616  
  1617  func (f *http2PushPromiseFrame) HeadersEnded() bool {
  1618  	return f.http2FrameHeader.Flags.Has(http2FlagPushPromiseEndHeaders)
  1619  }
  1620  
  1621  func http2parsePushPromise(fh http2FrameHeader, p []byte) (_ http2Frame, err error) {
  1622  	pp := &http2PushPromiseFrame{
  1623  		http2FrameHeader: fh,
  1624  	}
  1625  	if pp.StreamID == 0 {
  1626  
  1627  		return nil, http2ConnectionError(http2ErrCodeProtocol)
  1628  	}
  1629  	// The PUSH_PROMISE frame includes optional padding.
  1630  	// Padding fields and flags are identical to those defined for DATA frames
  1631  	var padLength uint8
  1632  	if fh.Flags.Has(http2FlagPushPromisePadded) {
  1633  		if p, padLength, err = http2readByte(p); err != nil {
  1634  			return
  1635  		}
  1636  	}
  1637  
  1638  	p, pp.PromiseID, err = http2readUint32(p)
  1639  	if err != nil {
  1640  		return
  1641  	}
  1642  	pp.PromiseID = pp.PromiseID & (1<<31 - 1)
  1643  
  1644  	if int(padLength) > len(p) {
  1645  
  1646  		return nil, http2ConnectionError(http2ErrCodeProtocol)
  1647  	}
  1648  	pp.headerFragBuf = p[:len(p)-int(padLength)]
  1649  	return pp, nil
  1650  }
  1651  
  1652  // PushPromiseParam are the parameters for writing a PUSH_PROMISE frame.
  1653  type http2PushPromiseParam struct {
  1654  	// StreamID is the required Stream ID to initiate.
  1655  	StreamID uint32
  1656  
  1657  	// PromiseID is the required Stream ID which this
  1658  	// Push Promises
  1659  	PromiseID uint32
  1660  
  1661  	// BlockFragment is part (or all) of a Header Block.
  1662  	BlockFragment []byte
  1663  
  1664  	// EndHeaders indicates that this frame contains an entire
  1665  	// header block and is not followed by any
  1666  	// CONTINUATION frames.
  1667  	EndHeaders bool
  1668  
  1669  	// PadLength is the optional number of bytes of zeros to add
  1670  	// to this frame.
  1671  	PadLength uint8
  1672  }
  1673  
  1674  // WritePushPromise writes a single PushPromise Frame.
  1675  //
  1676  // As with Header Frames, This is the low level call for writing
  1677  // individual frames. Continuation frames are handled elsewhere.
  1678  //
  1679  // It will perform exactly one Write to the underlying Writer.
  1680  // It is the caller's responsibility to not call other Write methods concurrently.
  1681  func (f *http2Framer) WritePushPromise(p http2PushPromiseParam) error {
  1682  	if !http2validStreamID(p.StreamID) && !f.AllowIllegalWrites {
  1683  		return http2errStreamID
  1684  	}
  1685  	var flags http2Flags
  1686  	if p.PadLength != 0 {
  1687  		flags |= http2FlagPushPromisePadded
  1688  	}
  1689  	if p.EndHeaders {
  1690  		flags |= http2FlagPushPromiseEndHeaders
  1691  	}
  1692  	f.startWrite(http2FramePushPromise, flags, p.StreamID)
  1693  	if p.PadLength != 0 {
  1694  		f.writeByte(p.PadLength)
  1695  	}
  1696  	if !http2validStreamID(p.PromiseID) && !f.AllowIllegalWrites {
  1697  		return http2errStreamID
  1698  	}
  1699  	f.writeUint32(p.PromiseID)
  1700  	f.wbuf = append(f.wbuf, p.BlockFragment...)
  1701  	f.wbuf = append(f.wbuf, http2padZeros[:p.PadLength]...)
  1702  	return f.endWrite()
  1703  }
  1704  
  1705  // WriteRawFrame writes a raw frame. This can be used to write
  1706  // extension frames unknown to this package.
  1707  func (f *http2Framer) WriteRawFrame(t http2FrameType, flags http2Flags, streamID uint32, payload []byte) error {
  1708  	f.startWrite(t, flags, streamID)
  1709  	f.writeBytes(payload)
  1710  	return f.endWrite()
  1711  }
  1712  
  1713  func http2readByte(p []byte) (remain []byte, b byte, err error) {
  1714  	if len(p) == 0 {
  1715  		return nil, 0, io.ErrUnexpectedEOF
  1716  	}
  1717  	return p[1:], p[0], nil
  1718  }
  1719  
  1720  func http2readUint32(p []byte) (remain []byte, v uint32, err error) {
  1721  	if len(p) < 4 {
  1722  		return nil, 0, io.ErrUnexpectedEOF
  1723  	}
  1724  	return p[4:], binary.BigEndian.Uint32(p[:4]), nil
  1725  }
  1726  
  1727  type http2streamEnder interface {
  1728  	StreamEnded() bool
  1729  }
  1730  
  1731  type http2headersEnder interface {
  1732  	HeadersEnded() bool
  1733  }
  1734  
  1735  type http2headersOrContinuation interface {
  1736  	http2headersEnder
  1737  	HeaderBlockFragment() []byte
  1738  }
  1739  
  1740  // A MetaHeadersFrame is the representation of one HEADERS frame and
  1741  // zero or more contiguous CONTINUATION frames and the decoding of
  1742  // their HPACK-encoded contents.
  1743  //
  1744  // This type of frame does not appear on the wire and is only returned
  1745  // by the Framer when Framer.ReadMetaHeaders is set.
  1746  type http2MetaHeadersFrame struct {
  1747  	*http2HeadersFrame
  1748  
  1749  	// Fields are the fields contained in the HEADERS and
  1750  	// CONTINUATION frames. The underlying slice is owned by the
  1751  	// Framer and must not be retained after the next call to
  1752  	// ReadFrame.
  1753  	//
  1754  	// Fields are guaranteed to be in the correct http2 order and
  1755  	// not have unknown pseudo header fields or invalid header
  1756  	// field names or values. Required pseudo header fields may be
  1757  	// missing, however. Use the MetaHeadersFrame.Pseudo accessor
  1758  	// method access pseudo headers.
  1759  	Fields []hpack.HeaderField
  1760  
  1761  	// Truncated is whether the max header list size limit was hit
  1762  	// and Fields is incomplete. The hpack decoder state is still
  1763  	// valid, however.
  1764  	Truncated bool
  1765  }
  1766  
  1767  // PseudoValue returns the given pseudo header field's value.
  1768  // The provided pseudo field should not contain the leading colon.
  1769  func (mh *http2MetaHeadersFrame) PseudoValue(pseudo string) string {
  1770  	for _, hf := range mh.Fields {
  1771  		if !hf.IsPseudo() {
  1772  			return ""
  1773  		}
  1774  		if hf.Name[1:] == pseudo {
  1775  			return hf.Value
  1776  		}
  1777  	}
  1778  	return ""
  1779  }
  1780  
  1781  // RegularFields returns the regular (non-pseudo) header fields of mh.
  1782  // The caller does not own the returned slice.
  1783  func (mh *http2MetaHeadersFrame) RegularFields() []hpack.HeaderField {
  1784  	for i, hf := range mh.Fields {
  1785  		if !hf.IsPseudo() {
  1786  			return mh.Fields[i:]
  1787  		}
  1788  	}
  1789  	return nil
  1790  }
  1791  
  1792  // PseudoFields returns the pseudo header fields of mh.
  1793  // The caller does not own the returned slice.
  1794  func (mh *http2MetaHeadersFrame) PseudoFields() []hpack.HeaderField {
  1795  	for i, hf := range mh.Fields {
  1796  		if !hf.IsPseudo() {
  1797  			return mh.Fields[:i]
  1798  		}
  1799  	}
  1800  	return mh.Fields
  1801  }
  1802  
  1803  func (mh *http2MetaHeadersFrame) checkPseudos() error {
  1804  	var isRequest, isResponse bool
  1805  	pf := mh.PseudoFields()
  1806  	for i, hf := range pf {
  1807  		switch hf.Name {
  1808  		case ":method", ":path", ":scheme", ":authority":
  1809  			isRequest = true
  1810  		case ":status":
  1811  			isResponse = true
  1812  		default:
  1813  			return http2pseudoHeaderError(hf.Name)
  1814  		}
  1815  
  1816  		for _, hf2 := range pf[:i] {
  1817  			if hf.Name == hf2.Name {
  1818  				return http2duplicatePseudoHeaderError(hf.Name)
  1819  			}
  1820  		}
  1821  	}
  1822  	if isRequest && isResponse {
  1823  		return http2errMixPseudoHeaderTypes
  1824  	}
  1825  	return nil
  1826  }
  1827  
  1828  func (fr *http2Framer) maxHeaderStringLen() int {
  1829  	v := fr.maxHeaderListSize()
  1830  	if uint32(int(v)) == v {
  1831  		return int(v)
  1832  	}
  1833  
  1834  	return 0
  1835  }
  1836  
  1837  // readMetaFrame returns 0 or more CONTINUATION frames from fr and
  1838  // merge them into into the provided hf and returns a MetaHeadersFrame
  1839  // with the decoded hpack values.
  1840  func (fr *http2Framer) readMetaFrame(hf *http2HeadersFrame) (*http2MetaHeadersFrame, error) {
  1841  	if fr.AllowIllegalReads {
  1842  		return nil, errors.New("illegal use of AllowIllegalReads with ReadMetaHeaders")
  1843  	}
  1844  	mh := &http2MetaHeadersFrame{
  1845  		http2HeadersFrame: hf,
  1846  	}
  1847  	var remainSize = fr.maxHeaderListSize()
  1848  	var sawRegular bool
  1849  
  1850  	var invalid error // pseudo header field errors
  1851  	hdec := fr.ReadMetaHeaders
  1852  	hdec.SetEmitEnabled(true)
  1853  	hdec.SetMaxStringLength(fr.maxHeaderStringLen())
  1854  	hdec.SetEmitFunc(func(hf hpack.HeaderField) {
  1855  		if !http2validHeaderFieldValue(hf.Value) {
  1856  			invalid = http2headerFieldValueError(hf.Value)
  1857  		}
  1858  		isPseudo := strings.HasPrefix(hf.Name, ":")
  1859  		if isPseudo {
  1860  			if sawRegular {
  1861  				invalid = http2errPseudoAfterRegular
  1862  			}
  1863  		} else {
  1864  			sawRegular = true
  1865  			if !http2validHeaderFieldName(hf.Name) {
  1866  				invalid = http2headerFieldNameError(hf.Name)
  1867  			}
  1868  		}
  1869  
  1870  		if invalid != nil {
  1871  			hdec.SetEmitEnabled(false)
  1872  			return
  1873  		}
  1874  
  1875  		size := hf.Size()
  1876  		if size > remainSize {
  1877  			hdec.SetEmitEnabled(false)
  1878  			mh.Truncated = true
  1879  			return
  1880  		}
  1881  		remainSize -= size
  1882  
  1883  		mh.Fields = append(mh.Fields, hf)
  1884  	})
  1885  
  1886  	defer hdec.SetEmitFunc(func(hf hpack.HeaderField) {})
  1887  
  1888  	var hc http2headersOrContinuation = hf
  1889  	for {
  1890  		frag := hc.HeaderBlockFragment()
  1891  		if _, err := hdec.Write(frag); err != nil {
  1892  			return nil, http2ConnectionError(http2ErrCodeCompression)
  1893  		}
  1894  
  1895  		if hc.HeadersEnded() {
  1896  			break
  1897  		}
  1898  		if f, err := fr.ReadFrame(); err != nil {
  1899  			return nil, err
  1900  		} else {
  1901  			hc = f.(*http2ContinuationFrame)
  1902  		}
  1903  	}
  1904  
  1905  	mh.http2HeadersFrame.headerFragBuf = nil
  1906  	mh.http2HeadersFrame.invalidate()
  1907  
  1908  	if err := hdec.Close(); err != nil {
  1909  		return nil, http2ConnectionError(http2ErrCodeCompression)
  1910  	}
  1911  	if invalid != nil {
  1912  		fr.errDetail = invalid
  1913  		return nil, http2StreamError{mh.StreamID, http2ErrCodeProtocol}
  1914  	}
  1915  	if err := mh.checkPseudos(); err != nil {
  1916  		fr.errDetail = err
  1917  		return nil, http2StreamError{mh.StreamID, http2ErrCodeProtocol}
  1918  	}
  1919  	return mh, nil
  1920  }
  1921  
  1922  func http2summarizeFrame(f http2Frame) string {
  1923  	var buf bytes.Buffer
  1924  	f.Header().writeDebug(&buf)
  1925  	switch f := f.(type) {
  1926  	case *http2SettingsFrame:
  1927  		n := 0
  1928  		f.ForeachSetting(func(s http2Setting) error {
  1929  			n++
  1930  			if n == 1 {
  1931  				buf.WriteString(", settings:")
  1932  			}
  1933  			fmt.Fprintf(&buf, " %v=%v,", s.ID, s.Val)
  1934  			return nil
  1935  		})
  1936  		if n > 0 {
  1937  			buf.Truncate(buf.Len() - 1)
  1938  		}
  1939  	case *http2DataFrame:
  1940  		data := f.Data()
  1941  		const max = 256
  1942  		if len(data) > max {
  1943  			data = data[:max]
  1944  		}
  1945  		fmt.Fprintf(&buf, " data=%q", data)
  1946  		if len(f.Data()) > max {
  1947  			fmt.Fprintf(&buf, " (%d bytes omitted)", len(f.Data())-max)
  1948  		}
  1949  	case *http2WindowUpdateFrame:
  1950  		if f.StreamID == 0 {
  1951  			buf.WriteString(" (conn)")
  1952  		}
  1953  		fmt.Fprintf(&buf, " incr=%v", f.Increment)
  1954  	case *http2PingFrame:
  1955  		fmt.Fprintf(&buf, " ping=%q", f.Data[:])
  1956  	case *http2GoAwayFrame:
  1957  		fmt.Fprintf(&buf, " LastStreamID=%v ErrCode=%v Debug=%q",
  1958  			f.LastStreamID, f.ErrCode, f.debugData)
  1959  	case *http2RSTStreamFrame:
  1960  		fmt.Fprintf(&buf, " ErrCode=%v", f.ErrCode)
  1961  	}
  1962  	return buf.String()
  1963  }
  1964  
  1965  func http2requestCancel(req *Request) <-chan struct{} { return req.Cancel }
  1966  
  1967  var http2DebugGoroutines = os.Getenv("DEBUG_HTTP2_GOROUTINES") == "1"
  1968  
  1969  type http2goroutineLock uint64
  1970  
  1971  func http2newGoroutineLock() http2goroutineLock {
  1972  	if !http2DebugGoroutines {
  1973  		return 0
  1974  	}
  1975  	return http2goroutineLock(http2curGoroutineID())
  1976  }
  1977  
  1978  func (g http2goroutineLock) check() {
  1979  	if !http2DebugGoroutines {
  1980  		return
  1981  	}
  1982  	if http2curGoroutineID() != uint64(g) {
  1983  		panic("running on the wrong goroutine")
  1984  	}
  1985  }
  1986  
  1987  func (g http2goroutineLock) checkNotOn() {
  1988  	if !http2DebugGoroutines {
  1989  		return
  1990  	}
  1991  	if http2curGoroutineID() == uint64(g) {
  1992  		panic("running on the wrong goroutine")
  1993  	}
  1994  }
  1995  
  1996  var http2goroutineSpace = []byte("goroutine ")
  1997  
  1998  func http2curGoroutineID() uint64 {
  1999  	bp := http2littleBuf.Get().(*[]byte)
  2000  	defer http2littleBuf.Put(bp)
  2001  	b := *bp
  2002  	b = b[:runtime.Stack(b, false)]
  2003  
  2004  	b = bytes.TrimPrefix(b, http2goroutineSpace)
  2005  	i := bytes.IndexByte(b, ' ')
  2006  	if i < 0 {
  2007  		panic(fmt.Sprintf("No space found in %q", b))
  2008  	}
  2009  	b = b[:i]
  2010  	n, err := http2parseUintBytes(b, 10, 64)
  2011  	if err != nil {
  2012  		panic(fmt.Sprintf("Failed to parse goroutine ID out of %q: %v", b, err))
  2013  	}
  2014  	return n
  2015  }
  2016  
  2017  var http2littleBuf = sync.Pool{
  2018  	New: func() interface{} {
  2019  		buf := make([]byte, 64)
  2020  		return &buf
  2021  	},
  2022  }
  2023  
  2024  // parseUintBytes is like strconv.ParseUint, but using a []byte.
  2025  func http2parseUintBytes(s []byte, base int, bitSize int) (n uint64, err error) {
  2026  	var cutoff, maxVal uint64
  2027  
  2028  	if bitSize == 0 {
  2029  		bitSize = int(strconv.IntSize)
  2030  	}
  2031  
  2032  	s0 := s
  2033  	switch {
  2034  	case len(s) < 1:
  2035  		err = strconv.ErrSyntax
  2036  		goto Error
  2037  
  2038  	case 2 <= base && base <= 36:
  2039  
  2040  	case base == 0:
  2041  
  2042  		switch {
  2043  		case s[0] == '0' && len(s) > 1 && (s[1] == 'x' || s[1] == 'X'):
  2044  			base = 16
  2045  			s = s[2:]
  2046  			if len(s) < 1 {
  2047  				err = strconv.ErrSyntax
  2048  				goto Error
  2049  			}
  2050  		case s[0] == '0':
  2051  			base = 8
  2052  		default:
  2053  			base = 10
  2054  		}
  2055  
  2056  	default:
  2057  		err = errors.New("invalid base " + strconv.Itoa(base))
  2058  		goto Error
  2059  	}
  2060  
  2061  	n = 0
  2062  	cutoff = http2cutoff64(base)
  2063  	maxVal = 1<<uint(bitSize) - 1
  2064  
  2065  	for i := 0; i < len(s); i++ {
  2066  		var v byte
  2067  		d := s[i]
  2068  		switch {
  2069  		case '0' <= d && d <= '9':
  2070  			v = d - '0'
  2071  		case 'a' <= d && d <= 'z':
  2072  			v = d - 'a' + 10
  2073  		case 'A' <= d && d <= 'Z':
  2074  			v = d - 'A' + 10
  2075  		default:
  2076  			n = 0
  2077  			err = strconv.ErrSyntax
  2078  			goto Error
  2079  		}
  2080  		if int(v) >= base {
  2081  			n = 0
  2082  			err = strconv.ErrSyntax
  2083  			goto Error
  2084  		}
  2085  
  2086  		if n >= cutoff {
  2087  
  2088  			n = 1<<64 - 1
  2089  			err = strconv.ErrRange
  2090  			goto Error
  2091  		}
  2092  		n *= uint64(base)
  2093  
  2094  		n1 := n + uint64(v)
  2095  		if n1 < n || n1 > maxVal {
  2096  
  2097  			n = 1<<64 - 1
  2098  			err = strconv.ErrRange
  2099  			goto Error
  2100  		}
  2101  		n = n1
  2102  	}
  2103  
  2104  	return n, nil
  2105  
  2106  Error:
  2107  	return n, &strconv.NumError{Func: "ParseUint", Num: string(s0), Err: err}
  2108  }
  2109  
  2110  // Return the first number n such that n*base >= 1<<64.
  2111  func http2cutoff64(base int) uint64 {
  2112  	if base < 2 {
  2113  		return 0
  2114  	}
  2115  	return (1<<64-1)/uint64(base) + 1
  2116  }
  2117  
  2118  var (
  2119  	http2commonLowerHeader = map[string]string{} // Go-Canonical-Case -> lower-case
  2120  	http2commonCanonHeader = map[string]string{} // lower-case -> Go-Canonical-Case
  2121  )
  2122  
  2123  func init() {
  2124  	for _, v := range []string{
  2125  		"accept",
  2126  		"accept-charset",
  2127  		"accept-encoding",
  2128  		"accept-language",
  2129  		"accept-ranges",
  2130  		"age",
  2131  		"access-control-allow-origin",
  2132  		"allow",
  2133  		"authorization",
  2134  		"cache-control",
  2135  		"content-disposition",
  2136  		"content-encoding",
  2137  		"content-language",
  2138  		"content-length",
  2139  		"content-location",
  2140  		"content-range",
  2141  		"content-type",
  2142  		"cookie",
  2143  		"date",
  2144  		"etag",
  2145  		"expect",
  2146  		"expires",
  2147  		"from",
  2148  		"host",
  2149  		"if-match",
  2150  		"if-modified-since",
  2151  		"if-none-match",
  2152  		"if-unmodified-since",
  2153  		"last-modified",
  2154  		"link",
  2155  		"location",
  2156  		"max-forwards",
  2157  		"proxy-authenticate",
  2158  		"proxy-authorization",
  2159  		"range",
  2160  		"referer",
  2161  		"refresh",
  2162  		"retry-after",
  2163  		"server",
  2164  		"set-cookie",
  2165  		"strict-transport-security",
  2166  		"trailer",
  2167  		"transfer-encoding",
  2168  		"user-agent",
  2169  		"vary",
  2170  		"via",
  2171  		"www-authenticate",
  2172  	} {
  2173  		chk := CanonicalHeaderKey(v)
  2174  		http2commonLowerHeader[chk] = v
  2175  		http2commonCanonHeader[v] = chk
  2176  	}
  2177  }
  2178  
  2179  func http2lowerHeader(v string) string {
  2180  	if s, ok := http2commonLowerHeader[v]; ok {
  2181  		return s
  2182  	}
  2183  	return strings.ToLower(v)
  2184  }
  2185  
  2186  var (
  2187  	http2VerboseLogs    bool
  2188  	http2logFrameWrites bool
  2189  	http2logFrameReads  bool
  2190  )
  2191  
  2192  func init() {
  2193  	e := os.Getenv("GODEBUG")
  2194  	if strings.Contains(e, "http2debug=1") {
  2195  		http2VerboseLogs = true
  2196  	}
  2197  	if strings.Contains(e, "http2debug=2") {
  2198  		http2VerboseLogs = true
  2199  		http2logFrameWrites = true
  2200  		http2logFrameReads = true
  2201  	}
  2202  }
  2203  
  2204  const (
  2205  	// ClientPreface is the string that must be sent by new
  2206  	// connections from clients.
  2207  	http2ClientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
  2208  
  2209  	// SETTINGS_MAX_FRAME_SIZE default
  2210  	// http://http2.github.io/http2-spec/#rfc.section.6.5.2
  2211  	http2initialMaxFrameSize = 16384
  2212  
  2213  	// NextProtoTLS is the NPN/ALPN protocol negotiated during
  2214  	// HTTP/2's TLS setup.
  2215  	http2NextProtoTLS = "h2"
  2216  
  2217  	// http://http2.github.io/http2-spec/#SettingValues
  2218  	http2initialHeaderTableSize = 4096
  2219  
  2220  	http2initialWindowSize = 65535 // 6.9.2 Initial Flow Control Window Size
  2221  
  2222  	http2defaultMaxReadFrameSize = 1 << 20
  2223  )
  2224  
  2225  var (
  2226  	http2clientPreface = []byte(http2ClientPreface)
  2227  )
  2228  
  2229  type http2streamState int
  2230  
  2231  const (
  2232  	http2stateIdle http2streamState = iota
  2233  	http2stateOpen
  2234  	http2stateHalfClosedLocal
  2235  	http2stateHalfClosedRemote
  2236  	http2stateResvLocal
  2237  	http2stateResvRemote
  2238  	http2stateClosed
  2239  )
  2240  
  2241  var http2stateName = [...]string{
  2242  	http2stateIdle:             "Idle",
  2243  	http2stateOpen:             "Open",
  2244  	http2stateHalfClosedLocal:  "HalfClosedLocal",
  2245  	http2stateHalfClosedRemote: "HalfClosedRemote",
  2246  	http2stateResvLocal:        "ResvLocal",
  2247  	http2stateResvRemote:       "ResvRemote",
  2248  	http2stateClosed:           "Closed",
  2249  }
  2250  
  2251  func (st http2streamState) String() string {
  2252  	return http2stateName[st]
  2253  }
  2254  
  2255  // Setting is a setting parameter: which setting it is, and its value.
  2256  type http2Setting struct {
  2257  	// ID is which setting is being set.
  2258  	// See http://http2.github.io/http2-spec/#SettingValues
  2259  	ID http2SettingID
  2260  
  2261  	// Val is the value.
  2262  	Val uint32
  2263  }
  2264  
  2265  func (s http2Setting) String() string {
  2266  	return fmt.Sprintf("[%v = %d]", s.ID, s.Val)
  2267  }
  2268  
  2269  // Valid reports whether the setting is valid.
  2270  func (s http2Setting) Valid() error {
  2271  
  2272  	switch s.ID {
  2273  	case http2SettingEnablePush:
  2274  		if s.Val != 1 && s.Val != 0 {
  2275  			return http2ConnectionError(http2ErrCodeProtocol)
  2276  		}
  2277  	case http2SettingInitialWindowSize:
  2278  		if s.Val > 1<<31-1 {
  2279  			return http2ConnectionError(http2ErrCodeFlowControl)
  2280  		}
  2281  	case http2SettingMaxFrameSize:
  2282  		if s.Val < 16384 || s.Val > 1<<24-1 {
  2283  			return http2ConnectionError(http2ErrCodeProtocol)
  2284  		}
  2285  	}
  2286  	return nil
  2287  }
  2288  
  2289  // A SettingID is an HTTP/2 setting as defined in
  2290  // http://http2.github.io/http2-spec/#iana-settings
  2291  type http2SettingID uint16
  2292  
  2293  const (
  2294  	http2SettingHeaderTableSize      http2SettingID = 0x1
  2295  	http2SettingEnablePush           http2SettingID = 0x2
  2296  	http2SettingMaxConcurrentStreams http2SettingID = 0x3
  2297  	http2SettingInitialWindowSize    http2SettingID = 0x4
  2298  	http2SettingMaxFrameSize         http2SettingID = 0x5
  2299  	http2SettingMaxHeaderListSize    http2SettingID = 0x6
  2300  )
  2301  
  2302  var http2settingName = map[http2SettingID]string{
  2303  	http2SettingHeaderTableSize:      "HEADER_TABLE_SIZE",
  2304  	http2SettingEnablePush:           "ENABLE_PUSH",
  2305  	http2SettingMaxConcurrentStreams: "MAX_CONCURRENT_STREAMS",
  2306  	http2SettingInitialWindowSize:    "INITIAL_WINDOW_SIZE",
  2307  	http2SettingMaxFrameSize:         "MAX_FRAME_SIZE",
  2308  	http2SettingMaxHeaderListSize:    "MAX_HEADER_LIST_SIZE",
  2309  }
  2310  
  2311  func (s http2SettingID) String() string {
  2312  	if v, ok := http2settingName[s]; ok {
  2313  		return v
  2314  	}
  2315  	return fmt.Sprintf("UNKNOWN_SETTING_%d", uint16(s))
  2316  }
  2317  
  2318  var (
  2319  	http2errInvalidHeaderFieldName  = errors.New("http2: invalid header field name")
  2320  	http2errInvalidHeaderFieldValue = errors.New("http2: invalid header field value")
  2321  )
  2322  
  2323  // validHeaderFieldName reports whether v is a valid header field name (key).
  2324  //  RFC 7230 says:
  2325  //   header-field   = field-name ":" OWS field-value OWS
  2326  //   field-name     = token
  2327  //   token          = 1*tchar
  2328  //   tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
  2329  //           "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
  2330  // Further, http2 says:
  2331  //   "Just as in HTTP/1.x, header field names are strings of ASCII
  2332  //   characters that are compared in a case-insensitive
  2333  //   fashion. However, header field names MUST be converted to
  2334  //   lowercase prior to their encoding in HTTP/2. "
  2335  func http2validHeaderFieldName(v string) bool {
  2336  	if len(v) == 0 {
  2337  		return false
  2338  	}
  2339  	for _, r := range v {
  2340  		if int(r) >= len(http2isTokenTable) || ('A' <= r && r <= 'Z') {
  2341  			return false
  2342  		}
  2343  		if !http2isTokenTable[byte(r)] {
  2344  			return false
  2345  		}
  2346  	}
  2347  	return true
  2348  }
  2349  
  2350  // validHeaderFieldValue reports whether v is a valid header field value.
  2351  //
  2352  // RFC 7230 says:
  2353  //  field-value    = *( field-content / obs-fold )
  2354  //  obj-fold       =  N/A to http2, and deprecated
  2355  //  field-content  = field-vchar [ 1*( SP / HTAB ) field-vchar ]
  2356  //  field-vchar    = VCHAR / obs-text
  2357  //  obs-text       = %x80-FF
  2358  //  VCHAR          = "any visible [USASCII] character"
  2359  //
  2360  // http2 further says: "Similarly, HTTP/2 allows header field values
  2361  // that are not valid. While most of the values that can be encoded
  2362  // will not alter header field parsing, carriage return (CR, ASCII
  2363  // 0xd), line feed (LF, ASCII 0xa), and the zero character (NUL, ASCII
  2364  // 0x0) might be exploited by an attacker if they are translated
  2365  // verbatim. Any request or response that contains a character not
  2366  // permitted in a header field value MUST be treated as malformed
  2367  // (Section 8.1.2.6). Valid characters are defined by the
  2368  // field-content ABNF rule in Section 3.2 of [RFC7230]."
  2369  //
  2370  // This function does not (yet?) properly handle the rejection of
  2371  // strings that begin or end with SP or HTAB.
  2372  func http2validHeaderFieldValue(v string) bool {
  2373  	for i := 0; i < len(v); i++ {
  2374  		if b := v[i]; b < ' ' && b != '\t' || b == 0x7f {
  2375  			return false
  2376  		}
  2377  	}
  2378  	return true
  2379  }
  2380  
  2381  var http2httpCodeStringCommon = map[int]string{} // n -> strconv.Itoa(n)
  2382  
  2383  func init() {
  2384  	for i := 100; i <= 999; i++ {
  2385  		if v := StatusText(i); v != "" {
  2386  			http2httpCodeStringCommon[i] = strconv.Itoa(i)
  2387  		}
  2388  	}
  2389  }
  2390  
  2391  func http2httpCodeString(code int) string {
  2392  	if s, ok := http2httpCodeStringCommon[code]; ok {
  2393  		return s
  2394  	}
  2395  	return strconv.Itoa(code)
  2396  }
  2397  
  2398  // from pkg io
  2399  type http2stringWriter interface {
  2400  	WriteString(s string) (n int, err error)
  2401  }
  2402  
  2403  // A gate lets two goroutines coordinate their activities.
  2404  type http2gate chan struct{}
  2405  
  2406  func (g http2gate) Done() { g <- struct{}{} }
  2407  
  2408  func (g http2gate) Wait() { <-g }
  2409  
  2410  // A closeWaiter is like a sync.WaitGroup but only goes 1 to 0 (open to closed).
  2411  type http2closeWaiter chan struct{}
  2412  
  2413  // Init makes a closeWaiter usable.
  2414  // It exists because so a closeWaiter value can be placed inside a
  2415  // larger struct and have the Mutex and Cond's memory in the same
  2416  // allocation.
  2417  func (cw *http2closeWaiter) Init() {
  2418  	*cw = make(chan struct{})
  2419  }
  2420  
  2421  // Close marks the closeWaiter as closed and unblocks any waiters.
  2422  func (cw http2closeWaiter) Close() {
  2423  	close(cw)
  2424  }
  2425  
  2426  // Wait waits for the closeWaiter to become closed.
  2427  func (cw http2closeWaiter) Wait() {
  2428  	<-cw
  2429  }
  2430  
  2431  // bufferedWriter is a buffered writer that writes to w.
  2432  // Its buffered writer is lazily allocated as needed, to minimize
  2433  // idle memory usage with many connections.
  2434  type http2bufferedWriter struct {
  2435  	w  io.Writer     // immutable
  2436  	bw *bufio.Writer // non-nil when data is buffered
  2437  }
  2438  
  2439  func http2newBufferedWriter(w io.Writer) *http2bufferedWriter {
  2440  	return &http2bufferedWriter{w: w}
  2441  }
  2442  
  2443  var http2bufWriterPool = sync.Pool{
  2444  	New: func() interface{} {
  2445  
  2446  		return bufio.NewWriterSize(nil, 4<<10)
  2447  	},
  2448  }
  2449  
  2450  func (w *http2bufferedWriter) Write(p []byte) (n int, err error) {
  2451  	if w.bw == nil {
  2452  		bw := http2bufWriterPool.Get().(*bufio.Writer)
  2453  		bw.Reset(w.w)
  2454  		w.bw = bw
  2455  	}
  2456  	return w.bw.Write(p)
  2457  }
  2458  
  2459  func (w *http2bufferedWriter) Flush() error {
  2460  	bw := w.bw
  2461  	if bw == nil {
  2462  		return nil
  2463  	}
  2464  	err := bw.Flush()
  2465  	bw.Reset(nil)
  2466  	http2bufWriterPool.Put(bw)
  2467  	w.bw = nil
  2468  	return err
  2469  }
  2470  
  2471  func http2mustUint31(v int32) uint32 {
  2472  	if v < 0 || v > 2147483647 {
  2473  		panic("out of range")
  2474  	}
  2475  	return uint32(v)
  2476  }
  2477  
  2478  // bodyAllowedForStatus reports whether a given response status code
  2479  // permits a body. See RFC2616, section 4.4.
  2480  func http2bodyAllowedForStatus(status int) bool {
  2481  	switch {
  2482  	case status >= 100 && status <= 199:
  2483  		return false
  2484  	case status == 204:
  2485  		return false
  2486  	case status == 304:
  2487  		return false
  2488  	}
  2489  	return true
  2490  }
  2491  
  2492  type http2httpError struct {
  2493  	msg     string
  2494  	timeout bool
  2495  }
  2496  
  2497  func (e *http2httpError) Error() string { return e.msg }
  2498  
  2499  func (e *http2httpError) Timeout() bool { return e.timeout }
  2500  
  2501  func (e *http2httpError) Temporary() bool { return true }
  2502  
  2503  var http2errTimeout error = &http2httpError{msg: "http2: timeout awaiting response headers", timeout: true}
  2504  
  2505  var http2isTokenTable = [127]bool{
  2506  	'!':  true,
  2507  	'#':  true,
  2508  	'$':  true,
  2509  	'%':  true,
  2510  	'&':  true,
  2511  	'\'': true,
  2512  	'*':  true,
  2513  	'+':  true,
  2514  	'-':  true,
  2515  	'.':  true,
  2516  	'0':  true,
  2517  	'1':  true,
  2518  	'2':  true,
  2519  	'3':  true,
  2520  	'4':  true,
  2521  	'5':  true,
  2522  	'6':  true,
  2523  	'7':  true,
  2524  	'8':  true,
  2525  	'9':  true,
  2526  	'A':  true,
  2527  	'B':  true,
  2528  	'C':  true,
  2529  	'D':  true,
  2530  	'E':  true,
  2531  	'F':  true,
  2532  	'G':  true,
  2533  	'H':  true,
  2534  	'I':  true,
  2535  	'J':  true,
  2536  	'K':  true,
  2537  	'L':  true,
  2538  	'M':  true,
  2539  	'N':  true,
  2540  	'O':  true,
  2541  	'P':  true,
  2542  	'Q':  true,
  2543  	'R':  true,
  2544  	'S':  true,
  2545  	'T':  true,
  2546  	'U':  true,
  2547  	'W':  true,
  2548  	'V':  true,
  2549  	'X':  true,
  2550  	'Y':  true,
  2551  	'Z':  true,
  2552  	'^':  true,
  2553  	'_':  true,
  2554  	'`':  true,
  2555  	'a':  true,
  2556  	'b':  true,
  2557  	'c':  true,
  2558  	'd':  true,
  2559  	'e':  true,
  2560  	'f':  true,
  2561  	'g':  true,
  2562  	'h':  true,
  2563  	'i':  true,
  2564  	'j':  true,
  2565  	'k':  true,
  2566  	'l':  true,
  2567  	'm':  true,
  2568  	'n':  true,
  2569  	'o':  true,
  2570  	'p':  true,
  2571  	'q':  true,
  2572  	'r':  true,
  2573  	's':  true,
  2574  	't':  true,
  2575  	'u':  true,
  2576  	'v':  true,
  2577  	'w':  true,
  2578  	'x':  true,
  2579  	'y':  true,
  2580  	'z':  true,
  2581  	'|':  true,
  2582  	'~':  true,
  2583  }
  2584  
  2585  type http2connectionStater interface {
  2586  	ConnectionState() tls.ConnectionState
  2587  }
  2588  
  2589  var http2sorterPool = sync.Pool{New: func() interface{} { return new(http2sorter) }}
  2590  
  2591  type http2sorter struct {
  2592  	v []string // owned by sorter
  2593  }
  2594  
  2595  func (s *http2sorter) Len() int { return len(s.v) }
  2596  
  2597  func (s *http2sorter) Swap(i, j int) { s.v[i], s.v[j] = s.v[j], s.v[i] }
  2598  
  2599  func (s *http2sorter) Less(i, j int) bool { return s.v[i] < s.v[j] }
  2600  
  2601  // Keys returns the sorted keys of h.
  2602  //
  2603  // The returned slice is only valid until s used again or returned to
  2604  // its pool.
  2605  func (s *http2sorter) Keys(h Header) []string {
  2606  	keys := s.v[:0]
  2607  	for k := range h {
  2608  		keys = append(keys, k)
  2609  	}
  2610  	s.v = keys
  2611  	sort.Sort(s)
  2612  	return keys
  2613  }
  2614  
  2615  func (s *http2sorter) SortStrings(ss []string) {
  2616  
  2617  	save := s.v
  2618  	s.v = ss
  2619  	sort.Sort(s)
  2620  	s.v = save
  2621  }
  2622  
  2623  // pipe is a goroutine-safe io.Reader/io.Writer pair.  It's like
  2624  // io.Pipe except there are no PipeReader/PipeWriter halves, and the
  2625  // underlying buffer is an interface. (io.Pipe is always unbuffered)
  2626  type http2pipe struct {
  2627  	mu       sync.Mutex
  2628  	c        sync.Cond // c.L lazily initialized to &p.mu
  2629  	b        http2pipeBuffer
  2630  	err      error         // read error once empty. non-nil means closed.
  2631  	breakErr error         // immediate read error (caller doesn't see rest of b)
  2632  	donec    chan struct{} // closed on error
  2633  	readFn   func()        // optional code to run in Read before error
  2634  }
  2635  
  2636  type http2pipeBuffer interface {
  2637  	Len() int
  2638  	io.Writer
  2639  	io.Reader
  2640  }
  2641  
  2642  // Read waits until data is available and copies bytes
  2643  // from the buffer into p.
  2644  func (p *http2pipe) Read(d []byte) (n int, err error) {
  2645  	p.mu.Lock()
  2646  	defer p.mu.Unlock()
  2647  	if p.c.L == nil {
  2648  		p.c.L = &p.mu
  2649  	}
  2650  	for {
  2651  		if p.breakErr != nil {
  2652  			return 0, p.breakErr
  2653  		}
  2654  		if p.b.Len() > 0 {
  2655  			return p.b.Read(d)
  2656  		}
  2657  		if p.err != nil {
  2658  			if p.readFn != nil {
  2659  				p.readFn()
  2660  				p.readFn = nil
  2661  			}
  2662  			return 0, p.err
  2663  		}
  2664  		p.c.Wait()
  2665  	}
  2666  }
  2667  
  2668  var http2errClosedPipeWrite = errors.New("write on closed buffer")
  2669  
  2670  // Write copies bytes from p into the buffer and wakes a reader.
  2671  // It is an error to write more data than the buffer can hold.
  2672  func (p *http2pipe) Write(d []byte) (n int, err error) {
  2673  	p.mu.Lock()
  2674  	defer p.mu.Unlock()
  2675  	if p.c.L == nil {
  2676  		p.c.L = &p.mu
  2677  	}
  2678  	defer p.c.Signal()
  2679  	if p.err != nil {
  2680  		return 0, http2errClosedPipeWrite
  2681  	}
  2682  	return p.b.Write(d)
  2683  }
  2684  
  2685  // CloseWithError causes the next Read (waking up a current blocked
  2686  // Read if needed) to return the provided err after all data has been
  2687  // read.
  2688  //
  2689  // The error must be non-nil.
  2690  func (p *http2pipe) CloseWithError(err error) { p.closeWithError(&p.err, err, nil) }
  2691  
  2692  // BreakWithError causes the next Read (waking up a current blocked
  2693  // Read if needed) to return the provided err immediately, without
  2694  // waiting for unread data.
  2695  func (p *http2pipe) BreakWithError(err error) { p.closeWithError(&p.breakErr, err, nil) }
  2696  
  2697  // closeWithErrorAndCode is like CloseWithError but also sets some code to run
  2698  // in the caller's goroutine before returning the error.
  2699  func (p *http2pipe) closeWithErrorAndCode(err error, fn func()) { p.closeWithError(&p.err, err, fn) }
  2700  
  2701  func (p *http2pipe) closeWithError(dst *error, err error, fn func()) {
  2702  	if err == nil {
  2703  		panic("err must be non-nil")
  2704  	}
  2705  	p.mu.Lock()
  2706  	defer p.mu.Unlock()
  2707  	if p.c.L == nil {
  2708  		p.c.L = &p.mu
  2709  	}
  2710  	defer p.c.Signal()
  2711  	if *dst != nil {
  2712  
  2713  		return
  2714  	}
  2715  	p.readFn = fn
  2716  	*dst = err
  2717  	p.closeDoneLocked()
  2718  }
  2719  
  2720  // requires p.mu be held.
  2721  func (p *http2pipe) closeDoneLocked() {
  2722  	if p.donec == nil {
  2723  		return
  2724  	}
  2725  
  2726  	select {
  2727  	case <-p.donec:
  2728  	default:
  2729  		close(p.donec)
  2730  	}
  2731  }
  2732  
  2733  // Err returns the error (if any) first set by BreakWithError or CloseWithError.
  2734  func (p *http2pipe) Err() error {
  2735  	p.mu.Lock()
  2736  	defer p.mu.Unlock()
  2737  	if p.breakErr != nil {
  2738  		return p.breakErr
  2739  	}
  2740  	return p.err
  2741  }
  2742  
  2743  // Done returns a channel which is closed if and when this pipe is closed
  2744  // with CloseWithError.
  2745  func (p *http2pipe) Done() <-chan struct{} {
  2746  	p.mu.Lock()
  2747  	defer p.mu.Unlock()
  2748  	if p.donec == nil {
  2749  		p.donec = make(chan struct{})
  2750  		if p.err != nil || p.breakErr != nil {
  2751  
  2752  			p.closeDoneLocked()
  2753  		}
  2754  	}
  2755  	return p.donec
  2756  }
  2757  
  2758  const (
  2759  	http2prefaceTimeout        = 10 * time.Second
  2760  	http2firstSettingsTimeout  = 2 * time.Second // should be in-flight with preface anyway
  2761  	http2handlerChunkWriteSize = 4 << 10
  2762  	http2defaultMaxStreams     = 250 // TODO: make this 100 as the GFE seems to?
  2763  )
  2764  
  2765  var (
  2766  	http2errClientDisconnected = errors.New("client disconnected")
  2767  	http2errClosedBody         = errors.New("body closed by handler")
  2768  	http2errHandlerComplete    = errors.New("http2: request body closed due to handler exiting")
  2769  	http2errStreamClosed       = errors.New("http2: stream closed")
  2770  )
  2771  
  2772  var http2responseWriterStatePool = sync.Pool{
  2773  	New: func() interface{} {
  2774  		rws := &http2responseWriterState{}
  2775  		rws.bw = bufio.NewWriterSize(http2chunkWriter{rws}, http2handlerChunkWriteSize)
  2776  		return rws
  2777  	},
  2778  }
  2779  
  2780  // Test hooks.
  2781  var (
  2782  	http2testHookOnConn        func()
  2783  	http2testHookGetServerConn func(*http2serverConn)
  2784  	http2testHookOnPanicMu     *sync.Mutex // nil except in tests
  2785  	http2testHookOnPanic       func(sc *http2serverConn, panicVal interface{}) (rePanic bool)
  2786  )
  2787  
  2788  // Server is an HTTP/2 server.
  2789  type http2Server struct {
  2790  	// MaxHandlers limits the number of http.Handler ServeHTTP goroutines
  2791  	// which may run at a time over all connections.
  2792  	// Negative or zero no limit.
  2793  	// TODO: implement
  2794  	MaxHandlers int
  2795  
  2796  	// MaxConcurrentStreams optionally specifies the number of
  2797  	// concurrent streams that each client may have open at a
  2798  	// time. This is unrelated to the number of http.Handler goroutines
  2799  	// which may be active globally, which is MaxHandlers.
  2800  	// If zero, MaxConcurrentStreams defaults to at least 100, per
  2801  	// the HTTP/2 spec's recommendations.
  2802  	MaxConcurrentStreams uint32
  2803  
  2804  	// MaxReadFrameSize optionally specifies the largest frame
  2805  	// this server is willing to read. A valid value is between
  2806  	// 16k and 16M, inclusive. If zero or otherwise invalid, a
  2807  	// default value is used.
  2808  	MaxReadFrameSize uint32
  2809  
  2810  	// PermitProhibitedCipherSuites, if true, permits the use of
  2811  	// cipher suites prohibited by the HTTP/2 spec.
  2812  	PermitProhibitedCipherSuites bool
  2813  }
  2814  
  2815  func (s *http2Server) maxReadFrameSize() uint32 {
  2816  	if v := s.MaxReadFrameSize; v >= http2minMaxFrameSize && v <= http2maxFrameSize {
  2817  		return v
  2818  	}
  2819  	return http2defaultMaxReadFrameSize
  2820  }
  2821  
  2822  func (s *http2Server) maxConcurrentStreams() uint32 {
  2823  	if v := s.MaxConcurrentStreams; v > 0 {
  2824  		return v
  2825  	}
  2826  	return http2defaultMaxStreams
  2827  }
  2828  
  2829  // ConfigureServer adds HTTP/2 support to a net/http Server.
  2830  //
  2831  // The configuration conf may be nil.
  2832  //
  2833  // ConfigureServer must be called before s begins serving.
  2834  func http2ConfigureServer(s *Server, conf *http2Server) error {
  2835  	if conf == nil {
  2836  		conf = new(http2Server)
  2837  	}
  2838  
  2839  	if s.TLSConfig == nil {
  2840  		s.TLSConfig = new(tls.Config)
  2841  	} else if s.TLSConfig.CipherSuites != nil {
  2842  		// If they already provided a CipherSuite list, return
  2843  		// an error if it has a bad order or is missing
  2844  		// ECDHE_RSA_WITH_AES_128_GCM_SHA256.
  2845  		const requiredCipher = tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  2846  		haveRequired := false
  2847  		sawBad := false
  2848  		for i, cs := range s.TLSConfig.CipherSuites {
  2849  			if cs == requiredCipher {
  2850  				haveRequired = true
  2851  			}
  2852  			if http2isBadCipher(cs) {
  2853  				sawBad = true
  2854  			} else if sawBad {
  2855  				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)
  2856  			}
  2857  		}
  2858  		if !haveRequired {
  2859  			return fmt.Errorf("http2: TLSConfig.CipherSuites is missing HTTP/2-required TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256")
  2860  		}
  2861  	}
  2862  
  2863  	s.TLSConfig.PreferServerCipherSuites = true
  2864  
  2865  	haveNPN := false
  2866  	for _, p := range s.TLSConfig.NextProtos {
  2867  		if p == http2NextProtoTLS {
  2868  			haveNPN = true
  2869  			break
  2870  		}
  2871  	}
  2872  	if !haveNPN {
  2873  		s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, http2NextProtoTLS)
  2874  	}
  2875  
  2876  	s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, "h2-14")
  2877  
  2878  	if s.TLSNextProto == nil {
  2879  		s.TLSNextProto = map[string]func(*Server, *tls.Conn, Handler){}
  2880  	}
  2881  	protoHandler := func(hs *Server, c *tls.Conn, h Handler) {
  2882  		if http2testHookOnConn != nil {
  2883  			http2testHookOnConn()
  2884  		}
  2885  		conf.ServeConn(c, &http2ServeConnOpts{
  2886  			Handler:    h,
  2887  			BaseConfig: hs,
  2888  		})
  2889  	}
  2890  	s.TLSNextProto[http2NextProtoTLS] = protoHandler
  2891  	s.TLSNextProto["h2-14"] = protoHandler
  2892  	return nil
  2893  }
  2894  
  2895  // ServeConnOpts are options for the Server.ServeConn method.
  2896  type http2ServeConnOpts struct {
  2897  	// BaseConfig optionally sets the base configuration
  2898  	// for values. If nil, defaults are used.
  2899  	BaseConfig *Server
  2900  
  2901  	// Handler specifies which handler to use for processing
  2902  	// requests. If nil, BaseConfig.Handler is used. If BaseConfig
  2903  	// or BaseConfig.Handler is nil, http.DefaultServeMux is used.
  2904  	Handler Handler
  2905  }
  2906  
  2907  func (o *http2ServeConnOpts) baseConfig() *Server {
  2908  	if o != nil && o.BaseConfig != nil {
  2909  		return o.BaseConfig
  2910  	}
  2911  	return new(Server)
  2912  }
  2913  
  2914  func (o *http2ServeConnOpts) handler() Handler {
  2915  	if o != nil {
  2916  		if o.Handler != nil {
  2917  			return o.Handler
  2918  		}
  2919  		if o.BaseConfig != nil && o.BaseConfig.Handler != nil {
  2920  			return o.BaseConfig.Handler
  2921  		}
  2922  	}
  2923  	return DefaultServeMux
  2924  }
  2925  
  2926  // ServeConn serves HTTP/2 requests on the provided connection and
  2927  // blocks until the connection is no longer readable.
  2928  //
  2929  // ServeConn starts speaking HTTP/2 assuming that c has not had any
  2930  // reads or writes. It writes its initial settings frame and expects
  2931  // to be able to read the preface and settings frame from the
  2932  // client. If c has a ConnectionState method like a *tls.Conn, the
  2933  // ConnectionState is used to verify the TLS ciphersuite and to set
  2934  // the Request.TLS field in Handlers.
  2935  //
  2936  // ServeConn does not support h2c by itself. Any h2c support must be
  2937  // implemented in terms of providing a suitably-behaving net.Conn.
  2938  //
  2939  // The opts parameter is optional. If nil, default values are used.
  2940  func (s *http2Server) ServeConn(c net.Conn, opts *http2ServeConnOpts) {
  2941  	sc := &http2serverConn{
  2942  		srv:              s,
  2943  		hs:               opts.baseConfig(),
  2944  		conn:             c,
  2945  		remoteAddrStr:    c.RemoteAddr().String(),
  2946  		bw:               http2newBufferedWriter(c),
  2947  		handler:          opts.handler(),
  2948  		streams:          make(map[uint32]*http2stream),
  2949  		readFrameCh:      make(chan http2readFrameResult),
  2950  		wantWriteFrameCh: make(chan http2frameWriteMsg, 8),
  2951  		wroteFrameCh:     make(chan http2frameWriteResult, 1),
  2952  		bodyReadCh:       make(chan http2bodyReadMsg),
  2953  		doneServing:      make(chan struct{}),
  2954  		advMaxStreams:    s.maxConcurrentStreams(),
  2955  		writeSched: http2writeScheduler{
  2956  			maxFrameSize: http2initialMaxFrameSize,
  2957  		},
  2958  		initialWindowSize: http2initialWindowSize,
  2959  		headerTableSize:   http2initialHeaderTableSize,
  2960  		serveG:            http2newGoroutineLock(),
  2961  		pushEnabled:       true,
  2962  	}
  2963  	sc.flow.add(http2initialWindowSize)
  2964  	sc.inflow.add(http2initialWindowSize)
  2965  	sc.hpackEncoder = hpack.NewEncoder(&sc.headerWriteBuf)
  2966  
  2967  	fr := http2NewFramer(sc.bw, c)
  2968  	fr.ReadMetaHeaders = hpack.NewDecoder(http2initialHeaderTableSize, nil)
  2969  	fr.MaxHeaderListSize = sc.maxHeaderListSize()
  2970  	fr.SetMaxReadFrameSize(s.maxReadFrameSize())
  2971  	sc.framer = fr
  2972  
  2973  	if tc, ok := c.(http2connectionStater); ok {
  2974  		sc.tlsState = new(tls.ConnectionState)
  2975  		*sc.tlsState = tc.ConnectionState()
  2976  
  2977  		if sc.tlsState.Version < tls.VersionTLS12 {
  2978  			sc.rejectConn(http2ErrCodeInadequateSecurity, "TLS version too low")
  2979  			return
  2980  		}
  2981  
  2982  		if sc.tlsState.ServerName == "" {
  2983  
  2984  		}
  2985  
  2986  		if !s.PermitProhibitedCipherSuites && http2isBadCipher(sc.tlsState.CipherSuite) {
  2987  
  2988  			sc.rejectConn(http2ErrCodeInadequateSecurity, fmt.Sprintf("Prohibited TLS 1.2 Cipher Suite: %x", sc.tlsState.CipherSuite))
  2989  			return
  2990  		}
  2991  	}
  2992  
  2993  	if hook := http2testHookGetServerConn; hook != nil {
  2994  		hook(sc)
  2995  	}
  2996  	sc.serve()
  2997  }
  2998  
  2999  // isBadCipher reports whether the cipher is blacklisted by the HTTP/2 spec.
  3000  func http2isBadCipher(cipher uint16) bool {
  3001  	switch cipher {
  3002  	case tls.TLS_RSA_WITH_RC4_128_SHA,
  3003  		tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
  3004  		tls.TLS_RSA_WITH_AES_128_CBC_SHA,
  3005  		tls.TLS_RSA_WITH_AES_256_CBC_SHA,
  3006  		tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
  3007  		tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  3008  		tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  3009  		tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
  3010  		tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
  3011  		tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  3012  		tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
  3013  
  3014  		return true
  3015  	default:
  3016  		return false
  3017  	}
  3018  }
  3019  
  3020  func (sc *http2serverConn) rejectConn(err http2ErrCode, debug string) {
  3021  	sc.vlogf("http2: server rejecting conn: %v, %s", err, debug)
  3022  
  3023  	sc.framer.WriteGoAway(0, err, []byte(debug))
  3024  	sc.bw.Flush()
  3025  	sc.conn.Close()
  3026  }
  3027  
  3028  type http2serverConn struct {
  3029  	// Immutable:
  3030  	srv              *http2Server
  3031  	hs               *Server
  3032  	conn             net.Conn
  3033  	bw               *http2bufferedWriter // writing to conn
  3034  	handler          Handler
  3035  	framer           *http2Framer
  3036  	doneServing      chan struct{}              // closed when serverConn.serve ends
  3037  	readFrameCh      chan http2readFrameResult  // written by serverConn.readFrames
  3038  	wantWriteFrameCh chan http2frameWriteMsg    // from handlers -> serve
  3039  	wroteFrameCh     chan http2frameWriteResult // from writeFrameAsync -> serve, tickles more frame writes
  3040  	bodyReadCh       chan http2bodyReadMsg      // from handlers -> serve
  3041  	testHookCh       chan func(int)             // code to run on the serve loop
  3042  	flow             http2flow                  // conn-wide (not stream-specific) outbound flow control
  3043  	inflow           http2flow                  // conn-wide inbound flow control
  3044  	tlsState         *tls.ConnectionState       // shared by all handlers, like net/http
  3045  	remoteAddrStr    string
  3046  
  3047  	// Everything following is owned by the serve loop; use serveG.check():
  3048  	serveG                http2goroutineLock // used to verify funcs are on serve()
  3049  	pushEnabled           bool
  3050  	sawFirstSettings      bool // got the initial SETTINGS frame after the preface
  3051  	needToSendSettingsAck bool
  3052  	unackedSettings       int    // how many SETTINGS have we sent without ACKs?
  3053  	clientMaxStreams      uint32 // SETTINGS_MAX_CONCURRENT_STREAMS from client (our PUSH_PROMISE limit)
  3054  	advMaxStreams         uint32 // our SETTINGS_MAX_CONCURRENT_STREAMS advertised the client
  3055  	curOpenStreams        uint32 // client's number of open streams
  3056  	maxStreamID           uint32 // max ever seen
  3057  	streams               map[uint32]*http2stream
  3058  	initialWindowSize     int32
  3059  	headerTableSize       uint32
  3060  	peerMaxHeaderListSize uint32            // zero means unknown (default)
  3061  	canonHeader           map[string]string // http2-lower-case -> Go-Canonical-Case
  3062  	writingFrame          bool              // started write goroutine but haven't heard back on wroteFrameCh
  3063  	needsFrameFlush       bool              // last frame write wasn't a flush
  3064  	writeSched            http2writeScheduler
  3065  	inGoAway              bool // we've started to or sent GOAWAY
  3066  	needToSendGoAway      bool // we need to schedule a GOAWAY frame write
  3067  	goAwayCode            http2ErrCode
  3068  	shutdownTimerCh       <-chan time.Time // nil until used
  3069  	shutdownTimer         *time.Timer      // nil until used
  3070  	freeRequestBodyBuf    []byte           // if non-nil, a free initialWindowSize buffer for getRequestBodyBuf
  3071  
  3072  	// Owned by the writeFrameAsync goroutine:
  3073  	headerWriteBuf bytes.Buffer
  3074  	hpackEncoder   *hpack.Encoder
  3075  }
  3076  
  3077  func (sc *http2serverConn) maxHeaderListSize() uint32 {
  3078  	n := sc.hs.MaxHeaderBytes
  3079  	if n <= 0 {
  3080  		n = DefaultMaxHeaderBytes
  3081  	}
  3082  	// http2's count is in a slightly different unit and includes 32 bytes per pair.
  3083  	// So, take the net/http.Server value and pad it up a bit, assuming 10 headers.
  3084  	const perFieldOverhead = 32 // per http2 spec
  3085  	const typicalHeaders = 10   // conservative
  3086  	return uint32(n + typicalHeaders*perFieldOverhead)
  3087  }
  3088  
  3089  // stream represents a stream. This is the minimal metadata needed by
  3090  // the serve goroutine. Most of the actual stream state is owned by
  3091  // the http.Handler's goroutine in the responseWriter. Because the
  3092  // responseWriter's responseWriterState is recycled at the end of a
  3093  // handler, this struct intentionally has no pointer to the
  3094  // *responseWriter{,State} itself, as the Handler ending nils out the
  3095  // responseWriter's state field.
  3096  type http2stream struct {
  3097  	// immutable:
  3098  	sc   *http2serverConn
  3099  	id   uint32
  3100  	body *http2pipe       // non-nil if expecting DATA frames
  3101  	cw   http2closeWaiter // closed wait stream transitions to closed state
  3102  
  3103  	// owned by serverConn's serve loop:
  3104  	bodyBytes        int64        // body bytes seen so far
  3105  	declBodyBytes    int64        // or -1 if undeclared
  3106  	flow             http2flow    // limits writing from Handler to client
  3107  	inflow           http2flow    // what the client is allowed to POST/etc to us
  3108  	parent           *http2stream // or nil
  3109  	numTrailerValues int64
  3110  	weight           uint8
  3111  	state            http2streamState
  3112  	sentReset        bool // only true once detached from streams map
  3113  	gotReset         bool // only true once detacted from streams map
  3114  	gotTrailerHeader bool // HEADER frame for trailers was seen
  3115  	reqBuf           []byte
  3116  
  3117  	trailer    Header // accumulated trailers
  3118  	reqTrailer Header // handler's Request.Trailer
  3119  }
  3120  
  3121  func (sc *http2serverConn) Framer() *http2Framer { return sc.framer }
  3122  
  3123  func (sc *http2serverConn) CloseConn() error { return sc.conn.Close() }
  3124  
  3125  func (sc *http2serverConn) Flush() error { return sc.bw.Flush() }
  3126  
  3127  func (sc *http2serverConn) HeaderEncoder() (*hpack.Encoder, *bytes.Buffer) {
  3128  	return sc.hpackEncoder, &sc.headerWriteBuf
  3129  }
  3130  
  3131  func (sc *http2serverConn) state(streamID uint32) (http2streamState, *http2stream) {
  3132  	sc.serveG.check()
  3133  
  3134  	if st, ok := sc.streams[streamID]; ok {
  3135  		return st.state, st
  3136  	}
  3137  
  3138  	if streamID <= sc.maxStreamID {
  3139  		return http2stateClosed, nil
  3140  	}
  3141  	return http2stateIdle, nil
  3142  }
  3143  
  3144  // setConnState calls the net/http ConnState hook for this connection, if configured.
  3145  // Note that the net/http package does StateNew and StateClosed for us.
  3146  // There is currently no plan for StateHijacked or hijacking HTTP/2 connections.
  3147  func (sc *http2serverConn) setConnState(state ConnState) {
  3148  	if sc.hs.ConnState != nil {
  3149  		sc.hs.ConnState(sc.conn, state)
  3150  	}
  3151  }
  3152  
  3153  func (sc *http2serverConn) vlogf(format string, args ...interface{}) {
  3154  	if http2VerboseLogs {
  3155  		sc.logf(format, args...)
  3156  	}
  3157  }
  3158  
  3159  func (sc *http2serverConn) logf(format string, args ...interface{}) {
  3160  	if lg := sc.hs.ErrorLog; lg != nil {
  3161  		lg.Printf(format, args...)
  3162  	} else {
  3163  		log.Printf(format, args...)
  3164  	}
  3165  }
  3166  
  3167  // errno returns v's underlying uintptr, else 0.
  3168  //
  3169  // TODO: remove this helper function once http2 can use build
  3170  // tags. See comment in isClosedConnError.
  3171  func http2errno(v error) uintptr {
  3172  	if rv := reflect.ValueOf(v); rv.Kind() == reflect.Uintptr {
  3173  		return uintptr(rv.Uint())
  3174  	}
  3175  	return 0
  3176  }
  3177  
  3178  // isClosedConnError reports whether err is an error from use of a closed
  3179  // network connection.
  3180  func http2isClosedConnError(err error) bool {
  3181  	if err == nil {
  3182  		return false
  3183  	}
  3184  
  3185  	str := err.Error()
  3186  	if strings.Contains(str, "use of closed network connection") {
  3187  		return true
  3188  	}
  3189  
  3190  	if runtime.GOOS == "windows" {
  3191  		if oe, ok := err.(*net.OpError); ok && oe.Op == "read" {
  3192  			if se, ok := oe.Err.(*os.SyscallError); ok && se.Syscall == "wsarecv" {
  3193  				const WSAECONNABORTED = 10053
  3194  				const WSAECONNRESET = 10054
  3195  				if n := http2errno(se.Err); n == WSAECONNRESET || n == WSAECONNABORTED {
  3196  					return true
  3197  				}
  3198  			}
  3199  		}
  3200  	}
  3201  	return false
  3202  }
  3203  
  3204  func (sc *http2serverConn) condlogf(err error, format string, args ...interface{}) {
  3205  	if err == nil {
  3206  		return
  3207  	}
  3208  	if err == io.EOF || err == io.ErrUnexpectedEOF || http2isClosedConnError(err) {
  3209  
  3210  		sc.vlogf(format, args...)
  3211  	} else {
  3212  		sc.logf(format, args...)
  3213  	}
  3214  }
  3215  
  3216  func (sc *http2serverConn) canonicalHeader(v string) string {
  3217  	sc.serveG.check()
  3218  	cv, ok := http2commonCanonHeader[v]
  3219  	if ok {
  3220  		return cv
  3221  	}
  3222  	cv, ok = sc.canonHeader[v]
  3223  	if ok {
  3224  		return cv
  3225  	}
  3226  	if sc.canonHeader == nil {
  3227  		sc.canonHeader = make(map[string]string)
  3228  	}
  3229  	cv = CanonicalHeaderKey(v)
  3230  	sc.canonHeader[v] = cv
  3231  	return cv
  3232  }
  3233  
  3234  type http2readFrameResult struct {
  3235  	f   http2Frame // valid until readMore is called
  3236  	err error
  3237  
  3238  	// readMore should be called once the consumer no longer needs or
  3239  	// retains f. After readMore, f is invalid and more frames can be
  3240  	// read.
  3241  	readMore func()
  3242  }
  3243  
  3244  // readFrames is the loop that reads incoming frames.
  3245  // It takes care to only read one frame at a time, blocking until the
  3246  // consumer is done with the frame.
  3247  // It's run on its own goroutine.
  3248  func (sc *http2serverConn) readFrames() {
  3249  	gate := make(http2gate)
  3250  	gateDone := gate.Done
  3251  	for {
  3252  		f, err := sc.framer.ReadFrame()
  3253  		select {
  3254  		case sc.readFrameCh <- http2readFrameResult{f, err, gateDone}:
  3255  		case <-sc.doneServing:
  3256  			return
  3257  		}
  3258  		select {
  3259  		case <-gate:
  3260  		case <-sc.doneServing:
  3261  			return
  3262  		}
  3263  		if http2terminalReadFrameError(err) {
  3264  			return
  3265  		}
  3266  	}
  3267  }
  3268  
  3269  // frameWriteResult is the message passed from writeFrameAsync to the serve goroutine.
  3270  type http2frameWriteResult struct {
  3271  	wm  http2frameWriteMsg // what was written (or attempted)
  3272  	err error              // result of the writeFrame call
  3273  }
  3274  
  3275  // writeFrameAsync runs in its own goroutine and writes a single frame
  3276  // and then reports when it's done.
  3277  // At most one goroutine can be running writeFrameAsync at a time per
  3278  // serverConn.
  3279  func (sc *http2serverConn) writeFrameAsync(wm http2frameWriteMsg) {
  3280  	err := wm.write.writeFrame(sc)
  3281  	sc.wroteFrameCh <- http2frameWriteResult{wm, err}
  3282  }
  3283  
  3284  func (sc *http2serverConn) closeAllStreamsOnConnClose() {
  3285  	sc.serveG.check()
  3286  	for _, st := range sc.streams {
  3287  		sc.closeStream(st, http2errClientDisconnected)
  3288  	}
  3289  }
  3290  
  3291  func (sc *http2serverConn) stopShutdownTimer() {
  3292  	sc.serveG.check()
  3293  	if t := sc.shutdownTimer; t != nil {
  3294  		t.Stop()
  3295  	}
  3296  }
  3297  
  3298  func (sc *http2serverConn) notePanic() {
  3299  
  3300  	if http2testHookOnPanicMu != nil {
  3301  		http2testHookOnPanicMu.Lock()
  3302  		defer http2testHookOnPanicMu.Unlock()
  3303  	}
  3304  	if http2testHookOnPanic != nil {
  3305  		if e := recover(); e != nil {
  3306  			if http2testHookOnPanic(sc, e) {
  3307  				panic(e)
  3308  			}
  3309  		}
  3310  	}
  3311  }
  3312  
  3313  func (sc *http2serverConn) serve() {
  3314  	sc.serveG.check()
  3315  	defer sc.notePanic()
  3316  	defer sc.conn.Close()
  3317  	defer sc.closeAllStreamsOnConnClose()
  3318  	defer sc.stopShutdownTimer()
  3319  	defer close(sc.doneServing)
  3320  
  3321  	if http2VerboseLogs {
  3322  		sc.vlogf("http2: server connection from %v on %p", sc.conn.RemoteAddr(), sc.hs)
  3323  	}
  3324  
  3325  	sc.writeFrame(http2frameWriteMsg{
  3326  		write: http2writeSettings{
  3327  			{http2SettingMaxFrameSize, sc.srv.maxReadFrameSize()},
  3328  			{http2SettingMaxConcurrentStreams, sc.advMaxStreams},
  3329  			{http2SettingMaxHeaderListSize, sc.maxHeaderListSize()},
  3330  		},
  3331  	})
  3332  	sc.unackedSettings++
  3333  
  3334  	if err := sc.readPreface(); err != nil {
  3335  		sc.condlogf(err, "http2: server: error reading preface from client %v: %v", sc.conn.RemoteAddr(), err)
  3336  		return
  3337  	}
  3338  
  3339  	sc.setConnState(StateActive)
  3340  	sc.setConnState(StateIdle)
  3341  
  3342  	go sc.readFrames()
  3343  
  3344  	settingsTimer := time.NewTimer(http2firstSettingsTimeout)
  3345  	loopNum := 0
  3346  	for {
  3347  		loopNum++
  3348  		select {
  3349  		case wm := <-sc.wantWriteFrameCh:
  3350  			sc.writeFrame(wm)
  3351  		case res := <-sc.wroteFrameCh:
  3352  			sc.wroteFrame(res)
  3353  		case res := <-sc.readFrameCh:
  3354  			if !sc.processFrameFromReader(res) {
  3355  				return
  3356  			}
  3357  			res.readMore()
  3358  			if settingsTimer.C != nil {
  3359  				settingsTimer.Stop()
  3360  				settingsTimer.C = nil
  3361  			}
  3362  		case m := <-sc.bodyReadCh:
  3363  			sc.noteBodyRead(m.st, m.n)
  3364  		case <-settingsTimer.C:
  3365  			sc.logf("timeout waiting for SETTINGS frames from %v", sc.conn.RemoteAddr())
  3366  			return
  3367  		case <-sc.shutdownTimerCh:
  3368  			sc.vlogf("GOAWAY close timer fired; closing conn from %v", sc.conn.RemoteAddr())
  3369  			return
  3370  		case fn := <-sc.testHookCh:
  3371  			fn(loopNum)
  3372  		}
  3373  	}
  3374  }
  3375  
  3376  // readPreface reads the ClientPreface greeting from the peer
  3377  // or returns an error on timeout or an invalid greeting.
  3378  func (sc *http2serverConn) readPreface() error {
  3379  	errc := make(chan error, 1)
  3380  	go func() {
  3381  
  3382  		buf := make([]byte, len(http2ClientPreface))
  3383  		if _, err := io.ReadFull(sc.conn, buf); err != nil {
  3384  			errc <- err
  3385  		} else if !bytes.Equal(buf, http2clientPreface) {
  3386  			errc <- fmt.Errorf("bogus greeting %q", buf)
  3387  		} else {
  3388  			errc <- nil
  3389  		}
  3390  	}()
  3391  	timer := time.NewTimer(http2prefaceTimeout)
  3392  	defer timer.Stop()
  3393  	select {
  3394  	case <-timer.C:
  3395  		return errors.New("timeout waiting for client preface")
  3396  	case err := <-errc:
  3397  		if err == nil {
  3398  			if http2VerboseLogs {
  3399  				sc.vlogf("http2: server: client %v said hello", sc.conn.RemoteAddr())
  3400  			}
  3401  		}
  3402  		return err
  3403  	}
  3404  }
  3405  
  3406  var http2errChanPool = sync.Pool{
  3407  	New: func() interface{} { return make(chan error, 1) },
  3408  }
  3409  
  3410  var http2writeDataPool = sync.Pool{
  3411  	New: func() interface{} { return new(http2writeData) },
  3412  }
  3413  
  3414  // writeDataFromHandler writes DATA response frames from a handler on
  3415  // the given stream.
  3416  func (sc *http2serverConn) writeDataFromHandler(stream *http2stream, data []byte, endStream bool) error {
  3417  	ch := http2errChanPool.Get().(chan error)
  3418  	writeArg := http2writeDataPool.Get().(*http2writeData)
  3419  	*writeArg = http2writeData{stream.id, data, endStream}
  3420  	err := sc.writeFrameFromHandler(http2frameWriteMsg{
  3421  		write:  writeArg,
  3422  		stream: stream,
  3423  		done:   ch,
  3424  	})
  3425  	if err != nil {
  3426  		return err
  3427  	}
  3428  	var frameWriteDone bool // the frame write is done (successfully or not)
  3429  	select {
  3430  	case err = <-ch:
  3431  		frameWriteDone = true
  3432  	case <-sc.doneServing:
  3433  		return http2errClientDisconnected
  3434  	case <-stream.cw:
  3435  
  3436  		select {
  3437  		case err = <-ch:
  3438  			frameWriteDone = true
  3439  		default:
  3440  			return http2errStreamClosed
  3441  		}
  3442  	}
  3443  	http2errChanPool.Put(ch)
  3444  	if frameWriteDone {
  3445  		http2writeDataPool.Put(writeArg)
  3446  	}
  3447  	return err
  3448  }
  3449  
  3450  // writeFrameFromHandler sends wm to sc.wantWriteFrameCh, but aborts
  3451  // if the connection has gone away.
  3452  //
  3453  // This must not be run from the serve goroutine itself, else it might
  3454  // deadlock writing to sc.wantWriteFrameCh (which is only mildly
  3455  // buffered and is read by serve itself). If you're on the serve
  3456  // goroutine, call writeFrame instead.
  3457  func (sc *http2serverConn) writeFrameFromHandler(wm http2frameWriteMsg) error {
  3458  	sc.serveG.checkNotOn()
  3459  	select {
  3460  	case sc.wantWriteFrameCh <- wm:
  3461  		return nil
  3462  	case <-sc.doneServing:
  3463  
  3464  		return http2errClientDisconnected
  3465  	}
  3466  }
  3467  
  3468  // writeFrame schedules a frame to write and sends it if there's nothing
  3469  // already being written.
  3470  //
  3471  // There is no pushback here (the serve goroutine never blocks). It's
  3472  // the http.Handlers that block, waiting for their previous frames to
  3473  // make it onto the wire
  3474  //
  3475  // If you're not on the serve goroutine, use writeFrameFromHandler instead.
  3476  func (sc *http2serverConn) writeFrame(wm http2frameWriteMsg) {
  3477  	sc.serveG.check()
  3478  	sc.writeSched.add(wm)
  3479  	sc.scheduleFrameWrite()
  3480  }
  3481  
  3482  // startFrameWrite starts a goroutine to write wm (in a separate
  3483  // goroutine since that might block on the network), and updates the
  3484  // serve goroutine's state about the world, updated from info in wm.
  3485  func (sc *http2serverConn) startFrameWrite(wm http2frameWriteMsg) {
  3486  	sc.serveG.check()
  3487  	if sc.writingFrame {
  3488  		panic("internal error: can only be writing one frame at a time")
  3489  	}
  3490  
  3491  	st := wm.stream
  3492  	if st != nil {
  3493  		switch st.state {
  3494  		case http2stateHalfClosedLocal:
  3495  			panic("internal error: attempt to send frame on half-closed-local stream")
  3496  		case http2stateClosed:
  3497  			if st.sentReset || st.gotReset {
  3498  
  3499  				sc.scheduleFrameWrite()
  3500  				return
  3501  			}
  3502  			panic(fmt.Sprintf("internal error: attempt to send a write %v on a closed stream", wm))
  3503  		}
  3504  	}
  3505  
  3506  	sc.writingFrame = true
  3507  	sc.needsFrameFlush = true
  3508  	go sc.writeFrameAsync(wm)
  3509  }
  3510  
  3511  // errHandlerPanicked is the error given to any callers blocked in a read from
  3512  // Request.Body when the main goroutine panics. Since most handlers read in the
  3513  // the main ServeHTTP goroutine, this will show up rarely.
  3514  var http2errHandlerPanicked = errors.New("http2: handler panicked")
  3515  
  3516  // wroteFrame is called on the serve goroutine with the result of
  3517  // whatever happened on writeFrameAsync.
  3518  func (sc *http2serverConn) wroteFrame(res http2frameWriteResult) {
  3519  	sc.serveG.check()
  3520  	if !sc.writingFrame {
  3521  		panic("internal error: expected to be already writing a frame")
  3522  	}
  3523  	sc.writingFrame = false
  3524  
  3525  	wm := res.wm
  3526  	st := wm.stream
  3527  
  3528  	closeStream := http2endsStream(wm.write)
  3529  
  3530  	if _, ok := wm.write.(http2handlerPanicRST); ok {
  3531  		sc.closeStream(st, http2errHandlerPanicked)
  3532  	}
  3533  
  3534  	if ch := wm.done; ch != nil {
  3535  		select {
  3536  		case ch <- res.err:
  3537  		default:
  3538  			panic(fmt.Sprintf("unbuffered done channel passed in for type %T", wm.write))
  3539  		}
  3540  	}
  3541  	wm.write = nil
  3542  
  3543  	if closeStream {
  3544  		if st == nil {
  3545  			panic("internal error: expecting non-nil stream")
  3546  		}
  3547  		switch st.state {
  3548  		case http2stateOpen:
  3549  
  3550  			st.state = http2stateHalfClosedLocal
  3551  			errCancel := http2StreamError{st.id, http2ErrCodeCancel}
  3552  			sc.resetStream(errCancel)
  3553  		case http2stateHalfClosedRemote:
  3554  			sc.closeStream(st, http2errHandlerComplete)
  3555  		}
  3556  	}
  3557  
  3558  	sc.scheduleFrameWrite()
  3559  }
  3560  
  3561  // scheduleFrameWrite tickles the frame writing scheduler.
  3562  //
  3563  // If a frame is already being written, nothing happens. This will be called again
  3564  // when the frame is done being written.
  3565  //
  3566  // If a frame isn't being written we need to send one, the best frame
  3567  // to send is selected, preferring first things that aren't
  3568  // stream-specific (e.g. ACKing settings), and then finding the
  3569  // highest priority stream.
  3570  //
  3571  // If a frame isn't being written and there's nothing else to send, we
  3572  // flush the write buffer.
  3573  func (sc *http2serverConn) scheduleFrameWrite() {
  3574  	sc.serveG.check()
  3575  	if sc.writingFrame {
  3576  		return
  3577  	}
  3578  	if sc.needToSendGoAway {
  3579  		sc.needToSendGoAway = false
  3580  		sc.startFrameWrite(http2frameWriteMsg{
  3581  			write: &http2writeGoAway{
  3582  				maxStreamID: sc.maxStreamID,
  3583  				code:        sc.goAwayCode,
  3584  			},
  3585  		})
  3586  		return
  3587  	}
  3588  	if sc.needToSendSettingsAck {
  3589  		sc.needToSendSettingsAck = false
  3590  		sc.startFrameWrite(http2frameWriteMsg{write: http2writeSettingsAck{}})
  3591  		return
  3592  	}
  3593  	if !sc.inGoAway {
  3594  		if wm, ok := sc.writeSched.take(); ok {
  3595  			sc.startFrameWrite(wm)
  3596  			return
  3597  		}
  3598  	}
  3599  	if sc.needsFrameFlush {
  3600  		sc.startFrameWrite(http2frameWriteMsg{write: http2flushFrameWriter{}})
  3601  		sc.needsFrameFlush = false
  3602  		return
  3603  	}
  3604  }
  3605  
  3606  func (sc *http2serverConn) goAway(code http2ErrCode) {
  3607  	sc.serveG.check()
  3608  	if sc.inGoAway {
  3609  		return
  3610  	}
  3611  	if code != http2ErrCodeNo {
  3612  		sc.shutDownIn(250 * time.Millisecond)
  3613  	} else {
  3614  
  3615  		sc.shutDownIn(1 * time.Second)
  3616  	}
  3617  	sc.inGoAway = true
  3618  	sc.needToSendGoAway = true
  3619  	sc.goAwayCode = code
  3620  	sc.scheduleFrameWrite()
  3621  }
  3622  
  3623  func (sc *http2serverConn) shutDownIn(d time.Duration) {
  3624  	sc.serveG.check()
  3625  	sc.shutdownTimer = time.NewTimer(d)
  3626  	sc.shutdownTimerCh = sc.shutdownTimer.C
  3627  }
  3628  
  3629  func (sc *http2serverConn) resetStream(se http2StreamError) {
  3630  	sc.serveG.check()
  3631  	sc.writeFrame(http2frameWriteMsg{write: se})
  3632  	if st, ok := sc.streams[se.StreamID]; ok {
  3633  		st.sentReset = true
  3634  		sc.closeStream(st, se)
  3635  	}
  3636  }
  3637  
  3638  // processFrameFromReader processes the serve loop's read from readFrameCh from the
  3639  // frame-reading goroutine.
  3640  // processFrameFromReader returns whether the connection should be kept open.
  3641  func (sc *http2serverConn) processFrameFromReader(res http2readFrameResult) bool {
  3642  	sc.serveG.check()
  3643  	err := res.err
  3644  	if err != nil {
  3645  		if err == http2ErrFrameTooLarge {
  3646  			sc.goAway(http2ErrCodeFrameSize)
  3647  			return true
  3648  		}
  3649  		clientGone := err == io.EOF || err == io.ErrUnexpectedEOF || http2isClosedConnError(err)
  3650  		if clientGone {
  3651  
  3652  			return false
  3653  		}
  3654  	} else {
  3655  		f := res.f
  3656  		if http2VerboseLogs {
  3657  			sc.vlogf("http2: server read frame %v", http2summarizeFrame(f))
  3658  		}
  3659  		err = sc.processFrame(f)
  3660  		if err == nil {
  3661  			return true
  3662  		}
  3663  	}
  3664  
  3665  	switch ev := err.(type) {
  3666  	case http2StreamError:
  3667  		sc.resetStream(ev)
  3668  		return true
  3669  	case http2goAwayFlowError:
  3670  		sc.goAway(http2ErrCodeFlowControl)
  3671  		return true
  3672  	case http2ConnectionError:
  3673  		sc.logf("http2: server connection error from %v: %v", sc.conn.RemoteAddr(), ev)
  3674  		sc.goAway(http2ErrCode(ev))
  3675  		return true
  3676  	default:
  3677  		if res.err != nil {
  3678  			sc.vlogf("http2: server closing client connection; error reading frame from client %s: %v", sc.conn.RemoteAddr(), err)
  3679  		} else {
  3680  			sc.logf("http2: server closing client connection: %v", err)
  3681  		}
  3682  		return false
  3683  	}
  3684  }
  3685  
  3686  func (sc *http2serverConn) processFrame(f http2Frame) error {
  3687  	sc.serveG.check()
  3688  
  3689  	if !sc.sawFirstSettings {
  3690  		if _, ok := f.(*http2SettingsFrame); !ok {
  3691  			return http2ConnectionError(http2ErrCodeProtocol)
  3692  		}
  3693  		sc.sawFirstSettings = true
  3694  	}
  3695  
  3696  	switch f := f.(type) {
  3697  	case *http2SettingsFrame:
  3698  		return sc.processSettings(f)
  3699  	case *http2MetaHeadersFrame:
  3700  		return sc.processHeaders(f)
  3701  	case *http2WindowUpdateFrame:
  3702  		return sc.processWindowUpdate(f)
  3703  	case *http2PingFrame:
  3704  		return sc.processPing(f)
  3705  	case *http2DataFrame:
  3706  		return sc.processData(f)
  3707  	case *http2RSTStreamFrame:
  3708  		return sc.processResetStream(f)
  3709  	case *http2PriorityFrame:
  3710  		return sc.processPriority(f)
  3711  	case *http2PushPromiseFrame:
  3712  
  3713  		return http2ConnectionError(http2ErrCodeProtocol)
  3714  	default:
  3715  		sc.vlogf("http2: server ignoring frame: %v", f.Header())
  3716  		return nil
  3717  	}
  3718  }
  3719  
  3720  func (sc *http2serverConn) processPing(f *http2PingFrame) error {
  3721  	sc.serveG.check()
  3722  	if f.IsAck() {
  3723  
  3724  		return nil
  3725  	}
  3726  	if f.StreamID != 0 {
  3727  
  3728  		return http2ConnectionError(http2ErrCodeProtocol)
  3729  	}
  3730  	sc.writeFrame(http2frameWriteMsg{write: http2writePingAck{f}})
  3731  	return nil
  3732  }
  3733  
  3734  func (sc *http2serverConn) processWindowUpdate(f *http2WindowUpdateFrame) error {
  3735  	sc.serveG.check()
  3736  	switch {
  3737  	case f.StreamID != 0:
  3738  		st := sc.streams[f.StreamID]
  3739  		if st == nil {
  3740  
  3741  			return nil
  3742  		}
  3743  		if !st.flow.add(int32(f.Increment)) {
  3744  			return http2StreamError{f.StreamID, http2ErrCodeFlowControl}
  3745  		}
  3746  	default:
  3747  		if !sc.flow.add(int32(f.Increment)) {
  3748  			return http2goAwayFlowError{}
  3749  		}
  3750  	}
  3751  	sc.scheduleFrameWrite()
  3752  	return nil
  3753  }
  3754  
  3755  func (sc *http2serverConn) processResetStream(f *http2RSTStreamFrame) error {
  3756  	sc.serveG.check()
  3757  
  3758  	state, st := sc.state(f.StreamID)
  3759  	if state == http2stateIdle {
  3760  
  3761  		return http2ConnectionError(http2ErrCodeProtocol)
  3762  	}
  3763  	if st != nil {
  3764  		st.gotReset = true
  3765  		sc.closeStream(st, http2StreamError{f.StreamID, f.ErrCode})
  3766  	}
  3767  	return nil
  3768  }
  3769  
  3770  func (sc *http2serverConn) closeStream(st *http2stream, err error) {
  3771  	sc.serveG.check()
  3772  	if st.state == http2stateIdle || st.state == http2stateClosed {
  3773  		panic(fmt.Sprintf("invariant; can't close stream in state %v", st.state))
  3774  	}
  3775  	st.state = http2stateClosed
  3776  	sc.curOpenStreams--
  3777  	if sc.curOpenStreams == 0 {
  3778  		sc.setConnState(StateIdle)
  3779  	}
  3780  	delete(sc.streams, st.id)
  3781  	if p := st.body; p != nil {
  3782  		p.CloseWithError(err)
  3783  	}
  3784  	st.cw.Close()
  3785  	sc.writeSched.forgetStream(st.id)
  3786  	if st.reqBuf != nil {
  3787  
  3788  		sc.freeRequestBodyBuf = st.reqBuf
  3789  	}
  3790  }
  3791  
  3792  func (sc *http2serverConn) processSettings(f *http2SettingsFrame) error {
  3793  	sc.serveG.check()
  3794  	if f.IsAck() {
  3795  		sc.unackedSettings--
  3796  		if sc.unackedSettings < 0 {
  3797  
  3798  			return http2ConnectionError(http2ErrCodeProtocol)
  3799  		}
  3800  		return nil
  3801  	}
  3802  	if err := f.ForeachSetting(sc.processSetting); err != nil {
  3803  		return err
  3804  	}
  3805  	sc.needToSendSettingsAck = true
  3806  	sc.scheduleFrameWrite()
  3807  	return nil
  3808  }
  3809  
  3810  func (sc *http2serverConn) processSetting(s http2Setting) error {
  3811  	sc.serveG.check()
  3812  	if err := s.Valid(); err != nil {
  3813  		return err
  3814  	}
  3815  	if http2VerboseLogs {
  3816  		sc.vlogf("http2: server processing setting %v", s)
  3817  	}
  3818  	switch s.ID {
  3819  	case http2SettingHeaderTableSize:
  3820  		sc.headerTableSize = s.Val
  3821  		sc.hpackEncoder.SetMaxDynamicTableSize(s.Val)
  3822  	case http2SettingEnablePush:
  3823  		sc.pushEnabled = s.Val != 0
  3824  	case http2SettingMaxConcurrentStreams:
  3825  		sc.clientMaxStreams = s.Val
  3826  	case http2SettingInitialWindowSize:
  3827  		return sc.processSettingInitialWindowSize(s.Val)
  3828  	case http2SettingMaxFrameSize:
  3829  		sc.writeSched.maxFrameSize = s.Val
  3830  	case http2SettingMaxHeaderListSize:
  3831  		sc.peerMaxHeaderListSize = s.Val
  3832  	default:
  3833  
  3834  		if http2VerboseLogs {
  3835  			sc.vlogf("http2: server ignoring unknown setting %v", s)
  3836  		}
  3837  	}
  3838  	return nil
  3839  }
  3840  
  3841  func (sc *http2serverConn) processSettingInitialWindowSize(val uint32) error {
  3842  	sc.serveG.check()
  3843  
  3844  	old := sc.initialWindowSize
  3845  	sc.initialWindowSize = int32(val)
  3846  	growth := sc.initialWindowSize - old
  3847  	for _, st := range sc.streams {
  3848  		if !st.flow.add(growth) {
  3849  
  3850  			return http2ConnectionError(http2ErrCodeFlowControl)
  3851  		}
  3852  	}
  3853  	return nil
  3854  }
  3855  
  3856  func (sc *http2serverConn) processData(f *http2DataFrame) error {
  3857  	sc.serveG.check()
  3858  
  3859  	id := f.Header().StreamID
  3860  	st, ok := sc.streams[id]
  3861  	if !ok || st.state != http2stateOpen || st.gotTrailerHeader {
  3862  
  3863  		return http2StreamError{id, http2ErrCodeStreamClosed}
  3864  	}
  3865  	if st.body == nil {
  3866  		panic("internal error: should have a body in this state")
  3867  	}
  3868  	data := f.Data()
  3869  
  3870  	if st.declBodyBytes != -1 && st.bodyBytes+int64(len(data)) > st.declBodyBytes {
  3871  		st.body.CloseWithError(fmt.Errorf("sender tried to send more than declared Content-Length of %d bytes", st.declBodyBytes))
  3872  		return http2StreamError{id, http2ErrCodeStreamClosed}
  3873  	}
  3874  	if len(data) > 0 {
  3875  
  3876  		if int(st.inflow.available()) < len(data) {
  3877  			return http2StreamError{id, http2ErrCodeFlowControl}
  3878  		}
  3879  		st.inflow.take(int32(len(data)))
  3880  		wrote, err := st.body.Write(data)
  3881  		if err != nil {
  3882  			return http2StreamError{id, http2ErrCodeStreamClosed}
  3883  		}
  3884  		if wrote != len(data) {
  3885  			panic("internal error: bad Writer")
  3886  		}
  3887  		st.bodyBytes += int64(len(data))
  3888  	}
  3889  	if f.StreamEnded() {
  3890  		st.endStream()
  3891  	}
  3892  	return nil
  3893  }
  3894  
  3895  // endStream closes a Request.Body's pipe. It is called when a DATA
  3896  // frame says a request body is over (or after trailers).
  3897  func (st *http2stream) endStream() {
  3898  	sc := st.sc
  3899  	sc.serveG.check()
  3900  
  3901  	if st.declBodyBytes != -1 && st.declBodyBytes != st.bodyBytes {
  3902  		st.body.CloseWithError(fmt.Errorf("request declared a Content-Length of %d but only wrote %d bytes",
  3903  			st.declBodyBytes, st.bodyBytes))
  3904  	} else {
  3905  		st.body.closeWithErrorAndCode(io.EOF, st.copyTrailersToHandlerRequest)
  3906  		st.body.CloseWithError(io.EOF)
  3907  	}
  3908  	st.state = http2stateHalfClosedRemote
  3909  }
  3910  
  3911  // copyTrailersToHandlerRequest is run in the Handler's goroutine in
  3912  // its Request.Body.Read just before it gets io.EOF.
  3913  func (st *http2stream) copyTrailersToHandlerRequest() {
  3914  	for k, vv := range st.trailer {
  3915  		if _, ok := st.reqTrailer[k]; ok {
  3916  
  3917  			st.reqTrailer[k] = vv
  3918  		}
  3919  	}
  3920  }
  3921  
  3922  func (sc *http2serverConn) processHeaders(f *http2MetaHeadersFrame) error {
  3923  	sc.serveG.check()
  3924  	id := f.Header().StreamID
  3925  	if sc.inGoAway {
  3926  
  3927  		return nil
  3928  	}
  3929  
  3930  	if id%2 != 1 {
  3931  		return http2ConnectionError(http2ErrCodeProtocol)
  3932  	}
  3933  
  3934  	st := sc.streams[f.Header().StreamID]
  3935  	if st != nil {
  3936  		return st.processTrailerHeaders(f)
  3937  	}
  3938  
  3939  	if id <= sc.maxStreamID {
  3940  		return http2ConnectionError(http2ErrCodeProtocol)
  3941  	}
  3942  	sc.maxStreamID = id
  3943  
  3944  	st = &http2stream{
  3945  		sc:    sc,
  3946  		id:    id,
  3947  		state: http2stateOpen,
  3948  	}
  3949  	if f.StreamEnded() {
  3950  		st.state = http2stateHalfClosedRemote
  3951  	}
  3952  	st.cw.Init()
  3953  
  3954  	st.flow.conn = &sc.flow
  3955  	st.flow.add(sc.initialWindowSize)
  3956  	st.inflow.conn = &sc.inflow
  3957  	st.inflow.add(http2initialWindowSize)
  3958  
  3959  	sc.streams[id] = st
  3960  	if f.HasPriority() {
  3961  		http2adjustStreamPriority(sc.streams, st.id, f.Priority)
  3962  	}
  3963  	sc.curOpenStreams++
  3964  	if sc.curOpenStreams == 1 {
  3965  		sc.setConnState(StateActive)
  3966  	}
  3967  	if sc.curOpenStreams > sc.advMaxStreams {
  3968  
  3969  		if sc.unackedSettings == 0 {
  3970  
  3971  			return http2StreamError{st.id, http2ErrCodeProtocol}
  3972  		}
  3973  
  3974  		return http2StreamError{st.id, http2ErrCodeRefusedStream}
  3975  	}
  3976  
  3977  	rw, req, err := sc.newWriterAndRequest(st, f)
  3978  	if err != nil {
  3979  		return err
  3980  	}
  3981  	st.reqTrailer = req.Trailer
  3982  	if st.reqTrailer != nil {
  3983  		st.trailer = make(Header)
  3984  	}
  3985  	st.body = req.Body.(*http2requestBody).pipe
  3986  	st.declBodyBytes = req.ContentLength
  3987  
  3988  	handler := sc.handler.ServeHTTP
  3989  	if f.Truncated {
  3990  
  3991  		handler = http2handleHeaderListTooLong
  3992  	}
  3993  
  3994  	go sc.runHandler(rw, req, handler)
  3995  	return nil
  3996  }
  3997  
  3998  func (st *http2stream) processTrailerHeaders(f *http2MetaHeadersFrame) error {
  3999  	sc := st.sc
  4000  	sc.serveG.check()
  4001  	if st.gotTrailerHeader {
  4002  		return http2ConnectionError(http2ErrCodeProtocol)
  4003  	}
  4004  	st.gotTrailerHeader = true
  4005  	if !f.StreamEnded() {
  4006  		return http2StreamError{st.id, http2ErrCodeProtocol}
  4007  	}
  4008  
  4009  	if len(f.PseudoFields()) > 0 {
  4010  		return http2StreamError{st.id, http2ErrCodeProtocol}
  4011  	}
  4012  	if st.trailer != nil {
  4013  		for _, hf := range f.RegularFields() {
  4014  			key := sc.canonicalHeader(hf.Name)
  4015  			st.trailer[key] = append(st.trailer[key], hf.Value)
  4016  		}
  4017  	}
  4018  	st.endStream()
  4019  	return nil
  4020  }
  4021  
  4022  func (sc *http2serverConn) processPriority(f *http2PriorityFrame) error {
  4023  	http2adjustStreamPriority(sc.streams, f.StreamID, f.http2PriorityParam)
  4024  	return nil
  4025  }
  4026  
  4027  func http2adjustStreamPriority(streams map[uint32]*http2stream, streamID uint32, priority http2PriorityParam) {
  4028  	st, ok := streams[streamID]
  4029  	if !ok {
  4030  
  4031  		return
  4032  	}
  4033  	st.weight = priority.Weight
  4034  	parent := streams[priority.StreamDep]
  4035  	if parent == st {
  4036  
  4037  		return
  4038  	}
  4039  
  4040  	for piter := parent; piter != nil; piter = piter.parent {
  4041  		if piter == st {
  4042  			parent.parent = st.parent
  4043  			break
  4044  		}
  4045  	}
  4046  	st.parent = parent
  4047  	if priority.Exclusive && (st.parent != nil || priority.StreamDep == 0) {
  4048  		for _, openStream := range streams {
  4049  			if openStream != st && openStream.parent == st.parent {
  4050  				openStream.parent = st
  4051  			}
  4052  		}
  4053  	}
  4054  }
  4055  
  4056  func (sc *http2serverConn) newWriterAndRequest(st *http2stream, f *http2MetaHeadersFrame) (*http2responseWriter, *Request, error) {
  4057  	sc.serveG.check()
  4058  
  4059  	method := f.PseudoValue("method")
  4060  	path := f.PseudoValue("path")
  4061  	scheme := f.PseudoValue("scheme")
  4062  	authority := f.PseudoValue("authority")
  4063  
  4064  	isConnect := method == "CONNECT"
  4065  	if isConnect {
  4066  		if path != "" || scheme != "" || authority == "" {
  4067  			return nil, nil, http2StreamError{f.StreamID, http2ErrCodeProtocol}
  4068  		}
  4069  	} else if method == "" || path == "" ||
  4070  		(scheme != "https" && scheme != "http") {
  4071  
  4072  		return nil, nil, http2StreamError{f.StreamID, http2ErrCodeProtocol}
  4073  	}
  4074  
  4075  	bodyOpen := !f.StreamEnded()
  4076  	if method == "HEAD" && bodyOpen {
  4077  
  4078  		return nil, nil, http2StreamError{f.StreamID, http2ErrCodeProtocol}
  4079  	}
  4080  	var tlsState *tls.ConnectionState // nil if not scheme https
  4081  
  4082  	if scheme == "https" {
  4083  		tlsState = sc.tlsState
  4084  	}
  4085  
  4086  	header := make(Header)
  4087  	for _, hf := range f.RegularFields() {
  4088  		header.Add(sc.canonicalHeader(hf.Name), hf.Value)
  4089  	}
  4090  
  4091  	if authority == "" {
  4092  		authority = header.Get("Host")
  4093  	}
  4094  	needsContinue := header.Get("Expect") == "100-continue"
  4095  	if needsContinue {
  4096  		header.Del("Expect")
  4097  	}
  4098  
  4099  	if cookies := header["Cookie"]; len(cookies) > 1 {
  4100  		header.Set("Cookie", strings.Join(cookies, "; "))
  4101  	}
  4102  
  4103  	// Setup Trailers
  4104  	var trailer Header
  4105  	for _, v := range header["Trailer"] {
  4106  		for _, key := range strings.Split(v, ",") {
  4107  			key = CanonicalHeaderKey(strings.TrimSpace(key))
  4108  			switch key {
  4109  			case "Transfer-Encoding", "Trailer", "Content-Length":
  4110  
  4111  			default:
  4112  				if trailer == nil {
  4113  					trailer = make(Header)
  4114  				}
  4115  				trailer[key] = nil
  4116  			}
  4117  		}
  4118  	}
  4119  	delete(header, "Trailer")
  4120  
  4121  	body := &http2requestBody{
  4122  		conn:          sc,
  4123  		stream:        st,
  4124  		needsContinue: needsContinue,
  4125  	}
  4126  	var url_ *url.URL
  4127  	var requestURI string
  4128  	if isConnect {
  4129  		url_ = &url.URL{Host: authority}
  4130  		requestURI = authority
  4131  	} else {
  4132  		var err error
  4133  		url_, err = url.ParseRequestURI(path)
  4134  		if err != nil {
  4135  			return nil, nil, http2StreamError{f.StreamID, http2ErrCodeProtocol}
  4136  		}
  4137  		requestURI = path
  4138  	}
  4139  	req := &Request{
  4140  		Method:     method,
  4141  		URL:        url_,
  4142  		RemoteAddr: sc.remoteAddrStr,
  4143  		Header:     header,
  4144  		RequestURI: requestURI,
  4145  		Proto:      "HTTP/2.0",
  4146  		ProtoMajor: 2,
  4147  		ProtoMinor: 0,
  4148  		TLS:        tlsState,
  4149  		Host:       authority,
  4150  		Body:       body,
  4151  		Trailer:    trailer,
  4152  	}
  4153  	if bodyOpen {
  4154  
  4155  		buf := make([]byte, http2initialWindowSize)
  4156  
  4157  		body.pipe = &http2pipe{
  4158  			b: &http2fixedBuffer{buf: buf},
  4159  		}
  4160  
  4161  		if vv, ok := header["Content-Length"]; ok {
  4162  			req.ContentLength, _ = strconv.ParseInt(vv[0], 10, 64)
  4163  		} else {
  4164  			req.ContentLength = -1
  4165  		}
  4166  	}
  4167  
  4168  	rws := http2responseWriterStatePool.Get().(*http2responseWriterState)
  4169  	bwSave := rws.bw
  4170  	*rws = http2responseWriterState{}
  4171  	rws.conn = sc
  4172  	rws.bw = bwSave
  4173  	rws.bw.Reset(http2chunkWriter{rws})
  4174  	rws.stream = st
  4175  	rws.req = req
  4176  	rws.body = body
  4177  
  4178  	rw := &http2responseWriter{rws: rws}
  4179  	return rw, req, nil
  4180  }
  4181  
  4182  func (sc *http2serverConn) getRequestBodyBuf() []byte {
  4183  	sc.serveG.check()
  4184  	if buf := sc.freeRequestBodyBuf; buf != nil {
  4185  		sc.freeRequestBodyBuf = nil
  4186  		return buf
  4187  	}
  4188  	return make([]byte, http2initialWindowSize)
  4189  }
  4190  
  4191  // Run on its own goroutine.
  4192  func (sc *http2serverConn) runHandler(rw *http2responseWriter, req *Request, handler func(ResponseWriter, *Request)) {
  4193  	didPanic := true
  4194  	defer func() {
  4195  		if didPanic {
  4196  			e := recover()
  4197  			// Same as net/http:
  4198  			const size = 64 << 10
  4199  			buf := make([]byte, size)
  4200  			buf = buf[:runtime.Stack(buf, false)]
  4201  			sc.writeFrameFromHandler(http2frameWriteMsg{
  4202  				write:  http2handlerPanicRST{rw.rws.stream.id},
  4203  				stream: rw.rws.stream,
  4204  			})
  4205  			sc.logf("http2: panic serving %v: %v\n%s", sc.conn.RemoteAddr(), e, buf)
  4206  			return
  4207  		}
  4208  		rw.handlerDone()
  4209  	}()
  4210  	handler(rw, req)
  4211  	didPanic = false
  4212  }
  4213  
  4214  func http2handleHeaderListTooLong(w ResponseWriter, r *Request) {
  4215  	// 10.5.1 Limits on Header Block Size:
  4216  	// .. "A server that receives a larger header block than it is
  4217  	// willing to handle can send an HTTP 431 (Request Header Fields Too
  4218  	// Large) status code"
  4219  	const statusRequestHeaderFieldsTooLarge = 431 // only in Go 1.6+
  4220  	w.WriteHeader(statusRequestHeaderFieldsTooLarge)
  4221  	io.WriteString(w, "<h1>HTTP Error 431</h1><p>Request Header Field(s) Too Large</p>")
  4222  }
  4223  
  4224  // called from handler goroutines.
  4225  // h may be nil.
  4226  func (sc *http2serverConn) writeHeaders(st *http2stream, headerData *http2writeResHeaders) error {
  4227  	sc.serveG.checkNotOn()
  4228  	var errc chan error
  4229  	if headerData.h != nil {
  4230  
  4231  		errc = http2errChanPool.Get().(chan error)
  4232  	}
  4233  	if err := sc.writeFrameFromHandler(http2frameWriteMsg{
  4234  		write:  headerData,
  4235  		stream: st,
  4236  		done:   errc,
  4237  	}); err != nil {
  4238  		return err
  4239  	}
  4240  	if errc != nil {
  4241  		select {
  4242  		case err := <-errc:
  4243  			http2errChanPool.Put(errc)
  4244  			return err
  4245  		case <-sc.doneServing:
  4246  			return http2errClientDisconnected
  4247  		case <-st.cw:
  4248  			return http2errStreamClosed
  4249  		}
  4250  	}
  4251  	return nil
  4252  }
  4253  
  4254  // called from handler goroutines.
  4255  func (sc *http2serverConn) write100ContinueHeaders(st *http2stream) {
  4256  	sc.writeFrameFromHandler(http2frameWriteMsg{
  4257  		write:  http2write100ContinueHeadersFrame{st.id},
  4258  		stream: st,
  4259  	})
  4260  }
  4261  
  4262  // A bodyReadMsg tells the server loop that the http.Handler read n
  4263  // bytes of the DATA from the client on the given stream.
  4264  type http2bodyReadMsg struct {
  4265  	st *http2stream
  4266  	n  int
  4267  }
  4268  
  4269  // called from handler goroutines.
  4270  // Notes that the handler for the given stream ID read n bytes of its body
  4271  // and schedules flow control tokens to be sent.
  4272  func (sc *http2serverConn) noteBodyReadFromHandler(st *http2stream, n int) {
  4273  	sc.serveG.checkNotOn()
  4274  	select {
  4275  	case sc.bodyReadCh <- http2bodyReadMsg{st, n}:
  4276  	case <-sc.doneServing:
  4277  	}
  4278  }
  4279  
  4280  func (sc *http2serverConn) noteBodyRead(st *http2stream, n int) {
  4281  	sc.serveG.check()
  4282  	sc.sendWindowUpdate(nil, n)
  4283  	if st.state != http2stateHalfClosedRemote && st.state != http2stateClosed {
  4284  
  4285  		sc.sendWindowUpdate(st, n)
  4286  	}
  4287  }
  4288  
  4289  // st may be nil for conn-level
  4290  func (sc *http2serverConn) sendWindowUpdate(st *http2stream, n int) {
  4291  	sc.serveG.check()
  4292  	// "The legal range for the increment to the flow control
  4293  	// window is 1 to 2^31-1 (2,147,483,647) octets."
  4294  	// A Go Read call on 64-bit machines could in theory read
  4295  	// a larger Read than this. Very unlikely, but we handle it here
  4296  	// rather than elsewhere for now.
  4297  	const maxUint31 = 1<<31 - 1
  4298  	for n >= maxUint31 {
  4299  		sc.sendWindowUpdate32(st, maxUint31)
  4300  		n -= maxUint31
  4301  	}
  4302  	sc.sendWindowUpdate32(st, int32(n))
  4303  }
  4304  
  4305  // st may be nil for conn-level
  4306  func (sc *http2serverConn) sendWindowUpdate32(st *http2stream, n int32) {
  4307  	sc.serveG.check()
  4308  	if n == 0 {
  4309  		return
  4310  	}
  4311  	if n < 0 {
  4312  		panic("negative update")
  4313  	}
  4314  	var streamID uint32
  4315  	if st != nil {
  4316  		streamID = st.id
  4317  	}
  4318  	sc.writeFrame(http2frameWriteMsg{
  4319  		write:  http2writeWindowUpdate{streamID: streamID, n: uint32(n)},
  4320  		stream: st,
  4321  	})
  4322  	var ok bool
  4323  	if st == nil {
  4324  		ok = sc.inflow.add(n)
  4325  	} else {
  4326  		ok = st.inflow.add(n)
  4327  	}
  4328  	if !ok {
  4329  		panic("internal error; sent too many window updates without decrements?")
  4330  	}
  4331  }
  4332  
  4333  type http2requestBody struct {
  4334  	stream        *http2stream
  4335  	conn          *http2serverConn
  4336  	closed        bool
  4337  	pipe          *http2pipe // non-nil if we have a HTTP entity message body
  4338  	needsContinue bool       // need to send a 100-continue
  4339  }
  4340  
  4341  func (b *http2requestBody) Close() error {
  4342  	if b.pipe != nil {
  4343  		b.pipe.CloseWithError(http2errClosedBody)
  4344  	}
  4345  	b.closed = true
  4346  	return nil
  4347  }
  4348  
  4349  func (b *http2requestBody) Read(p []byte) (n int, err error) {
  4350  	if b.needsContinue {
  4351  		b.needsContinue = false
  4352  		b.conn.write100ContinueHeaders(b.stream)
  4353  	}
  4354  	if b.pipe == nil {
  4355  		return 0, io.EOF
  4356  	}
  4357  	n, err = b.pipe.Read(p)
  4358  	if n > 0 {
  4359  		b.conn.noteBodyReadFromHandler(b.stream, n)
  4360  	}
  4361  	return
  4362  }
  4363  
  4364  // responseWriter is the http.ResponseWriter implementation.  It's
  4365  // intentionally small (1 pointer wide) to minimize garbage.  The
  4366  // responseWriterState pointer inside is zeroed at the end of a
  4367  // request (in handlerDone) and calls on the responseWriter thereafter
  4368  // simply crash (caller's mistake), but the much larger responseWriterState
  4369  // and buffers are reused between multiple requests.
  4370  type http2responseWriter struct {
  4371  	rws *http2responseWriterState
  4372  }
  4373  
  4374  // Optional http.ResponseWriter interfaces implemented.
  4375  var (
  4376  	_ CloseNotifier     = (*http2responseWriter)(nil)
  4377  	_ Flusher           = (*http2responseWriter)(nil)
  4378  	_ http2stringWriter = (*http2responseWriter)(nil)
  4379  )
  4380  
  4381  type http2responseWriterState struct {
  4382  	// immutable within a request:
  4383  	stream *http2stream
  4384  	req    *Request
  4385  	body   *http2requestBody // to close at end of request, if DATA frames didn't
  4386  	conn   *http2serverConn
  4387  
  4388  	// TODO: adjust buffer writing sizes based on server config, frame size updates from peer, etc
  4389  	bw *bufio.Writer // writing to a chunkWriter{this *responseWriterState}
  4390  
  4391  	// mutated by http.Handler goroutine:
  4392  	handlerHeader Header   // nil until called
  4393  	snapHeader    Header   // snapshot of handlerHeader at WriteHeader time
  4394  	trailers      []string // set in writeChunk
  4395  	status        int      // status code passed to WriteHeader
  4396  	wroteHeader   bool     // WriteHeader called (explicitly or implicitly). Not necessarily sent to user yet.
  4397  	sentHeader    bool     // have we sent the header frame?
  4398  	handlerDone   bool     // handler has finished
  4399  
  4400  	sentContentLen int64 // non-zero if handler set a Content-Length header
  4401  	wroteBytes     int64
  4402  
  4403  	closeNotifierMu sync.Mutex // guards closeNotifierCh
  4404  	closeNotifierCh chan bool  // nil until first used
  4405  }
  4406  
  4407  type http2chunkWriter struct{ rws *http2responseWriterState }
  4408  
  4409  func (cw http2chunkWriter) Write(p []byte) (n int, err error) { return cw.rws.writeChunk(p) }
  4410  
  4411  func (rws *http2responseWriterState) hasTrailers() bool { return len(rws.trailers) != 0 }
  4412  
  4413  // declareTrailer is called for each Trailer header when the
  4414  // response header is written. It notes that a header will need to be
  4415  // written in the trailers at the end of the response.
  4416  func (rws *http2responseWriterState) declareTrailer(k string) {
  4417  	k = CanonicalHeaderKey(k)
  4418  	switch k {
  4419  	case "Transfer-Encoding", "Content-Length", "Trailer":
  4420  
  4421  		return
  4422  	}
  4423  	if !http2strSliceContains(rws.trailers, k) {
  4424  		rws.trailers = append(rws.trailers, k)
  4425  	}
  4426  }
  4427  
  4428  // writeChunk writes chunks from the bufio.Writer. But because
  4429  // bufio.Writer may bypass its chunking, sometimes p may be
  4430  // arbitrarily large.
  4431  //
  4432  // writeChunk is also responsible (on the first chunk) for sending the
  4433  // HEADER response.
  4434  func (rws *http2responseWriterState) writeChunk(p []byte) (n int, err error) {
  4435  	if !rws.wroteHeader {
  4436  		rws.writeHeader(200)
  4437  	}
  4438  
  4439  	isHeadResp := rws.req.Method == "HEAD"
  4440  	if !rws.sentHeader {
  4441  		rws.sentHeader = true
  4442  		var ctype, clen string
  4443  		if clen = rws.snapHeader.Get("Content-Length"); clen != "" {
  4444  			rws.snapHeader.Del("Content-Length")
  4445  			clen64, err := strconv.ParseInt(clen, 10, 64)
  4446  			if err == nil && clen64 >= 0 {
  4447  				rws.sentContentLen = clen64
  4448  			} else {
  4449  				clen = ""
  4450  			}
  4451  		}
  4452  		if clen == "" && rws.handlerDone && http2bodyAllowedForStatus(rws.status) && (len(p) > 0 || !isHeadResp) {
  4453  			clen = strconv.Itoa(len(p))
  4454  		}
  4455  		_, hasContentType := rws.snapHeader["Content-Type"]
  4456  		if !hasContentType && http2bodyAllowedForStatus(rws.status) {
  4457  			ctype = DetectContentType(p)
  4458  		}
  4459  		var date string
  4460  		if _, ok := rws.snapHeader["Date"]; !ok {
  4461  
  4462  			date = time.Now().UTC().Format(TimeFormat)
  4463  		}
  4464  
  4465  		for _, v := range rws.snapHeader["Trailer"] {
  4466  			http2foreachHeaderElement(v, rws.declareTrailer)
  4467  		}
  4468  
  4469  		endStream := (rws.handlerDone && !rws.hasTrailers() && len(p) == 0) || isHeadResp
  4470  		err = rws.conn.writeHeaders(rws.stream, &http2writeResHeaders{
  4471  			streamID:      rws.stream.id,
  4472  			httpResCode:   rws.status,
  4473  			h:             rws.snapHeader,
  4474  			endStream:     endStream,
  4475  			contentType:   ctype,
  4476  			contentLength: clen,
  4477  			date:          date,
  4478  		})
  4479  		if err != nil {
  4480  			return 0, err
  4481  		}
  4482  		if endStream {
  4483  			return 0, nil
  4484  		}
  4485  	}
  4486  	if isHeadResp {
  4487  		return len(p), nil
  4488  	}
  4489  	if len(p) == 0 && !rws.handlerDone {
  4490  		return 0, nil
  4491  	}
  4492  
  4493  	if rws.handlerDone {
  4494  		rws.promoteUndeclaredTrailers()
  4495  	}
  4496  
  4497  	endStream := rws.handlerDone && !rws.hasTrailers()
  4498  	if len(p) > 0 || endStream {
  4499  
  4500  		if err := rws.conn.writeDataFromHandler(rws.stream, p, endStream); err != nil {
  4501  			return 0, err
  4502  		}
  4503  	}
  4504  
  4505  	if rws.handlerDone && rws.hasTrailers() {
  4506  		err = rws.conn.writeHeaders(rws.stream, &http2writeResHeaders{
  4507  			streamID:  rws.stream.id,
  4508  			h:         rws.handlerHeader,
  4509  			trailers:  rws.trailers,
  4510  			endStream: true,
  4511  		})
  4512  		return len(p), err
  4513  	}
  4514  	return len(p), nil
  4515  }
  4516  
  4517  // TrailerPrefix is a magic prefix for ResponseWriter.Header map keys
  4518  // that, if present, signals that the map entry is actually for
  4519  // the response trailers, and not the response headers. The prefix
  4520  // is stripped after the ServeHTTP call finishes and the values are
  4521  // sent in the trailers.
  4522  //
  4523  // This mechanism is intended only for trailers that are not known
  4524  // prior to the headers being written. If the set of trailers is fixed
  4525  // or known before the header is written, the normal Go trailers mechanism
  4526  // is preferred:
  4527  //    https://golang.org/pkg/net/http/#ResponseWriter
  4528  //    https://golang.org/pkg/net/http/#example_ResponseWriter_trailers
  4529  const http2TrailerPrefix = "Trailer:"
  4530  
  4531  // promoteUndeclaredTrailers permits http.Handlers to set trailers
  4532  // after the header has already been flushed. Because the Go
  4533  // ResponseWriter interface has no way to set Trailers (only the
  4534  // Header), and because we didn't want to expand the ResponseWriter
  4535  // interface, and because nobody used trailers, and because RFC 2616
  4536  // says you SHOULD (but not must) predeclare any trailers in the
  4537  // header, the official ResponseWriter rules said trailers in Go must
  4538  // be predeclared, and then we reuse the same ResponseWriter.Header()
  4539  // map to mean both Headers and Trailers.  When it's time to write the
  4540  // Trailers, we pick out the fields of Headers that were declared as
  4541  // trailers. That worked for a while, until we found the first major
  4542  // user of Trailers in the wild: gRPC (using them only over http2),
  4543  // and gRPC libraries permit setting trailers mid-stream without
  4544  // predeclarnig them. So: change of plans. We still permit the old
  4545  // way, but we also permit this hack: if a Header() key begins with
  4546  // "Trailer:", the suffix of that key is a Trailer. Because ':' is an
  4547  // invalid token byte anyway, there is no ambiguity. (And it's already
  4548  // filtered out) It's mildly hacky, but not terrible.
  4549  //
  4550  // This method runs after the Handler is done and promotes any Header
  4551  // fields to be trailers.
  4552  func (rws *http2responseWriterState) promoteUndeclaredTrailers() {
  4553  	for k, vv := range rws.handlerHeader {
  4554  		if !strings.HasPrefix(k, http2TrailerPrefix) {
  4555  			continue
  4556  		}
  4557  		trailerKey := strings.TrimPrefix(k, http2TrailerPrefix)
  4558  		rws.declareTrailer(trailerKey)
  4559  		rws.handlerHeader[CanonicalHeaderKey(trailerKey)] = vv
  4560  	}
  4561  
  4562  	if len(rws.trailers) > 1 {
  4563  		sorter := http2sorterPool.Get().(*http2sorter)
  4564  		sorter.SortStrings(rws.trailers)
  4565  		http2sorterPool.Put(sorter)
  4566  	}
  4567  }
  4568  
  4569  func (w *http2responseWriter) Flush() {
  4570  	rws := w.rws
  4571  	if rws == nil {
  4572  		panic("Header called after Handler finished")
  4573  	}
  4574  	if rws.bw.Buffered() > 0 {
  4575  		if err := rws.bw.Flush(); err != nil {
  4576  
  4577  			return
  4578  		}
  4579  	} else {
  4580  
  4581  		rws.writeChunk(nil)
  4582  	}
  4583  }
  4584  
  4585  func (w *http2responseWriter) CloseNotify() <-chan bool {
  4586  	rws := w.rws
  4587  	if rws == nil {
  4588  		panic("CloseNotify called after Handler finished")
  4589  	}
  4590  	rws.closeNotifierMu.Lock()
  4591  	ch := rws.closeNotifierCh
  4592  	if ch == nil {
  4593  		ch = make(chan bool, 1)
  4594  		rws.closeNotifierCh = ch
  4595  		go func() {
  4596  			rws.stream.cw.Wait()
  4597  			ch <- true
  4598  		}()
  4599  	}
  4600  	rws.closeNotifierMu.Unlock()
  4601  	return ch
  4602  }
  4603  
  4604  func (w *http2responseWriter) Header() Header {
  4605  	rws := w.rws
  4606  	if rws == nil {
  4607  		panic("Header called after Handler finished")
  4608  	}
  4609  	if rws.handlerHeader == nil {
  4610  		rws.handlerHeader = make(Header)
  4611  	}
  4612  	return rws.handlerHeader
  4613  }
  4614  
  4615  func (w *http2responseWriter) WriteHeader(code int) {
  4616  	rws := w.rws
  4617  	if rws == nil {
  4618  		panic("WriteHeader called after Handler finished")
  4619  	}
  4620  	rws.writeHeader(code)
  4621  }
  4622  
  4623  func (rws *http2responseWriterState) writeHeader(code int) {
  4624  	if !rws.wroteHeader {
  4625  		rws.wroteHeader = true
  4626  		rws.status = code
  4627  		if len(rws.handlerHeader) > 0 {
  4628  			rws.snapHeader = http2cloneHeader(rws.handlerHeader)
  4629  		}
  4630  	}
  4631  }
  4632  
  4633  func http2cloneHeader(h Header) Header {
  4634  	h2 := make(Header, len(h))
  4635  	for k, vv := range h {
  4636  		vv2 := make([]string, len(vv))
  4637  		copy(vv2, vv)
  4638  		h2[k] = vv2
  4639  	}
  4640  	return h2
  4641  }
  4642  
  4643  // The Life Of A Write is like this:
  4644  //
  4645  // * Handler calls w.Write or w.WriteString ->
  4646  // * -> rws.bw (*bufio.Writer) ->
  4647  // * (Handler migth call Flush)
  4648  // * -> chunkWriter{rws}
  4649  // * -> responseWriterState.writeChunk(p []byte)
  4650  // * -> responseWriterState.writeChunk (most of the magic; see comment there)
  4651  func (w *http2responseWriter) Write(p []byte) (n int, err error) {
  4652  	return w.write(len(p), p, "")
  4653  }
  4654  
  4655  func (w *http2responseWriter) WriteString(s string) (n int, err error) {
  4656  	return w.write(len(s), nil, s)
  4657  }
  4658  
  4659  // either dataB or dataS is non-zero.
  4660  func (w *http2responseWriter) write(lenData int, dataB []byte, dataS string) (n int, err error) {
  4661  	rws := w.rws
  4662  	if rws == nil {
  4663  		panic("Write called after Handler finished")
  4664  	}
  4665  	if !rws.wroteHeader {
  4666  		w.WriteHeader(200)
  4667  	}
  4668  	if !http2bodyAllowedForStatus(rws.status) {
  4669  		return 0, ErrBodyNotAllowed
  4670  	}
  4671  	rws.wroteBytes += int64(len(dataB)) + int64(len(dataS))
  4672  	if rws.sentContentLen != 0 && rws.wroteBytes > rws.sentContentLen {
  4673  
  4674  		return 0, errors.New("http2: handler wrote more than declared Content-Length")
  4675  	}
  4676  
  4677  	if dataB != nil {
  4678  		return rws.bw.Write(dataB)
  4679  	} else {
  4680  		return rws.bw.WriteString(dataS)
  4681  	}
  4682  }
  4683  
  4684  func (w *http2responseWriter) handlerDone() {
  4685  	rws := w.rws
  4686  	rws.handlerDone = true
  4687  	w.Flush()
  4688  	w.rws = nil
  4689  	http2responseWriterStatePool.Put(rws)
  4690  }
  4691  
  4692  // foreachHeaderElement splits v according to the "#rule" construction
  4693  // in RFC 2616 section 2.1 and calls fn for each non-empty element.
  4694  func http2foreachHeaderElement(v string, fn func(string)) {
  4695  	v = textproto.TrimString(v)
  4696  	if v == "" {
  4697  		return
  4698  	}
  4699  	if !strings.Contains(v, ",") {
  4700  		fn(v)
  4701  		return
  4702  	}
  4703  	for _, f := range strings.Split(v, ",") {
  4704  		if f = textproto.TrimString(f); f != "" {
  4705  			fn(f)
  4706  		}
  4707  	}
  4708  }
  4709  
  4710  const (
  4711  	// transportDefaultConnFlow is how many connection-level flow control
  4712  	// tokens we give the server at start-up, past the default 64k.
  4713  	http2transportDefaultConnFlow = 1 << 30
  4714  
  4715  	// transportDefaultStreamFlow is how many stream-level flow
  4716  	// control tokens we announce to the peer, and how many bytes
  4717  	// we buffer per stream.
  4718  	http2transportDefaultStreamFlow = 4 << 20
  4719  
  4720  	// transportDefaultStreamMinRefresh is the minimum number of bytes we'll send
  4721  	// a stream-level WINDOW_UPDATE for at a time.
  4722  	http2transportDefaultStreamMinRefresh = 4 << 10
  4723  
  4724  	http2defaultUserAgent = "Go-http-client/2.0"
  4725  )
  4726  
  4727  // Transport is an HTTP/2 Transport.
  4728  //
  4729  // A Transport internally caches connections to servers. It is safe
  4730  // for concurrent use by multiple goroutines.
  4731  type http2Transport struct {
  4732  	// DialTLS specifies an optional dial function for creating
  4733  	// TLS connections for requests.
  4734  	//
  4735  	// If DialTLS is nil, tls.Dial is used.
  4736  	//
  4737  	// If the returned net.Conn has a ConnectionState method like tls.Conn,
  4738  	// it will be used to set http.Response.TLS.
  4739  	DialTLS func(network, addr string, cfg *tls.Config) (net.Conn, error)
  4740  
  4741  	// TLSClientConfig specifies the TLS configuration to use with
  4742  	// tls.Client. If nil, the default configuration is used.
  4743  	TLSClientConfig *tls.Config
  4744  
  4745  	// ConnPool optionally specifies an alternate connection pool to use.
  4746  	// If nil, the default is used.
  4747  	ConnPool http2ClientConnPool
  4748  
  4749  	// DisableCompression, if true, prevents the Transport from
  4750  	// requesting compression with an "Accept-Encoding: gzip"
  4751  	// request header when the Request contains no existing
  4752  	// Accept-Encoding value. If the Transport requests gzip on
  4753  	// its own and gets a gzipped response, it's transparently
  4754  	// decoded in the Response.Body. However, if the user
  4755  	// explicitly requested gzip it is not automatically
  4756  	// uncompressed.
  4757  	DisableCompression bool
  4758  
  4759  	// MaxHeaderListSize is the http2 SETTINGS_MAX_HEADER_LIST_SIZE to
  4760  	// send in the initial settings frame. It is how many bytes
  4761  	// of response headers are allow. Unlike the http2 spec, zero here
  4762  	// means to use a default limit (currently 10MB). If you actually
  4763  	// want to advertise an ulimited value to the peer, Transport
  4764  	// interprets the highest possible value here (0xffffffff or 1<<32-1)
  4765  	// to mean no limit.
  4766  	MaxHeaderListSize uint32
  4767  
  4768  	// t1, if non-nil, is the standard library Transport using
  4769  	// this transport. Its settings are used (but not its
  4770  	// RoundTrip method, etc).
  4771  	t1 *Transport
  4772  
  4773  	connPoolOnce  sync.Once
  4774  	connPoolOrDef http2ClientConnPool // non-nil version of ConnPool
  4775  }
  4776  
  4777  func (t *http2Transport) maxHeaderListSize() uint32 {
  4778  	if t.MaxHeaderListSize == 0 {
  4779  		return 10 << 20
  4780  	}
  4781  	if t.MaxHeaderListSize == 0xffffffff {
  4782  		return 0
  4783  	}
  4784  	return t.MaxHeaderListSize
  4785  }
  4786  
  4787  func (t *http2Transport) disableCompression() bool {
  4788  	return t.DisableCompression || (t.t1 != nil && t.t1.DisableCompression)
  4789  }
  4790  
  4791  var http2errTransportVersion = errors.New("http2: ConfigureTransport is only supported starting at Go 1.6")
  4792  
  4793  // ConfigureTransport configures a net/http HTTP/1 Transport to use HTTP/2.
  4794  // It requires Go 1.6 or later and returns an error if the net/http package is too old
  4795  // or if t1 has already been HTTP/2-enabled.
  4796  func http2ConfigureTransport(t1 *Transport) error {
  4797  	_, err := http2configureTransport(t1)
  4798  	return err
  4799  }
  4800  
  4801  func (t *http2Transport) connPool() http2ClientConnPool {
  4802  	t.connPoolOnce.Do(t.initConnPool)
  4803  	return t.connPoolOrDef
  4804  }
  4805  
  4806  func (t *http2Transport) initConnPool() {
  4807  	if t.ConnPool != nil {
  4808  		t.connPoolOrDef = t.ConnPool
  4809  	} else {
  4810  		t.connPoolOrDef = &http2clientConnPool{t: t}
  4811  	}
  4812  }
  4813  
  4814  // ClientConn is the state of a single HTTP/2 client connection to an
  4815  // HTTP/2 server.
  4816  type http2ClientConn struct {
  4817  	t        *http2Transport
  4818  	tconn    net.Conn             // usually *tls.Conn, except specialized impls
  4819  	tlsState *tls.ConnectionState // nil only for specialized impls
  4820  
  4821  	// readLoop goroutine fields:
  4822  	readerDone chan struct{} // closed on error
  4823  	readerErr  error         // set before readerDone is closed
  4824  
  4825  	mu           sync.Mutex // guards following
  4826  	cond         *sync.Cond // hold mu; broadcast on flow/closed changes
  4827  	flow         http2flow  // our conn-level flow control quota (cs.flow is per stream)
  4828  	inflow       http2flow  // peer's conn-level flow control
  4829  	closed       bool
  4830  	goAway       *http2GoAwayFrame             // if non-nil, the GoAwayFrame we received
  4831  	streams      map[uint32]*http2clientStream // client-initiated
  4832  	nextStreamID uint32
  4833  	bw           *bufio.Writer
  4834  	br           *bufio.Reader
  4835  	fr           *http2Framer
  4836  	// Settings from peer:
  4837  	maxFrameSize         uint32
  4838  	maxConcurrentStreams uint32
  4839  	initialWindowSize    uint32
  4840  	hbuf                 bytes.Buffer // HPACK encoder writes into this
  4841  	henc                 *hpack.Encoder
  4842  	freeBuf              [][]byte
  4843  
  4844  	wmu  sync.Mutex // held while writing; acquire AFTER mu if holding both
  4845  	werr error      // first write error that has occurred
  4846  }
  4847  
  4848  // clientStream is the state for a single HTTP/2 stream. One of these
  4849  // is created for each Transport.RoundTrip call.
  4850  type http2clientStream struct {
  4851  	cc            *http2ClientConn
  4852  	req           *Request
  4853  	ID            uint32
  4854  	resc          chan http2resAndError
  4855  	bufPipe       http2pipe // buffered pipe with the flow-controlled response payload
  4856  	requestedGzip bool
  4857  
  4858  	flow        http2flow // guarded by cc.mu
  4859  	inflow      http2flow // guarded by cc.mu
  4860  	bytesRemain int64     // -1 means unknown; owned by transportResponseBody.Read
  4861  	readErr     error     // sticky read error; owned by transportResponseBody.Read
  4862  	stopReqBody error     // if non-nil, stop writing req body; guarded by cc.mu
  4863  
  4864  	peerReset chan struct{} // closed on peer reset
  4865  	resetErr  error         // populated before peerReset is closed
  4866  
  4867  	done chan struct{} // closed when stream remove from cc.streams map; close calls guarded by cc.mu
  4868  
  4869  	// owned by clientConnReadLoop:
  4870  	pastHeaders  bool // got first MetaHeadersFrame (actual headers)
  4871  	pastTrailers bool // got optional second MetaHeadersFrame (trailers)
  4872  
  4873  	trailer    Header  // accumulated trailers
  4874  	resTrailer *Header // client's Response.Trailer
  4875  }
  4876  
  4877  // awaitRequestCancel runs in its own goroutine and waits for the user
  4878  // to either cancel a RoundTrip request (using the provided
  4879  // Request.Cancel channel), or for the request to be done (any way it
  4880  // might be removed from the cc.streams map: peer reset, successful
  4881  // completion, TCP connection breakage, etc)
  4882  func (cs *http2clientStream) awaitRequestCancel(cancel <-chan struct{}) {
  4883  	if cancel == nil {
  4884  		return
  4885  	}
  4886  	select {
  4887  	case <-cancel:
  4888  		cs.bufPipe.CloseWithError(http2errRequestCanceled)
  4889  		cs.cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil)
  4890  	case <-cs.done:
  4891  	}
  4892  }
  4893  
  4894  // checkReset reports any error sent in a RST_STREAM frame by the
  4895  // server.
  4896  func (cs *http2clientStream) checkReset() error {
  4897  	select {
  4898  	case <-cs.peerReset:
  4899  		return cs.resetErr
  4900  	default:
  4901  		return nil
  4902  	}
  4903  }
  4904  
  4905  func (cs *http2clientStream) abortRequestBodyWrite(err error) {
  4906  	if err == nil {
  4907  		panic("nil error")
  4908  	}
  4909  	cc := cs.cc
  4910  	cc.mu.Lock()
  4911  	cs.stopReqBody = err
  4912  	cc.cond.Broadcast()
  4913  	cc.mu.Unlock()
  4914  }
  4915  
  4916  type http2stickyErrWriter struct {
  4917  	w   io.Writer
  4918  	err *error
  4919  }
  4920  
  4921  func (sew http2stickyErrWriter) Write(p []byte) (n int, err error) {
  4922  	if *sew.err != nil {
  4923  		return 0, *sew.err
  4924  	}
  4925  	n, err = sew.w.Write(p)
  4926  	*sew.err = err
  4927  	return
  4928  }
  4929  
  4930  var http2ErrNoCachedConn = errors.New("http2: no cached connection was available")
  4931  
  4932  // RoundTripOpt are options for the Transport.RoundTripOpt method.
  4933  type http2RoundTripOpt struct {
  4934  	// OnlyCachedConn controls whether RoundTripOpt may
  4935  	// create a new TCP connection. If set true and
  4936  	// no cached connection is available, RoundTripOpt
  4937  	// will return ErrNoCachedConn.
  4938  	OnlyCachedConn bool
  4939  }
  4940  
  4941  func (t *http2Transport) RoundTrip(req *Request) (*Response, error) {
  4942  	return t.RoundTripOpt(req, http2RoundTripOpt{})
  4943  }
  4944  
  4945  // authorityAddr returns a given authority (a host/IP, or host:port / ip:port)
  4946  // and returns a host:port. The port 443 is added if needed.
  4947  func http2authorityAddr(authority string) (addr string) {
  4948  	if _, _, err := net.SplitHostPort(authority); err == nil {
  4949  		return authority
  4950  	}
  4951  	return net.JoinHostPort(authority, "443")
  4952  }
  4953  
  4954  // RoundTripOpt is like RoundTrip, but takes options.
  4955  func (t *http2Transport) RoundTripOpt(req *Request, opt http2RoundTripOpt) (*Response, error) {
  4956  	if req.URL.Scheme != "https" {
  4957  		return nil, errors.New("http2: unsupported scheme")
  4958  	}
  4959  
  4960  	addr := http2authorityAddr(req.URL.Host)
  4961  	for {
  4962  		cc, err := t.connPool().GetClientConn(req, addr)
  4963  		if err != nil {
  4964  			t.vlogf("http2: Transport failed to get client conn for %s: %v", addr, err)
  4965  			return nil, err
  4966  		}
  4967  		res, err := cc.RoundTrip(req)
  4968  		if http2shouldRetryRequest(req, err) {
  4969  			continue
  4970  		}
  4971  		if err != nil {
  4972  			t.vlogf("RoundTrip failure: %v", err)
  4973  			return nil, err
  4974  		}
  4975  		return res, nil
  4976  	}
  4977  }
  4978  
  4979  // CloseIdleConnections closes any connections which were previously
  4980  // connected from previous requests but are now sitting idle.
  4981  // It does not interrupt any connections currently in use.
  4982  func (t *http2Transport) CloseIdleConnections() {
  4983  	if cp, ok := t.connPool().(*http2clientConnPool); ok {
  4984  		cp.closeIdleConnections()
  4985  	}
  4986  }
  4987  
  4988  var (
  4989  	http2errClientConnClosed   = errors.New("http2: client conn is closed")
  4990  	http2errClientConnUnusable = errors.New("http2: client conn not usable")
  4991  )
  4992  
  4993  func http2shouldRetryRequest(req *Request, err error) bool {
  4994  
  4995  	return err == http2errClientConnUnusable
  4996  }
  4997  
  4998  func (t *http2Transport) dialClientConn(addr string) (*http2ClientConn, error) {
  4999  	host, _, err := net.SplitHostPort(addr)
  5000  	if err != nil {
  5001  		return nil, err
  5002  	}
  5003  	tconn, err := t.dialTLS()("tcp", addr, t.newTLSConfig(host))
  5004  	if err != nil {
  5005  		return nil, err
  5006  	}
  5007  	return t.NewClientConn(tconn)
  5008  }
  5009  
  5010  func (t *http2Transport) newTLSConfig(host string) *tls.Config {
  5011  	cfg := new(tls.Config)
  5012  	if t.TLSClientConfig != nil {
  5013  		*cfg = *t.TLSClientConfig
  5014  	}
  5015  	if !http2strSliceContains(cfg.NextProtos, http2NextProtoTLS) {
  5016  		cfg.NextProtos = append([]string{http2NextProtoTLS}, cfg.NextProtos...)
  5017  	}
  5018  	if cfg.ServerName == "" {
  5019  		cfg.ServerName = host
  5020  	}
  5021  	return cfg
  5022  }
  5023  
  5024  func (t *http2Transport) dialTLS() func(string, string, *tls.Config) (net.Conn, error) {
  5025  	if t.DialTLS != nil {
  5026  		return t.DialTLS
  5027  	}
  5028  	return t.dialTLSDefault
  5029  }
  5030  
  5031  func (t *http2Transport) dialTLSDefault(network, addr string, cfg *tls.Config) (net.Conn, error) {
  5032  	cn, err := tls.Dial(network, addr, cfg)
  5033  	if err != nil {
  5034  		return nil, err
  5035  	}
  5036  	if err := cn.Handshake(); err != nil {
  5037  		return nil, err
  5038  	}
  5039  	if !cfg.InsecureSkipVerify {
  5040  		if err := cn.VerifyHostname(cfg.ServerName); err != nil {
  5041  			return nil, err
  5042  		}
  5043  	}
  5044  	state := cn.ConnectionState()
  5045  	if p := state.NegotiatedProtocol; p != http2NextProtoTLS {
  5046  		return nil, fmt.Errorf("http2: unexpected ALPN protocol %q; want %q", p, http2NextProtoTLS)
  5047  	}
  5048  	if !state.NegotiatedProtocolIsMutual {
  5049  		return nil, errors.New("http2: could not negotiate protocol mutually")
  5050  	}
  5051  	return cn, nil
  5052  }
  5053  
  5054  // disableKeepAlives reports whether connections should be closed as
  5055  // soon as possible after handling the first request.
  5056  func (t *http2Transport) disableKeepAlives() bool {
  5057  	return t.t1 != nil && t.t1.DisableKeepAlives
  5058  }
  5059  
  5060  func (t *http2Transport) NewClientConn(c net.Conn) (*http2ClientConn, error) {
  5061  	if http2VerboseLogs {
  5062  		t.vlogf("http2: Transport creating client conn to %v", c.RemoteAddr())
  5063  	}
  5064  	if _, err := c.Write(http2clientPreface); err != nil {
  5065  		t.vlogf("client preface write error: %v", err)
  5066  		return nil, err
  5067  	}
  5068  
  5069  	cc := &http2ClientConn{
  5070  		t:                    t,
  5071  		tconn:                c,
  5072  		readerDone:           make(chan struct{}),
  5073  		nextStreamID:         1,
  5074  		maxFrameSize:         16 << 10,
  5075  		initialWindowSize:    65535,
  5076  		maxConcurrentStreams: 1000,
  5077  		streams:              make(map[uint32]*http2clientStream),
  5078  	}
  5079  	cc.cond = sync.NewCond(&cc.mu)
  5080  	cc.flow.add(int32(http2initialWindowSize))
  5081  
  5082  	cc.bw = bufio.NewWriter(http2stickyErrWriter{c, &cc.werr})
  5083  	cc.br = bufio.NewReader(c)
  5084  	cc.fr = http2NewFramer(cc.bw, cc.br)
  5085  	cc.fr.ReadMetaHeaders = hpack.NewDecoder(http2initialHeaderTableSize, nil)
  5086  	cc.fr.MaxHeaderListSize = t.maxHeaderListSize()
  5087  
  5088  	cc.henc = hpack.NewEncoder(&cc.hbuf)
  5089  
  5090  	if cs, ok := c.(http2connectionStater); ok {
  5091  		state := cs.ConnectionState()
  5092  		cc.tlsState = &state
  5093  	}
  5094  
  5095  	initialSettings := []http2Setting{
  5096  		{ID: http2SettingEnablePush, Val: 0},
  5097  		{ID: http2SettingInitialWindowSize, Val: http2transportDefaultStreamFlow},
  5098  	}
  5099  	if max := t.maxHeaderListSize(); max != 0 {
  5100  		initialSettings = append(initialSettings, http2Setting{ID: http2SettingMaxHeaderListSize, Val: max})
  5101  	}
  5102  	cc.fr.WriteSettings(initialSettings...)
  5103  	cc.fr.WriteWindowUpdate(0, http2transportDefaultConnFlow)
  5104  	cc.inflow.add(http2transportDefaultConnFlow + http2initialWindowSize)
  5105  	cc.bw.Flush()
  5106  	if cc.werr != nil {
  5107  		return nil, cc.werr
  5108  	}
  5109  
  5110  	f, err := cc.fr.ReadFrame()
  5111  	if err != nil {
  5112  		return nil, err
  5113  	}
  5114  	sf, ok := f.(*http2SettingsFrame)
  5115  	if !ok {
  5116  		return nil, fmt.Errorf("expected settings frame, got: %T", f)
  5117  	}
  5118  	cc.fr.WriteSettingsAck()
  5119  	cc.bw.Flush()
  5120  
  5121  	sf.ForeachSetting(func(s http2Setting) error {
  5122  		switch s.ID {
  5123  		case http2SettingMaxFrameSize:
  5124  			cc.maxFrameSize = s.Val
  5125  		case http2SettingMaxConcurrentStreams:
  5126  			cc.maxConcurrentStreams = s.Val
  5127  		case http2SettingInitialWindowSize:
  5128  			cc.initialWindowSize = s.Val
  5129  		default:
  5130  
  5131  			t.vlogf("Unhandled Setting: %v", s)
  5132  		}
  5133  		return nil
  5134  	})
  5135  
  5136  	go cc.readLoop()
  5137  	return cc, nil
  5138  }
  5139  
  5140  func (cc *http2ClientConn) setGoAway(f *http2GoAwayFrame) {
  5141  	cc.mu.Lock()
  5142  	defer cc.mu.Unlock()
  5143  	cc.goAway = f
  5144  }
  5145  
  5146  func (cc *http2ClientConn) CanTakeNewRequest() bool {
  5147  	cc.mu.Lock()
  5148  	defer cc.mu.Unlock()
  5149  	return cc.canTakeNewRequestLocked()
  5150  }
  5151  
  5152  func (cc *http2ClientConn) canTakeNewRequestLocked() bool {
  5153  	return cc.goAway == nil && !cc.closed &&
  5154  		int64(len(cc.streams)+1) < int64(cc.maxConcurrentStreams) &&
  5155  		cc.nextStreamID < 2147483647
  5156  }
  5157  
  5158  func (cc *http2ClientConn) closeIfIdle() {
  5159  	cc.mu.Lock()
  5160  	if len(cc.streams) > 0 {
  5161  		cc.mu.Unlock()
  5162  		return
  5163  	}
  5164  	cc.closed = true
  5165  
  5166  	cc.mu.Unlock()
  5167  
  5168  	cc.tconn.Close()
  5169  }
  5170  
  5171  const http2maxAllocFrameSize = 512 << 10
  5172  
  5173  // frameBuffer returns a scratch buffer suitable for writing DATA frames.
  5174  // They're capped at the min of the peer's max frame size or 512KB
  5175  // (kinda arbitrarily), but definitely capped so we don't allocate 4GB
  5176  // bufers.
  5177  func (cc *http2ClientConn) frameScratchBuffer() []byte {
  5178  	cc.mu.Lock()
  5179  	size := cc.maxFrameSize
  5180  	if size > http2maxAllocFrameSize {
  5181  		size = http2maxAllocFrameSize
  5182  	}
  5183  	for i, buf := range cc.freeBuf {
  5184  		if len(buf) >= int(size) {
  5185  			cc.freeBuf[i] = nil
  5186  			cc.mu.Unlock()
  5187  			return buf[:size]
  5188  		}
  5189  	}
  5190  	cc.mu.Unlock()
  5191  	return make([]byte, size)
  5192  }
  5193  
  5194  func (cc *http2ClientConn) putFrameScratchBuffer(buf []byte) {
  5195  	cc.mu.Lock()
  5196  	defer cc.mu.Unlock()
  5197  	const maxBufs = 4 // arbitrary; 4 concurrent requests per conn? investigate.
  5198  	if len(cc.freeBuf) < maxBufs {
  5199  		cc.freeBuf = append(cc.freeBuf, buf)
  5200  		return
  5201  	}
  5202  	for i, old := range cc.freeBuf {
  5203  		if old == nil {
  5204  			cc.freeBuf[i] = buf
  5205  			return
  5206  		}
  5207  	}
  5208  
  5209  }
  5210  
  5211  // errRequestCanceled is a copy of net/http's errRequestCanceled because it's not
  5212  // exported. At least they'll be DeepEqual for h1-vs-h2 comparisons tests.
  5213  var http2errRequestCanceled = errors.New("net/http: request canceled")
  5214  
  5215  func http2commaSeparatedTrailers(req *Request) (string, error) {
  5216  	keys := make([]string, 0, len(req.Trailer))
  5217  	for k := range req.Trailer {
  5218  		k = CanonicalHeaderKey(k)
  5219  		switch k {
  5220  		case "Transfer-Encoding", "Trailer", "Content-Length":
  5221  			return "", &http2badStringError{"invalid Trailer key", k}
  5222  		}
  5223  		keys = append(keys, k)
  5224  	}
  5225  	if len(keys) > 0 {
  5226  		sort.Strings(keys)
  5227  
  5228  		return strings.Join(keys, ","), nil
  5229  	}
  5230  	return "", nil
  5231  }
  5232  
  5233  func (cc *http2ClientConn) responseHeaderTimeout() time.Duration {
  5234  	if cc.t.t1 != nil {
  5235  		return cc.t.t1.ResponseHeaderTimeout
  5236  	}
  5237  
  5238  	return 0
  5239  }
  5240  
  5241  // checkConnHeaders checks whether req has any invalid connection-level headers.
  5242  // per RFC 7540 section 8.1.2.2: Connection-Specific Header Fields.
  5243  // Certain headers are special-cased as okay but not transmitted later.
  5244  func http2checkConnHeaders(req *Request) error {
  5245  	if v := req.Header.Get("Upgrade"); v != "" {
  5246  		return errors.New("http2: invalid Upgrade request header")
  5247  	}
  5248  	if v := req.Header.Get("Transfer-Encoding"); (v != "" && v != "chunked") || len(req.Header["Transfer-Encoding"]) > 1 {
  5249  		return errors.New("http2: invalid Transfer-Encoding request header")
  5250  	}
  5251  	if v := req.Header.Get("Connection"); (v != "" && v != "close" && v != "keep-alive") || len(req.Header["Connection"]) > 1 {
  5252  		return errors.New("http2: invalid Connection request header")
  5253  	}
  5254  	return nil
  5255  }
  5256  
  5257  func (cc *http2ClientConn) RoundTrip(req *Request) (*Response, error) {
  5258  	if err := http2checkConnHeaders(req); err != nil {
  5259  		return nil, err
  5260  	}
  5261  
  5262  	trailers, err := http2commaSeparatedTrailers(req)
  5263  	if err != nil {
  5264  		return nil, err
  5265  	}
  5266  	hasTrailers := trailers != ""
  5267  
  5268  	var body io.Reader = req.Body
  5269  	contentLen := req.ContentLength
  5270  	if req.Body != nil && contentLen == 0 {
  5271  		// Test to see if it's actually zero or just unset.
  5272  		var buf [1]byte
  5273  		n, rerr := io.ReadFull(body, buf[:])
  5274  		if rerr != nil && rerr != io.EOF {
  5275  			contentLen = -1
  5276  			body = http2errorReader{rerr}
  5277  		} else if n == 1 {
  5278  
  5279  			contentLen = -1
  5280  			body = io.MultiReader(bytes.NewReader(buf[:]), body)
  5281  		} else {
  5282  
  5283  			body = nil
  5284  		}
  5285  	}
  5286  
  5287  	cc.mu.Lock()
  5288  	if cc.closed || !cc.canTakeNewRequestLocked() {
  5289  		cc.mu.Unlock()
  5290  		return nil, http2errClientConnUnusable
  5291  	}
  5292  
  5293  	cs := cc.newStream()
  5294  	cs.req = req
  5295  	hasBody := body != nil
  5296  
  5297  	if !cc.t.disableCompression() &&
  5298  		req.Header.Get("Accept-Encoding") == "" &&
  5299  		req.Header.Get("Range") == "" &&
  5300  		req.Method != "HEAD" {
  5301  
  5302  		cs.requestedGzip = true
  5303  	}
  5304  
  5305  	hdrs := cc.encodeHeaders(req, cs.requestedGzip, trailers, contentLen)
  5306  	cc.wmu.Lock()
  5307  	endStream := !hasBody && !hasTrailers
  5308  	werr := cc.writeHeaders(cs.ID, endStream, hdrs)
  5309  	cc.wmu.Unlock()
  5310  	cc.mu.Unlock()
  5311  
  5312  	if werr != nil {
  5313  		if hasBody {
  5314  			req.Body.Close()
  5315  		}
  5316  		cc.forgetStreamID(cs.ID)
  5317  
  5318  		return nil, werr
  5319  	}
  5320  
  5321  	var respHeaderTimer <-chan time.Time
  5322  	var bodyCopyErrc chan error // result of body copy
  5323  	if hasBody {
  5324  		bodyCopyErrc = make(chan error, 1)
  5325  		go func() {
  5326  			bodyCopyErrc <- cs.writeRequestBody(body, req.Body)
  5327  		}()
  5328  	} else {
  5329  		if d := cc.responseHeaderTimeout(); d != 0 {
  5330  			timer := time.NewTimer(d)
  5331  			defer timer.Stop()
  5332  			respHeaderTimer = timer.C
  5333  		}
  5334  	}
  5335  
  5336  	readLoopResCh := cs.resc
  5337  	requestCanceledCh := http2requestCancel(req)
  5338  	bodyWritten := false
  5339  
  5340  	for {
  5341  		select {
  5342  		case re := <-readLoopResCh:
  5343  			res := re.res
  5344  			if re.err != nil || res.StatusCode > 299 {
  5345  
  5346  				cs.abortRequestBodyWrite(http2errStopReqBodyWrite)
  5347  			}
  5348  			if re.err != nil {
  5349  				cc.forgetStreamID(cs.ID)
  5350  				return nil, re.err
  5351  			}
  5352  			res.Request = req
  5353  			res.TLS = cc.tlsState
  5354  			return res, nil
  5355  		case <-respHeaderTimer:
  5356  			cc.forgetStreamID(cs.ID)
  5357  			if !hasBody || bodyWritten {
  5358  				cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil)
  5359  			} else {
  5360  				cs.abortRequestBodyWrite(http2errStopReqBodyWriteAndCancel)
  5361  			}
  5362  			return nil, http2errTimeout
  5363  		case <-requestCanceledCh:
  5364  			cc.forgetStreamID(cs.ID)
  5365  			if !hasBody || bodyWritten {
  5366  				cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil)
  5367  			} else {
  5368  				cs.abortRequestBodyWrite(http2errStopReqBodyWriteAndCancel)
  5369  			}
  5370  			return nil, http2errRequestCanceled
  5371  		case <-cs.peerReset:
  5372  
  5373  			return nil, cs.resetErr
  5374  		case err := <-bodyCopyErrc:
  5375  			if err != nil {
  5376  				return nil, err
  5377  			}
  5378  			bodyWritten = true
  5379  			if d := cc.responseHeaderTimeout(); d != 0 {
  5380  				timer := time.NewTimer(d)
  5381  				defer timer.Stop()
  5382  				respHeaderTimer = timer.C
  5383  			}
  5384  		}
  5385  	}
  5386  }
  5387  
  5388  // requires cc.wmu be held
  5389  func (cc *http2ClientConn) writeHeaders(streamID uint32, endStream bool, hdrs []byte) error {
  5390  	first := true
  5391  	frameSize := int(cc.maxFrameSize)
  5392  	for len(hdrs) > 0 && cc.werr == nil {
  5393  		chunk := hdrs
  5394  		if len(chunk) > frameSize {
  5395  			chunk = chunk[:frameSize]
  5396  		}
  5397  		hdrs = hdrs[len(chunk):]
  5398  		endHeaders := len(hdrs) == 0
  5399  		if first {
  5400  			cc.fr.WriteHeaders(http2HeadersFrameParam{
  5401  				StreamID:      streamID,
  5402  				BlockFragment: chunk,
  5403  				EndStream:     endStream,
  5404  				EndHeaders:    endHeaders,
  5405  			})
  5406  			first = false
  5407  		} else {
  5408  			cc.fr.WriteContinuation(streamID, endHeaders, chunk)
  5409  		}
  5410  	}
  5411  
  5412  	cc.bw.Flush()
  5413  	return cc.werr
  5414  }
  5415  
  5416  // internal error values; they don't escape to callers
  5417  var (
  5418  	// abort request body write; don't send cancel
  5419  	http2errStopReqBodyWrite = errors.New("http2: aborting request body write")
  5420  
  5421  	// abort request body write, but send stream reset of cancel.
  5422  	http2errStopReqBodyWriteAndCancel = errors.New("http2: canceling request")
  5423  )
  5424  
  5425  func (cs *http2clientStream) writeRequestBody(body io.Reader, bodyCloser io.Closer) (err error) {
  5426  	cc := cs.cc
  5427  	sentEnd := false
  5428  	buf := cc.frameScratchBuffer()
  5429  	defer cc.putFrameScratchBuffer(buf)
  5430  
  5431  	defer func() {
  5432  
  5433  		cerr := bodyCloser.Close()
  5434  		if err == nil {
  5435  			err = cerr
  5436  		}
  5437  	}()
  5438  
  5439  	req := cs.req
  5440  	hasTrailers := req.Trailer != nil
  5441  
  5442  	var sawEOF bool
  5443  	for !sawEOF {
  5444  		n, err := body.Read(buf)
  5445  		if err == io.EOF {
  5446  			sawEOF = true
  5447  			err = nil
  5448  		} else if err != nil {
  5449  			return err
  5450  		}
  5451  
  5452  		remain := buf[:n]
  5453  		for len(remain) > 0 && err == nil {
  5454  			var allowed int32
  5455  			allowed, err = cs.awaitFlowControl(len(remain))
  5456  			switch {
  5457  			case err == http2errStopReqBodyWrite:
  5458  				return err
  5459  			case err == http2errStopReqBodyWriteAndCancel:
  5460  				cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil)
  5461  				return err
  5462  			case err != nil:
  5463  				return err
  5464  			}
  5465  			cc.wmu.Lock()
  5466  			data := remain[:allowed]
  5467  			remain = remain[allowed:]
  5468  			sentEnd = sawEOF && len(remain) == 0 && !hasTrailers
  5469  			err = cc.fr.WriteData(cs.ID, sentEnd, data)
  5470  			if err == nil {
  5471  
  5472  				err = cc.bw.Flush()
  5473  			}
  5474  			cc.wmu.Unlock()
  5475  		}
  5476  		if err != nil {
  5477  			return err
  5478  		}
  5479  	}
  5480  
  5481  	cc.wmu.Lock()
  5482  	if !sentEnd {
  5483  		var trls []byte
  5484  		if hasTrailers {
  5485  			cc.mu.Lock()
  5486  			trls = cc.encodeTrailers(req)
  5487  			cc.mu.Unlock()
  5488  		}
  5489  
  5490  		if len(trls) > 0 {
  5491  			err = cc.writeHeaders(cs.ID, true, trls)
  5492  		} else {
  5493  			err = cc.fr.WriteData(cs.ID, true, nil)
  5494  		}
  5495  	}
  5496  	if ferr := cc.bw.Flush(); ferr != nil && err == nil {
  5497  		err = ferr
  5498  	}
  5499  	cc.wmu.Unlock()
  5500  
  5501  	return err
  5502  }
  5503  
  5504  // awaitFlowControl waits for [1, min(maxBytes, cc.cs.maxFrameSize)] flow
  5505  // control tokens from the server.
  5506  // It returns either the non-zero number of tokens taken or an error
  5507  // if the stream is dead.
  5508  func (cs *http2clientStream) awaitFlowControl(maxBytes int) (taken int32, err error) {
  5509  	cc := cs.cc
  5510  	cc.mu.Lock()
  5511  	defer cc.mu.Unlock()
  5512  	for {
  5513  		if cc.closed {
  5514  			return 0, http2errClientConnClosed
  5515  		}
  5516  		if cs.stopReqBody != nil {
  5517  			return 0, cs.stopReqBody
  5518  		}
  5519  		if err := cs.checkReset(); err != nil {
  5520  			return 0, err
  5521  		}
  5522  		if a := cs.flow.available(); a > 0 {
  5523  			take := a
  5524  			if int(take) > maxBytes {
  5525  
  5526  				take = int32(maxBytes)
  5527  			}
  5528  			if take > int32(cc.maxFrameSize) {
  5529  				take = int32(cc.maxFrameSize)
  5530  			}
  5531  			cs.flow.take(take)
  5532  			return take, nil
  5533  		}
  5534  		cc.cond.Wait()
  5535  	}
  5536  }
  5537  
  5538  type http2badStringError struct {
  5539  	what string
  5540  	str  string
  5541  }
  5542  
  5543  func (e *http2badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) }
  5544  
  5545  // requires cc.mu be held.
  5546  func (cc *http2ClientConn) encodeHeaders(req *Request, addGzipHeader bool, trailers string, contentLength int64) []byte {
  5547  	cc.hbuf.Reset()
  5548  
  5549  	host := req.Host
  5550  	if host == "" {
  5551  		host = req.URL.Host
  5552  	}
  5553  
  5554  	cc.writeHeader(":authority", host)
  5555  	cc.writeHeader(":method", req.Method)
  5556  	if req.Method != "CONNECT" {
  5557  		cc.writeHeader(":path", req.URL.RequestURI())
  5558  		cc.writeHeader(":scheme", "https")
  5559  	}
  5560  	if trailers != "" {
  5561  		cc.writeHeader("trailer", trailers)
  5562  	}
  5563  
  5564  	var didUA bool
  5565  	for k, vv := range req.Header {
  5566  		lowKey := strings.ToLower(k)
  5567  		switch lowKey {
  5568  		case "host", "content-length":
  5569  
  5570  			continue
  5571  		case "connection", "proxy-connection", "transfer-encoding", "upgrade":
  5572  
  5573  			continue
  5574  		case "user-agent":
  5575  
  5576  			didUA = true
  5577  			if len(vv) < 1 {
  5578  				continue
  5579  			}
  5580  			vv = vv[:1]
  5581  			if vv[0] == "" {
  5582  				continue
  5583  			}
  5584  		}
  5585  		for _, v := range vv {
  5586  			cc.writeHeader(lowKey, v)
  5587  		}
  5588  	}
  5589  	if http2shouldSendReqContentLength(req.Method, contentLength) {
  5590  		cc.writeHeader("content-length", strconv.FormatInt(contentLength, 10))
  5591  	}
  5592  	if addGzipHeader {
  5593  		cc.writeHeader("accept-encoding", "gzip")
  5594  	}
  5595  	if !didUA {
  5596  		cc.writeHeader("user-agent", http2defaultUserAgent)
  5597  	}
  5598  	return cc.hbuf.Bytes()
  5599  }
  5600  
  5601  // shouldSendReqContentLength reports whether the http2.Transport should send
  5602  // a "content-length" request header. This logic is basically a copy of the net/http
  5603  // transferWriter.shouldSendContentLength.
  5604  // The contentLength is the corrected contentLength (so 0 means actually 0, not unknown).
  5605  // -1 means unknown.
  5606  func http2shouldSendReqContentLength(method string, contentLength int64) bool {
  5607  	if contentLength > 0 {
  5608  		return true
  5609  	}
  5610  	if contentLength < 0 {
  5611  		return false
  5612  	}
  5613  
  5614  	switch method {
  5615  	case "POST", "PUT", "PATCH":
  5616  		return true
  5617  	default:
  5618  		return false
  5619  	}
  5620  }
  5621  
  5622  // requires cc.mu be held.
  5623  func (cc *http2ClientConn) encodeTrailers(req *Request) []byte {
  5624  	cc.hbuf.Reset()
  5625  	for k, vv := range req.Trailer {
  5626  
  5627  		lowKey := strings.ToLower(k)
  5628  		for _, v := range vv {
  5629  			cc.writeHeader(lowKey, v)
  5630  		}
  5631  	}
  5632  	return cc.hbuf.Bytes()
  5633  }
  5634  
  5635  func (cc *http2ClientConn) writeHeader(name, value string) {
  5636  	if http2VerboseLogs {
  5637  		log.Printf("http2: Transport encoding header %q = %q", name, value)
  5638  	}
  5639  	cc.henc.WriteField(hpack.HeaderField{Name: name, Value: value})
  5640  }
  5641  
  5642  type http2resAndError struct {
  5643  	res *Response
  5644  	err error
  5645  }
  5646  
  5647  // requires cc.mu be held.
  5648  func (cc *http2ClientConn) newStream() *http2clientStream {
  5649  	cs := &http2clientStream{
  5650  		cc:        cc,
  5651  		ID:        cc.nextStreamID,
  5652  		resc:      make(chan http2resAndError, 1),
  5653  		peerReset: make(chan struct{}),
  5654  		done:      make(chan struct{}),
  5655  	}
  5656  	cs.flow.add(int32(cc.initialWindowSize))
  5657  	cs.flow.setConnFlow(&cc.flow)
  5658  	cs.inflow.add(http2transportDefaultStreamFlow)
  5659  	cs.inflow.setConnFlow(&cc.inflow)
  5660  	cc.nextStreamID += 2
  5661  	cc.streams[cs.ID] = cs
  5662  	return cs
  5663  }
  5664  
  5665  func (cc *http2ClientConn) forgetStreamID(id uint32) {
  5666  	cc.streamByID(id, true)
  5667  }
  5668  
  5669  func (cc *http2ClientConn) streamByID(id uint32, andRemove bool) *http2clientStream {
  5670  	cc.mu.Lock()
  5671  	defer cc.mu.Unlock()
  5672  	cs := cc.streams[id]
  5673  	if andRemove && cs != nil && !cc.closed {
  5674  		delete(cc.streams, id)
  5675  		close(cs.done)
  5676  	}
  5677  	return cs
  5678  }
  5679  
  5680  // clientConnReadLoop is the state owned by the clientConn's frame-reading readLoop.
  5681  type http2clientConnReadLoop struct {
  5682  	cc            *http2ClientConn
  5683  	activeRes     map[uint32]*http2clientStream // keyed by streamID
  5684  	closeWhenIdle bool
  5685  }
  5686  
  5687  // readLoop runs in its own goroutine and reads and dispatches frames.
  5688  func (cc *http2ClientConn) readLoop() {
  5689  	rl := &http2clientConnReadLoop{
  5690  		cc:        cc,
  5691  		activeRes: make(map[uint32]*http2clientStream),
  5692  	}
  5693  
  5694  	defer rl.cleanup()
  5695  	cc.readerErr = rl.run()
  5696  	if ce, ok := cc.readerErr.(http2ConnectionError); ok {
  5697  		cc.wmu.Lock()
  5698  		cc.fr.WriteGoAway(0, http2ErrCode(ce), nil)
  5699  		cc.wmu.Unlock()
  5700  	}
  5701  }
  5702  
  5703  func (rl *http2clientConnReadLoop) cleanup() {
  5704  	cc := rl.cc
  5705  	defer cc.tconn.Close()
  5706  	defer cc.t.connPool().MarkDead(cc)
  5707  	defer close(cc.readerDone)
  5708  
  5709  	err := cc.readerErr
  5710  	if err == io.EOF {
  5711  		err = io.ErrUnexpectedEOF
  5712  	}
  5713  	cc.mu.Lock()
  5714  	for _, cs := range rl.activeRes {
  5715  		cs.bufPipe.CloseWithError(err)
  5716  	}
  5717  	for _, cs := range cc.streams {
  5718  		select {
  5719  		case cs.resc <- http2resAndError{err: err}:
  5720  		default:
  5721  		}
  5722  		close(cs.done)
  5723  	}
  5724  	cc.closed = true
  5725  	cc.cond.Broadcast()
  5726  	cc.mu.Unlock()
  5727  }
  5728  
  5729  func (rl *http2clientConnReadLoop) run() error {
  5730  	cc := rl.cc
  5731  	rl.closeWhenIdle = cc.t.disableKeepAlives()
  5732  	gotReply := false
  5733  	for {
  5734  		f, err := cc.fr.ReadFrame()
  5735  		if err != nil {
  5736  			cc.vlogf("Transport readFrame error: (%T) %v", err, err)
  5737  		}
  5738  		if se, ok := err.(http2StreamError); ok {
  5739  			if cs := cc.streamByID(se.StreamID, true); cs != nil {
  5740  				rl.endStreamError(cs, cc.fr.errDetail)
  5741  			}
  5742  			continue
  5743  		} else if err != nil {
  5744  			return err
  5745  		}
  5746  		if http2VerboseLogs {
  5747  			cc.vlogf("http2: Transport received %s", http2summarizeFrame(f))
  5748  		}
  5749  		maybeIdle := false
  5750  
  5751  		switch f := f.(type) {
  5752  		case *http2MetaHeadersFrame:
  5753  			err = rl.processHeaders(f)
  5754  			maybeIdle = true
  5755  			gotReply = true
  5756  		case *http2DataFrame:
  5757  			err = rl.processData(f)
  5758  			maybeIdle = true
  5759  		case *http2GoAwayFrame:
  5760  			err = rl.processGoAway(f)
  5761  			maybeIdle = true
  5762  		case *http2RSTStreamFrame:
  5763  			err = rl.processResetStream(f)
  5764  			maybeIdle = true
  5765  		case *http2SettingsFrame:
  5766  			err = rl.processSettings(f)
  5767  		case *http2PushPromiseFrame:
  5768  			err = rl.processPushPromise(f)
  5769  		case *http2WindowUpdateFrame:
  5770  			err = rl.processWindowUpdate(f)
  5771  		case *http2PingFrame:
  5772  			err = rl.processPing(f)
  5773  		default:
  5774  			cc.logf("Transport: unhandled response frame type %T", f)
  5775  		}
  5776  		if err != nil {
  5777  			return err
  5778  		}
  5779  		if rl.closeWhenIdle && gotReply && maybeIdle && len(rl.activeRes) == 0 {
  5780  			cc.closeIfIdle()
  5781  		}
  5782  	}
  5783  }
  5784  
  5785  func (rl *http2clientConnReadLoop) processHeaders(f *http2MetaHeadersFrame) error {
  5786  	cc := rl.cc
  5787  	cs := cc.streamByID(f.StreamID, f.StreamEnded())
  5788  	if cs == nil {
  5789  
  5790  		return nil
  5791  	}
  5792  	if !cs.pastHeaders {
  5793  		cs.pastHeaders = true
  5794  	} else {
  5795  		return rl.processTrailers(cs, f)
  5796  	}
  5797  
  5798  	res, err := rl.handleResponse(cs, f)
  5799  	if err != nil {
  5800  		if _, ok := err.(http2ConnectionError); ok {
  5801  			return err
  5802  		}
  5803  
  5804  		cs.cc.writeStreamReset(f.StreamID, http2ErrCodeProtocol, err)
  5805  		cs.resc <- http2resAndError{err: err}
  5806  		return nil
  5807  	}
  5808  	if res == nil {
  5809  
  5810  		return nil
  5811  	}
  5812  	if res.Body != http2noBody {
  5813  		rl.activeRes[cs.ID] = cs
  5814  	}
  5815  	cs.resTrailer = &res.Trailer
  5816  	cs.resc <- http2resAndError{res: res}
  5817  	return nil
  5818  }
  5819  
  5820  // may return error types nil, or ConnectionError. Any other error value
  5821  // is a StreamError of type ErrCodeProtocol. The returned error in that case
  5822  // is the detail.
  5823  //
  5824  // As a special case, handleResponse may return (nil, nil) to skip the
  5825  // frame (currently only used for 100 expect continue). This special
  5826  // case is going away after Issue 13851 is fixed.
  5827  func (rl *http2clientConnReadLoop) handleResponse(cs *http2clientStream, f *http2MetaHeadersFrame) (*Response, error) {
  5828  	if f.Truncated {
  5829  		return nil, http2errResponseHeaderListSize
  5830  	}
  5831  
  5832  	status := f.PseudoValue("status")
  5833  	if status == "" {
  5834  		return nil, errors.New("missing status pseudo header")
  5835  	}
  5836  	statusCode, err := strconv.Atoi(status)
  5837  	if err != nil {
  5838  		return nil, errors.New("malformed non-numeric status pseudo header")
  5839  	}
  5840  
  5841  	if statusCode == 100 {
  5842  
  5843  		cs.pastHeaders = false
  5844  		return nil, nil
  5845  	}
  5846  
  5847  	header := make(Header)
  5848  	res := &Response{
  5849  		Proto:      "HTTP/2.0",
  5850  		ProtoMajor: 2,
  5851  		Header:     header,
  5852  		StatusCode: statusCode,
  5853  		Status:     status + " " + StatusText(statusCode),
  5854  	}
  5855  	for _, hf := range f.RegularFields() {
  5856  		key := CanonicalHeaderKey(hf.Name)
  5857  		if key == "Trailer" {
  5858  			t := res.Trailer
  5859  			if t == nil {
  5860  				t = make(Header)
  5861  				res.Trailer = t
  5862  			}
  5863  			http2foreachHeaderElement(hf.Value, func(v string) {
  5864  				t[CanonicalHeaderKey(v)] = nil
  5865  			})
  5866  		} else {
  5867  			header[key] = append(header[key], hf.Value)
  5868  		}
  5869  	}
  5870  
  5871  	streamEnded := f.StreamEnded()
  5872  	isHead := cs.req.Method == "HEAD"
  5873  	if !streamEnded || isHead {
  5874  		res.ContentLength = -1
  5875  		if clens := res.Header["Content-Length"]; len(clens) == 1 {
  5876  			if clen64, err := strconv.ParseInt(clens[0], 10, 64); err == nil {
  5877  				res.ContentLength = clen64
  5878  			} else {
  5879  
  5880  			}
  5881  		} else if len(clens) > 1 {
  5882  
  5883  		}
  5884  	}
  5885  
  5886  	if streamEnded || isHead {
  5887  		res.Body = http2noBody
  5888  		return res, nil
  5889  	}
  5890  
  5891  	buf := new(bytes.Buffer)
  5892  	cs.bufPipe = http2pipe{b: buf}
  5893  	cs.bytesRemain = res.ContentLength
  5894  	res.Body = http2transportResponseBody{cs}
  5895  	go cs.awaitRequestCancel(http2requestCancel(cs.req))
  5896  
  5897  	if cs.requestedGzip && res.Header.Get("Content-Encoding") == "gzip" {
  5898  		res.Header.Del("Content-Encoding")
  5899  		res.Header.Del("Content-Length")
  5900  		res.ContentLength = -1
  5901  		res.Body = &http2gzipReader{body: res.Body}
  5902  	}
  5903  	return res, nil
  5904  }
  5905  
  5906  func (rl *http2clientConnReadLoop) processTrailers(cs *http2clientStream, f *http2MetaHeadersFrame) error {
  5907  	if cs.pastTrailers {
  5908  
  5909  		return http2ConnectionError(http2ErrCodeProtocol)
  5910  	}
  5911  	cs.pastTrailers = true
  5912  	if !f.StreamEnded() {
  5913  
  5914  		return http2ConnectionError(http2ErrCodeProtocol)
  5915  	}
  5916  	if len(f.PseudoFields()) > 0 {
  5917  
  5918  		return http2ConnectionError(http2ErrCodeProtocol)
  5919  	}
  5920  
  5921  	trailer := make(Header)
  5922  	for _, hf := range f.RegularFields() {
  5923  		key := CanonicalHeaderKey(hf.Name)
  5924  		trailer[key] = append(trailer[key], hf.Value)
  5925  	}
  5926  	cs.trailer = trailer
  5927  
  5928  	rl.endStream(cs)
  5929  	return nil
  5930  }
  5931  
  5932  // transportResponseBody is the concrete type of Transport.RoundTrip's
  5933  // Response.Body. It is an io.ReadCloser. On Read, it reads from cs.body.
  5934  // On Close it sends RST_STREAM if EOF wasn't already seen.
  5935  type http2transportResponseBody struct {
  5936  	cs *http2clientStream
  5937  }
  5938  
  5939  func (b http2transportResponseBody) Read(p []byte) (n int, err error) {
  5940  	cs := b.cs
  5941  	cc := cs.cc
  5942  
  5943  	if cs.readErr != nil {
  5944  		return 0, cs.readErr
  5945  	}
  5946  	n, err = b.cs.bufPipe.Read(p)
  5947  	if cs.bytesRemain != -1 {
  5948  		if int64(n) > cs.bytesRemain {
  5949  			n = int(cs.bytesRemain)
  5950  			if err == nil {
  5951  				err = errors.New("net/http: server replied with more than declared Content-Length; truncated")
  5952  				cc.writeStreamReset(cs.ID, http2ErrCodeProtocol, err)
  5953  			}
  5954  			cs.readErr = err
  5955  			return int(cs.bytesRemain), err
  5956  		}
  5957  		cs.bytesRemain -= int64(n)
  5958  		if err == io.EOF && cs.bytesRemain > 0 {
  5959  			err = io.ErrUnexpectedEOF
  5960  			cs.readErr = err
  5961  			return n, err
  5962  		}
  5963  	}
  5964  	if n == 0 {
  5965  
  5966  		return
  5967  	}
  5968  
  5969  	cc.mu.Lock()
  5970  	defer cc.mu.Unlock()
  5971  
  5972  	var connAdd, streamAdd int32
  5973  
  5974  	if v := cc.inflow.available(); v < http2transportDefaultConnFlow/2 {
  5975  		connAdd = http2transportDefaultConnFlow - v
  5976  		cc.inflow.add(connAdd)
  5977  	}
  5978  	if err == nil {
  5979  		if v := cs.inflow.available(); v < http2transportDefaultStreamFlow-http2transportDefaultStreamMinRefresh {
  5980  			streamAdd = http2transportDefaultStreamFlow - v
  5981  			cs.inflow.add(streamAdd)
  5982  		}
  5983  	}
  5984  	if connAdd != 0 || streamAdd != 0 {
  5985  		cc.wmu.Lock()
  5986  		defer cc.wmu.Unlock()
  5987  		if connAdd != 0 {
  5988  			cc.fr.WriteWindowUpdate(0, http2mustUint31(connAdd))
  5989  		}
  5990  		if streamAdd != 0 {
  5991  			cc.fr.WriteWindowUpdate(cs.ID, http2mustUint31(streamAdd))
  5992  		}
  5993  		cc.bw.Flush()
  5994  	}
  5995  	return
  5996  }
  5997  
  5998  var http2errClosedResponseBody = errors.New("http2: response body closed")
  5999  
  6000  func (b http2transportResponseBody) Close() error {
  6001  	cs := b.cs
  6002  	if cs.bufPipe.Err() != io.EOF {
  6003  
  6004  		cs.cc.writeStreamReset(cs.ID, http2ErrCodeCancel, nil)
  6005  	}
  6006  	cs.bufPipe.BreakWithError(http2errClosedResponseBody)
  6007  	return nil
  6008  }
  6009  
  6010  func (rl *http2clientConnReadLoop) processData(f *http2DataFrame) error {
  6011  	cc := rl.cc
  6012  	cs := cc.streamByID(f.StreamID, f.StreamEnded())
  6013  	if cs == nil {
  6014  		cc.mu.Lock()
  6015  		neverSent := cc.nextStreamID
  6016  		cc.mu.Unlock()
  6017  		if f.StreamID >= neverSent {
  6018  
  6019  			cc.logf("http2: Transport received unsolicited DATA frame; closing connection")
  6020  			return http2ConnectionError(http2ErrCodeProtocol)
  6021  		}
  6022  
  6023  		return nil
  6024  	}
  6025  	if data := f.Data(); len(data) > 0 {
  6026  		if cs.bufPipe.b == nil {
  6027  
  6028  			cc.logf("http2: Transport received DATA frame for closed stream; closing connection")
  6029  			return http2ConnectionError(http2ErrCodeProtocol)
  6030  		}
  6031  
  6032  		cc.mu.Lock()
  6033  		if cs.inflow.available() >= int32(len(data)) {
  6034  			cs.inflow.take(int32(len(data)))
  6035  		} else {
  6036  			cc.mu.Unlock()
  6037  			return http2ConnectionError(http2ErrCodeFlowControl)
  6038  		}
  6039  		cc.mu.Unlock()
  6040  
  6041  		if _, err := cs.bufPipe.Write(data); err != nil {
  6042  			rl.endStreamError(cs, err)
  6043  			return err
  6044  		}
  6045  	}
  6046  
  6047  	if f.StreamEnded() {
  6048  		rl.endStream(cs)
  6049  	}
  6050  	return nil
  6051  }
  6052  
  6053  var http2errInvalidTrailers = errors.New("http2: invalid trailers")
  6054  
  6055  func (rl *http2clientConnReadLoop) endStream(cs *http2clientStream) {
  6056  
  6057  	rl.endStreamError(cs, nil)
  6058  }
  6059  
  6060  func (rl *http2clientConnReadLoop) endStreamError(cs *http2clientStream, err error) {
  6061  	var code func()
  6062  	if err == nil {
  6063  		err = io.EOF
  6064  		code = cs.copyTrailers
  6065  	}
  6066  	cs.bufPipe.closeWithErrorAndCode(err, code)
  6067  	delete(rl.activeRes, cs.ID)
  6068  	if cs.req.Close || cs.req.Header.Get("Connection") == "close" {
  6069  		rl.closeWhenIdle = true
  6070  	}
  6071  }
  6072  
  6073  func (cs *http2clientStream) copyTrailers() {
  6074  	for k, vv := range cs.trailer {
  6075  		t := cs.resTrailer
  6076  		if *t == nil {
  6077  			*t = make(Header)
  6078  		}
  6079  		(*t)[k] = vv
  6080  	}
  6081  }
  6082  
  6083  func (rl *http2clientConnReadLoop) processGoAway(f *http2GoAwayFrame) error {
  6084  	cc := rl.cc
  6085  	cc.t.connPool().MarkDead(cc)
  6086  	if f.ErrCode != 0 {
  6087  
  6088  		cc.vlogf("transport got GOAWAY with error code = %v", f.ErrCode)
  6089  	}
  6090  	cc.setGoAway(f)
  6091  	return nil
  6092  }
  6093  
  6094  func (rl *http2clientConnReadLoop) processSettings(f *http2SettingsFrame) error {
  6095  	cc := rl.cc
  6096  	cc.mu.Lock()
  6097  	defer cc.mu.Unlock()
  6098  	return f.ForeachSetting(func(s http2Setting) error {
  6099  		switch s.ID {
  6100  		case http2SettingMaxFrameSize:
  6101  			cc.maxFrameSize = s.Val
  6102  		case http2SettingMaxConcurrentStreams:
  6103  			cc.maxConcurrentStreams = s.Val
  6104  		case http2SettingInitialWindowSize:
  6105  
  6106  			cc.initialWindowSize = s.Val
  6107  		default:
  6108  
  6109  			cc.vlogf("Unhandled Setting: %v", s)
  6110  		}
  6111  		return nil
  6112  	})
  6113  }
  6114  
  6115  func (rl *http2clientConnReadLoop) processWindowUpdate(f *http2WindowUpdateFrame) error {
  6116  	cc := rl.cc
  6117  	cs := cc.streamByID(f.StreamID, false)
  6118  	if f.StreamID != 0 && cs == nil {
  6119  		return nil
  6120  	}
  6121  
  6122  	cc.mu.Lock()
  6123  	defer cc.mu.Unlock()
  6124  
  6125  	fl := &cc.flow
  6126  	if cs != nil {
  6127  		fl = &cs.flow
  6128  	}
  6129  	if !fl.add(int32(f.Increment)) {
  6130  		return http2ConnectionError(http2ErrCodeFlowControl)
  6131  	}
  6132  	cc.cond.Broadcast()
  6133  	return nil
  6134  }
  6135  
  6136  func (rl *http2clientConnReadLoop) processResetStream(f *http2RSTStreamFrame) error {
  6137  	cs := rl.cc.streamByID(f.StreamID, true)
  6138  	if cs == nil {
  6139  
  6140  		return nil
  6141  	}
  6142  	select {
  6143  	case <-cs.peerReset:
  6144  
  6145  	default:
  6146  		err := http2StreamError{cs.ID, f.ErrCode}
  6147  		cs.resetErr = err
  6148  		close(cs.peerReset)
  6149  		cs.bufPipe.CloseWithError(err)
  6150  		cs.cc.cond.Broadcast()
  6151  	}
  6152  	delete(rl.activeRes, cs.ID)
  6153  	return nil
  6154  }
  6155  
  6156  func (rl *http2clientConnReadLoop) processPing(f *http2PingFrame) error {
  6157  	if f.IsAck() {
  6158  
  6159  		return nil
  6160  	}
  6161  	cc := rl.cc
  6162  	cc.wmu.Lock()
  6163  	defer cc.wmu.Unlock()
  6164  	if err := cc.fr.WritePing(true, f.Data); err != nil {
  6165  		return err
  6166  	}
  6167  	return cc.bw.Flush()
  6168  }
  6169  
  6170  func (rl *http2clientConnReadLoop) processPushPromise(f *http2PushPromiseFrame) error {
  6171  
  6172  	return http2ConnectionError(http2ErrCodeProtocol)
  6173  }
  6174  
  6175  func (cc *http2ClientConn) writeStreamReset(streamID uint32, code http2ErrCode, err error) {
  6176  
  6177  	cc.wmu.Lock()
  6178  	cc.fr.WriteRSTStream(streamID, code)
  6179  	cc.bw.Flush()
  6180  	cc.wmu.Unlock()
  6181  }
  6182  
  6183  var (
  6184  	http2errResponseHeaderListSize = errors.New("http2: response header list larger than advertised limit")
  6185  	http2errPseudoTrailers         = errors.New("http2: invalid pseudo header in trailers")
  6186  )
  6187  
  6188  func (cc *http2ClientConn) logf(format string, args ...interface{}) {
  6189  	cc.t.logf(format, args...)
  6190  }
  6191  
  6192  func (cc *http2ClientConn) vlogf(format string, args ...interface{}) {
  6193  	cc.t.vlogf(format, args...)
  6194  }
  6195  
  6196  func (t *http2Transport) vlogf(format string, args ...interface{}) {
  6197  	if http2VerboseLogs {
  6198  		t.logf(format, args...)
  6199  	}
  6200  }
  6201  
  6202  func (t *http2Transport) logf(format string, args ...interface{}) {
  6203  	log.Printf(format, args...)
  6204  }
  6205  
  6206  var http2noBody io.ReadCloser = ioutil.NopCloser(bytes.NewReader(nil))
  6207  
  6208  func http2strSliceContains(ss []string, s string) bool {
  6209  	for _, v := range ss {
  6210  		if v == s {
  6211  			return true
  6212  		}
  6213  	}
  6214  	return false
  6215  }
  6216  
  6217  type http2erringRoundTripper struct{ err error }
  6218  
  6219  func (rt http2erringRoundTripper) RoundTrip(*Request) (*Response, error) { return nil, rt.err }
  6220  
  6221  // gzipReader wraps a response body so it can lazily
  6222  // call gzip.NewReader on the first call to Read
  6223  type http2gzipReader struct {
  6224  	body io.ReadCloser // underlying Response.Body
  6225  	zr   *gzip.Reader  // lazily-initialized gzip reader
  6226  	zerr error         // sticky error
  6227  }
  6228  
  6229  func (gz *http2gzipReader) Read(p []byte) (n int, err error) {
  6230  	if gz.zerr != nil {
  6231  		return 0, gz.zerr
  6232  	}
  6233  	if gz.zr == nil {
  6234  		gz.zr, err = gzip.NewReader(gz.body)
  6235  		if err != nil {
  6236  			gz.zerr = err
  6237  			return 0, err
  6238  		}
  6239  	}
  6240  	return gz.zr.Read(p)
  6241  }
  6242  
  6243  func (gz *http2gzipReader) Close() error {
  6244  	return gz.body.Close()
  6245  }
  6246  
  6247  type http2errorReader struct{ err error }
  6248  
  6249  func (r http2errorReader) Read(p []byte) (int, error) { return 0, r.err }
  6250  
  6251  // writeFramer is implemented by any type that is used to write frames.
  6252  type http2writeFramer interface {
  6253  	writeFrame(http2writeContext) error
  6254  }
  6255  
  6256  // writeContext is the interface needed by the various frame writer
  6257  // types below. All the writeFrame methods below are scheduled via the
  6258  // frame writing scheduler (see writeScheduler in writesched.go).
  6259  //
  6260  // This interface is implemented by *serverConn.
  6261  //
  6262  // TODO: decide whether to a) use this in the client code (which didn't
  6263  // end up using this yet, because it has a simpler design, not
  6264  // currently implementing priorities), or b) delete this and
  6265  // make the server code a bit more concrete.
  6266  type http2writeContext interface {
  6267  	Framer() *http2Framer
  6268  	Flush() error
  6269  	CloseConn() error
  6270  	// HeaderEncoder returns an HPACK encoder that writes to the
  6271  	// returned buffer.
  6272  	HeaderEncoder() (*hpack.Encoder, *bytes.Buffer)
  6273  }
  6274  
  6275  // endsStream reports whether the given frame writer w will locally
  6276  // close the stream.
  6277  func http2endsStream(w http2writeFramer) bool {
  6278  	switch v := w.(type) {
  6279  	case *http2writeData:
  6280  		return v.endStream
  6281  	case *http2writeResHeaders:
  6282  		return v.endStream
  6283  	case nil:
  6284  
  6285  		panic("endsStream called on nil writeFramer")
  6286  	}
  6287  	return false
  6288  }
  6289  
  6290  type http2flushFrameWriter struct{}
  6291  
  6292  func (http2flushFrameWriter) writeFrame(ctx http2writeContext) error {
  6293  	return ctx.Flush()
  6294  }
  6295  
  6296  type http2writeSettings []http2Setting
  6297  
  6298  func (s http2writeSettings) writeFrame(ctx http2writeContext) error {
  6299  	return ctx.Framer().WriteSettings([]http2Setting(s)...)
  6300  }
  6301  
  6302  type http2writeGoAway struct {
  6303  	maxStreamID uint32
  6304  	code        http2ErrCode
  6305  }
  6306  
  6307  func (p *http2writeGoAway) writeFrame(ctx http2writeContext) error {
  6308  	err := ctx.Framer().WriteGoAway(p.maxStreamID, p.code, nil)
  6309  	if p.code != 0 {
  6310  		ctx.Flush()
  6311  		time.Sleep(50 * time.Millisecond)
  6312  		ctx.CloseConn()
  6313  	}
  6314  	return err
  6315  }
  6316  
  6317  type http2writeData struct {
  6318  	streamID  uint32
  6319  	p         []byte
  6320  	endStream bool
  6321  }
  6322  
  6323  func (w *http2writeData) String() string {
  6324  	return fmt.Sprintf("writeData(stream=%d, p=%d, endStream=%v)", w.streamID, len(w.p), w.endStream)
  6325  }
  6326  
  6327  func (w *http2writeData) writeFrame(ctx http2writeContext) error {
  6328  	return ctx.Framer().WriteData(w.streamID, w.endStream, w.p)
  6329  }
  6330  
  6331  // handlerPanicRST is the message sent from handler goroutines when
  6332  // the handler panics.
  6333  type http2handlerPanicRST struct {
  6334  	StreamID uint32
  6335  }
  6336  
  6337  func (hp http2handlerPanicRST) writeFrame(ctx http2writeContext) error {
  6338  	return ctx.Framer().WriteRSTStream(hp.StreamID, http2ErrCodeInternal)
  6339  }
  6340  
  6341  func (se http2StreamError) writeFrame(ctx http2writeContext) error {
  6342  	return ctx.Framer().WriteRSTStream(se.StreamID, se.Code)
  6343  }
  6344  
  6345  type http2writePingAck struct{ pf *http2PingFrame }
  6346  
  6347  func (w http2writePingAck) writeFrame(ctx http2writeContext) error {
  6348  	return ctx.Framer().WritePing(true, w.pf.Data)
  6349  }
  6350  
  6351  type http2writeSettingsAck struct{}
  6352  
  6353  func (http2writeSettingsAck) writeFrame(ctx http2writeContext) error {
  6354  	return ctx.Framer().WriteSettingsAck()
  6355  }
  6356  
  6357  // writeResHeaders is a request to write a HEADERS and 0+ CONTINUATION frames
  6358  // for HTTP response headers or trailers from a server handler.
  6359  type http2writeResHeaders struct {
  6360  	streamID    uint32
  6361  	httpResCode int      // 0 means no ":status" line
  6362  	h           Header   // may be nil
  6363  	trailers    []string // if non-nil, which keys of h to write. nil means all.
  6364  	endStream   bool
  6365  
  6366  	date          string
  6367  	contentType   string
  6368  	contentLength string
  6369  }
  6370  
  6371  func http2encKV(enc *hpack.Encoder, k, v string) {
  6372  	if http2VerboseLogs {
  6373  		log.Printf("http2: server encoding header %q = %q", k, v)
  6374  	}
  6375  	enc.WriteField(hpack.HeaderField{Name: k, Value: v})
  6376  }
  6377  
  6378  func (w *http2writeResHeaders) writeFrame(ctx http2writeContext) error {
  6379  	enc, buf := ctx.HeaderEncoder()
  6380  	buf.Reset()
  6381  
  6382  	if w.httpResCode != 0 {
  6383  		http2encKV(enc, ":status", http2httpCodeString(w.httpResCode))
  6384  	}
  6385  
  6386  	http2encodeHeaders(enc, w.h, w.trailers)
  6387  
  6388  	if w.contentType != "" {
  6389  		http2encKV(enc, "content-type", w.contentType)
  6390  	}
  6391  	if w.contentLength != "" {
  6392  		http2encKV(enc, "content-length", w.contentLength)
  6393  	}
  6394  	if w.date != "" {
  6395  		http2encKV(enc, "date", w.date)
  6396  	}
  6397  
  6398  	headerBlock := buf.Bytes()
  6399  	if len(headerBlock) == 0 && w.trailers == nil {
  6400  		panic("unexpected empty hpack")
  6401  	}
  6402  
  6403  	// For now we're lazy and just pick the minimum MAX_FRAME_SIZE
  6404  	// that all peers must support (16KB). Later we could care
  6405  	// more and send larger frames if the peer advertised it, but
  6406  	// there's little point. Most headers are small anyway (so we
  6407  	// generally won't have CONTINUATION frames), and extra frames
  6408  	// only waste 9 bytes anyway.
  6409  	const maxFrameSize = 16384
  6410  
  6411  	first := true
  6412  	for len(headerBlock) > 0 {
  6413  		frag := headerBlock
  6414  		if len(frag) > maxFrameSize {
  6415  			frag = frag[:maxFrameSize]
  6416  		}
  6417  		headerBlock = headerBlock[len(frag):]
  6418  		endHeaders := len(headerBlock) == 0
  6419  		var err error
  6420  		if first {
  6421  			first = false
  6422  			err = ctx.Framer().WriteHeaders(http2HeadersFrameParam{
  6423  				StreamID:      w.streamID,
  6424  				BlockFragment: frag,
  6425  				EndStream:     w.endStream,
  6426  				EndHeaders:    endHeaders,
  6427  			})
  6428  		} else {
  6429  			err = ctx.Framer().WriteContinuation(w.streamID, endHeaders, frag)
  6430  		}
  6431  		if err != nil {
  6432  			return err
  6433  		}
  6434  	}
  6435  	return nil
  6436  }
  6437  
  6438  type http2write100ContinueHeadersFrame struct {
  6439  	streamID uint32
  6440  }
  6441  
  6442  func (w http2write100ContinueHeadersFrame) writeFrame(ctx http2writeContext) error {
  6443  	enc, buf := ctx.HeaderEncoder()
  6444  	buf.Reset()
  6445  	http2encKV(enc, ":status", "100")
  6446  	return ctx.Framer().WriteHeaders(http2HeadersFrameParam{
  6447  		StreamID:      w.streamID,
  6448  		BlockFragment: buf.Bytes(),
  6449  		EndStream:     false,
  6450  		EndHeaders:    true,
  6451  	})
  6452  }
  6453  
  6454  type http2writeWindowUpdate struct {
  6455  	streamID uint32 // or 0 for conn-level
  6456  	n        uint32
  6457  }
  6458  
  6459  func (wu http2writeWindowUpdate) writeFrame(ctx http2writeContext) error {
  6460  	return ctx.Framer().WriteWindowUpdate(wu.streamID, wu.n)
  6461  }
  6462  
  6463  func http2encodeHeaders(enc *hpack.Encoder, h Header, keys []string) {
  6464  	if keys == nil {
  6465  		sorter := http2sorterPool.Get().(*http2sorter)
  6466  
  6467  		defer http2sorterPool.Put(sorter)
  6468  		keys = sorter.Keys(h)
  6469  	}
  6470  	for _, k := range keys {
  6471  		vv := h[k]
  6472  		k = http2lowerHeader(k)
  6473  		if !http2validHeaderFieldName(k) {
  6474  
  6475  			continue
  6476  		}
  6477  		isTE := k == "transfer-encoding"
  6478  		for _, v := range vv {
  6479  			if !http2validHeaderFieldValue(v) {
  6480  
  6481  				continue
  6482  			}
  6483  
  6484  			if isTE && v != "trailers" {
  6485  				continue
  6486  			}
  6487  			http2encKV(enc, k, v)
  6488  		}
  6489  	}
  6490  }
  6491  
  6492  // frameWriteMsg is a request to write a frame.
  6493  type http2frameWriteMsg struct {
  6494  	// write is the interface value that does the writing, once the
  6495  	// writeScheduler (below) has decided to select this frame
  6496  	// to write. The write functions are all defined in write.go.
  6497  	write http2writeFramer
  6498  
  6499  	stream *http2stream // used for prioritization. nil for non-stream frames.
  6500  
  6501  	// done, if non-nil, must be a buffered channel with space for
  6502  	// 1 message and is sent the return value from write (or an
  6503  	// earlier error) when the frame has been written.
  6504  	done chan error
  6505  }
  6506  
  6507  // for debugging only:
  6508  func (wm http2frameWriteMsg) String() string {
  6509  	var streamID uint32
  6510  	if wm.stream != nil {
  6511  		streamID = wm.stream.id
  6512  	}
  6513  	var des string
  6514  	if s, ok := wm.write.(fmt.Stringer); ok {
  6515  		des = s.String()
  6516  	} else {
  6517  		des = fmt.Sprintf("%T", wm.write)
  6518  	}
  6519  	return fmt.Sprintf("[frameWriteMsg stream=%d, ch=%v, type: %v]", streamID, wm.done != nil, des)
  6520  }
  6521  
  6522  // writeScheduler tracks pending frames to write, priorities, and decides
  6523  // the next one to use. It is not thread-safe.
  6524  type http2writeScheduler struct {
  6525  	// zero are frames not associated with a specific stream.
  6526  	// They're sent before any stream-specific freams.
  6527  	zero http2writeQueue
  6528  
  6529  	// maxFrameSize is the maximum size of a DATA frame
  6530  	// we'll write. Must be non-zero and between 16K-16M.
  6531  	maxFrameSize uint32
  6532  
  6533  	// sq contains the stream-specific queues, keyed by stream ID.
  6534  	// when a stream is idle, it's deleted from the map.
  6535  	sq map[uint32]*http2writeQueue
  6536  
  6537  	// canSend is a slice of memory that's reused between frame
  6538  	// scheduling decisions to hold the list of writeQueues (from sq)
  6539  	// which have enough flow control data to send. After canSend is
  6540  	// built, the best is selected.
  6541  	canSend []*http2writeQueue
  6542  
  6543  	// pool of empty queues for reuse.
  6544  	queuePool []*http2writeQueue
  6545  }
  6546  
  6547  func (ws *http2writeScheduler) putEmptyQueue(q *http2writeQueue) {
  6548  	if len(q.s) != 0 {
  6549  		panic("queue must be empty")
  6550  	}
  6551  	ws.queuePool = append(ws.queuePool, q)
  6552  }
  6553  
  6554  func (ws *http2writeScheduler) getEmptyQueue() *http2writeQueue {
  6555  	ln := len(ws.queuePool)
  6556  	if ln == 0 {
  6557  		return new(http2writeQueue)
  6558  	}
  6559  	q := ws.queuePool[ln-1]
  6560  	ws.queuePool = ws.queuePool[:ln-1]
  6561  	return q
  6562  }
  6563  
  6564  func (ws *http2writeScheduler) empty() bool { return ws.zero.empty() && len(ws.sq) == 0 }
  6565  
  6566  func (ws *http2writeScheduler) add(wm http2frameWriteMsg) {
  6567  	st := wm.stream
  6568  	if st == nil {
  6569  		ws.zero.push(wm)
  6570  	} else {
  6571  		ws.streamQueue(st.id).push(wm)
  6572  	}
  6573  }
  6574  
  6575  func (ws *http2writeScheduler) streamQueue(streamID uint32) *http2writeQueue {
  6576  	if q, ok := ws.sq[streamID]; ok {
  6577  		return q
  6578  	}
  6579  	if ws.sq == nil {
  6580  		ws.sq = make(map[uint32]*http2writeQueue)
  6581  	}
  6582  	q := ws.getEmptyQueue()
  6583  	ws.sq[streamID] = q
  6584  	return q
  6585  }
  6586  
  6587  // take returns the most important frame to write and removes it from the scheduler.
  6588  // It is illegal to call this if the scheduler is empty or if there are no connection-level
  6589  // flow control bytes available.
  6590  func (ws *http2writeScheduler) take() (wm http2frameWriteMsg, ok bool) {
  6591  	if ws.maxFrameSize == 0 {
  6592  		panic("internal error: ws.maxFrameSize not initialized or invalid")
  6593  	}
  6594  
  6595  	if !ws.zero.empty() {
  6596  		return ws.zero.shift(), true
  6597  	}
  6598  	if len(ws.sq) == 0 {
  6599  		return
  6600  	}
  6601  
  6602  	for id, q := range ws.sq {
  6603  		if q.firstIsNoCost() {
  6604  			return ws.takeFrom(id, q)
  6605  		}
  6606  	}
  6607  
  6608  	if len(ws.canSend) != 0 {
  6609  		panic("should be empty")
  6610  	}
  6611  	for _, q := range ws.sq {
  6612  		if n := ws.streamWritableBytes(q); n > 0 {
  6613  			ws.canSend = append(ws.canSend, q)
  6614  		}
  6615  	}
  6616  	if len(ws.canSend) == 0 {
  6617  		return
  6618  	}
  6619  	defer ws.zeroCanSend()
  6620  
  6621  	q := ws.canSend[0]
  6622  
  6623  	return ws.takeFrom(q.streamID(), q)
  6624  }
  6625  
  6626  // zeroCanSend is defered from take.
  6627  func (ws *http2writeScheduler) zeroCanSend() {
  6628  	for i := range ws.canSend {
  6629  		ws.canSend[i] = nil
  6630  	}
  6631  	ws.canSend = ws.canSend[:0]
  6632  }
  6633  
  6634  // streamWritableBytes returns the number of DATA bytes we could write
  6635  // from the given queue's stream, if this stream/queue were
  6636  // selected. It is an error to call this if q's head isn't a
  6637  // *writeData.
  6638  func (ws *http2writeScheduler) streamWritableBytes(q *http2writeQueue) int32 {
  6639  	wm := q.head()
  6640  	ret := wm.stream.flow.available()
  6641  	if ret == 0 {
  6642  		return 0
  6643  	}
  6644  	if int32(ws.maxFrameSize) < ret {
  6645  		ret = int32(ws.maxFrameSize)
  6646  	}
  6647  	if ret == 0 {
  6648  		panic("internal error: ws.maxFrameSize not initialized or invalid")
  6649  	}
  6650  	wd := wm.write.(*http2writeData)
  6651  	if len(wd.p) < int(ret) {
  6652  		ret = int32(len(wd.p))
  6653  	}
  6654  	return ret
  6655  }
  6656  
  6657  func (ws *http2writeScheduler) takeFrom(id uint32, q *http2writeQueue) (wm http2frameWriteMsg, ok bool) {
  6658  	wm = q.head()
  6659  
  6660  	if wd, ok := wm.write.(*http2writeData); ok && len(wd.p) > 0 {
  6661  		allowed := wm.stream.flow.available()
  6662  		if allowed == 0 {
  6663  
  6664  			return http2frameWriteMsg{}, false
  6665  		}
  6666  		if int32(ws.maxFrameSize) < allowed {
  6667  			allowed = int32(ws.maxFrameSize)
  6668  		}
  6669  
  6670  		if len(wd.p) > int(allowed) {
  6671  			wm.stream.flow.take(allowed)
  6672  			chunk := wd.p[:allowed]
  6673  			wd.p = wd.p[allowed:]
  6674  
  6675  			return http2frameWriteMsg{
  6676  				stream: wm.stream,
  6677  				write: &http2writeData{
  6678  					streamID: wd.streamID,
  6679  					p:        chunk,
  6680  
  6681  					endStream: false,
  6682  				},
  6683  
  6684  				done: nil,
  6685  			}, true
  6686  		}
  6687  		wm.stream.flow.take(int32(len(wd.p)))
  6688  	}
  6689  
  6690  	q.shift()
  6691  	if q.empty() {
  6692  		ws.putEmptyQueue(q)
  6693  		delete(ws.sq, id)
  6694  	}
  6695  	return wm, true
  6696  }
  6697  
  6698  func (ws *http2writeScheduler) forgetStream(id uint32) {
  6699  	q, ok := ws.sq[id]
  6700  	if !ok {
  6701  		return
  6702  	}
  6703  	delete(ws.sq, id)
  6704  
  6705  	for i := range q.s {
  6706  		q.s[i] = http2frameWriteMsg{}
  6707  	}
  6708  	q.s = q.s[:0]
  6709  	ws.putEmptyQueue(q)
  6710  }
  6711  
  6712  type http2writeQueue struct {
  6713  	s []http2frameWriteMsg
  6714  }
  6715  
  6716  // streamID returns the stream ID for a non-empty stream-specific queue.
  6717  func (q *http2writeQueue) streamID() uint32 { return q.s[0].stream.id }
  6718  
  6719  func (q *http2writeQueue) empty() bool { return len(q.s) == 0 }
  6720  
  6721  func (q *http2writeQueue) push(wm http2frameWriteMsg) {
  6722  	q.s = append(q.s, wm)
  6723  }
  6724  
  6725  // head returns the next item that would be removed by shift.
  6726  func (q *http2writeQueue) head() http2frameWriteMsg {
  6727  	if len(q.s) == 0 {
  6728  		panic("invalid use of queue")
  6729  	}
  6730  	return q.s[0]
  6731  }
  6732  
  6733  func (q *http2writeQueue) shift() http2frameWriteMsg {
  6734  	if len(q.s) == 0 {
  6735  		panic("invalid use of queue")
  6736  	}
  6737  	wm := q.s[0]
  6738  
  6739  	copy(q.s, q.s[1:])
  6740  	q.s[len(q.s)-1] = http2frameWriteMsg{}
  6741  	q.s = q.s[:len(q.s)-1]
  6742  	return wm
  6743  }
  6744  
  6745  func (q *http2writeQueue) firstIsNoCost() bool {
  6746  	if df, ok := q.s[0].write.(*http2writeData); ok {
  6747  		return len(df.p) == 0
  6748  	}
  6749  	return true
  6750  }