github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/net/http/transport.go (about) 1 // Copyright 2011 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 // HTTP client implementation. See RFC 7230 through 7235. 6 // 7 // This is the low-level Transport implementation of RoundTripper. 8 // The high-level interface is in client.go. 9 10 package http 11 12 import ( 13 "bufio" 14 "compress/gzip" 15 "container/list" 16 "context" 17 "crypto/tls" 18 "errors" 19 "fmt" 20 "io" 21 "log" 22 "net" 23 "net/http/httptrace" 24 "net/textproto" 25 "net/url" 26 "os" 27 "reflect" 28 "strings" 29 "sync" 30 "sync/atomic" 31 "time" 32 33 "golang.org/x/net/http/httpguts" 34 "golang.org/x/net/http/httpproxy" 35 ) 36 37 // DefaultTransport is the default implementation of Transport and is 38 // used by DefaultClient. It establishes network connections as needed 39 // and caches them for reuse by subsequent calls. It uses HTTP proxies 40 // as directed by the $HTTP_PROXY and $NO_PROXY (or $http_proxy and 41 // $no_proxy) environment variables. 42 var DefaultTransport RoundTripper = &Transport{ 43 Proxy: ProxyFromEnvironment, 44 DialContext: (&net.Dialer{ 45 Timeout: 30 * time.Second, 46 KeepAlive: 30 * time.Second, 47 }).DialContext, 48 ForceAttemptHTTP2: true, 49 MaxIdleConns: 100, 50 IdleConnTimeout: 90 * time.Second, 51 TLSHandshakeTimeout: 10 * time.Second, 52 ExpectContinueTimeout: 1 * time.Second, 53 } 54 55 // DefaultMaxIdleConnsPerHost is the default value of Transport's 56 // MaxIdleConnsPerHost. 57 const DefaultMaxIdleConnsPerHost = 2 58 59 // Transport is an implementation of RoundTripper that supports HTTP, 60 // HTTPS, and HTTP proxies (for either HTTP or HTTPS with CONNECT). 61 // 62 // By default, Transport caches connections for future re-use. 63 // This may leave many open connections when accessing many hosts. 64 // This behavior can be managed using Transport's CloseIdleConnections method 65 // and the MaxIdleConnsPerHost and DisableKeepAlives fields. 66 // 67 // Transports should be reused instead of created as needed. 68 // Transports are safe for concurrent use by multiple goroutines. 69 // 70 // A Transport is a low-level primitive for making HTTP and HTTPS requests. 71 // For high-level functionality, such as cookies and redirects, see Client. 72 // 73 // Transport uses HTTP/1.1 for HTTP URLs and either HTTP/1.1 or HTTP/2 74 // for HTTPS URLs, depending on whether the server supports HTTP/2, 75 // and how the Transport is configured. The DefaultTransport supports HTTP/2. 76 // To explicitly enable HTTP/2 on a transport, use golang.org/x/net/http2 77 // and call ConfigureTransport. See the package docs for more about HTTP/2. 78 // 79 // Responses with status codes in the 1xx range are either handled 80 // automatically (100 expect-continue) or ignored. The one 81 // exception is HTTP status code 101 (Switching Protocols), which is 82 // considered a terminal status and returned by RoundTrip. To see the 83 // ignored 1xx responses, use the httptrace trace package's 84 // ClientTrace.Got1xxResponse. 85 // 86 // Transport only retries a request upon encountering a network error 87 // if the request is idempotent and either has no body or has its 88 // Request.GetBody defined. HTTP requests are considered idempotent if 89 // they have HTTP methods GET, HEAD, OPTIONS, or TRACE; or if their 90 // Header map contains an "Idempotency-Key" or "X-Idempotency-Key" 91 // entry. If the idempotency key value is a zero-length slice, the 92 // request is treated as idempotent but the header is not sent on the 93 // wire. 94 type Transport struct { 95 idleMu sync.Mutex 96 closeIdle bool // user has requested to close all idle conns 97 idleConn map[connectMethodKey][]*persistConn // most recently used at end 98 idleConnWait map[connectMethodKey]wantConnQueue // waiting getConns 99 idleLRU connLRU 100 101 reqMu sync.Mutex 102 reqCanceler map[cancelKey]func(error) 103 104 altMu sync.Mutex // guards changing altProto only 105 altProto atomic.Value // of nil or map[string]RoundTripper, key is URI scheme 106 107 connsPerHostMu sync.Mutex 108 connsPerHost map[connectMethodKey]int 109 connsPerHostWait map[connectMethodKey]wantConnQueue // waiting getConns 110 111 // Proxy specifies a function to return a proxy for a given 112 // Request. If the function returns a non-nil error, the 113 // request is aborted with the provided error. 114 // 115 // The proxy type is determined by the URL scheme. "http", 116 // "https", and "socks5" are supported. If the scheme is empty, 117 // "http" is assumed. 118 // 119 // If Proxy is nil or returns a nil *URL, no proxy is used. 120 Proxy func(*Request) (*url.URL, error) 121 122 // DialContext specifies the dial function for creating unencrypted TCP connections. 123 // If DialContext is nil (and the deprecated Dial below is also nil), 124 // then the transport dials using package net. 125 // 126 // DialContext runs concurrently with calls to RoundTrip. 127 // A RoundTrip call that initiates a dial may end up using 128 // a connection dialed previously when the earlier connection 129 // becomes idle before the later DialContext completes. 130 DialContext func(ctx context.Context, network, addr string) (net.Conn, error) 131 132 // Dial specifies the dial function for creating unencrypted TCP connections. 133 // 134 // Dial runs concurrently with calls to RoundTrip. 135 // A RoundTrip call that initiates a dial may end up using 136 // a connection dialed previously when the earlier connection 137 // becomes idle before the later Dial completes. 138 // 139 // Deprecated: Use DialContext instead, which allows the transport 140 // to cancel dials as soon as they are no longer needed. 141 // If both are set, DialContext takes priority. 142 Dial func(network, addr string) (net.Conn, error) 143 144 // DialTLSContext specifies an optional dial function for creating 145 // TLS connections for non-proxied HTTPS requests. 146 // 147 // If DialTLSContext is nil (and the deprecated DialTLS below is also nil), 148 // DialContext and TLSClientConfig are used. 149 // 150 // If DialTLSContext is set, the Dial and DialContext hooks are not used for HTTPS 151 // requests and the TLSClientConfig and TLSHandshakeTimeout 152 // are ignored. The returned net.Conn is assumed to already be 153 // past the TLS handshake. 154 DialTLSContext func(ctx context.Context, network, addr string) (net.Conn, error) 155 156 // DialTLS specifies an optional dial function for creating 157 // TLS connections for non-proxied HTTPS requests. 158 // 159 // Deprecated: Use DialTLSContext instead, which allows the transport 160 // to cancel dials as soon as they are no longer needed. 161 // If both are set, DialTLSContext takes priority. 162 DialTLS func(network, addr string) (net.Conn, error) 163 164 // TLSClientConfig specifies the TLS configuration to use with 165 // tls.Client. 166 // If nil, the default configuration is used. 167 // If non-nil, HTTP/2 support may not be enabled by default. 168 TLSClientConfig *tls.Config 169 170 // TLSHandshakeTimeout specifies the maximum amount of time waiting to 171 // wait for a TLS handshake. Zero means no timeout. 172 TLSHandshakeTimeout time.Duration 173 174 // DisableKeepAlives, if true, disables HTTP keep-alives and 175 // will only use the connection to the server for a single 176 // HTTP request. 177 // 178 // This is unrelated to the similarly named TCP keep-alives. 179 DisableKeepAlives bool 180 181 // DisableCompression, if true, prevents the Transport from 182 // requesting compression with an "Accept-Encoding: gzip" 183 // request header when the Request contains no existing 184 // Accept-Encoding value. If the Transport requests gzip on 185 // its own and gets a gzipped response, it's transparently 186 // decoded in the Response.Body. However, if the user 187 // explicitly requested gzip it is not automatically 188 // uncompressed. 189 DisableCompression bool 190 191 // MaxIdleConns controls the maximum number of idle (keep-alive) 192 // connections across all hosts. Zero means no limit. 193 MaxIdleConns int 194 195 // MaxIdleConnsPerHost, if non-zero, controls the maximum idle 196 // (keep-alive) connections to keep per-host. If zero, 197 // DefaultMaxIdleConnsPerHost is used. 198 MaxIdleConnsPerHost int 199 200 // MaxConnsPerHost optionally limits the total number of 201 // connections per host, including connections in the dialing, 202 // active, and idle states. On limit violation, dials will block. 203 // 204 // Zero means no limit. 205 MaxConnsPerHost int 206 207 // IdleConnTimeout is the maximum amount of time an idle 208 // (keep-alive) connection will remain idle before closing 209 // itself. 210 // Zero means no limit. 211 IdleConnTimeout time.Duration 212 213 // ResponseHeaderTimeout, if non-zero, specifies the amount of 214 // time to wait for a server's response headers after fully 215 // writing the request (including its body, if any). This 216 // time does not include the time to read the response body. 217 ResponseHeaderTimeout time.Duration 218 219 // ExpectContinueTimeout, if non-zero, specifies the amount of 220 // time to wait for a server's first response headers after fully 221 // writing the request headers if the request has an 222 // "Expect: 100-continue" header. Zero means no timeout and 223 // causes the body to be sent immediately, without 224 // waiting for the server to approve. 225 // This time does not include the time to send the request header. 226 ExpectContinueTimeout time.Duration 227 228 // TLSNextProto specifies how the Transport switches to an 229 // alternate protocol (such as HTTP/2) after a TLS ALPN 230 // protocol negotiation. If Transport dials an TLS connection 231 // with a non-empty protocol name and TLSNextProto contains a 232 // map entry for that key (such as "h2"), then the func is 233 // called with the request's authority (such as "example.com" 234 // or "example.com:1234") and the TLS connection. The function 235 // must return a RoundTripper that then handles the request. 236 // If TLSNextProto is not nil, HTTP/2 support is not enabled 237 // automatically. 238 TLSNextProto map[string]func(authority string, c *tls.Conn) RoundTripper 239 240 // ProxyConnectHeader optionally specifies headers to send to 241 // proxies during CONNECT requests. 242 // To set the header dynamically, see GetProxyConnectHeader. 243 ProxyConnectHeader Header 244 245 // GetProxyConnectHeader optionally specifies a func to return 246 // headers to send to proxyURL during a CONNECT request to the 247 // ip:port target. 248 // If it returns an error, the Transport's RoundTrip fails with 249 // that error. It can return (nil, nil) to not add headers. 250 // If GetProxyConnectHeader is non-nil, ProxyConnectHeader is 251 // ignored. 252 GetProxyConnectHeader func(ctx context.Context, proxyURL *url.URL, target string) (Header, error) 253 254 // MaxResponseHeaderBytes specifies a limit on how many 255 // response bytes are allowed in the server's response 256 // header. 257 // 258 // Zero means to use a default limit. 259 MaxResponseHeaderBytes int64 260 261 // WriteBufferSize specifies the size of the write buffer used 262 // when writing to the transport. 263 // If zero, a default (currently 4KB) is used. 264 WriteBufferSize int 265 266 // ReadBufferSize specifies the size of the read buffer used 267 // when reading from the transport. 268 // If zero, a default (currently 4KB) is used. 269 ReadBufferSize int 270 271 // nextProtoOnce guards initialization of TLSNextProto and 272 // h2transport (via onceSetNextProtoDefaults) 273 nextProtoOnce sync.Once 274 h2transport h2Transport // non-nil if http2 wired up 275 tlsNextProtoWasNil bool // whether TLSNextProto was nil when the Once fired 276 277 // ForceAttemptHTTP2 controls whether HTTP/2 is enabled when a non-zero 278 // Dial, DialTLS, or DialContext func or TLSClientConfig is provided. 279 // By default, use of any those fields conservatively disables HTTP/2. 280 // To use a custom dialer or TLS config and still attempt HTTP/2 281 // upgrades, set this to true. 282 ForceAttemptHTTP2 bool 283 } 284 285 // A cancelKey is the key of the reqCanceler map. 286 // We wrap the *Request in this type since we want to use the original request, 287 // not any transient one created by roundTrip. 288 type cancelKey struct { 289 req *Request 290 } 291 292 func (t *Transport) writeBufferSize() int { 293 if t.WriteBufferSize > 0 { 294 return t.WriteBufferSize 295 } 296 return 4 << 10 297 } 298 299 func (t *Transport) readBufferSize() int { 300 if t.ReadBufferSize > 0 { 301 return t.ReadBufferSize 302 } 303 return 4 << 10 304 } 305 306 // Clone returns a deep copy of t's exported fields. 307 func (t *Transport) Clone() *Transport { 308 t.nextProtoOnce.Do(t.onceSetNextProtoDefaults) 309 t2 := &Transport{ 310 Proxy: t.Proxy, 311 DialContext: t.DialContext, 312 Dial: t.Dial, 313 DialTLS: t.DialTLS, 314 DialTLSContext: t.DialTLSContext, 315 TLSHandshakeTimeout: t.TLSHandshakeTimeout, 316 DisableKeepAlives: t.DisableKeepAlives, 317 DisableCompression: t.DisableCompression, 318 MaxIdleConns: t.MaxIdleConns, 319 MaxIdleConnsPerHost: t.MaxIdleConnsPerHost, 320 MaxConnsPerHost: t.MaxConnsPerHost, 321 IdleConnTimeout: t.IdleConnTimeout, 322 ResponseHeaderTimeout: t.ResponseHeaderTimeout, 323 ExpectContinueTimeout: t.ExpectContinueTimeout, 324 ProxyConnectHeader: t.ProxyConnectHeader.Clone(), 325 GetProxyConnectHeader: t.GetProxyConnectHeader, 326 MaxResponseHeaderBytes: t.MaxResponseHeaderBytes, 327 ForceAttemptHTTP2: t.ForceAttemptHTTP2, 328 WriteBufferSize: t.WriteBufferSize, 329 ReadBufferSize: t.ReadBufferSize, 330 } 331 if t.TLSClientConfig != nil { 332 t2.TLSClientConfig = t.TLSClientConfig.Clone() 333 } 334 if !t.tlsNextProtoWasNil { 335 npm := map[string]func(authority string, c *tls.Conn) RoundTripper{} 336 for k, v := range t.TLSNextProto { 337 npm[k] = v 338 } 339 t2.TLSNextProto = npm 340 } 341 return t2 342 } 343 344 // h2Transport is the interface we expect to be able to call from 345 // net/http against an *http2.Transport that's either bundled into 346 // h2_bundle.go or supplied by the user via x/net/http2. 347 // 348 // We name it with the "h2" prefix to stay out of the "http2" prefix 349 // namespace used by x/tools/cmd/bundle for h2_bundle.go. 350 type h2Transport interface { 351 CloseIdleConnections() 352 } 353 354 func (t *Transport) hasCustomTLSDialer() bool { 355 return t.DialTLS != nil || t.DialTLSContext != nil 356 } 357 358 // onceSetNextProtoDefaults initializes TLSNextProto. 359 // It must be called via t.nextProtoOnce.Do. 360 func (t *Transport) onceSetNextProtoDefaults() { 361 t.tlsNextProtoWasNil = (t.TLSNextProto == nil) 362 if strings.Contains(os.Getenv("GODEBUG"), "http2client=0") { 363 return 364 } 365 366 // If they've already configured http2 with 367 // golang.org/x/net/http2 instead of the bundled copy, try to 368 // get at its http2.Transport value (via the "https" 369 // altproto map) so we can call CloseIdleConnections on it if 370 // requested. (Issue 22891) 371 altProto, _ := t.altProto.Load().(map[string]RoundTripper) 372 if rv := reflect.ValueOf(altProto["https"]); rv.IsValid() && rv.Type().Kind() == reflect.Struct && rv.Type().NumField() == 1 { 373 if v := rv.Field(0); v.CanInterface() { 374 if h2i, ok := v.Interface().(h2Transport); ok { 375 t.h2transport = h2i 376 return 377 } 378 } 379 } 380 381 if t.TLSNextProto != nil { 382 // This is the documented way to disable http2 on a 383 // Transport. 384 return 385 } 386 if !t.ForceAttemptHTTP2 && (t.TLSClientConfig != nil || t.Dial != nil || t.DialContext != nil || t.hasCustomTLSDialer()) { 387 // Be conservative and don't automatically enable 388 // http2 if they've specified a custom TLS config or 389 // custom dialers. Let them opt-in themselves via 390 // http2.ConfigureTransport so we don't surprise them 391 // by modifying their tls.Config. Issue 14275. 392 // However, if ForceAttemptHTTP2 is true, it overrides the above checks. 393 return 394 } 395 if omitBundledHTTP2 { 396 return 397 } 398 t2, err := http2configureTransports(t) 399 if err != nil { 400 log.Printf("Error enabling Transport HTTP/2 support: %v", err) 401 return 402 } 403 t.h2transport = t2 404 405 // Auto-configure the http2.Transport's MaxHeaderListSize from 406 // the http.Transport's MaxResponseHeaderBytes. They don't 407 // exactly mean the same thing, but they're close. 408 // 409 // TODO: also add this to x/net/http2.Configure Transport, behind 410 // a +build go1.7 build tag: 411 if limit1 := t.MaxResponseHeaderBytes; limit1 != 0 && t2.MaxHeaderListSize == 0 { 412 const h2max = 1<<32 - 1 413 if limit1 >= h2max { 414 t2.MaxHeaderListSize = h2max 415 } else { 416 t2.MaxHeaderListSize = uint32(limit1) 417 } 418 } 419 } 420 421 // ProxyFromEnvironment returns the URL of the proxy to use for a 422 // given request, as indicated by the environment variables 423 // HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lowercase versions 424 // thereof). HTTPS_PROXY takes precedence over HTTP_PROXY for https 425 // requests. 426 // 427 // The environment values may be either a complete URL or a 428 // "host[:port]", in which case the "http" scheme is assumed. 429 // An error is returned if the value is a different form. 430 // 431 // A nil URL and nil error are returned if no proxy is defined in the 432 // environment, or a proxy should not be used for the given request, 433 // as defined by NO_PROXY. 434 // 435 // As a special case, if req.URL.Host is "localhost" (with or without 436 // a port number), then a nil URL and nil error will be returned. 437 func ProxyFromEnvironment(req *Request) (*url.URL, error) { 438 return envProxyFunc()(req.URL) 439 } 440 441 // ProxyURL returns a proxy function (for use in a Transport) 442 // that always returns the same URL. 443 func ProxyURL(fixedURL *url.URL) func(*Request) (*url.URL, error) { 444 return func(*Request) (*url.URL, error) { 445 return fixedURL, nil 446 } 447 } 448 449 // transportRequest is a wrapper around a *Request that adds 450 // optional extra headers to write and stores any error to return 451 // from roundTrip. 452 type transportRequest struct { 453 *Request // original request, not to be mutated 454 extra Header // extra headers to write, or nil 455 trace *httptrace.ClientTrace // optional 456 cancelKey cancelKey 457 458 mu sync.Mutex // guards err 459 err error // first setError value for mapRoundTripError to consider 460 } 461 462 func (tr *transportRequest) extraHeaders() Header { 463 if tr.extra == nil { 464 tr.extra = make(Header) 465 } 466 return tr.extra 467 } 468 469 func (tr *transportRequest) setError(err error) { 470 tr.mu.Lock() 471 if tr.err == nil { 472 tr.err = err 473 } 474 tr.mu.Unlock() 475 } 476 477 // useRegisteredProtocol reports whether an alternate protocol (as registered 478 // with Transport.RegisterProtocol) should be respected for this request. 479 func (t *Transport) useRegisteredProtocol(req *Request) bool { 480 if req.URL.Scheme == "https" && req.requiresHTTP1() { 481 // If this request requires HTTP/1, don't use the 482 // "https" alternate protocol, which is used by the 483 // HTTP/2 code to take over requests if there's an 484 // existing cached HTTP/2 connection. 485 return false 486 } 487 return true 488 } 489 490 // alternateRoundTripper returns the alternate RoundTripper to use 491 // for this request if the Request's URL scheme requires one, 492 // or nil for the normal case of using the Transport. 493 func (t *Transport) alternateRoundTripper(req *Request) RoundTripper { 494 if !t.useRegisteredProtocol(req) { 495 return nil 496 } 497 altProto, _ := t.altProto.Load().(map[string]RoundTripper) 498 return altProto[req.URL.Scheme] 499 } 500 501 // roundTrip implements a RoundTripper over HTTP. 502 func (t *Transport) roundTrip(req *Request) (*Response, error) { 503 t.nextProtoOnce.Do(t.onceSetNextProtoDefaults) 504 ctx := req.Context() 505 trace := httptrace.ContextClientTrace(ctx) 506 507 if req.URL == nil { 508 req.closeBody() 509 return nil, errors.New("http: nil Request.URL") 510 } 511 if req.Header == nil { 512 req.closeBody() 513 return nil, errors.New("http: nil Request.Header") 514 } 515 scheme := req.URL.Scheme 516 isHTTP := scheme == "http" || scheme == "https" 517 if isHTTP { 518 for k, vv := range req.Header { 519 if !httpguts.ValidHeaderFieldName(k) { 520 req.closeBody() 521 return nil, fmt.Errorf("net/http: invalid header field name %q", k) 522 } 523 for _, v := range vv { 524 if !httpguts.ValidHeaderFieldValue(v) { 525 req.closeBody() 526 return nil, fmt.Errorf("net/http: invalid header field value %q for key %v", v, k) 527 } 528 } 529 } 530 } 531 532 origReq := req 533 cancelKey := cancelKey{origReq} 534 req = setupRewindBody(req) 535 536 if altRT := t.alternateRoundTripper(req); altRT != nil { 537 if resp, err := altRT.RoundTrip(req); err != ErrSkipAltProtocol { 538 return resp, err 539 } 540 var err error 541 req, err = rewindBody(req) 542 if err != nil { 543 return nil, err 544 } 545 } 546 if !isHTTP { 547 req.closeBody() 548 return nil, badStringError("unsupported protocol scheme", scheme) 549 } 550 if req.Method != "" && !validMethod(req.Method) { 551 req.closeBody() 552 return nil, fmt.Errorf("net/http: invalid method %q", req.Method) 553 } 554 if req.URL.Host == "" { 555 req.closeBody() 556 return nil, errors.New("http: no Host in request URL") 557 } 558 559 for { 560 select { 561 case <-ctx.Done(): 562 req.closeBody() 563 return nil, ctx.Err() 564 default: 565 } 566 567 // treq gets modified by roundTrip, so we need to recreate for each retry. 568 treq := &transportRequest{Request: req, trace: trace, cancelKey: cancelKey} 569 cm, err := t.connectMethodForRequest(treq) 570 if err != nil { 571 req.closeBody() 572 return nil, err 573 } 574 575 // Get the cached or newly-created connection to either the 576 // host (for http or https), the http proxy, or the http proxy 577 // pre-CONNECTed to https server. In any case, we'll be ready 578 // to send it requests. 579 pconn, err := t.getConn(treq, cm) 580 if err != nil { 581 t.setReqCanceler(cancelKey, nil) 582 req.closeBody() 583 return nil, err 584 } 585 586 var resp *Response 587 if pconn.alt != nil { 588 // HTTP/2 path. 589 t.setReqCanceler(cancelKey, nil) // not cancelable with CancelRequest 590 resp, err = pconn.alt.RoundTrip(req) 591 } else { 592 resp, err = pconn.roundTrip(treq) 593 } 594 if err == nil { 595 resp.Request = origReq 596 return resp, nil 597 } 598 599 // Failed. Clean up and determine whether to retry. 600 if http2isNoCachedConnError(err) { 601 if t.removeIdleConn(pconn) { 602 t.decConnsPerHost(pconn.cacheKey) 603 } 604 } else if !pconn.shouldRetryRequest(req, err) { 605 // Issue 16465: return underlying net.Conn.Read error from peek, 606 // as we've historically done. 607 if e, ok := err.(transportReadFromServerError); ok { 608 err = e.err 609 } 610 return nil, err 611 } 612 testHookRoundTripRetried() 613 614 // Rewind the body if we're able to. 615 req, err = rewindBody(req) 616 if err != nil { 617 return nil, err 618 } 619 } 620 } 621 622 var errCannotRewind = errors.New("net/http: cannot rewind body after connection loss") 623 624 type readTrackingBody struct { 625 io.ReadCloser 626 didRead bool 627 didClose bool 628 } 629 630 func (r *readTrackingBody) Read(data []byte) (int, error) { 631 r.didRead = true 632 return r.ReadCloser.Read(data) 633 } 634 635 func (r *readTrackingBody) Close() error { 636 r.didClose = true 637 return r.ReadCloser.Close() 638 } 639 640 // setupRewindBody returns a new request with a custom body wrapper 641 // that can report whether the body needs rewinding. 642 // This lets rewindBody avoid an error result when the request 643 // does not have GetBody but the body hasn't been read at all yet. 644 func setupRewindBody(req *Request) *Request { 645 if req.Body == nil || req.Body == NoBody { 646 return req 647 } 648 newReq := *req 649 newReq.Body = &readTrackingBody{ReadCloser: req.Body} 650 return &newReq 651 } 652 653 // rewindBody returns a new request with the body rewound. 654 // It returns req unmodified if the body does not need rewinding. 655 // rewindBody takes care of closing req.Body when appropriate 656 // (in all cases except when rewindBody returns req unmodified). 657 func rewindBody(req *Request) (rewound *Request, err error) { 658 if req.Body == nil || req.Body == NoBody || (!req.Body.(*readTrackingBody).didRead && !req.Body.(*readTrackingBody).didClose) { 659 return req, nil // nothing to rewind 660 } 661 if !req.Body.(*readTrackingBody).didClose { 662 req.closeBody() 663 } 664 if req.GetBody == nil { 665 return nil, errCannotRewind 666 } 667 body, err := req.GetBody() 668 if err != nil { 669 return nil, err 670 } 671 newReq := *req 672 newReq.Body = &readTrackingBody{ReadCloser: body} 673 return &newReq, nil 674 } 675 676 // shouldRetryRequest reports whether we should retry sending a failed 677 // HTTP request on a new connection. The non-nil input error is the 678 // error from roundTrip. 679 func (pc *persistConn) shouldRetryRequest(req *Request, err error) bool { 680 if http2isNoCachedConnError(err) { 681 // Issue 16582: if the user started a bunch of 682 // requests at once, they can all pick the same conn 683 // and violate the server's max concurrent streams. 684 // Instead, match the HTTP/1 behavior for now and dial 685 // again to get a new TCP connection, rather than failing 686 // this request. 687 return true 688 } 689 if err == errMissingHost { 690 // User error. 691 return false 692 } 693 if !pc.isReused() { 694 // This was a fresh connection. There's no reason the server 695 // should've hung up on us. 696 // 697 // Also, if we retried now, we could loop forever 698 // creating new connections and retrying if the server 699 // is just hanging up on us because it doesn't like 700 // our request (as opposed to sending an error). 701 return false 702 } 703 if _, ok := err.(nothingWrittenError); ok { 704 // We never wrote anything, so it's safe to retry, if there's no body or we 705 // can "rewind" the body with GetBody. 706 return req.outgoingLength() == 0 || req.GetBody != nil 707 } 708 if !req.isReplayable() { 709 // Don't retry non-idempotent requests. 710 return false 711 } 712 if _, ok := err.(transportReadFromServerError); ok { 713 // We got some non-EOF net.Conn.Read failure reading 714 // the 1st response byte from the server. 715 return true 716 } 717 if err == errServerClosedIdle { 718 // The server replied with io.EOF while we were trying to 719 // read the response. Probably an unfortunately keep-alive 720 // timeout, just as the client was writing a request. 721 return true 722 } 723 return false // conservatively 724 } 725 726 // ErrSkipAltProtocol is a sentinel error value defined by Transport.RegisterProtocol. 727 var ErrSkipAltProtocol = errors.New("net/http: skip alternate protocol") 728 729 // RegisterProtocol registers a new protocol with scheme. 730 // The Transport will pass requests using the given scheme to rt. 731 // It is rt's responsibility to simulate HTTP request semantics. 732 // 733 // RegisterProtocol can be used by other packages to provide 734 // implementations of protocol schemes like "ftp" or "file". 735 // 736 // If rt.RoundTrip returns ErrSkipAltProtocol, the Transport will 737 // handle the RoundTrip itself for that one request, as if the 738 // protocol were not registered. 739 func (t *Transport) RegisterProtocol(scheme string, rt RoundTripper) { 740 t.altMu.Lock() 741 defer t.altMu.Unlock() 742 oldMap, _ := t.altProto.Load().(map[string]RoundTripper) 743 if _, exists := oldMap[scheme]; exists { 744 panic("protocol " + scheme + " already registered") 745 } 746 newMap := make(map[string]RoundTripper) 747 for k, v := range oldMap { 748 newMap[k] = v 749 } 750 newMap[scheme] = rt 751 t.altProto.Store(newMap) 752 } 753 754 // CloseIdleConnections closes any connections which were previously 755 // connected from previous requests but are now sitting idle in 756 // a "keep-alive" state. It does not interrupt any connections currently 757 // in use. 758 func (t *Transport) CloseIdleConnections() { 759 t.nextProtoOnce.Do(t.onceSetNextProtoDefaults) 760 t.idleMu.Lock() 761 m := t.idleConn 762 t.idleConn = nil 763 t.closeIdle = true // close newly idle connections 764 t.idleLRU = connLRU{} 765 t.idleMu.Unlock() 766 for _, conns := range m { 767 for _, pconn := range conns { 768 pconn.close(errCloseIdleConns) 769 } 770 } 771 if t2 := t.h2transport; t2 != nil { 772 t2.CloseIdleConnections() 773 } 774 } 775 776 // CancelRequest cancels an in-flight request by closing its connection. 777 // CancelRequest should only be called after RoundTrip has returned. 778 // 779 // Deprecated: Use Request.WithContext to create a request with a 780 // cancelable context instead. CancelRequest cannot cancel HTTP/2 781 // requests. 782 func (t *Transport) CancelRequest(req *Request) { 783 t.cancelRequest(cancelKey{req}, errRequestCanceled) 784 } 785 786 // Cancel an in-flight request, recording the error value. 787 // Returns whether the request was canceled. 788 func (t *Transport) cancelRequest(key cancelKey, err error) bool { 789 t.reqMu.Lock() 790 cancel := t.reqCanceler[key] 791 delete(t.reqCanceler, key) 792 t.reqMu.Unlock() 793 if cancel != nil { 794 cancel(err) 795 } 796 797 return cancel != nil 798 } 799 800 // 801 // Private implementation past this point. 802 // 803 804 var ( 805 // proxyConfigOnce guards proxyConfig 806 envProxyOnce sync.Once 807 envProxyFuncValue func(*url.URL) (*url.URL, error) 808 ) 809 810 // defaultProxyConfig returns a ProxyConfig value looked up 811 // from the environment. This mitigates expensive lookups 812 // on some platforms (e.g. Windows). 813 func envProxyFunc() func(*url.URL) (*url.URL, error) { 814 envProxyOnce.Do(func() { 815 envProxyFuncValue = httpproxy.FromEnvironment().ProxyFunc() 816 }) 817 return envProxyFuncValue 818 } 819 820 // resetProxyConfig is used by tests. 821 func resetProxyConfig() { 822 envProxyOnce = sync.Once{} 823 envProxyFuncValue = nil 824 } 825 826 func (t *Transport) connectMethodForRequest(treq *transportRequest) (cm connectMethod, err error) { 827 cm.targetScheme = treq.URL.Scheme 828 cm.targetAddr = canonicalAddr(treq.URL) 829 if t.Proxy != nil { 830 cm.proxyURL, err = t.Proxy(treq.Request) 831 } 832 cm.onlyH1 = treq.requiresHTTP1() 833 return cm, err 834 } 835 836 // proxyAuth returns the Proxy-Authorization header to set 837 // on requests, if applicable. 838 func (cm *connectMethod) proxyAuth() string { 839 if cm.proxyURL == nil { 840 return "" 841 } 842 if u := cm.proxyURL.User; u != nil { 843 username := u.Username() 844 password, _ := u.Password() 845 return "Basic " + basicAuth(username, password) 846 } 847 return "" 848 } 849 850 // error values for debugging and testing, not seen by users. 851 var ( 852 errKeepAlivesDisabled = errors.New("http: putIdleConn: keep alives disabled") 853 errConnBroken = errors.New("http: putIdleConn: connection is in bad state") 854 errCloseIdle = errors.New("http: putIdleConn: CloseIdleConnections was called") 855 errTooManyIdle = errors.New("http: putIdleConn: too many idle connections") 856 errTooManyIdleHost = errors.New("http: putIdleConn: too many idle connections for host") 857 errCloseIdleConns = errors.New("http: CloseIdleConnections called") 858 errReadLoopExiting = errors.New("http: persistConn.readLoop exiting") 859 errIdleConnTimeout = errors.New("http: idle connection timeout") 860 861 // errServerClosedIdle is not seen by users for idempotent requests, but may be 862 // seen by a user if the server shuts down an idle connection and sends its FIN 863 // in flight with already-written POST body bytes from the client. 864 // See https://github.com/golang/go/issues/19943#issuecomment-355607646 865 errServerClosedIdle = errors.New("http: server closed idle connection") 866 ) 867 868 // transportReadFromServerError is used by Transport.readLoop when the 869 // 1 byte peek read fails and we're actually anticipating a response. 870 // Usually this is just due to the inherent keep-alive shut down race, 871 // where the server closed the connection at the same time the client 872 // wrote. The underlying err field is usually io.EOF or some 873 // ECONNRESET sort of thing which varies by platform. But it might be 874 // the user's custom net.Conn.Read error too, so we carry it along for 875 // them to return from Transport.RoundTrip. 876 type transportReadFromServerError struct { 877 err error 878 } 879 880 func (e transportReadFromServerError) Unwrap() error { return e.err } 881 882 func (e transportReadFromServerError) Error() string { 883 return fmt.Sprintf("net/http: Transport failed to read from server: %v", e.err) 884 } 885 886 func (t *Transport) putOrCloseIdleConn(pconn *persistConn) { 887 if err := t.tryPutIdleConn(pconn); err != nil { 888 pconn.close(err) 889 } 890 } 891 892 func (t *Transport) maxIdleConnsPerHost() int { 893 if v := t.MaxIdleConnsPerHost; v != 0 { 894 return v 895 } 896 return DefaultMaxIdleConnsPerHost 897 } 898 899 // tryPutIdleConn adds pconn to the list of idle persistent connections awaiting 900 // a new request. 901 // If pconn is no longer needed or not in a good state, tryPutIdleConn returns 902 // an error explaining why it wasn't registered. 903 // tryPutIdleConn does not close pconn. Use putOrCloseIdleConn instead for that. 904 func (t *Transport) tryPutIdleConn(pconn *persistConn) error { 905 if t.DisableKeepAlives || t.MaxIdleConnsPerHost < 0 { 906 return errKeepAlivesDisabled 907 } 908 if pconn.isBroken() { 909 return errConnBroken 910 } 911 pconn.markReused() 912 913 t.idleMu.Lock() 914 defer t.idleMu.Unlock() 915 916 // HTTP/2 (pconn.alt != nil) connections do not come out of the idle list, 917 // because multiple goroutines can use them simultaneously. 918 // If this is an HTTP/2 connection being “returned,” we're done. 919 if pconn.alt != nil && t.idleLRU.m[pconn] != nil { 920 return nil 921 } 922 923 // Deliver pconn to goroutine waiting for idle connection, if any. 924 // (They may be actively dialing, but this conn is ready first. 925 // Chrome calls this socket late binding. 926 // See https://www.chromium.org/developers/design-documents/network-stack#TOC-Connection-Management.) 927 key := pconn.cacheKey 928 if q, ok := t.idleConnWait[key]; ok { 929 done := false 930 if pconn.alt == nil { 931 // HTTP/1. 932 // Loop over the waiting list until we find a w that isn't done already, and hand it pconn. 933 for q.len() > 0 { 934 w := q.popFront() 935 if w.tryDeliver(pconn, nil) { 936 done = true 937 break 938 } 939 } 940 } else { 941 // HTTP/2. 942 // Can hand the same pconn to everyone in the waiting list, 943 // and we still won't be done: we want to put it in the idle 944 // list unconditionally, for any future clients too. 945 for q.len() > 0 { 946 w := q.popFront() 947 w.tryDeliver(pconn, nil) 948 } 949 } 950 if q.len() == 0 { 951 delete(t.idleConnWait, key) 952 } else { 953 t.idleConnWait[key] = q 954 } 955 if done { 956 return nil 957 } 958 } 959 960 if t.closeIdle { 961 return errCloseIdle 962 } 963 if t.idleConn == nil { 964 t.idleConn = make(map[connectMethodKey][]*persistConn) 965 } 966 idles := t.idleConn[key] 967 if len(idles) >= t.maxIdleConnsPerHost() { 968 return errTooManyIdleHost 969 } 970 for _, exist := range idles { 971 if exist == pconn { 972 log.Fatalf("dup idle pconn %p in freelist", pconn) 973 } 974 } 975 t.idleConn[key] = append(idles, pconn) 976 t.idleLRU.add(pconn) 977 if t.MaxIdleConns != 0 && t.idleLRU.len() > t.MaxIdleConns { 978 oldest := t.idleLRU.removeOldest() 979 oldest.close(errTooManyIdle) 980 t.removeIdleConnLocked(oldest) 981 } 982 983 // Set idle timer, but only for HTTP/1 (pconn.alt == nil). 984 // The HTTP/2 implementation manages the idle timer itself 985 // (see idleConnTimeout in h2_bundle.go). 986 if t.IdleConnTimeout > 0 && pconn.alt == nil { 987 if pconn.idleTimer != nil { 988 pconn.idleTimer.Reset(t.IdleConnTimeout) 989 } else { 990 pconn.idleTimer = time.AfterFunc(t.IdleConnTimeout, pconn.closeConnIfStillIdle) 991 } 992 } 993 pconn.idleAt = time.Now() 994 return nil 995 } 996 997 // queueForIdleConn queues w to receive the next idle connection for w.cm. 998 // As an optimization hint to the caller, queueForIdleConn reports whether 999 // it successfully delivered an already-idle connection. 1000 func (t *Transport) queueForIdleConn(w *wantConn) (delivered bool) { 1001 if t.DisableKeepAlives { 1002 return false 1003 } 1004 1005 t.idleMu.Lock() 1006 defer t.idleMu.Unlock() 1007 1008 // Stop closing connections that become idle - we might want one. 1009 // (That is, undo the effect of t.CloseIdleConnections.) 1010 t.closeIdle = false 1011 1012 if w == nil { 1013 // Happens in test hook. 1014 return false 1015 } 1016 1017 // If IdleConnTimeout is set, calculate the oldest 1018 // persistConn.idleAt time we're willing to use a cached idle 1019 // conn. 1020 var oldTime time.Time 1021 if t.IdleConnTimeout > 0 { 1022 oldTime = time.Now().Add(-t.IdleConnTimeout) 1023 } 1024 1025 // Look for most recently-used idle connection. 1026 if list, ok := t.idleConn[w.key]; ok { 1027 stop := false 1028 delivered := false 1029 for len(list) > 0 && !stop { 1030 pconn := list[len(list)-1] 1031 1032 // See whether this connection has been idle too long, considering 1033 // only the wall time (the Round(0)), in case this is a laptop or VM 1034 // coming out of suspend with previously cached idle connections. 1035 tooOld := !oldTime.IsZero() && pconn.idleAt.Round(0).Before(oldTime) 1036 if tooOld { 1037 // Async cleanup. Launch in its own goroutine (as if a 1038 // time.AfterFunc called it); it acquires idleMu, which we're 1039 // holding, and does a synchronous net.Conn.Close. 1040 go pconn.closeConnIfStillIdle() 1041 } 1042 if pconn.isBroken() || tooOld { 1043 // If either persistConn.readLoop has marked the connection 1044 // broken, but Transport.removeIdleConn has not yet removed it 1045 // from the idle list, or if this persistConn is too old (it was 1046 // idle too long), then ignore it and look for another. In both 1047 // cases it's already in the process of being closed. 1048 list = list[:len(list)-1] 1049 continue 1050 } 1051 delivered = w.tryDeliver(pconn, nil) 1052 if delivered { 1053 if pconn.alt != nil { 1054 // HTTP/2: multiple clients can share pconn. 1055 // Leave it in the list. 1056 } else { 1057 // HTTP/1: only one client can use pconn. 1058 // Remove it from the list. 1059 t.idleLRU.remove(pconn) 1060 list = list[:len(list)-1] 1061 } 1062 } 1063 stop = true 1064 } 1065 if len(list) > 0 { 1066 t.idleConn[w.key] = list 1067 } else { 1068 delete(t.idleConn, w.key) 1069 } 1070 if stop { 1071 return delivered 1072 } 1073 } 1074 1075 // Register to receive next connection that becomes idle. 1076 if t.idleConnWait == nil { 1077 t.idleConnWait = make(map[connectMethodKey]wantConnQueue) 1078 } 1079 q := t.idleConnWait[w.key] 1080 q.cleanFront() 1081 q.pushBack(w) 1082 t.idleConnWait[w.key] = q 1083 return false 1084 } 1085 1086 // removeIdleConn marks pconn as dead. 1087 func (t *Transport) removeIdleConn(pconn *persistConn) bool { 1088 t.idleMu.Lock() 1089 defer t.idleMu.Unlock() 1090 return t.removeIdleConnLocked(pconn) 1091 } 1092 1093 // t.idleMu must be held. 1094 func (t *Transport) removeIdleConnLocked(pconn *persistConn) bool { 1095 if pconn.idleTimer != nil { 1096 pconn.idleTimer.Stop() 1097 } 1098 t.idleLRU.remove(pconn) 1099 key := pconn.cacheKey 1100 pconns := t.idleConn[key] 1101 var removed bool 1102 switch len(pconns) { 1103 case 0: 1104 // Nothing 1105 case 1: 1106 if pconns[0] == pconn { 1107 delete(t.idleConn, key) 1108 removed = true 1109 } 1110 default: 1111 for i, v := range pconns { 1112 if v != pconn { 1113 continue 1114 } 1115 // Slide down, keeping most recently-used 1116 // conns at the end. 1117 copy(pconns[i:], pconns[i+1:]) 1118 t.idleConn[key] = pconns[:len(pconns)-1] 1119 removed = true 1120 break 1121 } 1122 } 1123 return removed 1124 } 1125 1126 func (t *Transport) setReqCanceler(key cancelKey, fn func(error)) { 1127 t.reqMu.Lock() 1128 defer t.reqMu.Unlock() 1129 if t.reqCanceler == nil { 1130 t.reqCanceler = make(map[cancelKey]func(error)) 1131 } 1132 if fn != nil { 1133 t.reqCanceler[key] = fn 1134 } else { 1135 delete(t.reqCanceler, key) 1136 } 1137 } 1138 1139 // replaceReqCanceler replaces an existing cancel function. If there is no cancel function 1140 // for the request, we don't set the function and return false. 1141 // Since CancelRequest will clear the canceler, we can use the return value to detect if 1142 // the request was canceled since the last setReqCancel call. 1143 func (t *Transport) replaceReqCanceler(key cancelKey, fn func(error)) bool { 1144 t.reqMu.Lock() 1145 defer t.reqMu.Unlock() 1146 _, ok := t.reqCanceler[key] 1147 if !ok { 1148 return false 1149 } 1150 if fn != nil { 1151 t.reqCanceler[key] = fn 1152 } else { 1153 delete(t.reqCanceler, key) 1154 } 1155 return true 1156 } 1157 1158 var zeroDialer net.Dialer 1159 1160 func (t *Transport) dial(ctx context.Context, network, addr string) (net.Conn, error) { 1161 if t.DialContext != nil { 1162 return t.DialContext(ctx, network, addr) 1163 } 1164 if t.Dial != nil { 1165 c, err := t.Dial(network, addr) 1166 if c == nil && err == nil { 1167 err = errors.New("net/http: Transport.Dial hook returned (nil, nil)") 1168 } 1169 return c, err 1170 } 1171 return zeroDialer.DialContext(ctx, network, addr) 1172 } 1173 1174 // A wantConn records state about a wanted connection 1175 // (that is, an active call to getConn). 1176 // The conn may be gotten by dialing or by finding an idle connection, 1177 // or a cancellation may make the conn no longer wanted. 1178 // These three options are racing against each other and use 1179 // wantConn to coordinate and agree about the winning outcome. 1180 type wantConn struct { 1181 cm connectMethod 1182 key connectMethodKey // cm.key() 1183 ctx context.Context // context for dial 1184 ready chan struct{} // closed when pc, err pair is delivered 1185 1186 // hooks for testing to know when dials are done 1187 // beforeDial is called in the getConn goroutine when the dial is queued. 1188 // afterDial is called when the dial is completed or cancelled. 1189 beforeDial func() 1190 afterDial func() 1191 1192 mu sync.Mutex // protects pc, err, close(ready) 1193 pc *persistConn 1194 err error 1195 } 1196 1197 // waiting reports whether w is still waiting for an answer (connection or error). 1198 func (w *wantConn) waiting() bool { 1199 select { 1200 case <-w.ready: 1201 return false 1202 default: 1203 return true 1204 } 1205 } 1206 1207 // tryDeliver attempts to deliver pc, err to w and reports whether it succeeded. 1208 func (w *wantConn) tryDeliver(pc *persistConn, err error) bool { 1209 w.mu.Lock() 1210 defer w.mu.Unlock() 1211 1212 if w.pc != nil || w.err != nil { 1213 return false 1214 } 1215 1216 w.pc = pc 1217 w.err = err 1218 if w.pc == nil && w.err == nil { 1219 panic("net/http: internal error: misuse of tryDeliver") 1220 } 1221 close(w.ready) 1222 return true 1223 } 1224 1225 // cancel marks w as no longer wanting a result (for example, due to cancellation). 1226 // If a connection has been delivered already, cancel returns it with t.putOrCloseIdleConn. 1227 func (w *wantConn) cancel(t *Transport, err error) { 1228 w.mu.Lock() 1229 if w.pc == nil && w.err == nil { 1230 close(w.ready) // catch misbehavior in future delivery 1231 } 1232 pc := w.pc 1233 w.pc = nil 1234 w.err = err 1235 w.mu.Unlock() 1236 1237 if pc != nil { 1238 t.putOrCloseIdleConn(pc) 1239 } 1240 } 1241 1242 // A wantConnQueue is a queue of wantConns. 1243 type wantConnQueue struct { 1244 // This is a queue, not a deque. 1245 // It is split into two stages - head[headPos:] and tail. 1246 // popFront is trivial (headPos++) on the first stage, and 1247 // pushBack is trivial (append) on the second stage. 1248 // If the first stage is empty, popFront can swap the 1249 // first and second stages to remedy the situation. 1250 // 1251 // This two-stage split is analogous to the use of two lists 1252 // in Okasaki's purely functional queue but without the 1253 // overhead of reversing the list when swapping stages. 1254 head []*wantConn 1255 headPos int 1256 tail []*wantConn 1257 } 1258 1259 // len returns the number of items in the queue. 1260 func (q *wantConnQueue) len() int { 1261 return len(q.head) - q.headPos + len(q.tail) 1262 } 1263 1264 // pushBack adds w to the back of the queue. 1265 func (q *wantConnQueue) pushBack(w *wantConn) { 1266 q.tail = append(q.tail, w) 1267 } 1268 1269 // popFront removes and returns the wantConn at the front of the queue. 1270 func (q *wantConnQueue) popFront() *wantConn { 1271 if q.headPos >= len(q.head) { 1272 if len(q.tail) == 0 { 1273 return nil 1274 } 1275 // Pick up tail as new head, clear tail. 1276 q.head, q.headPos, q.tail = q.tail, 0, q.head[:0] 1277 } 1278 w := q.head[q.headPos] 1279 q.head[q.headPos] = nil 1280 q.headPos++ 1281 return w 1282 } 1283 1284 // peekFront returns the wantConn at the front of the queue without removing it. 1285 func (q *wantConnQueue) peekFront() *wantConn { 1286 if q.headPos < len(q.head) { 1287 return q.head[q.headPos] 1288 } 1289 if len(q.tail) > 0 { 1290 return q.tail[0] 1291 } 1292 return nil 1293 } 1294 1295 // cleanFront pops any wantConns that are no longer waiting from the head of the 1296 // queue, reporting whether any were popped. 1297 func (q *wantConnQueue) cleanFront() (cleaned bool) { 1298 for { 1299 w := q.peekFront() 1300 if w == nil || w.waiting() { 1301 return cleaned 1302 } 1303 q.popFront() 1304 cleaned = true 1305 } 1306 } 1307 1308 func (t *Transport) customDialTLS(ctx context.Context, network, addr string) (conn net.Conn, err error) { 1309 if t.DialTLSContext != nil { 1310 conn, err = t.DialTLSContext(ctx, network, addr) 1311 } else { 1312 conn, err = t.DialTLS(network, addr) 1313 } 1314 if conn == nil && err == nil { 1315 err = errors.New("net/http: Transport.DialTLS or DialTLSContext returned (nil, nil)") 1316 } 1317 return 1318 } 1319 1320 // getConn dials and creates a new persistConn to the target as 1321 // specified in the connectMethod. This includes doing a proxy CONNECT 1322 // and/or setting up TLS. If this doesn't return an error, the persistConn 1323 // is ready to write requests to. 1324 func (t *Transport) getConn(treq *transportRequest, cm connectMethod) (pc *persistConn, err error) { 1325 req := treq.Request 1326 trace := treq.trace 1327 ctx := req.Context() 1328 if trace != nil && trace.GetConn != nil { 1329 trace.GetConn(cm.addr()) 1330 } 1331 1332 w := &wantConn{ 1333 cm: cm, 1334 key: cm.key(), 1335 ctx: ctx, 1336 ready: make(chan struct{}, 1), 1337 beforeDial: testHookPrePendingDial, 1338 afterDial: testHookPostPendingDial, 1339 } 1340 defer func() { 1341 if err != nil { 1342 w.cancel(t, err) 1343 } 1344 }() 1345 1346 // Queue for idle connection. 1347 if delivered := t.queueForIdleConn(w); delivered { 1348 pc := w.pc 1349 // Trace only for HTTP/1. 1350 // HTTP/2 calls trace.GotConn itself. 1351 if pc.alt == nil && trace != nil && trace.GotConn != nil { 1352 trace.GotConn(pc.gotIdleConnTrace(pc.idleAt)) 1353 } 1354 // set request canceler to some non-nil function so we 1355 // can detect whether it was cleared between now and when 1356 // we enter roundTrip 1357 t.setReqCanceler(treq.cancelKey, func(error) {}) 1358 return pc, nil 1359 } 1360 1361 cancelc := make(chan error, 1) 1362 t.setReqCanceler(treq.cancelKey, func(err error) { cancelc <- err }) 1363 1364 // Queue for permission to dial. 1365 t.queueForDial(w) 1366 1367 // Wait for completion or cancellation. 1368 select { 1369 case <-w.ready: 1370 // Trace success but only for HTTP/1. 1371 // HTTP/2 calls trace.GotConn itself. 1372 if w.pc != nil && w.pc.alt == nil && trace != nil && trace.GotConn != nil { 1373 trace.GotConn(httptrace.GotConnInfo{Conn: w.pc.conn, Reused: w.pc.isReused()}) 1374 } 1375 if w.err != nil { 1376 // If the request has been cancelled, that's probably 1377 // what caused w.err; if so, prefer to return the 1378 // cancellation error (see golang.org/issue/16049). 1379 select { 1380 case <-req.Cancel: 1381 return nil, errRequestCanceledConn 1382 case <-req.Context().Done(): 1383 return nil, req.Context().Err() 1384 case err := <-cancelc: 1385 if err == errRequestCanceled { 1386 err = errRequestCanceledConn 1387 } 1388 return nil, err 1389 default: 1390 // return below 1391 } 1392 } 1393 return w.pc, w.err 1394 case <-req.Cancel: 1395 return nil, errRequestCanceledConn 1396 case <-req.Context().Done(): 1397 return nil, req.Context().Err() 1398 case err := <-cancelc: 1399 if err == errRequestCanceled { 1400 err = errRequestCanceledConn 1401 } 1402 return nil, err 1403 } 1404 } 1405 1406 // queueForDial queues w to wait for permission to begin dialing. 1407 // Once w receives permission to dial, it will do so in a separate goroutine. 1408 func (t *Transport) queueForDial(w *wantConn) { 1409 w.beforeDial() 1410 if t.MaxConnsPerHost <= 0 { 1411 go t.dialConnFor(w) 1412 return 1413 } 1414 1415 t.connsPerHostMu.Lock() 1416 defer t.connsPerHostMu.Unlock() 1417 1418 if n := t.connsPerHost[w.key]; n < t.MaxConnsPerHost { 1419 if t.connsPerHost == nil { 1420 t.connsPerHost = make(map[connectMethodKey]int) 1421 } 1422 t.connsPerHost[w.key] = n + 1 1423 go t.dialConnFor(w) 1424 return 1425 } 1426 1427 if t.connsPerHostWait == nil { 1428 t.connsPerHostWait = make(map[connectMethodKey]wantConnQueue) 1429 } 1430 q := t.connsPerHostWait[w.key] 1431 q.cleanFront() 1432 q.pushBack(w) 1433 t.connsPerHostWait[w.key] = q 1434 } 1435 1436 // dialConnFor dials on behalf of w and delivers the result to w. 1437 // dialConnFor has received permission to dial w.cm and is counted in t.connCount[w.cm.key()]. 1438 // If the dial is cancelled or unsuccessful, dialConnFor decrements t.connCount[w.cm.key()]. 1439 func (t *Transport) dialConnFor(w *wantConn) { 1440 defer w.afterDial() 1441 1442 pc, err := t.dialConn(w.ctx, w.cm) 1443 delivered := w.tryDeliver(pc, err) 1444 if err == nil && (!delivered || pc.alt != nil) { 1445 // pconn was not passed to w, 1446 // or it is HTTP/2 and can be shared. 1447 // Add to the idle connection pool. 1448 t.putOrCloseIdleConn(pc) 1449 } 1450 if err != nil { 1451 t.decConnsPerHost(w.key) 1452 } 1453 } 1454 1455 // decConnsPerHost decrements the per-host connection count for key, 1456 // which may in turn give a different waiting goroutine permission to dial. 1457 func (t *Transport) decConnsPerHost(key connectMethodKey) { 1458 if t.MaxConnsPerHost <= 0 { 1459 return 1460 } 1461 1462 t.connsPerHostMu.Lock() 1463 defer t.connsPerHostMu.Unlock() 1464 n := t.connsPerHost[key] 1465 if n == 0 { 1466 // Shouldn't happen, but if it does, the counting is buggy and could 1467 // easily lead to a silent deadlock, so report the problem loudly. 1468 panic("net/http: internal error: connCount underflow") 1469 } 1470 1471 // Can we hand this count to a goroutine still waiting to dial? 1472 // (Some goroutines on the wait list may have timed out or 1473 // gotten a connection another way. If they're all gone, 1474 // we don't want to kick off any spurious dial operations.) 1475 if q := t.connsPerHostWait[key]; q.len() > 0 { 1476 done := false 1477 for q.len() > 0 { 1478 w := q.popFront() 1479 if w.waiting() { 1480 go t.dialConnFor(w) 1481 done = true 1482 break 1483 } 1484 } 1485 if q.len() == 0 { 1486 delete(t.connsPerHostWait, key) 1487 } else { 1488 // q is a value (like a slice), so we have to store 1489 // the updated q back into the map. 1490 t.connsPerHostWait[key] = q 1491 } 1492 if done { 1493 return 1494 } 1495 } 1496 1497 // Otherwise, decrement the recorded count. 1498 if n--; n == 0 { 1499 delete(t.connsPerHost, key) 1500 } else { 1501 t.connsPerHost[key] = n 1502 } 1503 } 1504 1505 // Add TLS to a persistent connection, i.e. negotiate a TLS session. If pconn is already a TLS 1506 // tunnel, this function establishes a nested TLS session inside the encrypted channel. 1507 // The remote endpoint's name may be overridden by TLSClientConfig.ServerName. 1508 func (pconn *persistConn) addTLS(name string, trace *httptrace.ClientTrace) error { 1509 // Initiate TLS and check remote host name against certificate. 1510 cfg := cloneTLSConfig(pconn.t.TLSClientConfig) 1511 if cfg.ServerName == "" { 1512 cfg.ServerName = name 1513 } 1514 if pconn.cacheKey.onlyH1 { 1515 cfg.NextProtos = nil 1516 } 1517 plainConn := pconn.conn 1518 tlsConn := tls.Client(plainConn, cfg) 1519 errc := make(chan error, 2) 1520 var timer *time.Timer // for canceling TLS handshake 1521 if d := pconn.t.TLSHandshakeTimeout; d != 0 { 1522 timer = time.AfterFunc(d, func() { 1523 errc <- tlsHandshakeTimeoutError{} 1524 }) 1525 } 1526 go func() { 1527 if trace != nil && trace.TLSHandshakeStart != nil { 1528 trace.TLSHandshakeStart() 1529 } 1530 err := tlsConn.Handshake() 1531 if timer != nil { 1532 timer.Stop() 1533 } 1534 errc <- err 1535 }() 1536 if err := <-errc; err != nil { 1537 plainConn.Close() 1538 if trace != nil && trace.TLSHandshakeDone != nil { 1539 trace.TLSHandshakeDone(tls.ConnectionState{}, err) 1540 } 1541 return err 1542 } 1543 cs := tlsConn.ConnectionState() 1544 if trace != nil && trace.TLSHandshakeDone != nil { 1545 trace.TLSHandshakeDone(cs, nil) 1546 } 1547 pconn.tlsState = &cs 1548 pconn.conn = tlsConn 1549 return nil 1550 } 1551 1552 type erringRoundTripper interface { 1553 RoundTripErr() error 1554 } 1555 1556 func (t *Transport) dialConn(ctx context.Context, cm connectMethod) (pconn *persistConn, err error) { 1557 pconn = &persistConn{ 1558 t: t, 1559 cacheKey: cm.key(), 1560 reqch: make(chan requestAndChan, 1), 1561 writech: make(chan writeRequest, 1), 1562 closech: make(chan struct{}), 1563 writeErrCh: make(chan error, 1), 1564 writeLoopDone: make(chan struct{}), 1565 } 1566 trace := httptrace.ContextClientTrace(ctx) 1567 wrapErr := func(err error) error { 1568 if cm.proxyURL != nil { 1569 // Return a typed error, per Issue 16997 1570 return &net.OpError{Op: "proxyconnect", Net: "tcp", Err: err} 1571 } 1572 return err 1573 } 1574 if cm.scheme() == "https" && t.hasCustomTLSDialer() { 1575 var err error 1576 pconn.conn, err = t.customDialTLS(ctx, "tcp", cm.addr()) 1577 if err != nil { 1578 return nil, wrapErr(err) 1579 } 1580 if tc, ok := pconn.conn.(*tls.Conn); ok { 1581 // Handshake here, in case DialTLS didn't. TLSNextProto below 1582 // depends on it for knowing the connection state. 1583 if trace != nil && trace.TLSHandshakeStart != nil { 1584 trace.TLSHandshakeStart() 1585 } 1586 if err := tc.Handshake(); err != nil { 1587 go pconn.conn.Close() 1588 if trace != nil && trace.TLSHandshakeDone != nil { 1589 trace.TLSHandshakeDone(tls.ConnectionState{}, err) 1590 } 1591 return nil, err 1592 } 1593 cs := tc.ConnectionState() 1594 if trace != nil && trace.TLSHandshakeDone != nil { 1595 trace.TLSHandshakeDone(cs, nil) 1596 } 1597 pconn.tlsState = &cs 1598 } 1599 } else { 1600 conn, err := t.dial(ctx, "tcp", cm.addr()) 1601 if err != nil { 1602 return nil, wrapErr(err) 1603 } 1604 pconn.conn = conn 1605 if cm.scheme() == "https" { 1606 var firstTLSHost string 1607 if firstTLSHost, _, err = net.SplitHostPort(cm.addr()); err != nil { 1608 return nil, wrapErr(err) 1609 } 1610 if err = pconn.addTLS(firstTLSHost, trace); err != nil { 1611 return nil, wrapErr(err) 1612 } 1613 } 1614 } 1615 1616 // Proxy setup. 1617 switch { 1618 case cm.proxyURL == nil: 1619 // Do nothing. Not using a proxy. 1620 case cm.proxyURL.Scheme == "socks5": 1621 conn := pconn.conn 1622 d := socksNewDialer("tcp", conn.RemoteAddr().String()) 1623 if u := cm.proxyURL.User; u != nil { 1624 auth := &socksUsernamePassword{ 1625 Username: u.Username(), 1626 } 1627 auth.Password, _ = u.Password() 1628 d.AuthMethods = []socksAuthMethod{ 1629 socksAuthMethodNotRequired, 1630 socksAuthMethodUsernamePassword, 1631 } 1632 d.Authenticate = auth.Authenticate 1633 } 1634 if _, err := d.DialWithConn(ctx, conn, "tcp", cm.targetAddr); err != nil { 1635 conn.Close() 1636 return nil, err 1637 } 1638 case cm.targetScheme == "http": 1639 pconn.isProxy = true 1640 if pa := cm.proxyAuth(); pa != "" { 1641 pconn.mutateHeaderFunc = func(h Header) { 1642 h.Set("Proxy-Authorization", pa) 1643 } 1644 } 1645 case cm.targetScheme == "https": 1646 conn := pconn.conn 1647 var hdr Header 1648 if t.GetProxyConnectHeader != nil { 1649 var err error 1650 hdr, err = t.GetProxyConnectHeader(ctx, cm.proxyURL, cm.targetAddr) 1651 if err != nil { 1652 conn.Close() 1653 return nil, err 1654 } 1655 } else { 1656 hdr = t.ProxyConnectHeader 1657 } 1658 if hdr == nil { 1659 hdr = make(Header) 1660 } 1661 if pa := cm.proxyAuth(); pa != "" { 1662 hdr = hdr.Clone() 1663 hdr.Set("Proxy-Authorization", pa) 1664 } 1665 connectReq := &Request{ 1666 Method: "CONNECT", 1667 URL: &url.URL{Opaque: cm.targetAddr}, 1668 Host: cm.targetAddr, 1669 Header: hdr, 1670 } 1671 1672 // If there's no done channel (no deadline or cancellation 1673 // from the caller possible), at least set some (long) 1674 // timeout here. This will make sure we don't block forever 1675 // and leak a goroutine if the connection stops replying 1676 // after the TCP connect. 1677 connectCtx := ctx 1678 if ctx.Done() == nil { 1679 newCtx, cancel := context.WithTimeout(ctx, 1*time.Minute) 1680 defer cancel() 1681 connectCtx = newCtx 1682 } 1683 1684 didReadResponse := make(chan struct{}) // closed after CONNECT write+read is done or fails 1685 var ( 1686 resp *Response 1687 err error // write or read error 1688 ) 1689 // Write the CONNECT request & read the response. 1690 go func() { 1691 defer close(didReadResponse) 1692 err = connectReq.Write(conn) 1693 if err != nil { 1694 return 1695 } 1696 // Okay to use and discard buffered reader here, because 1697 // TLS server will not speak until spoken to. 1698 br := bufio.NewReader(conn) 1699 resp, err = ReadResponse(br, connectReq) 1700 }() 1701 select { 1702 case <-connectCtx.Done(): 1703 conn.Close() 1704 <-didReadResponse 1705 return nil, connectCtx.Err() 1706 case <-didReadResponse: 1707 // resp or err now set 1708 } 1709 if err != nil { 1710 conn.Close() 1711 return nil, err 1712 } 1713 if resp.StatusCode != 200 { 1714 f := strings.SplitN(resp.Status, " ", 2) 1715 conn.Close() 1716 if len(f) < 2 { 1717 return nil, errors.New("unknown status code") 1718 } 1719 return nil, errors.New(f[1]) 1720 } 1721 } 1722 1723 if cm.proxyURL != nil && cm.targetScheme == "https" { 1724 if err := pconn.addTLS(cm.tlsHost(), trace); err != nil { 1725 return nil, err 1726 } 1727 } 1728 1729 if s := pconn.tlsState; s != nil && s.NegotiatedProtocolIsMutual && s.NegotiatedProtocol != "" { 1730 if next, ok := t.TLSNextProto[s.NegotiatedProtocol]; ok { 1731 alt := next(cm.targetAddr, pconn.conn.(*tls.Conn)) 1732 if e, ok := alt.(erringRoundTripper); ok { 1733 // pconn.conn was closed by next (http2configureTransports.upgradeFn). 1734 return nil, e.RoundTripErr() 1735 } 1736 return &persistConn{t: t, cacheKey: pconn.cacheKey, alt: alt}, nil 1737 } 1738 } 1739 1740 pconn.br = bufio.NewReaderSize(pconn, t.readBufferSize()) 1741 pconn.bw = bufio.NewWriterSize(persistConnWriter{pconn}, t.writeBufferSize()) 1742 1743 go pconn.readLoop() 1744 go pconn.writeLoop() 1745 return pconn, nil 1746 } 1747 1748 // persistConnWriter is the io.Writer written to by pc.bw. 1749 // It accumulates the number of bytes written to the underlying conn, 1750 // so the retry logic can determine whether any bytes made it across 1751 // the wire. 1752 // This is exactly 1 pointer field wide so it can go into an interface 1753 // without allocation. 1754 type persistConnWriter struct { 1755 pc *persistConn 1756 } 1757 1758 func (w persistConnWriter) Write(p []byte) (n int, err error) { 1759 n, err = w.pc.conn.Write(p) 1760 w.pc.nwrite += int64(n) 1761 return 1762 } 1763 1764 // ReadFrom exposes persistConnWriter's underlying Conn to io.Copy and if 1765 // the Conn implements io.ReaderFrom, it can take advantage of optimizations 1766 // such as sendfile. 1767 func (w persistConnWriter) ReadFrom(r io.Reader) (n int64, err error) { 1768 n, err = io.Copy(w.pc.conn, r) 1769 w.pc.nwrite += n 1770 return 1771 } 1772 1773 var _ io.ReaderFrom = (*persistConnWriter)(nil) 1774 1775 // connectMethod is the map key (in its String form) for keeping persistent 1776 // TCP connections alive for subsequent HTTP requests. 1777 // 1778 // A connect method may be of the following types: 1779 // 1780 // connectMethod.key().String() Description 1781 // ------------------------------ ------------------------- 1782 // |http|foo.com http directly to server, no proxy 1783 // |https|foo.com https directly to server, no proxy 1784 // |https,h1|foo.com https directly to server w/o HTTP/2, no proxy 1785 // http://proxy.com|https|foo.com http to proxy, then CONNECT to foo.com 1786 // http://proxy.com|http http to proxy, http to anywhere after that 1787 // socks5://proxy.com|http|foo.com socks5 to proxy, then http to foo.com 1788 // socks5://proxy.com|https|foo.com socks5 to proxy, then https to foo.com 1789 // https://proxy.com|https|foo.com https to proxy, then CONNECT to foo.com 1790 // https://proxy.com|http https to proxy, http to anywhere after that 1791 // 1792 type connectMethod struct { 1793 _ incomparable 1794 proxyURL *url.URL // nil for no proxy, else full proxy URL 1795 targetScheme string // "http" or "https" 1796 // If proxyURL specifies an http or https proxy, and targetScheme is http (not https), 1797 // then targetAddr is not included in the connect method key, because the socket can 1798 // be reused for different targetAddr values. 1799 targetAddr string 1800 onlyH1 bool // whether to disable HTTP/2 and force HTTP/1 1801 } 1802 1803 func (cm *connectMethod) key() connectMethodKey { 1804 proxyStr := "" 1805 targetAddr := cm.targetAddr 1806 if cm.proxyURL != nil { 1807 proxyStr = cm.proxyURL.String() 1808 if (cm.proxyURL.Scheme == "http" || cm.proxyURL.Scheme == "https") && cm.targetScheme == "http" { 1809 targetAddr = "" 1810 } 1811 } 1812 return connectMethodKey{ 1813 proxy: proxyStr, 1814 scheme: cm.targetScheme, 1815 addr: targetAddr, 1816 onlyH1: cm.onlyH1, 1817 } 1818 } 1819 1820 // scheme returns the first hop scheme: http, https, or socks5 1821 func (cm *connectMethod) scheme() string { 1822 if cm.proxyURL != nil { 1823 return cm.proxyURL.Scheme 1824 } 1825 return cm.targetScheme 1826 } 1827 1828 // addr returns the first hop "host:port" to which we need to TCP connect. 1829 func (cm *connectMethod) addr() string { 1830 if cm.proxyURL != nil { 1831 return canonicalAddr(cm.proxyURL) 1832 } 1833 return cm.targetAddr 1834 } 1835 1836 // tlsHost returns the host name to match against the peer's 1837 // TLS certificate. 1838 func (cm *connectMethod) tlsHost() string { 1839 h := cm.targetAddr 1840 if hasPort(h) { 1841 h = h[:strings.LastIndex(h, ":")] 1842 } 1843 return h 1844 } 1845 1846 // connectMethodKey is the map key version of connectMethod, with a 1847 // stringified proxy URL (or the empty string) instead of a pointer to 1848 // a URL. 1849 type connectMethodKey struct { 1850 proxy, scheme, addr string 1851 onlyH1 bool 1852 } 1853 1854 func (k connectMethodKey) String() string { 1855 // Only used by tests. 1856 var h1 string 1857 if k.onlyH1 { 1858 h1 = ",h1" 1859 } 1860 return fmt.Sprintf("%s|%s%s|%s", k.proxy, k.scheme, h1, k.addr) 1861 } 1862 1863 // persistConn wraps a connection, usually a persistent one 1864 // (but may be used for non-keep-alive requests as well) 1865 type persistConn struct { 1866 // alt optionally specifies the TLS NextProto RoundTripper. 1867 // This is used for HTTP/2 today and future protocols later. 1868 // If it's non-nil, the rest of the fields are unused. 1869 alt RoundTripper 1870 1871 t *Transport 1872 cacheKey connectMethodKey 1873 conn net.Conn 1874 tlsState *tls.ConnectionState 1875 br *bufio.Reader // from conn 1876 bw *bufio.Writer // to conn 1877 nwrite int64 // bytes written 1878 reqch chan requestAndChan // written by roundTrip; read by readLoop 1879 writech chan writeRequest // written by roundTrip; read by writeLoop 1880 closech chan struct{} // closed when conn closed 1881 isProxy bool 1882 sawEOF bool // whether we've seen EOF from conn; owned by readLoop 1883 readLimit int64 // bytes allowed to be read; owned by readLoop 1884 // writeErrCh passes the request write error (usually nil) 1885 // from the writeLoop goroutine to the readLoop which passes 1886 // it off to the res.Body reader, which then uses it to decide 1887 // whether or not a connection can be reused. Issue 7569. 1888 writeErrCh chan error 1889 1890 writeLoopDone chan struct{} // closed when write loop ends 1891 1892 // Both guarded by Transport.idleMu: 1893 idleAt time.Time // time it last become idle 1894 idleTimer *time.Timer // holding an AfterFunc to close it 1895 1896 mu sync.Mutex // guards following fields 1897 numExpectedResponses int 1898 closed error // set non-nil when conn is closed, before closech is closed 1899 canceledErr error // set non-nil if conn is canceled 1900 broken bool // an error has happened on this connection; marked broken so it's not reused. 1901 reused bool // whether conn has had successful request/response and is being reused. 1902 // mutateHeaderFunc is an optional func to modify extra 1903 // headers on each outbound request before it's written. (the 1904 // original Request given to RoundTrip is not modified) 1905 mutateHeaderFunc func(Header) 1906 } 1907 1908 func (pc *persistConn) maxHeaderResponseSize() int64 { 1909 if v := pc.t.MaxResponseHeaderBytes; v != 0 { 1910 return v 1911 } 1912 return 10 << 20 // conservative default; same as http2 1913 } 1914 1915 func (pc *persistConn) Read(p []byte) (n int, err error) { 1916 if pc.readLimit <= 0 { 1917 return 0, fmt.Errorf("read limit of %d bytes exhausted", pc.maxHeaderResponseSize()) 1918 } 1919 if int64(len(p)) > pc.readLimit { 1920 p = p[:pc.readLimit] 1921 } 1922 n, err = pc.conn.Read(p) 1923 if err == io.EOF { 1924 pc.sawEOF = true 1925 } 1926 pc.readLimit -= int64(n) 1927 return 1928 } 1929 1930 // isBroken reports whether this connection is in a known broken state. 1931 func (pc *persistConn) isBroken() bool { 1932 pc.mu.Lock() 1933 b := pc.closed != nil 1934 pc.mu.Unlock() 1935 return b 1936 } 1937 1938 // canceled returns non-nil if the connection was closed due to 1939 // CancelRequest or due to context cancellation. 1940 func (pc *persistConn) canceled() error { 1941 pc.mu.Lock() 1942 defer pc.mu.Unlock() 1943 return pc.canceledErr 1944 } 1945 1946 // isReused reports whether this connection has been used before. 1947 func (pc *persistConn) isReused() bool { 1948 pc.mu.Lock() 1949 r := pc.reused 1950 pc.mu.Unlock() 1951 return r 1952 } 1953 1954 func (pc *persistConn) gotIdleConnTrace(idleAt time.Time) (t httptrace.GotConnInfo) { 1955 pc.mu.Lock() 1956 defer pc.mu.Unlock() 1957 t.Reused = pc.reused 1958 t.Conn = pc.conn 1959 t.WasIdle = true 1960 if !idleAt.IsZero() { 1961 t.IdleTime = time.Since(idleAt) 1962 } 1963 return 1964 } 1965 1966 func (pc *persistConn) cancelRequest(err error) { 1967 pc.mu.Lock() 1968 defer pc.mu.Unlock() 1969 pc.canceledErr = err 1970 pc.closeLocked(errRequestCanceled) 1971 } 1972 1973 // closeConnIfStillIdle closes the connection if it's still sitting idle. 1974 // This is what's called by the persistConn's idleTimer, and is run in its 1975 // own goroutine. 1976 func (pc *persistConn) closeConnIfStillIdle() { 1977 t := pc.t 1978 t.idleMu.Lock() 1979 defer t.idleMu.Unlock() 1980 if _, ok := t.idleLRU.m[pc]; !ok { 1981 // Not idle. 1982 return 1983 } 1984 t.removeIdleConnLocked(pc) 1985 pc.close(errIdleConnTimeout) 1986 } 1987 1988 // mapRoundTripError returns the appropriate error value for 1989 // persistConn.roundTrip. 1990 // 1991 // The provided err is the first error that (*persistConn).roundTrip 1992 // happened to receive from its select statement. 1993 // 1994 // The startBytesWritten value should be the value of pc.nwrite before the roundTrip 1995 // started writing the request. 1996 func (pc *persistConn) mapRoundTripError(req *transportRequest, startBytesWritten int64, err error) error { 1997 if err == nil { 1998 return nil 1999 } 2000 2001 // Wait for the writeLoop goroutine to terminate to avoid data 2002 // races on callers who mutate the request on failure. 2003 // 2004 // When resc in pc.roundTrip and hence rc.ch receives a responseAndError 2005 // with a non-nil error it implies that the persistConn is either closed 2006 // or closing. Waiting on pc.writeLoopDone is hence safe as all callers 2007 // close closech which in turn ensures writeLoop returns. 2008 <-pc.writeLoopDone 2009 2010 // If the request was canceled, that's better than network 2011 // failures that were likely the result of tearing down the 2012 // connection. 2013 if cerr := pc.canceled(); cerr != nil { 2014 return cerr 2015 } 2016 2017 // See if an error was set explicitly. 2018 req.mu.Lock() 2019 reqErr := req.err 2020 req.mu.Unlock() 2021 if reqErr != nil { 2022 return reqErr 2023 } 2024 2025 if err == errServerClosedIdle { 2026 // Don't decorate 2027 return err 2028 } 2029 2030 if _, ok := err.(transportReadFromServerError); ok { 2031 // Don't decorate 2032 return err 2033 } 2034 if pc.isBroken() { 2035 if pc.nwrite == startBytesWritten { 2036 return nothingWrittenError{err} 2037 } 2038 return fmt.Errorf("net/http: HTTP/1.x transport connection broken: %v", err) 2039 } 2040 return err 2041 } 2042 2043 // errCallerOwnsConn is an internal sentinel error used when we hand 2044 // off a writable response.Body to the caller. We use this to prevent 2045 // closing a net.Conn that is now owned by the caller. 2046 var errCallerOwnsConn = errors.New("read loop ending; caller owns writable underlying conn") 2047 2048 func (pc *persistConn) readLoop() { 2049 closeErr := errReadLoopExiting // default value, if not changed below 2050 defer func() { 2051 pc.close(closeErr) 2052 pc.t.removeIdleConn(pc) 2053 }() 2054 2055 tryPutIdleConn := func(trace *httptrace.ClientTrace) bool { 2056 if err := pc.t.tryPutIdleConn(pc); err != nil { 2057 closeErr = err 2058 if trace != nil && trace.PutIdleConn != nil && err != errKeepAlivesDisabled { 2059 trace.PutIdleConn(err) 2060 } 2061 return false 2062 } 2063 if trace != nil && trace.PutIdleConn != nil { 2064 trace.PutIdleConn(nil) 2065 } 2066 return true 2067 } 2068 2069 // eofc is used to block caller goroutines reading from Response.Body 2070 // at EOF until this goroutines has (potentially) added the connection 2071 // back to the idle pool. 2072 eofc := make(chan struct{}) 2073 defer close(eofc) // unblock reader on errors 2074 2075 // Read this once, before loop starts. (to avoid races in tests) 2076 testHookMu.Lock() 2077 testHookReadLoopBeforeNextRead := testHookReadLoopBeforeNextRead 2078 testHookMu.Unlock() 2079 2080 alive := true 2081 for alive { 2082 pc.readLimit = pc.maxHeaderResponseSize() 2083 _, err := pc.br.Peek(1) 2084 2085 pc.mu.Lock() 2086 if pc.numExpectedResponses == 0 { 2087 pc.readLoopPeekFailLocked(err) 2088 pc.mu.Unlock() 2089 return 2090 } 2091 pc.mu.Unlock() 2092 2093 rc := <-pc.reqch 2094 trace := httptrace.ContextClientTrace(rc.req.Context()) 2095 2096 var resp *Response 2097 if err == nil { 2098 resp, err = pc.readResponse(rc, trace) 2099 } else { 2100 err = transportReadFromServerError{err} 2101 closeErr = err 2102 } 2103 2104 if err != nil { 2105 if pc.readLimit <= 0 { 2106 err = fmt.Errorf("net/http: server response headers exceeded %d bytes; aborted", pc.maxHeaderResponseSize()) 2107 } 2108 2109 select { 2110 case rc.ch <- responseAndError{err: err}: 2111 case <-rc.callerGone: 2112 return 2113 } 2114 return 2115 } 2116 pc.readLimit = maxInt64 // effectively no limit for response bodies 2117 2118 pc.mu.Lock() 2119 pc.numExpectedResponses-- 2120 pc.mu.Unlock() 2121 2122 bodyWritable := resp.bodyIsWritable() 2123 hasBody := rc.req.Method != "HEAD" && resp.ContentLength != 0 2124 2125 if resp.Close || rc.req.Close || resp.StatusCode <= 199 || bodyWritable { 2126 // Don't do keep-alive on error if either party requested a close 2127 // or we get an unexpected informational (1xx) response. 2128 // StatusCode 100 is already handled above. 2129 alive = false 2130 } 2131 2132 if !hasBody || bodyWritable { 2133 replaced := pc.t.replaceReqCanceler(rc.cancelKey, nil) 2134 2135 // Put the idle conn back into the pool before we send the response 2136 // so if they process it quickly and make another request, they'll 2137 // get this same conn. But we use the unbuffered channel 'rc' 2138 // to guarantee that persistConn.roundTrip got out of its select 2139 // potentially waiting for this persistConn to close. 2140 alive = alive && 2141 !pc.sawEOF && 2142 pc.wroteRequest() && 2143 replaced && tryPutIdleConn(trace) 2144 2145 if bodyWritable { 2146 closeErr = errCallerOwnsConn 2147 } 2148 2149 select { 2150 case rc.ch <- responseAndError{res: resp}: 2151 case <-rc.callerGone: 2152 return 2153 } 2154 2155 // Now that they've read from the unbuffered channel, they're safely 2156 // out of the select that also waits on this goroutine to die, so 2157 // we're allowed to exit now if needed (if alive is false) 2158 testHookReadLoopBeforeNextRead() 2159 continue 2160 } 2161 2162 waitForBodyRead := make(chan bool, 2) 2163 body := &bodyEOFSignal{ 2164 body: resp.Body, 2165 earlyCloseFn: func() error { 2166 waitForBodyRead <- false 2167 <-eofc // will be closed by deferred call at the end of the function 2168 return nil 2169 2170 }, 2171 fn: func(err error) error { 2172 isEOF := err == io.EOF 2173 waitForBodyRead <- isEOF 2174 if isEOF { 2175 <-eofc // see comment above eofc declaration 2176 } else if err != nil { 2177 if cerr := pc.canceled(); cerr != nil { 2178 return cerr 2179 } 2180 } 2181 return err 2182 }, 2183 } 2184 2185 resp.Body = body 2186 if rc.addedGzip && strings.EqualFold(resp.Header.Get("Content-Encoding"), "gzip") { 2187 resp.Body = &gzipReader{body: body} 2188 resp.Header.Del("Content-Encoding") 2189 resp.Header.Del("Content-Length") 2190 resp.ContentLength = -1 2191 resp.Uncompressed = true 2192 } 2193 2194 select { 2195 case rc.ch <- responseAndError{res: resp}: 2196 case <-rc.callerGone: 2197 return 2198 } 2199 2200 // Before looping back to the top of this function and peeking on 2201 // the bufio.Reader, wait for the caller goroutine to finish 2202 // reading the response body. (or for cancellation or death) 2203 select { 2204 case bodyEOF := <-waitForBodyRead: 2205 replaced := pc.t.replaceReqCanceler(rc.cancelKey, nil) // before pc might return to idle pool 2206 alive = alive && 2207 bodyEOF && 2208 !pc.sawEOF && 2209 pc.wroteRequest() && 2210 replaced && tryPutIdleConn(trace) 2211 if bodyEOF { 2212 eofc <- struct{}{} 2213 } 2214 case <-rc.req.Cancel: 2215 alive = false 2216 pc.t.CancelRequest(rc.req) 2217 case <-rc.req.Context().Done(): 2218 alive = false 2219 pc.t.cancelRequest(rc.cancelKey, rc.req.Context().Err()) 2220 case <-pc.closech: 2221 alive = false 2222 } 2223 2224 testHookReadLoopBeforeNextRead() 2225 } 2226 } 2227 2228 func (pc *persistConn) readLoopPeekFailLocked(peekErr error) { 2229 if pc.closed != nil { 2230 return 2231 } 2232 if n := pc.br.Buffered(); n > 0 { 2233 buf, _ := pc.br.Peek(n) 2234 if is408Message(buf) { 2235 pc.closeLocked(errServerClosedIdle) 2236 return 2237 } else { 2238 log.Printf("Unsolicited response received on idle HTTP channel starting with %q; err=%v", buf, peekErr) 2239 } 2240 } 2241 if peekErr == io.EOF { 2242 // common case. 2243 pc.closeLocked(errServerClosedIdle) 2244 } else { 2245 pc.closeLocked(fmt.Errorf("readLoopPeekFailLocked: %v", peekErr)) 2246 } 2247 } 2248 2249 // is408Message reports whether buf has the prefix of an 2250 // HTTP 408 Request Timeout response. 2251 // See golang.org/issue/32310. 2252 func is408Message(buf []byte) bool { 2253 if len(buf) < len("HTTP/1.x 408") { 2254 return false 2255 } 2256 if string(buf[:7]) != "HTTP/1." { 2257 return false 2258 } 2259 return string(buf[8:12]) == " 408" 2260 } 2261 2262 // readResponse reads an HTTP response (or two, in the case of "Expect: 2263 // 100-continue") from the server. It returns the final non-100 one. 2264 // trace is optional. 2265 func (pc *persistConn) readResponse(rc requestAndChan, trace *httptrace.ClientTrace) (resp *Response, err error) { 2266 if trace != nil && trace.GotFirstResponseByte != nil { 2267 if peek, err := pc.br.Peek(1); err == nil && len(peek) == 1 { 2268 trace.GotFirstResponseByte() 2269 } 2270 } 2271 num1xx := 0 // number of informational 1xx headers received 2272 const max1xxResponses = 5 // arbitrary bound on number of informational responses 2273 2274 continueCh := rc.continueCh 2275 for { 2276 resp, err = ReadResponse(pc.br, rc.req) 2277 if err != nil { 2278 return 2279 } 2280 resCode := resp.StatusCode 2281 if continueCh != nil { 2282 if resCode == 100 { 2283 if trace != nil && trace.Got100Continue != nil { 2284 trace.Got100Continue() 2285 } 2286 continueCh <- struct{}{} 2287 continueCh = nil 2288 } else if resCode >= 200 { 2289 close(continueCh) 2290 continueCh = nil 2291 } 2292 } 2293 is1xx := 100 <= resCode && resCode <= 199 2294 // treat 101 as a terminal status, see issue 26161 2295 is1xxNonTerminal := is1xx && resCode != StatusSwitchingProtocols 2296 if is1xxNonTerminal { 2297 num1xx++ 2298 if num1xx > max1xxResponses { 2299 return nil, errors.New("net/http: too many 1xx informational responses") 2300 } 2301 pc.readLimit = pc.maxHeaderResponseSize() // reset the limit 2302 if trace != nil && trace.Got1xxResponse != nil { 2303 if err := trace.Got1xxResponse(resCode, textproto.MIMEHeader(resp.Header)); err != nil { 2304 return nil, err 2305 } 2306 } 2307 continue 2308 } 2309 break 2310 } 2311 if resp.isProtocolSwitch() { 2312 resp.Body = newReadWriteCloserBody(pc.br, pc.conn) 2313 } 2314 2315 resp.TLS = pc.tlsState 2316 return 2317 } 2318 2319 // waitForContinue returns the function to block until 2320 // any response, timeout or connection close. After any of them, 2321 // the function returns a bool which indicates if the body should be sent. 2322 func (pc *persistConn) waitForContinue(continueCh <-chan struct{}) func() bool { 2323 if continueCh == nil { 2324 return nil 2325 } 2326 return func() bool { 2327 timer := time.NewTimer(pc.t.ExpectContinueTimeout) 2328 defer timer.Stop() 2329 2330 select { 2331 case _, ok := <-continueCh: 2332 return ok 2333 case <-timer.C: 2334 return true 2335 case <-pc.closech: 2336 return false 2337 } 2338 } 2339 } 2340 2341 func newReadWriteCloserBody(br *bufio.Reader, rwc io.ReadWriteCloser) io.ReadWriteCloser { 2342 body := &readWriteCloserBody{ReadWriteCloser: rwc} 2343 if br.Buffered() != 0 { 2344 body.br = br 2345 } 2346 return body 2347 } 2348 2349 // readWriteCloserBody is the Response.Body type used when we want to 2350 // give users write access to the Body through the underlying 2351 // connection (TCP, unless using custom dialers). This is then 2352 // the concrete type for a Response.Body on the 101 Switching 2353 // Protocols response, as used by WebSockets, h2c, etc. 2354 type readWriteCloserBody struct { 2355 _ incomparable 2356 br *bufio.Reader // used until empty 2357 io.ReadWriteCloser 2358 } 2359 2360 func (b *readWriteCloserBody) Read(p []byte) (n int, err error) { 2361 if b.br != nil { 2362 if n := b.br.Buffered(); len(p) > n { 2363 p = p[:n] 2364 } 2365 n, err = b.br.Read(p) 2366 if b.br.Buffered() == 0 { 2367 b.br = nil 2368 } 2369 return n, err 2370 } 2371 return b.ReadWriteCloser.Read(p) 2372 } 2373 2374 // nothingWrittenError wraps a write errors which ended up writing zero bytes. 2375 type nothingWrittenError struct { 2376 error 2377 } 2378 2379 func (pc *persistConn) writeLoop() { 2380 defer close(pc.writeLoopDone) 2381 for { 2382 select { 2383 case wr := <-pc.writech: 2384 startBytesWritten := pc.nwrite 2385 err := wr.req.Request.write(pc.bw, pc.isProxy, wr.req.extra, pc.waitForContinue(wr.continueCh)) 2386 if bre, ok := err.(requestBodyReadError); ok { 2387 err = bre.error 2388 // Errors reading from the user's 2389 // Request.Body are high priority. 2390 // Set it here before sending on the 2391 // channels below or calling 2392 // pc.close() which tears down 2393 // connections and causes other 2394 // errors. 2395 wr.req.setError(err) 2396 } 2397 if err == nil { 2398 err = pc.bw.Flush() 2399 } 2400 if err != nil { 2401 if pc.nwrite == startBytesWritten { 2402 err = nothingWrittenError{err} 2403 } 2404 } 2405 pc.writeErrCh <- err // to the body reader, which might recycle us 2406 wr.ch <- err // to the roundTrip function 2407 if err != nil { 2408 pc.close(err) 2409 return 2410 } 2411 case <-pc.closech: 2412 return 2413 } 2414 } 2415 } 2416 2417 // maxWriteWaitBeforeConnReuse is how long the a Transport RoundTrip 2418 // will wait to see the Request's Body.Write result after getting a 2419 // response from the server. See comments in (*persistConn).wroteRequest. 2420 const maxWriteWaitBeforeConnReuse = 50 * time.Millisecond 2421 2422 // wroteRequest is a check before recycling a connection that the previous write 2423 // (from writeLoop above) happened and was successful. 2424 func (pc *persistConn) wroteRequest() bool { 2425 select { 2426 case err := <-pc.writeErrCh: 2427 // Common case: the write happened well before the response, so 2428 // avoid creating a timer. 2429 return err == nil 2430 default: 2431 // Rare case: the request was written in writeLoop above but 2432 // before it could send to pc.writeErrCh, the reader read it 2433 // all, processed it, and called us here. In this case, give the 2434 // write goroutine a bit of time to finish its send. 2435 // 2436 // Less rare case: We also get here in the legitimate case of 2437 // Issue 7569, where the writer is still writing (or stalled), 2438 // but the server has already replied. In this case, we don't 2439 // want to wait too long, and we want to return false so this 2440 // connection isn't re-used. 2441 t := time.NewTimer(maxWriteWaitBeforeConnReuse) 2442 defer t.Stop() 2443 select { 2444 case err := <-pc.writeErrCh: 2445 return err == nil 2446 case <-t.C: 2447 return false 2448 } 2449 } 2450 } 2451 2452 // responseAndError is how the goroutine reading from an HTTP/1 server 2453 // communicates with the goroutine doing the RoundTrip. 2454 type responseAndError struct { 2455 _ incomparable 2456 res *Response // else use this response (see res method) 2457 err error 2458 } 2459 2460 type requestAndChan struct { 2461 _ incomparable 2462 req *Request 2463 cancelKey cancelKey 2464 ch chan responseAndError // unbuffered; always send in select on callerGone 2465 2466 // whether the Transport (as opposed to the user client code) 2467 // added the Accept-Encoding gzip header. If the Transport 2468 // set it, only then do we transparently decode the gzip. 2469 addedGzip bool 2470 2471 // Optional blocking chan for Expect: 100-continue (for send). 2472 // If the request has an "Expect: 100-continue" header and 2473 // the server responds 100 Continue, readLoop send a value 2474 // to writeLoop via this chan. 2475 continueCh chan<- struct{} 2476 2477 callerGone <-chan struct{} // closed when roundTrip caller has returned 2478 } 2479 2480 // A writeRequest is sent by the readLoop's goroutine to the 2481 // writeLoop's goroutine to write a request while the read loop 2482 // concurrently waits on both the write response and the server's 2483 // reply. 2484 type writeRequest struct { 2485 req *transportRequest 2486 ch chan<- error 2487 2488 // Optional blocking chan for Expect: 100-continue (for receive). 2489 // If not nil, writeLoop blocks sending request body until 2490 // it receives from this chan. 2491 continueCh <-chan struct{} 2492 } 2493 2494 type httpError struct { 2495 err string 2496 timeout bool 2497 } 2498 2499 func (e *httpError) Error() string { return e.err } 2500 func (e *httpError) Timeout() bool { return e.timeout } 2501 func (e *httpError) Temporary() bool { return true } 2502 2503 var errTimeout error = &httpError{err: "net/http: timeout awaiting response headers", timeout: true} 2504 2505 // errRequestCanceled is set to be identical to the one from h2 to facilitate 2506 // testing. 2507 var errRequestCanceled = http2errRequestCanceled 2508 var errRequestCanceledConn = errors.New("net/http: request canceled while waiting for connection") // TODO: unify? 2509 2510 func nop() {} 2511 2512 // testHooks. Always non-nil. 2513 var ( 2514 testHookEnterRoundTrip = nop 2515 testHookWaitResLoop = nop 2516 testHookRoundTripRetried = nop 2517 testHookPrePendingDial = nop 2518 testHookPostPendingDial = nop 2519 2520 testHookMu sync.Locker = fakeLocker{} // guards following 2521 testHookReadLoopBeforeNextRead = nop 2522 ) 2523 2524 func (pc *persistConn) roundTrip(req *transportRequest) (resp *Response, err error) { 2525 testHookEnterRoundTrip() 2526 if !pc.t.replaceReqCanceler(req.cancelKey, pc.cancelRequest) { 2527 pc.t.putOrCloseIdleConn(pc) 2528 return nil, errRequestCanceled 2529 } 2530 pc.mu.Lock() 2531 pc.numExpectedResponses++ 2532 headerFn := pc.mutateHeaderFunc 2533 pc.mu.Unlock() 2534 2535 if headerFn != nil { 2536 headerFn(req.extraHeaders()) 2537 } 2538 2539 // Ask for a compressed version if the caller didn't set their 2540 // own value for Accept-Encoding. We only attempt to 2541 // uncompress the gzip stream if we were the layer that 2542 // requested it. 2543 requestedGzip := false 2544 if !pc.t.DisableCompression && 2545 req.Header.Get("Accept-Encoding") == "" && 2546 req.Header.Get("Range") == "" && 2547 req.Method != "HEAD" { 2548 // Request gzip only, not deflate. Deflate is ambiguous and 2549 // not as universally supported anyway. 2550 // See: https://zlib.net/zlib_faq.html#faq39 2551 // 2552 // Note that we don't request this for HEAD requests, 2553 // due to a bug in nginx: 2554 // https://trac.nginx.org/nginx/ticket/358 2555 // https://golang.org/issue/5522 2556 // 2557 // We don't request gzip if the request is for a range, since 2558 // auto-decoding a portion of a gzipped document will just fail 2559 // anyway. See https://golang.org/issue/8923 2560 requestedGzip = true 2561 req.extraHeaders().Set("Accept-Encoding", "gzip") 2562 } 2563 2564 var continueCh chan struct{} 2565 if req.ProtoAtLeast(1, 1) && req.Body != nil && req.expectsContinue() { 2566 continueCh = make(chan struct{}, 1) 2567 } 2568 2569 if pc.t.DisableKeepAlives && 2570 !req.wantsClose() && 2571 !isProtocolSwitchHeader(req.Header) { 2572 req.extraHeaders().Set("Connection", "close") 2573 } 2574 2575 gone := make(chan struct{}) 2576 defer close(gone) 2577 2578 defer func() { 2579 if err != nil { 2580 pc.t.setReqCanceler(req.cancelKey, nil) 2581 } 2582 }() 2583 2584 const debugRoundTrip = false 2585 2586 // Write the request concurrently with waiting for a response, 2587 // in case the server decides to reply before reading our full 2588 // request body. 2589 startBytesWritten := pc.nwrite 2590 writeErrCh := make(chan error, 1) 2591 pc.writech <- writeRequest{req, writeErrCh, continueCh} 2592 2593 resc := make(chan responseAndError) 2594 pc.reqch <- requestAndChan{ 2595 req: req.Request, 2596 cancelKey: req.cancelKey, 2597 ch: resc, 2598 addedGzip: requestedGzip, 2599 continueCh: continueCh, 2600 callerGone: gone, 2601 } 2602 2603 var respHeaderTimer <-chan time.Time 2604 cancelChan := req.Request.Cancel 2605 ctxDoneChan := req.Context().Done() 2606 pcClosed := pc.closech 2607 canceled := false 2608 for { 2609 testHookWaitResLoop() 2610 select { 2611 case err := <-writeErrCh: 2612 if debugRoundTrip { 2613 req.logf("writeErrCh resv: %T/%#v", err, err) 2614 } 2615 if err != nil { 2616 pc.close(fmt.Errorf("write error: %v", err)) 2617 return nil, pc.mapRoundTripError(req, startBytesWritten, err) 2618 } 2619 if d := pc.t.ResponseHeaderTimeout; d > 0 { 2620 if debugRoundTrip { 2621 req.logf("starting timer for %v", d) 2622 } 2623 timer := time.NewTimer(d) 2624 defer timer.Stop() // prevent leaks 2625 respHeaderTimer = timer.C 2626 } 2627 case <-pcClosed: 2628 pcClosed = nil 2629 if canceled || pc.t.replaceReqCanceler(req.cancelKey, nil) { 2630 if debugRoundTrip { 2631 req.logf("closech recv: %T %#v", pc.closed, pc.closed) 2632 } 2633 return nil, pc.mapRoundTripError(req, startBytesWritten, pc.closed) 2634 } 2635 case <-respHeaderTimer: 2636 if debugRoundTrip { 2637 req.logf("timeout waiting for response headers.") 2638 } 2639 pc.close(errTimeout) 2640 return nil, errTimeout 2641 case re := <-resc: 2642 if (re.res == nil) == (re.err == nil) { 2643 panic(fmt.Sprintf("internal error: exactly one of res or err should be set; nil=%v", re.res == nil)) 2644 } 2645 if debugRoundTrip { 2646 req.logf("resc recv: %p, %T/%#v", re.res, re.err, re.err) 2647 } 2648 if re.err != nil { 2649 return nil, pc.mapRoundTripError(req, startBytesWritten, re.err) 2650 } 2651 return re.res, nil 2652 case <-cancelChan: 2653 canceled = pc.t.cancelRequest(req.cancelKey, errRequestCanceled) 2654 cancelChan = nil 2655 case <-ctxDoneChan: 2656 canceled = pc.t.cancelRequest(req.cancelKey, req.Context().Err()) 2657 cancelChan = nil 2658 ctxDoneChan = nil 2659 } 2660 } 2661 } 2662 2663 // tLogKey is a context WithValue key for test debugging contexts containing 2664 // a t.Logf func. See export_test.go's Request.WithT method. 2665 type tLogKey struct{} 2666 2667 func (tr *transportRequest) logf(format string, args ...interface{}) { 2668 if logf, ok := tr.Request.Context().Value(tLogKey{}).(func(string, ...interface{})); ok { 2669 logf(time.Now().Format(time.RFC3339Nano)+": "+format, args...) 2670 } 2671 } 2672 2673 // markReused marks this connection as having been successfully used for a 2674 // request and response. 2675 func (pc *persistConn) markReused() { 2676 pc.mu.Lock() 2677 pc.reused = true 2678 pc.mu.Unlock() 2679 } 2680 2681 // close closes the underlying TCP connection and closes 2682 // the pc.closech channel. 2683 // 2684 // The provided err is only for testing and debugging; in normal 2685 // circumstances it should never be seen by users. 2686 func (pc *persistConn) close(err error) { 2687 pc.mu.Lock() 2688 defer pc.mu.Unlock() 2689 pc.closeLocked(err) 2690 } 2691 2692 func (pc *persistConn) closeLocked(err error) { 2693 if err == nil { 2694 panic("nil error") 2695 } 2696 pc.broken = true 2697 if pc.closed == nil { 2698 pc.closed = err 2699 pc.t.decConnsPerHost(pc.cacheKey) 2700 // Close HTTP/1 (pc.alt == nil) connection. 2701 // HTTP/2 closes its connection itself. 2702 if pc.alt == nil { 2703 if err != errCallerOwnsConn { 2704 pc.conn.Close() 2705 } 2706 close(pc.closech) 2707 } 2708 } 2709 pc.mutateHeaderFunc = nil 2710 } 2711 2712 var portMap = map[string]string{ 2713 "http": "80", 2714 "https": "443", 2715 "socks5": "1080", 2716 } 2717 2718 // canonicalAddr returns url.Host but always with a ":port" suffix 2719 func canonicalAddr(url *url.URL) string { 2720 addr := url.Hostname() 2721 if v, err := idnaASCII(addr); err == nil { 2722 addr = v 2723 } 2724 port := url.Port() 2725 if port == "" { 2726 port = portMap[url.Scheme] 2727 } 2728 return net.JoinHostPort(addr, port) 2729 } 2730 2731 // bodyEOFSignal is used by the HTTP/1 transport when reading response 2732 // bodies to make sure we see the end of a response body before 2733 // proceeding and reading on the connection again. 2734 // 2735 // It wraps a ReadCloser but runs fn (if non-nil) at most 2736 // once, right before its final (error-producing) Read or Close call 2737 // returns. fn should return the new error to return from Read or Close. 2738 // 2739 // If earlyCloseFn is non-nil and Close is called before io.EOF is 2740 // seen, earlyCloseFn is called instead of fn, and its return value is 2741 // the return value from Close. 2742 type bodyEOFSignal struct { 2743 body io.ReadCloser 2744 mu sync.Mutex // guards following 4 fields 2745 closed bool // whether Close has been called 2746 rerr error // sticky Read error 2747 fn func(error) error // err will be nil on Read io.EOF 2748 earlyCloseFn func() error // optional alt Close func used if io.EOF not seen 2749 } 2750 2751 var errReadOnClosedResBody = errors.New("http: read on closed response body") 2752 2753 func (es *bodyEOFSignal) Read(p []byte) (n int, err error) { 2754 es.mu.Lock() 2755 closed, rerr := es.closed, es.rerr 2756 es.mu.Unlock() 2757 if closed { 2758 return 0, errReadOnClosedResBody 2759 } 2760 if rerr != nil { 2761 return 0, rerr 2762 } 2763 2764 n, err = es.body.Read(p) 2765 if err != nil { 2766 es.mu.Lock() 2767 defer es.mu.Unlock() 2768 if es.rerr == nil { 2769 es.rerr = err 2770 } 2771 err = es.condfn(err) 2772 } 2773 return 2774 } 2775 2776 func (es *bodyEOFSignal) Close() error { 2777 es.mu.Lock() 2778 defer es.mu.Unlock() 2779 if es.closed { 2780 return nil 2781 } 2782 es.closed = true 2783 if es.earlyCloseFn != nil && es.rerr != io.EOF { 2784 return es.earlyCloseFn() 2785 } 2786 err := es.body.Close() 2787 return es.condfn(err) 2788 } 2789 2790 // caller must hold es.mu. 2791 func (es *bodyEOFSignal) condfn(err error) error { 2792 if es.fn == nil { 2793 return err 2794 } 2795 err = es.fn(err) 2796 es.fn = nil 2797 return err 2798 } 2799 2800 // gzipReader wraps a response body so it can lazily 2801 // call gzip.NewReader on the first call to Read 2802 type gzipReader struct { 2803 _ incomparable 2804 body *bodyEOFSignal // underlying HTTP/1 response body framing 2805 zr *gzip.Reader // lazily-initialized gzip reader 2806 zerr error // any error from gzip.NewReader; sticky 2807 } 2808 2809 func (gz *gzipReader) Read(p []byte) (n int, err error) { 2810 if gz.zr == nil { 2811 if gz.zerr == nil { 2812 gz.zr, gz.zerr = gzip.NewReader(gz.body) 2813 } 2814 if gz.zerr != nil { 2815 return 0, gz.zerr 2816 } 2817 } 2818 2819 gz.body.mu.Lock() 2820 if gz.body.closed { 2821 err = errReadOnClosedResBody 2822 } 2823 gz.body.mu.Unlock() 2824 2825 if err != nil { 2826 return 0, err 2827 } 2828 return gz.zr.Read(p) 2829 } 2830 2831 func (gz *gzipReader) Close() error { 2832 return gz.body.Close() 2833 } 2834 2835 type tlsHandshakeTimeoutError struct{} 2836 2837 func (tlsHandshakeTimeoutError) Timeout() bool { return true } 2838 func (tlsHandshakeTimeoutError) Temporary() bool { return true } 2839 func (tlsHandshakeTimeoutError) Error() string { return "net/http: TLS handshake timeout" } 2840 2841 // fakeLocker is a sync.Locker which does nothing. It's used to guard 2842 // test-only fields when not under test, to avoid runtime atomic 2843 // overhead. 2844 type fakeLocker struct{} 2845 2846 func (fakeLocker) Lock() {} 2847 func (fakeLocker) Unlock() {} 2848 2849 // cloneTLSConfig returns a shallow clone of cfg, or a new zero tls.Config if 2850 // cfg is nil. This is safe to call even if cfg is in active use by a TLS 2851 // client or server. 2852 func cloneTLSConfig(cfg *tls.Config) *tls.Config { 2853 if cfg == nil { 2854 return &tls.Config{} 2855 } 2856 return cfg.Clone() 2857 } 2858 2859 type connLRU struct { 2860 ll *list.List // list.Element.Value type is of *persistConn 2861 m map[*persistConn]*list.Element 2862 } 2863 2864 // add adds pc to the head of the linked list. 2865 func (cl *connLRU) add(pc *persistConn) { 2866 if cl.ll == nil { 2867 cl.ll = list.New() 2868 cl.m = make(map[*persistConn]*list.Element) 2869 } 2870 ele := cl.ll.PushFront(pc) 2871 if _, ok := cl.m[pc]; ok { 2872 panic("persistConn was already in LRU") 2873 } 2874 cl.m[pc] = ele 2875 } 2876 2877 func (cl *connLRU) removeOldest() *persistConn { 2878 ele := cl.ll.Back() 2879 pc := ele.Value.(*persistConn) 2880 cl.ll.Remove(ele) 2881 delete(cl.m, pc) 2882 return pc 2883 } 2884 2885 // remove removes pc from cl. 2886 func (cl *connLRU) remove(pc *persistConn) { 2887 if ele, ok := cl.m[pc]; ok { 2888 cl.ll.Remove(ele) 2889 delete(cl.m, pc) 2890 } 2891 } 2892 2893 // len returns the number of items in the cache. 2894 func (cl *connLRU) len() int { 2895 return len(cl.m) 2896 }