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