Source file src/net/http/transport.go

Documentation: net/http

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

View as plain text