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