github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/src/net/http/h2_bundle.go (about)

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