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.
35
"golang.org/x/net/http/httpguts"
36
"golang.org/x/net/http2/hpack"
37
"golang.org/x/net/idna"
41
// transportDefaultConnFlow is how many connection-level flow control
42
// tokens we give the server at start-up, past the default 64k.
43
transportDefaultConnFlow = 1 << 30
45
// transportDefaultStreamFlow is how many stream-level flow
46
// control tokens we announce to the peer, and how many bytes
47
// we buffer per stream.
48
transportDefaultStreamFlow = 4 << 20
50
defaultUserAgent = "Go-http-client/2.0"
52
// initialMaxConcurrentStreams is a connections maxConcurrentStreams until
53
// it's received servers initial SETTINGS frame, which corresponds with the
54
// spec's minimum recommended value.
55
initialMaxConcurrentStreams = 100
57
// defaultMaxConcurrentStreams is a connections default maxConcurrentStreams
58
// if the server doesn't include one in its initial SETTINGS frame.
59
defaultMaxConcurrentStreams = 1000
62
// Transport is an HTTP/2 Transport.
64
// A Transport internally caches connections to servers. It is safe
65
// for concurrent use by multiple goroutines.
66
type Transport struct {
67
// DialTLSContext specifies an optional dial function with context for
68
// creating TLS connections for requests.
70
// If DialTLSContext and DialTLS is nil, tls.Dial is used.
72
// If the returned net.Conn has a ConnectionState method like tls.Conn,
73
// it will be used to set http.Response.TLS.
74
DialTLSContext func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error)
76
// DialTLS specifies an optional dial function for creating
77
// TLS connections for requests.
79
// If DialTLSContext and DialTLS is nil, tls.Dial is used.
81
// Deprecated: Use DialTLSContext instead, which allows the transport
82
// to cancel dials as soon as they are no longer needed.
83
// If both are set, DialTLSContext takes priority.
84
DialTLS func(network, addr string, cfg *tls.Config) (net.Conn, error)
86
// TLSClientConfig specifies the TLS configuration to use with
87
// tls.Client. If nil, the default configuration is used.
88
TLSClientConfig *tls.Config
90
// ConnPool optionally specifies an alternate connection pool to use.
91
// If nil, the default is used.
92
ConnPool ClientConnPool
94
// DisableCompression, if true, prevents the Transport from
95
// requesting compression with an "Accept-Encoding: gzip"
96
// request header when the Request contains no existing
97
// Accept-Encoding value. If the Transport requests gzip on
98
// its own and gets a gzipped response, it's transparently
99
// decoded in the Response.Body. However, if the user
100
// explicitly requested gzip it is not automatically
102
DisableCompression bool
104
// AllowHTTP, if true, permits HTTP/2 requests using the insecure,
105
// plain-text "http" scheme. Note that this does not enable h2c support.
108
// MaxHeaderListSize is the http2 SETTINGS_MAX_HEADER_LIST_SIZE to
109
// send in the initial settings frame. It is how many bytes
110
// of response headers are allowed. Unlike the http2 spec, zero here
111
// means to use a default limit (currently 10MB). If you actually
112
// want to advertise an unlimited value to the peer, Transport
113
// interprets the highest possible value here (0xffffffff or 1<<32-1)
115
MaxHeaderListSize uint32
117
// MaxReadFrameSize is the http2 SETTINGS_MAX_FRAME_SIZE to send in the
118
// initial settings frame. It is the size in bytes of the largest frame
119
// payload that the sender is willing to receive. If 0, no setting is
120
// sent, and the value is provided by the peer, which should be 16384
121
// according to the spec:
122
// https://datatracker.ietf.org/doc/html/rfc7540#section-6.5.2.
123
// Values are bounded in the range 16k to 16M.
124
MaxReadFrameSize uint32
126
// MaxDecoderHeaderTableSize optionally specifies the http2
127
// SETTINGS_HEADER_TABLE_SIZE to send in the initial settings frame. It
128
// informs the remote endpoint of the maximum size of the header compression
129
// table used to decode header blocks, in octets. If zero, the default value
131
MaxDecoderHeaderTableSize uint32
133
// MaxEncoderHeaderTableSize optionally specifies an upper limit for the
134
// header compression table used for encoding request headers. Received
135
// SETTINGS_HEADER_TABLE_SIZE settings are capped at this limit. If zero,
136
// the default value of 4096 is used.
137
MaxEncoderHeaderTableSize uint32
139
// StrictMaxConcurrentStreams controls whether the server's
140
// SETTINGS_MAX_CONCURRENT_STREAMS should be respected
141
// globally. If false, new TCP connections are created to the
142
// server as needed to keep each under the per-connection
143
// SETTINGS_MAX_CONCURRENT_STREAMS limit. If true, the
144
// server's SETTINGS_MAX_CONCURRENT_STREAMS is interpreted as
145
// a global limit and callers of RoundTrip block when needed,
146
// waiting for their turn.
147
StrictMaxConcurrentStreams bool
149
// ReadIdleTimeout is the timeout after which a health check using ping
150
// frame will be carried out if no frame is received on the connection.
151
// Note that a ping response will is considered a received frame, so if
152
// there is no other traffic on the connection, the health check will
153
// be performed every ReadIdleTimeout interval.
154
// If zero, no health check is performed.
155
ReadIdleTimeout time.Duration
157
// PingTimeout is the timeout after which the connection will be closed
158
// if a response to Ping is not received.
160
PingTimeout time.Duration
162
// WriteByteTimeout is the timeout after which the connection will be
163
// closed no data can be written to it. The timeout begins when data is
164
// available to write, and is extended whenever any bytes are written.
165
WriteByteTimeout time.Duration
167
// CountError, if non-nil, is called on HTTP/2 transport errors.
168
// It's intended to increment a metric for monitoring, such
169
// as an expvar or Prometheus metric.
170
// The errType consists of only ASCII word characters.
171
CountError func(errType string)
173
// t1, if non-nil, is the standard library Transport using
174
// this transport. Its settings are used (but not its
175
// RoundTrip method, etc).
178
connPoolOnce sync.Once
179
connPoolOrDef ClientConnPool // non-nil version of ConnPool
182
func (t *Transport) maxHeaderListSize() uint32 {
183
if t.MaxHeaderListSize == 0 {
186
if t.MaxHeaderListSize == 0xffffffff {
189
return t.MaxHeaderListSize
192
func (t *Transport) maxFrameReadSize() uint32 {
193
if t.MaxReadFrameSize == 0 {
194
return 0 // use the default provided by the peer
196
if t.MaxReadFrameSize < minMaxFrameSize {
197
return minMaxFrameSize
199
if t.MaxReadFrameSize > maxFrameSize {
202
return t.MaxReadFrameSize
205
func (t *Transport) disableCompression() bool {
206
return t.DisableCompression || (t.t1 != nil && t.t1.DisableCompression)
209
func (t *Transport) pingTimeout() time.Duration {
210
if t.PingTimeout == 0 {
211
return 15 * time.Second
217
// ConfigureTransport configures a net/http HTTP/1 Transport to use HTTP/2.
218
// It returns an error if t1 has already been HTTP/2-enabled.
220
// Use ConfigureTransports instead to configure the HTTP/2 Transport.
221
func ConfigureTransport(t1 *http.Transport) error {
222
_, err := ConfigureTransports(t1)
226
// ConfigureTransports configures a net/http HTTP/1 Transport to use HTTP/2.
227
// It returns a new HTTP/2 Transport for further configuration.
228
// It returns an error if t1 has already been HTTP/2-enabled.
229
func ConfigureTransports(t1 *http.Transport) (*Transport, error) {
230
return configureTransports(t1)
233
func configureTransports(t1 *http.Transport) (*Transport, error) {
234
connPool := new(clientConnPool)
236
ConnPool: noDialClientConnPool{connPool},
240
if err := registerHTTPSProtocol(t1, noDialH2RoundTripper{t2}); err != nil {
243
if t1.TLSClientConfig == nil {
244
t1.TLSClientConfig = new(tls.Config)
246
if !strSliceContains(t1.TLSClientConfig.NextProtos, "h2") {
247
t1.TLSClientConfig.NextProtos = append([]string{"h2"}, t1.TLSClientConfig.NextProtos...)
249
if !strSliceContains(t1.TLSClientConfig.NextProtos, "http/1.1") {
250
t1.TLSClientConfig.NextProtos = append(t1.TLSClientConfig.NextProtos, "http/1.1")
252
upgradeFn := func(authority string, c *tls.Conn) http.RoundTripper {
253
addr := authorityAddr("https", authority)
254
if used, err := connPool.addConnIfNeeded(addr, t2, c); err != nil {
256
return erringRoundTripper{err}
258
// Turns out we don't need this c.
259
// For example, two goroutines made requests to the same host
260
// at the same time, both kicking off TCP dials. (since protocol
266
if m := t1.TLSNextProto; len(m) == 0 {
267
t1.TLSNextProto = map[string]func(string, *tls.Conn) http.RoundTripper{
276
func (t *Transport) connPool() ClientConnPool {
277
t.connPoolOnce.Do(t.initConnPool)
278
return t.connPoolOrDef
281
func (t *Transport) initConnPool() {
282
if t.ConnPool != nil {
283
t.connPoolOrDef = t.ConnPool
285
t.connPoolOrDef = &clientConnPool{t: t}
289
// ClientConn is the state of a single HTTP/2 client connection to an
291
type ClientConn struct {
293
tconn net.Conn // usually *tls.Conn, except specialized impls
295
tlsState *tls.ConnectionState // nil only for specialized impls
296
reused uint32 // whether conn is being reused; atomic
297
singleUse bool // whether being used for a single http.Request
298
getConnCalled bool // used by clientConnPool
300
// readLoop goroutine fields:
301
readerDone chan struct{} // closed on error
302
readerErr error // set before readerDone is closed
304
idleTimeout time.Duration // or 0 for never
305
idleTimer *time.Timer
307
mu sync.Mutex // guards following
308
cond *sync.Cond // hold mu; broadcast on flow/closed changes
309
flow outflow // our conn-level flow control quota (cs.outflow is per stream)
310
inflow inflow // peer's conn-level flow control
311
doNotReuse bool // whether conn is marked to not be reused for any future requests
314
seenSettings bool // true if we've seen a settings frame, false otherwise
315
wantSettingsAck bool // we sent a SETTINGS frame and haven't heard back
316
goAway *GoAwayFrame // if non-nil, the GoAwayFrame we received
317
goAwayDebug string // goAway frame's debug data, retained as a string
318
streams map[uint32]*clientStream // client-initiated
319
streamsReserved int // incr by ReserveNewRequest; decr on RoundTrip
321
pendingRequests int // requests blocked and waiting to be sent because len(streams) == maxConcurrentStreams
322
pings map[[8]byte]chan struct{} // in flight ping data to notification channel
325
lastIdle time.Time // time last idle
326
// Settings from peer: (also guarded by wmu)
328
maxConcurrentStreams uint32
329
peerMaxHeaderListSize uint64
330
peerMaxHeaderTableSize uint32
331
initialWindowSize uint32
333
// reqHeaderMu is a 1-element semaphore channel controlling access to sending new requests.
334
// Write to reqHeaderMu to lock it, read from it to unlock.
335
// Lock reqmu BEFORE mu or wmu.
336
reqHeaderMu chan struct{}
338
// wmu is held while writing.
339
// Acquire BEFORE mu when holding both, to avoid blocking mu on network writes.
340
// Only acquire both at the same time when changing peer settings.
344
werr error // first write error that has occurred
345
hbuf bytes.Buffer // HPACK encoder writes into this
349
// clientStream is the state for a single HTTP/2 stream. One of these
350
// is created for each Transport.RoundTrip call.
351
type clientStream struct {
354
// Fields of Request that we may access even after the response body is closed.
356
reqCancel <-chan struct{}
358
trace *httptrace.ClientTrace // or nil
360
bufPipe pipe // buffered pipe with the flow-controlled response payload
365
abort chan struct{} // closed to signal stream should end immediately
366
abortErr error // set if abort is closed
368
peerClosed chan struct{} // closed when the peer sends an END_STREAM flag
369
donec chan struct{} // closed after the stream is in the closed state
370
on100 chan struct{} // buffered; written to if a 100 is received
372
respHeaderRecv chan struct{} // closed when headers are received
373
res *http.Response // set if respHeaderRecv is closed
375
flow outflow // guarded by cc.mu
376
inflow inflow // guarded by cc.mu
377
bytesRemain int64 // -1 means unknown; owned by transportResponseBody.Read
378
readErr error // sticky read error; owned by transportResponseBody.Read
380
reqBody io.ReadCloser
381
reqBodyContentLength int64 // -1 means unknown
382
reqBodyClosed chan struct{} // guarded by cc.mu; non-nil on Close, closed when done
384
// owned by writeRequest:
385
sentEndStream bool // sent an END_STREAM flag to the peer
388
// owned by clientConnReadLoop:
389
firstByte bool // got the first response byte
390
pastHeaders bool // got first MetaHeadersFrame (actual headers)
391
pastTrailers bool // got optional second MetaHeadersFrame (trailers)
392
num1xx uint8 // number of 1xx responses seen
393
readClosed bool // peer sent an END_STREAM flag
394
readAborted bool // read loop reset the stream
396
trailer http.Header // accumulated trailers
397
resTrailer *http.Header // client's Response.Trailer
400
var got1xxFuncForTests func(int, textproto.MIMEHeader) error
402
// get1xxTraceFunc returns the value of request's httptrace.ClientTrace.Got1xxResponse func,
403
// if any. It returns nil if not set or if the Go version is too old.
404
func (cs *clientStream) get1xxTraceFunc() func(int, textproto.MIMEHeader) error {
405
if fn := got1xxFuncForTests; fn != nil {
408
return traceGot1xxResponseFunc(cs.trace)
411
func (cs *clientStream) abortStream(err error) {
413
defer cs.cc.mu.Unlock()
414
cs.abortStreamLocked(err)
417
func (cs *clientStream) abortStreamLocked(err error) {
418
cs.abortOnce.Do(func() {
422
if cs.reqBody != nil {
423
cs.closeReqBodyLocked()
425
// TODO(dneil): Clean up tests where cs.cc.cond is nil.
426
if cs.cc.cond != nil {
427
// Wake up writeRequestBody if it is waiting on flow control.
428
cs.cc.cond.Broadcast()
432
func (cs *clientStream) abortRequestBodyWrite() {
436
if cs.reqBody != nil && cs.reqBodyClosed == nil {
437
cs.closeReqBodyLocked()
442
func (cs *clientStream) closeReqBodyLocked() {
443
if cs.reqBodyClosed != nil {
446
cs.reqBodyClosed = make(chan struct{})
447
reqBodyClosed := cs.reqBodyClosed
454
type stickyErrWriter struct {
456
timeout time.Duration
460
func (sew stickyErrWriter) Write(p []byte) (n int, err error) {
465
if sew.timeout != 0 {
466
sew.conn.SetWriteDeadline(time.Now().Add(sew.timeout))
468
nn, err := sew.conn.Write(p[n:])
470
if n < len(p) && nn > 0 && errors.Is(err, os.ErrDeadlineExceeded) {
471
// Keep extending the deadline so long as we're making progress.
474
if sew.timeout != 0 {
475
sew.conn.SetWriteDeadline(time.Time{})
482
// noCachedConnError is the concrete type of ErrNoCachedConn, which
483
// needs to be detected by net/http regardless of whether it's its
484
// bundled version (in h2_bundle.go with a rewritten type name) or
485
// from a user's x/net/http2. As such, as it has a unique method name
486
// (IsHTTP2NoCachedConnError) that net/http sniffs for via func
487
// isNoCachedConnError.
488
type noCachedConnError struct{}
490
func (noCachedConnError) IsHTTP2NoCachedConnError() {}
491
func (noCachedConnError) Error() string { return "http2: no cached connection was available" }
493
// isNoCachedConnError reports whether err is of type noCachedConnError
494
// or its equivalent renamed type in net/http2's h2_bundle.go. Both types
495
// may coexist in the same running program.
496
func isNoCachedConnError(err error) bool {
497
_, ok := err.(interface{ IsHTTP2NoCachedConnError() })
501
var ErrNoCachedConn error = noCachedConnError{}
503
// RoundTripOpt are options for the Transport.RoundTripOpt method.
504
type RoundTripOpt struct {
505
// OnlyCachedConn controls whether RoundTripOpt may
506
// create a new TCP connection. If set true and
507
// no cached connection is available, RoundTripOpt
508
// will return ErrNoCachedConn.
512
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
513
return t.RoundTripOpt(req, RoundTripOpt{})
516
// authorityAddr returns a given authority (a host/IP, or host:port / ip:port)
517
// and returns a host:port. The port 443 is added if needed.
518
func authorityAddr(scheme string, authority string) (addr string) {
519
host, port, err := net.SplitHostPort(authority)
520
if err != nil { // authority didn't have a port
522
if scheme == "http" {
527
if a, err := idna.ToASCII(host); err == nil {
530
// IPv6 address literal, without a port:
531
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
532
return host + ":" + port
534
return net.JoinHostPort(host, port)
537
var retryBackoffHook func(time.Duration) *time.Timer
539
func backoffNewTimer(d time.Duration) *time.Timer {
540
if retryBackoffHook != nil {
541
return retryBackoffHook(d)
543
return time.NewTimer(d)
546
// RoundTripOpt is like RoundTrip, but takes options.
547
func (t *Transport) RoundTripOpt(req *http.Request, opt RoundTripOpt) (*http.Response, error) {
548
if !(req.URL.Scheme == "https" || (req.URL.Scheme == "http" && t.AllowHTTP)) {
549
return nil, errors.New("http2: unsupported scheme")
552
addr := authorityAddr(req.URL.Scheme, req.URL.Host)
553
for retry := 0; ; retry++ {
554
cc, err := t.connPool().GetClientConn(req, addr)
556
t.vlogf("http2: Transport failed to get client conn for %s: %v", addr, err)
559
reused := !atomic.CompareAndSwapUint32(&cc.reused, 0, 1)
560
traceGotConn(req, cc, reused)
561
res, err := cc.RoundTrip(req)
562
if err != nil && retry <= 6 {
563
if req, err = shouldRetryRequest(req, err); err == nil {
564
// After the first retry, do exponential backoff with 10% jitter.
566
t.vlogf("RoundTrip retrying after failure: %v", err)
569
backoff := float64(uint(1) << (uint(retry) - 1))
570
backoff += backoff * (0.1 * mathrand.Float64())
571
d := time.Second * time.Duration(backoff)
572
timer := backoffNewTimer(d)
575
t.vlogf("RoundTrip retrying after failure: %v", err)
577
case <-req.Context().Done():
579
err = req.Context().Err()
584
t.vlogf("RoundTrip failure: %v", err)
591
// CloseIdleConnections closes any connections which were previously
592
// connected from previous requests but are now sitting idle.
593
// It does not interrupt any connections currently in use.
594
func (t *Transport) CloseIdleConnections() {
595
if cp, ok := t.connPool().(clientConnPoolIdleCloser); ok {
596
cp.closeIdleConnections()
601
errClientConnClosed = errors.New("http2: client conn is closed")
602
errClientConnUnusable = errors.New("http2: client conn not usable")
603
errClientConnGotGoAway = errors.New("http2: Transport received Server's graceful shutdown GOAWAY")
606
// shouldRetryRequest is called by RoundTrip when a request fails to get
607
// response headers. It is always called with a non-nil error.
608
// It returns either a request to retry (either the same request, or a
609
// modified clone), or an error if the request can't be replayed.
610
func shouldRetryRequest(req *http.Request, err error) (*http.Request, error) {
611
if !canRetryError(err) {
614
// If the Body is nil (or http.NoBody), it's safe to reuse
615
// this request and its Body.
616
if req.Body == nil || req.Body == http.NoBody {
620
// If the request body can be reset back to its original
621
// state via the optional req.GetBody, do that.
622
if req.GetBody != nil {
623
body, err := req.GetBody()
632
// The Request.Body can't reset back to the beginning, but we
633
// don't seem to have started to read from it yet, so reuse
634
// the request directly.
635
if err == errClientConnUnusable {
639
return nil, fmt.Errorf("http2: Transport: cannot retry err [%v] after Request.Body was written; define Request.GetBody to avoid this error", err)
642
func canRetryError(err error) bool {
643
if err == errClientConnUnusable || err == errClientConnGotGoAway {
646
if se, ok := err.(StreamError); ok {
647
if se.Code == ErrCodeProtocol && se.Cause == errFromPeer {
648
// See golang/go#47635, golang/go#42777
651
return se.Code == ErrCodeRefusedStream
656
func (t *Transport) dialClientConn(ctx context.Context, addr string, singleUse bool) (*ClientConn, error) {
657
host, _, err := net.SplitHostPort(addr)
661
tconn, err := t.dialTLS(ctx, "tcp", addr, t.newTLSConfig(host))
665
return t.newClientConn(tconn, singleUse)
668
func (t *Transport) newTLSConfig(host string) *tls.Config {
669
cfg := new(tls.Config)
670
if t.TLSClientConfig != nil {
671
*cfg = *t.TLSClientConfig.Clone()
673
if !strSliceContains(cfg.NextProtos, NextProtoTLS) {
674
cfg.NextProtos = append([]string{NextProtoTLS}, cfg.NextProtos...)
676
if cfg.ServerName == "" {
677
cfg.ServerName = host
682
func (t *Transport) dialTLS(ctx context.Context, network, addr string, tlsCfg *tls.Config) (net.Conn, error) {
683
if t.DialTLSContext != nil {
684
return t.DialTLSContext(ctx, network, addr, tlsCfg)
685
} else if t.DialTLS != nil {
686
return t.DialTLS(network, addr, tlsCfg)
689
tlsCn, err := t.dialTLSWithContext(ctx, network, addr, tlsCfg)
693
state := tlsCn.ConnectionState()
694
if p := state.NegotiatedProtocol; p != NextProtoTLS {
695
return nil, fmt.Errorf("http2: unexpected ALPN protocol %q; want %q", p, NextProtoTLS)
697
if !state.NegotiatedProtocolIsMutual {
698
return nil, errors.New("http2: could not negotiate protocol mutually")
703
// disableKeepAlives reports whether connections should be closed as
704
// soon as possible after handling the first request.
705
func (t *Transport) disableKeepAlives() bool {
706
return t.t1 != nil && t.t1.DisableKeepAlives
709
func (t *Transport) expectContinueTimeout() time.Duration {
713
return t.t1.ExpectContinueTimeout
716
func (t *Transport) maxDecoderHeaderTableSize() uint32 {
717
if v := t.MaxDecoderHeaderTableSize; v > 0 {
720
return initialHeaderTableSize
723
func (t *Transport) maxEncoderHeaderTableSize() uint32 {
724
if v := t.MaxEncoderHeaderTableSize; v > 0 {
727
return initialHeaderTableSize
730
func (t *Transport) NewClientConn(c net.Conn) (*ClientConn, error) {
731
return t.newClientConn(c, t.disableKeepAlives())
734
func (t *Transport) newClientConn(c net.Conn, singleUse bool) (*ClientConn, error) {
738
readerDone: make(chan struct{}),
740
maxFrameSize: 16 << 10, // spec default
741
initialWindowSize: 65535, // spec default
742
maxConcurrentStreams: initialMaxConcurrentStreams, // "infinite", per spec. Use a smaller value until we have received server settings.
743
peerMaxHeaderListSize: 0xffffffffffffffff, // "infinite", per spec. Use 2^64-1 instead.
744
streams: make(map[uint32]*clientStream),
745
singleUse: singleUse,
746
wantSettingsAck: true,
747
pings: make(map[[8]byte]chan struct{}),
748
reqHeaderMu: make(chan struct{}, 1),
750
if d := t.idleConnTimeout(); d != 0 {
752
cc.idleTimer = time.AfterFunc(d, cc.onIdleTimeout)
755
t.vlogf("http2: Transport creating client conn %p to %v", cc, c.RemoteAddr())
758
cc.cond = sync.NewCond(&cc.mu)
759
cc.flow.add(int32(initialWindowSize))
761
// TODO: adjust this writer size to account for frame size +
762
// MTU + crypto/tls record padding.
763
cc.bw = bufio.NewWriter(stickyErrWriter{
765
timeout: t.WriteByteTimeout,
768
cc.br = bufio.NewReader(c)
769
cc.fr = NewFramer(cc.bw, cc.br)
770
if t.maxFrameReadSize() != 0 {
771
cc.fr.SetMaxReadFrameSize(t.maxFrameReadSize())
773
if t.CountError != nil {
774
cc.fr.countError = t.CountError
776
maxHeaderTableSize := t.maxDecoderHeaderTableSize()
777
cc.fr.ReadMetaHeaders = hpack.NewDecoder(maxHeaderTableSize, nil)
778
cc.fr.MaxHeaderListSize = t.maxHeaderListSize()
780
cc.henc = hpack.NewEncoder(&cc.hbuf)
781
cc.henc.SetMaxDynamicTableSizeLimit(t.maxEncoderHeaderTableSize())
782
cc.peerMaxHeaderTableSize = initialHeaderTableSize
788
if cs, ok := c.(connectionStater); ok {
789
state := cs.ConnectionState()
793
initialSettings := []Setting{
794
{ID: SettingEnablePush, Val: 0},
795
{ID: SettingInitialWindowSize, Val: transportDefaultStreamFlow},
797
if max := t.maxFrameReadSize(); max != 0 {
798
initialSettings = append(initialSettings, Setting{ID: SettingMaxFrameSize, Val: max})
800
if max := t.maxHeaderListSize(); max != 0 {
801
initialSettings = append(initialSettings, Setting{ID: SettingMaxHeaderListSize, Val: max})
803
if maxHeaderTableSize != initialHeaderTableSize {
804
initialSettings = append(initialSettings, Setting{ID: SettingHeaderTableSize, Val: maxHeaderTableSize})
807
cc.bw.Write(clientPreface)
808
cc.fr.WriteSettings(initialSettings...)
809
cc.fr.WriteWindowUpdate(0, transportDefaultConnFlow)
810
cc.inflow.init(transportDefaultConnFlow + initialWindowSize)
821
func (cc *ClientConn) healthCheck() {
822
pingTimeout := cc.t.pingTimeout()
823
// We don't need to periodically ping in the health check, because the readLoop of ClientConn will
824
// trigger the healthCheck again if there is no frame received.
825
ctx, cancel := context.WithTimeout(context.Background(), pingTimeout)
827
cc.vlogf("http2: Transport sending health check")
830
cc.vlogf("http2: Transport health check failure: %v", err)
831
cc.closeForLostPing()
833
cc.vlogf("http2: Transport health check success")
837
// SetDoNotReuse marks cc as not reusable for future HTTP requests.
838
func (cc *ClientConn) SetDoNotReuse() {
844
func (cc *ClientConn) setGoAway(f *GoAwayFrame) {
851
// Merge the previous and current GoAway error frames.
852
if cc.goAwayDebug == "" {
853
cc.goAwayDebug = string(f.DebugData())
855
if old != nil && old.ErrCode != ErrCodeNo {
856
cc.goAway.ErrCode = old.ErrCode
858
last := f.LastStreamID
859
for streamID, cs := range cc.streams {
861
cs.abortStreamLocked(errClientConnGotGoAway)
866
// CanTakeNewRequest reports whether the connection can take a new request,
867
// meaning it has not been closed or received or sent a GOAWAY.
869
// If the caller is going to immediately make a new request on this
870
// connection, use ReserveNewRequest instead.
871
func (cc *ClientConn) CanTakeNewRequest() bool {
874
return cc.canTakeNewRequestLocked()
877
// ReserveNewRequest is like CanTakeNewRequest but also reserves a
878
// concurrent stream in cc. The reservation is decremented on the
879
// next call to RoundTrip.
880
func (cc *ClientConn) ReserveNewRequest() bool {
883
if st := cc.idleStateLocked(); !st.canTakeNewRequest {
890
// ClientConnState describes the state of a ClientConn.
891
type ClientConnState struct {
892
// Closed is whether the connection is closed.
895
// Closing is whether the connection is in the process of
896
// closing. It may be closing due to shutdown, being a
897
// single-use connection, being marked as DoNotReuse, or
898
// having received a GOAWAY frame.
901
// StreamsActive is how many streams are active.
904
// StreamsReserved is how many streams have been reserved via
905
// ClientConn.ReserveNewRequest.
908
// StreamsPending is how many requests have been sent in excess
909
// of the peer's advertised MaxConcurrentStreams setting and
910
// are waiting for other streams to complete.
913
// MaxConcurrentStreams is how many concurrent streams the
914
// peer advertised as acceptable. Zero means no SETTINGS
915
// frame has been received yet.
916
MaxConcurrentStreams uint32
918
// LastIdle, if non-zero, is when the connection last
919
// transitioned to idle state.
923
// State returns a snapshot of cc's state.
924
func (cc *ClientConn) State() ClientConnState {
926
maxConcurrent := cc.maxConcurrentStreams
927
if !cc.seenSettings {
934
return ClientConnState{
936
Closing: cc.closing || cc.singleUse || cc.doNotReuse || cc.goAway != nil,
937
StreamsActive: len(cc.streams),
938
StreamsReserved: cc.streamsReserved,
939
StreamsPending: cc.pendingRequests,
940
LastIdle: cc.lastIdle,
941
MaxConcurrentStreams: maxConcurrent,
945
// clientConnIdleState describes the suitability of a client
946
// connection to initiate a new RoundTrip request.
947
type clientConnIdleState struct {
948
canTakeNewRequest bool
951
func (cc *ClientConn) idleState() clientConnIdleState {
954
return cc.idleStateLocked()
957
func (cc *ClientConn) idleStateLocked() (st clientConnIdleState) {
958
if cc.singleUse && cc.nextStreamID > 1 {
961
var maxConcurrentOkay bool
962
if cc.t.StrictMaxConcurrentStreams {
963
// We'll tell the caller we can take a new request to
964
// prevent the caller from dialing a new TCP
965
// connection, but then we'll block later before
967
maxConcurrentOkay = true
969
maxConcurrentOkay = int64(len(cc.streams)+cc.streamsReserved+1) <= int64(cc.maxConcurrentStreams)
972
st.canTakeNewRequest = cc.goAway == nil && !cc.closed && !cc.closing && maxConcurrentOkay &&
974
int64(cc.nextStreamID)+2*int64(cc.pendingRequests) < math.MaxInt32 &&
979
func (cc *ClientConn) canTakeNewRequestLocked() bool {
980
st := cc.idleStateLocked()
981
return st.canTakeNewRequest
984
// tooIdleLocked reports whether this connection has been been sitting idle
985
// for too much wall time.
986
func (cc *ClientConn) tooIdleLocked() bool {
987
// The Round(0) strips the monontonic clock reading so the
988
// times are compared based on their wall time. We don't want
989
// to reuse a connection that's been sitting idle during
990
// VM/laptop suspend if monotonic time was also frozen.
991
return cc.idleTimeout != 0 && !cc.lastIdle.IsZero() && time.Since(cc.lastIdle.Round(0)) > cc.idleTimeout
994
// onIdleTimeout is called from a time.AfterFunc goroutine. It will
995
// only be called when we're idle, but because we're coming from a new
996
// goroutine, there could be a new request coming in at the same time,
997
// so this simply calls the synchronized closeIfIdle to shut down this
998
// connection. The timer could just call closeIfIdle, but this is more
1000
func (cc *ClientConn) onIdleTimeout() {
1004
func (cc *ClientConn) closeConn() {
1005
t := time.AfterFunc(250*time.Millisecond, cc.forceCloseConn)
1010
// A tls.Conn.Close can hang for a long time if the peer is unresponsive.
1011
// Try to shut it down more aggressively.
1012
func (cc *ClientConn) forceCloseConn() {
1013
tc, ok := cc.tconn.(*tls.Conn)
1017
if nc := tlsUnderlyingConn(tc); nc != nil {
1022
func (cc *ClientConn) closeIfIdle() {
1024
if len(cc.streams) > 0 || cc.streamsReserved > 0 {
1029
nextID := cc.nextStreamID
1030
// TODO: do clients send GOAWAY too? maybe? Just Close:
1034
cc.vlogf("http2: Transport closing idle conn %p (forSingleUse=%v, maxStream=%v)", cc, cc.singleUse, nextID-2)
1039
func (cc *ClientConn) isDoNotReuseAndIdle() bool {
1041
defer cc.mu.Unlock()
1042
return cc.doNotReuse && len(cc.streams) == 0
1045
var shutdownEnterWaitStateHook = func() {}
1047
// Shutdown gracefully closes the client connection, waiting for running streams to complete.
1048
func (cc *ClientConn) Shutdown(ctx context.Context) error {
1049
if err := cc.sendGoAway(); err != nil {
1052
// Wait for all in-flight streams to complete or connection to close
1053
done := make(chan struct{})
1054
cancelled := false // guarded by cc.mu
1057
defer cc.mu.Unlock()
1059
if len(cc.streams) == 0 || cc.closed {
1070
shutdownEnterWaitStateHook()
1077
// Free the goroutine above
1085
func (cc *ClientConn) sendGoAway() error {
1087
closing := cc.closing
1089
maxStreamID := cc.nextStreamID
1092
// GOAWAY sent already
1097
defer cc.wmu.Unlock()
1098
// Send a graceful shutdown frame to server
1099
if err := cc.fr.WriteGoAway(maxStreamID, ErrCodeNo, nil); err != nil {
1102
if err := cc.bw.Flush(); err != nil {
1105
// Prevent new requests
1109
// closes the client connection immediately. In-flight requests are interrupted.
1110
// err is sent to streams.
1111
func (cc *ClientConn) closeForError(err error) {
1114
for _, cs := range cc.streams {
1115
cs.abortStreamLocked(err)
1122
// Close closes the client connection immediately.
1124
// In-flight requests are interrupted. For a graceful shutdown, use Shutdown instead.
1125
func (cc *ClientConn) Close() error {
1126
err := errors.New("http2: client connection force closed via ClientConn.Close")
1127
cc.closeForError(err)
1131
// closes the client connection immediately. In-flight requests are interrupted.
1132
func (cc *ClientConn) closeForLostPing() {
1133
err := errors.New("http2: client connection lost")
1134
if f := cc.t.CountError; f != nil {
1135
f("conn_close_lost_ping")
1137
cc.closeForError(err)
1140
// errRequestCanceled is a copy of net/http's errRequestCanceled because it's not
1141
// exported. At least they'll be DeepEqual for h1-vs-h2 comparisons tests.
1142
var errRequestCanceled = errors.New("net/http: request canceled")
1144
func commaSeparatedTrailers(req *http.Request) (string, error) {
1145
keys := make([]string, 0, len(req.Trailer))
1146
for k := range req.Trailer {
1147
k = canonicalHeader(k)
1149
case "Transfer-Encoding", "Trailer", "Content-Length":
1150
return "", fmt.Errorf("invalid Trailer key %q", k)
1152
keys = append(keys, k)
1156
return strings.Join(keys, ","), nil
1161
func (cc *ClientConn) responseHeaderTimeout() time.Duration {
1163
return cc.t.t1.ResponseHeaderTimeout
1165
// No way to do this (yet?) with just an http2.Transport. Probably
1166
// no need. Request.Cancel this is the new way. We only need to support
1167
// this for compatibility with the old http.Transport fields when
1168
// we're doing transparent http2.
1172
// checkConnHeaders checks whether req has any invalid connection-level headers.
1173
// per RFC 7540 section 8.1.2.2: Connection-Specific Header Fields.
1174
// Certain headers are special-cased as okay but not transmitted later.
1175
func checkConnHeaders(req *http.Request) error {
1176
if v := req.Header.Get("Upgrade"); v != "" {
1177
return fmt.Errorf("http2: invalid Upgrade request header: %q", req.Header["Upgrade"])
1179
if vv := req.Header["Transfer-Encoding"]; len(vv) > 0 && (len(vv) > 1 || vv[0] != "" && vv[0] != "chunked") {
1180
return fmt.Errorf("http2: invalid Transfer-Encoding request header: %q", vv)
1182
if vv := req.Header["Connection"]; len(vv) > 0 && (len(vv) > 1 || vv[0] != "" && !asciiEqualFold(vv[0], "close") && !asciiEqualFold(vv[0], "keep-alive")) {
1183
return fmt.Errorf("http2: invalid Connection request header: %q", vv)
1188
// actualContentLength returns a sanitized version of
1189
// req.ContentLength, where 0 actually means zero (not unknown) and -1
1191
func actualContentLength(req *http.Request) int64 {
1192
if req.Body == nil || req.Body == http.NoBody {
1195
if req.ContentLength != 0 {
1196
return req.ContentLength
1201
func (cc *ClientConn) decrStreamReservations() {
1203
defer cc.mu.Unlock()
1204
cc.decrStreamReservationsLocked()
1207
func (cc *ClientConn) decrStreamReservationsLocked() {
1208
if cc.streamsReserved > 0 {
1209
cc.streamsReserved--
1213
func (cc *ClientConn) RoundTrip(req *http.Request) (*http.Response, error) {
1214
ctx := req.Context()
1215
cs := &clientStream{
1218
reqCancel: req.Cancel,
1219
isHead: req.Method == "HEAD",
1221
reqBodyContentLength: actualContentLength(req),
1222
trace: httptrace.ContextClientTrace(ctx),
1223
peerClosed: make(chan struct{}),
1224
abort: make(chan struct{}),
1225
respHeaderRecv: make(chan struct{}),
1226
donec: make(chan struct{}),
1228
go cs.doRequest(req)
1230
waitDone := func() error {
1236
case <-cs.reqCancel:
1237
return errRequestCanceled
1241
handleResponseHeaders := func() (*http.Response, error) {
1243
if res.StatusCode > 299 {
1244
// On error or status code 3xx, 4xx, 5xx, etc abort any
1245
// ongoing write, assuming that the server doesn't care
1246
// about our request body. If the server replied with 1xx or
1247
// 2xx, however, then assume the server DOES potentially
1248
// want our body (e.g. full-duplex streaming:
1249
// golang.org/issue/13444). If it turns out the server
1250
// doesn't, they'll RST_STREAM us soon enough. This is a
1251
// heuristic to avoid adding knobs to Transport. Hopefully
1253
cs.abortRequestBodyWrite()
1256
res.TLS = cc.tlsState
1257
if res.Body == noBody && actualContentLength(req) == 0 {
1258
// If there isn't a request or response body still being
1259
// written, then wait for the stream to be closed before
1260
// RoundTrip returns.
1261
if err := waitDone(); err != nil {
1270
case <-cs.respHeaderRecv:
1271
return handleResponseHeaders()
1274
case <-cs.respHeaderRecv:
1275
// If both cs.respHeaderRecv and cs.abort are signaling,
1276
// pick respHeaderRecv. The server probably wrote the
1277
// response and immediately reset the stream.
1278
// golang.org/issue/49645
1279
return handleResponseHeaders()
1282
return nil, cs.abortErr
1288
case <-cs.reqCancel:
1289
cs.abortStream(errRequestCanceled)
1290
return nil, errRequestCanceled
1295
// doRequest runs for the duration of the request lifetime.
1297
// It sends the request and performs post-request cleanup (closing Request.Body, etc.).
1298
func (cs *clientStream) doRequest(req *http.Request) {
1299
err := cs.writeRequest(req)
1300
cs.cleanupWriteRequest(err)
1303
// writeRequest sends a request.
1305
// It returns nil after the request is written, the response read,
1306
// and the request stream is half-closed by the peer.
1308
// It returns non-nil if the request ends otherwise.
1309
// If the returned error is StreamError, the error Code may be used in resetting the stream.
1310
func (cs *clientStream) writeRequest(req *http.Request) (err error) {
1314
if err := checkConnHeaders(req); err != nil {
1318
// Acquire the new-request lock by writing to reqHeaderMu.
1319
// This lock guards the critical section covering allocating a new stream ID
1320
// (requires mu) and creating the stream (requires wmu).
1321
if cc.reqHeaderMu == nil {
1322
panic("RoundTrip on uninitialized ClientConn") // for tests
1325
case cc.reqHeaderMu <- struct{}{}:
1326
case <-cs.reqCancel:
1327
return errRequestCanceled
1333
if cc.idleTimer != nil {
1336
cc.decrStreamReservationsLocked()
1337
if err := cc.awaitOpenSlotForStreamLocked(cs); err != nil {
1342
cc.addStreamLocked(cs) // assigns stream ID
1343
if isConnectionCloseRequest(req) {
1344
cc.doNotReuse = true
1348
// TODO(bradfitz): this is a copy of the logic in net/http. Unify somewhere?
1349
if !cc.t.disableCompression() &&
1350
req.Header.Get("Accept-Encoding") == "" &&
1351
req.Header.Get("Range") == "" &&
1353
// Request gzip only, not deflate. Deflate is ambiguous and
1354
// not as universally supported anyway.
1355
// See: https://zlib.net/zlib_faq.html#faq39
1357
// Note that we don't request this for HEAD requests,
1358
// due to a bug in nginx:
1359
// http://trac.nginx.org/nginx/ticket/358
1360
// https://golang.org/issue/5522
1362
// We don't request gzip if the request is for a range, since
1363
// auto-decoding a portion of a gzipped document will just fail
1364
// anyway. See https://golang.org/issue/8923
1365
cs.requestedGzip = true
1368
continueTimeout := cc.t.expectContinueTimeout()
1369
if continueTimeout != 0 {
1370
if !httpguts.HeaderValuesContainsToken(req.Header["Expect"], "100-continue") {
1373
cs.on100 = make(chan struct{}, 1)
1377
// Past this point (where we send request headers), it is possible for
1378
// RoundTrip to return successfully. Since the RoundTrip contract permits
1379
// the caller to "mutate or reuse" the Request after closing the Response's Body,
1380
// we must take care when referencing the Request from here on.
1381
err = cs.encodeAndWriteHeaders(req)
1387
hasBody := cs.reqBodyContentLength != 0
1389
cs.sentEndStream = true
1391
if continueTimeout != 0 {
1392
traceWait100Continue(cs.trace)
1393
timer := time.NewTimer(continueTimeout)
1403
case <-cs.reqCancel:
1404
err = errRequestCanceled
1408
traceWroteRequest(cs.trace, err)
1413
if err = cs.writeRequestBody(req); err != nil {
1414
if err != errStopReqBodyWrite {
1415
traceWroteRequest(cs.trace, err)
1419
cs.sentEndStream = true
1423
traceWroteRequest(cs.trace, err)
1425
var respHeaderTimer <-chan time.Time
1426
var respHeaderRecv chan struct{}
1427
if d := cc.responseHeaderTimeout(); d != 0 {
1428
timer := time.NewTimer(d)
1430
respHeaderTimer = timer.C
1431
respHeaderRecv = cs.respHeaderRecv
1433
// Wait until the peer half-closes its end of the stream,
1434
// or until the request is aborted (via context, error, or otherwise),
1435
// whichever comes first.
1438
case <-cs.peerClosed:
1440
case <-respHeaderTimer:
1442
case <-respHeaderRecv:
1443
respHeaderRecv = nil
1444
respHeaderTimer = nil // keep waiting for END_STREAM
1449
case <-cs.reqCancel:
1450
return errRequestCanceled
1455
func (cs *clientStream) encodeAndWriteHeaders(req *http.Request) error {
1460
defer cc.wmu.Unlock()
1462
// If the request was canceled while waiting for cc.mu, just quit.
1468
case <-cs.reqCancel:
1469
return errRequestCanceled
1475
// we send: HEADERS{1}, CONTINUATION{0,} + DATA{0,} (DATA is
1476
// sent by writeRequestBody below, along with any Trailers,
1477
// again in form HEADERS{1}, CONTINUATION{0,})
1478
trailers, err := commaSeparatedTrailers(req)
1482
hasTrailers := trailers != ""
1483
contentLen := actualContentLength(req)
1484
hasBody := contentLen != 0
1485
hdrs, err := cc.encodeHeaders(req, cs.requestedGzip, trailers, contentLen)
1490
// Write the request.
1491
endStream := !hasBody && !hasTrailers
1492
cs.sentHeaders = true
1493
err = cc.writeHeaders(cs.ID, endStream, int(cc.maxFrameSize), hdrs)
1494
traceWroteHeaders(cs.trace)
1498
// cleanupWriteRequest performs post-request tasks.
1500
// If err (the result of writeRequest) is non-nil and the stream is not closed,
1501
// cleanupWriteRequest will send a reset to the peer.
1502
func (cs *clientStream) cleanupWriteRequest(err error) {
1506
// We were canceled before creating the stream, so return our reservation.
1507
cc.decrStreamReservations()
1510
// TODO: write h12Compare test showing whether
1511
// Request.Body is closed by the Transport,
1512
// and in multiple cases: server replies <=299 and >299
1513
// while still writing request body
1515
mustCloseBody := false
1516
if cs.reqBody != nil && cs.reqBodyClosed == nil {
1517
mustCloseBody = true
1518
cs.reqBodyClosed = make(chan struct{})
1520
bodyClosed := cs.reqBodyClosed
1526
if bodyClosed != nil {
1530
if err != nil && cs.sentEndStream {
1531
// If the connection is closed immediately after the response is read,
1532
// we may be aborted before finishing up here. If the stream was closed
1533
// cleanly on both sides, there is no error.
1535
case <-cs.peerClosed:
1541
cs.abortStream(err) // possibly redundant, but harmless
1543
if se, ok := err.(StreamError); ok {
1544
if se.Cause != errFromPeer {
1545
cc.writeStreamReset(cs.ID, se.Code, err)
1548
cc.writeStreamReset(cs.ID, ErrCodeCancel, err)
1551
cs.bufPipe.CloseWithError(err) // no-op if already closed
1553
if cs.sentHeaders && !cs.sentEndStream {
1554
cc.writeStreamReset(cs.ID, ErrCodeNo, nil)
1556
cs.bufPipe.CloseWithError(errRequestCanceled)
1559
cc.forgetStreamID(cs.ID)
1572
// awaitOpenSlotForStreamLocked waits until len(streams) < maxConcurrentStreams.
1574
func (cc *ClientConn) awaitOpenSlotForStreamLocked(cs *clientStream) error {
1576
cc.lastActive = time.Now()
1577
if cc.closed || !cc.canTakeNewRequestLocked() {
1578
return errClientConnUnusable
1580
cc.lastIdle = time.Time{}
1581
if int64(len(cc.streams)) < int64(cc.maxConcurrentStreams) {
1584
cc.pendingRequests++
1586
cc.pendingRequests--
1595
// requires cc.wmu be held
1596
func (cc *ClientConn) writeHeaders(streamID uint32, endStream bool, maxFrameSize int, hdrs []byte) error {
1597
first := true // first frame written (HEADERS is first, then CONTINUATION)
1598
for len(hdrs) > 0 && cc.werr == nil {
1600
if len(chunk) > maxFrameSize {
1601
chunk = chunk[:maxFrameSize]
1603
hdrs = hdrs[len(chunk):]
1604
endHeaders := len(hdrs) == 0
1606
cc.fr.WriteHeaders(HeadersFrameParam{
1608
BlockFragment: chunk,
1609
EndStream: endStream,
1610
EndHeaders: endHeaders,
1614
cc.fr.WriteContinuation(streamID, endHeaders, chunk)
1621
// internal error values; they don't escape to callers
1623
// abort request body write; don't send cancel
1624
errStopReqBodyWrite = errors.New("http2: aborting request body write")
1626
// abort request body write, but send stream reset of cancel.
1627
errStopReqBodyWriteAndCancel = errors.New("http2: canceling request")
1629
errReqBodyTooLong = errors.New("http2: request body larger than specified content length")
1632
// frameScratchBufferLen returns the length of a buffer to use for
1633
// outgoing request bodies to read/write to/from.
1635
// It returns max(1, min(peer's advertised max frame size,
1636
// Request.ContentLength+1, 512KB)).
1637
func (cs *clientStream) frameScratchBufferLen(maxFrameSize int) int {
1638
const max = 512 << 10
1639
n := int64(maxFrameSize)
1643
if cl := cs.reqBodyContentLength; cl != -1 && cl+1 < n {
1644
// Add an extra byte past the declared content-length to
1645
// give the caller's Request.Body io.Reader a chance to
1646
// give us more bytes than they declared, so we can catch it
1653
return int(n) // doesn't truncate; max is 512K
1656
var bufPool sync.Pool // of *[]byte
1658
func (cs *clientStream) writeRequestBody(req *http.Request) (err error) {
1661
sentEnd := false // whether we sent the final DATA frame w/ END_STREAM
1663
hasTrailers := req.Trailer != nil
1664
remainLen := cs.reqBodyContentLength
1665
hasContentLen := remainLen != -1
1668
maxFrameSize := int(cc.maxFrameSize)
1671
// Scratch buffer for reading into & writing from.
1672
scratchLen := cs.frameScratchBufferLen(maxFrameSize)
1674
if bp, ok := bufPool.Get().(*[]byte); ok && len(*bp) >= scratchLen {
1675
defer bufPool.Put(bp)
1678
buf = make([]byte, scratchLen)
1679
defer bufPool.Put(&buf)
1684
n, err := body.Read(buf)
1686
remainLen -= int64(n)
1687
if remainLen == 0 && err == nil {
1688
// The request body's Content-Length was predeclared and
1689
// we just finished reading it all, but the underlying io.Reader
1690
// returned the final chunk with a nil error (which is one of
1691
// the two valid things a Reader can do at EOF). Because we'd prefer
1692
// to send the END_STREAM bit early, double-check that we're actually
1693
// at EOF. Subsequent reads should return (0, EOF) at this point.
1694
// If either value is different, we return an error in one of two ways below.
1697
n1, err = body.Read(scratch[:])
1698
remainLen -= int64(n1)
1701
err = errReqBodyTooLong
1707
bodyClosed := cs.reqBodyClosed != nil
1711
return errStopReqBodyWrite
1721
for len(remain) > 0 && err == nil {
1723
allowed, err = cs.awaitFlowControl(len(remain))
1728
data := remain[:allowed]
1729
remain = remain[allowed:]
1730
sentEnd = sawEOF && len(remain) == 0 && !hasTrailers
1731
err = cc.fr.WriteData(cs.ID, sentEnd, data)
1733
// TODO(bradfitz): this flush is for latency, not bandwidth.
1734
// Most requests won't need this. Make this opt-in or
1735
// opt-out? Use some heuristic on the body type? Nagel-like
1736
// timers? Based on 'n'? Only last chunk of this for loop,
1737
// unless flow control tokens are low? For now, always.
1738
// If we change this, see comment below.
1749
// Already sent END_STREAM (which implies we have no
1750
// trailers) and flushed, because currently all
1751
// WriteData frames above get a flush. So we're done.
1755
// Since the RoundTrip contract permits the caller to "mutate or reuse"
1756
// a request after the Response's Body is closed, verify that this hasn't
1757
// happened before accessing the trailers.
1759
trailer := req.Trailer
1767
defer cc.wmu.Unlock()
1769
if len(trailer) > 0 {
1770
trls, err = cc.encodeTrailers(trailer)
1776
// Two ways to send END_STREAM: either with trailers, or
1777
// with an empty DATA frame.
1779
err = cc.writeHeaders(cs.ID, true, maxFrameSize, trls)
1781
err = cc.fr.WriteData(cs.ID, true, nil)
1783
if ferr := cc.bw.Flush(); ferr != nil && err == nil {
1789
// awaitFlowControl waits for [1, min(maxBytes, cc.cs.maxFrameSize)] flow
1790
// control tokens from the server.
1791
// It returns either the non-zero number of tokens taken or an error
1792
// if the stream is dead.
1793
func (cs *clientStream) awaitFlowControl(maxBytes int) (taken int32, err error) {
1797
defer cc.mu.Unlock()
1800
return 0, errClientConnClosed
1802
if cs.reqBodyClosed != nil {
1803
return 0, errStopReqBodyWrite
1807
return 0, cs.abortErr
1810
case <-cs.reqCancel:
1811
return 0, errRequestCanceled
1814
if a := cs.flow.available(); a > 0 {
1816
if int(take) > maxBytes {
1818
take = int32(maxBytes) // can't truncate int; take is int32
1820
if take > int32(cc.maxFrameSize) {
1821
take = int32(cc.maxFrameSize)
1830
var errNilRequestURL = errors.New("http2: Request.URI is nil")
1832
// requires cc.wmu be held.
1833
func (cc *ClientConn) encodeHeaders(req *http.Request, addGzipHeader bool, trailers string, contentLength int64) ([]byte, error) {
1836
return nil, errNilRequestURL
1843
host, err := httpguts.PunycodeHostPort(host)
1849
if req.Method != "CONNECT" {
1850
path = req.URL.RequestURI()
1851
if !validPseudoPath(path) {
1853
path = strings.TrimPrefix(path, req.URL.Scheme+"://"+host)
1854
if !validPseudoPath(path) {
1855
if req.URL.Opaque != "" {
1856
return nil, fmt.Errorf("invalid request :path %q from URL.Opaque = %q", orig, req.URL.Opaque)
1858
return nil, fmt.Errorf("invalid request :path %q", orig)
1864
// Check for any invalid headers and return an error before we
1865
// potentially pollute our hpack state. (We want to be able to
1866
// continue to reuse the hpack encoder for future requests)
1867
for k, vv := range req.Header {
1868
if !httpguts.ValidHeaderFieldName(k) {
1869
return nil, fmt.Errorf("invalid HTTP header name %q", k)
1871
for _, v := range vv {
1872
if !httpguts.ValidHeaderFieldValue(v) {
1873
// Don't include the value in the error, because it may be sensitive.
1874
return nil, fmt.Errorf("invalid HTTP header value for header %q", k)
1879
enumerateHeaders := func(f func(name, value string)) {
1880
// 8.1.2.3 Request Pseudo-Header Fields
1881
// The :path pseudo-header field includes the path and query parts of the
1882
// target URI (the path-absolute production and optionally a '?' character
1883
// followed by the query production (see Sections 3.3 and 3.4 of
1885
f(":authority", host)
1891
if req.Method != "CONNECT" {
1893
f(":scheme", req.URL.Scheme)
1896
f("trailer", trailers)
1900
for k, vv := range req.Header {
1901
if asciiEqualFold(k, "host") || asciiEqualFold(k, "content-length") {
1902
// Host is :authority, already sent.
1903
// Content-Length is automatic, set below.
1905
} else if asciiEqualFold(k, "connection") ||
1906
asciiEqualFold(k, "proxy-connection") ||
1907
asciiEqualFold(k, "transfer-encoding") ||
1908
asciiEqualFold(k, "upgrade") ||
1909
asciiEqualFold(k, "keep-alive") {
1910
// Per 8.1.2.2 Connection-Specific Header
1911
// Fields, don't send connection-specific
1912
// fields. We have already checked if any
1913
// are error-worthy so just ignore the rest.
1915
} else if asciiEqualFold(k, "user-agent") {
1916
// Match Go's http1 behavior: at most one
1917
// User-Agent. If set to nil or empty string,
1918
// then omit it. Otherwise if not mentioned,
1919
// include the default (below).
1928
} else if asciiEqualFold(k, "cookie") {
1929
// Per 8.1.2.5 To allow for better compression efficiency, the
1930
// Cookie header field MAY be split into separate header fields,
1931
// each with one or more cookie-pairs.
1932
for _, v := range vv {
1934
p := strings.IndexByte(v, ';')
1940
// strip space after semicolon if any.
1941
for p+1 <= len(v) && v[p] == ' ' {
1953
for _, v := range vv {
1957
if shouldSendReqContentLength(req.Method, contentLength) {
1958
f("content-length", strconv.FormatInt(contentLength, 10))
1961
f("accept-encoding", "gzip")
1964
f("user-agent", defaultUserAgent)
1968
// Do a first pass over the headers counting bytes to ensure
1969
// we don't exceed cc.peerMaxHeaderListSize. This is done as a
1970
// separate pass before encoding the headers to prevent
1971
// modifying the hpack state.
1973
enumerateHeaders(func(name, value string) {
1974
hf := hpack.HeaderField{Name: name, Value: value}
1975
hlSize += uint64(hf.Size())
1978
if hlSize > cc.peerMaxHeaderListSize {
1979
return nil, errRequestHeaderListSize
1982
trace := httptrace.ContextClientTrace(req.Context())
1983
traceHeaders := traceHasWroteHeaderField(trace)
1985
// Header list size is ok. Write the headers.
1986
enumerateHeaders(func(name, value string) {
1987
name, ascii := lowerHeader(name)
1989
// Skip writing invalid headers. Per RFC 7540, Section 8.1.2, header
1990
// field names have to be ASCII characters (just as in HTTP/1.x).
1993
cc.writeHeader(name, value)
1995
traceWroteHeaderField(trace, name, value)
1999
return cc.hbuf.Bytes(), nil
2002
// shouldSendReqContentLength reports whether the http2.Transport should send
2003
// a "content-length" request header. This logic is basically a copy of the net/http
2004
// transferWriter.shouldSendContentLength.
2005
// The contentLength is the corrected contentLength (so 0 means actually 0, not unknown).
2007
func shouldSendReqContentLength(method string, contentLength int64) bool {
2008
if contentLength > 0 {
2011
if contentLength < 0 {
2014
// For zero bodies, whether we send a content-length depends on the method.
2015
// It also kinda doesn't matter for http2 either way, with END_STREAM.
2017
case "POST", "PUT", "PATCH":
2024
// requires cc.wmu be held.
2025
func (cc *ClientConn) encodeTrailers(trailer http.Header) ([]byte, error) {
2029
for k, vv := range trailer {
2030
for _, v := range vv {
2031
hf := hpack.HeaderField{Name: k, Value: v}
2032
hlSize += uint64(hf.Size())
2035
if hlSize > cc.peerMaxHeaderListSize {
2036
return nil, errRequestHeaderListSize
2039
for k, vv := range trailer {
2040
lowKey, ascii := lowerHeader(k)
2042
// Skip writing invalid headers. Per RFC 7540, Section 8.1.2, header
2043
// field names have to be ASCII characters (just as in HTTP/1.x).
2046
// Transfer-Encoding, etc.. have already been filtered at the
2047
// start of RoundTrip
2048
for _, v := range vv {
2049
cc.writeHeader(lowKey, v)
2052
return cc.hbuf.Bytes(), nil
2055
func (cc *ClientConn) writeHeader(name, value string) {
2057
log.Printf("http2: Transport encoding header %q = %q", name, value)
2059
cc.henc.WriteField(hpack.HeaderField{Name: name, Value: value})
2062
type resAndError struct {
2068
// requires cc.mu be held.
2069
func (cc *ClientConn) addStreamLocked(cs *clientStream) {
2070
cs.flow.add(int32(cc.initialWindowSize))
2071
cs.flow.setConnFlow(&cc.flow)
2072
cs.inflow.init(transportDefaultStreamFlow)
2073
cs.ID = cc.nextStreamID
2074
cc.nextStreamID += 2
2075
cc.streams[cs.ID] = cs
2077
panic("assigned stream ID 0")
2081
func (cc *ClientConn) forgetStreamID(id uint32) {
2083
slen := len(cc.streams)
2084
delete(cc.streams, id)
2085
if len(cc.streams) != slen-1 {
2086
panic("forgetting unknown stream id")
2088
cc.lastActive = time.Now()
2089
if len(cc.streams) == 0 && cc.idleTimer != nil {
2090
cc.idleTimer.Reset(cc.idleTimeout)
2091
cc.lastIdle = time.Now()
2093
// Wake up writeRequestBody via clientStream.awaitFlowControl and
2094
// wake up RoundTrip if there is a pending request.
2097
closeOnIdle := cc.singleUse || cc.doNotReuse || cc.t.disableKeepAlives() || cc.goAway != nil
2098
if closeOnIdle && cc.streamsReserved == 0 && len(cc.streams) == 0 {
2100
cc.vlogf("http2: Transport closing idle conn %p (forSingleUse=%v, maxStream=%v)", cc, cc.singleUse, cc.nextStreamID-2)
2103
defer cc.closeConn()
2109
// clientConnReadLoop is the state owned by the clientConn's frame-reading readLoop.
2110
type clientConnReadLoop struct {
2115
// readLoop runs in its own goroutine and reads and dispatches frames.
2116
func (cc *ClientConn) readLoop() {
2117
rl := &clientConnReadLoop{cc: cc}
2119
cc.readerErr = rl.run()
2120
if ce, ok := cc.readerErr.(ConnectionError); ok {
2122
cc.fr.WriteGoAway(0, ErrCode(ce), nil)
2127
// GoAwayError is returned by the Transport when the server closes the
2128
// TCP connection after sending a GOAWAY frame.
2129
type GoAwayError struct {
2135
func (e GoAwayError) Error() string {
2136
return fmt.Sprintf("http2: server sent GOAWAY and closed the connection; LastStreamID=%v, ErrCode=%v, debug=%q",
2137
e.LastStreamID, e.ErrCode, e.DebugData)
2140
func isEOFOrNetReadError(err error) bool {
2144
ne, ok := err.(*net.OpError)
2145
return ok && ne.Op == "read"
2148
func (rl *clientConnReadLoop) cleanup() {
2150
cc.t.connPool().MarkDead(cc)
2151
defer cc.closeConn()
2152
defer close(cc.readerDone)
2154
if cc.idleTimer != nil {
2158
// Close any response bodies if the server closes prematurely.
2159
// TODO: also do this if we've written the headers but not
2160
// gotten a response yet.
2163
if cc.goAway != nil && isEOFOrNetReadError(err) {
2165
LastStreamID: cc.goAway.LastStreamID,
2166
ErrCode: cc.goAway.ErrCode,
2167
DebugData: cc.goAwayDebug,
2169
} else if err == io.EOF {
2170
err = io.ErrUnexpectedEOF
2174
for _, cs := range cc.streams {
2176
case <-cs.peerClosed:
2177
// The server closed the stream before closing the conn,
2178
// so no need to interrupt it.
2180
cs.abortStreamLocked(err)
2187
// countReadFrameError calls Transport.CountError with a string
2189
func (cc *ClientConn) countReadFrameError(err error) {
2190
f := cc.t.CountError
2191
if f == nil || err == nil {
2194
if ce, ok := err.(ConnectionError); ok {
2195
errCode := ErrCode(ce)
2196
f(fmt.Sprintf("read_frame_conn_error_%s", errCode.stringToken()))
2199
if errors.Is(err, io.EOF) {
2203
if errors.Is(err, io.ErrUnexpectedEOF) {
2204
f("read_frame_unexpected_eof")
2207
if errors.Is(err, ErrFrameTooLarge) {
2208
f("read_frame_too_large")
2211
f("read_frame_other")
2214
func (rl *clientConnReadLoop) run() error {
2216
gotSettings := false
2217
readIdleTimeout := cc.t.ReadIdleTimeout
2219
if readIdleTimeout != 0 {
2220
t = time.AfterFunc(readIdleTimeout, cc.healthCheck)
2224
f, err := cc.fr.ReadFrame()
2226
t.Reset(readIdleTimeout)
2229
cc.vlogf("http2: Transport readFrame error on conn %p: (%T) %v", cc, err, err)
2231
if se, ok := err.(StreamError); ok {
2232
if cs := rl.streamByID(se.StreamID); cs != nil {
2233
if se.Cause == nil {
2234
se.Cause = cc.fr.errDetail
2236
rl.endStreamError(cs, se)
2239
} else if err != nil {
2240
cc.countReadFrameError(err)
2244
cc.vlogf("http2: Transport received %s", summarizeFrame(f))
2247
if _, ok := f.(*SettingsFrame); !ok {
2248
cc.logf("protocol error: received %T before a SETTINGS frame", f)
2249
return ConnectionError(ErrCodeProtocol)
2254
switch f := f.(type) {
2255
case *MetaHeadersFrame:
2256
err = rl.processHeaders(f)
2258
err = rl.processData(f)
2260
err = rl.processGoAway(f)
2261
case *RSTStreamFrame:
2262
err = rl.processResetStream(f)
2263
case *SettingsFrame:
2264
err = rl.processSettings(f)
2265
case *PushPromiseFrame:
2266
err = rl.processPushPromise(f)
2267
case *WindowUpdateFrame:
2268
err = rl.processWindowUpdate(f)
2270
err = rl.processPing(f)
2272
cc.logf("Transport: unhandled response frame type %T", f)
2276
cc.vlogf("http2: Transport conn %p received error from processing frame %v: %v", cc, summarizeFrame(f), err)
2283
func (rl *clientConnReadLoop) processHeaders(f *MetaHeadersFrame) error {
2284
cs := rl.streamByID(f.StreamID)
2286
// We'd get here if we canceled a request while the
2287
// server had its response still in flight. So if this
2288
// was just something we canceled, ignore it.
2292
rl.endStreamError(cs, StreamError{
2293
StreamID: f.StreamID,
2294
Code: ErrCodeProtocol,
2295
Cause: errors.New("protocol error: headers after END_STREAM"),
2300
if cs.trace != nil {
2301
// TODO(bradfitz): move first response byte earlier,
2302
// when we first read the 9 byte header, not waiting
2303
// until all the HEADERS+CONTINUATION frames have been
2304
// merged. This works for now.
2305
traceFirstResponseByte(cs.trace)
2309
if !cs.pastHeaders {
2310
cs.pastHeaders = true
2312
return rl.processTrailers(cs, f)
2315
res, err := rl.handleResponse(cs, f)
2317
if _, ok := err.(ConnectionError); ok {
2320
// Any other error type is a stream error.
2321
rl.endStreamError(cs, StreamError{
2322
StreamID: f.StreamID,
2323
Code: ErrCodeProtocol,
2326
return nil // return nil from process* funcs to keep conn alive
2329
// (nil, nil) special case. See handleResponse docs.
2332
cs.resTrailer = &res.Trailer
2334
close(cs.respHeaderRecv)
2335
if f.StreamEnded() {
2341
// may return error types nil, or ConnectionError. Any other error value
2342
// is a StreamError of type ErrCodeProtocol. The returned error in that case
2345
// As a special case, handleResponse may return (nil, nil) to skip the
2346
// frame (currently only used for 1xx responses).
2347
func (rl *clientConnReadLoop) handleResponse(cs *clientStream, f *MetaHeadersFrame) (*http.Response, error) {
2349
return nil, errResponseHeaderListSize
2352
status := f.PseudoValue("status")
2354
return nil, errors.New("malformed response from server: missing status pseudo header")
2356
statusCode, err := strconv.Atoi(status)
2358
return nil, errors.New("malformed response from server: malformed non-numeric status pseudo header")
2361
regularFields := f.RegularFields()
2362
strs := make([]string, len(regularFields))
2363
header := make(http.Header, len(regularFields))
2364
res := &http.Response{
2368
StatusCode: statusCode,
2369
Status: status + " " + http.StatusText(statusCode),
2371
for _, hf := range regularFields {
2372
key := canonicalHeader(hf.Name)
2373
if key == "Trailer" {
2376
t = make(http.Header)
2379
foreachHeaderElement(hf.Value, func(v string) {
2380
t[canonicalHeader(v)] = nil
2384
if vv == nil && len(strs) > 0 {
2385
// More than likely this will be a single-element key.
2386
// Most headers aren't multi-valued.
2387
// Set the capacity on strs[0] to 1, so any future append
2388
// won't extend the slice into the other strings.
2389
vv, strs = strs[:1:1], strs[1:]
2393
header[key] = append(vv, hf.Value)
2398
if statusCode >= 100 && statusCode <= 199 {
2399
if f.StreamEnded() {
2400
return nil, errors.New("1xx informational response with END_STREAM flag")
2403
const max1xxResponses = 5 // arbitrary bound on number of informational responses, same as net/http
2404
if cs.num1xx > max1xxResponses {
2405
return nil, errors.New("http2: too many 1xx informational responses")
2407
if fn := cs.get1xxTraceFunc(); fn != nil {
2408
if err := fn(statusCode, textproto.MIMEHeader(header)); err != nil {
2412
if statusCode == 100 {
2413
traceGot100Continue(cs.trace)
2415
case cs.on100 <- struct{}{}:
2419
cs.pastHeaders = false // do it all again
2423
res.ContentLength = -1
2424
if clens := res.Header["Content-Length"]; len(clens) == 1 {
2425
if cl, err := strconv.ParseUint(clens[0], 10, 63); err == nil {
2426
res.ContentLength = int64(cl)
2428
// TODO: care? unlike http/1, it won't mess up our framing, so it's
2429
// more safe smuggling-wise to ignore.
2431
} else if len(clens) > 1 {
2432
// TODO: care? unlike http/1, it won't mess up our framing, so it's
2433
// more safe smuggling-wise to ignore.
2434
} else if f.StreamEnded() && !cs.isHead {
2435
res.ContentLength = 0
2443
if f.StreamEnded() {
2444
if res.ContentLength > 0 {
2445
res.Body = missingBody{}
2452
cs.bufPipe.setBuffer(&dataBuffer{expected: res.ContentLength})
2453
cs.bytesRemain = res.ContentLength
2454
res.Body = transportResponseBody{cs}
2456
if cs.requestedGzip && asciiEqualFold(res.Header.Get("Content-Encoding"), "gzip") {
2457
res.Header.Del("Content-Encoding")
2458
res.Header.Del("Content-Length")
2459
res.ContentLength = -1
2460
res.Body = &gzipReader{body: res.Body}
2461
res.Uncompressed = true
2466
func (rl *clientConnReadLoop) processTrailers(cs *clientStream, f *MetaHeadersFrame) error {
2467
if cs.pastTrailers {
2468
// Too many HEADERS frames for this stream.
2469
return ConnectionError(ErrCodeProtocol)
2471
cs.pastTrailers = true
2472
if !f.StreamEnded() {
2473
// We expect that any headers for trailers also
2475
return ConnectionError(ErrCodeProtocol)
2477
if len(f.PseudoFields()) > 0 {
2478
// No pseudo header fields are defined for trailers.
2479
// TODO: ConnectionError might be overly harsh? Check.
2480
return ConnectionError(ErrCodeProtocol)
2483
trailer := make(http.Header)
2484
for _, hf := range f.RegularFields() {
2485
key := canonicalHeader(hf.Name)
2486
trailer[key] = append(trailer[key], hf.Value)
2488
cs.trailer = trailer
2494
// transportResponseBody is the concrete type of Transport.RoundTrip's
2495
// Response.Body. It is an io.ReadCloser.
2496
type transportResponseBody struct {
2500
func (b transportResponseBody) Read(p []byte) (n int, err error) {
2504
if cs.readErr != nil {
2505
return 0, cs.readErr
2507
n, err = b.cs.bufPipe.Read(p)
2508
if cs.bytesRemain != -1 {
2509
if int64(n) > cs.bytesRemain {
2510
n = int(cs.bytesRemain)
2512
err = errors.New("net/http: server replied with more than declared Content-Length; truncated")
2516
return int(cs.bytesRemain), err
2518
cs.bytesRemain -= int64(n)
2519
if err == io.EOF && cs.bytesRemain > 0 {
2520
err = io.ErrUnexpectedEOF
2526
// No flow control tokens to send back.
2531
connAdd := cc.inflow.add(n)
2533
if err == nil { // No need to refresh if the stream is over or failed.
2534
streamAdd = cs.inflow.add(n)
2538
if connAdd != 0 || streamAdd != 0 {
2540
defer cc.wmu.Unlock()
2542
cc.fr.WriteWindowUpdate(0, mustUint31(connAdd))
2545
cc.fr.WriteWindowUpdate(cs.ID, mustUint31(streamAdd))
2552
var errClosedResponseBody = errors.New("http2: response body closed")
2554
func (b transportResponseBody) Close() error {
2558
unread := cs.bufPipe.Len()
2561
// Return connection-level flow control.
2562
connAdd := cc.inflow.add(unread)
2565
// TODO(dneil): Acquiring this mutex can block indefinitely.
2566
// Move flow control return to a goroutine?
2568
// Return connection-level flow control.
2570
cc.fr.WriteWindowUpdate(0, uint32(connAdd))
2576
cs.bufPipe.BreakWithError(errClosedResponseBody)
2577
cs.abortStream(errClosedResponseBody)
2581
case <-cs.ctx.Done():
2582
// See golang/go#49366: The net/http package can cancel the
2583
// request context after the response body is fully read.
2584
// Don't treat this as an error.
2586
case <-cs.reqCancel:
2587
return errRequestCanceled
2592
func (rl *clientConnReadLoop) processData(f *DataFrame) error {
2594
cs := rl.streamByID(f.StreamID)
2598
neverSent := cc.nextStreamID
2600
if f.StreamID >= neverSent {
2601
// We never asked for this.
2602
cc.logf("http2: Transport received unsolicited DATA frame; closing connection")
2603
return ConnectionError(ErrCodeProtocol)
2605
// We probably did ask for this, but canceled. Just ignore it.
2606
// TODO: be stricter here? only silently ignore things which
2607
// we canceled, but not things which were closed normally
2608
// by the peer? Tough without accumulating too much state.
2610
// But at least return their flow control:
2613
ok := cc.inflow.take(f.Length)
2614
connAdd := cc.inflow.add(int(f.Length))
2617
return ConnectionError(ErrCodeFlowControl)
2621
cc.fr.WriteWindowUpdate(0, uint32(connAdd))
2629
cc.logf("protocol error: received DATA after END_STREAM")
2630
rl.endStreamError(cs, StreamError{
2631
StreamID: f.StreamID,
2632
Code: ErrCodeProtocol,
2637
cc.logf("protocol error: received DATA before a HEADERS frame")
2638
rl.endStreamError(cs, StreamError{
2639
StreamID: f.StreamID,
2640
Code: ErrCodeProtocol,
2645
if cs.isHead && len(data) > 0 {
2646
cc.logf("protocol error: received DATA on a HEAD request")
2647
rl.endStreamError(cs, StreamError{
2648
StreamID: f.StreamID,
2649
Code: ErrCodeProtocol,
2653
// Check connection-level flow control.
2655
if !takeInflows(&cc.inflow, &cs.inflow, f.Length) {
2657
return ConnectionError(ErrCodeFlowControl)
2659
// Return any padded flow control now, since we won't
2660
// refund it later on body reads.
2662
if pad := int(f.Length) - len(data); pad > 0 {
2669
if _, err = cs.bufPipe.Write(data); err != nil {
2670
// Return len(data) now if the stream is already closed,
2671
// since data will never be read.
2677
sendConn := cc.inflow.add(refund)
2678
var sendStream int32
2680
sendStream = cs.inflow.add(refund)
2684
if sendConn > 0 || sendStream > 0 {
2687
cc.fr.WriteWindowUpdate(0, uint32(sendConn))
2690
cc.fr.WriteWindowUpdate(cs.ID, uint32(sendStream))
2697
rl.endStreamError(cs, err)
2702
if f.StreamEnded() {
2708
func (rl *clientConnReadLoop) endStream(cs *clientStream) {
2709
// TODO: check that any declared content-length matches, like
2710
// server.go's (*stream).endStream method.
2712
cs.readClosed = true
2713
// Close cs.bufPipe and cs.peerClosed with cc.mu held to avoid a
2714
// race condition: The caller can read io.EOF from Response.Body
2715
// and close the body before we close cs.peerClosed, causing
2716
// cleanupWriteRequest to send a RST_STREAM.
2718
defer rl.cc.mu.Unlock()
2719
cs.bufPipe.closeWithErrorAndCode(io.EOF, cs.copyTrailers)
2720
close(cs.peerClosed)
2724
func (rl *clientConnReadLoop) endStreamError(cs *clientStream, err error) {
2725
cs.readAborted = true
2729
func (rl *clientConnReadLoop) streamByID(id uint32) *clientStream {
2731
defer rl.cc.mu.Unlock()
2732
cs := rl.cc.streams[id]
2733
if cs != nil && !cs.readAborted {
2739
func (cs *clientStream) copyTrailers() {
2740
for k, vv := range cs.trailer {
2743
*t = make(http.Header)
2749
func (rl *clientConnReadLoop) processGoAway(f *GoAwayFrame) error {
2751
cc.t.connPool().MarkDead(cc)
2753
// TODO: deal with GOAWAY more. particularly the error code
2754
cc.vlogf("transport got GOAWAY with error code = %v", f.ErrCode)
2755
if fn := cc.t.CountError; fn != nil {
2756
fn("recv_goaway_" + f.ErrCode.stringToken())
2763
func (rl *clientConnReadLoop) processSettings(f *SettingsFrame) error {
2765
// Locking both mu and wmu here allows frame encoding to read settings with only wmu held.
2766
// Acquiring wmu when f.IsAck() is unnecessary, but convenient and mostly harmless.
2768
defer cc.wmu.Unlock()
2770
if err := rl.processSettingsNoWrite(f); err != nil {
2774
cc.fr.WriteSettingsAck()
2780
func (rl *clientConnReadLoop) processSettingsNoWrite(f *SettingsFrame) error {
2783
defer cc.mu.Unlock()
2786
if cc.wantSettingsAck {
2787
cc.wantSettingsAck = false
2790
return ConnectionError(ErrCodeProtocol)
2793
var seenMaxConcurrentStreams bool
2794
err := f.ForeachSetting(func(s Setting) error {
2796
case SettingMaxFrameSize:
2797
cc.maxFrameSize = s.Val
2798
case SettingMaxConcurrentStreams:
2799
cc.maxConcurrentStreams = s.Val
2800
seenMaxConcurrentStreams = true
2801
case SettingMaxHeaderListSize:
2802
cc.peerMaxHeaderListSize = uint64(s.Val)
2803
case SettingInitialWindowSize:
2804
// Values above the maximum flow-control
2805
// window size of 2^31-1 MUST be treated as a
2806
// connection error (Section 5.4.1) of type
2807
// FLOW_CONTROL_ERROR.
2808
if s.Val > math.MaxInt32 {
2809
return ConnectionError(ErrCodeFlowControl)
2812
// Adjust flow control of currently-open
2813
// frames by the difference of the old initial
2814
// window size and this one.
2815
delta := int32(s.Val) - int32(cc.initialWindowSize)
2816
for _, cs := range cc.streams {
2821
cc.initialWindowSize = s.Val
2822
case SettingHeaderTableSize:
2823
cc.henc.SetMaxDynamicTableSize(s.Val)
2824
cc.peerMaxHeaderTableSize = s.Val
2826
cc.vlogf("Unhandled Setting: %v", s)
2834
if !cc.seenSettings {
2835
if !seenMaxConcurrentStreams {
2836
// This was the servers initial SETTINGS frame and it
2837
// didn't contain a MAX_CONCURRENT_STREAMS field so
2838
// increase the number of concurrent streams this
2839
// connection can establish to our default.
2840
cc.maxConcurrentStreams = defaultMaxConcurrentStreams
2842
cc.seenSettings = true
2848
func (rl *clientConnReadLoop) processWindowUpdate(f *WindowUpdateFrame) error {
2850
cs := rl.streamByID(f.StreamID)
2851
if f.StreamID != 0 && cs == nil {
2856
defer cc.mu.Unlock()
2862
if !fl.add(int32(f.Increment)) {
2863
return ConnectionError(ErrCodeFlowControl)
2869
func (rl *clientConnReadLoop) processResetStream(f *RSTStreamFrame) error {
2870
cs := rl.streamByID(f.StreamID)
2872
// TODO: return error if server tries to RST_STREAM an idle stream
2875
serr := streamError(cs.ID, f.ErrCode)
2876
serr.Cause = errFromPeer
2877
if f.ErrCode == ErrCodeProtocol {
2878
rl.cc.SetDoNotReuse()
2880
if fn := cs.cc.t.CountError; fn != nil {
2881
fn("recv_rststream_" + f.ErrCode.stringToken())
2883
cs.abortStream(serr)
2885
cs.bufPipe.CloseWithError(serr)
2889
// Ping sends a PING frame to the server and waits for the ack.
2890
func (cc *ClientConn) Ping(ctx context.Context) error {
2891
c := make(chan struct{})
2892
// Generate a random payload
2895
if _, err := rand.Read(p[:]); err != nil {
2899
// check for dup before insert
2900
if _, found := cc.pings[p]; !found {
2907
errc := make(chan error, 1)
2910
defer cc.wmu.Unlock()
2911
if err := cc.fr.WritePing(false, p); err != nil {
2915
if err := cc.bw.Flush(); err != nil {
2927
case <-cc.readerDone:
2928
// connection closed
2933
func (rl *clientConnReadLoop) processPing(f *PingFrame) error {
2937
defer cc.mu.Unlock()
2938
// If ack, notify listener if any
2939
if c, ok := cc.pings[f.Data]; ok {
2941
delete(cc.pings, f.Data)
2947
defer cc.wmu.Unlock()
2948
if err := cc.fr.WritePing(true, f.Data); err != nil {
2951
return cc.bw.Flush()
2954
func (rl *clientConnReadLoop) processPushPromise(f *PushPromiseFrame) error {
2955
// We told the peer we don't want them.
2957
// "PUSH_PROMISE MUST NOT be sent if the SETTINGS_ENABLE_PUSH
2958
// setting of the peer endpoint is set to 0. An endpoint that
2959
// has set this setting and has received acknowledgement MUST
2960
// treat the receipt of a PUSH_PROMISE frame as a connection
2961
// error (Section 5.4.1) of type PROTOCOL_ERROR."
2962
return ConnectionError(ErrCodeProtocol)
2965
func (cc *ClientConn) writeStreamReset(streamID uint32, code ErrCode, err error) {
2966
// TODO: map err to more interesting error codes, once the
2967
// HTTP community comes up with some. But currently for
2968
// RST_STREAM there's no equivalent to GOAWAY frame's debug
2969
// data, and the error codes are all pretty vague ("cancel").
2971
cc.fr.WriteRSTStream(streamID, code)
2977
errResponseHeaderListSize = errors.New("http2: response header list larger than advertised limit")
2978
errRequestHeaderListSize = errors.New("http2: request header list larger than peer's advertised limit")
2981
func (cc *ClientConn) logf(format string, args ...interface{}) {
2982
cc.t.logf(format, args...)
2985
func (cc *ClientConn) vlogf(format string, args ...interface{}) {
2986
cc.t.vlogf(format, args...)
2989
func (t *Transport) vlogf(format string, args ...interface{}) {
2991
t.logf(format, args...)
2995
func (t *Transport) logf(format string, args ...interface{}) {
2996
log.Printf(format, args...)
2999
var noBody io.ReadCloser = noBodyReader{}
3001
type noBodyReader struct{}
3003
func (noBodyReader) Close() error { return nil }
3004
func (noBodyReader) Read([]byte) (int, error) { return 0, io.EOF }
3006
type missingBody struct{}
3008
func (missingBody) Close() error { return nil }
3009
func (missingBody) Read([]byte) (int, error) { return 0, io.ErrUnexpectedEOF }
3011
func strSliceContains(ss []string, s string) bool {
3012
for _, v := range ss {
3020
type erringRoundTripper struct{ err error }
3022
func (rt erringRoundTripper) RoundTripErr() error { return rt.err }
3023
func (rt erringRoundTripper) RoundTrip(*http.Request) (*http.Response, error) { return nil, rt.err }
3025
// gzipReader wraps a response body so it can lazily
3026
// call gzip.NewReader on the first call to Read
3027
type gzipReader struct {
3029
body io.ReadCloser // underlying Response.Body
3030
zr *gzip.Reader // lazily-initialized gzip reader
3031
zerr error // sticky error
3034
func (gz *gzipReader) Read(p []byte) (n int, err error) {
3039
gz.zr, err = gzip.NewReader(gz.body)
3045
return gz.zr.Read(p)
3048
func (gz *gzipReader) Close() error {
3049
if err := gz.body.Close(); err != nil {
3052
gz.zerr = fs.ErrClosed
3056
type errorReader struct{ err error }
3058
func (r errorReader) Read(p []byte) (int, error) { return 0, r.err }
3060
// isConnectionCloseRequest reports whether req should use its own
3061
// connection for a single request and then close the connection.
3062
func isConnectionCloseRequest(req *http.Request) bool {
3063
return req.Close || httpguts.HeaderValuesContainsToken(req.Header["Connection"], "close")
3066
// registerHTTPSProtocol calls Transport.RegisterProtocol but
3067
// converting panics into errors.
3068
func registerHTTPSProtocol(t *http.Transport, rt noDialH2RoundTripper) (err error) {
3070
if e := recover(); e != nil {
3071
err = fmt.Errorf("%v", e)
3074
t.RegisterProtocol("https", rt)
3078
// noDialH2RoundTripper is a RoundTripper which only tries to complete the request
3079
// if there's already has a cached connection to the host.
3080
// (The field is exported so it can be accessed via reflect from net/http; tested
3081
// by TestNoDialH2RoundTripperType)
3082
type noDialH2RoundTripper struct{ *Transport }
3084
func (rt noDialH2RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
3085
res, err := rt.Transport.RoundTrip(req)
3086
if isNoCachedConnError(err) {
3087
return nil, http.ErrSkipAltProtocol
3092
func (t *Transport) idleConnTimeout() time.Duration {
3094
return t.t1.IdleConnTimeout
3099
func traceGetConn(req *http.Request, hostPort string) {
3100
trace := httptrace.ContextClientTrace(req.Context())
3101
if trace == nil || trace.GetConn == nil {
3104
trace.GetConn(hostPort)
3107
func traceGotConn(req *http.Request, cc *ClientConn, reused bool) {
3108
trace := httptrace.ContextClientTrace(req.Context())
3109
if trace == nil || trace.GotConn == nil {
3112
ci := httptrace.GotConnInfo{Conn: cc.tconn}
3115
ci.WasIdle = len(cc.streams) == 0 && reused
3116
if ci.WasIdle && !cc.lastActive.IsZero() {
3117
ci.IdleTime = time.Since(cc.lastActive)
3124
func traceWroteHeaders(trace *httptrace.ClientTrace) {
3125
if trace != nil && trace.WroteHeaders != nil {
3126
trace.WroteHeaders()
3130
func traceGot100Continue(trace *httptrace.ClientTrace) {
3131
if trace != nil && trace.Got100Continue != nil {
3132
trace.Got100Continue()
3136
func traceWait100Continue(trace *httptrace.ClientTrace) {
3137
if trace != nil && trace.Wait100Continue != nil {
3138
trace.Wait100Continue()
3142
func traceWroteRequest(trace *httptrace.ClientTrace, err error) {
3143
if trace != nil && trace.WroteRequest != nil {
3144
trace.WroteRequest(httptrace.WroteRequestInfo{Err: err})
3148
func traceFirstResponseByte(trace *httptrace.ClientTrace) {
3149
if trace != nil && trace.GotFirstResponseByte != nil {
3150
trace.GotFirstResponseByte()