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