github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/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 "github.com/hxx258456/ccgo/gmhttp/httptrace" 32 "github.com/hxx258456/ccgo/gmhttp/internal/ascii" 33 tls "github.com/hxx258456/ccgo/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 // Deprecated: 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 // Deprecated: 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("github.com/hxx258456/ccgo/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("github.com/hxx258456/ccgo/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("github.com/hxx258456/ccgo/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("github.com/hxx258456/ccgo/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("github.com/hxx258456/ccgo/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 // Deprecated: 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("github.com/hxx258456/ccgo/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 list, ok := t.idleConn[w.key]; ok { 1032 stop := false 1033 delivered := false 1034 for len(list) > 0 && !stop { 1035 pconn := list[len(list)-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 list = list[:len(list)-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 list = list[:len(list)-1] 1066 } 1067 } 1068 stop = true 1069 } 1070 if len(list) > 0 { 1071 t.idleConn[w.key] = list 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("github.com/hxx258456/ccgo/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("github.com/hxx258456/ccgo/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("github.com/hxx258456/ccgo/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("github.com/hxx258456/ccgo/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 (pconn *persistConn) addTLS(ctx context.Context, name string, trace *httptrace.ClientTrace) error { 1514 // Initiate TLS and check remote host name against certificate. 1515 cfg := cloneTLSConfig(pconn.t.TLSClientConfig) 1516 if cfg.ServerName == "" { 1517 cfg.ServerName = name 1518 } 1519 if pconn.cacheKey.onlyH1 { 1520 cfg.NextProtos = nil 1521 } 1522 plainConn := pconn.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 := pconn.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 pconn.tlsState = &cs 1553 pconn.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 pconn.conn.Close() 1593 if trace != nil && trace.TLSHandshakeDone != nil { 1594 trace.TLSHandshakeDone(tls.ConnectionState{}, err) 1595 } 1596 return nil, err 1597 } 1598 cs := tc.ConnectionState() 1599 if trace != nil && trace.TLSHandshakeDone != nil { 1600 trace.TLSHandshakeDone(cs, nil) 1601 } 1602 pconn.tlsState = &cs 1603 } 1604 } else { 1605 conn, err := t.dial(ctx, "tcp", cm.addr()) 1606 if err != nil { 1607 return nil, wrapErr(err) 1608 } 1609 pconn.conn = conn 1610 if cm.scheme() == "https" { 1611 var firstTLSHost string 1612 if firstTLSHost, _, err = net.SplitHostPort(cm.addr()); err != nil { 1613 return nil, wrapErr(err) 1614 } 1615 if err = pconn.addTLS(ctx, firstTLSHost, trace); err != nil { 1616 return nil, wrapErr(err) 1617 } 1618 } 1619 } 1620 1621 // Proxy setup. 1622 switch { 1623 case cm.proxyURL == nil: 1624 // Do nothing. Not using a proxy. 1625 case cm.proxyURL.Scheme == "socks5": 1626 conn := pconn.conn 1627 d := socksNewDialer("tcp", conn.RemoteAddr().String()) 1628 if u := cm.proxyURL.User; u != nil { 1629 auth := &socksUsernamePassword{ 1630 Username: u.Username(), 1631 } 1632 auth.Password, _ = u.Password() 1633 d.AuthMethods = []socksAuthMethod{ 1634 socksAuthMethodNotRequired, 1635 socksAuthMethodUsernamePassword, 1636 } 1637 d.Authenticate = auth.Authenticate 1638 } 1639 if _, err := d.DialWithConn(ctx, conn, "tcp", cm.targetAddr); err != nil { 1640 conn.Close() 1641 return nil, err 1642 } 1643 case cm.targetScheme == "http": 1644 pconn.isProxy = true 1645 if pa := cm.proxyAuth(); pa != "" { 1646 pconn.mutateHeaderFunc = func(h Header) { 1647 h.Set("Proxy-Authorization", pa) 1648 } 1649 } 1650 case cm.targetScheme == "https": 1651 conn := pconn.conn 1652 var hdr Header 1653 if t.GetProxyConnectHeader != nil { 1654 var err error 1655 hdr, err = t.GetProxyConnectHeader(ctx, cm.proxyURL, cm.targetAddr) 1656 if err != nil { 1657 conn.Close() 1658 return nil, err 1659 } 1660 } else { 1661 hdr = t.ProxyConnectHeader 1662 } 1663 if hdr == nil { 1664 hdr = make(Header) 1665 } 1666 if pa := cm.proxyAuth(); pa != "" { 1667 hdr = hdr.Clone() 1668 hdr.Set("Proxy-Authorization", pa) 1669 } 1670 connectReq := &Request{ 1671 Method: "CONNECT", 1672 URL: &url.URL{Opaque: cm.targetAddr}, 1673 Host: cm.targetAddr, 1674 Header: hdr, 1675 } 1676 1677 // If there's no done channel (no deadline or cancellation 1678 // from the caller possible), at least set some (long) 1679 // timeout here. This will make sure we don't block forever 1680 // and leak a goroutine if the connection stops replying 1681 // after the TCP connect. 1682 connectCtx := ctx 1683 if ctx.Done() == nil { 1684 newCtx, cancel := context.WithTimeout(ctx, 1*time.Minute) 1685 defer cancel() 1686 connectCtx = newCtx 1687 } 1688 1689 didReadResponse := make(chan struct{}) // closed after CONNECT write+read is done or fails 1690 var ( 1691 resp *Response 1692 err error // write or read error 1693 ) 1694 // Write the CONNECT request & read the response. 1695 go func() { 1696 defer close(didReadResponse) 1697 err = connectReq.Write(conn) 1698 if err != nil { 1699 return 1700 } 1701 // Okay to use and discard buffered reader here, because 1702 // TLS server will not speak until spoken to. 1703 br := bufio.NewReader(conn) 1704 resp, err = ReadResponse(br, connectReq) 1705 }() 1706 select { 1707 case <-connectCtx.Done(): 1708 conn.Close() 1709 <-didReadResponse 1710 return nil, connectCtx.Err() 1711 case <-didReadResponse: 1712 // resp or err now set 1713 } 1714 if err != nil { 1715 conn.Close() 1716 return nil, err 1717 } 1718 if resp.StatusCode != 200 { 1719 f := strings.SplitN(resp.Status, " ", 2) 1720 conn.Close() 1721 if len(f) < 2 { 1722 return nil, errors.New("unknown status code") 1723 } 1724 return nil, errors.New(f[1]) 1725 } 1726 } 1727 1728 if cm.proxyURL != nil && cm.targetScheme == "https" { 1729 if err := pconn.addTLS(ctx, cm.tlsHost(), trace); err != nil { 1730 return nil, err 1731 } 1732 } 1733 1734 if s := pconn.tlsState; s != nil && s.NegotiatedProtocolIsMutual && s.NegotiatedProtocol != "" { 1735 if next, ok := t.TLSNextProto[s.NegotiatedProtocol]; ok { 1736 alt := next(cm.targetAddr, pconn.conn.(*tls.Conn)) 1737 if e, ok := alt.(erringRoundTripper); ok { 1738 // pconn.conn was closed by next (http2configureTransports.upgradeFn). 1739 return nil, e.RoundTripErr() 1740 } 1741 return &persistConn{t: t, cacheKey: pconn.cacheKey, alt: alt}, nil 1742 } 1743 } 1744 1745 pconn.br = bufio.NewReaderSize(pconn, t.readBufferSize()) 1746 pconn.bw = bufio.NewWriterSize(persistConnWriter{pconn}, t.writeBufferSize()) 1747 1748 go pconn.readLoop() 1749 go pconn.writeLoop() 1750 return pconn, nil 1751 } 1752 1753 // persistConnWriter is the io.Writer written to by pc.bw. 1754 // It accumulates the number of bytes written to the underlying conn, 1755 // so the retry logic can determine whether any bytes made it across 1756 // the wire. 1757 // This is exactly 1 pointer field wide so it can go into an interface 1758 // without allocation. 1759 type persistConnWriter struct { 1760 pc *persistConn 1761 } 1762 1763 func (w persistConnWriter) Write(p []byte) (n int, err error) { 1764 n, err = w.pc.conn.Write(p) 1765 w.pc.nwrite += int64(n) 1766 return 1767 } 1768 1769 // ReadFrom exposes persistConnWriter's underlying Conn to io.Copy and if 1770 // the Conn implements io.ReaderFrom, it can take advantage of optimizations 1771 // such as sendfile. 1772 func (w persistConnWriter) ReadFrom(r io.Reader) (n int64, err error) { 1773 n, err = io.Copy(w.pc.conn, r) 1774 w.pc.nwrite += n 1775 return 1776 } 1777 1778 var _ io.ReaderFrom = (*persistConnWriter)(nil) 1779 1780 // connectMethod is the map key (in its String form) for keeping persistent 1781 // TCP connections alive for subsequent HTTP requests. 1782 // 1783 // A connect method may be of the following types: 1784 // 1785 // connectMethod.key().String() Description 1786 // ------------------------------ ------------------------- 1787 // |http|foo.com http directly to server, no proxy 1788 // |https|foo.com https directly to server, no proxy 1789 // |https,h1|foo.com https directly to server w/o HTTP/2, no proxy 1790 // http://proxy.com|https|foo.com http to proxy, then CONNECT to foo.com 1791 // http://proxy.com|http http to proxy, http to anywhere after that 1792 // socks5://proxy.com|http|foo.com socks5 to proxy, then http to foo.com 1793 // socks5://proxy.com|https|foo.com socks5 to proxy, then https to foo.com 1794 // https://proxy.com|https|foo.com https to proxy, then CONNECT to foo.com 1795 // https://proxy.com|http https to proxy, http to anywhere after that 1796 // 1797 type connectMethod struct { 1798 _ incomparable 1799 proxyURL *url.URL // nil for no proxy, else full proxy URL 1800 targetScheme string // "http" or "https" 1801 // If proxyURL specifies an http or https proxy, and targetScheme is http (not https), 1802 // then targetAddr is not included in the connect method key, because the socket can 1803 // be reused for different targetAddr values. 1804 targetAddr string 1805 onlyH1 bool // whether to disable HTTP/2 and force HTTP/1 1806 } 1807 1808 func (cm *connectMethod) key() connectMethodKey { 1809 proxyStr := "" 1810 targetAddr := cm.targetAddr 1811 if cm.proxyURL != nil { 1812 proxyStr = cm.proxyURL.String() 1813 if (cm.proxyURL.Scheme == "http" || cm.proxyURL.Scheme == "https") && cm.targetScheme == "http" { 1814 targetAddr = "" 1815 } 1816 } 1817 return connectMethodKey{ 1818 proxy: proxyStr, 1819 scheme: cm.targetScheme, 1820 addr: targetAddr, 1821 onlyH1: cm.onlyH1, 1822 } 1823 } 1824 1825 // scheme returns the first hop scheme: http, https, or socks5 1826 func (cm *connectMethod) scheme() string { 1827 if cm.proxyURL != nil { 1828 return cm.proxyURL.Scheme 1829 } 1830 return cm.targetScheme 1831 } 1832 1833 // addr returns the first hop "host:port" to which we need to TCP connect. 1834 func (cm *connectMethod) addr() string { 1835 if cm.proxyURL != nil { 1836 return canonicalAddr(cm.proxyURL) 1837 } 1838 return cm.targetAddr 1839 } 1840 1841 // tlsHost returns the host name to match against the peer's 1842 // TLS certificate. 1843 func (cm *connectMethod) tlsHost() string { 1844 h := cm.targetAddr 1845 if hasPort(h) { 1846 h = h[:strings.LastIndex(h, ":")] 1847 } 1848 return h 1849 } 1850 1851 // connectMethodKey is the map key version of connectMethod, with a 1852 // stringified proxy URL (or the empty string) instead of a pointer to 1853 // a URL. 1854 type connectMethodKey struct { 1855 proxy, scheme, addr string 1856 onlyH1 bool 1857 } 1858 1859 func (k connectMethodKey) String() string { 1860 // Only used by tests. 1861 var h1 string 1862 if k.onlyH1 { 1863 h1 = ",h1" 1864 } 1865 return fmt.Sprintf("%s|%s%s|%s", k.proxy, k.scheme, h1, k.addr) 1866 } 1867 1868 // persistConn wraps a connection, usually a persistent one 1869 // (but may be used for non-keep-alive requests as well) 1870 type persistConn struct { 1871 // alt optionally specifies the TLS NextProto RoundTripper. 1872 // This is used for HTTP/2 today and future protocols later. 1873 // If it's non-nil, the rest of the fields are unused. 1874 alt RoundTripper 1875 1876 t *Transport 1877 cacheKey connectMethodKey 1878 conn net.Conn 1879 tlsState *tls.ConnectionState 1880 br *bufio.Reader // from conn 1881 bw *bufio.Writer // to conn 1882 nwrite int64 // bytes written 1883 reqch chan requestAndChan // written by roundTrip; read by readLoop 1884 writech chan writeRequest // written by roundTrip; read by writeLoop 1885 closech chan struct{} // closed when conn closed 1886 isProxy bool 1887 sawEOF bool // whether we've seen EOF from conn; owned by readLoop 1888 readLimit int64 // bytes allowed to be read; owned by readLoop 1889 // writeErrCh passes the request write error (usually nil) 1890 // from the writeLoop goroutine to the readLoop which passes 1891 // it off to the res.Body reader, which then uses it to decide 1892 // whether or not a connection can be reused. Issue 7569. 1893 writeErrCh chan error 1894 1895 writeLoopDone chan struct{} // closed when write loop ends 1896 1897 // Both guarded by Transport.idleMu: 1898 idleAt time.Time // time it last become idle 1899 idleTimer *time.Timer // holding an AfterFunc to close it 1900 1901 mu sync.Mutex // guards following fields 1902 numExpectedResponses int 1903 closed error // set non-nil when conn is closed, before closech is closed 1904 canceledErr error // set non-nil if conn is canceled 1905 broken bool // an error has happened on this connection; marked broken so it's not reused. 1906 reused bool // whether conn has had successful request/response and is being reused. 1907 // mutateHeaderFunc is an optional func to modify extra 1908 // headers on each outbound request before it's written. (the 1909 // original Request given to RoundTrip is not modified) 1910 mutateHeaderFunc func(Header) 1911 } 1912 1913 func (pc *persistConn) maxHeaderResponseSize() int64 { 1914 if v := pc.t.MaxResponseHeaderBytes; v != 0 { 1915 return v 1916 } 1917 return 10 << 20 // conservative default; same as http2 1918 } 1919 1920 func (pc *persistConn) Read(p []byte) (n int, err error) { 1921 if pc.readLimit <= 0 { 1922 return 0, fmt.Errorf("read limit of %d bytes exhausted", pc.maxHeaderResponseSize()) 1923 } 1924 if int64(len(p)) > pc.readLimit { 1925 p = p[:pc.readLimit] 1926 } 1927 n, err = pc.conn.Read(p) 1928 if err == io.EOF { 1929 pc.sawEOF = true 1930 } 1931 pc.readLimit -= int64(n) 1932 return 1933 } 1934 1935 // isBroken reports whether this connection is in a known broken state. 1936 func (pc *persistConn) isBroken() bool { 1937 pc.mu.Lock() 1938 b := pc.closed != nil 1939 pc.mu.Unlock() 1940 return b 1941 } 1942 1943 // canceled returns non-nil if the connection was closed due to 1944 // CancelRequest or due to context cancellation. 1945 func (pc *persistConn) canceled() error { 1946 pc.mu.Lock() 1947 defer pc.mu.Unlock() 1948 return pc.canceledErr 1949 } 1950 1951 // isReused reports whether this connection has been used before. 1952 func (pc *persistConn) isReused() bool { 1953 pc.mu.Lock() 1954 r := pc.reused 1955 pc.mu.Unlock() 1956 return r 1957 } 1958 1959 func (pc *persistConn) gotIdleConnTrace(idleAt time.Time) (t httptrace.GotConnInfo) { 1960 pc.mu.Lock() 1961 defer pc.mu.Unlock() 1962 t.Reused = pc.reused 1963 t.Conn = pc.conn 1964 t.WasIdle = true 1965 if !idleAt.IsZero() { 1966 t.IdleTime = time.Since(idleAt) 1967 } 1968 return 1969 } 1970 1971 func (pc *persistConn) cancelRequest(err error) { 1972 pc.mu.Lock() 1973 defer pc.mu.Unlock() 1974 pc.canceledErr = err 1975 pc.closeLocked(errRequestCanceled) 1976 } 1977 1978 // closeConnIfStillIdle closes the connection if it's still sitting idle. 1979 // This is what's called by the persistConn's idleTimer, and is run in its 1980 // own goroutine. 1981 func (pc *persistConn) closeConnIfStillIdle() { 1982 t := pc.t 1983 t.idleMu.Lock() 1984 defer t.idleMu.Unlock() 1985 if _, ok := t.idleLRU.m[pc]; !ok { 1986 // Not idle. 1987 return 1988 } 1989 t.removeIdleConnLocked(pc) 1990 pc.close(errIdleConnTimeout) 1991 } 1992 1993 // mapRoundTripError returns the appropriate error value for 1994 // persistConn.roundTrip. 1995 // 1996 // The provided err is the first error that (*persistConn).roundTrip 1997 // happened to receive from its select statement. 1998 // 1999 // The startBytesWritten value should be the value of pc.nwrite before the roundTrip 2000 // started writing the request. 2001 func (pc *persistConn) mapRoundTripError(req *transportRequest, startBytesWritten int64, err error) error { 2002 if err == nil { 2003 return nil 2004 } 2005 2006 // Wait for the writeLoop goroutine to terminate to avoid data 2007 // races on callers who mutate the request on failure. 2008 // 2009 // When resc in pc.roundTrip and hence rc.ch receives a responseAndError 2010 // with a non-nil error it implies that the persistConn is either closed 2011 // or closing. Waiting on pc.writeLoopDone is hence safe as all callers 2012 // close closech which in turn ensures writeLoop returns. 2013 <-pc.writeLoopDone 2014 2015 // If the request was canceled, that's better than network 2016 // failures that were likely the result of tearing down the 2017 // connection. 2018 if cerr := pc.canceled(); cerr != nil { 2019 return cerr 2020 } 2021 2022 // See if an error was set explicitly. 2023 req.mu.Lock() 2024 reqErr := req.err 2025 req.mu.Unlock() 2026 if reqErr != nil { 2027 return reqErr 2028 } 2029 2030 if err == errServerClosedIdle { 2031 // Don't decorate 2032 return err 2033 } 2034 2035 if _, ok := err.(transportReadFromServerError); ok { 2036 // Don't decorate 2037 return err 2038 } 2039 if pc.isBroken() { 2040 if pc.nwrite == startBytesWritten { 2041 return nothingWrittenError{err} 2042 } 2043 return fmt.Errorf("github.com/hxx258456/ccgo/gmhttp: HTTP/1.x transport connection broken: %v", err) 2044 } 2045 return err 2046 } 2047 2048 // errCallerOwnsConn is an internal sentinel error used when we hand 2049 // off a writable response.Body to the caller. We use this to prevent 2050 // closing a net.Conn that is now owned by the caller. 2051 var errCallerOwnsConn = errors.New("read loop ending; caller owns writable underlying conn") 2052 2053 func (pc *persistConn) readLoop() { 2054 closeErr := errReadLoopExiting // default value, if not changed below 2055 defer func() { 2056 pc.close(closeErr) 2057 pc.t.removeIdleConn(pc) 2058 }() 2059 2060 tryPutIdleConn := func(trace *httptrace.ClientTrace) bool { 2061 if err := pc.t.tryPutIdleConn(pc); err != nil { 2062 closeErr = err 2063 if trace != nil && trace.PutIdleConn != nil && err != errKeepAlivesDisabled { 2064 trace.PutIdleConn(err) 2065 } 2066 return false 2067 } 2068 if trace != nil && trace.PutIdleConn != nil { 2069 trace.PutIdleConn(nil) 2070 } 2071 return true 2072 } 2073 2074 // eofc is used to block caller goroutines reading from Response.Body 2075 // at EOF until this goroutines has (potentially) added the connection 2076 // back to the idle pool. 2077 eofc := make(chan struct{}) 2078 defer close(eofc) // unblock reader on errors 2079 2080 // Read this once, before loop starts. (to avoid races in tests) 2081 testHookMu.Lock() 2082 testHookReadLoopBeforeNextRead := testHookReadLoopBeforeNextRead 2083 testHookMu.Unlock() 2084 2085 alive := true 2086 for alive { 2087 pc.readLimit = pc.maxHeaderResponseSize() 2088 _, err := pc.br.Peek(1) 2089 2090 pc.mu.Lock() 2091 if pc.numExpectedResponses == 0 { 2092 pc.readLoopPeekFailLocked(err) 2093 pc.mu.Unlock() 2094 return 2095 } 2096 pc.mu.Unlock() 2097 2098 rc := <-pc.reqch 2099 trace := httptrace.ContextClientTrace(rc.req.Context()) 2100 2101 var resp *Response 2102 if err == nil { 2103 resp, err = pc.readResponse(rc, trace) 2104 } else { 2105 err = transportReadFromServerError{err} 2106 closeErr = err 2107 } 2108 2109 if err != nil { 2110 if pc.readLimit <= 0 { 2111 err = fmt.Errorf("github.com/hxx258456/ccgo/gmhttp: server response headers exceeded %d bytes; aborted", pc.maxHeaderResponseSize()) 2112 } 2113 2114 select { 2115 case rc.ch <- responseAndError{err: err}: 2116 case <-rc.callerGone: 2117 return 2118 } 2119 return 2120 } 2121 pc.readLimit = maxInt64 // effectively no limit for response bodies 2122 2123 pc.mu.Lock() 2124 pc.numExpectedResponses-- 2125 pc.mu.Unlock() 2126 2127 bodyWritable := resp.bodyIsWritable() 2128 hasBody := rc.req.Method != "HEAD" && resp.ContentLength != 0 2129 2130 if resp.Close || rc.req.Close || resp.StatusCode <= 199 || bodyWritable { 2131 // Don't do keep-alive on error if either party requested a close 2132 // or we get an unexpected informational (1xx) response. 2133 // StatusCode 100 is already handled above. 2134 alive = false 2135 } 2136 2137 if !hasBody || bodyWritable { 2138 replaced := pc.t.replaceReqCanceler(rc.cancelKey, nil) 2139 2140 // Put the idle conn back into the pool before we send the response 2141 // so if they process it quickly and make another request, they'll 2142 // get this same conn. But we use the unbuffered channel 'rc' 2143 // to guarantee that persistConn.roundTrip got out of its select 2144 // potentially waiting for this persistConn to close. 2145 alive = alive && 2146 !pc.sawEOF && 2147 pc.wroteRequest() && 2148 replaced && tryPutIdleConn(trace) 2149 2150 if bodyWritable { 2151 closeErr = errCallerOwnsConn 2152 } 2153 2154 select { 2155 case rc.ch <- responseAndError{res: resp}: 2156 case <-rc.callerGone: 2157 return 2158 } 2159 2160 // Now that they've read from the unbuffered channel, they're safely 2161 // out of the select that also waits on this goroutine to die, so 2162 // we're allowed to exit now if needed (if alive is false) 2163 testHookReadLoopBeforeNextRead() 2164 continue 2165 } 2166 2167 waitForBodyRead := make(chan bool, 2) 2168 body := &bodyEOFSignal{ 2169 body: resp.Body, 2170 earlyCloseFn: func() error { 2171 waitForBodyRead <- false 2172 <-eofc // will be closed by deferred call at the end of the function 2173 return nil 2174 2175 }, 2176 fn: func(err error) error { 2177 isEOF := err == io.EOF 2178 waitForBodyRead <- isEOF 2179 if isEOF { 2180 <-eofc // see comment above eofc declaration 2181 } else if err != nil { 2182 if cerr := pc.canceled(); cerr != nil { 2183 return cerr 2184 } 2185 } 2186 return err 2187 }, 2188 } 2189 2190 resp.Body = body 2191 if rc.addedGzip && ascii.EqualFold(resp.Header.Get("Content-Encoding"), "gzip") { 2192 resp.Body = &gzipReader{body: body} 2193 resp.Header.Del("Content-Encoding") 2194 resp.Header.Del("Content-Length") 2195 resp.ContentLength = -1 2196 resp.Uncompressed = true 2197 } 2198 2199 select { 2200 case rc.ch <- responseAndError{res: resp}: 2201 case <-rc.callerGone: 2202 return 2203 } 2204 2205 // Before looping back to the top of this function and peeking on 2206 // the bufio.Reader, wait for the caller goroutine to finish 2207 // reading the response body. (or for cancellation or death) 2208 select { 2209 case bodyEOF := <-waitForBodyRead: 2210 replaced := pc.t.replaceReqCanceler(rc.cancelKey, nil) // before pc might return to idle pool 2211 alive = alive && 2212 bodyEOF && 2213 !pc.sawEOF && 2214 pc.wroteRequest() && 2215 replaced && tryPutIdleConn(trace) 2216 if bodyEOF { 2217 eofc <- struct{}{} 2218 } 2219 case <-rc.req.Cancel: 2220 alive = false 2221 pc.t.CancelRequest(rc.req) 2222 case <-rc.req.Context().Done(): 2223 alive = false 2224 pc.t.cancelRequest(rc.cancelKey, rc.req.Context().Err()) 2225 case <-pc.closech: 2226 alive = false 2227 } 2228 2229 testHookReadLoopBeforeNextRead() 2230 } 2231 } 2232 2233 func (pc *persistConn) readLoopPeekFailLocked(peekErr error) { 2234 if pc.closed != nil { 2235 return 2236 } 2237 if n := pc.br.Buffered(); n > 0 { 2238 buf, _ := pc.br.Peek(n) 2239 if is408Message(buf) { 2240 pc.closeLocked(errServerClosedIdle) 2241 return 2242 } else { 2243 log.Printf("Unsolicited response received on idle HTTP channel starting with %q; err=%v", buf, peekErr) 2244 } 2245 } 2246 if peekErr == io.EOF { 2247 // common case. 2248 pc.closeLocked(errServerClosedIdle) 2249 } else { 2250 pc.closeLocked(fmt.Errorf("readLoopPeekFailLocked: %v", peekErr)) 2251 } 2252 } 2253 2254 // is408Message reports whether buf has the prefix of an 2255 // HTTP 408 Request Timeout response. 2256 // See golang.org/issue/32310. 2257 func is408Message(buf []byte) bool { 2258 if len(buf) < len("HTTP/1.x 408") { 2259 return false 2260 } 2261 if string(buf[:7]) != "HTTP/1." { 2262 return false 2263 } 2264 return string(buf[8:12]) == " 408" 2265 } 2266 2267 // readResponse reads an HTTP response (or two, in the case of "Expect: 2268 // 100-continue") from the server. It returns the final non-100 one. 2269 // trace is optional. 2270 func (pc *persistConn) readResponse(rc requestAndChan, trace *httptrace.ClientTrace) (resp *Response, err error) { 2271 if trace != nil && trace.GotFirstResponseByte != nil { 2272 if peek, err := pc.br.Peek(1); err == nil && len(peek) == 1 { 2273 trace.GotFirstResponseByte() 2274 } 2275 } 2276 num1xx := 0 // number of informational 1xx headers received 2277 const max1xxResponses = 5 // arbitrary bound on number of informational responses 2278 2279 continueCh := rc.continueCh 2280 for { 2281 resp, err = ReadResponse(pc.br, rc.req) 2282 if err != nil { 2283 return 2284 } 2285 resCode := resp.StatusCode 2286 if continueCh != nil { 2287 if resCode == 100 { 2288 if trace != nil && trace.Got100Continue != nil { 2289 trace.Got100Continue() 2290 } 2291 continueCh <- struct{}{} 2292 continueCh = nil 2293 } else if resCode >= 200 { 2294 close(continueCh) 2295 continueCh = nil 2296 } 2297 } 2298 is1xx := 100 <= resCode && resCode <= 199 2299 // treat 101 as a terminal status, see issue 26161 2300 is1xxNonTerminal := is1xx && resCode != StatusSwitchingProtocols 2301 if is1xxNonTerminal { 2302 num1xx++ 2303 if num1xx > max1xxResponses { 2304 return nil, errors.New("github.com/hxx258456/ccgo/gmhttp: too many 1xx informational responses") 2305 } 2306 pc.readLimit = pc.maxHeaderResponseSize() // reset the limit 2307 if trace != nil && trace.Got1xxResponse != nil { 2308 if err := trace.Got1xxResponse(resCode, textproto.MIMEHeader(resp.Header)); err != nil { 2309 return nil, err 2310 } 2311 } 2312 continue 2313 } 2314 break 2315 } 2316 if resp.isProtocolSwitch() { 2317 resp.Body = newReadWriteCloserBody(pc.br, pc.conn) 2318 } 2319 2320 resp.TLS = pc.tlsState 2321 return 2322 } 2323 2324 // waitForContinue returns the function to block until 2325 // any response, timeout or connection close. After any of them, 2326 // the function returns a bool which indicates if the body should be sent. 2327 func (pc *persistConn) waitForContinue(continueCh <-chan struct{}) func() bool { 2328 if continueCh == nil { 2329 return nil 2330 } 2331 return func() bool { 2332 timer := time.NewTimer(pc.t.ExpectContinueTimeout) 2333 defer timer.Stop() 2334 2335 select { 2336 case _, ok := <-continueCh: 2337 return ok 2338 case <-timer.C: 2339 return true 2340 case <-pc.closech: 2341 return false 2342 } 2343 } 2344 } 2345 2346 func newReadWriteCloserBody(br *bufio.Reader, rwc io.ReadWriteCloser) io.ReadWriteCloser { 2347 body := &readWriteCloserBody{ReadWriteCloser: rwc} 2348 if br.Buffered() != 0 { 2349 body.br = br 2350 } 2351 return body 2352 } 2353 2354 // readWriteCloserBody is the Response.Body type used when we want to 2355 // give users write access to the Body through the underlying 2356 // connection (TCP, unless using custom dialers). This is then 2357 // the concrete type for a Response.Body on the 101 Switching 2358 // Protocols response, as used by WebSockets, h2c, etc. 2359 type readWriteCloserBody struct { 2360 _ incomparable 2361 br *bufio.Reader // used until empty 2362 io.ReadWriteCloser 2363 } 2364 2365 func (b *readWriteCloserBody) Read(p []byte) (n int, err error) { 2366 if b.br != nil { 2367 if n := b.br.Buffered(); len(p) > n { 2368 p = p[:n] 2369 } 2370 n, err = b.br.Read(p) 2371 if b.br.Buffered() == 0 { 2372 b.br = nil 2373 } 2374 return n, err 2375 } 2376 return b.ReadWriteCloser.Read(p) 2377 } 2378 2379 // nothingWrittenError wraps a write errors which ended up writing zero bytes. 2380 type nothingWrittenError struct { 2381 error 2382 } 2383 2384 func (pc *persistConn) writeLoop() { 2385 defer close(pc.writeLoopDone) 2386 for { 2387 select { 2388 case wr := <-pc.writech: 2389 startBytesWritten := pc.nwrite 2390 err := wr.req.Request.write(pc.bw, pc.isProxy, wr.req.extra, pc.waitForContinue(wr.continueCh)) 2391 if bre, ok := err.(requestBodyReadError); ok { 2392 err = bre.error 2393 // Errors reading from the user's 2394 // Request.Body are high priority. 2395 // Set it here before sending on the 2396 // channels below or calling 2397 // pc.close() which tears down 2398 // connections and causes other 2399 // errors. 2400 wr.req.setError(err) 2401 } 2402 if err == nil { 2403 err = pc.bw.Flush() 2404 } 2405 if err != nil { 2406 if pc.nwrite == startBytesWritten { 2407 err = nothingWrittenError{err} 2408 } 2409 } 2410 pc.writeErrCh <- err // to the body reader, which might recycle us 2411 wr.ch <- err // to the roundTrip function 2412 if err != nil { 2413 pc.close(err) 2414 return 2415 } 2416 case <-pc.closech: 2417 return 2418 } 2419 } 2420 } 2421 2422 // maxWriteWaitBeforeConnReuse is how long the a Transport RoundTrip 2423 // will wait to see the Request's Body.Write result after getting a 2424 // response from the server. See comments in (*persistConn).wroteRequest. 2425 const maxWriteWaitBeforeConnReuse = 50 * time.Millisecond 2426 2427 // wroteRequest is a check before recycling a connection that the previous write 2428 // (from writeLoop above) happened and was successful. 2429 func (pc *persistConn) wroteRequest() bool { 2430 select { 2431 case err := <-pc.writeErrCh: 2432 // Common case: the write happened well before the response, so 2433 // avoid creating a timer. 2434 return err == nil 2435 default: 2436 // Rare case: the request was written in writeLoop above but 2437 // before it could send to pc.writeErrCh, the reader read it 2438 // all, processed it, and called us here. In this case, give the 2439 // write goroutine a bit of time to finish its send. 2440 // 2441 // Less rare case: We also get here in the legitimate case of 2442 // Issue 7569, where the writer is still writing (or stalled), 2443 // but the server has already replied. In this case, we don't 2444 // want to wait too long, and we want to return false so this 2445 // connection isn't re-used. 2446 t := time.NewTimer(maxWriteWaitBeforeConnReuse) 2447 defer t.Stop() 2448 select { 2449 case err := <-pc.writeErrCh: 2450 return err == nil 2451 case <-t.C: 2452 return false 2453 } 2454 } 2455 } 2456 2457 // responseAndError is how the goroutine reading from an HTTP/1 server 2458 // communicates with the goroutine doing the RoundTrip. 2459 type responseAndError struct { 2460 _ incomparable 2461 res *Response // else use this response (see res method) 2462 err error 2463 } 2464 2465 type requestAndChan struct { 2466 _ incomparable 2467 req *Request 2468 cancelKey cancelKey 2469 ch chan responseAndError // unbuffered; always send in select on callerGone 2470 2471 // whether the Transport (as opposed to the user client code) 2472 // added the Accept-Encoding gzip header. If the Transport 2473 // set it, only then do we transparently decode the gzip. 2474 addedGzip bool 2475 2476 // Optional blocking chan for Expect: 100-continue (for send). 2477 // If the request has an "Expect: 100-continue" header and 2478 // the server responds 100 Continue, readLoop send a value 2479 // to writeLoop via this chan. 2480 continueCh chan<- struct{} 2481 2482 callerGone <-chan struct{} // closed when roundTrip caller has returned 2483 } 2484 2485 // A writeRequest is sent by the readLoop's goroutine to the 2486 // writeLoop's goroutine to write a request while the read loop 2487 // concurrently waits on both the write response and the server's 2488 // reply. 2489 type writeRequest struct { 2490 req *transportRequest 2491 ch chan<- error 2492 2493 // Optional blocking chan for Expect: 100-continue (for receive). 2494 // If not nil, writeLoop blocks sending request body until 2495 // it receives from this chan. 2496 continueCh <-chan struct{} 2497 } 2498 2499 type httpError struct { 2500 err string 2501 timeout bool 2502 } 2503 2504 func (e *httpError) Error() string { return e.err } 2505 func (e *httpError) Timeout() bool { return e.timeout } 2506 func (e *httpError) Temporary() bool { return true } 2507 2508 var errTimeout error = &httpError{err: "github.com/hxx258456/ccgo/gmhttp: timeout awaiting response headers", timeout: true} 2509 2510 // errRequestCanceled is set to be identical to the one from h2 to facilitate 2511 // testing. 2512 var errRequestCanceled = http2errRequestCanceled 2513 var errRequestCanceledConn = errors.New("github.com/hxx258456/ccgo/gmhttp: request canceled while waiting for connection") // TODO: unify? 2514 2515 func nop() {} 2516 2517 // testHooks. Always non-nil. 2518 var ( 2519 testHookEnterRoundTrip = nop 2520 testHookWaitResLoop = nop 2521 testHookRoundTripRetried = nop 2522 testHookPrePendingDial = nop 2523 testHookPostPendingDial = nop 2524 2525 testHookMu sync.Locker = fakeLocker{} // guards following 2526 testHookReadLoopBeforeNextRead = nop 2527 ) 2528 2529 func (pc *persistConn) roundTrip(req *transportRequest) (resp *Response, err error) { 2530 testHookEnterRoundTrip() 2531 if !pc.t.replaceReqCanceler(req.cancelKey, pc.cancelRequest) { 2532 pc.t.putOrCloseIdleConn(pc) 2533 return nil, errRequestCanceled 2534 } 2535 pc.mu.Lock() 2536 pc.numExpectedResponses++ 2537 headerFn := pc.mutateHeaderFunc 2538 pc.mu.Unlock() 2539 2540 if headerFn != nil { 2541 headerFn(req.extraHeaders()) 2542 } 2543 2544 // Ask for a compressed version if the caller didn't set their 2545 // own value for Accept-Encoding. We only attempt to 2546 // uncompress the gzip stream if we were the layer that 2547 // requested it. 2548 requestedGzip := false 2549 if !pc.t.DisableCompression && 2550 req.Header.Get("Accept-Encoding") == "" && 2551 req.Header.Get("Range") == "" && 2552 req.Method != "HEAD" { 2553 // Request gzip only, not deflate. Deflate is ambiguous and 2554 // not as universally supported anyway. 2555 // See: https://zlib.net/zlib_faq.html#faq39 2556 // 2557 // Note that we don't request this for HEAD requests, 2558 // due to a bug in nginx: 2559 // https://trac.nginx.org/nginx/ticket/358 2560 // https://golang.org/issue/5522 2561 // 2562 // We don't request gzip if the request is for a range, since 2563 // auto-decoding a portion of a gzipped document will just fail 2564 // anyway. See https://golang.org/issue/8923 2565 requestedGzip = true 2566 req.extraHeaders().Set("Accept-Encoding", "gzip") 2567 } 2568 2569 var continueCh chan struct{} 2570 if req.ProtoAtLeast(1, 1) && req.Body != nil && req.expectsContinue() { 2571 continueCh = make(chan struct{}, 1) 2572 } 2573 2574 if pc.t.DisableKeepAlives && 2575 !req.wantsClose() && 2576 !isProtocolSwitchHeader(req.Header) { 2577 req.extraHeaders().Set("Connection", "close") 2578 } 2579 2580 gone := make(chan struct{}) 2581 defer close(gone) 2582 2583 defer func() { 2584 if err != nil { 2585 pc.t.setReqCanceler(req.cancelKey, nil) 2586 } 2587 }() 2588 2589 const debugRoundTrip = false 2590 2591 // Write the request concurrently with waiting for a response, 2592 // in case the server decides to reply before reading our full 2593 // request body. 2594 startBytesWritten := pc.nwrite 2595 writeErrCh := make(chan error, 1) 2596 pc.writech <- writeRequest{req, writeErrCh, continueCh} 2597 2598 resc := make(chan responseAndError) 2599 pc.reqch <- requestAndChan{ 2600 req: req.Request, 2601 cancelKey: req.cancelKey, 2602 ch: resc, 2603 addedGzip: requestedGzip, 2604 continueCh: continueCh, 2605 callerGone: gone, 2606 } 2607 2608 var respHeaderTimer <-chan time.Time 2609 cancelChan := req.Request.Cancel 2610 ctxDoneChan := req.Context().Done() 2611 pcClosed := pc.closech 2612 canceled := false 2613 for { 2614 testHookWaitResLoop() 2615 select { 2616 case err := <-writeErrCh: 2617 if debugRoundTrip { 2618 req.logf("writeErrCh resv: %T/%#v", err, err) 2619 } 2620 if err != nil { 2621 pc.close(fmt.Errorf("write error: %v", err)) 2622 return nil, pc.mapRoundTripError(req, startBytesWritten, err) 2623 } 2624 if d := pc.t.ResponseHeaderTimeout; d > 0 { 2625 if debugRoundTrip { 2626 req.logf("starting timer for %v", d) 2627 } 2628 timer := time.NewTimer(d) 2629 defer timer.Stop() // prevent leaks 2630 respHeaderTimer = timer.C 2631 } 2632 case <-pcClosed: 2633 pcClosed = nil 2634 if canceled || pc.t.replaceReqCanceler(req.cancelKey, nil) { 2635 if debugRoundTrip { 2636 req.logf("closech recv: %T %#v", pc.closed, pc.closed) 2637 } 2638 return nil, pc.mapRoundTripError(req, startBytesWritten, pc.closed) 2639 } 2640 case <-respHeaderTimer: 2641 if debugRoundTrip { 2642 req.logf("timeout waiting for response headers.") 2643 } 2644 pc.close(errTimeout) 2645 return nil, errTimeout 2646 case re := <-resc: 2647 if (re.res == nil) == (re.err == nil) { 2648 panic(fmt.Sprintf("internal error: exactly one of res or err should be set; nil=%v", re.res == nil)) 2649 } 2650 if debugRoundTrip { 2651 req.logf("resc recv: %p, %T/%#v", re.res, re.err, re.err) 2652 } 2653 if re.err != nil { 2654 return nil, pc.mapRoundTripError(req, startBytesWritten, re.err) 2655 } 2656 return re.res, nil 2657 case <-cancelChan: 2658 canceled = pc.t.cancelRequest(req.cancelKey, errRequestCanceled) 2659 cancelChan = nil 2660 case <-ctxDoneChan: 2661 canceled = pc.t.cancelRequest(req.cancelKey, req.Context().Err()) 2662 cancelChan = nil 2663 ctxDoneChan = nil 2664 } 2665 } 2666 } 2667 2668 // tLogKey is a context WithValue key for test debugging contexts containing 2669 // a t.Logf func. See export_test.go's Request.WithT method. 2670 type tLogKey struct{} 2671 2672 func (tr *transportRequest) logf(format string, args ...interface{}) { 2673 if logf, ok := tr.Request.Context().Value(tLogKey{}).(func(string, ...interface{})); ok { 2674 logf(time.Now().Format(time.RFC3339Nano)+": "+format, args...) 2675 } 2676 } 2677 2678 // markReused marks this connection as having been successfully used for a 2679 // request and response. 2680 func (pc *persistConn) markReused() { 2681 pc.mu.Lock() 2682 pc.reused = true 2683 pc.mu.Unlock() 2684 } 2685 2686 // close closes the underlying TCP connection and closes 2687 // the pc.closech channel. 2688 // 2689 // The provided err is only for testing and debugging; in normal 2690 // circumstances it should never be seen by users. 2691 func (pc *persistConn) close(err error) { 2692 pc.mu.Lock() 2693 defer pc.mu.Unlock() 2694 pc.closeLocked(err) 2695 } 2696 2697 func (pc *persistConn) closeLocked(err error) { 2698 if err == nil { 2699 panic("nil error") 2700 } 2701 pc.broken = true 2702 if pc.closed == nil { 2703 pc.closed = err 2704 pc.t.decConnsPerHost(pc.cacheKey) 2705 // Close HTTP/1 (pc.alt == nil) connection. 2706 // HTTP/2 closes its connection itself. 2707 if pc.alt == nil { 2708 if err != errCallerOwnsConn { 2709 pc.conn.Close() 2710 } 2711 close(pc.closech) 2712 } 2713 } 2714 pc.mutateHeaderFunc = nil 2715 } 2716 2717 var portMap = map[string]string{ 2718 "http": "80", 2719 "https": "443", 2720 "socks5": "1080", 2721 } 2722 2723 // canonicalAddr returns url.Host but always with a ":port" suffix 2724 func canonicalAddr(url *url.URL) string { 2725 addr := url.Hostname() 2726 if v, err := idnaASCII(addr); err == nil { 2727 addr = v 2728 } 2729 port := url.Port() 2730 if port == "" { 2731 port = portMap[url.Scheme] 2732 } 2733 return net.JoinHostPort(addr, port) 2734 } 2735 2736 // bodyEOFSignal is used by the HTTP/1 transport when reading response 2737 // bodies to make sure we see the end of a response body before 2738 // proceeding and reading on the connection again. 2739 // 2740 // It wraps a ReadCloser but runs fn (if non-nil) at most 2741 // once, right before its final (error-producing) Read or Close call 2742 // returns. fn should return the new error to return from Read or Close. 2743 // 2744 // If earlyCloseFn is non-nil and Close is called before io.EOF is 2745 // seen, earlyCloseFn is called instead of fn, and its return value is 2746 // the return value from Close. 2747 type bodyEOFSignal struct { 2748 body io.ReadCloser 2749 mu sync.Mutex // guards following 4 fields 2750 closed bool // whether Close has been called 2751 rerr error // sticky Read error 2752 fn func(error) error // err will be nil on Read io.EOF 2753 earlyCloseFn func() error // optional alt Close func used if io.EOF not seen 2754 } 2755 2756 var errReadOnClosedResBody = errors.New("http: read on closed response body") 2757 2758 func (es *bodyEOFSignal) Read(p []byte) (n int, err error) { 2759 es.mu.Lock() 2760 closed, rerr := es.closed, es.rerr 2761 es.mu.Unlock() 2762 if closed { 2763 return 0, errReadOnClosedResBody 2764 } 2765 if rerr != nil { 2766 return 0, rerr 2767 } 2768 2769 n, err = es.body.Read(p) 2770 if err != nil { 2771 es.mu.Lock() 2772 defer es.mu.Unlock() 2773 if es.rerr == nil { 2774 es.rerr = err 2775 } 2776 err = es.condfn(err) 2777 } 2778 return 2779 } 2780 2781 func (es *bodyEOFSignal) Close() error { 2782 es.mu.Lock() 2783 defer es.mu.Unlock() 2784 if es.closed { 2785 return nil 2786 } 2787 es.closed = true 2788 if es.earlyCloseFn != nil && es.rerr != io.EOF { 2789 return es.earlyCloseFn() 2790 } 2791 err := es.body.Close() 2792 return es.condfn(err) 2793 } 2794 2795 // caller must hold es.mu. 2796 func (es *bodyEOFSignal) condfn(err error) error { 2797 if es.fn == nil { 2798 return err 2799 } 2800 err = es.fn(err) 2801 es.fn = nil 2802 return err 2803 } 2804 2805 // gzipReader wraps a response body so it can lazily 2806 // call gzip.NewReader on the first call to Read 2807 type gzipReader struct { 2808 _ incomparable 2809 body *bodyEOFSignal // underlying HTTP/1 response body framing 2810 zr *gzip.Reader // lazily-initialized gzip reader 2811 zerr error // any error from gzip.NewReader; sticky 2812 } 2813 2814 func (gz *gzipReader) Read(p []byte) (n int, err error) { 2815 if gz.zr == nil { 2816 if gz.zerr == nil { 2817 gz.zr, gz.zerr = gzip.NewReader(gz.body) 2818 } 2819 if gz.zerr != nil { 2820 return 0, gz.zerr 2821 } 2822 } 2823 2824 gz.body.mu.Lock() 2825 if gz.body.closed { 2826 err = errReadOnClosedResBody 2827 } 2828 gz.body.mu.Unlock() 2829 2830 if err != nil { 2831 return 0, err 2832 } 2833 return gz.zr.Read(p) 2834 } 2835 2836 func (gz *gzipReader) Close() error { 2837 return gz.body.Close() 2838 } 2839 2840 type tlsHandshakeTimeoutError struct{} 2841 2842 func (tlsHandshakeTimeoutError) Timeout() bool { return true } 2843 func (tlsHandshakeTimeoutError) Temporary() bool { return true } 2844 func (tlsHandshakeTimeoutError) Error() string { 2845 return "github.com/hxx258456/ccgo/gmhttp: TLS handshake timeout" 2846 } 2847 2848 // fakeLocker is a sync.Locker which does nothing. It's used to guard 2849 // test-only fields when not under test, to avoid runtime atomic 2850 // overhead. 2851 type fakeLocker struct{} 2852 2853 func (fakeLocker) Lock() {} 2854 func (fakeLocker) Unlock() {} 2855 2856 // cloneTLSConfig returns a shallow clone of cfg, or a new zero tls.Config if 2857 // cfg is nil. This is safe to call even if cfg is in active use by a TLS 2858 // client or server. 2859 func cloneTLSConfig(cfg *tls.Config) *tls.Config { 2860 if cfg == nil { 2861 return &tls.Config{} 2862 } 2863 return cfg.Clone() 2864 } 2865 2866 type connLRU struct { 2867 ll *list.List // list.Element.Value type is of *persistConn 2868 m map[*persistConn]*list.Element 2869 } 2870 2871 // add adds pc to the head of the linked list. 2872 func (cl *connLRU) add(pc *persistConn) { 2873 if cl.ll == nil { 2874 cl.ll = list.New() 2875 cl.m = make(map[*persistConn]*list.Element) 2876 } 2877 ele := cl.ll.PushFront(pc) 2878 if _, ok := cl.m[pc]; ok { 2879 panic("persistConn was already in LRU") 2880 } 2881 cl.m[pc] = ele 2882 } 2883 2884 func (cl *connLRU) removeOldest() *persistConn { 2885 ele := cl.ll.Back() 2886 pc := ele.Value.(*persistConn) 2887 cl.ll.Remove(ele) 2888 delete(cl.m, pc) 2889 return pc 2890 } 2891 2892 // remove removes pc from cl. 2893 func (cl *connLRU) remove(pc *persistConn) { 2894 if ele, ok := cl.m[pc]; ok { 2895 cl.ll.Remove(ele) 2896 delete(cl.m, pc) 2897 } 2898 } 2899 2900 // len returns the number of items in the cache. 2901 func (cl *connLRU) len() int { 2902 return len(cl.m) 2903 }