github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/src/net/http/h2_bundle.go (about)

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