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

View as plain text