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