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