podman

Форк
0
3419 строк · 93.8 Кб
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

7
package http2
8

9
import (
10
	"bufio"
11
	"bytes"
12
	"compress/gzip"
13
	"context"
14
	"crypto/rand"
15
	"crypto/tls"
16
	"errors"
17
	"fmt"
18
	"io"
19
	"io/fs"
20
	"log"
21
	"math"
22
	"math/bits"
23
	mathrand "math/rand"
24
	"net"
25
	"net/http"
26
	"net/http/httptrace"
27
	"net/textproto"
28
	"os"
29
	"sort"
30
	"strconv"
31
	"strings"
32
	"sync"
33
	"sync/atomic"
34
	"time"
35

36
	"golang.org/x/net/http/httpguts"
37
	"golang.org/x/net/http2/hpack"
38
	"golang.org/x/net/idna"
39
)
40

41
const (
42
	// transportDefaultConnFlow is how many connection-level flow control
43
	// tokens we give the server at start-up, past the default 64k.
44
	transportDefaultConnFlow = 1 << 30
45

46
	// transportDefaultStreamFlow is how many stream-level flow
47
	// control tokens we announce to the peer, and how many bytes
48
	// we buffer per stream.
49
	transportDefaultStreamFlow = 4 << 20
50

51
	defaultUserAgent = "Go-http-client/2.0"
52

53
	// initialMaxConcurrentStreams is a connections maxConcurrentStreams until
54
	// it's received servers initial SETTINGS frame, which corresponds with the
55
	// spec's minimum recommended value.
56
	initialMaxConcurrentStreams = 100
57

58
	// defaultMaxConcurrentStreams is a connections default maxConcurrentStreams
59
	// if the server doesn't include one in its initial SETTINGS frame.
60
	defaultMaxConcurrentStreams = 1000
61
)
62

63
// Transport is an HTTP/2 Transport.
64
//
65
// A Transport internally caches connections to servers. It is safe
66
// for concurrent use by multiple goroutines.
67
type Transport struct {
68
	// DialTLSContext specifies an optional dial function with context for
69
	// creating TLS connections for requests.
70
	//
71
	// If DialTLSContext and DialTLS is nil, tls.Dial is used.
72
	//
73
	// If the returned net.Conn has a ConnectionState method like tls.Conn,
74
	// it will be used to set http.Response.TLS.
75
	DialTLSContext func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error)
76

77
	// DialTLS specifies an optional dial function for creating
78
	// TLS connections for requests.
79
	//
80
	// If DialTLSContext and DialTLS is nil, tls.Dial is used.
81
	//
82
	// Deprecated: Use DialTLSContext instead, which allows the transport
83
	// to cancel dials as soon as they are no longer needed.
84
	// If both are set, DialTLSContext takes priority.
85
	DialTLS func(network, addr string, cfg *tls.Config) (net.Conn, error)
86

87
	// TLSClientConfig specifies the TLS configuration to use with
88
	// tls.Client. If nil, the default configuration is used.
89
	TLSClientConfig *tls.Config
90

91
	// ConnPool optionally specifies an alternate connection pool to use.
92
	// If nil, the default is used.
93
	ConnPool ClientConnPool
94

95
	// DisableCompression, if true, prevents the Transport from
96
	// requesting compression with an "Accept-Encoding: gzip"
97
	// request header when the Request contains no existing
98
	// Accept-Encoding value. If the Transport requests gzip on
99
	// its own and gets a gzipped response, it's transparently
100
	// decoded in the Response.Body. However, if the user
101
	// explicitly requested gzip it is not automatically
102
	// uncompressed.
103
	DisableCompression bool
104

105
	// AllowHTTP, if true, permits HTTP/2 requests using the insecure,
106
	// plain-text "http" scheme. Note that this does not enable h2c support.
107
	AllowHTTP bool
108

109
	// MaxHeaderListSize is the http2 SETTINGS_MAX_HEADER_LIST_SIZE to
110
	// send in the initial settings frame. It is how many bytes
111
	// of response headers are allowed. Unlike the http2 spec, zero here
112
	// means to use a default limit (currently 10MB). If you actually
113
	// want to advertise an unlimited value to the peer, Transport
114
	// interprets the highest possible value here (0xffffffff or 1<<32-1)
115
	// to mean no limit.
116
	MaxHeaderListSize uint32
117

118
	// MaxReadFrameSize is the http2 SETTINGS_MAX_FRAME_SIZE to send in the
119
	// initial settings frame. It is the size in bytes of the largest frame
120
	// payload that the sender is willing to receive. If 0, no setting is
121
	// sent, and the value is provided by the peer, which should be 16384
122
	// according to the spec:
123
	// https://datatracker.ietf.org/doc/html/rfc7540#section-6.5.2.
124
	// Values are bounded in the range 16k to 16M.
125
	MaxReadFrameSize uint32
126

127
	// MaxDecoderHeaderTableSize optionally specifies the http2
128
	// SETTINGS_HEADER_TABLE_SIZE to send in the initial settings frame. It
129
	// informs the remote endpoint of the maximum size of the header compression
130
	// table used to decode header blocks, in octets. If zero, the default value
131
	// of 4096 is used.
132
	MaxDecoderHeaderTableSize uint32
133

134
	// MaxEncoderHeaderTableSize optionally specifies an upper limit for the
135
	// header compression table used for encoding request headers. Received
136
	// SETTINGS_HEADER_TABLE_SIZE settings are capped at this limit. If zero,
137
	// the default value of 4096 is used.
138
	MaxEncoderHeaderTableSize uint32
139

140
	// StrictMaxConcurrentStreams controls whether the server's
141
	// SETTINGS_MAX_CONCURRENT_STREAMS should be respected
142
	// globally. If false, new TCP connections are created to the
143
	// server as needed to keep each under the per-connection
144
	// SETTINGS_MAX_CONCURRENT_STREAMS limit. If true, the
145
	// server's SETTINGS_MAX_CONCURRENT_STREAMS is interpreted as
146
	// a global limit and callers of RoundTrip block when needed,
147
	// waiting for their turn.
148
	StrictMaxConcurrentStreams bool
149

150
	// IdleConnTimeout is the maximum amount of time an idle
151
	// (keep-alive) connection will remain idle before closing
152
	// itself.
153
	// Zero means no limit.
154
	IdleConnTimeout time.Duration
155

156
	// ReadIdleTimeout is the timeout after which a health check using ping
157
	// frame will be carried out if no frame is received on the connection.
158
	// Note that a ping response will is considered a received frame, so if
159
	// there is no other traffic on the connection, the health check will
160
	// be performed every ReadIdleTimeout interval.
161
	// If zero, no health check is performed.
162
	ReadIdleTimeout time.Duration
163

164
	// PingTimeout is the timeout after which the connection will be closed
165
	// if a response to Ping is not received.
166
	// Defaults to 15s.
167
	PingTimeout time.Duration
168

169
	// WriteByteTimeout is the timeout after which the connection will be
170
	// closed no data can be written to it. The timeout begins when data is
171
	// available to write, and is extended whenever any bytes are written.
172
	WriteByteTimeout time.Duration
173

174
	// CountError, if non-nil, is called on HTTP/2 transport errors.
175
	// It's intended to increment a metric for monitoring, such
176
	// as an expvar or Prometheus metric.
177
	// The errType consists of only ASCII word characters.
178
	CountError func(errType string)
179

180
	// t1, if non-nil, is the standard library Transport using
181
	// this transport. Its settings are used (but not its
182
	// RoundTrip method, etc).
183
	t1 *http.Transport
184

185
	connPoolOnce  sync.Once
186
	connPoolOrDef ClientConnPool // non-nil version of ConnPool
187

188
	syncHooks *testSyncHooks
189
}
190

191
func (t *Transport) maxHeaderListSize() uint32 {
192
	if t.MaxHeaderListSize == 0 {
193
		return 10 << 20
194
	}
195
	if t.MaxHeaderListSize == 0xffffffff {
196
		return 0
197
	}
198
	return t.MaxHeaderListSize
199
}
200

201
func (t *Transport) maxFrameReadSize() uint32 {
202
	if t.MaxReadFrameSize == 0 {
203
		return 0 // use the default provided by the peer
204
	}
205
	if t.MaxReadFrameSize < minMaxFrameSize {
206
		return minMaxFrameSize
207
	}
208
	if t.MaxReadFrameSize > maxFrameSize {
209
		return maxFrameSize
210
	}
211
	return t.MaxReadFrameSize
212
}
213

214
func (t *Transport) disableCompression() bool {
215
	return t.DisableCompression || (t.t1 != nil && t.t1.DisableCompression)
216
}
217

218
func (t *Transport) pingTimeout() time.Duration {
219
	if t.PingTimeout == 0 {
220
		return 15 * time.Second
221
	}
222
	return t.PingTimeout
223

224
}
225

226
// ConfigureTransport configures a net/http HTTP/1 Transport to use HTTP/2.
227
// It returns an error if t1 has already been HTTP/2-enabled.
228
//
229
// Use ConfigureTransports instead to configure the HTTP/2 Transport.
230
func ConfigureTransport(t1 *http.Transport) error {
231
	_, err := ConfigureTransports(t1)
232
	return err
233
}
234

235
// ConfigureTransports configures a net/http HTTP/1 Transport to use HTTP/2.
236
// It returns a new HTTP/2 Transport for further configuration.
237
// It returns an error if t1 has already been HTTP/2-enabled.
238
func ConfigureTransports(t1 *http.Transport) (*Transport, error) {
239
	return configureTransports(t1)
240
}
241

242
func configureTransports(t1 *http.Transport) (*Transport, error) {
243
	connPool := new(clientConnPool)
244
	t2 := &Transport{
245
		ConnPool: noDialClientConnPool{connPool},
246
		t1:       t1,
247
	}
248
	connPool.t = t2
249
	if err := registerHTTPSProtocol(t1, noDialH2RoundTripper{t2}); err != nil {
250
		return nil, err
251
	}
252
	if t1.TLSClientConfig == nil {
253
		t1.TLSClientConfig = new(tls.Config)
254
	}
255
	if !strSliceContains(t1.TLSClientConfig.NextProtos, "h2") {
256
		t1.TLSClientConfig.NextProtos = append([]string{"h2"}, t1.TLSClientConfig.NextProtos...)
257
	}
258
	if !strSliceContains(t1.TLSClientConfig.NextProtos, "http/1.1") {
259
		t1.TLSClientConfig.NextProtos = append(t1.TLSClientConfig.NextProtos, "http/1.1")
260
	}
261
	upgradeFn := func(authority string, c *tls.Conn) http.RoundTripper {
262
		addr := authorityAddr("https", authority)
263
		if used, err := connPool.addConnIfNeeded(addr, t2, c); err != nil {
264
			go c.Close()
265
			return erringRoundTripper{err}
266
		} else if !used {
267
			// Turns out we don't need this c.
268
			// For example, two goroutines made requests to the same host
269
			// at the same time, both kicking off TCP dials. (since protocol
270
			// was unknown)
271
			go c.Close()
272
		}
273
		return t2
274
	}
275
	if m := t1.TLSNextProto; len(m) == 0 {
276
		t1.TLSNextProto = map[string]func(string, *tls.Conn) http.RoundTripper{
277
			"h2": upgradeFn,
278
		}
279
	} else {
280
		m["h2"] = upgradeFn
281
	}
282
	return t2, nil
283
}
284

285
func (t *Transport) connPool() ClientConnPool {
286
	t.connPoolOnce.Do(t.initConnPool)
287
	return t.connPoolOrDef
288
}
289

290
func (t *Transport) initConnPool() {
291
	if t.ConnPool != nil {
292
		t.connPoolOrDef = t.ConnPool
293
	} else {
294
		t.connPoolOrDef = &clientConnPool{t: t}
295
	}
296
}
297

298
// ClientConn is the state of a single HTTP/2 client connection to an
299
// HTTP/2 server.
300
type ClientConn struct {
301
	t             *Transport
302
	tconn         net.Conn             // usually *tls.Conn, except specialized impls
303
	tlsState      *tls.ConnectionState // nil only for specialized impls
304
	reused        uint32               // whether conn is being reused; atomic
305
	singleUse     bool                 // whether being used for a single http.Request
306
	getConnCalled bool                 // used by clientConnPool
307

308
	// readLoop goroutine fields:
309
	readerDone chan struct{} // closed on error
310
	readerErr  error         // set before readerDone is closed
311

312
	idleTimeout time.Duration // or 0 for never
313
	idleTimer   timer
314

315
	mu              sync.Mutex // guards following
316
	cond            *sync.Cond // hold mu; broadcast on flow/closed changes
317
	flow            outflow    // our conn-level flow control quota (cs.outflow is per stream)
318
	inflow          inflow     // peer's conn-level flow control
319
	doNotReuse      bool       // whether conn is marked to not be reused for any future requests
320
	closing         bool
321
	closed          bool
322
	seenSettings    bool                     // true if we've seen a settings frame, false otherwise
323
	wantSettingsAck bool                     // we sent a SETTINGS frame and haven't heard back
324
	goAway          *GoAwayFrame             // if non-nil, the GoAwayFrame we received
325
	goAwayDebug     string                   // goAway frame's debug data, retained as a string
326
	streams         map[uint32]*clientStream // client-initiated
327
	streamsReserved int                      // incr by ReserveNewRequest; decr on RoundTrip
328
	nextStreamID    uint32
329
	pendingRequests int                       // requests blocked and waiting to be sent because len(streams) == maxConcurrentStreams
330
	pings           map[[8]byte]chan struct{} // in flight ping data to notification channel
331
	br              *bufio.Reader
332
	lastActive      time.Time
333
	lastIdle        time.Time // time last idle
334
	// Settings from peer: (also guarded by wmu)
335
	maxFrameSize           uint32
336
	maxConcurrentStreams   uint32
337
	peerMaxHeaderListSize  uint64
338
	peerMaxHeaderTableSize uint32
339
	initialWindowSize      uint32
340

341
	// reqHeaderMu is a 1-element semaphore channel controlling access to sending new requests.
342
	// Write to reqHeaderMu to lock it, read from it to unlock.
343
	// Lock reqmu BEFORE mu or wmu.
344
	reqHeaderMu chan struct{}
345

346
	// wmu is held while writing.
347
	// Acquire BEFORE mu when holding both, to avoid blocking mu on network writes.
348
	// Only acquire both at the same time when changing peer settings.
349
	wmu  sync.Mutex
350
	bw   *bufio.Writer
351
	fr   *Framer
352
	werr error        // first write error that has occurred
353
	hbuf bytes.Buffer // HPACK encoder writes into this
354
	henc *hpack.Encoder
355

356
	syncHooks *testSyncHooks // can be nil
357
}
358

359
// Hook points used for testing.
360
// Outside of tests, cc.syncHooks is nil and these all have minimal implementations.
361
// Inside tests, see the testSyncHooks function docs.
362

363
// goRun starts a new goroutine.
364
func (cc *ClientConn) goRun(f func()) {
365
	if cc.syncHooks != nil {
366
		cc.syncHooks.goRun(f)
367
		return
368
	}
369
	go f()
370
}
371

372
// condBroadcast is cc.cond.Broadcast.
373
func (cc *ClientConn) condBroadcast() {
374
	if cc.syncHooks != nil {
375
		cc.syncHooks.condBroadcast(cc.cond)
376
	}
377
	cc.cond.Broadcast()
378
}
379

380
// condWait is cc.cond.Wait.
381
func (cc *ClientConn) condWait() {
382
	if cc.syncHooks != nil {
383
		cc.syncHooks.condWait(cc.cond)
384
	}
385
	cc.cond.Wait()
386
}
387

388
// newTimer creates a new time.Timer, or a synthetic timer in tests.
389
func (cc *ClientConn) newTimer(d time.Duration) timer {
390
	if cc.syncHooks != nil {
391
		return cc.syncHooks.newTimer(d)
392
	}
393
	return newTimeTimer(d)
394
}
395

396
// afterFunc creates a new time.AfterFunc timer, or a synthetic timer in tests.
397
func (cc *ClientConn) afterFunc(d time.Duration, f func()) timer {
398
	if cc.syncHooks != nil {
399
		return cc.syncHooks.afterFunc(d, f)
400
	}
401
	return newTimeAfterFunc(d, f)
402
}
403

404
func (cc *ClientConn) contextWithTimeout(ctx context.Context, d time.Duration) (context.Context, context.CancelFunc) {
405
	if cc.syncHooks != nil {
406
		return cc.syncHooks.contextWithTimeout(ctx, d)
407
	}
408
	return context.WithTimeout(ctx, d)
409
}
410

411
// clientStream is the state for a single HTTP/2 stream. One of these
412
// is created for each Transport.RoundTrip call.
413
type clientStream struct {
414
	cc *ClientConn
415

416
	// Fields of Request that we may access even after the response body is closed.
417
	ctx       context.Context
418
	reqCancel <-chan struct{}
419

420
	trace         *httptrace.ClientTrace // or nil
421
	ID            uint32
422
	bufPipe       pipe // buffered pipe with the flow-controlled response payload
423
	requestedGzip bool
424
	isHead        bool
425

426
	abortOnce sync.Once
427
	abort     chan struct{} // closed to signal stream should end immediately
428
	abortErr  error         // set if abort is closed
429

430
	peerClosed chan struct{} // closed when the peer sends an END_STREAM flag
431
	donec      chan struct{} // closed after the stream is in the closed state
432
	on100      chan struct{} // buffered; written to if a 100 is received
433

434
	respHeaderRecv chan struct{}  // closed when headers are received
435
	res            *http.Response // set if respHeaderRecv is closed
436

437
	flow        outflow // guarded by cc.mu
438
	inflow      inflow  // guarded by cc.mu
439
	bytesRemain int64   // -1 means unknown; owned by transportResponseBody.Read
440
	readErr     error   // sticky read error; owned by transportResponseBody.Read
441

442
	reqBody              io.ReadCloser
443
	reqBodyContentLength int64         // -1 means unknown
444
	reqBodyClosed        chan struct{} // guarded by cc.mu; non-nil on Close, closed when done
445

446
	// owned by writeRequest:
447
	sentEndStream bool // sent an END_STREAM flag to the peer
448
	sentHeaders   bool
449

450
	// owned by clientConnReadLoop:
451
	firstByte    bool  // got the first response byte
452
	pastHeaders  bool  // got first MetaHeadersFrame (actual headers)
453
	pastTrailers bool  // got optional second MetaHeadersFrame (trailers)
454
	num1xx       uint8 // number of 1xx responses seen
455
	readClosed   bool  // peer sent an END_STREAM flag
456
	readAborted  bool  // read loop reset the stream
457

458
	trailer    http.Header  // accumulated trailers
459
	resTrailer *http.Header // client's Response.Trailer
460
}
461

462
var got1xxFuncForTests func(int, textproto.MIMEHeader) error
463

464
// get1xxTraceFunc returns the value of request's httptrace.ClientTrace.Got1xxResponse func,
465
// if any. It returns nil if not set or if the Go version is too old.
466
func (cs *clientStream) get1xxTraceFunc() func(int, textproto.MIMEHeader) error {
467
	if fn := got1xxFuncForTests; fn != nil {
468
		return fn
469
	}
470
	return traceGot1xxResponseFunc(cs.trace)
471
}
472

473
func (cs *clientStream) abortStream(err error) {
474
	cs.cc.mu.Lock()
475
	defer cs.cc.mu.Unlock()
476
	cs.abortStreamLocked(err)
477
}
478

479
func (cs *clientStream) abortStreamLocked(err error) {
480
	cs.abortOnce.Do(func() {
481
		cs.abortErr = err
482
		close(cs.abort)
483
	})
484
	if cs.reqBody != nil {
485
		cs.closeReqBodyLocked()
486
	}
487
	// TODO(dneil): Clean up tests where cs.cc.cond is nil.
488
	if cs.cc.cond != nil {
489
		// Wake up writeRequestBody if it is waiting on flow control.
490
		cs.cc.condBroadcast()
491
	}
492
}
493

494
func (cs *clientStream) abortRequestBodyWrite() {
495
	cc := cs.cc
496
	cc.mu.Lock()
497
	defer cc.mu.Unlock()
498
	if cs.reqBody != nil && cs.reqBodyClosed == nil {
499
		cs.closeReqBodyLocked()
500
		cc.condBroadcast()
501
	}
502
}
503

504
func (cs *clientStream) closeReqBodyLocked() {
505
	if cs.reqBodyClosed != nil {
506
		return
507
	}
508
	cs.reqBodyClosed = make(chan struct{})
509
	reqBodyClosed := cs.reqBodyClosed
510
	cs.cc.goRun(func() {
511
		cs.reqBody.Close()
512
		close(reqBodyClosed)
513
	})
514
}
515

516
type stickyErrWriter struct {
517
	conn    net.Conn
518
	timeout time.Duration
519
	err     *error
520
}
521

522
func (sew stickyErrWriter) Write(p []byte) (n int, err error) {
523
	if *sew.err != nil {
524
		return 0, *sew.err
525
	}
526
	for {
527
		if sew.timeout != 0 {
528
			sew.conn.SetWriteDeadline(time.Now().Add(sew.timeout))
529
		}
530
		nn, err := sew.conn.Write(p[n:])
531
		n += nn
532
		if n < len(p) && nn > 0 && errors.Is(err, os.ErrDeadlineExceeded) {
533
			// Keep extending the deadline so long as we're making progress.
534
			continue
535
		}
536
		if sew.timeout != 0 {
537
			sew.conn.SetWriteDeadline(time.Time{})
538
		}
539
		*sew.err = err
540
		return n, err
541
	}
542
}
543

544
// noCachedConnError is the concrete type of ErrNoCachedConn, which
545
// needs to be detected by net/http regardless of whether it's its
546
// bundled version (in h2_bundle.go with a rewritten type name) or
547
// from a user's x/net/http2. As such, as it has a unique method name
548
// (IsHTTP2NoCachedConnError) that net/http sniffs for via func
549
// isNoCachedConnError.
550
type noCachedConnError struct{}
551

552
func (noCachedConnError) IsHTTP2NoCachedConnError() {}
553
func (noCachedConnError) Error() string             { return "http2: no cached connection was available" }
554

555
// isNoCachedConnError reports whether err is of type noCachedConnError
556
// or its equivalent renamed type in net/http2's h2_bundle.go. Both types
557
// may coexist in the same running program.
558
func isNoCachedConnError(err error) bool {
559
	_, ok := err.(interface{ IsHTTP2NoCachedConnError() })
560
	return ok
561
}
562

563
var ErrNoCachedConn error = noCachedConnError{}
564

565
// RoundTripOpt are options for the Transport.RoundTripOpt method.
566
type RoundTripOpt struct {
567
	// OnlyCachedConn controls whether RoundTripOpt may
568
	// create a new TCP connection. If set true and
569
	// no cached connection is available, RoundTripOpt
570
	// will return ErrNoCachedConn.
571
	OnlyCachedConn bool
572
}
573

574
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
575
	return t.RoundTripOpt(req, RoundTripOpt{})
576
}
577

578
// authorityAddr returns a given authority (a host/IP, or host:port / ip:port)
579
// and returns a host:port. The port 443 is added if needed.
580
func authorityAddr(scheme string, authority string) (addr string) {
581
	host, port, err := net.SplitHostPort(authority)
582
	if err != nil { // authority didn't have a port
583
		host = authority
584
		port = ""
585
	}
586
	if port == "" { // authority's port was empty
587
		port = "443"
588
		if scheme == "http" {
589
			port = "80"
590
		}
591
	}
592
	if a, err := idna.ToASCII(host); err == nil {
593
		host = a
594
	}
595
	// IPv6 address literal, without a port:
596
	if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
597
		return host + ":" + port
598
	}
599
	return net.JoinHostPort(host, port)
600
}
601

602
// RoundTripOpt is like RoundTrip, but takes options.
603
func (t *Transport) RoundTripOpt(req *http.Request, opt RoundTripOpt) (*http.Response, error) {
604
	if !(req.URL.Scheme == "https" || (req.URL.Scheme == "http" && t.AllowHTTP)) {
605
		return nil, errors.New("http2: unsupported scheme")
606
	}
607

608
	addr := authorityAddr(req.URL.Scheme, req.URL.Host)
609
	for retry := 0; ; retry++ {
610
		cc, err := t.connPool().GetClientConn(req, addr)
611
		if err != nil {
612
			t.vlogf("http2: Transport failed to get client conn for %s: %v", addr, err)
613
			return nil, err
614
		}
615
		reused := !atomic.CompareAndSwapUint32(&cc.reused, 0, 1)
616
		traceGotConn(req, cc, reused)
617
		res, err := cc.RoundTrip(req)
618
		if err != nil && retry <= 6 {
619
			roundTripErr := err
620
			if req, err = shouldRetryRequest(req, err); err == nil {
621
				// After the first retry, do exponential backoff with 10% jitter.
622
				if retry == 0 {
623
					t.vlogf("RoundTrip retrying after failure: %v", roundTripErr)
624
					continue
625
				}
626
				backoff := float64(uint(1) << (uint(retry) - 1))
627
				backoff += backoff * (0.1 * mathrand.Float64())
628
				d := time.Second * time.Duration(backoff)
629
				var tm timer
630
				if t.syncHooks != nil {
631
					tm = t.syncHooks.newTimer(d)
632
					t.syncHooks.blockUntil(func() bool {
633
						select {
634
						case <-tm.C():
635
						case <-req.Context().Done():
636
						default:
637
							return false
638
						}
639
						return true
640
					})
641
				} else {
642
					tm = newTimeTimer(d)
643
				}
644
				select {
645
				case <-tm.C():
646
					t.vlogf("RoundTrip retrying after failure: %v", roundTripErr)
647
					continue
648
				case <-req.Context().Done():
649
					tm.Stop()
650
					err = req.Context().Err()
651
				}
652
			}
653
		}
654
		if err != nil {
655
			t.vlogf("RoundTrip failure: %v", err)
656
			return nil, err
657
		}
658
		return res, nil
659
	}
660
}
661

662
// CloseIdleConnections closes any connections which were previously
663
// connected from previous requests but are now sitting idle.
664
// It does not interrupt any connections currently in use.
665
func (t *Transport) CloseIdleConnections() {
666
	if cp, ok := t.connPool().(clientConnPoolIdleCloser); ok {
667
		cp.closeIdleConnections()
668
	}
669
}
670

671
var (
672
	errClientConnClosed    = errors.New("http2: client conn is closed")
673
	errClientConnUnusable  = errors.New("http2: client conn not usable")
674
	errClientConnGotGoAway = errors.New("http2: Transport received Server's graceful shutdown GOAWAY")
675
)
676

677
// shouldRetryRequest is called by RoundTrip when a request fails to get
678
// response headers. It is always called with a non-nil error.
679
// It returns either a request to retry (either the same request, or a
680
// modified clone), or an error if the request can't be replayed.
681
func shouldRetryRequest(req *http.Request, err error) (*http.Request, error) {
682
	if !canRetryError(err) {
683
		return nil, err
684
	}
685
	// If the Body is nil (or http.NoBody), it's safe to reuse
686
	// this request and its Body.
687
	if req.Body == nil || req.Body == http.NoBody {
688
		return req, nil
689
	}
690

691
	// If the request body can be reset back to its original
692
	// state via the optional req.GetBody, do that.
693
	if req.GetBody != nil {
694
		body, err := req.GetBody()
695
		if err != nil {
696
			return nil, err
697
		}
698
		newReq := *req
699
		newReq.Body = body
700
		return &newReq, nil
701
	}
702

703
	// The Request.Body can't reset back to the beginning, but we
704
	// don't seem to have started to read from it yet, so reuse
705
	// the request directly.
706
	if err == errClientConnUnusable {
707
		return req, nil
708
	}
709

710
	return nil, fmt.Errorf("http2: Transport: cannot retry err [%v] after Request.Body was written; define Request.GetBody to avoid this error", err)
711
}
712

713
func canRetryError(err error) bool {
714
	if err == errClientConnUnusable || err == errClientConnGotGoAway {
715
		return true
716
	}
717
	if se, ok := err.(StreamError); ok {
718
		if se.Code == ErrCodeProtocol && se.Cause == errFromPeer {
719
			// See golang/go#47635, golang/go#42777
720
			return true
721
		}
722
		return se.Code == ErrCodeRefusedStream
723
	}
724
	return false
725
}
726

727
func (t *Transport) dialClientConn(ctx context.Context, addr string, singleUse bool) (*ClientConn, error) {
728
	if t.syncHooks != nil {
729
		return t.newClientConn(nil, singleUse, t.syncHooks)
730
	}
731
	host, _, err := net.SplitHostPort(addr)
732
	if err != nil {
733
		return nil, err
734
	}
735
	tconn, err := t.dialTLS(ctx, "tcp", addr, t.newTLSConfig(host))
736
	if err != nil {
737
		return nil, err
738
	}
739
	return t.newClientConn(tconn, singleUse, nil)
740
}
741

742
func (t *Transport) newTLSConfig(host string) *tls.Config {
743
	cfg := new(tls.Config)
744
	if t.TLSClientConfig != nil {
745
		*cfg = *t.TLSClientConfig.Clone()
746
	}
747
	if !strSliceContains(cfg.NextProtos, NextProtoTLS) {
748
		cfg.NextProtos = append([]string{NextProtoTLS}, cfg.NextProtos...)
749
	}
750
	if cfg.ServerName == "" {
751
		cfg.ServerName = host
752
	}
753
	return cfg
754
}
755

756
func (t *Transport) dialTLS(ctx context.Context, network, addr string, tlsCfg *tls.Config) (net.Conn, error) {
757
	if t.DialTLSContext != nil {
758
		return t.DialTLSContext(ctx, network, addr, tlsCfg)
759
	} else if t.DialTLS != nil {
760
		return t.DialTLS(network, addr, tlsCfg)
761
	}
762

763
	tlsCn, err := t.dialTLSWithContext(ctx, network, addr, tlsCfg)
764
	if err != nil {
765
		return nil, err
766
	}
767
	state := tlsCn.ConnectionState()
768
	if p := state.NegotiatedProtocol; p != NextProtoTLS {
769
		return nil, fmt.Errorf("http2: unexpected ALPN protocol %q; want %q", p, NextProtoTLS)
770
	}
771
	if !state.NegotiatedProtocolIsMutual {
772
		return nil, errors.New("http2: could not negotiate protocol mutually")
773
	}
774
	return tlsCn, nil
775
}
776

777
// disableKeepAlives reports whether connections should be closed as
778
// soon as possible after handling the first request.
779
func (t *Transport) disableKeepAlives() bool {
780
	return t.t1 != nil && t.t1.DisableKeepAlives
781
}
782

783
func (t *Transport) expectContinueTimeout() time.Duration {
784
	if t.t1 == nil {
785
		return 0
786
	}
787
	return t.t1.ExpectContinueTimeout
788
}
789

790
func (t *Transport) maxDecoderHeaderTableSize() uint32 {
791
	if v := t.MaxDecoderHeaderTableSize; v > 0 {
792
		return v
793
	}
794
	return initialHeaderTableSize
795
}
796

797
func (t *Transport) maxEncoderHeaderTableSize() uint32 {
798
	if v := t.MaxEncoderHeaderTableSize; v > 0 {
799
		return v
800
	}
801
	return initialHeaderTableSize
802
}
803

804
func (t *Transport) NewClientConn(c net.Conn) (*ClientConn, error) {
805
	return t.newClientConn(c, t.disableKeepAlives(), nil)
806
}
807

808
func (t *Transport) newClientConn(c net.Conn, singleUse bool, hooks *testSyncHooks) (*ClientConn, error) {
809
	cc := &ClientConn{
810
		t:                     t,
811
		tconn:                 c,
812
		readerDone:            make(chan struct{}),
813
		nextStreamID:          1,
814
		maxFrameSize:          16 << 10,                    // spec default
815
		initialWindowSize:     65535,                       // spec default
816
		maxConcurrentStreams:  initialMaxConcurrentStreams, // "infinite", per spec. Use a smaller value until we have received server settings.
817
		peerMaxHeaderListSize: 0xffffffffffffffff,          // "infinite", per spec. Use 2^64-1 instead.
818
		streams:               make(map[uint32]*clientStream),
819
		singleUse:             singleUse,
820
		wantSettingsAck:       true,
821
		pings:                 make(map[[8]byte]chan struct{}),
822
		reqHeaderMu:           make(chan struct{}, 1),
823
		syncHooks:             hooks,
824
	}
825
	if hooks != nil {
826
		hooks.newclientconn(cc)
827
		c = cc.tconn
828
	}
829
	if d := t.idleConnTimeout(); d != 0 {
830
		cc.idleTimeout = d
831
		cc.idleTimer = cc.afterFunc(d, cc.onIdleTimeout)
832
	}
833
	if VerboseLogs {
834
		t.vlogf("http2: Transport creating client conn %p to %v", cc, c.RemoteAddr())
835
	}
836

837
	cc.cond = sync.NewCond(&cc.mu)
838
	cc.flow.add(int32(initialWindowSize))
839

840
	// TODO: adjust this writer size to account for frame size +
841
	// MTU + crypto/tls record padding.
842
	cc.bw = bufio.NewWriter(stickyErrWriter{
843
		conn:    c,
844
		timeout: t.WriteByteTimeout,
845
		err:     &cc.werr,
846
	})
847
	cc.br = bufio.NewReader(c)
848
	cc.fr = NewFramer(cc.bw, cc.br)
849
	if t.maxFrameReadSize() != 0 {
850
		cc.fr.SetMaxReadFrameSize(t.maxFrameReadSize())
851
	}
852
	if t.CountError != nil {
853
		cc.fr.countError = t.CountError
854
	}
855
	maxHeaderTableSize := t.maxDecoderHeaderTableSize()
856
	cc.fr.ReadMetaHeaders = hpack.NewDecoder(maxHeaderTableSize, nil)
857
	cc.fr.MaxHeaderListSize = t.maxHeaderListSize()
858

859
	cc.henc = hpack.NewEncoder(&cc.hbuf)
860
	cc.henc.SetMaxDynamicTableSizeLimit(t.maxEncoderHeaderTableSize())
861
	cc.peerMaxHeaderTableSize = initialHeaderTableSize
862

863
	if t.AllowHTTP {
864
		cc.nextStreamID = 3
865
	}
866

867
	if cs, ok := c.(connectionStater); ok {
868
		state := cs.ConnectionState()
869
		cc.tlsState = &state
870
	}
871

872
	initialSettings := []Setting{
873
		{ID: SettingEnablePush, Val: 0},
874
		{ID: SettingInitialWindowSize, Val: transportDefaultStreamFlow},
875
	}
876
	if max := t.maxFrameReadSize(); max != 0 {
877
		initialSettings = append(initialSettings, Setting{ID: SettingMaxFrameSize, Val: max})
878
	}
879
	if max := t.maxHeaderListSize(); max != 0 {
880
		initialSettings = append(initialSettings, Setting{ID: SettingMaxHeaderListSize, Val: max})
881
	}
882
	if maxHeaderTableSize != initialHeaderTableSize {
883
		initialSettings = append(initialSettings, Setting{ID: SettingHeaderTableSize, Val: maxHeaderTableSize})
884
	}
885

886
	cc.bw.Write(clientPreface)
887
	cc.fr.WriteSettings(initialSettings...)
888
	cc.fr.WriteWindowUpdate(0, transportDefaultConnFlow)
889
	cc.inflow.init(transportDefaultConnFlow + initialWindowSize)
890
	cc.bw.Flush()
891
	if cc.werr != nil {
892
		cc.Close()
893
		return nil, cc.werr
894
	}
895

896
	cc.goRun(cc.readLoop)
897
	return cc, nil
898
}
899

900
func (cc *ClientConn) healthCheck() {
901
	pingTimeout := cc.t.pingTimeout()
902
	// We don't need to periodically ping in the health check, because the readLoop of ClientConn will
903
	// trigger the healthCheck again if there is no frame received.
904
	ctx, cancel := cc.contextWithTimeout(context.Background(), pingTimeout)
905
	defer cancel()
906
	cc.vlogf("http2: Transport sending health check")
907
	err := cc.Ping(ctx)
908
	if err != nil {
909
		cc.vlogf("http2: Transport health check failure: %v", err)
910
		cc.closeForLostPing()
911
	} else {
912
		cc.vlogf("http2: Transport health check success")
913
	}
914
}
915

916
// SetDoNotReuse marks cc as not reusable for future HTTP requests.
917
func (cc *ClientConn) SetDoNotReuse() {
918
	cc.mu.Lock()
919
	defer cc.mu.Unlock()
920
	cc.doNotReuse = true
921
}
922

923
func (cc *ClientConn) setGoAway(f *GoAwayFrame) {
924
	cc.mu.Lock()
925
	defer cc.mu.Unlock()
926

927
	old := cc.goAway
928
	cc.goAway = f
929

930
	// Merge the previous and current GoAway error frames.
931
	if cc.goAwayDebug == "" {
932
		cc.goAwayDebug = string(f.DebugData())
933
	}
934
	if old != nil && old.ErrCode != ErrCodeNo {
935
		cc.goAway.ErrCode = old.ErrCode
936
	}
937
	last := f.LastStreamID
938
	for streamID, cs := range cc.streams {
939
		if streamID > last {
940
			cs.abortStreamLocked(errClientConnGotGoAway)
941
		}
942
	}
943
}
944

945
// CanTakeNewRequest reports whether the connection can take a new request,
946
// meaning it has not been closed or received or sent a GOAWAY.
947
//
948
// If the caller is going to immediately make a new request on this
949
// connection, use ReserveNewRequest instead.
950
func (cc *ClientConn) CanTakeNewRequest() bool {
951
	cc.mu.Lock()
952
	defer cc.mu.Unlock()
953
	return cc.canTakeNewRequestLocked()
954
}
955

956
// ReserveNewRequest is like CanTakeNewRequest but also reserves a
957
// concurrent stream in cc. The reservation is decremented on the
958
// next call to RoundTrip.
959
func (cc *ClientConn) ReserveNewRequest() bool {
960
	cc.mu.Lock()
961
	defer cc.mu.Unlock()
962
	if st := cc.idleStateLocked(); !st.canTakeNewRequest {
963
		return false
964
	}
965
	cc.streamsReserved++
966
	return true
967
}
968

969
// ClientConnState describes the state of a ClientConn.
970
type ClientConnState struct {
971
	// Closed is whether the connection is closed.
972
	Closed bool
973

974
	// Closing is whether the connection is in the process of
975
	// closing. It may be closing due to shutdown, being a
976
	// single-use connection, being marked as DoNotReuse, or
977
	// having received a GOAWAY frame.
978
	Closing bool
979

980
	// StreamsActive is how many streams are active.
981
	StreamsActive int
982

983
	// StreamsReserved is how many streams have been reserved via
984
	// ClientConn.ReserveNewRequest.
985
	StreamsReserved int
986

987
	// StreamsPending is how many requests have been sent in excess
988
	// of the peer's advertised MaxConcurrentStreams setting and
989
	// are waiting for other streams to complete.
990
	StreamsPending int
991

992
	// MaxConcurrentStreams is how many concurrent streams the
993
	// peer advertised as acceptable. Zero means no SETTINGS
994
	// frame has been received yet.
995
	MaxConcurrentStreams uint32
996

997
	// LastIdle, if non-zero, is when the connection last
998
	// transitioned to idle state.
999
	LastIdle time.Time
1000
}
1001

1002
// State returns a snapshot of cc's state.
1003
func (cc *ClientConn) State() ClientConnState {
1004
	cc.wmu.Lock()
1005
	maxConcurrent := cc.maxConcurrentStreams
1006
	if !cc.seenSettings {
1007
		maxConcurrent = 0
1008
	}
1009
	cc.wmu.Unlock()
1010

1011
	cc.mu.Lock()
1012
	defer cc.mu.Unlock()
1013
	return ClientConnState{
1014
		Closed:               cc.closed,
1015
		Closing:              cc.closing || cc.singleUse || cc.doNotReuse || cc.goAway != nil,
1016
		StreamsActive:        len(cc.streams),
1017
		StreamsReserved:      cc.streamsReserved,
1018
		StreamsPending:       cc.pendingRequests,
1019
		LastIdle:             cc.lastIdle,
1020
		MaxConcurrentStreams: maxConcurrent,
1021
	}
1022
}
1023

1024
// clientConnIdleState describes the suitability of a client
1025
// connection to initiate a new RoundTrip request.
1026
type clientConnIdleState struct {
1027
	canTakeNewRequest bool
1028
}
1029

1030
func (cc *ClientConn) idleState() clientConnIdleState {
1031
	cc.mu.Lock()
1032
	defer cc.mu.Unlock()
1033
	return cc.idleStateLocked()
1034
}
1035

1036
func (cc *ClientConn) idleStateLocked() (st clientConnIdleState) {
1037
	if cc.singleUse && cc.nextStreamID > 1 {
1038
		return
1039
	}
1040
	var maxConcurrentOkay bool
1041
	if cc.t.StrictMaxConcurrentStreams {
1042
		// We'll tell the caller we can take a new request to
1043
		// prevent the caller from dialing a new TCP
1044
		// connection, but then we'll block later before
1045
		// writing it.
1046
		maxConcurrentOkay = true
1047
	} else {
1048
		maxConcurrentOkay = int64(len(cc.streams)+cc.streamsReserved+1) <= int64(cc.maxConcurrentStreams)
1049
	}
1050

1051
	st.canTakeNewRequest = cc.goAway == nil && !cc.closed && !cc.closing && maxConcurrentOkay &&
1052
		!cc.doNotReuse &&
1053
		int64(cc.nextStreamID)+2*int64(cc.pendingRequests) < math.MaxInt32 &&
1054
		!cc.tooIdleLocked()
1055
	return
1056
}
1057

1058
func (cc *ClientConn) canTakeNewRequestLocked() bool {
1059
	st := cc.idleStateLocked()
1060
	return st.canTakeNewRequest
1061
}
1062

1063
// tooIdleLocked reports whether this connection has been been sitting idle
1064
// for too much wall time.
1065
func (cc *ClientConn) tooIdleLocked() bool {
1066
	// The Round(0) strips the monontonic clock reading so the
1067
	// times are compared based on their wall time. We don't want
1068
	// to reuse a connection that's been sitting idle during
1069
	// VM/laptop suspend if monotonic time was also frozen.
1070
	return cc.idleTimeout != 0 && !cc.lastIdle.IsZero() && time.Since(cc.lastIdle.Round(0)) > cc.idleTimeout
1071
}
1072

1073
// onIdleTimeout is called from a time.AfterFunc goroutine. It will
1074
// only be called when we're idle, but because we're coming from a new
1075
// goroutine, there could be a new request coming in at the same time,
1076
// so this simply calls the synchronized closeIfIdle to shut down this
1077
// connection. The timer could just call closeIfIdle, but this is more
1078
// clear.
1079
func (cc *ClientConn) onIdleTimeout() {
1080
	cc.closeIfIdle()
1081
}
1082

1083
func (cc *ClientConn) closeConn() {
1084
	t := time.AfterFunc(250*time.Millisecond, cc.forceCloseConn)
1085
	defer t.Stop()
1086
	cc.tconn.Close()
1087
}
1088

1089
// A tls.Conn.Close can hang for a long time if the peer is unresponsive.
1090
// Try to shut it down more aggressively.
1091
func (cc *ClientConn) forceCloseConn() {
1092
	tc, ok := cc.tconn.(*tls.Conn)
1093
	if !ok {
1094
		return
1095
	}
1096
	if nc := tc.NetConn(); nc != nil {
1097
		nc.Close()
1098
	}
1099
}
1100

1101
func (cc *ClientConn) closeIfIdle() {
1102
	cc.mu.Lock()
1103
	if len(cc.streams) > 0 || cc.streamsReserved > 0 {
1104
		cc.mu.Unlock()
1105
		return
1106
	}
1107
	cc.closed = true
1108
	nextID := cc.nextStreamID
1109
	// TODO: do clients send GOAWAY too? maybe? Just Close:
1110
	cc.mu.Unlock()
1111

1112
	if VerboseLogs {
1113
		cc.vlogf("http2: Transport closing idle conn %p (forSingleUse=%v, maxStream=%v)", cc, cc.singleUse, nextID-2)
1114
	}
1115
	cc.closeConn()
1116
}
1117

1118
func (cc *ClientConn) isDoNotReuseAndIdle() bool {
1119
	cc.mu.Lock()
1120
	defer cc.mu.Unlock()
1121
	return cc.doNotReuse && len(cc.streams) == 0
1122
}
1123

1124
var shutdownEnterWaitStateHook = func() {}
1125

1126
// Shutdown gracefully closes the client connection, waiting for running streams to complete.
1127
func (cc *ClientConn) Shutdown(ctx context.Context) error {
1128
	if err := cc.sendGoAway(); err != nil {
1129
		return err
1130
	}
1131
	// Wait for all in-flight streams to complete or connection to close
1132
	done := make(chan struct{})
1133
	cancelled := false // guarded by cc.mu
1134
	cc.goRun(func() {
1135
		cc.mu.Lock()
1136
		defer cc.mu.Unlock()
1137
		for {
1138
			if len(cc.streams) == 0 || cc.closed {
1139
				cc.closed = true
1140
				close(done)
1141
				break
1142
			}
1143
			if cancelled {
1144
				break
1145
			}
1146
			cc.condWait()
1147
		}
1148
	})
1149
	shutdownEnterWaitStateHook()
1150
	select {
1151
	case <-done:
1152
		cc.closeConn()
1153
		return nil
1154
	case <-ctx.Done():
1155
		cc.mu.Lock()
1156
		// Free the goroutine above
1157
		cancelled = true
1158
		cc.condBroadcast()
1159
		cc.mu.Unlock()
1160
		return ctx.Err()
1161
	}
1162
}
1163

1164
func (cc *ClientConn) sendGoAway() error {
1165
	cc.mu.Lock()
1166
	closing := cc.closing
1167
	cc.closing = true
1168
	maxStreamID := cc.nextStreamID
1169
	cc.mu.Unlock()
1170
	if closing {
1171
		// GOAWAY sent already
1172
		return nil
1173
	}
1174

1175
	cc.wmu.Lock()
1176
	defer cc.wmu.Unlock()
1177
	// Send a graceful shutdown frame to server
1178
	if err := cc.fr.WriteGoAway(maxStreamID, ErrCodeNo, nil); err != nil {
1179
		return err
1180
	}
1181
	if err := cc.bw.Flush(); err != nil {
1182
		return err
1183
	}
1184
	// Prevent new requests
1185
	return nil
1186
}
1187

1188
// closes the client connection immediately. In-flight requests are interrupted.
1189
// err is sent to streams.
1190
func (cc *ClientConn) closeForError(err error) {
1191
	cc.mu.Lock()
1192
	cc.closed = true
1193
	for _, cs := range cc.streams {
1194
		cs.abortStreamLocked(err)
1195
	}
1196
	cc.condBroadcast()
1197
	cc.mu.Unlock()
1198
	cc.closeConn()
1199
}
1200

1201
// Close closes the client connection immediately.
1202
//
1203
// In-flight requests are interrupted. For a graceful shutdown, use Shutdown instead.
1204
func (cc *ClientConn) Close() error {
1205
	err := errors.New("http2: client connection force closed via ClientConn.Close")
1206
	cc.closeForError(err)
1207
	return nil
1208
}
1209

1210
// closes the client connection immediately. In-flight requests are interrupted.
1211
func (cc *ClientConn) closeForLostPing() {
1212
	err := errors.New("http2: client connection lost")
1213
	if f := cc.t.CountError; f != nil {
1214
		f("conn_close_lost_ping")
1215
	}
1216
	cc.closeForError(err)
1217
}
1218

1219
// errRequestCanceled is a copy of net/http's errRequestCanceled because it's not
1220
// exported. At least they'll be DeepEqual for h1-vs-h2 comparisons tests.
1221
var errRequestCanceled = errors.New("net/http: request canceled")
1222

1223
func commaSeparatedTrailers(req *http.Request) (string, error) {
1224
	keys := make([]string, 0, len(req.Trailer))
1225
	for k := range req.Trailer {
1226
		k = canonicalHeader(k)
1227
		switch k {
1228
		case "Transfer-Encoding", "Trailer", "Content-Length":
1229
			return "", fmt.Errorf("invalid Trailer key %q", k)
1230
		}
1231
		keys = append(keys, k)
1232
	}
1233
	if len(keys) > 0 {
1234
		sort.Strings(keys)
1235
		return strings.Join(keys, ","), nil
1236
	}
1237
	return "", nil
1238
}
1239

1240
func (cc *ClientConn) responseHeaderTimeout() time.Duration {
1241
	if cc.t.t1 != nil {
1242
		return cc.t.t1.ResponseHeaderTimeout
1243
	}
1244
	// No way to do this (yet?) with just an http2.Transport. Probably
1245
	// no need. Request.Cancel this is the new way. We only need to support
1246
	// this for compatibility with the old http.Transport fields when
1247
	// we're doing transparent http2.
1248
	return 0
1249
}
1250

1251
// checkConnHeaders checks whether req has any invalid connection-level headers.
1252
// per RFC 7540 section 8.1.2.2: Connection-Specific Header Fields.
1253
// Certain headers are special-cased as okay but not transmitted later.
1254
func checkConnHeaders(req *http.Request) error {
1255
	if v := req.Header.Get("Upgrade"); v != "" {
1256
		return fmt.Errorf("http2: invalid Upgrade request header: %q", req.Header["Upgrade"])
1257
	}
1258
	if vv := req.Header["Transfer-Encoding"]; len(vv) > 0 && (len(vv) > 1 || vv[0] != "" && vv[0] != "chunked") {
1259
		return fmt.Errorf("http2: invalid Transfer-Encoding request header: %q", vv)
1260
	}
1261
	if vv := req.Header["Connection"]; len(vv) > 0 && (len(vv) > 1 || vv[0] != "" && !asciiEqualFold(vv[0], "close") && !asciiEqualFold(vv[0], "keep-alive")) {
1262
		return fmt.Errorf("http2: invalid Connection request header: %q", vv)
1263
	}
1264
	return nil
1265
}
1266

1267
// actualContentLength returns a sanitized version of
1268
// req.ContentLength, where 0 actually means zero (not unknown) and -1
1269
// means unknown.
1270
func actualContentLength(req *http.Request) int64 {
1271
	if req.Body == nil || req.Body == http.NoBody {
1272
		return 0
1273
	}
1274
	if req.ContentLength != 0 {
1275
		return req.ContentLength
1276
	}
1277
	return -1
1278
}
1279

1280
func (cc *ClientConn) decrStreamReservations() {
1281
	cc.mu.Lock()
1282
	defer cc.mu.Unlock()
1283
	cc.decrStreamReservationsLocked()
1284
}
1285

1286
func (cc *ClientConn) decrStreamReservationsLocked() {
1287
	if cc.streamsReserved > 0 {
1288
		cc.streamsReserved--
1289
	}
1290
}
1291

1292
func (cc *ClientConn) RoundTrip(req *http.Request) (*http.Response, error) {
1293
	return cc.roundTrip(req, nil)
1294
}
1295

1296
func (cc *ClientConn) roundTrip(req *http.Request, streamf func(*clientStream)) (*http.Response, error) {
1297
	ctx := req.Context()
1298
	cs := &clientStream{
1299
		cc:                   cc,
1300
		ctx:                  ctx,
1301
		reqCancel:            req.Cancel,
1302
		isHead:               req.Method == "HEAD",
1303
		reqBody:              req.Body,
1304
		reqBodyContentLength: actualContentLength(req),
1305
		trace:                httptrace.ContextClientTrace(ctx),
1306
		peerClosed:           make(chan struct{}),
1307
		abort:                make(chan struct{}),
1308
		respHeaderRecv:       make(chan struct{}),
1309
		donec:                make(chan struct{}),
1310
	}
1311
	cc.goRun(func() {
1312
		cs.doRequest(req)
1313
	})
1314

1315
	waitDone := func() error {
1316
		if cc.syncHooks != nil {
1317
			cc.syncHooks.blockUntil(func() bool {
1318
				select {
1319
				case <-cs.donec:
1320
				case <-ctx.Done():
1321
				case <-cs.reqCancel:
1322
				default:
1323
					return false
1324
				}
1325
				return true
1326
			})
1327
		}
1328
		select {
1329
		case <-cs.donec:
1330
			return nil
1331
		case <-ctx.Done():
1332
			return ctx.Err()
1333
		case <-cs.reqCancel:
1334
			return errRequestCanceled
1335
		}
1336
	}
1337

1338
	handleResponseHeaders := func() (*http.Response, error) {
1339
		res := cs.res
1340
		if res.StatusCode > 299 {
1341
			// On error or status code 3xx, 4xx, 5xx, etc abort any
1342
			// ongoing write, assuming that the server doesn't care
1343
			// about our request body. If the server replied with 1xx or
1344
			// 2xx, however, then assume the server DOES potentially
1345
			// want our body (e.g. full-duplex streaming:
1346
			// golang.org/issue/13444). If it turns out the server
1347
			// doesn't, they'll RST_STREAM us soon enough. This is a
1348
			// heuristic to avoid adding knobs to Transport. Hopefully
1349
			// we can keep it.
1350
			cs.abortRequestBodyWrite()
1351
		}
1352
		res.Request = req
1353
		res.TLS = cc.tlsState
1354
		if res.Body == noBody && actualContentLength(req) == 0 {
1355
			// If there isn't a request or response body still being
1356
			// written, then wait for the stream to be closed before
1357
			// RoundTrip returns.
1358
			if err := waitDone(); err != nil {
1359
				return nil, err
1360
			}
1361
		}
1362
		return res, nil
1363
	}
1364

1365
	cancelRequest := func(cs *clientStream, err error) error {
1366
		cs.cc.mu.Lock()
1367
		bodyClosed := cs.reqBodyClosed
1368
		cs.cc.mu.Unlock()
1369
		// Wait for the request body to be closed.
1370
		//
1371
		// If nothing closed the body before now, abortStreamLocked
1372
		// will have started a goroutine to close it.
1373
		//
1374
		// Closing the body before returning avoids a race condition
1375
		// with net/http checking its readTrackingBody to see if the
1376
		// body was read from or closed. See golang/go#60041.
1377
		//
1378
		// The body is closed in a separate goroutine without the
1379
		// connection mutex held, but dropping the mutex before waiting
1380
		// will keep us from holding it indefinitely if the body
1381
		// close is slow for some reason.
1382
		if bodyClosed != nil {
1383
			<-bodyClosed
1384
		}
1385
		return err
1386
	}
1387

1388
	if streamf != nil {
1389
		streamf(cs)
1390
	}
1391

1392
	for {
1393
		if cc.syncHooks != nil {
1394
			cc.syncHooks.blockUntil(func() bool {
1395
				select {
1396
				case <-cs.respHeaderRecv:
1397
				case <-cs.abort:
1398
				case <-ctx.Done():
1399
				case <-cs.reqCancel:
1400
				default:
1401
					return false
1402
				}
1403
				return true
1404
			})
1405
		}
1406
		select {
1407
		case <-cs.respHeaderRecv:
1408
			return handleResponseHeaders()
1409
		case <-cs.abort:
1410
			select {
1411
			case <-cs.respHeaderRecv:
1412
				// If both cs.respHeaderRecv and cs.abort are signaling,
1413
				// pick respHeaderRecv. The server probably wrote the
1414
				// response and immediately reset the stream.
1415
				// golang.org/issue/49645
1416
				return handleResponseHeaders()
1417
			default:
1418
				waitDone()
1419
				return nil, cs.abortErr
1420
			}
1421
		case <-ctx.Done():
1422
			err := ctx.Err()
1423
			cs.abortStream(err)
1424
			return nil, cancelRequest(cs, err)
1425
		case <-cs.reqCancel:
1426
			cs.abortStream(errRequestCanceled)
1427
			return nil, cancelRequest(cs, errRequestCanceled)
1428
		}
1429
	}
1430
}
1431

1432
// doRequest runs for the duration of the request lifetime.
1433
//
1434
// It sends the request and performs post-request cleanup (closing Request.Body, etc.).
1435
func (cs *clientStream) doRequest(req *http.Request) {
1436
	err := cs.writeRequest(req)
1437
	cs.cleanupWriteRequest(err)
1438
}
1439

1440
// writeRequest sends a request.
1441
//
1442
// It returns nil after the request is written, the response read,
1443
// and the request stream is half-closed by the peer.
1444
//
1445
// It returns non-nil if the request ends otherwise.
1446
// If the returned error is StreamError, the error Code may be used in resetting the stream.
1447
func (cs *clientStream) writeRequest(req *http.Request) (err error) {
1448
	cc := cs.cc
1449
	ctx := cs.ctx
1450

1451
	if err := checkConnHeaders(req); err != nil {
1452
		return err
1453
	}
1454

1455
	// Acquire the new-request lock by writing to reqHeaderMu.
1456
	// This lock guards the critical section covering allocating a new stream ID
1457
	// (requires mu) and creating the stream (requires wmu).
1458
	if cc.reqHeaderMu == nil {
1459
		panic("RoundTrip on uninitialized ClientConn") // for tests
1460
	}
1461
	var newStreamHook func(*clientStream)
1462
	if cc.syncHooks != nil {
1463
		newStreamHook = cc.syncHooks.newstream
1464
		cc.syncHooks.blockUntil(func() bool {
1465
			select {
1466
			case cc.reqHeaderMu <- struct{}{}:
1467
				<-cc.reqHeaderMu
1468
			case <-cs.reqCancel:
1469
			case <-ctx.Done():
1470
			default:
1471
				return false
1472
			}
1473
			return true
1474
		})
1475
	}
1476
	select {
1477
	case cc.reqHeaderMu <- struct{}{}:
1478
	case <-cs.reqCancel:
1479
		return errRequestCanceled
1480
	case <-ctx.Done():
1481
		return ctx.Err()
1482
	}
1483

1484
	cc.mu.Lock()
1485
	if cc.idleTimer != nil {
1486
		cc.idleTimer.Stop()
1487
	}
1488
	cc.decrStreamReservationsLocked()
1489
	if err := cc.awaitOpenSlotForStreamLocked(cs); err != nil {
1490
		cc.mu.Unlock()
1491
		<-cc.reqHeaderMu
1492
		return err
1493
	}
1494
	cc.addStreamLocked(cs) // assigns stream ID
1495
	if isConnectionCloseRequest(req) {
1496
		cc.doNotReuse = true
1497
	}
1498
	cc.mu.Unlock()
1499

1500
	if newStreamHook != nil {
1501
		newStreamHook(cs)
1502
	}
1503

1504
	// TODO(bradfitz): this is a copy of the logic in net/http. Unify somewhere?
1505
	if !cc.t.disableCompression() &&
1506
		req.Header.Get("Accept-Encoding") == "" &&
1507
		req.Header.Get("Range") == "" &&
1508
		!cs.isHead {
1509
		// Request gzip only, not deflate. Deflate is ambiguous and
1510
		// not as universally supported anyway.
1511
		// See: https://zlib.net/zlib_faq.html#faq39
1512
		//
1513
		// Note that we don't request this for HEAD requests,
1514
		// due to a bug in nginx:
1515
		//   http://trac.nginx.org/nginx/ticket/358
1516
		//   https://golang.org/issue/5522
1517
		//
1518
		// We don't request gzip if the request is for a range, since
1519
		// auto-decoding a portion of a gzipped document will just fail
1520
		// anyway. See https://golang.org/issue/8923
1521
		cs.requestedGzip = true
1522
	}
1523

1524
	continueTimeout := cc.t.expectContinueTimeout()
1525
	if continueTimeout != 0 {
1526
		if !httpguts.HeaderValuesContainsToken(req.Header["Expect"], "100-continue") {
1527
			continueTimeout = 0
1528
		} else {
1529
			cs.on100 = make(chan struct{}, 1)
1530
		}
1531
	}
1532

1533
	// Past this point (where we send request headers), it is possible for
1534
	// RoundTrip to return successfully. Since the RoundTrip contract permits
1535
	// the caller to "mutate or reuse" the Request after closing the Response's Body,
1536
	// we must take care when referencing the Request from here on.
1537
	err = cs.encodeAndWriteHeaders(req)
1538
	<-cc.reqHeaderMu
1539
	if err != nil {
1540
		return err
1541
	}
1542

1543
	hasBody := cs.reqBodyContentLength != 0
1544
	if !hasBody {
1545
		cs.sentEndStream = true
1546
	} else {
1547
		if continueTimeout != 0 {
1548
			traceWait100Continue(cs.trace)
1549
			timer := time.NewTimer(continueTimeout)
1550
			select {
1551
			case <-timer.C:
1552
				err = nil
1553
			case <-cs.on100:
1554
				err = nil
1555
			case <-cs.abort:
1556
				err = cs.abortErr
1557
			case <-ctx.Done():
1558
				err = ctx.Err()
1559
			case <-cs.reqCancel:
1560
				err = errRequestCanceled
1561
			}
1562
			timer.Stop()
1563
			if err != nil {
1564
				traceWroteRequest(cs.trace, err)
1565
				return err
1566
			}
1567
		}
1568

1569
		if err = cs.writeRequestBody(req); err != nil {
1570
			if err != errStopReqBodyWrite {
1571
				traceWroteRequest(cs.trace, err)
1572
				return err
1573
			}
1574
		} else {
1575
			cs.sentEndStream = true
1576
		}
1577
	}
1578

1579
	traceWroteRequest(cs.trace, err)
1580

1581
	var respHeaderTimer <-chan time.Time
1582
	var respHeaderRecv chan struct{}
1583
	if d := cc.responseHeaderTimeout(); d != 0 {
1584
		timer := cc.newTimer(d)
1585
		defer timer.Stop()
1586
		respHeaderTimer = timer.C()
1587
		respHeaderRecv = cs.respHeaderRecv
1588
	}
1589
	// Wait until the peer half-closes its end of the stream,
1590
	// or until the request is aborted (via context, error, or otherwise),
1591
	// whichever comes first.
1592
	for {
1593
		if cc.syncHooks != nil {
1594
			cc.syncHooks.blockUntil(func() bool {
1595
				select {
1596
				case <-cs.peerClosed:
1597
				case <-respHeaderTimer:
1598
				case <-respHeaderRecv:
1599
				case <-cs.abort:
1600
				case <-ctx.Done():
1601
				case <-cs.reqCancel:
1602
				default:
1603
					return false
1604
				}
1605
				return true
1606
			})
1607
		}
1608
		select {
1609
		case <-cs.peerClosed:
1610
			return nil
1611
		case <-respHeaderTimer:
1612
			return errTimeout
1613
		case <-respHeaderRecv:
1614
			respHeaderRecv = nil
1615
			respHeaderTimer = nil // keep waiting for END_STREAM
1616
		case <-cs.abort:
1617
			return cs.abortErr
1618
		case <-ctx.Done():
1619
			return ctx.Err()
1620
		case <-cs.reqCancel:
1621
			return errRequestCanceled
1622
		}
1623
	}
1624
}
1625

1626
func (cs *clientStream) encodeAndWriteHeaders(req *http.Request) error {
1627
	cc := cs.cc
1628
	ctx := cs.ctx
1629

1630
	cc.wmu.Lock()
1631
	defer cc.wmu.Unlock()
1632

1633
	// If the request was canceled while waiting for cc.mu, just quit.
1634
	select {
1635
	case <-cs.abort:
1636
		return cs.abortErr
1637
	case <-ctx.Done():
1638
		return ctx.Err()
1639
	case <-cs.reqCancel:
1640
		return errRequestCanceled
1641
	default:
1642
	}
1643

1644
	// Encode headers.
1645
	//
1646
	// we send: HEADERS{1}, CONTINUATION{0,} + DATA{0,} (DATA is
1647
	// sent by writeRequestBody below, along with any Trailers,
1648
	// again in form HEADERS{1}, CONTINUATION{0,})
1649
	trailers, err := commaSeparatedTrailers(req)
1650
	if err != nil {
1651
		return err
1652
	}
1653
	hasTrailers := trailers != ""
1654
	contentLen := actualContentLength(req)
1655
	hasBody := contentLen != 0
1656
	hdrs, err := cc.encodeHeaders(req, cs.requestedGzip, trailers, contentLen)
1657
	if err != nil {
1658
		return err
1659
	}
1660

1661
	// Write the request.
1662
	endStream := !hasBody && !hasTrailers
1663
	cs.sentHeaders = true
1664
	err = cc.writeHeaders(cs.ID, endStream, int(cc.maxFrameSize), hdrs)
1665
	traceWroteHeaders(cs.trace)
1666
	return err
1667
}
1668

1669
// cleanupWriteRequest performs post-request tasks.
1670
//
1671
// If err (the result of writeRequest) is non-nil and the stream is not closed,
1672
// cleanupWriteRequest will send a reset to the peer.
1673
func (cs *clientStream) cleanupWriteRequest(err error) {
1674
	cc := cs.cc
1675

1676
	if cs.ID == 0 {
1677
		// We were canceled before creating the stream, so return our reservation.
1678
		cc.decrStreamReservations()
1679
	}
1680

1681
	// TODO: write h12Compare test showing whether
1682
	// Request.Body is closed by the Transport,
1683
	// and in multiple cases: server replies <=299 and >299
1684
	// while still writing request body
1685
	cc.mu.Lock()
1686
	mustCloseBody := false
1687
	if cs.reqBody != nil && cs.reqBodyClosed == nil {
1688
		mustCloseBody = true
1689
		cs.reqBodyClosed = make(chan struct{})
1690
	}
1691
	bodyClosed := cs.reqBodyClosed
1692
	cc.mu.Unlock()
1693
	if mustCloseBody {
1694
		cs.reqBody.Close()
1695
		close(bodyClosed)
1696
	}
1697
	if bodyClosed != nil {
1698
		<-bodyClosed
1699
	}
1700

1701
	if err != nil && cs.sentEndStream {
1702
		// If the connection is closed immediately after the response is read,
1703
		// we may be aborted before finishing up here. If the stream was closed
1704
		// cleanly on both sides, there is no error.
1705
		select {
1706
		case <-cs.peerClosed:
1707
			err = nil
1708
		default:
1709
		}
1710
	}
1711
	if err != nil {
1712
		cs.abortStream(err) // possibly redundant, but harmless
1713
		if cs.sentHeaders {
1714
			if se, ok := err.(StreamError); ok {
1715
				if se.Cause != errFromPeer {
1716
					cc.writeStreamReset(cs.ID, se.Code, err)
1717
				}
1718
			} else {
1719
				cc.writeStreamReset(cs.ID, ErrCodeCancel, err)
1720
			}
1721
		}
1722
		cs.bufPipe.CloseWithError(err) // no-op if already closed
1723
	} else {
1724
		if cs.sentHeaders && !cs.sentEndStream {
1725
			cc.writeStreamReset(cs.ID, ErrCodeNo, nil)
1726
		}
1727
		cs.bufPipe.CloseWithError(errRequestCanceled)
1728
	}
1729
	if cs.ID != 0 {
1730
		cc.forgetStreamID(cs.ID)
1731
	}
1732

1733
	cc.wmu.Lock()
1734
	werr := cc.werr
1735
	cc.wmu.Unlock()
1736
	if werr != nil {
1737
		cc.Close()
1738
	}
1739

1740
	close(cs.donec)
1741
}
1742

1743
// awaitOpenSlotForStreamLocked waits until len(streams) < maxConcurrentStreams.
1744
// Must hold cc.mu.
1745
func (cc *ClientConn) awaitOpenSlotForStreamLocked(cs *clientStream) error {
1746
	for {
1747
		cc.lastActive = time.Now()
1748
		if cc.closed || !cc.canTakeNewRequestLocked() {
1749
			return errClientConnUnusable
1750
		}
1751
		cc.lastIdle = time.Time{}
1752
		if int64(len(cc.streams)) < int64(cc.maxConcurrentStreams) {
1753
			return nil
1754
		}
1755
		cc.pendingRequests++
1756
		cc.condWait()
1757
		cc.pendingRequests--
1758
		select {
1759
		case <-cs.abort:
1760
			return cs.abortErr
1761
		default:
1762
		}
1763
	}
1764
}
1765

1766
// requires cc.wmu be held
1767
func (cc *ClientConn) writeHeaders(streamID uint32, endStream bool, maxFrameSize int, hdrs []byte) error {
1768
	first := true // first frame written (HEADERS is first, then CONTINUATION)
1769
	for len(hdrs) > 0 && cc.werr == nil {
1770
		chunk := hdrs
1771
		if len(chunk) > maxFrameSize {
1772
			chunk = chunk[:maxFrameSize]
1773
		}
1774
		hdrs = hdrs[len(chunk):]
1775
		endHeaders := len(hdrs) == 0
1776
		if first {
1777
			cc.fr.WriteHeaders(HeadersFrameParam{
1778
				StreamID:      streamID,
1779
				BlockFragment: chunk,
1780
				EndStream:     endStream,
1781
				EndHeaders:    endHeaders,
1782
			})
1783
			first = false
1784
		} else {
1785
			cc.fr.WriteContinuation(streamID, endHeaders, chunk)
1786
		}
1787
	}
1788
	cc.bw.Flush()
1789
	return cc.werr
1790
}
1791

1792
// internal error values; they don't escape to callers
1793
var (
1794
	// abort request body write; don't send cancel
1795
	errStopReqBodyWrite = errors.New("http2: aborting request body write")
1796

1797
	// abort request body write, but send stream reset of cancel.
1798
	errStopReqBodyWriteAndCancel = errors.New("http2: canceling request")
1799

1800
	errReqBodyTooLong = errors.New("http2: request body larger than specified content length")
1801
)
1802

1803
// frameScratchBufferLen returns the length of a buffer to use for
1804
// outgoing request bodies to read/write to/from.
1805
//
1806
// It returns max(1, min(peer's advertised max frame size,
1807
// Request.ContentLength+1, 512KB)).
1808
func (cs *clientStream) frameScratchBufferLen(maxFrameSize int) int {
1809
	const max = 512 << 10
1810
	n := int64(maxFrameSize)
1811
	if n > max {
1812
		n = max
1813
	}
1814
	if cl := cs.reqBodyContentLength; cl != -1 && cl+1 < n {
1815
		// Add an extra byte past the declared content-length to
1816
		// give the caller's Request.Body io.Reader a chance to
1817
		// give us more bytes than they declared, so we can catch it
1818
		// early.
1819
		n = cl + 1
1820
	}
1821
	if n < 1 {
1822
		return 1
1823
	}
1824
	return int(n) // doesn't truncate; max is 512K
1825
}
1826

1827
// Seven bufPools manage different frame sizes. This helps to avoid scenarios where long-running
1828
// streaming requests using small frame sizes occupy large buffers initially allocated for prior
1829
// requests needing big buffers. The size ranges are as follows:
1830
// {0 KB, 16 KB], {16 KB, 32 KB], {32 KB, 64 KB], {64 KB, 128 KB], {128 KB, 256 KB],
1831
// {256 KB, 512 KB], {512 KB, infinity}
1832
// In practice, the maximum scratch buffer size should not exceed 512 KB due to
1833
// frameScratchBufferLen(maxFrameSize), thus the "infinity pool" should never be used.
1834
// It exists mainly as a safety measure, for potential future increases in max buffer size.
1835
var bufPools [7]sync.Pool // of *[]byte
1836
func bufPoolIndex(size int) int {
1837
	if size <= 16384 {
1838
		return 0
1839
	}
1840
	size -= 1
1841
	bits := bits.Len(uint(size))
1842
	index := bits - 14
1843
	if index >= len(bufPools) {
1844
		return len(bufPools) - 1
1845
	}
1846
	return index
1847
}
1848

1849
func (cs *clientStream) writeRequestBody(req *http.Request) (err error) {
1850
	cc := cs.cc
1851
	body := cs.reqBody
1852
	sentEnd := false // whether we sent the final DATA frame w/ END_STREAM
1853

1854
	hasTrailers := req.Trailer != nil
1855
	remainLen := cs.reqBodyContentLength
1856
	hasContentLen := remainLen != -1
1857

1858
	cc.mu.Lock()
1859
	maxFrameSize := int(cc.maxFrameSize)
1860
	cc.mu.Unlock()
1861

1862
	// Scratch buffer for reading into & writing from.
1863
	scratchLen := cs.frameScratchBufferLen(maxFrameSize)
1864
	var buf []byte
1865
	index := bufPoolIndex(scratchLen)
1866
	if bp, ok := bufPools[index].Get().(*[]byte); ok && len(*bp) >= scratchLen {
1867
		defer bufPools[index].Put(bp)
1868
		buf = *bp
1869
	} else {
1870
		buf = make([]byte, scratchLen)
1871
		defer bufPools[index].Put(&buf)
1872
	}
1873

1874
	var sawEOF bool
1875
	for !sawEOF {
1876
		n, err := body.Read(buf)
1877
		if hasContentLen {
1878
			remainLen -= int64(n)
1879
			if remainLen == 0 && err == nil {
1880
				// The request body's Content-Length was predeclared and
1881
				// we just finished reading it all, but the underlying io.Reader
1882
				// returned the final chunk with a nil error (which is one of
1883
				// the two valid things a Reader can do at EOF). Because we'd prefer
1884
				// to send the END_STREAM bit early, double-check that we're actually
1885
				// at EOF. Subsequent reads should return (0, EOF) at this point.
1886
				// If either value is different, we return an error in one of two ways below.
1887
				var scratch [1]byte
1888
				var n1 int
1889
				n1, err = body.Read(scratch[:])
1890
				remainLen -= int64(n1)
1891
			}
1892
			if remainLen < 0 {
1893
				err = errReqBodyTooLong
1894
				return err
1895
			}
1896
		}
1897
		if err != nil {
1898
			cc.mu.Lock()
1899
			bodyClosed := cs.reqBodyClosed != nil
1900
			cc.mu.Unlock()
1901
			switch {
1902
			case bodyClosed:
1903
				return errStopReqBodyWrite
1904
			case err == io.EOF:
1905
				sawEOF = true
1906
				err = nil
1907
			default:
1908
				return err
1909
			}
1910
		}
1911

1912
		remain := buf[:n]
1913
		for len(remain) > 0 && err == nil {
1914
			var allowed int32
1915
			allowed, err = cs.awaitFlowControl(len(remain))
1916
			if err != nil {
1917
				return err
1918
			}
1919
			cc.wmu.Lock()
1920
			data := remain[:allowed]
1921
			remain = remain[allowed:]
1922
			sentEnd = sawEOF && len(remain) == 0 && !hasTrailers
1923
			err = cc.fr.WriteData(cs.ID, sentEnd, data)
1924
			if err == nil {
1925
				// TODO(bradfitz): this flush is for latency, not bandwidth.
1926
				// Most requests won't need this. Make this opt-in or
1927
				// opt-out?  Use some heuristic on the body type? Nagel-like
1928
				// timers?  Based on 'n'? Only last chunk of this for loop,
1929
				// unless flow control tokens are low? For now, always.
1930
				// If we change this, see comment below.
1931
				err = cc.bw.Flush()
1932
			}
1933
			cc.wmu.Unlock()
1934
		}
1935
		if err != nil {
1936
			return err
1937
		}
1938
	}
1939

1940
	if sentEnd {
1941
		// Already sent END_STREAM (which implies we have no
1942
		// trailers) and flushed, because currently all
1943
		// WriteData frames above get a flush. So we're done.
1944
		return nil
1945
	}
1946

1947
	// Since the RoundTrip contract permits the caller to "mutate or reuse"
1948
	// a request after the Response's Body is closed, verify that this hasn't
1949
	// happened before accessing the trailers.
1950
	cc.mu.Lock()
1951
	trailer := req.Trailer
1952
	err = cs.abortErr
1953
	cc.mu.Unlock()
1954
	if err != nil {
1955
		return err
1956
	}
1957

1958
	cc.wmu.Lock()
1959
	defer cc.wmu.Unlock()
1960
	var trls []byte
1961
	if len(trailer) > 0 {
1962
		trls, err = cc.encodeTrailers(trailer)
1963
		if err != nil {
1964
			return err
1965
		}
1966
	}
1967

1968
	// Two ways to send END_STREAM: either with trailers, or
1969
	// with an empty DATA frame.
1970
	if len(trls) > 0 {
1971
		err = cc.writeHeaders(cs.ID, true, maxFrameSize, trls)
1972
	} else {
1973
		err = cc.fr.WriteData(cs.ID, true, nil)
1974
	}
1975
	if ferr := cc.bw.Flush(); ferr != nil && err == nil {
1976
		err = ferr
1977
	}
1978
	return err
1979
}
1980

1981
// awaitFlowControl waits for [1, min(maxBytes, cc.cs.maxFrameSize)] flow
1982
// control tokens from the server.
1983
// It returns either the non-zero number of tokens taken or an error
1984
// if the stream is dead.
1985
func (cs *clientStream) awaitFlowControl(maxBytes int) (taken int32, err error) {
1986
	cc := cs.cc
1987
	ctx := cs.ctx
1988
	cc.mu.Lock()
1989
	defer cc.mu.Unlock()
1990
	for {
1991
		if cc.closed {
1992
			return 0, errClientConnClosed
1993
		}
1994
		if cs.reqBodyClosed != nil {
1995
			return 0, errStopReqBodyWrite
1996
		}
1997
		select {
1998
		case <-cs.abort:
1999
			return 0, cs.abortErr
2000
		case <-ctx.Done():
2001
			return 0, ctx.Err()
2002
		case <-cs.reqCancel:
2003
			return 0, errRequestCanceled
2004
		default:
2005
		}
2006
		if a := cs.flow.available(); a > 0 {
2007
			take := a
2008
			if int(take) > maxBytes {
2009

2010
				take = int32(maxBytes) // can't truncate int; take is int32
2011
			}
2012
			if take > int32(cc.maxFrameSize) {
2013
				take = int32(cc.maxFrameSize)
2014
			}
2015
			cs.flow.take(take)
2016
			return take, nil
2017
		}
2018
		cc.condWait()
2019
	}
2020
}
2021

2022
func validateHeaders(hdrs http.Header) string {
2023
	for k, vv := range hdrs {
2024
		if !httpguts.ValidHeaderFieldName(k) {
2025
			return fmt.Sprintf("name %q", k)
2026
		}
2027
		for _, v := range vv {
2028
			if !httpguts.ValidHeaderFieldValue(v) {
2029
				// Don't include the value in the error,
2030
				// because it may be sensitive.
2031
				return fmt.Sprintf("value for header %q", k)
2032
			}
2033
		}
2034
	}
2035
	return ""
2036
}
2037

2038
var errNilRequestURL = errors.New("http2: Request.URI is nil")
2039

2040
// requires cc.wmu be held.
2041
func (cc *ClientConn) encodeHeaders(req *http.Request, addGzipHeader bool, trailers string, contentLength int64) ([]byte, error) {
2042
	cc.hbuf.Reset()
2043
	if req.URL == nil {
2044
		return nil, errNilRequestURL
2045
	}
2046

2047
	host := req.Host
2048
	if host == "" {
2049
		host = req.URL.Host
2050
	}
2051
	host, err := httpguts.PunycodeHostPort(host)
2052
	if err != nil {
2053
		return nil, err
2054
	}
2055
	if !httpguts.ValidHostHeader(host) {
2056
		return nil, errors.New("http2: invalid Host header")
2057
	}
2058

2059
	var path string
2060
	if req.Method != "CONNECT" {
2061
		path = req.URL.RequestURI()
2062
		if !validPseudoPath(path) {
2063
			orig := path
2064
			path = strings.TrimPrefix(path, req.URL.Scheme+"://"+host)
2065
			if !validPseudoPath(path) {
2066
				if req.URL.Opaque != "" {
2067
					return nil, fmt.Errorf("invalid request :path %q from URL.Opaque = %q", orig, req.URL.Opaque)
2068
				} else {
2069
					return nil, fmt.Errorf("invalid request :path %q", orig)
2070
				}
2071
			}
2072
		}
2073
	}
2074

2075
	// Check for any invalid headers+trailers and return an error before we
2076
	// potentially pollute our hpack state. (We want to be able to
2077
	// continue to reuse the hpack encoder for future requests)
2078
	if err := validateHeaders(req.Header); err != "" {
2079
		return nil, fmt.Errorf("invalid HTTP header %s", err)
2080
	}
2081
	if err := validateHeaders(req.Trailer); err != "" {
2082
		return nil, fmt.Errorf("invalid HTTP trailer %s", err)
2083
	}
2084

2085
	enumerateHeaders := func(f func(name, value string)) {
2086
		// 8.1.2.3 Request Pseudo-Header Fields
2087
		// The :path pseudo-header field includes the path and query parts of the
2088
		// target URI (the path-absolute production and optionally a '?' character
2089
		// followed by the query production, see Sections 3.3 and 3.4 of
2090
		// [RFC3986]).
2091
		f(":authority", host)
2092
		m := req.Method
2093
		if m == "" {
2094
			m = http.MethodGet
2095
		}
2096
		f(":method", m)
2097
		if req.Method != "CONNECT" {
2098
			f(":path", path)
2099
			f(":scheme", req.URL.Scheme)
2100
		}
2101
		if trailers != "" {
2102
			f("trailer", trailers)
2103
		}
2104

2105
		var didUA bool
2106
		for k, vv := range req.Header {
2107
			if asciiEqualFold(k, "host") || asciiEqualFold(k, "content-length") {
2108
				// Host is :authority, already sent.
2109
				// Content-Length is automatic, set below.
2110
				continue
2111
			} else if asciiEqualFold(k, "connection") ||
2112
				asciiEqualFold(k, "proxy-connection") ||
2113
				asciiEqualFold(k, "transfer-encoding") ||
2114
				asciiEqualFold(k, "upgrade") ||
2115
				asciiEqualFold(k, "keep-alive") {
2116
				// Per 8.1.2.2 Connection-Specific Header
2117
				// Fields, don't send connection-specific
2118
				// fields. We have already checked if any
2119
				// are error-worthy so just ignore the rest.
2120
				continue
2121
			} else if asciiEqualFold(k, "user-agent") {
2122
				// Match Go's http1 behavior: at most one
2123
				// User-Agent. If set to nil or empty string,
2124
				// then omit it. Otherwise if not mentioned,
2125
				// include the default (below).
2126
				didUA = true
2127
				if len(vv) < 1 {
2128
					continue
2129
				}
2130
				vv = vv[:1]
2131
				if vv[0] == "" {
2132
					continue
2133
				}
2134
			} else if asciiEqualFold(k, "cookie") {
2135
				// Per 8.1.2.5 To allow for better compression efficiency, the
2136
				// Cookie header field MAY be split into separate header fields,
2137
				// each with one or more cookie-pairs.
2138
				for _, v := range vv {
2139
					for {
2140
						p := strings.IndexByte(v, ';')
2141
						if p < 0 {
2142
							break
2143
						}
2144
						f("cookie", v[:p])
2145
						p++
2146
						// strip space after semicolon if any.
2147
						for p+1 <= len(v) && v[p] == ' ' {
2148
							p++
2149
						}
2150
						v = v[p:]
2151
					}
2152
					if len(v) > 0 {
2153
						f("cookie", v)
2154
					}
2155
				}
2156
				continue
2157
			}
2158

2159
			for _, v := range vv {
2160
				f(k, v)
2161
			}
2162
		}
2163
		if shouldSendReqContentLength(req.Method, contentLength) {
2164
			f("content-length", strconv.FormatInt(contentLength, 10))
2165
		}
2166
		if addGzipHeader {
2167
			f("accept-encoding", "gzip")
2168
		}
2169
		if !didUA {
2170
			f("user-agent", defaultUserAgent)
2171
		}
2172
	}
2173

2174
	// Do a first pass over the headers counting bytes to ensure
2175
	// we don't exceed cc.peerMaxHeaderListSize. This is done as a
2176
	// separate pass before encoding the headers to prevent
2177
	// modifying the hpack state.
2178
	hlSize := uint64(0)
2179
	enumerateHeaders(func(name, value string) {
2180
		hf := hpack.HeaderField{Name: name, Value: value}
2181
		hlSize += uint64(hf.Size())
2182
	})
2183

2184
	if hlSize > cc.peerMaxHeaderListSize {
2185
		return nil, errRequestHeaderListSize
2186
	}
2187

2188
	trace := httptrace.ContextClientTrace(req.Context())
2189
	traceHeaders := traceHasWroteHeaderField(trace)
2190

2191
	// Header list size is ok. Write the headers.
2192
	enumerateHeaders(func(name, value string) {
2193
		name, ascii := lowerHeader(name)
2194
		if !ascii {
2195
			// Skip writing invalid headers. Per RFC 7540, Section 8.1.2, header
2196
			// field names have to be ASCII characters (just as in HTTP/1.x).
2197
			return
2198
		}
2199
		cc.writeHeader(name, value)
2200
		if traceHeaders {
2201
			traceWroteHeaderField(trace, name, value)
2202
		}
2203
	})
2204

2205
	return cc.hbuf.Bytes(), nil
2206
}
2207

2208
// shouldSendReqContentLength reports whether the http2.Transport should send
2209
// a "content-length" request header. This logic is basically a copy of the net/http
2210
// transferWriter.shouldSendContentLength.
2211
// The contentLength is the corrected contentLength (so 0 means actually 0, not unknown).
2212
// -1 means unknown.
2213
func shouldSendReqContentLength(method string, contentLength int64) bool {
2214
	if contentLength > 0 {
2215
		return true
2216
	}
2217
	if contentLength < 0 {
2218
		return false
2219
	}
2220
	// For zero bodies, whether we send a content-length depends on the method.
2221
	// It also kinda doesn't matter for http2 either way, with END_STREAM.
2222
	switch method {
2223
	case "POST", "PUT", "PATCH":
2224
		return true
2225
	default:
2226
		return false
2227
	}
2228
}
2229

2230
// requires cc.wmu be held.
2231
func (cc *ClientConn) encodeTrailers(trailer http.Header) ([]byte, error) {
2232
	cc.hbuf.Reset()
2233

2234
	hlSize := uint64(0)
2235
	for k, vv := range trailer {
2236
		for _, v := range vv {
2237
			hf := hpack.HeaderField{Name: k, Value: v}
2238
			hlSize += uint64(hf.Size())
2239
		}
2240
	}
2241
	if hlSize > cc.peerMaxHeaderListSize {
2242
		return nil, errRequestHeaderListSize
2243
	}
2244

2245
	for k, vv := range trailer {
2246
		lowKey, ascii := lowerHeader(k)
2247
		if !ascii {
2248
			// Skip writing invalid headers. Per RFC 7540, Section 8.1.2, header
2249
			// field names have to be ASCII characters (just as in HTTP/1.x).
2250
			continue
2251
		}
2252
		// Transfer-Encoding, etc.. have already been filtered at the
2253
		// start of RoundTrip
2254
		for _, v := range vv {
2255
			cc.writeHeader(lowKey, v)
2256
		}
2257
	}
2258
	return cc.hbuf.Bytes(), nil
2259
}
2260

2261
func (cc *ClientConn) writeHeader(name, value string) {
2262
	if VerboseLogs {
2263
		log.Printf("http2: Transport encoding header %q = %q", name, value)
2264
	}
2265
	cc.henc.WriteField(hpack.HeaderField{Name: name, Value: value})
2266
}
2267

2268
type resAndError struct {
2269
	_   incomparable
2270
	res *http.Response
2271
	err error
2272
}
2273

2274
// requires cc.mu be held.
2275
func (cc *ClientConn) addStreamLocked(cs *clientStream) {
2276
	cs.flow.add(int32(cc.initialWindowSize))
2277
	cs.flow.setConnFlow(&cc.flow)
2278
	cs.inflow.init(transportDefaultStreamFlow)
2279
	cs.ID = cc.nextStreamID
2280
	cc.nextStreamID += 2
2281
	cc.streams[cs.ID] = cs
2282
	if cs.ID == 0 {
2283
		panic("assigned stream ID 0")
2284
	}
2285
}
2286

2287
func (cc *ClientConn) forgetStreamID(id uint32) {
2288
	cc.mu.Lock()
2289
	slen := len(cc.streams)
2290
	delete(cc.streams, id)
2291
	if len(cc.streams) != slen-1 {
2292
		panic("forgetting unknown stream id")
2293
	}
2294
	cc.lastActive = time.Now()
2295
	if len(cc.streams) == 0 && cc.idleTimer != nil {
2296
		cc.idleTimer.Reset(cc.idleTimeout)
2297
		cc.lastIdle = time.Now()
2298
	}
2299
	// Wake up writeRequestBody via clientStream.awaitFlowControl and
2300
	// wake up RoundTrip if there is a pending request.
2301
	cc.condBroadcast()
2302

2303
	closeOnIdle := cc.singleUse || cc.doNotReuse || cc.t.disableKeepAlives() || cc.goAway != nil
2304
	if closeOnIdle && cc.streamsReserved == 0 && len(cc.streams) == 0 {
2305
		if VerboseLogs {
2306
			cc.vlogf("http2: Transport closing idle conn %p (forSingleUse=%v, maxStream=%v)", cc, cc.singleUse, cc.nextStreamID-2)
2307
		}
2308
		cc.closed = true
2309
		defer cc.closeConn()
2310
	}
2311

2312
	cc.mu.Unlock()
2313
}
2314

2315
// clientConnReadLoop is the state owned by the clientConn's frame-reading readLoop.
2316
type clientConnReadLoop struct {
2317
	_  incomparable
2318
	cc *ClientConn
2319
}
2320

2321
// readLoop runs in its own goroutine and reads and dispatches frames.
2322
func (cc *ClientConn) readLoop() {
2323
	rl := &clientConnReadLoop{cc: cc}
2324
	defer rl.cleanup()
2325
	cc.readerErr = rl.run()
2326
	if ce, ok := cc.readerErr.(ConnectionError); ok {
2327
		cc.wmu.Lock()
2328
		cc.fr.WriteGoAway(0, ErrCode(ce), nil)
2329
		cc.wmu.Unlock()
2330
	}
2331
}
2332

2333
// GoAwayError is returned by the Transport when the server closes the
2334
// TCP connection after sending a GOAWAY frame.
2335
type GoAwayError struct {
2336
	LastStreamID uint32
2337
	ErrCode      ErrCode
2338
	DebugData    string
2339
}
2340

2341
func (e GoAwayError) Error() string {
2342
	return fmt.Sprintf("http2: server sent GOAWAY and closed the connection; LastStreamID=%v, ErrCode=%v, debug=%q",
2343
		e.LastStreamID, e.ErrCode, e.DebugData)
2344
}
2345

2346
func isEOFOrNetReadError(err error) bool {
2347
	if err == io.EOF {
2348
		return true
2349
	}
2350
	ne, ok := err.(*net.OpError)
2351
	return ok && ne.Op == "read"
2352
}
2353

2354
func (rl *clientConnReadLoop) cleanup() {
2355
	cc := rl.cc
2356
	cc.t.connPool().MarkDead(cc)
2357
	defer cc.closeConn()
2358
	defer close(cc.readerDone)
2359

2360
	if cc.idleTimer != nil {
2361
		cc.idleTimer.Stop()
2362
	}
2363

2364
	// Close any response bodies if the server closes prematurely.
2365
	// TODO: also do this if we've written the headers but not
2366
	// gotten a response yet.
2367
	err := cc.readerErr
2368
	cc.mu.Lock()
2369
	if cc.goAway != nil && isEOFOrNetReadError(err) {
2370
		err = GoAwayError{
2371
			LastStreamID: cc.goAway.LastStreamID,
2372
			ErrCode:      cc.goAway.ErrCode,
2373
			DebugData:    cc.goAwayDebug,
2374
		}
2375
	} else if err == io.EOF {
2376
		err = io.ErrUnexpectedEOF
2377
	}
2378
	cc.closed = true
2379

2380
	for _, cs := range cc.streams {
2381
		select {
2382
		case <-cs.peerClosed:
2383
			// The server closed the stream before closing the conn,
2384
			// so no need to interrupt it.
2385
		default:
2386
			cs.abortStreamLocked(err)
2387
		}
2388
	}
2389
	cc.condBroadcast()
2390
	cc.mu.Unlock()
2391
}
2392

2393
// countReadFrameError calls Transport.CountError with a string
2394
// representing err.
2395
func (cc *ClientConn) countReadFrameError(err error) {
2396
	f := cc.t.CountError
2397
	if f == nil || err == nil {
2398
		return
2399
	}
2400
	if ce, ok := err.(ConnectionError); ok {
2401
		errCode := ErrCode(ce)
2402
		f(fmt.Sprintf("read_frame_conn_error_%s", errCode.stringToken()))
2403
		return
2404
	}
2405
	if errors.Is(err, io.EOF) {
2406
		f("read_frame_eof")
2407
		return
2408
	}
2409
	if errors.Is(err, io.ErrUnexpectedEOF) {
2410
		f("read_frame_unexpected_eof")
2411
		return
2412
	}
2413
	if errors.Is(err, ErrFrameTooLarge) {
2414
		f("read_frame_too_large")
2415
		return
2416
	}
2417
	f("read_frame_other")
2418
}
2419

2420
func (rl *clientConnReadLoop) run() error {
2421
	cc := rl.cc
2422
	gotSettings := false
2423
	readIdleTimeout := cc.t.ReadIdleTimeout
2424
	var t timer
2425
	if readIdleTimeout != 0 {
2426
		t = cc.afterFunc(readIdleTimeout, cc.healthCheck)
2427
	}
2428
	for {
2429
		f, err := cc.fr.ReadFrame()
2430
		if t != nil {
2431
			t.Reset(readIdleTimeout)
2432
		}
2433
		if err != nil {
2434
			cc.vlogf("http2: Transport readFrame error on conn %p: (%T) %v", cc, err, err)
2435
		}
2436
		if se, ok := err.(StreamError); ok {
2437
			if cs := rl.streamByID(se.StreamID); cs != nil {
2438
				if se.Cause == nil {
2439
					se.Cause = cc.fr.errDetail
2440
				}
2441
				rl.endStreamError(cs, se)
2442
			}
2443
			continue
2444
		} else if err != nil {
2445
			cc.countReadFrameError(err)
2446
			return err
2447
		}
2448
		if VerboseLogs {
2449
			cc.vlogf("http2: Transport received %s", summarizeFrame(f))
2450
		}
2451
		if !gotSettings {
2452
			if _, ok := f.(*SettingsFrame); !ok {
2453
				cc.logf("protocol error: received %T before a SETTINGS frame", f)
2454
				return ConnectionError(ErrCodeProtocol)
2455
			}
2456
			gotSettings = true
2457
		}
2458

2459
		switch f := f.(type) {
2460
		case *MetaHeadersFrame:
2461
			err = rl.processHeaders(f)
2462
		case *DataFrame:
2463
			err = rl.processData(f)
2464
		case *GoAwayFrame:
2465
			err = rl.processGoAway(f)
2466
		case *RSTStreamFrame:
2467
			err = rl.processResetStream(f)
2468
		case *SettingsFrame:
2469
			err = rl.processSettings(f)
2470
		case *PushPromiseFrame:
2471
			err = rl.processPushPromise(f)
2472
		case *WindowUpdateFrame:
2473
			err = rl.processWindowUpdate(f)
2474
		case *PingFrame:
2475
			err = rl.processPing(f)
2476
		default:
2477
			cc.logf("Transport: unhandled response frame type %T", f)
2478
		}
2479
		if err != nil {
2480
			if VerboseLogs {
2481
				cc.vlogf("http2: Transport conn %p received error from processing frame %v: %v", cc, summarizeFrame(f), err)
2482
			}
2483
			return err
2484
		}
2485
	}
2486
}
2487

2488
func (rl *clientConnReadLoop) processHeaders(f *MetaHeadersFrame) error {
2489
	cs := rl.streamByID(f.StreamID)
2490
	if cs == nil {
2491
		// We'd get here if we canceled a request while the
2492
		// server had its response still in flight. So if this
2493
		// was just something we canceled, ignore it.
2494
		return nil
2495
	}
2496
	if cs.readClosed {
2497
		rl.endStreamError(cs, StreamError{
2498
			StreamID: f.StreamID,
2499
			Code:     ErrCodeProtocol,
2500
			Cause:    errors.New("protocol error: headers after END_STREAM"),
2501
		})
2502
		return nil
2503
	}
2504
	if !cs.firstByte {
2505
		if cs.trace != nil {
2506
			// TODO(bradfitz): move first response byte earlier,
2507
			// when we first read the 9 byte header, not waiting
2508
			// until all the HEADERS+CONTINUATION frames have been
2509
			// merged. This works for now.
2510
			traceFirstResponseByte(cs.trace)
2511
		}
2512
		cs.firstByte = true
2513
	}
2514
	if !cs.pastHeaders {
2515
		cs.pastHeaders = true
2516
	} else {
2517
		return rl.processTrailers(cs, f)
2518
	}
2519

2520
	res, err := rl.handleResponse(cs, f)
2521
	if err != nil {
2522
		if _, ok := err.(ConnectionError); ok {
2523
			return err
2524
		}
2525
		// Any other error type is a stream error.
2526
		rl.endStreamError(cs, StreamError{
2527
			StreamID: f.StreamID,
2528
			Code:     ErrCodeProtocol,
2529
			Cause:    err,
2530
		})
2531
		return nil // return nil from process* funcs to keep conn alive
2532
	}
2533
	if res == nil {
2534
		// (nil, nil) special case. See handleResponse docs.
2535
		return nil
2536
	}
2537
	cs.resTrailer = &res.Trailer
2538
	cs.res = res
2539
	close(cs.respHeaderRecv)
2540
	if f.StreamEnded() {
2541
		rl.endStream(cs)
2542
	}
2543
	return nil
2544
}
2545

2546
// may return error types nil, or ConnectionError. Any other error value
2547
// is a StreamError of type ErrCodeProtocol. The returned error in that case
2548
// is the detail.
2549
//
2550
// As a special case, handleResponse may return (nil, nil) to skip the
2551
// frame (currently only used for 1xx responses).
2552
func (rl *clientConnReadLoop) handleResponse(cs *clientStream, f *MetaHeadersFrame) (*http.Response, error) {
2553
	if f.Truncated {
2554
		return nil, errResponseHeaderListSize
2555
	}
2556

2557
	status := f.PseudoValue("status")
2558
	if status == "" {
2559
		return nil, errors.New("malformed response from server: missing status pseudo header")
2560
	}
2561
	statusCode, err := strconv.Atoi(status)
2562
	if err != nil {
2563
		return nil, errors.New("malformed response from server: malformed non-numeric status pseudo header")
2564
	}
2565

2566
	regularFields := f.RegularFields()
2567
	strs := make([]string, len(regularFields))
2568
	header := make(http.Header, len(regularFields))
2569
	res := &http.Response{
2570
		Proto:      "HTTP/2.0",
2571
		ProtoMajor: 2,
2572
		Header:     header,
2573
		StatusCode: statusCode,
2574
		Status:     status + " " + http.StatusText(statusCode),
2575
	}
2576
	for _, hf := range regularFields {
2577
		key := canonicalHeader(hf.Name)
2578
		if key == "Trailer" {
2579
			t := res.Trailer
2580
			if t == nil {
2581
				t = make(http.Header)
2582
				res.Trailer = t
2583
			}
2584
			foreachHeaderElement(hf.Value, func(v string) {
2585
				t[canonicalHeader(v)] = nil
2586
			})
2587
		} else {
2588
			vv := header[key]
2589
			if vv == nil && len(strs) > 0 {
2590
				// More than likely this will be a single-element key.
2591
				// Most headers aren't multi-valued.
2592
				// Set the capacity on strs[0] to 1, so any future append
2593
				// won't extend the slice into the other strings.
2594
				vv, strs = strs[:1:1], strs[1:]
2595
				vv[0] = hf.Value
2596
				header[key] = vv
2597
			} else {
2598
				header[key] = append(vv, hf.Value)
2599
			}
2600
		}
2601
	}
2602

2603
	if statusCode >= 100 && statusCode <= 199 {
2604
		if f.StreamEnded() {
2605
			return nil, errors.New("1xx informational response with END_STREAM flag")
2606
		}
2607
		cs.num1xx++
2608
		const max1xxResponses = 5 // arbitrary bound on number of informational responses, same as net/http
2609
		if cs.num1xx > max1xxResponses {
2610
			return nil, errors.New("http2: too many 1xx informational responses")
2611
		}
2612
		if fn := cs.get1xxTraceFunc(); fn != nil {
2613
			if err := fn(statusCode, textproto.MIMEHeader(header)); err != nil {
2614
				return nil, err
2615
			}
2616
		}
2617
		if statusCode == 100 {
2618
			traceGot100Continue(cs.trace)
2619
			select {
2620
			case cs.on100 <- struct{}{}:
2621
			default:
2622
			}
2623
		}
2624
		cs.pastHeaders = false // do it all again
2625
		return nil, nil
2626
	}
2627

2628
	res.ContentLength = -1
2629
	if clens := res.Header["Content-Length"]; len(clens) == 1 {
2630
		if cl, err := strconv.ParseUint(clens[0], 10, 63); err == nil {
2631
			res.ContentLength = int64(cl)
2632
		} else {
2633
			// TODO: care? unlike http/1, it won't mess up our framing, so it's
2634
			// more safe smuggling-wise to ignore.
2635
		}
2636
	} else if len(clens) > 1 {
2637
		// TODO: care? unlike http/1, it won't mess up our framing, so it's
2638
		// more safe smuggling-wise to ignore.
2639
	} else if f.StreamEnded() && !cs.isHead {
2640
		res.ContentLength = 0
2641
	}
2642

2643
	if cs.isHead {
2644
		res.Body = noBody
2645
		return res, nil
2646
	}
2647

2648
	if f.StreamEnded() {
2649
		if res.ContentLength > 0 {
2650
			res.Body = missingBody{}
2651
		} else {
2652
			res.Body = noBody
2653
		}
2654
		return res, nil
2655
	}
2656

2657
	cs.bufPipe.setBuffer(&dataBuffer{expected: res.ContentLength})
2658
	cs.bytesRemain = res.ContentLength
2659
	res.Body = transportResponseBody{cs}
2660

2661
	if cs.requestedGzip && asciiEqualFold(res.Header.Get("Content-Encoding"), "gzip") {
2662
		res.Header.Del("Content-Encoding")
2663
		res.Header.Del("Content-Length")
2664
		res.ContentLength = -1
2665
		res.Body = &gzipReader{body: res.Body}
2666
		res.Uncompressed = true
2667
	}
2668
	return res, nil
2669
}
2670

2671
func (rl *clientConnReadLoop) processTrailers(cs *clientStream, f *MetaHeadersFrame) error {
2672
	if cs.pastTrailers {
2673
		// Too many HEADERS frames for this stream.
2674
		return ConnectionError(ErrCodeProtocol)
2675
	}
2676
	cs.pastTrailers = true
2677
	if !f.StreamEnded() {
2678
		// We expect that any headers for trailers also
2679
		// has END_STREAM.
2680
		return ConnectionError(ErrCodeProtocol)
2681
	}
2682
	if len(f.PseudoFields()) > 0 {
2683
		// No pseudo header fields are defined for trailers.
2684
		// TODO: ConnectionError might be overly harsh? Check.
2685
		return ConnectionError(ErrCodeProtocol)
2686
	}
2687

2688
	trailer := make(http.Header)
2689
	for _, hf := range f.RegularFields() {
2690
		key := canonicalHeader(hf.Name)
2691
		trailer[key] = append(trailer[key], hf.Value)
2692
	}
2693
	cs.trailer = trailer
2694

2695
	rl.endStream(cs)
2696
	return nil
2697
}
2698

2699
// transportResponseBody is the concrete type of Transport.RoundTrip's
2700
// Response.Body. It is an io.ReadCloser.
2701
type transportResponseBody struct {
2702
	cs *clientStream
2703
}
2704

2705
func (b transportResponseBody) Read(p []byte) (n int, err error) {
2706
	cs := b.cs
2707
	cc := cs.cc
2708

2709
	if cs.readErr != nil {
2710
		return 0, cs.readErr
2711
	}
2712
	n, err = b.cs.bufPipe.Read(p)
2713
	if cs.bytesRemain != -1 {
2714
		if int64(n) > cs.bytesRemain {
2715
			n = int(cs.bytesRemain)
2716
			if err == nil {
2717
				err = errors.New("net/http: server replied with more than declared Content-Length; truncated")
2718
				cs.abortStream(err)
2719
			}
2720
			cs.readErr = err
2721
			return int(cs.bytesRemain), err
2722
		}
2723
		cs.bytesRemain -= int64(n)
2724
		if err == io.EOF && cs.bytesRemain > 0 {
2725
			err = io.ErrUnexpectedEOF
2726
			cs.readErr = err
2727
			return n, err
2728
		}
2729
	}
2730
	if n == 0 {
2731
		// No flow control tokens to send back.
2732
		return
2733
	}
2734

2735
	cc.mu.Lock()
2736
	connAdd := cc.inflow.add(n)
2737
	var streamAdd int32
2738
	if err == nil { // No need to refresh if the stream is over or failed.
2739
		streamAdd = cs.inflow.add(n)
2740
	}
2741
	cc.mu.Unlock()
2742

2743
	if connAdd != 0 || streamAdd != 0 {
2744
		cc.wmu.Lock()
2745
		defer cc.wmu.Unlock()
2746
		if connAdd != 0 {
2747
			cc.fr.WriteWindowUpdate(0, mustUint31(connAdd))
2748
		}
2749
		if streamAdd != 0 {
2750
			cc.fr.WriteWindowUpdate(cs.ID, mustUint31(streamAdd))
2751
		}
2752
		cc.bw.Flush()
2753
	}
2754
	return
2755
}
2756

2757
var errClosedResponseBody = errors.New("http2: response body closed")
2758

2759
func (b transportResponseBody) Close() error {
2760
	cs := b.cs
2761
	cc := cs.cc
2762

2763
	cs.bufPipe.BreakWithError(errClosedResponseBody)
2764
	cs.abortStream(errClosedResponseBody)
2765

2766
	unread := cs.bufPipe.Len()
2767
	if unread > 0 {
2768
		cc.mu.Lock()
2769
		// Return connection-level flow control.
2770
		connAdd := cc.inflow.add(unread)
2771
		cc.mu.Unlock()
2772

2773
		// TODO(dneil): Acquiring this mutex can block indefinitely.
2774
		// Move flow control return to a goroutine?
2775
		cc.wmu.Lock()
2776
		// Return connection-level flow control.
2777
		if connAdd > 0 {
2778
			cc.fr.WriteWindowUpdate(0, uint32(connAdd))
2779
		}
2780
		cc.bw.Flush()
2781
		cc.wmu.Unlock()
2782
	}
2783

2784
	select {
2785
	case <-cs.donec:
2786
	case <-cs.ctx.Done():
2787
		// See golang/go#49366: The net/http package can cancel the
2788
		// request context after the response body is fully read.
2789
		// Don't treat this as an error.
2790
		return nil
2791
	case <-cs.reqCancel:
2792
		return errRequestCanceled
2793
	}
2794
	return nil
2795
}
2796

2797
func (rl *clientConnReadLoop) processData(f *DataFrame) error {
2798
	cc := rl.cc
2799
	cs := rl.streamByID(f.StreamID)
2800
	data := f.Data()
2801
	if cs == nil {
2802
		cc.mu.Lock()
2803
		neverSent := cc.nextStreamID
2804
		cc.mu.Unlock()
2805
		if f.StreamID >= neverSent {
2806
			// We never asked for this.
2807
			cc.logf("http2: Transport received unsolicited DATA frame; closing connection")
2808
			return ConnectionError(ErrCodeProtocol)
2809
		}
2810
		// We probably did ask for this, but canceled. Just ignore it.
2811
		// TODO: be stricter here? only silently ignore things which
2812
		// we canceled, but not things which were closed normally
2813
		// by the peer? Tough without accumulating too much state.
2814

2815
		// But at least return their flow control:
2816
		if f.Length > 0 {
2817
			cc.mu.Lock()
2818
			ok := cc.inflow.take(f.Length)
2819
			connAdd := cc.inflow.add(int(f.Length))
2820
			cc.mu.Unlock()
2821
			if !ok {
2822
				return ConnectionError(ErrCodeFlowControl)
2823
			}
2824
			if connAdd > 0 {
2825
				cc.wmu.Lock()
2826
				cc.fr.WriteWindowUpdate(0, uint32(connAdd))
2827
				cc.bw.Flush()
2828
				cc.wmu.Unlock()
2829
			}
2830
		}
2831
		return nil
2832
	}
2833
	if cs.readClosed {
2834
		cc.logf("protocol error: received DATA after END_STREAM")
2835
		rl.endStreamError(cs, StreamError{
2836
			StreamID: f.StreamID,
2837
			Code:     ErrCodeProtocol,
2838
		})
2839
		return nil
2840
	}
2841
	if !cs.pastHeaders {
2842
		cc.logf("protocol error: received DATA before a HEADERS frame")
2843
		rl.endStreamError(cs, StreamError{
2844
			StreamID: f.StreamID,
2845
			Code:     ErrCodeProtocol,
2846
		})
2847
		return nil
2848
	}
2849
	if f.Length > 0 {
2850
		if cs.isHead && len(data) > 0 {
2851
			cc.logf("protocol error: received DATA on a HEAD request")
2852
			rl.endStreamError(cs, StreamError{
2853
				StreamID: f.StreamID,
2854
				Code:     ErrCodeProtocol,
2855
			})
2856
			return nil
2857
		}
2858
		// Check connection-level flow control.
2859
		cc.mu.Lock()
2860
		if !takeInflows(&cc.inflow, &cs.inflow, f.Length) {
2861
			cc.mu.Unlock()
2862
			return ConnectionError(ErrCodeFlowControl)
2863
		}
2864
		// Return any padded flow control now, since we won't
2865
		// refund it later on body reads.
2866
		var refund int
2867
		if pad := int(f.Length) - len(data); pad > 0 {
2868
			refund += pad
2869
		}
2870

2871
		didReset := false
2872
		var err error
2873
		if len(data) > 0 {
2874
			if _, err = cs.bufPipe.Write(data); err != nil {
2875
				// Return len(data) now if the stream is already closed,
2876
				// since data will never be read.
2877
				didReset = true
2878
				refund += len(data)
2879
			}
2880
		}
2881

2882
		sendConn := cc.inflow.add(refund)
2883
		var sendStream int32
2884
		if !didReset {
2885
			sendStream = cs.inflow.add(refund)
2886
		}
2887
		cc.mu.Unlock()
2888

2889
		if sendConn > 0 || sendStream > 0 {
2890
			cc.wmu.Lock()
2891
			if sendConn > 0 {
2892
				cc.fr.WriteWindowUpdate(0, uint32(sendConn))
2893
			}
2894
			if sendStream > 0 {
2895
				cc.fr.WriteWindowUpdate(cs.ID, uint32(sendStream))
2896
			}
2897
			cc.bw.Flush()
2898
			cc.wmu.Unlock()
2899
		}
2900

2901
		if err != nil {
2902
			rl.endStreamError(cs, err)
2903
			return nil
2904
		}
2905
	}
2906

2907
	if f.StreamEnded() {
2908
		rl.endStream(cs)
2909
	}
2910
	return nil
2911
}
2912

2913
func (rl *clientConnReadLoop) endStream(cs *clientStream) {
2914
	// TODO: check that any declared content-length matches, like
2915
	// server.go's (*stream).endStream method.
2916
	if !cs.readClosed {
2917
		cs.readClosed = true
2918
		// Close cs.bufPipe and cs.peerClosed with cc.mu held to avoid a
2919
		// race condition: The caller can read io.EOF from Response.Body
2920
		// and close the body before we close cs.peerClosed, causing
2921
		// cleanupWriteRequest to send a RST_STREAM.
2922
		rl.cc.mu.Lock()
2923
		defer rl.cc.mu.Unlock()
2924
		cs.bufPipe.closeWithErrorAndCode(io.EOF, cs.copyTrailers)
2925
		close(cs.peerClosed)
2926
	}
2927
}
2928

2929
func (rl *clientConnReadLoop) endStreamError(cs *clientStream, err error) {
2930
	cs.readAborted = true
2931
	cs.abortStream(err)
2932
}
2933

2934
func (rl *clientConnReadLoop) streamByID(id uint32) *clientStream {
2935
	rl.cc.mu.Lock()
2936
	defer rl.cc.mu.Unlock()
2937
	cs := rl.cc.streams[id]
2938
	if cs != nil && !cs.readAborted {
2939
		return cs
2940
	}
2941
	return nil
2942
}
2943

2944
func (cs *clientStream) copyTrailers() {
2945
	for k, vv := range cs.trailer {
2946
		t := cs.resTrailer
2947
		if *t == nil {
2948
			*t = make(http.Header)
2949
		}
2950
		(*t)[k] = vv
2951
	}
2952
}
2953

2954
func (rl *clientConnReadLoop) processGoAway(f *GoAwayFrame) error {
2955
	cc := rl.cc
2956
	cc.t.connPool().MarkDead(cc)
2957
	if f.ErrCode != 0 {
2958
		// TODO: deal with GOAWAY more. particularly the error code
2959
		cc.vlogf("transport got GOAWAY with error code = %v", f.ErrCode)
2960
		if fn := cc.t.CountError; fn != nil {
2961
			fn("recv_goaway_" + f.ErrCode.stringToken())
2962
		}
2963
	}
2964
	cc.setGoAway(f)
2965
	return nil
2966
}
2967

2968
func (rl *clientConnReadLoop) processSettings(f *SettingsFrame) error {
2969
	cc := rl.cc
2970
	// Locking both mu and wmu here allows frame encoding to read settings with only wmu held.
2971
	// Acquiring wmu when f.IsAck() is unnecessary, but convenient and mostly harmless.
2972
	cc.wmu.Lock()
2973
	defer cc.wmu.Unlock()
2974

2975
	if err := rl.processSettingsNoWrite(f); err != nil {
2976
		return err
2977
	}
2978
	if !f.IsAck() {
2979
		cc.fr.WriteSettingsAck()
2980
		cc.bw.Flush()
2981
	}
2982
	return nil
2983
}
2984

2985
func (rl *clientConnReadLoop) processSettingsNoWrite(f *SettingsFrame) error {
2986
	cc := rl.cc
2987
	cc.mu.Lock()
2988
	defer cc.mu.Unlock()
2989

2990
	if f.IsAck() {
2991
		if cc.wantSettingsAck {
2992
			cc.wantSettingsAck = false
2993
			return nil
2994
		}
2995
		return ConnectionError(ErrCodeProtocol)
2996
	}
2997

2998
	var seenMaxConcurrentStreams bool
2999
	err := f.ForeachSetting(func(s Setting) error {
3000
		switch s.ID {
3001
		case SettingMaxFrameSize:
3002
			cc.maxFrameSize = s.Val
3003
		case SettingMaxConcurrentStreams:
3004
			cc.maxConcurrentStreams = s.Val
3005
			seenMaxConcurrentStreams = true
3006
		case SettingMaxHeaderListSize:
3007
			cc.peerMaxHeaderListSize = uint64(s.Val)
3008
		case SettingInitialWindowSize:
3009
			// Values above the maximum flow-control
3010
			// window size of 2^31-1 MUST be treated as a
3011
			// connection error (Section 5.4.1) of type
3012
			// FLOW_CONTROL_ERROR.
3013
			if s.Val > math.MaxInt32 {
3014
				return ConnectionError(ErrCodeFlowControl)
3015
			}
3016

3017
			// Adjust flow control of currently-open
3018
			// frames by the difference of the old initial
3019
			// window size and this one.
3020
			delta := int32(s.Val) - int32(cc.initialWindowSize)
3021
			for _, cs := range cc.streams {
3022
				cs.flow.add(delta)
3023
			}
3024
			cc.condBroadcast()
3025

3026
			cc.initialWindowSize = s.Val
3027
		case SettingHeaderTableSize:
3028
			cc.henc.SetMaxDynamicTableSize(s.Val)
3029
			cc.peerMaxHeaderTableSize = s.Val
3030
		default:
3031
			cc.vlogf("Unhandled Setting: %v", s)
3032
		}
3033
		return nil
3034
	})
3035
	if err != nil {
3036
		return err
3037
	}
3038

3039
	if !cc.seenSettings {
3040
		if !seenMaxConcurrentStreams {
3041
			// This was the servers initial SETTINGS frame and it
3042
			// didn't contain a MAX_CONCURRENT_STREAMS field so
3043
			// increase the number of concurrent streams this
3044
			// connection can establish to our default.
3045
			cc.maxConcurrentStreams = defaultMaxConcurrentStreams
3046
		}
3047
		cc.seenSettings = true
3048
	}
3049

3050
	return nil
3051
}
3052

3053
func (rl *clientConnReadLoop) processWindowUpdate(f *WindowUpdateFrame) error {
3054
	cc := rl.cc
3055
	cs := rl.streamByID(f.StreamID)
3056
	if f.StreamID != 0 && cs == nil {
3057
		return nil
3058
	}
3059

3060
	cc.mu.Lock()
3061
	defer cc.mu.Unlock()
3062

3063
	fl := &cc.flow
3064
	if cs != nil {
3065
		fl = &cs.flow
3066
	}
3067
	if !fl.add(int32(f.Increment)) {
3068
		// For stream, the sender sends RST_STREAM with an error code of FLOW_CONTROL_ERROR
3069
		if cs != nil {
3070
			rl.endStreamError(cs, StreamError{
3071
				StreamID: f.StreamID,
3072
				Code:     ErrCodeFlowControl,
3073
			})
3074
			return nil
3075
		}
3076

3077
		return ConnectionError(ErrCodeFlowControl)
3078
	}
3079
	cc.condBroadcast()
3080
	return nil
3081
}
3082

3083
func (rl *clientConnReadLoop) processResetStream(f *RSTStreamFrame) error {
3084
	cs := rl.streamByID(f.StreamID)
3085
	if cs == nil {
3086
		// TODO: return error if server tries to RST_STREAM an idle stream
3087
		return nil
3088
	}
3089
	serr := streamError(cs.ID, f.ErrCode)
3090
	serr.Cause = errFromPeer
3091
	if f.ErrCode == ErrCodeProtocol {
3092
		rl.cc.SetDoNotReuse()
3093
	}
3094
	if fn := cs.cc.t.CountError; fn != nil {
3095
		fn("recv_rststream_" + f.ErrCode.stringToken())
3096
	}
3097
	cs.abortStream(serr)
3098

3099
	cs.bufPipe.CloseWithError(serr)
3100
	return nil
3101
}
3102

3103
// Ping sends a PING frame to the server and waits for the ack.
3104
func (cc *ClientConn) Ping(ctx context.Context) error {
3105
	c := make(chan struct{})
3106
	// Generate a random payload
3107
	var p [8]byte
3108
	for {
3109
		if _, err := rand.Read(p[:]); err != nil {
3110
			return err
3111
		}
3112
		cc.mu.Lock()
3113
		// check for dup before insert
3114
		if _, found := cc.pings[p]; !found {
3115
			cc.pings[p] = c
3116
			cc.mu.Unlock()
3117
			break
3118
		}
3119
		cc.mu.Unlock()
3120
	}
3121
	var pingError error
3122
	errc := make(chan struct{})
3123
	cc.goRun(func() {
3124
		cc.wmu.Lock()
3125
		defer cc.wmu.Unlock()
3126
		if pingError = cc.fr.WritePing(false, p); pingError != nil {
3127
			close(errc)
3128
			return
3129
		}
3130
		if pingError = cc.bw.Flush(); pingError != nil {
3131
			close(errc)
3132
			return
3133
		}
3134
	})
3135
	if cc.syncHooks != nil {
3136
		cc.syncHooks.blockUntil(func() bool {
3137
			select {
3138
			case <-c:
3139
			case <-errc:
3140
			case <-ctx.Done():
3141
			case <-cc.readerDone:
3142
			default:
3143
				return false
3144
			}
3145
			return true
3146
		})
3147
	}
3148
	select {
3149
	case <-c:
3150
		return nil
3151
	case <-errc:
3152
		return pingError
3153
	case <-ctx.Done():
3154
		return ctx.Err()
3155
	case <-cc.readerDone:
3156
		// connection closed
3157
		return cc.readerErr
3158
	}
3159
}
3160

3161
func (rl *clientConnReadLoop) processPing(f *PingFrame) error {
3162
	if f.IsAck() {
3163
		cc := rl.cc
3164
		cc.mu.Lock()
3165
		defer cc.mu.Unlock()
3166
		// If ack, notify listener if any
3167
		if c, ok := cc.pings[f.Data]; ok {
3168
			close(c)
3169
			delete(cc.pings, f.Data)
3170
		}
3171
		return nil
3172
	}
3173
	cc := rl.cc
3174
	cc.wmu.Lock()
3175
	defer cc.wmu.Unlock()
3176
	if err := cc.fr.WritePing(true, f.Data); err != nil {
3177
		return err
3178
	}
3179
	return cc.bw.Flush()
3180
}
3181

3182
func (rl *clientConnReadLoop) processPushPromise(f *PushPromiseFrame) error {
3183
	// We told the peer we don't want them.
3184
	// Spec says:
3185
	// "PUSH_PROMISE MUST NOT be sent if the SETTINGS_ENABLE_PUSH
3186
	// setting of the peer endpoint is set to 0. An endpoint that
3187
	// has set this setting and has received acknowledgement MUST
3188
	// treat the receipt of a PUSH_PROMISE frame as a connection
3189
	// error (Section 5.4.1) of type PROTOCOL_ERROR."
3190
	return ConnectionError(ErrCodeProtocol)
3191
}
3192

3193
func (cc *ClientConn) writeStreamReset(streamID uint32, code ErrCode, err error) {
3194
	// TODO: map err to more interesting error codes, once the
3195
	// HTTP community comes up with some. But currently for
3196
	// RST_STREAM there's no equivalent to GOAWAY frame's debug
3197
	// data, and the error codes are all pretty vague ("cancel").
3198
	cc.wmu.Lock()
3199
	cc.fr.WriteRSTStream(streamID, code)
3200
	cc.bw.Flush()
3201
	cc.wmu.Unlock()
3202
}
3203

3204
var (
3205
	errResponseHeaderListSize = errors.New("http2: response header list larger than advertised limit")
3206
	errRequestHeaderListSize  = errors.New("http2: request header list larger than peer's advertised limit")
3207
)
3208

3209
func (cc *ClientConn) logf(format string, args ...interface{}) {
3210
	cc.t.logf(format, args...)
3211
}
3212

3213
func (cc *ClientConn) vlogf(format string, args ...interface{}) {
3214
	cc.t.vlogf(format, args...)
3215
}
3216

3217
func (t *Transport) vlogf(format string, args ...interface{}) {
3218
	if VerboseLogs {
3219
		t.logf(format, args...)
3220
	}
3221
}
3222

3223
func (t *Transport) logf(format string, args ...interface{}) {
3224
	log.Printf(format, args...)
3225
}
3226

3227
var noBody io.ReadCloser = noBodyReader{}
3228

3229
type noBodyReader struct{}
3230

3231
func (noBodyReader) Close() error             { return nil }
3232
func (noBodyReader) Read([]byte) (int, error) { return 0, io.EOF }
3233

3234
type missingBody struct{}
3235

3236
func (missingBody) Close() error             { return nil }
3237
func (missingBody) Read([]byte) (int, error) { return 0, io.ErrUnexpectedEOF }
3238

3239
func strSliceContains(ss []string, s string) bool {
3240
	for _, v := range ss {
3241
		if v == s {
3242
			return true
3243
		}
3244
	}
3245
	return false
3246
}
3247

3248
type erringRoundTripper struct{ err error }
3249

3250
func (rt erringRoundTripper) RoundTripErr() error                             { return rt.err }
3251
func (rt erringRoundTripper) RoundTrip(*http.Request) (*http.Response, error) { return nil, rt.err }
3252

3253
// gzipReader wraps a response body so it can lazily
3254
// call gzip.NewReader on the first call to Read
3255
type gzipReader struct {
3256
	_    incomparable
3257
	body io.ReadCloser // underlying Response.Body
3258
	zr   *gzip.Reader  // lazily-initialized gzip reader
3259
	zerr error         // sticky error
3260
}
3261

3262
func (gz *gzipReader) Read(p []byte) (n int, err error) {
3263
	if gz.zerr != nil {
3264
		return 0, gz.zerr
3265
	}
3266
	if gz.zr == nil {
3267
		gz.zr, err = gzip.NewReader(gz.body)
3268
		if err != nil {
3269
			gz.zerr = err
3270
			return 0, err
3271
		}
3272
	}
3273
	return gz.zr.Read(p)
3274
}
3275

3276
func (gz *gzipReader) Close() error {
3277
	if err := gz.body.Close(); err != nil {
3278
		return err
3279
	}
3280
	gz.zerr = fs.ErrClosed
3281
	return nil
3282
}
3283

3284
type errorReader struct{ err error }
3285

3286
func (r errorReader) Read(p []byte) (int, error) { return 0, r.err }
3287

3288
// isConnectionCloseRequest reports whether req should use its own
3289
// connection for a single request and then close the connection.
3290
func isConnectionCloseRequest(req *http.Request) bool {
3291
	return req.Close || httpguts.HeaderValuesContainsToken(req.Header["Connection"], "close")
3292
}
3293

3294
// registerHTTPSProtocol calls Transport.RegisterProtocol but
3295
// converting panics into errors.
3296
func registerHTTPSProtocol(t *http.Transport, rt noDialH2RoundTripper) (err error) {
3297
	defer func() {
3298
		if e := recover(); e != nil {
3299
			err = fmt.Errorf("%v", e)
3300
		}
3301
	}()
3302
	t.RegisterProtocol("https", rt)
3303
	return nil
3304
}
3305

3306
// noDialH2RoundTripper is a RoundTripper which only tries to complete the request
3307
// if there's already has a cached connection to the host.
3308
// (The field is exported so it can be accessed via reflect from net/http; tested
3309
// by TestNoDialH2RoundTripperType)
3310
type noDialH2RoundTripper struct{ *Transport }
3311

3312
func (rt noDialH2RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
3313
	res, err := rt.Transport.RoundTrip(req)
3314
	if isNoCachedConnError(err) {
3315
		return nil, http.ErrSkipAltProtocol
3316
	}
3317
	return res, err
3318
}
3319

3320
func (t *Transport) idleConnTimeout() time.Duration {
3321
	// to keep things backwards compatible, we use non-zero values of
3322
	// IdleConnTimeout, followed by using the IdleConnTimeout on the underlying
3323
	// http1 transport, followed by 0
3324
	if t.IdleConnTimeout != 0 {
3325
		return t.IdleConnTimeout
3326
	}
3327

3328
	if t.t1 != nil {
3329
		return t.t1.IdleConnTimeout
3330
	}
3331

3332
	return 0
3333
}
3334

3335
func traceGetConn(req *http.Request, hostPort string) {
3336
	trace := httptrace.ContextClientTrace(req.Context())
3337
	if trace == nil || trace.GetConn == nil {
3338
		return
3339
	}
3340
	trace.GetConn(hostPort)
3341
}
3342

3343
func traceGotConn(req *http.Request, cc *ClientConn, reused bool) {
3344
	trace := httptrace.ContextClientTrace(req.Context())
3345
	if trace == nil || trace.GotConn == nil {
3346
		return
3347
	}
3348
	ci := httptrace.GotConnInfo{Conn: cc.tconn}
3349
	ci.Reused = reused
3350
	cc.mu.Lock()
3351
	ci.WasIdle = len(cc.streams) == 0 && reused
3352
	if ci.WasIdle && !cc.lastActive.IsZero() {
3353
		ci.IdleTime = time.Since(cc.lastActive)
3354
	}
3355
	cc.mu.Unlock()
3356

3357
	trace.GotConn(ci)
3358
}
3359

3360
func traceWroteHeaders(trace *httptrace.ClientTrace) {
3361
	if trace != nil && trace.WroteHeaders != nil {
3362
		trace.WroteHeaders()
3363
	}
3364
}
3365

3366
func traceGot100Continue(trace *httptrace.ClientTrace) {
3367
	if trace != nil && trace.Got100Continue != nil {
3368
		trace.Got100Continue()
3369
	}
3370
}
3371

3372
func traceWait100Continue(trace *httptrace.ClientTrace) {
3373
	if trace != nil && trace.Wait100Continue != nil {
3374
		trace.Wait100Continue()
3375
	}
3376
}
3377

3378
func traceWroteRequest(trace *httptrace.ClientTrace, err error) {
3379
	if trace != nil && trace.WroteRequest != nil {
3380
		trace.WroteRequest(httptrace.WroteRequestInfo{Err: err})
3381
	}
3382
}
3383

3384
func traceFirstResponseByte(trace *httptrace.ClientTrace) {
3385
	if trace != nil && trace.GotFirstResponseByte != nil {
3386
		trace.GotFirstResponseByte()
3387
	}
3388
}
3389

3390
func traceHasWroteHeaderField(trace *httptrace.ClientTrace) bool {
3391
	return trace != nil && trace.WroteHeaderField != nil
3392
}
3393

3394
func traceWroteHeaderField(trace *httptrace.ClientTrace, k, v string) {
3395
	if trace != nil && trace.WroteHeaderField != nil {
3396
		trace.WroteHeaderField(k, []string{v})
3397
	}
3398
}
3399

3400
func traceGot1xxResponseFunc(trace *httptrace.ClientTrace) func(int, textproto.MIMEHeader) error {
3401
	if trace != nil {
3402
		return trace.Got1xxResponse
3403
	}
3404
	return nil
3405
}
3406

3407
// dialTLSWithContext uses tls.Dialer, added in Go 1.15, to open a TLS
3408
// connection.
3409
func (t *Transport) dialTLSWithContext(ctx context.Context, network, addr string, cfg *tls.Config) (*tls.Conn, error) {
3410
	dialer := &tls.Dialer{
3411
		Config: cfg,
3412
	}
3413
	cn, err := dialer.DialContext(ctx, network, addr)
3414
	if err != nil {
3415
		return nil, err
3416
	}
3417
	tlsCn := cn.(*tls.Conn) // DialContext comment promises this will always succeed
3418
	return tlsCn, nil
3419
}
3420

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.