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