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

View as plain text