github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/src/net/http/transport.go (about)

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