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

View as plain text