1// Copyright 2015 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Transport code.
6
7package http2
8
9import (
10	"bufio"
11	"bytes"
12	"compress/gzip"
13	"context"
14	"crypto/rand"
15	"crypto/tls"
16	"errors"
17	"fmt"
18	"io"
19	"io/ioutil"
20	"log"
21	"math"
22	mathrand "math/rand"
23	"net"
24	"net/http"
25	"net/http/httptrace"
26	"net/textproto"
27	"sort"
28	"strconv"
29	"strings"
30	"sync"
31	"sync/atomic"
32	"time"
33
34	"golang.org/x/net/http/httpguts"
35	"golang.org/x/net/http2/hpack"
36	"golang.org/x/net/idna"
37)
38
39const (
40	// transportDefaultConnFlow is how many connection-level flow control
41	// tokens we give the server at start-up, past the default 64k.
42	transportDefaultConnFlow = 1 << 30
43
44	// transportDefaultStreamFlow is how many stream-level flow
45	// control tokens we announce to the peer, and how many bytes
46	// we buffer per stream.
47	transportDefaultStreamFlow = 4 << 20
48
49	// transportDefaultStreamMinRefresh is the minimum number of bytes we'll send
50	// a stream-level WINDOW_UPDATE for at a time.
51	transportDefaultStreamMinRefresh = 4 << 10
52
53	defaultUserAgent = "Go-http-client/2.0"
54)
55
56// Transport is an HTTP/2 Transport.
57//
58// A Transport internally caches connections to servers. It is safe
59// for concurrent use by multiple goroutines.
60type Transport struct {
61	// DialTLS specifies an optional dial function for creating
62	// TLS connections for requests.
63	//
64	// If DialTLS is nil, tls.Dial is used.
65	//
66	// If the returned net.Conn has a ConnectionState method like tls.Conn,
67	// it will be used to set http.Response.TLS.
68	DialTLS func(network, addr string, cfg *tls.Config) (net.Conn, error)
69
70	// TLSClientConfig specifies the TLS configuration to use with
71	// tls.Client. If nil, the default configuration is used.
72	TLSClientConfig *tls.Config
73
74	// ConnPool optionally specifies an alternate connection pool to use.
75	// If nil, the default is used.
76	ConnPool ClientConnPool
77
78	// DisableCompression, if true, prevents the Transport from
79	// requesting compression with an "Accept-Encoding: gzip"
80	// request header when the Request contains no existing
81	// Accept-Encoding value. If the Transport requests gzip on
82	// its own and gets a gzipped response, it's transparently
83	// decoded in the Response.Body. However, if the user
84	// explicitly requested gzip it is not automatically
85	// uncompressed.
86	DisableCompression bool
87
88	// AllowHTTP, if true, permits HTTP/2 requests using the insecure,
89	// plain-text "http" scheme. Note that this does not enable h2c support.
90	AllowHTTP bool
91
92	// MaxHeaderListSize is the http2 SETTINGS_MAX_HEADER_LIST_SIZE to
93	// send in the initial settings frame. It is how many bytes
94	// of response headers are allowed. Unlike the http2 spec, zero here
95	// means to use a default limit (currently 10MB). If you actually
96	// want to advertise an ulimited value to the peer, Transport
97	// interprets the highest possible value here (0xffffffff or 1<<32-1)
98	// to mean no limit.
99	MaxHeaderListSize uint32
100
101	// StrictMaxConcurrentStreams controls whether the server's
102	// SETTINGS_MAX_CONCURRENT_STREAMS should be respected
103	// globally. If false, new TCP connections are created to the
104	// server as needed to keep each under the per-connection
105	// SETTINGS_MAX_CONCURRENT_STREAMS limit. If true, the
106	// server's SETTINGS_MAX_CONCURRENT_STREAMS is interpreted as
107	// a global limit and callers of RoundTrip block when needed,
108	// waiting for their turn.
109	StrictMaxConcurrentStreams bool
110
111	// t1, if non-nil, is the standard library Transport using
112	// this transport. Its settings are used (but not its
113	// RoundTrip method, etc).
114	t1 *http.Transport
115
116	connPoolOnce  sync.Once
117	connPoolOrDef ClientConnPool // non-nil version of ConnPool
118}
119
120func (t *Transport) maxHeaderListSize() uint32 {
121	if t.MaxHeaderListSize == 0 {
122		return 10 << 20
123	}
124	if t.MaxHeaderListSize == 0xffffffff {
125		return 0
126	}
127	return t.MaxHeaderListSize
128}
129
130func (t *Transport) disableCompression() bool {
131	return t.DisableCompression || (t.t1 != nil && t.t1.DisableCompression)
132}
133
134// ConfigureTransport configures a net/http HTTP/1 Transport to use HTTP/2.
135// It returns an error if t1 has already been HTTP/2-enabled.
136func ConfigureTransport(t1 *http.Transport) error {
137	_, err := configureTransport(t1)
138	return err
139}
140
141func configureTransport(t1 *http.Transport) (*Transport, error) {
142	connPool := new(clientConnPool)
143	t2 := &Transport{
144		ConnPool: noDialClientConnPool{connPool},
145		t1:       t1,
146	}
147	connPool.t = t2
148	if err := registerHTTPSProtocol(t1, noDialH2RoundTripper{t2}); err != nil {
149		return nil, err
150	}
151	if t1.TLSClientConfig == nil {
152		t1.TLSClientConfig = new(tls.Config)
153	}
154	if !strSliceContains(t1.TLSClientConfig.NextProtos, "h2") {
155		t1.TLSClientConfig.NextProtos = append([]string{"h2"}, t1.TLSClientConfig.NextProtos...)
156	}
157	if !strSliceContains(t1.TLSClientConfig.NextProtos, "http/1.1") {
158		t1.TLSClientConfig.NextProtos = append(t1.TLSClientConfig.NextProtos, "http/1.1")
159	}
160	upgradeFn := func(authority string, c *tls.Conn) http.RoundTripper {
161		addr := authorityAddr("https", authority)
162		if used, err := connPool.addConnIfNeeded(addr, t2, c); err != nil {
163			go c.Close()
164			return erringRoundTripper{err}
165		} else if !used {
166			// Turns out we don't need this c.
167			// For example, two goroutines made requests to the same host
168			// at the same time, both kicking off TCP dials. (since protocol
169			// was unknown)
170			go c.Close()
171		}
172		return t2
173	}
174	if m := t1.TLSNextProto; len(m) == 0 {
175		t1.TLSNextProto = map[string]func(string, *tls.Conn) http.RoundTripper{
176			"h2": upgradeFn,
177		}
178	} else {
179		m["h2"] = upgradeFn
180	}
181	return t2, nil
182}
183
184func (t *Transport) connPool() ClientConnPool {
185	t.connPoolOnce.Do(t.initConnPool)
186	return t.connPoolOrDef
187}
188
189func (t *Transport) initConnPool() {
190	if t.ConnPool != nil {
191		t.connPoolOrDef = t.ConnPool
192	} else {
193		t.connPoolOrDef = &clientConnPool{t: t}
194	}
195}
196
197// ClientConn is the state of a single HTTP/2 client connection to an
198// HTTP/2 server.
199type ClientConn struct {
200	t         *Transport
201	tconn     net.Conn             // usually *tls.Conn, except specialized impls
202	tlsState  *tls.ConnectionState // nil only for specialized impls
203	reused    uint32               // whether conn is being reused; atomic
204	singleUse bool                 // whether being used for a single http.Request
205
206	// readLoop goroutine fields:
207	readerDone chan struct{} // closed on error
208	readerErr  error         // set before readerDone is closed
209
210	idleTimeout time.Duration // or 0 for never
211	idleTimer   *time.Timer
212
213	mu              sync.Mutex // guards following
214	cond            *sync.Cond // hold mu; broadcast on flow/closed changes
215	flow            flow       // our conn-level flow control quota (cs.flow is per stream)
216	inflow          flow       // peer's conn-level flow control
217	closing         bool
218	closed          bool
219	wantSettingsAck bool                     // we sent a SETTINGS frame and haven't heard back
220	goAway          *GoAwayFrame             // if non-nil, the GoAwayFrame we received
221	goAwayDebug     string                   // goAway frame's debug data, retained as a string
222	streams         map[uint32]*clientStream // client-initiated
223	nextStreamID    uint32
224	pendingRequests int                       // requests blocked and waiting to be sent because len(streams) == maxConcurrentStreams
225	pings           map[[8]byte]chan struct{} // in flight ping data to notification channel
226	bw              *bufio.Writer
227	br              *bufio.Reader
228	fr              *Framer
229	lastActive      time.Time
230	lastIdle        time.Time // time last idle
231	// Settings from peer: (also guarded by mu)
232	maxFrameSize          uint32
233	maxConcurrentStreams  uint32
234	peerMaxHeaderListSize uint64
235	initialWindowSize     uint32
236
237	hbuf    bytes.Buffer // HPACK encoder writes into this
238	henc    *hpack.Encoder
239	freeBuf [][]byte
240
241	wmu  sync.Mutex // held while writing; acquire AFTER mu if holding both
242	werr error      // first write error that has occurred
243}
244
245// clientStream is the state for a single HTTP/2 stream. One of these
246// is created for each Transport.RoundTrip call.
247type clientStream struct {
248	cc            *ClientConn
249	req           *http.Request
250	trace         *httptrace.ClientTrace // or nil
251	ID            uint32
252	resc          chan resAndError
253	bufPipe       pipe // buffered pipe with the flow-controlled response payload
254	startedWrite  bool // started request body write; guarded by cc.mu
255	requestedGzip bool
256	on100         func() // optional code to run if get a 100 continue response
257
258	flow        flow  // guarded by cc.mu
259	inflow      flow  // guarded by cc.mu
260	bytesRemain int64 // -1 means unknown; owned by transportResponseBody.Read
261	readErr     error // sticky read error; owned by transportResponseBody.Read
262	stopReqBody error // if non-nil, stop writing req body; guarded by cc.mu
263	didReset    bool  // whether we sent a RST_STREAM to the server; guarded by cc.mu
264
265	peerReset chan struct{} // closed on peer reset
266	resetErr  error         // populated before peerReset is closed
267
268	done chan struct{} // closed when stream remove from cc.streams map; close calls guarded by cc.mu
269
270	// owned by clientConnReadLoop:
271	firstByte    bool  // got the first response byte
272	pastHeaders  bool  // got first MetaHeadersFrame (actual headers)
273	pastTrailers bool  // got optional second MetaHeadersFrame (trailers)
274	num1xx       uint8 // number of 1xx responses seen
275
276	trailer    http.Header  // accumulated trailers
277	resTrailer *http.Header // client's Response.Trailer
278}
279
280// awaitRequestCancel waits for the user to cancel a request or for the done
281// channel to be signaled. A non-nil error is returned only if the request was
282// canceled.
283func awaitRequestCancel(req *http.Request, done <-chan struct{}) error {
284	ctx := req.Context()
285	if req.Cancel == nil && ctx.Done() == nil {
286		return nil
287	}
288	select {
289	case <-req.Cancel:
290		return errRequestCanceled
291	case <-ctx.Done():
292		return ctx.Err()
293	case <-done:
294		return nil
295	}
296}
297
298var got1xxFuncForTests func(int, textproto.MIMEHeader) error
299
300// get1xxTraceFunc returns the value of request's httptrace.ClientTrace.Got1xxResponse func,
301// if any. It returns nil if not set or if the Go version is too old.
302func (cs *clientStream) get1xxTraceFunc() func(int, textproto.MIMEHeader) error {
303	if fn := got1xxFuncForTests; fn != nil {
304		return fn
305	}
306	return traceGot1xxResponseFunc(cs.trace)
307}
308
309// awaitRequestCancel waits for the user to cancel a request, its context to
310// expire, or for the request to be done (any way it might be removed from the
311// cc.streams map: peer reset, successful completion, TCP connection breakage,
312// etc). If the request is canceled, then cs will be canceled and closed.
313func (cs *clientStream) awaitRequestCancel(req *http.Request) {
314	if err := awaitRequestCancel(req, cs.done); err != nil {
315		cs.cancelStream()
316		cs.bufPipe.CloseWithError(err)
317	}
318}
319
320func (cs *clientStream) cancelStream() {
321	cc := cs.cc
322	cc.mu.Lock()
323	didReset := cs.didReset
324	cs.didReset = true
325	cc.mu.Unlock()
326
327	if !didReset {
328		cc.writeStreamReset(cs.ID, ErrCodeCancel, nil)
329		cc.forgetStreamID(cs.ID)
330	}
331}
332
333// checkResetOrDone reports any error sent in a RST_STREAM frame by the
334// server, or errStreamClosed if the stream is complete.
335func (cs *clientStream) checkResetOrDone() error {
336	select {
337	case <-cs.peerReset:
338		return cs.resetErr
339	case <-cs.done:
340		return errStreamClosed
341	default:
342		return nil
343	}
344}
345
346func (cs *clientStream) getStartedWrite() bool {
347	cc := cs.cc
348	cc.mu.Lock()
349	defer cc.mu.Unlock()
350	return cs.startedWrite
351}
352
353func (cs *clientStream) abortRequestBodyWrite(err error) {
354	if err == nil {
355		panic("nil error")
356	}
357	cc := cs.cc
358	cc.mu.Lock()
359	cs.stopReqBody = err
360	cc.cond.Broadcast()
361	cc.mu.Unlock()
362}
363
364type stickyErrWriter struct {
365	w   io.Writer
366	err *error
367}
368
369func (sew stickyErrWriter) Write(p []byte) (n int, err error) {
370	if *sew.err != nil {
371		return 0, *sew.err
372	}
373	n, err = sew.w.Write(p)
374	*sew.err = err
375	return
376}
377
378// noCachedConnError is the concrete type of ErrNoCachedConn, which
379// needs to be detected by net/http regardless of whether it's its
380// bundled version (in h2_bundle.go with a rewritten type name) or
381// from a user's x/net/http2. As such, as it has a unique method name
382// (IsHTTP2NoCachedConnError) that net/http sniffs for via func
383// isNoCachedConnError.
384type noCachedConnError struct{}
385
386func (noCachedConnError) IsHTTP2NoCachedConnError() {}
387func (noCachedConnError) Error() string             { return "http2: no cached connection was available" }
388
389// isNoCachedConnError reports whether err is of type noCachedConnError
390// or its equivalent renamed type in net/http2's h2_bundle.go. Both types
391// may coexist in the same running program.
392func isNoCachedConnError(err error) bool {
393	_, ok := err.(interface{ IsHTTP2NoCachedConnError() })
394	return ok
395}
396
397var ErrNoCachedConn error = noCachedConnError{}
398
399// RoundTripOpt are options for the Transport.RoundTripOpt method.
400type RoundTripOpt struct {
401	// OnlyCachedConn controls whether RoundTripOpt may
402	// create a new TCP connection. If set true and
403	// no cached connection is available, RoundTripOpt
404	// will return ErrNoCachedConn.
405	OnlyCachedConn bool
406}
407
408func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
409	return t.RoundTripOpt(req, RoundTripOpt{})
410}
411
412// authorityAddr returns a given authority (a host/IP, or host:port / ip:port)
413// and returns a host:port. The port 443 is added if needed.
414func authorityAddr(scheme string, authority string) (addr string) {
415	host, port, err := net.SplitHostPort(authority)
416	if err != nil { // authority didn't have a port
417		port = "443"
418		if scheme == "http" {
419			port = "80"
420		}
421		host = authority
422	}
423	if a, err := idna.ToASCII(host); err == nil {
424		host = a
425	}
426	// IPv6 address literal, without a port:
427	if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
428		return host + ":" + port
429	}
430	return net.JoinHostPort(host, port)
431}
432
433// RoundTripOpt is like RoundTrip, but takes options.
434func (t *Transport) RoundTripOpt(req *http.Request, opt RoundTripOpt) (*http.Response, error) {
435	if !(req.URL.Scheme == "https" || (req.URL.Scheme == "http" && t.AllowHTTP)) {
436		return nil, errors.New("http2: unsupported scheme")
437	}
438
439	addr := authorityAddr(req.URL.Scheme, req.URL.Host)
440	for retry := 0; ; retry++ {
441		cc, err := t.connPool().GetClientConn(req, addr)
442		if err != nil {
443			t.vlogf("http2: Transport failed to get client conn for %s: %v", addr, err)
444			return nil, err
445		}
446		reused := !atomic.CompareAndSwapUint32(&cc.reused, 0, 1)
447		traceGotConn(req, cc, reused)
448		res, gotErrAfterReqBodyWrite, err := cc.roundTrip(req)
449		if err != nil && retry <= 6 {
450			if req, err = shouldRetryRequest(req, err, gotErrAfterReqBodyWrite); err == nil {
451				// After the first retry, do exponential backoff with 10% jitter.
452				if retry == 0 {
453					continue
454				}
455				backoff := float64(uint(1) << (uint(retry) - 1))
456				backoff += backoff * (0.1 * mathrand.Float64())
457				select {
458				case <-time.After(time.Second * time.Duration(backoff)):
459					continue
460				case <-req.Context().Done():
461					return nil, req.Context().Err()
462				}
463			}
464		}
465		if err != nil {
466			t.vlogf("RoundTrip failure: %v", err)
467			return nil, err
468		}
469		return res, nil
470	}
471}
472
473// CloseIdleConnections closes any connections which were previously
474// connected from previous requests but are now sitting idle.
475// It does not interrupt any connections currently in use.
476func (t *Transport) CloseIdleConnections() {
477	if cp, ok := t.connPool().(clientConnPoolIdleCloser); ok {
478		cp.closeIdleConnections()
479	}
480}
481
482var (
483	errClientConnClosed    = errors.New("http2: client conn is closed")
484	errClientConnUnusable  = errors.New("http2: client conn not usable")
485	errClientConnGotGoAway = errors.New("http2: Transport received Server's graceful shutdown GOAWAY")
486)
487
488// shouldRetryRequest is called by RoundTrip when a request fails to get
489// response headers. It is always called with a non-nil error.
490// It returns either a request to retry (either the same request, or a
491// modified clone), or an error if the request can't be replayed.
492func shouldRetryRequest(req *http.Request, err error, afterBodyWrite bool) (*http.Request, error) {
493	if !canRetryError(err) {
494		return nil, err
495	}
496	// If the Body is nil (or http.NoBody), it's safe to reuse
497	// this request and its Body.
498	if req.Body == nil || req.Body == http.NoBody {
499		return req, nil
500	}
501
502	// If the request body can be reset back to its original
503	// state via the optional req.GetBody, do that.
504	if req.GetBody != nil {
505		// TODO: consider a req.Body.Close here? or audit that all caller paths do?
506		body, err := req.GetBody()
507		if err != nil {
508			return nil, err
509		}
510		newReq := *req
511		newReq.Body = body
512		return &newReq, nil
513	}
514
515	// The Request.Body can't reset back to the beginning, but we
516	// don't seem to have started to read from it yet, so reuse
517	// the request directly. The "afterBodyWrite" means the
518	// bodyWrite process has started, which becomes true before
519	// the first Read.
520	if !afterBodyWrite {
521		return req, nil
522	}
523
524	return nil, fmt.Errorf("http2: Transport: cannot retry err [%v] after Request.Body was written; define Request.GetBody to avoid this error", err)
525}
526
527func canRetryError(err error) bool {
528	if err == errClientConnUnusable || err == errClientConnGotGoAway {
529		return true
530	}
531	if se, ok := err.(StreamError); ok {
532		return se.Code == ErrCodeRefusedStream
533	}
534	return false
535}
536
537func (t *Transport) dialClientConn(addr string, singleUse bool) (*ClientConn, error) {
538	host, _, err := net.SplitHostPort(addr)
539	if err != nil {
540		return nil, err
541	}
542	tconn, err := t.dialTLS()("tcp", addr, t.newTLSConfig(host))
543	if err != nil {
544		return nil, err
545	}
546	return t.newClientConn(tconn, singleUse)
547}
548
549func (t *Transport) newTLSConfig(host string) *tls.Config {
550	cfg := new(tls.Config)
551	if t.TLSClientConfig != nil {
552		*cfg = *t.TLSClientConfig.Clone()
553	}
554	if !strSliceContains(cfg.NextProtos, NextProtoTLS) {
555		cfg.NextProtos = append([]string{NextProtoTLS}, cfg.NextProtos...)
556	}
557	if cfg.ServerName == "" {
558		cfg.ServerName = host
559	}
560	return cfg
561}
562
563func (t *Transport) dialTLS() func(string, string, *tls.Config) (net.Conn, error) {
564	if t.DialTLS != nil {
565		return t.DialTLS
566	}
567	return t.dialTLSDefault
568}
569
570func (t *Transport) dialTLSDefault(network, addr string, cfg *tls.Config) (net.Conn, error) {
571	cn, err := tls.Dial(network, addr, cfg)
572	if err != nil {
573		return nil, err
574	}
575	if err := cn.Handshake(); err != nil {
576		return nil, err
577	}
578	if !cfg.InsecureSkipVerify {
579		if err := cn.VerifyHostname(cfg.ServerName); err != nil {
580			return nil, err
581		}
582	}
583	state := cn.ConnectionState()
584	if p := state.NegotiatedProtocol; p != NextProtoTLS {
585		return nil, fmt.Errorf("http2: unexpected ALPN protocol %q; want %q", p, NextProtoTLS)
586	}
587	if !state.NegotiatedProtocolIsMutual {
588		return nil, errors.New("http2: could not negotiate protocol mutually")
589	}
590	return cn, nil
591}
592
593// disableKeepAlives reports whether connections should be closed as
594// soon as possible after handling the first request.
595func (t *Transport) disableKeepAlives() bool {
596	return t.t1 != nil && t.t1.DisableKeepAlives
597}
598
599func (t *Transport) expectContinueTimeout() time.Duration {
600	if t.t1 == nil {
601		return 0
602	}
603	return t.t1.ExpectContinueTimeout
604}
605
606func (t *Transport) NewClientConn(c net.Conn) (*ClientConn, error) {
607	return t.newClientConn(c, t.disableKeepAlives())
608}
609
610func (t *Transport) newClientConn(c net.Conn, singleUse bool) (*ClientConn, error) {
611	cc := &ClientConn{
612		t:                     t,
613		tconn:                 c,
614		readerDone:            make(chan struct{}),
615		nextStreamID:          1,
616		maxFrameSize:          16 << 10,           // spec default
617		initialWindowSize:     65535,              // spec default
618		maxConcurrentStreams:  1000,               // "infinite", per spec. 1000 seems good enough.
619		peerMaxHeaderListSize: 0xffffffffffffffff, // "infinite", per spec. Use 2^64-1 instead.
620		streams:               make(map[uint32]*clientStream),
621		singleUse:             singleUse,
622		wantSettingsAck:       true,
623		pings:                 make(map[[8]byte]chan struct{}),
624	}
625	if d := t.idleConnTimeout(); d != 0 {
626		cc.idleTimeout = d
627		cc.idleTimer = time.AfterFunc(d, cc.onIdleTimeout)
628	}
629	if VerboseLogs {
630		t.vlogf("http2: Transport creating client conn %p to %v", cc, c.RemoteAddr())
631	}
632
633	cc.cond = sync.NewCond(&cc.mu)
634	cc.flow.add(int32(initialWindowSize))
635
636	// TODO: adjust this writer size to account for frame size +
637	// MTU + crypto/tls record padding.
638	cc.bw = bufio.NewWriter(stickyErrWriter{c, &cc.werr})
639	cc.br = bufio.NewReader(c)
640	cc.fr = NewFramer(cc.bw, cc.br)
641	cc.fr.ReadMetaHeaders = hpack.NewDecoder(initialHeaderTableSize, nil)
642	cc.fr.MaxHeaderListSize = t.maxHeaderListSize()
643
644	// TODO: SetMaxDynamicTableSize, SetMaxDynamicTableSizeLimit on
645	// henc in response to SETTINGS frames?
646	cc.henc = hpack.NewEncoder(&cc.hbuf)
647
648	if t.AllowHTTP {
649		cc.nextStreamID = 3
650	}
651
652	if cs, ok := c.(connectionStater); ok {
653		state := cs.ConnectionState()
654		cc.tlsState = &state
655	}
656
657	initialSettings := []Setting{
658		{ID: SettingEnablePush, Val: 0},
659		{ID: SettingInitialWindowSize, Val: transportDefaultStreamFlow},
660	}
661	if max := t.maxHeaderListSize(); max != 0 {
662		initialSettings = append(initialSettings, Setting{ID: SettingMaxHeaderListSize, Val: max})
663	}
664
665	cc.bw.Write(clientPreface)
666	cc.fr.WriteSettings(initialSettings...)
667	cc.fr.WriteWindowUpdate(0, transportDefaultConnFlow)
668	cc.inflow.add(transportDefaultConnFlow + initialWindowSize)
669	cc.bw.Flush()
670	if cc.werr != nil {
671		return nil, cc.werr
672	}
673
674	go cc.readLoop()
675	return cc, nil
676}
677
678func (cc *ClientConn) setGoAway(f *GoAwayFrame) {
679	cc.mu.Lock()
680	defer cc.mu.Unlock()
681
682	old := cc.goAway
683	cc.goAway = f
684
685	// Merge the previous and current GoAway error frames.
686	if cc.goAwayDebug == "" {
687		cc.goAwayDebug = string(f.DebugData())
688	}
689	if old != nil && old.ErrCode != ErrCodeNo {
690		cc.goAway.ErrCode = old.ErrCode
691	}
692	last := f.LastStreamID
693	for streamID, cs := range cc.streams {
694		if streamID > last {
695			select {
696			case cs.resc <- resAndError{err: errClientConnGotGoAway}:
697			default:
698			}
699		}
700	}
701}
702
703// CanTakeNewRequest reports whether the connection can take a new request,
704// meaning it has not been closed or received or sent a GOAWAY.
705func (cc *ClientConn) CanTakeNewRequest() bool {
706	cc.mu.Lock()
707	defer cc.mu.Unlock()
708	return cc.canTakeNewRequestLocked()
709}
710
711// clientConnIdleState describes the suitability of a client
712// connection to initiate a new RoundTrip request.
713type clientConnIdleState struct {
714	canTakeNewRequest bool
715	freshConn         bool // whether it's unused by any previous request
716}
717
718func (cc *ClientConn) idleState() clientConnIdleState {
719	cc.mu.Lock()
720	defer cc.mu.Unlock()
721	return cc.idleStateLocked()
722}
723
724func (cc *ClientConn) idleStateLocked() (st clientConnIdleState) {
725	if cc.singleUse && cc.nextStreamID > 1 {
726		return
727	}
728	var maxConcurrentOkay bool
729	if cc.t.StrictMaxConcurrentStreams {
730		// We'll tell the caller we can take a new request to
731		// prevent the caller from dialing a new TCP
732		// connection, but then we'll block later before
733		// writing it.
734		maxConcurrentOkay = true
735	} else {
736		maxConcurrentOkay = int64(len(cc.streams)+1) < int64(cc.maxConcurrentStreams)
737	}
738
739	st.canTakeNewRequest = cc.goAway == nil && !cc.closed && !cc.closing && maxConcurrentOkay &&
740		int64(cc.nextStreamID)+2*int64(cc.pendingRequests) < math.MaxInt32 &&
741		!cc.tooIdleLocked()
742	st.freshConn = cc.nextStreamID == 1 && st.canTakeNewRequest
743	return
744}
745
746func (cc *ClientConn) canTakeNewRequestLocked() bool {
747	st := cc.idleStateLocked()
748	return st.canTakeNewRequest
749}
750
751// tooIdleLocked reports whether this connection has been been sitting idle
752// for too much wall time.
753func (cc *ClientConn) tooIdleLocked() bool {
754	// The Round(0) strips the monontonic clock reading so the
755	// times are compared based on their wall time. We don't want
756	// to reuse a connection that's been sitting idle during
757	// VM/laptop suspend if monotonic time was also frozen.
758	return cc.idleTimeout != 0 && !cc.lastIdle.IsZero() && time.Since(cc.lastIdle.Round(0)) > cc.idleTimeout
759}
760
761// onIdleTimeout is called from a time.AfterFunc goroutine. It will
762// only be called when we're idle, but because we're coming from a new
763// goroutine, there could be a new request coming in at the same time,
764// so this simply calls the synchronized closeIfIdle to shut down this
765// connection. The timer could just call closeIfIdle, but this is more
766// clear.
767func (cc *ClientConn) onIdleTimeout() {
768	cc.closeIfIdle()
769}
770
771func (cc *ClientConn) closeIfIdle() {
772	cc.mu.Lock()
773	if len(cc.streams) > 0 {
774		cc.mu.Unlock()
775		return
776	}
777	cc.closed = true
778	nextID := cc.nextStreamID
779	// TODO: do clients send GOAWAY too? maybe? Just Close:
780	cc.mu.Unlock()
781
782	if VerboseLogs {
783		cc.vlogf("http2: Transport closing idle conn %p (forSingleUse=%v, maxStream=%v)", cc, cc.singleUse, nextID-2)
784	}
785	cc.tconn.Close()
786}
787
788var shutdownEnterWaitStateHook = func() {}
789
790// Shutdown gracefully close the client connection, waiting for running streams to complete.
791func (cc *ClientConn) Shutdown(ctx context.Context) error {
792	if err := cc.sendGoAway(); err != nil {
793		return err
794	}
795	// Wait for all in-flight streams to complete or connection to close
796	done := make(chan error, 1)
797	cancelled := false // guarded by cc.mu
798	go func() {
799		cc.mu.Lock()
800		defer cc.mu.Unlock()
801		for {
802			if len(cc.streams) == 0 || cc.closed {
803				cc.closed = true
804				done <- cc.tconn.Close()
805				break
806			}
807			if cancelled {
808				break
809			}
810			cc.cond.Wait()
811		}
812	}()
813	shutdownEnterWaitStateHook()
814	select {
815	case err := <-done:
816		return err
817	case <-ctx.Done():
818		cc.mu.Lock()
819		// Free the goroutine above
820		cancelled = true
821		cc.cond.Broadcast()
822		cc.mu.Unlock()
823		return ctx.Err()
824	}
825}
826
827func (cc *ClientConn) sendGoAway() error {
828	cc.mu.Lock()
829	defer cc.mu.Unlock()
830	cc.wmu.Lock()
831	defer cc.wmu.Unlock()
832	if cc.closing {
833		// GOAWAY sent already
834		return nil
835	}
836	// Send a graceful shutdown frame to server
837	maxStreamID := cc.nextStreamID
838	if err := cc.fr.WriteGoAway(maxStreamID, ErrCodeNo, nil); err != nil {
839		return err
840	}
841	if err := cc.bw.Flush(); err != nil {
842		return err
843	}
844	// Prevent new requests
845	cc.closing = true
846	return nil
847}
848
849// Close closes the client connection immediately.
850//
851// In-flight requests are interrupted. For a graceful shutdown, use Shutdown instead.
852func (cc *ClientConn) Close() error {
853	cc.mu.Lock()
854	defer cc.cond.Broadcast()
855	defer cc.mu.Unlock()
856	err := errors.New("http2: client connection force closed via ClientConn.Close")
857	for id, cs := range cc.streams {
858		select {
859		case cs.resc <- resAndError{err: err}:
860		default:
861		}
862		cs.bufPipe.CloseWithError(err)
863		delete(cc.streams, id)
864	}
865	cc.closed = true
866	return cc.tconn.Close()
867}
868
869const maxAllocFrameSize = 512 << 10
870
871// frameBuffer returns a scratch buffer suitable for writing DATA frames.
872// They're capped at the min of the peer's max frame size or 512KB
873// (kinda arbitrarily), but definitely capped so we don't allocate 4GB
874// bufers.
875func (cc *ClientConn) frameScratchBuffer() []byte {
876	cc.mu.Lock()
877	size := cc.maxFrameSize
878	if size > maxAllocFrameSize {
879		size = maxAllocFrameSize
880	}
881	for i, buf := range cc.freeBuf {
882		if len(buf) >= int(size) {
883			cc.freeBuf[i] = nil
884			cc.mu.Unlock()
885			return buf[:size]
886		}
887	}
888	cc.mu.Unlock()
889	return make([]byte, size)
890}
891
892func (cc *ClientConn) putFrameScratchBuffer(buf []byte) {
893	cc.mu.Lock()
894	defer cc.mu.Unlock()
895	const maxBufs = 4 // arbitrary; 4 concurrent requests per conn? investigate.
896	if len(cc.freeBuf) < maxBufs {
897		cc.freeBuf = append(cc.freeBuf, buf)
898		return
899	}
900	for i, old := range cc.freeBuf {
901		if old == nil {
902			cc.freeBuf[i] = buf
903			return
904		}
905	}
906	// forget about it.
907}
908
909// errRequestCanceled is a copy of net/http's errRequestCanceled because it's not
910// exported. At least they'll be DeepEqual for h1-vs-h2 comparisons tests.
911var errRequestCanceled = errors.New("net/http: request canceled")
912
913func commaSeparatedTrailers(req *http.Request) (string, error) {
914	keys := make([]string, 0, len(req.Trailer))
915	for k := range req.Trailer {
916		k = http.CanonicalHeaderKey(k)
917		switch k {
918		case "Transfer-Encoding", "Trailer", "Content-Length":
919			return "", &badStringError{"invalid Trailer key", k}
920		}
921		keys = append(keys, k)
922	}
923	if len(keys) > 0 {
924		sort.Strings(keys)
925		return strings.Join(keys, ","), nil
926	}
927	return "", nil
928}
929
930func (cc *ClientConn) responseHeaderTimeout() time.Duration {
931	if cc.t.t1 != nil {
932		return cc.t.t1.ResponseHeaderTimeout
933	}
934	// No way to do this (yet?) with just an http2.Transport. Probably
935	// no need. Request.Cancel this is the new way. We only need to support
936	// this for compatibility with the old http.Transport fields when
937	// we're doing transparent http2.
938	return 0
939}
940
941// checkConnHeaders checks whether req has any invalid connection-level headers.
942// per RFC 7540 section 8.1.2.2: Connection-Specific Header Fields.
943// Certain headers are special-cased as okay but not transmitted later.
944func checkConnHeaders(req *http.Request) error {
945	if v := req.Header.Get("Upgrade"); v != "" {
946		return fmt.Errorf("http2: invalid Upgrade request header: %q", req.Header["Upgrade"])
947	}
948	if vv := req.Header["Transfer-Encoding"]; len(vv) > 0 && (len(vv) > 1 || vv[0] != "" && vv[0] != "chunked") {
949		return fmt.Errorf("http2: invalid Transfer-Encoding request header: %q", vv)
950	}
951	if vv := req.Header["Connection"]; len(vv) > 0 && (len(vv) > 1 || vv[0] != "" && !strings.EqualFold(vv[0], "close") && !strings.EqualFold(vv[0], "keep-alive")) {
952		return fmt.Errorf("http2: invalid Connection request header: %q", vv)
953	}
954	return nil
955}
956
957// actualContentLength returns a sanitized version of
958// req.ContentLength, where 0 actually means zero (not unknown) and -1
959// means unknown.
960func actualContentLength(req *http.Request) int64 {
961	if req.Body == nil || req.Body == http.NoBody {
962		return 0
963	}
964	if req.ContentLength != 0 {
965		return req.ContentLength
966	}
967	return -1
968}
969
970func (cc *ClientConn) RoundTrip(req *http.Request) (*http.Response, error) {
971	resp, _, err := cc.roundTrip(req)
972	return resp, err
973}
974
975func (cc *ClientConn) roundTrip(req *http.Request) (res *http.Response, gotErrAfterReqBodyWrite bool, err error) {
976	if err := checkConnHeaders(req); err != nil {
977		return nil, false, err
978	}
979	if cc.idleTimer != nil {
980		cc.idleTimer.Stop()
981	}
982
983	trailers, err := commaSeparatedTrailers(req)
984	if err != nil {
985		return nil, false, err
986	}
987	hasTrailers := trailers != ""
988
989	cc.mu.Lock()
990	if err := cc.awaitOpenSlotForRequest(req); err != nil {
991		cc.mu.Unlock()
992		return nil, false, err
993	}
994
995	body := req.Body
996	contentLen := actualContentLength(req)
997	hasBody := contentLen != 0
998
999	// TODO(bradfitz): this is a copy of the logic in net/http. Unify somewhere?
1000	var requestedGzip bool
1001	if !cc.t.disableCompression() &&
1002		req.Header.Get("Accept-Encoding") == "" &&
1003		req.Header.Get("Range") == "" &&
1004		req.Method != "HEAD" {
1005		// Request gzip only, not deflate. Deflate is ambiguous and
1006		// not as universally supported anyway.
1007		// See: https://zlib.net/zlib_faq.html#faq39
1008		//
1009		// Note that we don't request this for HEAD requests,
1010		// due to a bug in nginx:
1011		//   http://trac.nginx.org/nginx/ticket/358
1012		//   https://golang.org/issue/5522
1013		//
1014		// We don't request gzip if the request is for a range, since
1015		// auto-decoding a portion of a gzipped document will just fail
1016		// anyway. See https://golang.org/issue/8923
1017		requestedGzip = true
1018	}
1019
1020	// we send: HEADERS{1}, CONTINUATION{0,} + DATA{0,} (DATA is
1021	// sent by writeRequestBody below, along with any Trailers,
1022	// again in form HEADERS{1}, CONTINUATION{0,})
1023	hdrs, err := cc.encodeHeaders(req, requestedGzip, trailers, contentLen)
1024	if err != nil {
1025		cc.mu.Unlock()
1026		return nil, false, err
1027	}
1028
1029	cs := cc.newStream()
1030	cs.req = req
1031	cs.trace = httptrace.ContextClientTrace(req.Context())
1032	cs.requestedGzip = requestedGzip
1033	bodyWriter := cc.t.getBodyWriterState(cs, body)
1034	cs.on100 = bodyWriter.on100
1035
1036	cc.wmu.Lock()
1037	endStream := !hasBody && !hasTrailers
1038	werr := cc.writeHeaders(cs.ID, endStream, int(cc.maxFrameSize), hdrs)
1039	cc.wmu.Unlock()
1040	traceWroteHeaders(cs.trace)
1041	cc.mu.Unlock()
1042
1043	if werr != nil {
1044		if hasBody {
1045			req.Body.Close() // per RoundTripper contract
1046			bodyWriter.cancel()
1047		}
1048		cc.forgetStreamID(cs.ID)
1049		// Don't bother sending a RST_STREAM (our write already failed;
1050		// no need to keep writing)
1051		traceWroteRequest(cs.trace, werr)
1052		return nil, false, werr
1053	}
1054
1055	var respHeaderTimer <-chan time.Time
1056	if hasBody {
1057		bodyWriter.scheduleBodyWrite()
1058	} else {
1059		traceWroteRequest(cs.trace, nil)
1060		if d := cc.responseHeaderTimeout(); d != 0 {
1061			timer := time.NewTimer(d)
1062			defer timer.Stop()
1063			respHeaderTimer = timer.C
1064		}
1065	}
1066
1067	readLoopResCh := cs.resc
1068	bodyWritten := false
1069	ctx := req.Context()
1070
1071	handleReadLoopResponse := func(re resAndError) (*http.Response, bool, error) {
1072		res := re.res
1073		if re.err != nil || res.StatusCode > 299 {
1074			// On error or status code 3xx, 4xx, 5xx, etc abort any
1075			// ongoing write, assuming that the server doesn't care
1076			// about our request body. If the server replied with 1xx or
1077			// 2xx, however, then assume the server DOES potentially
1078			// want our body (e.g. full-duplex streaming:
1079			// golang.org/issue/13444). If it turns out the server
1080			// doesn't, they'll RST_STREAM us soon enough. This is a
1081			// heuristic to avoid adding knobs to Transport. Hopefully
1082			// we can keep it.
1083			bodyWriter.cancel()
1084			cs.abortRequestBodyWrite(errStopReqBodyWrite)
1085		}
1086		if re.err != nil {
1087			cc.forgetStreamID(cs.ID)
1088			return nil, cs.getStartedWrite(), re.err
1089		}
1090		res.Request = req
1091		res.TLS = cc.tlsState
1092		return res, false, nil
1093	}
1094
1095	for {
1096		select {
1097		case re := <-readLoopResCh:
1098			return handleReadLoopResponse(re)
1099		case <-respHeaderTimer:
1100			if !hasBody || bodyWritten {
1101				cc.writeStreamReset(cs.ID, ErrCodeCancel, nil)
1102			} else {
1103				bodyWriter.cancel()
1104				cs.abortRequestBodyWrite(errStopReqBodyWriteAndCancel)
1105			}
1106			cc.forgetStreamID(cs.ID)
1107			return nil, cs.getStartedWrite(), errTimeout
1108		case <-ctx.Done():
1109			if !hasBody || bodyWritten {
1110				cc.writeStreamReset(cs.ID, ErrCodeCancel, nil)
1111			} else {
1112				bodyWriter.cancel()
1113				cs.abortRequestBodyWrite(errStopReqBodyWriteAndCancel)
1114			}
1115			cc.forgetStreamID(cs.ID)
1116			return nil, cs.getStartedWrite(), ctx.Err()
1117		case <-req.Cancel:
1118			if !hasBody || bodyWritten {
1119				cc.writeStreamReset(cs.ID, ErrCodeCancel, nil)
1120			} else {
1121				bodyWriter.cancel()
1122				cs.abortRequestBodyWrite(errStopReqBodyWriteAndCancel)
1123			}
1124			cc.forgetStreamID(cs.ID)
1125			return nil, cs.getStartedWrite(), errRequestCanceled
1126		case <-cs.peerReset:
1127			// processResetStream already removed the
1128			// stream from the streams map; no need for
1129			// forgetStreamID.
1130			return nil, cs.getStartedWrite(), cs.resetErr
1131		case err := <-bodyWriter.resc:
1132			// Prefer the read loop's response, if available. Issue 16102.
1133			select {
1134			case re := <-readLoopResCh:
1135				return handleReadLoopResponse(re)
1136			default:
1137			}
1138			if err != nil {
1139				cc.forgetStreamID(cs.ID)
1140				return nil, cs.getStartedWrite(), err
1141			}
1142			bodyWritten = true
1143			if d := cc.responseHeaderTimeout(); d != 0 {
1144				timer := time.NewTimer(d)
1145				defer timer.Stop()
1146				respHeaderTimer = timer.C
1147			}
1148		}
1149	}
1150}
1151
1152// awaitOpenSlotForRequest waits until len(streams) < maxConcurrentStreams.
1153// Must hold cc.mu.
1154func (cc *ClientConn) awaitOpenSlotForRequest(req *http.Request) error {
1155	var waitingForConn chan struct{}
1156	var waitingForConnErr error // guarded by cc.mu
1157	for {
1158		cc.lastActive = time.Now()
1159		if cc.closed || !cc.canTakeNewRequestLocked() {
1160			if waitingForConn != nil {
1161				close(waitingForConn)
1162			}
1163			return errClientConnUnusable
1164		}
1165		cc.lastIdle = time.Time{}
1166		if int64(len(cc.streams))+1 <= int64(cc.maxConcurrentStreams) {
1167			if waitingForConn != nil {
1168				close(waitingForConn)
1169			}
1170			return nil
1171		}
1172		// Unfortunately, we cannot wait on a condition variable and channel at
1173		// the same time, so instead, we spin up a goroutine to check if the
1174		// request is canceled while we wait for a slot to open in the connection.
1175		if waitingForConn == nil {
1176			waitingForConn = make(chan struct{})
1177			go func() {
1178				if err := awaitRequestCancel(req, waitingForConn); err != nil {
1179					cc.mu.Lock()
1180					waitingForConnErr = err
1181					cc.cond.Broadcast()
1182					cc.mu.Unlock()
1183				}
1184			}()
1185		}
1186		cc.pendingRequests++
1187		cc.cond.Wait()
1188		cc.pendingRequests--
1189		if waitingForConnErr != nil {
1190			return waitingForConnErr
1191		}
1192	}
1193}
1194
1195// requires cc.wmu be held
1196func (cc *ClientConn) writeHeaders(streamID uint32, endStream bool, maxFrameSize int, hdrs []byte) error {
1197	first := true // first frame written (HEADERS is first, then CONTINUATION)
1198	for len(hdrs) > 0 && cc.werr == nil {
1199		chunk := hdrs
1200		if len(chunk) > maxFrameSize {
1201			chunk = chunk[:maxFrameSize]
1202		}
1203		hdrs = hdrs[len(chunk):]
1204		endHeaders := len(hdrs) == 0
1205		if first {
1206			cc.fr.WriteHeaders(HeadersFrameParam{
1207				StreamID:      streamID,
1208				BlockFragment: chunk,
1209				EndStream:     endStream,
1210				EndHeaders:    endHeaders,
1211			})
1212			first = false
1213		} else {
1214			cc.fr.WriteContinuation(streamID, endHeaders, chunk)
1215		}
1216	}
1217	// TODO(bradfitz): this Flush could potentially block (as
1218	// could the WriteHeaders call(s) above), which means they
1219	// wouldn't respond to Request.Cancel being readable. That's
1220	// rare, but this should probably be in a goroutine.
1221	cc.bw.Flush()
1222	return cc.werr
1223}
1224
1225// internal error values; they don't escape to callers
1226var (
1227	// abort request body write; don't send cancel
1228	errStopReqBodyWrite = errors.New("http2: aborting request body write")
1229
1230	// abort request body write, but send stream reset of cancel.
1231	errStopReqBodyWriteAndCancel = errors.New("http2: canceling request")
1232
1233	errReqBodyTooLong = errors.New("http2: request body larger than specified content length")
1234)
1235
1236func (cs *clientStream) writeRequestBody(body io.Reader, bodyCloser io.Closer) (err error) {
1237	cc := cs.cc
1238	sentEnd := false // whether we sent the final DATA frame w/ END_STREAM
1239	buf := cc.frameScratchBuffer()
1240	defer cc.putFrameScratchBuffer(buf)
1241
1242	defer func() {
1243		traceWroteRequest(cs.trace, err)
1244		// TODO: write h12Compare test showing whether
1245		// Request.Body is closed by the Transport,
1246		// and in multiple cases: server replies <=299 and >299
1247		// while still writing request body
1248		cerr := bodyCloser.Close()
1249		if err == nil {
1250			err = cerr
1251		}
1252	}()
1253
1254	req := cs.req
1255	hasTrailers := req.Trailer != nil
1256	remainLen := actualContentLength(req)
1257	hasContentLen := remainLen != -1
1258
1259	var sawEOF bool
1260	for !sawEOF {
1261		n, err := body.Read(buf[:len(buf)-1])
1262		if hasContentLen {
1263			remainLen -= int64(n)
1264			if remainLen == 0 && err == nil {
1265				// The request body's Content-Length was predeclared and
1266				// we just finished reading it all, but the underlying io.Reader
1267				// returned the final chunk with a nil error (which is one of
1268				// the two valid things a Reader can do at EOF). Because we'd prefer
1269				// to send the END_STREAM bit early, double-check that we're actually
1270				// at EOF. Subsequent reads should return (0, EOF) at this point.
1271				// If either value is different, we return an error in one of two ways below.
1272				var n1 int
1273				n1, err = body.Read(buf[n:])
1274				remainLen -= int64(n1)
1275			}
1276			if remainLen < 0 {
1277				err = errReqBodyTooLong
1278				cc.writeStreamReset(cs.ID, ErrCodeCancel, err)
1279				return err
1280			}
1281		}
1282		if err == io.EOF {
1283			sawEOF = true
1284			err = nil
1285		} else if err != nil {
1286			cc.writeStreamReset(cs.ID, ErrCodeCancel, err)
1287			return err
1288		}
1289
1290		remain := buf[:n]
1291		for len(remain) > 0 && err == nil {
1292			var allowed int32
1293			allowed, err = cs.awaitFlowControl(len(remain))
1294			switch {
1295			case err == errStopReqBodyWrite:
1296				return err
1297			case err == errStopReqBodyWriteAndCancel:
1298				cc.writeStreamReset(cs.ID, ErrCodeCancel, nil)
1299				return err
1300			case err != nil:
1301				return err
1302			}
1303			cc.wmu.Lock()
1304			data := remain[:allowed]
1305			remain = remain[allowed:]
1306			sentEnd = sawEOF && len(remain) == 0 && !hasTrailers
1307			err = cc.fr.WriteData(cs.ID, sentEnd, data)
1308			if err == nil {
1309				// TODO(bradfitz): this flush is for latency, not bandwidth.
1310				// Most requests won't need this. Make this opt-in or
1311				// opt-out?  Use some heuristic on the body type? Nagel-like
1312				// timers?  Based on 'n'? Only last chunk of this for loop,
1313				// unless flow control tokens are low? For now, always.
1314				// If we change this, see comment below.
1315				err = cc.bw.Flush()
1316			}
1317			cc.wmu.Unlock()
1318		}
1319		if err != nil {
1320			return err
1321		}
1322	}
1323
1324	if sentEnd {
1325		// Already sent END_STREAM (which implies we have no
1326		// trailers) and flushed, because currently all
1327		// WriteData frames above get a flush. So we're done.
1328		return nil
1329	}
1330
1331	var trls []byte
1332	if hasTrailers {
1333		cc.mu.Lock()
1334		trls, err = cc.encodeTrailers(req)
1335		cc.mu.Unlock()
1336		if err != nil {
1337			cc.writeStreamReset(cs.ID, ErrCodeInternal, err)
1338			cc.forgetStreamID(cs.ID)
1339			return err
1340		}
1341	}
1342
1343	cc.mu.Lock()
1344	maxFrameSize := int(cc.maxFrameSize)
1345	cc.mu.Unlock()
1346
1347	cc.wmu.Lock()
1348	defer cc.wmu.Unlock()
1349
1350	// Two ways to send END_STREAM: either with trailers, or
1351	// with an empty DATA frame.
1352	if len(trls) > 0 {
1353		err = cc.writeHeaders(cs.ID, true, maxFrameSize, trls)
1354	} else {
1355		err = cc.fr.WriteData(cs.ID, true, nil)
1356	}
1357	if ferr := cc.bw.Flush(); ferr != nil && err == nil {
1358		err = ferr
1359	}
1360	return err
1361}
1362
1363// awaitFlowControl waits for [1, min(maxBytes, cc.cs.maxFrameSize)] flow
1364// control tokens from the server.
1365// It returns either the non-zero number of tokens taken or an error
1366// if the stream is dead.
1367func (cs *clientStream) awaitFlowControl(maxBytes int) (taken int32, err error) {
1368	cc := cs.cc
1369	cc.mu.Lock()
1370	defer cc.mu.Unlock()
1371	for {
1372		if cc.closed {
1373			return 0, errClientConnClosed
1374		}
1375		if cs.stopReqBody != nil {
1376			return 0, cs.stopReqBody
1377		}
1378		if err := cs.checkResetOrDone(); err != nil {
1379			return 0, err
1380		}
1381		if a := cs.flow.available(); a > 0 {
1382			take := a
1383			if int(take) > maxBytes {
1384
1385				take = int32(maxBytes) // can't truncate int; take is int32
1386			}
1387			if take > int32(cc.maxFrameSize) {
1388				take = int32(cc.maxFrameSize)
1389			}
1390			cs.flow.take(take)
1391			return take, nil
1392		}
1393		cc.cond.Wait()
1394	}
1395}
1396
1397type badStringError struct {
1398	what string
1399	str  string
1400}
1401
1402func (e *badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) }
1403
1404// requires cc.mu be held.
1405func (cc *ClientConn) encodeHeaders(req *http.Request, addGzipHeader bool, trailers string, contentLength int64) ([]byte, error) {
1406	cc.hbuf.Reset()
1407
1408	host := req.Host
1409	if host == "" {
1410		host = req.URL.Host
1411	}
1412	host, err := httpguts.PunycodeHostPort(host)
1413	if err != nil {
1414		return nil, err
1415	}
1416
1417	var path string
1418	if req.Method != "CONNECT" {
1419		path = req.URL.RequestURI()
1420		if !validPseudoPath(path) {
1421			orig := path
1422			path = strings.TrimPrefix(path, req.URL.Scheme+"://"+host)
1423			if !validPseudoPath(path) {
1424				if req.URL.Opaque != "" {
1425					return nil, fmt.Errorf("invalid request :path %q from URL.Opaque = %q", orig, req.URL.Opaque)
1426				} else {
1427					return nil, fmt.Errorf("invalid request :path %q", orig)
1428				}
1429			}
1430		}
1431	}
1432
1433	// Check for any invalid headers and return an error before we
1434	// potentially pollute our hpack state. (We want to be able to
1435	// continue to reuse the hpack encoder for future requests)
1436	for k, vv := range req.Header {
1437		if !httpguts.ValidHeaderFieldName(k) {
1438			return nil, fmt.Errorf("invalid HTTP header name %q", k)
1439		}
1440		for _, v := range vv {
1441			if !httpguts.ValidHeaderFieldValue(v) {
1442				return nil, fmt.Errorf("invalid HTTP header value %q for header %q", v, k)
1443			}
1444		}
1445	}
1446
1447	enumerateHeaders := func(f func(name, value string)) {
1448		// 8.1.2.3 Request Pseudo-Header Fields
1449		// The :path pseudo-header field includes the path and query parts of the
1450		// target URI (the path-absolute production and optionally a '?' character
1451		// followed by the query production (see Sections 3.3 and 3.4 of
1452		// [RFC3986]).
1453		f(":authority", host)
1454		m := req.Method
1455		if m == "" {
1456			m = http.MethodGet
1457		}
1458		f(":method", m)
1459		if req.Method != "CONNECT" {
1460			f(":path", path)
1461			f(":scheme", req.URL.Scheme)
1462		}
1463		if trailers != "" {
1464			f("trailer", trailers)
1465		}
1466
1467		var didUA bool
1468		for k, vv := range req.Header {
1469			if strings.EqualFold(k, "host") || strings.EqualFold(k, "content-length") {
1470				// Host is :authority, already sent.
1471				// Content-Length is automatic, set below.
1472				continue
1473			} else if strings.EqualFold(k, "connection") || strings.EqualFold(k, "proxy-connection") ||
1474				strings.EqualFold(k, "transfer-encoding") || strings.EqualFold(k, "upgrade") ||
1475				strings.EqualFold(k, "keep-alive") {
1476				// Per 8.1.2.2 Connection-Specific Header
1477				// Fields, don't send connection-specific
1478				// fields. We have already checked if any
1479				// are error-worthy so just ignore the rest.
1480				continue
1481			} else if strings.EqualFold(k, "user-agent") {
1482				// Match Go's http1 behavior: at most one
1483				// User-Agent. If set to nil or empty string,
1484				// then omit it. Otherwise if not mentioned,
1485				// include the default (below).
1486				didUA = true
1487				if len(vv) < 1 {
1488					continue
1489				}
1490				vv = vv[:1]
1491				if vv[0] == "" {
1492					continue
1493				}
1494			} else if strings.EqualFold(k, "cookie") {
1495				// Per 8.1.2.5 To allow for better compression efficiency, the
1496				// Cookie header field MAY be split into separate header fields,
1497				// each with one or more cookie-pairs.
1498				for _, v := range vv {
1499					for {
1500						p := strings.IndexByte(v, ';')
1501						if p < 0 {
1502							break
1503						}
1504						f("cookie", v[:p])
1505						p++
1506						// strip space after semicolon if any.
1507						for p+1 <= len(v) && v[p] == ' ' {
1508							p++
1509						}
1510						v = v[p:]
1511					}
1512					if len(v) > 0 {
1513						f("cookie", v)
1514					}
1515				}
1516				continue
1517			}
1518
1519			for _, v := range vv {
1520				f(k, v)
1521			}
1522		}
1523		if shouldSendReqContentLength(req.Method, contentLength) {
1524			f("content-length", strconv.FormatInt(contentLength, 10))
1525		}
1526		if addGzipHeader {
1527			f("accept-encoding", "gzip")
1528		}
1529		if !didUA {
1530			f("user-agent", defaultUserAgent)
1531		}
1532	}
1533
1534	// Do a first pass over the headers counting bytes to ensure
1535	// we don't exceed cc.peerMaxHeaderListSize. This is done as a
1536	// separate pass before encoding the headers to prevent
1537	// modifying the hpack state.
1538	hlSize := uint64(0)
1539	enumerateHeaders(func(name, value string) {
1540		hf := hpack.HeaderField{Name: name, Value: value}
1541		hlSize += uint64(hf.Size())
1542	})
1543
1544	if hlSize > cc.peerMaxHeaderListSize {
1545		return nil, errRequestHeaderListSize
1546	}
1547
1548	trace := httptrace.ContextClientTrace(req.Context())
1549	traceHeaders := traceHasWroteHeaderField(trace)
1550
1551	// Header list size is ok. Write the headers.
1552	enumerateHeaders(func(name, value string) {
1553		name = strings.ToLower(name)
1554		cc.writeHeader(name, value)
1555		if traceHeaders {
1556			traceWroteHeaderField(trace, name, value)
1557		}
1558	})
1559
1560	return cc.hbuf.Bytes(), nil
1561}
1562
1563// shouldSendReqContentLength reports whether the http2.Transport should send
1564// a "content-length" request header. This logic is basically a copy of the net/http
1565// transferWriter.shouldSendContentLength.
1566// The contentLength is the corrected contentLength (so 0 means actually 0, not unknown).
1567// -1 means unknown.
1568func shouldSendReqContentLength(method string, contentLength int64) bool {
1569	if contentLength > 0 {
1570		return true
1571	}
1572	if contentLength < 0 {
1573		return false
1574	}
1575	// For zero bodies, whether we send a content-length depends on the method.
1576	// It also kinda doesn't matter for http2 either way, with END_STREAM.
1577	switch method {
1578	case "POST", "PUT", "PATCH":
1579		return true
1580	default:
1581		return false
1582	}
1583}
1584
1585// requires cc.mu be held.
1586func (cc *ClientConn) encodeTrailers(req *http.Request) ([]byte, error) {
1587	cc.hbuf.Reset()
1588
1589	hlSize := uint64(0)
1590	for k, vv := range req.Trailer {
1591		for _, v := range vv {
1592			hf := hpack.HeaderField{Name: k, Value: v}
1593			hlSize += uint64(hf.Size())
1594		}
1595	}
1596	if hlSize > cc.peerMaxHeaderListSize {
1597		return nil, errRequestHeaderListSize
1598	}
1599
1600	for k, vv := range req.Trailer {
1601		// Transfer-Encoding, etc.. have already been filtered at the
1602		// start of RoundTrip
1603		lowKey := strings.ToLower(k)
1604		for _, v := range vv {
1605			cc.writeHeader(lowKey, v)
1606		}
1607	}
1608	return cc.hbuf.Bytes(), nil
1609}
1610
1611func (cc *ClientConn) writeHeader(name, value string) {
1612	if VerboseLogs {
1613		log.Printf("http2: Transport encoding header %q = %q", name, value)
1614	}
1615	cc.henc.WriteField(hpack.HeaderField{Name: name, Value: value})
1616}
1617
1618type resAndError struct {
1619	res *http.Response
1620	err error
1621}
1622
1623// requires cc.mu be held.
1624func (cc *ClientConn) newStream() *clientStream {
1625	cs := &clientStream{
1626		cc:        cc,
1627		ID:        cc.nextStreamID,
1628		resc:      make(chan resAndError, 1),
1629		peerReset: make(chan struct{}),
1630		done:      make(chan struct{}),
1631	}
1632	cs.flow.add(int32(cc.initialWindowSize))
1633	cs.flow.setConnFlow(&cc.flow)
1634	cs.inflow.add(transportDefaultStreamFlow)
1635	cs.inflow.setConnFlow(&cc.inflow)
1636	cc.nextStreamID += 2
1637	cc.streams[cs.ID] = cs
1638	return cs
1639}
1640
1641func (cc *ClientConn) forgetStreamID(id uint32) {
1642	cc.streamByID(id, true)
1643}
1644
1645func (cc *ClientConn) streamByID(id uint32, andRemove bool) *clientStream {
1646	cc.mu.Lock()
1647	defer cc.mu.Unlock()
1648	cs := cc.streams[id]
1649	if andRemove && cs != nil && !cc.closed {
1650		cc.lastActive = time.Now()
1651		delete(cc.streams, id)
1652		if len(cc.streams) == 0 && cc.idleTimer != nil {
1653			cc.idleTimer.Reset(cc.idleTimeout)
1654			cc.lastIdle = time.Now()
1655		}
1656		close(cs.done)
1657		// Wake up checkResetOrDone via clientStream.awaitFlowControl and
1658		// wake up RoundTrip if there is a pending request.
1659		cc.cond.Broadcast()
1660	}
1661	return cs
1662}
1663
1664// clientConnReadLoop is the state owned by the clientConn's frame-reading readLoop.
1665type clientConnReadLoop struct {
1666	cc            *ClientConn
1667	closeWhenIdle bool
1668}
1669
1670// readLoop runs in its own goroutine and reads and dispatches frames.
1671func (cc *ClientConn) readLoop() {
1672	rl := &clientConnReadLoop{cc: cc}
1673	defer rl.cleanup()
1674	cc.readerErr = rl.run()
1675	if ce, ok := cc.readerErr.(ConnectionError); ok {
1676		cc.wmu.Lock()
1677		cc.fr.WriteGoAway(0, ErrCode(ce), nil)
1678		cc.wmu.Unlock()
1679	}
1680}
1681
1682// GoAwayError is returned by the Transport when the server closes the
1683// TCP connection after sending a GOAWAY frame.
1684type GoAwayError struct {
1685	LastStreamID uint32
1686	ErrCode      ErrCode
1687	DebugData    string
1688}
1689
1690func (e GoAwayError) Error() string {
1691	return fmt.Sprintf("http2: server sent GOAWAY and closed the connection; LastStreamID=%v, ErrCode=%v, debug=%q",
1692		e.LastStreamID, e.ErrCode, e.DebugData)
1693}
1694
1695func isEOFOrNetReadError(err error) bool {
1696	if err == io.EOF {
1697		return true
1698	}
1699	ne, ok := err.(*net.OpError)
1700	return ok && ne.Op == "read"
1701}
1702
1703func (rl *clientConnReadLoop) cleanup() {
1704	cc := rl.cc
1705	defer cc.tconn.Close()
1706	defer cc.t.connPool().MarkDead(cc)
1707	defer close(cc.readerDone)
1708
1709	if cc.idleTimer != nil {
1710		cc.idleTimer.Stop()
1711	}
1712
1713	// Close any response bodies if the server closes prematurely.
1714	// TODO: also do this if we've written the headers but not
1715	// gotten a response yet.
1716	err := cc.readerErr
1717	cc.mu.Lock()
1718	if cc.goAway != nil && isEOFOrNetReadError(err) {
1719		err = GoAwayError{
1720			LastStreamID: cc.goAway.LastStreamID,
1721			ErrCode:      cc.goAway.ErrCode,
1722			DebugData:    cc.goAwayDebug,
1723		}
1724	} else if err == io.EOF {
1725		err = io.ErrUnexpectedEOF
1726	}
1727	for _, cs := range cc.streams {
1728		cs.bufPipe.CloseWithError(err) // no-op if already closed
1729		select {
1730		case cs.resc <- resAndError{err: err}:
1731		default:
1732		}
1733		close(cs.done)
1734	}
1735	cc.closed = true
1736	cc.cond.Broadcast()
1737	cc.mu.Unlock()
1738}
1739
1740func (rl *clientConnReadLoop) run() error {
1741	cc := rl.cc
1742	rl.closeWhenIdle = cc.t.disableKeepAlives() || cc.singleUse
1743	gotReply := false // ever saw a HEADERS reply
1744	gotSettings := false
1745	for {
1746		f, err := cc.fr.ReadFrame()
1747		if err != nil {
1748			cc.vlogf("http2: Transport readFrame error on conn %p: (%T) %v", cc, err, err)
1749		}
1750		if se, ok := err.(StreamError); ok {
1751			if cs := cc.streamByID(se.StreamID, false); cs != nil {
1752				cs.cc.writeStreamReset(cs.ID, se.Code, err)
1753				cs.cc.forgetStreamID(cs.ID)
1754				if se.Cause == nil {
1755					se.Cause = cc.fr.errDetail
1756				}
1757				rl.endStreamError(cs, se)
1758			}
1759			continue
1760		} else if err != nil {
1761			return err
1762		}
1763		if VerboseLogs {
1764			cc.vlogf("http2: Transport received %s", summarizeFrame(f))
1765		}
1766		if !gotSettings {
1767			if _, ok := f.(*SettingsFrame); !ok {
1768				cc.logf("protocol error: received %T before a SETTINGS frame", f)
1769				return ConnectionError(ErrCodeProtocol)
1770			}
1771			gotSettings = true
1772		}
1773		maybeIdle := false // whether frame might transition us to idle
1774
1775		switch f := f.(type) {
1776		case *MetaHeadersFrame:
1777			err = rl.processHeaders(f)
1778			maybeIdle = true
1779			gotReply = true
1780		case *DataFrame:
1781			err = rl.processData(f)
1782			maybeIdle = true
1783		case *GoAwayFrame:
1784			err = rl.processGoAway(f)
1785			maybeIdle = true
1786		case *RSTStreamFrame:
1787			err = rl.processResetStream(f)
1788			maybeIdle = true
1789		case *SettingsFrame:
1790			err = rl.processSettings(f)
1791		case *PushPromiseFrame:
1792			err = rl.processPushPromise(f)
1793		case *WindowUpdateFrame:
1794			err = rl.processWindowUpdate(f)
1795		case *PingFrame:
1796			err = rl.processPing(f)
1797		default:
1798			cc.logf("Transport: unhandled response frame type %T", f)
1799		}
1800		if err != nil {
1801			if VerboseLogs {
1802				cc.vlogf("http2: Transport conn %p received error from processing frame %v: %v", cc, summarizeFrame(f), err)
1803			}
1804			return err
1805		}
1806		if rl.closeWhenIdle && gotReply && maybeIdle {
1807			cc.closeIfIdle()
1808		}
1809	}
1810}
1811
1812func (rl *clientConnReadLoop) processHeaders(f *MetaHeadersFrame) error {
1813	cc := rl.cc
1814	cs := cc.streamByID(f.StreamID, false)
1815	if cs == nil {
1816		// We'd get here if we canceled a request while the
1817		// server had its response still in flight. So if this
1818		// was just something we canceled, ignore it.
1819		return nil
1820	}
1821	if f.StreamEnded() {
1822		// Issue 20521: If the stream has ended, streamByID() causes
1823		// clientStream.done to be closed, which causes the request's bodyWriter
1824		// to be closed with an errStreamClosed, which may be received by
1825		// clientConn.RoundTrip before the result of processing these headers.
1826		// Deferring stream closure allows the header processing to occur first.
1827		// clientConn.RoundTrip may still receive the bodyWriter error first, but
1828		// the fix for issue 16102 prioritises any response.
1829		//
1830		// Issue 22413: If there is no request body, we should close the
1831		// stream before writing to cs.resc so that the stream is closed
1832		// immediately once RoundTrip returns.
1833		if cs.req.Body != nil {
1834			defer cc.forgetStreamID(f.StreamID)
1835		} else {
1836			cc.forgetStreamID(f.StreamID)
1837		}
1838	}
1839	if !cs.firstByte {
1840		if cs.trace != nil {
1841			// TODO(bradfitz): move first response byte earlier,
1842			// when we first read the 9 byte header, not waiting
1843			// until all the HEADERS+CONTINUATION frames have been
1844			// merged. This works for now.
1845			traceFirstResponseByte(cs.trace)
1846		}
1847		cs.firstByte = true
1848	}
1849	if !cs.pastHeaders {
1850		cs.pastHeaders = true
1851	} else {
1852		return rl.processTrailers(cs, f)
1853	}
1854
1855	res, err := rl.handleResponse(cs, f)
1856	if err != nil {
1857		if _, ok := err.(ConnectionError); ok {
1858			return err
1859		}
1860		// Any other error type is a stream error.
1861		cs.cc.writeStreamReset(f.StreamID, ErrCodeProtocol, err)
1862		cc.forgetStreamID(cs.ID)
1863		cs.resc <- resAndError{err: err}
1864		return nil // return nil from process* funcs to keep conn alive
1865	}
1866	if res == nil {
1867		// (nil, nil) special case. See handleResponse docs.
1868		return nil
1869	}
1870	cs.resTrailer = &res.Trailer
1871	cs.resc <- resAndError{res: res}
1872	return nil
1873}
1874
1875// may return error types nil, or ConnectionError. Any other error value
1876// is a StreamError of type ErrCodeProtocol. The returned error in that case
1877// is the detail.
1878//
1879// As a special case, handleResponse may return (nil, nil) to skip the
1880// frame (currently only used for 1xx responses).
1881func (rl *clientConnReadLoop) handleResponse(cs *clientStream, f *MetaHeadersFrame) (*http.Response, error) {
1882	if f.Truncated {
1883		return nil, errResponseHeaderListSize
1884	}
1885
1886	status := f.PseudoValue("status")
1887	if status == "" {
1888		return nil, errors.New("malformed response from server: missing status pseudo header")
1889	}
1890	statusCode, err := strconv.Atoi(status)
1891	if err != nil {
1892		return nil, errors.New("malformed response from server: malformed non-numeric status pseudo header")
1893	}
1894
1895	header := make(http.Header)
1896	res := &http.Response{
1897		Proto:      "HTTP/2.0",
1898		ProtoMajor: 2,
1899		Header:     header,
1900		StatusCode: statusCode,
1901		Status:     status + " " + http.StatusText(statusCode),
1902	}
1903	for _, hf := range f.RegularFields() {
1904		key := http.CanonicalHeaderKey(hf.Name)
1905		if key == "Trailer" {
1906			t := res.Trailer
1907			if t == nil {
1908				t = make(http.Header)
1909				res.Trailer = t
1910			}
1911			foreachHeaderElement(hf.Value, func(v string) {
1912				t[http.CanonicalHeaderKey(v)] = nil
1913			})
1914		} else {
1915			header[key] = append(header[key], hf.Value)
1916		}
1917	}
1918
1919	if statusCode >= 100 && statusCode <= 199 {
1920		cs.num1xx++
1921		const max1xxResponses = 5 // arbitrary bound on number of informational responses, same as net/http
1922		if cs.num1xx > max1xxResponses {
1923			return nil, errors.New("http2: too many 1xx informational responses")
1924		}
1925		if fn := cs.get1xxTraceFunc(); fn != nil {
1926			if err := fn(statusCode, textproto.MIMEHeader(header)); err != nil {
1927				return nil, err
1928			}
1929		}
1930		if statusCode == 100 {
1931			traceGot100Continue(cs.trace)
1932			if cs.on100 != nil {
1933				cs.on100() // forces any write delay timer to fire
1934			}
1935		}
1936		cs.pastHeaders = false // do it all again
1937		return nil, nil
1938	}
1939
1940	streamEnded := f.StreamEnded()
1941	isHead := cs.req.Method == "HEAD"
1942	if !streamEnded || isHead {
1943		res.ContentLength = -1
1944		if clens := res.Header["Content-Length"]; len(clens) == 1 {
1945			if clen64, err := strconv.ParseInt(clens[0], 10, 64); err == nil {
1946				res.ContentLength = clen64
1947			} else {
1948				// TODO: care? unlike http/1, it won't mess up our framing, so it's
1949				// more safe smuggling-wise to ignore.
1950			}
1951		} else if len(clens) > 1 {
1952			// TODO: care? unlike http/1, it won't mess up our framing, so it's
1953			// more safe smuggling-wise to ignore.
1954		}
1955	}
1956
1957	if streamEnded || isHead {
1958		res.Body = noBody
1959		return res, nil
1960	}
1961
1962	cs.bufPipe = pipe{b: &dataBuffer{expected: res.ContentLength}}
1963	cs.bytesRemain = res.ContentLength
1964	res.Body = transportResponseBody{cs}
1965	go cs.awaitRequestCancel(cs.req)
1966
1967	if cs.requestedGzip && res.Header.Get("Content-Encoding") == "gzip" {
1968		res.Header.Del("Content-Encoding")
1969		res.Header.Del("Content-Length")
1970		res.ContentLength = -1
1971		res.Body = &gzipReader{body: res.Body}
1972		res.Uncompressed = true
1973	}
1974	return res, nil
1975}
1976
1977func (rl *clientConnReadLoop) processTrailers(cs *clientStream, f *MetaHeadersFrame) error {
1978	if cs.pastTrailers {
1979		// Too many HEADERS frames for this stream.
1980		return ConnectionError(ErrCodeProtocol)
1981	}
1982	cs.pastTrailers = true
1983	if !f.StreamEnded() {
1984		// We expect that any headers for trailers also
1985		// has END_STREAM.
1986		return ConnectionError(ErrCodeProtocol)
1987	}
1988	if len(f.PseudoFields()) > 0 {
1989		// No pseudo header fields are defined for trailers.
1990		// TODO: ConnectionError might be overly harsh? Check.
1991		return ConnectionError(ErrCodeProtocol)
1992	}
1993
1994	trailer := make(http.Header)
1995	for _, hf := range f.RegularFields() {
1996		key := http.CanonicalHeaderKey(hf.Name)
1997		trailer[key] = append(trailer[key], hf.Value)
1998	}
1999	cs.trailer = trailer
2000
2001	rl.endStream(cs)
2002	return nil
2003}
2004
2005// transportResponseBody is the concrete type of Transport.RoundTrip's
2006// Response.Body. It is an io.ReadCloser. On Read, it reads from cs.body.
2007// On Close it sends RST_STREAM if EOF wasn't already seen.
2008type transportResponseBody struct {
2009	cs *clientStream
2010}
2011
2012func (b transportResponseBody) Read(p []byte) (n int, err error) {
2013	cs := b.cs
2014	cc := cs.cc
2015
2016	if cs.readErr != nil {
2017		return 0, cs.readErr
2018	}
2019	n, err = b.cs.bufPipe.Read(p)
2020	if cs.bytesRemain != -1 {
2021		if int64(n) > cs.bytesRemain {
2022			n = int(cs.bytesRemain)
2023			if err == nil {
2024				err = errors.New("net/http: server replied with more than declared Content-Length; truncated")
2025				cc.writeStreamReset(cs.ID, ErrCodeProtocol, err)
2026			}
2027			cs.readErr = err
2028			return int(cs.bytesRemain), err
2029		}
2030		cs.bytesRemain -= int64(n)
2031		if err == io.EOF && cs.bytesRemain > 0 {
2032			err = io.ErrUnexpectedEOF
2033			cs.readErr = err
2034			return n, err
2035		}
2036	}
2037	if n == 0 {
2038		// No flow control tokens to send back.
2039		return
2040	}
2041
2042	cc.mu.Lock()
2043	defer cc.mu.Unlock()
2044
2045	var connAdd, streamAdd int32
2046	// Check the conn-level first, before the stream-level.
2047	if v := cc.inflow.available(); v < transportDefaultConnFlow/2 {
2048		connAdd = transportDefaultConnFlow - v
2049		cc.inflow.add(connAdd)
2050	}
2051	if err == nil { // No need to refresh if the stream is over or failed.
2052		// Consider any buffered body data (read from the conn but not
2053		// consumed by the client) when computing flow control for this
2054		// stream.
2055		v := int(cs.inflow.available()) + cs.bufPipe.Len()
2056		if v < transportDefaultStreamFlow-transportDefaultStreamMinRefresh {
2057			streamAdd = int32(transportDefaultStreamFlow - v)
2058			cs.inflow.add(streamAdd)
2059		}
2060	}
2061	if connAdd != 0 || streamAdd != 0 {
2062		cc.wmu.Lock()
2063		defer cc.wmu.Unlock()
2064		if connAdd != 0 {
2065			cc.fr.WriteWindowUpdate(0, mustUint31(connAdd))
2066		}
2067		if streamAdd != 0 {
2068			cc.fr.WriteWindowUpdate(cs.ID, mustUint31(streamAdd))
2069		}
2070		cc.bw.Flush()
2071	}
2072	return
2073}
2074
2075var errClosedResponseBody = errors.New("http2: response body closed")
2076
2077func (b transportResponseBody) Close() error {
2078	cs := b.cs
2079	cc := cs.cc
2080
2081	serverSentStreamEnd := cs.bufPipe.Err() == io.EOF
2082	unread := cs.bufPipe.Len()
2083
2084	if unread > 0 || !serverSentStreamEnd {
2085		cc.mu.Lock()
2086		cc.wmu.Lock()
2087		if !serverSentStreamEnd {
2088			cc.fr.WriteRSTStream(cs.ID, ErrCodeCancel)
2089			cs.didReset = true
2090		}
2091		// Return connection-level flow control.
2092		if unread > 0 {
2093			cc.inflow.add(int32(unread))
2094			cc.fr.WriteWindowUpdate(0, uint32(unread))
2095		}
2096		cc.bw.Flush()
2097		cc.wmu.Unlock()
2098		cc.mu.Unlock()
2099	}
2100
2101	cs.bufPipe.BreakWithError(errClosedResponseBody)
2102	cc.forgetStreamID(cs.ID)
2103	return nil
2104}
2105
2106func (rl *clientConnReadLoop) processData(f *DataFrame) error {
2107	cc := rl.cc
2108	cs := cc.streamByID(f.StreamID, f.StreamEnded())
2109	data := f.Data()
2110	if cs == nil {
2111		cc.mu.Lock()
2112		neverSent := cc.nextStreamID
2113		cc.mu.Unlock()
2114		if f.StreamID >= neverSent {
2115			// We never asked for this.
2116			cc.logf("http2: Transport received unsolicited DATA frame; closing connection")
2117			return ConnectionError(ErrCodeProtocol)
2118		}
2119		// We probably did ask for this, but canceled. Just ignore it.
2120		// TODO: be stricter here? only silently ignore things which
2121		// we canceled, but not things which were closed normally
2122		// by the peer? Tough without accumulating too much state.
2123
2124		// But at least return their flow control:
2125		if f.Length > 0 {
2126			cc.mu.Lock()
2127			cc.inflow.add(int32(f.Length))
2128			cc.mu.Unlock()
2129
2130			cc.wmu.Lock()
2131			cc.fr.WriteWindowUpdate(0, uint32(f.Length))
2132			cc.bw.Flush()
2133			cc.wmu.Unlock()
2134		}
2135		return nil
2136	}
2137	if !cs.firstByte {
2138		cc.logf("protocol error: received DATA before a HEADERS frame")
2139		rl.endStreamError(cs, StreamError{
2140			StreamID: f.StreamID,
2141			Code:     ErrCodeProtocol,
2142		})
2143		return nil
2144	}
2145	if f.Length > 0 {
2146		if cs.req.Method == "HEAD" && len(data) > 0 {
2147			cc.logf("protocol error: received DATA on a HEAD request")
2148			rl.endStreamError(cs, StreamError{
2149				StreamID: f.StreamID,
2150				Code:     ErrCodeProtocol,
2151			})
2152			return nil
2153		}
2154		// Check connection-level flow control.
2155		cc.mu.Lock()
2156		if cs.inflow.available() >= int32(f.Length) {
2157			cs.inflow.take(int32(f.Length))
2158		} else {
2159			cc.mu.Unlock()
2160			return ConnectionError(ErrCodeFlowControl)
2161		}
2162		// Return any padded flow control now, since we won't
2163		// refund it later on body reads.
2164		var refund int
2165		if pad := int(f.Length) - len(data); pad > 0 {
2166			refund += pad
2167		}
2168		// Return len(data) now if the stream is already closed,
2169		// since data will never be read.
2170		didReset := cs.didReset
2171		if didReset {
2172			refund += len(data)
2173		}
2174		if refund > 0 {
2175			cc.inflow.add(int32(refund))
2176			cc.wmu.Lock()
2177			cc.fr.WriteWindowUpdate(0, uint32(refund))
2178			if !didReset {
2179				cs.inflow.add(int32(refund))
2180				cc.fr.WriteWindowUpdate(cs.ID, uint32(refund))
2181			}
2182			cc.bw.Flush()
2183			cc.wmu.Unlock()
2184		}
2185		cc.mu.Unlock()
2186
2187		if len(data) > 0 && !didReset {
2188			if _, err := cs.bufPipe.Write(data); err != nil {
2189				rl.endStreamError(cs, err)
2190				return err
2191			}
2192		}
2193	}
2194
2195	if f.StreamEnded() {
2196		rl.endStream(cs)
2197	}
2198	return nil
2199}
2200
2201var errInvalidTrailers = errors.New("http2: invalid trailers")
2202
2203func (rl *clientConnReadLoop) endStream(cs *clientStream) {
2204	// TODO: check that any declared content-length matches, like
2205	// server.go's (*stream).endStream method.
2206	rl.endStreamError(cs, nil)
2207}
2208
2209func (rl *clientConnReadLoop) endStreamError(cs *clientStream, err error) {
2210	var code func()
2211	if err == nil {
2212		err = io.EOF
2213		code = cs.copyTrailers
2214	}
2215	if isConnectionCloseRequest(cs.req) {
2216		rl.closeWhenIdle = true
2217	}
2218	cs.bufPipe.closeWithErrorAndCode(err, code)
2219
2220	select {
2221	case cs.resc <- resAndError{err: err}:
2222	default:
2223	}
2224}
2225
2226func (cs *clientStream) copyTrailers() {
2227	for k, vv := range cs.trailer {
2228		t := cs.resTrailer
2229		if *t == nil {
2230			*t = make(http.Header)
2231		}
2232		(*t)[k] = vv
2233	}
2234}
2235
2236func (rl *clientConnReadLoop) processGoAway(f *GoAwayFrame) error {
2237	cc := rl.cc
2238	cc.t.connPool().MarkDead(cc)
2239	if f.ErrCode != 0 {
2240		// TODO: deal with GOAWAY more. particularly the error code
2241		cc.vlogf("transport got GOAWAY with error code = %v", f.ErrCode)
2242	}
2243	cc.setGoAway(f)
2244	return nil
2245}
2246
2247func (rl *clientConnReadLoop) processSettings(f *SettingsFrame) error {
2248	cc := rl.cc
2249	cc.mu.Lock()
2250	defer cc.mu.Unlock()
2251
2252	if f.IsAck() {
2253		if cc.wantSettingsAck {
2254			cc.wantSettingsAck = false
2255			return nil
2256		}
2257		return ConnectionError(ErrCodeProtocol)
2258	}
2259
2260	err := f.ForeachSetting(func(s Setting) error {
2261		switch s.ID {
2262		case SettingMaxFrameSize:
2263			cc.maxFrameSize = s.Val
2264		case SettingMaxConcurrentStreams:
2265			cc.maxConcurrentStreams = s.Val
2266		case SettingMaxHeaderListSize:
2267			cc.peerMaxHeaderListSize = uint64(s.Val)
2268		case SettingInitialWindowSize:
2269			// Values above the maximum flow-control
2270			// window size of 2^31-1 MUST be treated as a
2271			// connection error (Section 5.4.1) of type
2272			// FLOW_CONTROL_ERROR.
2273			if s.Val > math.MaxInt32 {
2274				return ConnectionError(ErrCodeFlowControl)
2275			}
2276
2277			// Adjust flow control of currently-open
2278			// frames by the difference of the old initial
2279			// window size and this one.
2280			delta := int32(s.Val) - int32(cc.initialWindowSize)
2281			for _, cs := range cc.streams {
2282				cs.flow.add(delta)
2283			}
2284			cc.cond.Broadcast()
2285
2286			cc.initialWindowSize = s.Val
2287		default:
2288			// TODO(bradfitz): handle more settings? SETTINGS_HEADER_TABLE_SIZE probably.
2289			cc.vlogf("Unhandled Setting: %v", s)
2290		}
2291		return nil
2292	})
2293	if err != nil {
2294		return err
2295	}
2296
2297	cc.wmu.Lock()
2298	defer cc.wmu.Unlock()
2299
2300	cc.fr.WriteSettingsAck()
2301	cc.bw.Flush()
2302	return cc.werr
2303}
2304
2305func (rl *clientConnReadLoop) processWindowUpdate(f *WindowUpdateFrame) error {
2306	cc := rl.cc
2307	cs := cc.streamByID(f.StreamID, false)
2308	if f.StreamID != 0 && cs == nil {
2309		return nil
2310	}
2311
2312	cc.mu.Lock()
2313	defer cc.mu.Unlock()
2314
2315	fl := &cc.flow
2316	if cs != nil {
2317		fl = &cs.flow
2318	}
2319	if !fl.add(int32(f.Increment)) {
2320		return ConnectionError(ErrCodeFlowControl)
2321	}
2322	cc.cond.Broadcast()
2323	return nil
2324}
2325
2326func (rl *clientConnReadLoop) processResetStream(f *RSTStreamFrame) error {
2327	cs := rl.cc.streamByID(f.StreamID, true)
2328	if cs == nil {
2329		// TODO: return error if server tries to RST_STEAM an idle stream
2330		return nil
2331	}
2332	select {
2333	case <-cs.peerReset:
2334		// Already reset.
2335		// This is the only goroutine
2336		// which closes this, so there
2337		// isn't a race.
2338	default:
2339		err := streamError(cs.ID, f.ErrCode)
2340		cs.resetErr = err
2341		close(cs.peerReset)
2342		cs.bufPipe.CloseWithError(err)
2343		cs.cc.cond.Broadcast() // wake up checkResetOrDone via clientStream.awaitFlowControl
2344	}
2345	return nil
2346}
2347
2348// Ping sends a PING frame to the server and waits for the ack.
2349func (cc *ClientConn) Ping(ctx context.Context) error {
2350	c := make(chan struct{})
2351	// Generate a random payload
2352	var p [8]byte
2353	for {
2354		if _, err := rand.Read(p[:]); err != nil {
2355			return err
2356		}
2357		cc.mu.Lock()
2358		// check for dup before insert
2359		if _, found := cc.pings[p]; !found {
2360			cc.pings[p] = c
2361			cc.mu.Unlock()
2362			break
2363		}
2364		cc.mu.Unlock()
2365	}
2366	cc.wmu.Lock()
2367	if err := cc.fr.WritePing(false, p); err != nil {
2368		cc.wmu.Unlock()
2369		return err
2370	}
2371	if err := cc.bw.Flush(); err != nil {
2372		cc.wmu.Unlock()
2373		return err
2374	}
2375	cc.wmu.Unlock()
2376	select {
2377	case <-c:
2378		return nil
2379	case <-ctx.Done():
2380		return ctx.Err()
2381	case <-cc.readerDone:
2382		// connection closed
2383		return cc.readerErr
2384	}
2385}
2386
2387func (rl *clientConnReadLoop) processPing(f *PingFrame) error {
2388	if f.IsAck() {
2389		cc := rl.cc
2390		cc.mu.Lock()
2391		defer cc.mu.Unlock()
2392		// If ack, notify listener if any
2393		if c, ok := cc.pings[f.Data]; ok {
2394			close(c)
2395			delete(cc.pings, f.Data)
2396		}
2397		return nil
2398	}
2399	cc := rl.cc
2400	cc.wmu.Lock()
2401	defer cc.wmu.Unlock()
2402	if err := cc.fr.WritePing(true, f.Data); err != nil {
2403		return err
2404	}
2405	return cc.bw.Flush()
2406}
2407
2408func (rl *clientConnReadLoop) processPushPromise(f *PushPromiseFrame) error {
2409	// We told the peer we don't want them.
2410	// Spec says:
2411	// "PUSH_PROMISE MUST NOT be sent if the SETTINGS_ENABLE_PUSH
2412	// setting of the peer endpoint is set to 0. An endpoint that
2413	// has set this setting and has received acknowledgement MUST
2414	// treat the receipt of a PUSH_PROMISE frame as a connection
2415	// error (Section 5.4.1) of type PROTOCOL_ERROR."
2416	return ConnectionError(ErrCodeProtocol)
2417}
2418
2419func (cc *ClientConn) writeStreamReset(streamID uint32, code ErrCode, err error) {
2420	// TODO: map err to more interesting error codes, once the
2421	// HTTP community comes up with some. But currently for
2422	// RST_STREAM there's no equivalent to GOAWAY frame's debug
2423	// data, and the error codes are all pretty vague ("cancel").
2424	cc.wmu.Lock()
2425	cc.fr.WriteRSTStream(streamID, code)
2426	cc.bw.Flush()
2427	cc.wmu.Unlock()
2428}
2429
2430var (
2431	errResponseHeaderListSize = errors.New("http2: response header list larger than advertised limit")
2432	errRequestHeaderListSize  = errors.New("http2: request header list larger than peer's advertised limit")
2433	errPseudoTrailers         = errors.New("http2: invalid pseudo header in trailers")
2434)
2435
2436func (cc *ClientConn) logf(format string, args ...interface{}) {
2437	cc.t.logf(format, args...)
2438}
2439
2440func (cc *ClientConn) vlogf(format string, args ...interface{}) {
2441	cc.t.vlogf(format, args...)
2442}
2443
2444func (t *Transport) vlogf(format string, args ...interface{}) {
2445	if VerboseLogs {
2446		t.logf(format, args...)
2447	}
2448}
2449
2450func (t *Transport) logf(format string, args ...interface{}) {
2451	log.Printf(format, args...)
2452}
2453
2454var noBody io.ReadCloser = ioutil.NopCloser(bytes.NewReader(nil))
2455
2456func strSliceContains(ss []string, s string) bool {
2457	for _, v := range ss {
2458		if v == s {
2459			return true
2460		}
2461	}
2462	return false
2463}
2464
2465type erringRoundTripper struct{ err error }
2466
2467func (rt erringRoundTripper) RoundTrip(*http.Request) (*http.Response, error) { return nil, rt.err }
2468
2469// gzipReader wraps a response body so it can lazily
2470// call gzip.NewReader on the first call to Read
2471type gzipReader struct {
2472	body io.ReadCloser // underlying Response.Body
2473	zr   *gzip.Reader  // lazily-initialized gzip reader
2474	zerr error         // sticky error
2475}
2476
2477func (gz *gzipReader) Read(p []byte) (n int, err error) {
2478	if gz.zerr != nil {
2479		return 0, gz.zerr
2480	}
2481	if gz.zr == nil {
2482		gz.zr, err = gzip.NewReader(gz.body)
2483		if err != nil {
2484			gz.zerr = err
2485			return 0, err
2486		}
2487	}
2488	return gz.zr.Read(p)
2489}
2490
2491func (gz *gzipReader) Close() error {
2492	return gz.body.Close()
2493}
2494
2495type errorReader struct{ err error }
2496
2497func (r errorReader) Read(p []byte) (int, error) { return 0, r.err }
2498
2499// bodyWriterState encapsulates various state around the Transport's writing
2500// of the request body, particularly regarding doing delayed writes of the body
2501// when the request contains "Expect: 100-continue".
2502type bodyWriterState struct {
2503	cs     *clientStream
2504	timer  *time.Timer   // if non-nil, we're doing a delayed write
2505	fnonce *sync.Once    // to call fn with
2506	fn     func()        // the code to run in the goroutine, writing the body
2507	resc   chan error    // result of fn's execution
2508	delay  time.Duration // how long we should delay a delayed write for
2509}
2510
2511func (t *Transport) getBodyWriterState(cs *clientStream, body io.Reader) (s bodyWriterState) {
2512	s.cs = cs
2513	if body == nil {
2514		return
2515	}
2516	resc := make(chan error, 1)
2517	s.resc = resc
2518	s.fn = func() {
2519		cs.cc.mu.Lock()
2520		cs.startedWrite = true
2521		cs.cc.mu.Unlock()
2522		resc <- cs.writeRequestBody(body, cs.req.Body)
2523	}
2524	s.delay = t.expectContinueTimeout()
2525	if s.delay == 0 ||
2526		!httpguts.HeaderValuesContainsToken(
2527			cs.req.Header["Expect"],
2528			"100-continue") {
2529		return
2530	}
2531	s.fnonce = new(sync.Once)
2532
2533	// Arm the timer with a very large duration, which we'll
2534	// intentionally lower later. It has to be large now because
2535	// we need a handle to it before writing the headers, but the
2536	// s.delay value is defined to not start until after the
2537	// request headers were written.
2538	const hugeDuration = 365 * 24 * time.Hour
2539	s.timer = time.AfterFunc(hugeDuration, func() {
2540		s.fnonce.Do(s.fn)
2541	})
2542	return
2543}
2544
2545func (s bodyWriterState) cancel() {
2546	if s.timer != nil {
2547		s.timer.Stop()
2548	}
2549}
2550
2551func (s bodyWriterState) on100() {
2552	if s.timer == nil {
2553		// If we didn't do a delayed write, ignore the server's
2554		// bogus 100 continue response.
2555		return
2556	}
2557	s.timer.Stop()
2558	go func() { s.fnonce.Do(s.fn) }()
2559}
2560
2561// scheduleBodyWrite starts writing the body, either immediately (in
2562// the common case) or after the delay timeout. It should not be
2563// called until after the headers have been written.
2564func (s bodyWriterState) scheduleBodyWrite() {
2565	if s.timer == nil {
2566		// We're not doing a delayed write (see
2567		// getBodyWriterState), so just start the writing
2568		// goroutine immediately.
2569		go s.fn()
2570		return
2571	}
2572	traceWait100Continue(s.cs.trace)
2573	if s.timer.Stop() {
2574		s.timer.Reset(s.delay)
2575	}
2576}
2577
2578// isConnectionCloseRequest reports whether req should use its own
2579// connection for a single request and then close the connection.
2580func isConnectionCloseRequest(req *http.Request) bool {
2581	return req.Close || httpguts.HeaderValuesContainsToken(req.Header["Connection"], "close")
2582}
2583
2584// registerHTTPSProtocol calls Transport.RegisterProtocol but
2585// converting panics into errors.
2586func registerHTTPSProtocol(t *http.Transport, rt noDialH2RoundTripper) (err error) {
2587	defer func() {
2588		if e := recover(); e != nil {
2589			err = fmt.Errorf("%v", e)
2590		}
2591	}()
2592	t.RegisterProtocol("https", rt)
2593	return nil
2594}
2595
2596// noDialH2RoundTripper is a RoundTripper which only tries to complete the request
2597// if there's already has a cached connection to the host.
2598// (The field is exported so it can be accessed via reflect from net/http; tested
2599// by TestNoDialH2RoundTripperType)
2600type noDialH2RoundTripper struct{ *Transport }
2601
2602func (rt noDialH2RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
2603	res, err := rt.Transport.RoundTrip(req)
2604	if isNoCachedConnError(err) {
2605		return nil, http.ErrSkipAltProtocol
2606	}
2607	return res, err
2608}
2609
2610func (t *Transport) idleConnTimeout() time.Duration {
2611	if t.t1 != nil {
2612		return t.t1.IdleConnTimeout
2613	}
2614	return 0
2615}
2616
2617func traceGetConn(req *http.Request, hostPort string) {
2618	trace := httptrace.ContextClientTrace(req.Context())
2619	if trace == nil || trace.GetConn == nil {
2620		return
2621	}
2622	trace.GetConn(hostPort)
2623}
2624
2625func traceGotConn(req *http.Request, cc *ClientConn, reused bool) {
2626	trace := httptrace.ContextClientTrace(req.Context())
2627	if trace == nil || trace.GotConn == nil {
2628		return
2629	}
2630	ci := httptrace.GotConnInfo{Conn: cc.tconn}
2631	ci.Reused = reused
2632	cc.mu.Lock()
2633	ci.WasIdle = len(cc.streams) == 0 && reused
2634	if ci.WasIdle && !cc.lastActive.IsZero() {
2635		ci.IdleTime = time.Now().Sub(cc.lastActive)
2636	}
2637	cc.mu.Unlock()
2638
2639	trace.GotConn(ci)
2640}
2641
2642func traceWroteHeaders(trace *httptrace.ClientTrace) {
2643	if trace != nil && trace.WroteHeaders != nil {
2644		trace.WroteHeaders()
2645	}
2646}
2647
2648func traceGot100Continue(trace *httptrace.ClientTrace) {
2649	if trace != nil && trace.Got100Continue != nil {
2650		trace.Got100Continue()
2651	}
2652}
2653
2654func traceWait100Continue(trace *httptrace.ClientTrace) {
2655	if trace != nil && trace.Wait100Continue != nil {
2656		trace.Wait100Continue()
2657	}
2658}
2659
2660func traceWroteRequest(trace *httptrace.ClientTrace, err error) {
2661	if trace != nil && trace.WroteRequest != nil {
2662		trace.WroteRequest(httptrace.WroteRequestInfo{Err: err})
2663	}
2664}
2665
2666func traceFirstResponseByte(trace *httptrace.ClientTrace) {
2667	if trace != nil && trace.GotFirstResponseByte != nil {
2668		trace.GotFirstResponseByte()
2669	}
2670}
2671