Source file src/net/http/transport.go

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

View as plain text