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