github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/src/net/http/h2_bundle.go (about)

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