github.com/ooni/oohttp@v0.7.2/client.go (about)

     1  // Copyright 2009 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. See RFC 7230 through 7235.
     6  //
     7  // This is the high-level Client interface.
     8  // The low-level implementation is in transport.go.
     9  
    10  package http
    11  
    12  import (
    13  	"context"
    14  	"crypto/tls"
    15  	"encoding/base64"
    16  	"errors"
    17  	"fmt"
    18  	"io"
    19  	"log"
    20  	"net/url"
    21  	"reflect"
    22  	"sort"
    23  	"strings"
    24  	"sync"
    25  	"sync/atomic"
    26  	"time"
    27  
    28  	"github.com/ooni/oohttp/internal/ascii"
    29  )
    30  
    31  // A Client is an HTTP client. Its zero value (DefaultClient) is a
    32  // usable client that uses DefaultTransport.
    33  //
    34  // The Client's Transport typically has internal state (cached TCP
    35  // connections), so Clients should be reused instead of created as
    36  // needed. Clients are safe for concurrent use by multiple goroutines.
    37  //
    38  // A Client is higher-level than a RoundTripper (such as Transport)
    39  // and additionally handles HTTP details such as cookies and
    40  // redirects.
    41  //
    42  // When following redirects, the Client will forward all headers set on the
    43  // initial Request except:
    44  //
    45  // • when forwarding sensitive headers like "Authorization",
    46  // "WWW-Authenticate", and "Cookie" to untrusted targets.
    47  // These headers will be ignored when following a redirect to a domain
    48  // that is not a subdomain match or exact match of the initial domain.
    49  // For example, a redirect from "foo.com" to either "foo.com" or "sub.foo.com"
    50  // will forward the sensitive headers, but a redirect to "bar.com" will not.
    51  //
    52  // • when forwarding the "Cookie" header with a non-nil cookie Jar.
    53  // Since each redirect may mutate the state of the cookie jar,
    54  // a redirect may possibly alter a cookie set in the initial request.
    55  // When forwarding the "Cookie" header, any mutated cookies will be omitted,
    56  // with the expectation that the Jar will insert those mutated cookies
    57  // with the updated values (assuming the origin matches).
    58  // If Jar is nil, the initial cookies are forwarded without change.
    59  type Client struct {
    60  	// Transport specifies the mechanism by which individual
    61  	// HTTP requests are made.
    62  	// If nil, DefaultTransport is used.
    63  	Transport RoundTripper
    64  
    65  	// CheckRedirect specifies the policy for handling redirects.
    66  	// If CheckRedirect is not nil, the client calls it before
    67  	// following an HTTP redirect. The arguments req and via are
    68  	// the upcoming request and the requests made already, oldest
    69  	// first. If CheckRedirect returns an error, the Client's Get
    70  	// method returns both the previous Response (with its Body
    71  	// closed) and CheckRedirect's error (wrapped in a url.Error)
    72  	// instead of issuing the Request req.
    73  	// As a special case, if CheckRedirect returns ErrUseLastResponse,
    74  	// then the most recent response is returned with its body
    75  	// unclosed, along with a nil error.
    76  	//
    77  	// If CheckRedirect is nil, the Client uses its default policy,
    78  	// which is to stop after 10 consecutive requests.
    79  	CheckRedirect func(req *Request, via []*Request) error
    80  
    81  	// Jar specifies the cookie jar.
    82  	//
    83  	// The Jar is used to insert relevant cookies into every
    84  	// outbound Request and is updated with the cookie values
    85  	// of every inbound Response. The Jar is consulted for every
    86  	// redirect that the Client follows.
    87  	//
    88  	// If Jar is nil, cookies are only sent if they are explicitly
    89  	// set on the Request.
    90  	Jar CookieJar
    91  
    92  	// Timeout specifies a time limit for requests made by this
    93  	// Client. The timeout includes connection time, any
    94  	// redirects, and reading the response body. The timer remains
    95  	// running after Get, Head, Post, or Do return and will
    96  	// interrupt reading of the Response.Body.
    97  	//
    98  	// A Timeout of zero means no timeout.
    99  	//
   100  	// The Client cancels requests to the underlying Transport
   101  	// as if the Request's Context ended.
   102  	//
   103  	// For compatibility, the Client will also use the deprecated
   104  	// CancelRequest method on Transport if found. New
   105  	// RoundTripper implementations should use the Request's Context
   106  	// for cancellation instead of implementing CancelRequest.
   107  	Timeout time.Duration
   108  }
   109  
   110  // DefaultClient is the default Client and is used by Get, Head, and Post.
   111  var DefaultClient = &Client{}
   112  
   113  // RoundTripper is an interface representing the ability to execute a
   114  // single HTTP transaction, obtaining the Response for a given Request.
   115  //
   116  // A RoundTripper must be safe for concurrent use by multiple
   117  // goroutines.
   118  type RoundTripper interface {
   119  	// RoundTrip executes a single HTTP transaction, returning
   120  	// a Response for the provided Request.
   121  	//
   122  	// RoundTrip should not attempt to interpret the response. In
   123  	// particular, RoundTrip must return err == nil if it obtained
   124  	// a response, regardless of the response's HTTP status code.
   125  	// A non-nil err should be reserved for failure to obtain a
   126  	// response. Similarly, RoundTrip should not attempt to
   127  	// handle higher-level protocol details such as redirects,
   128  	// authentication, or cookies.
   129  	//
   130  	// RoundTrip should not modify the request, except for
   131  	// consuming and closing the Request's Body. RoundTrip may
   132  	// read fields of the request in a separate goroutine. Callers
   133  	// should not mutate or reuse the request until the Response's
   134  	// Body has been closed.
   135  	//
   136  	// RoundTrip must always close the body, including on errors,
   137  	// but depending on the implementation may do so in a separate
   138  	// goroutine even after RoundTrip returns. This means that
   139  	// callers wanting to reuse the body for subsequent requests
   140  	// must arrange to wait for the Close call before doing so.
   141  	//
   142  	// The Request's URL and Header fields must be initialized.
   143  	RoundTrip(*Request) (*Response, error)
   144  }
   145  
   146  // refererForURL returns a referer without any authentication info or
   147  // an empty string if lastReq scheme is https and newReq scheme is http.
   148  // If the referer was explicitly set, then it will continue to be used.
   149  func refererForURL(lastReq, newReq *url.URL, explicitRef string) string {
   150  	// https://tools.ietf.org/html/rfc7231#section-5.5.2
   151  	//   "Clients SHOULD NOT include a Referer header field in a
   152  	//    (non-secure) HTTP request if the referring page was
   153  	//    transferred with a secure protocol."
   154  	if lastReq.Scheme == "https" && newReq.Scheme == "http" {
   155  		return ""
   156  	}
   157  	if explicitRef != "" {
   158  		return explicitRef
   159  	}
   160  
   161  	referer := lastReq.String()
   162  	if lastReq.User != nil {
   163  		// This is not very efficient, but is the best we can
   164  		// do without:
   165  		// - introducing a new method on URL
   166  		// - creating a race condition
   167  		// - copying the URL struct manually, which would cause
   168  		//   maintenance problems down the line
   169  		auth := lastReq.User.String() + "@"
   170  		referer = strings.Replace(referer, auth, "", 1)
   171  	}
   172  	return referer
   173  }
   174  
   175  // didTimeout is non-nil only if err != nil.
   176  func (c *Client) send(req *Request, deadline time.Time) (resp *Response, didTimeout func() bool, err error) {
   177  	if c.Jar != nil {
   178  		for _, cookie := range c.Jar.Cookies(req.URL) {
   179  			req.AddCookie(cookie)
   180  		}
   181  	}
   182  	resp, didTimeout, err = send(req, c.transport(), deadline)
   183  	if err != nil {
   184  		return nil, didTimeout, err
   185  	}
   186  	if c.Jar != nil {
   187  		if rc := resp.Cookies(); len(rc) > 0 {
   188  			c.Jar.SetCookies(req.URL, rc)
   189  		}
   190  	}
   191  	return resp, nil, nil
   192  }
   193  
   194  func (c *Client) deadline() time.Time {
   195  	if c.Timeout > 0 {
   196  		return time.Now().Add(c.Timeout)
   197  	}
   198  	return time.Time{}
   199  }
   200  
   201  func (c *Client) transport() RoundTripper {
   202  	if c.Transport != nil {
   203  		return c.Transport
   204  	}
   205  	return DefaultTransport
   206  }
   207  
   208  // ErrSchemeMismatch is returned when a server returns an HTTP response to an HTTPS client.
   209  var ErrSchemeMismatch = errors.New("http: server gave HTTP response to HTTPS client")
   210  
   211  // send issues an HTTP request.
   212  // Caller should close resp.Body when done reading from it.
   213  func send(ireq *Request, rt RoundTripper, deadline time.Time) (resp *Response, didTimeout func() bool, err error) {
   214  	req := ireq // req is either the original request, or a modified fork
   215  
   216  	if rt == nil {
   217  		req.closeBody()
   218  		return nil, alwaysFalse, errors.New("http: no Client.Transport or DefaultTransport")
   219  	}
   220  
   221  	if req.URL == nil {
   222  		req.closeBody()
   223  		return nil, alwaysFalse, errors.New("http: nil Request.URL")
   224  	}
   225  
   226  	if req.RequestURI != "" {
   227  		req.closeBody()
   228  		return nil, alwaysFalse, errors.New("http: Request.RequestURI can't be set in client requests")
   229  	}
   230  
   231  	// forkReq forks req into a shallow clone of ireq the first
   232  	// time it's called.
   233  	forkReq := func() {
   234  		if ireq == req {
   235  			req = new(Request)
   236  			*req = *ireq // shallow clone
   237  		}
   238  	}
   239  
   240  	// Most the callers of send (Get, Post, et al) don't need
   241  	// Headers, leaving it uninitialized. We guarantee to the
   242  	// Transport that this has been initialized, though.
   243  	if req.Header == nil {
   244  		forkReq()
   245  		req.Header = make(Header)
   246  	}
   247  
   248  	if u := req.URL.User; u != nil && req.Header.Get("Authorization") == "" {
   249  		username := u.Username()
   250  		password, _ := u.Password()
   251  		forkReq()
   252  		req.Header = cloneOrMakeHeader(ireq.Header)
   253  		req.Header.Set("Authorization", "Basic "+basicAuth(username, password))
   254  	}
   255  
   256  	if !deadline.IsZero() {
   257  		forkReq()
   258  	}
   259  	stopTimer, didTimeout := setRequestCancel(req, rt, deadline)
   260  
   261  	resp, err = rt.RoundTrip(req)
   262  	if err != nil {
   263  		stopTimer()
   264  		if resp != nil {
   265  			log.Printf("RoundTripper returned a response & error; ignoring response")
   266  		}
   267  		if tlsErr, ok := err.(tls.RecordHeaderError); ok {
   268  			// If we get a bad TLS record header, check to see if the
   269  			// response looks like HTTP and give a more helpful error.
   270  			// See golang.org/issue/11111.
   271  			if string(tlsErr.RecordHeader[:]) == "HTTP/" {
   272  				err = ErrSchemeMismatch
   273  			}
   274  		}
   275  		return nil, didTimeout, err
   276  	}
   277  	if resp == nil {
   278  		return nil, didTimeout, fmt.Errorf("http: RoundTripper implementation (%T) returned a nil *Response with a nil error", rt)
   279  	}
   280  	if resp.Body == nil {
   281  		// The documentation on the Body field says “The http Client and Transport
   282  		// guarantee that Body is always non-nil, even on responses without a body
   283  		// or responses with a zero-length body.” Unfortunately, we didn't document
   284  		// that same constraint for arbitrary RoundTripper implementations, and
   285  		// RoundTripper implementations in the wild (mostly in tests) assume that
   286  		// they can use a nil Body to mean an empty one (similar to Request.Body).
   287  		// (See https://golang.org/issue/38095.)
   288  		//
   289  		// If the ContentLength allows the Body to be empty, fill in an empty one
   290  		// here to ensure that it is non-nil.
   291  		if resp.ContentLength > 0 && req.Method != "HEAD" {
   292  			return nil, didTimeout, fmt.Errorf("http: RoundTripper implementation (%T) returned a *Response with content length %d but a nil Body", rt, resp.ContentLength)
   293  		}
   294  		resp.Body = io.NopCloser(strings.NewReader(""))
   295  	}
   296  	if !deadline.IsZero() {
   297  		resp.Body = &cancelTimerBody{
   298  			stop:          stopTimer,
   299  			rc:            resp.Body,
   300  			reqDidTimeout: didTimeout,
   301  		}
   302  	}
   303  	return resp, nil, nil
   304  }
   305  
   306  // timeBeforeContextDeadline reports whether the non-zero Time t is
   307  // before ctx's deadline, if any. If ctx does not have a deadline, it
   308  // always reports true (the deadline is considered infinite).
   309  func timeBeforeContextDeadline(t time.Time, ctx context.Context) bool {
   310  	d, ok := ctx.Deadline()
   311  	if !ok {
   312  		return true
   313  	}
   314  	return t.Before(d)
   315  }
   316  
   317  // knownRoundTripperImpl reports whether rt is a RoundTripper that's
   318  // maintained by the Go team and known to implement the latest
   319  // optional semantics (notably contexts). The Request is used
   320  // to check whether this particular request is using an alternate protocol,
   321  // in which case we need to check the RoundTripper for that protocol.
   322  func knownRoundTripperImpl(rt RoundTripper, req *Request) bool {
   323  	switch t := rt.(type) {
   324  	case *Transport:
   325  		if altRT := t.alternateRoundTripper(req); altRT != nil {
   326  			return knownRoundTripperImpl(altRT, req)
   327  		}
   328  		return true
   329  	case *http2Transport, http2noDialH2RoundTripper:
   330  		return true
   331  	}
   332  	// There's a very minor chance of a false positive with this.
   333  	// Instead of detecting our golang.org/x/net/http2.Transport,
   334  	// it might detect a Transport type in a different http2
   335  	// package. But I know of none, and the only problem would be
   336  	// some temporarily leaked goroutines if the transport didn't
   337  	// support contexts. So this is a good enough heuristic:
   338  	if reflect.TypeOf(rt).String() == "*http2.Transport" {
   339  		return true
   340  	}
   341  	return false
   342  }
   343  
   344  // setRequestCancel sets req.Cancel and adds a deadline context to req
   345  // if deadline is non-zero. The RoundTripper's type is used to
   346  // determine whether the legacy CancelRequest behavior should be used.
   347  //
   348  // As background, there are three ways to cancel a request:
   349  // First was Transport.CancelRequest. (deprecated)
   350  // Second was Request.Cancel.
   351  // Third was Request.Context.
   352  // This function populates the second and third, and uses the first if it really needs to.
   353  func setRequestCancel(req *Request, rt RoundTripper, deadline time.Time) (stopTimer func(), didTimeout func() bool) {
   354  	if deadline.IsZero() {
   355  		return nop, alwaysFalse
   356  	}
   357  	knownTransport := knownRoundTripperImpl(rt, req)
   358  	oldCtx := req.Context()
   359  
   360  	if req.Cancel == nil && knownTransport {
   361  		// If they already had a Request.Context that's
   362  		// expiring sooner, do nothing:
   363  		if !timeBeforeContextDeadline(deadline, oldCtx) {
   364  			return nop, alwaysFalse
   365  		}
   366  
   367  		var cancelCtx func()
   368  		req.ctx, cancelCtx = context.WithDeadline(oldCtx, deadline)
   369  		return cancelCtx, func() bool { return time.Now().After(deadline) }
   370  	}
   371  	initialReqCancel := req.Cancel // the user's original Request.Cancel, if any
   372  
   373  	var cancelCtx func()
   374  	if timeBeforeContextDeadline(deadline, oldCtx) {
   375  		req.ctx, cancelCtx = context.WithDeadline(oldCtx, deadline)
   376  	}
   377  
   378  	cancel := make(chan struct{})
   379  	req.Cancel = cancel
   380  
   381  	doCancel := func() {
   382  		// The second way in the func comment above:
   383  		close(cancel)
   384  		// The first way, used only for RoundTripper
   385  		// implementations written before Go 1.5 or Go 1.6.
   386  		type canceler interface{ CancelRequest(*Request) }
   387  		if v, ok := rt.(canceler); ok {
   388  			v.CancelRequest(req)
   389  		}
   390  	}
   391  
   392  	stopTimerCh := make(chan struct{})
   393  	var once sync.Once
   394  	stopTimer = func() {
   395  		once.Do(func() {
   396  			close(stopTimerCh)
   397  			if cancelCtx != nil {
   398  				cancelCtx()
   399  			}
   400  		})
   401  	}
   402  
   403  	timer := time.NewTimer(time.Until(deadline))
   404  	var timedOut atomic.Bool
   405  
   406  	go func() {
   407  		select {
   408  		case <-initialReqCancel:
   409  			doCancel()
   410  			timer.Stop()
   411  		case <-timer.C:
   412  			timedOut.Store(true)
   413  			doCancel()
   414  		case <-stopTimerCh:
   415  			timer.Stop()
   416  		}
   417  	}()
   418  
   419  	return stopTimer, timedOut.Load
   420  }
   421  
   422  // See 2 (end of page 4) https://www.ietf.org/rfc/rfc2617.txt
   423  // "To receive authorization, the client sends the userid and password,
   424  // separated by a single colon (":") character, within a base64
   425  // encoded string in the credentials."
   426  // It is not meant to be urlencoded.
   427  func basicAuth(username, password string) string {
   428  	auth := username + ":" + password
   429  	return base64.StdEncoding.EncodeToString([]byte(auth))
   430  }
   431  
   432  // Get issues a GET to the specified URL. If the response is one of
   433  // the following redirect codes, Get follows the redirect, up to a
   434  // maximum of 10 redirects:
   435  //
   436  //	301 (Moved Permanently)
   437  //	302 (Found)
   438  //	303 (See Other)
   439  //	307 (Temporary Redirect)
   440  //	308 (Permanent Redirect)
   441  //
   442  // An error is returned if there were too many redirects or if there
   443  // was an HTTP protocol error. A non-2xx response doesn't cause an
   444  // error. Any returned error will be of type *url.Error. The url.Error
   445  // value's Timeout method will report true if the request timed out.
   446  //
   447  // When err is nil, resp always contains a non-nil resp.Body.
   448  // Caller should close resp.Body when done reading from it.
   449  //
   450  // Get is a wrapper around DefaultClient.Get.
   451  //
   452  // To make a request with custom headers, use NewRequest and
   453  // DefaultClient.Do.
   454  //
   455  // To make a request with a specified context.Context, use NewRequestWithContext
   456  // and DefaultClient.Do.
   457  func Get(url string) (resp *Response, err error) {
   458  	return DefaultClient.Get(url)
   459  }
   460  
   461  // Get issues a GET to the specified URL. If the response is one of the
   462  // following redirect codes, Get follows the redirect after calling the
   463  // Client's CheckRedirect function:
   464  //
   465  //	301 (Moved Permanently)
   466  //	302 (Found)
   467  //	303 (See Other)
   468  //	307 (Temporary Redirect)
   469  //	308 (Permanent Redirect)
   470  //
   471  // An error is returned if the Client's CheckRedirect function fails
   472  // or if there was an HTTP protocol error. A non-2xx response doesn't
   473  // cause an error. Any returned error will be of type *url.Error. The
   474  // url.Error value's Timeout method will report true if the request
   475  // timed out.
   476  //
   477  // When err is nil, resp always contains a non-nil resp.Body.
   478  // Caller should close resp.Body when done reading from it.
   479  //
   480  // To make a request with custom headers, use NewRequest and Client.Do.
   481  //
   482  // To make a request with a specified context.Context, use NewRequestWithContext
   483  // and Client.Do.
   484  func (c *Client) Get(url string) (resp *Response, err error) {
   485  	req, err := NewRequest("GET", url, nil)
   486  	if err != nil {
   487  		return nil, err
   488  	}
   489  	return c.Do(req)
   490  }
   491  
   492  func alwaysFalse() bool { return false }
   493  
   494  // ErrUseLastResponse can be returned by Client.CheckRedirect hooks to
   495  // control how redirects are processed. If returned, the next request
   496  // is not sent and the most recent response is returned with its body
   497  // unclosed.
   498  var ErrUseLastResponse = errors.New("net/http: use last response")
   499  
   500  // checkRedirect calls either the user's configured CheckRedirect
   501  // function, or the default.
   502  func (c *Client) checkRedirect(req *Request, via []*Request) error {
   503  	fn := c.CheckRedirect
   504  	if fn == nil {
   505  		fn = defaultCheckRedirect
   506  	}
   507  	return fn(req, via)
   508  }
   509  
   510  // redirectBehavior describes what should happen when the
   511  // client encounters a 3xx status code from the server.
   512  func redirectBehavior(reqMethod string, resp *Response, ireq *Request) (redirectMethod string, shouldRedirect, includeBody bool) {
   513  	switch resp.StatusCode {
   514  	case 301, 302, 303:
   515  		redirectMethod = reqMethod
   516  		shouldRedirect = true
   517  		includeBody = false
   518  
   519  		// RFC 2616 allowed automatic redirection only with GET and
   520  		// HEAD requests. RFC 7231 lifts this restriction, but we still
   521  		// restrict other methods to GET to maintain compatibility.
   522  		// See Issue 18570.
   523  		if reqMethod != "GET" && reqMethod != "HEAD" {
   524  			redirectMethod = "GET"
   525  		}
   526  	case 307, 308:
   527  		redirectMethod = reqMethod
   528  		shouldRedirect = true
   529  		includeBody = true
   530  
   531  		if ireq.GetBody == nil && ireq.outgoingLength() != 0 {
   532  			// We had a request body, and 307/308 require
   533  			// re-sending it, but GetBody is not defined. So just
   534  			// return this response to the user instead of an
   535  			// error, like we did in Go 1.7 and earlier.
   536  			shouldRedirect = false
   537  		}
   538  	}
   539  	return redirectMethod, shouldRedirect, includeBody
   540  }
   541  
   542  // urlErrorOp returns the (*url.Error).Op value to use for the
   543  // provided (*Request).Method value.
   544  func urlErrorOp(method string) string {
   545  	if method == "" {
   546  		return "Get"
   547  	}
   548  	if lowerMethod, ok := ascii.ToLower(method); ok {
   549  		return method[:1] + lowerMethod[1:]
   550  	}
   551  	return method
   552  }
   553  
   554  // Do sends an HTTP request and returns an HTTP response, following
   555  // policy (such as redirects, cookies, auth) as configured on the
   556  // client.
   557  //
   558  // An error is returned if caused by client policy (such as
   559  // CheckRedirect), or failure to speak HTTP (such as a network
   560  // connectivity problem). A non-2xx status code doesn't cause an
   561  // error.
   562  //
   563  // If the returned error is nil, the Response will contain a non-nil
   564  // Body which the user is expected to close. If the Body is not both
   565  // read to EOF and closed, the Client's underlying RoundTripper
   566  // (typically Transport) may not be able to re-use a persistent TCP
   567  // connection to the server for a subsequent "keep-alive" request.
   568  //
   569  // The request Body, if non-nil, will be closed by the underlying
   570  // Transport, even on errors.
   571  //
   572  // On error, any Response can be ignored. A non-nil Response with a
   573  // non-nil error only occurs when CheckRedirect fails, and even then
   574  // the returned Response.Body is already closed.
   575  //
   576  // Generally Get, Post, or PostForm will be used instead of Do.
   577  //
   578  // If the server replies with a redirect, the Client first uses the
   579  // CheckRedirect function to determine whether the redirect should be
   580  // followed. If permitted, a 301, 302, or 303 redirect causes
   581  // subsequent requests to use HTTP method GET
   582  // (or HEAD if the original request was HEAD), with no body.
   583  // A 307 or 308 redirect preserves the original HTTP method and body,
   584  // provided that the Request.GetBody function is defined.
   585  // The NewRequest function automatically sets GetBody for common
   586  // standard library body types.
   587  //
   588  // Any returned error will be of type *url.Error. The url.Error
   589  // value's Timeout method will report true if the request timed out.
   590  func (c *Client) Do(req *Request) (*Response, error) {
   591  	return c.do(req)
   592  }
   593  
   594  var testHookClientDoResult func(retres *Response, reterr error)
   595  
   596  func (c *Client) do(req *Request) (retres *Response, reterr error) {
   597  	if testHookClientDoResult != nil {
   598  		defer func() { testHookClientDoResult(retres, reterr) }()
   599  	}
   600  	if req.URL == nil {
   601  		req.closeBody()
   602  		return nil, &url.Error{
   603  			Op:  urlErrorOp(req.Method),
   604  			Err: errors.New("http: nil Request.URL"),
   605  		}
   606  	}
   607  
   608  	var (
   609  		deadline      = c.deadline()
   610  		reqs          []*Request
   611  		resp          *Response
   612  		copyHeaders   = c.makeHeadersCopier(req)
   613  		reqBodyClosed = false // have we closed the current req.Body?
   614  
   615  		// Redirect behavior:
   616  		redirectMethod string
   617  		includeBody    bool
   618  	)
   619  	uerr := func(err error) error {
   620  		// the body may have been closed already by c.send()
   621  		if !reqBodyClosed {
   622  			req.closeBody()
   623  		}
   624  		var urlStr string
   625  		if resp != nil && resp.Request != nil {
   626  			urlStr = stripPassword(resp.Request.URL)
   627  		} else {
   628  			urlStr = stripPassword(req.URL)
   629  		}
   630  		return &url.Error{
   631  			Op:  urlErrorOp(reqs[0].Method),
   632  			URL: urlStr,
   633  			Err: err,
   634  		}
   635  	}
   636  	for {
   637  		// For all but the first request, create the next
   638  		// request hop and replace req.
   639  		if len(reqs) > 0 {
   640  			loc := resp.Header.Get("Location")
   641  			if loc == "" {
   642  				// While most 3xx responses include a Location, it is not
   643  				// required and 3xx responses without a Location have been
   644  				// observed in the wild. See issues #17773 and #49281.
   645  				return resp, nil
   646  			}
   647  			u, err := req.URL.Parse(loc)
   648  			if err != nil {
   649  				resp.closeBody()
   650  				return nil, uerr(fmt.Errorf("failed to parse Location header %q: %v", loc, err))
   651  			}
   652  			host := ""
   653  			if req.Host != "" && req.Host != req.URL.Host {
   654  				// If the caller specified a custom Host header and the
   655  				// redirect location is relative, preserve the Host header
   656  				// through the redirect. See issue #22233.
   657  				if u, _ := url.Parse(loc); u != nil && !u.IsAbs() {
   658  					host = req.Host
   659  				}
   660  			}
   661  			ireq := reqs[0]
   662  			req = &Request{
   663  				Method:   redirectMethod,
   664  				Response: resp,
   665  				URL:      u,
   666  				Header:   make(Header),
   667  				Host:     host,
   668  				Cancel:   ireq.Cancel,
   669  				ctx:      ireq.ctx,
   670  			}
   671  			if includeBody && ireq.GetBody != nil {
   672  				req.Body, err = ireq.GetBody()
   673  				if err != nil {
   674  					resp.closeBody()
   675  					return nil, uerr(err)
   676  				}
   677  				req.ContentLength = ireq.ContentLength
   678  			}
   679  
   680  			// Copy original headers before setting the Referer,
   681  			// in case the user set Referer on their first request.
   682  			// If they really want to override, they can do it in
   683  			// their CheckRedirect func.
   684  			copyHeaders(req)
   685  
   686  			// Add the Referer header from the most recent
   687  			// request URL to the new one, if it's not https->http:
   688  			if ref := refererForURL(reqs[len(reqs)-1].URL, req.URL, req.Header.Get("Referer")); ref != "" {
   689  				req.Header.Set("Referer", ref)
   690  			}
   691  			err = c.checkRedirect(req, reqs)
   692  
   693  			// Sentinel error to let users select the
   694  			// previous response, without closing its
   695  			// body. See Issue 10069.
   696  			if err == ErrUseLastResponse {
   697  				return resp, nil
   698  			}
   699  
   700  			// Close the previous response's body. But
   701  			// read at least some of the body so if it's
   702  			// small the underlying TCP connection will be
   703  			// re-used. No need to check for errors: if it
   704  			// fails, the Transport won't reuse it anyway.
   705  			const maxBodySlurpSize = 2 << 10
   706  			if resp.ContentLength == -1 || resp.ContentLength <= maxBodySlurpSize {
   707  				io.CopyN(io.Discard, resp.Body, maxBodySlurpSize)
   708  			}
   709  			resp.Body.Close()
   710  
   711  			if err != nil {
   712  				// Special case for Go 1 compatibility: return both the response
   713  				// and an error if the CheckRedirect function failed.
   714  				// See https://golang.org/issue/3795
   715  				// The resp.Body has already been closed.
   716  				ue := uerr(err)
   717  				ue.(*url.Error).URL = loc
   718  				return resp, ue
   719  			}
   720  		}
   721  
   722  		reqs = append(reqs, req)
   723  		var err error
   724  		var didTimeout func() bool
   725  		if resp, didTimeout, err = c.send(req, deadline); err != nil {
   726  			// c.send() always closes req.Body
   727  			reqBodyClosed = true
   728  			if !deadline.IsZero() && didTimeout() {
   729  				err = &httpError{
   730  					err:     err.Error() + " (Client.Timeout exceeded while awaiting headers)",
   731  					timeout: true,
   732  				}
   733  			}
   734  			return nil, uerr(err)
   735  		}
   736  
   737  		var shouldRedirect bool
   738  		redirectMethod, shouldRedirect, includeBody = redirectBehavior(req.Method, resp, reqs[0])
   739  		if !shouldRedirect {
   740  			return resp, nil
   741  		}
   742  
   743  		req.closeBody()
   744  	}
   745  }
   746  
   747  // makeHeadersCopier makes a function that copies headers from the
   748  // initial Request, ireq. For every redirect, this function must be called
   749  // so that it can copy headers into the upcoming Request.
   750  func (c *Client) makeHeadersCopier(ireq *Request) func(*Request) {
   751  	// The headers to copy are from the very initial request.
   752  	// We use a closured callback to keep a reference to these original headers.
   753  	var (
   754  		ireqhdr  = cloneOrMakeHeader(ireq.Header)
   755  		icookies map[string][]*Cookie
   756  	)
   757  	if c.Jar != nil && ireq.Header.Get("Cookie") != "" {
   758  		icookies = make(map[string][]*Cookie)
   759  		for _, c := range ireq.Cookies() {
   760  			icookies[c.Name] = append(icookies[c.Name], c)
   761  		}
   762  	}
   763  
   764  	preq := ireq // The previous request
   765  	return func(req *Request) {
   766  		// If Jar is present and there was some initial cookies provided
   767  		// via the request header, then we may need to alter the initial
   768  		// cookies as we follow redirects since each redirect may end up
   769  		// modifying a pre-existing cookie.
   770  		//
   771  		// Since cookies already set in the request header do not contain
   772  		// information about the original domain and path, the logic below
   773  		// assumes any new set cookies override the original cookie
   774  		// regardless of domain or path.
   775  		//
   776  		// See https://golang.org/issue/17494
   777  		if c.Jar != nil && icookies != nil {
   778  			var changed bool
   779  			resp := req.Response // The response that caused the upcoming redirect
   780  			for _, c := range resp.Cookies() {
   781  				if _, ok := icookies[c.Name]; ok {
   782  					delete(icookies, c.Name)
   783  					changed = true
   784  				}
   785  			}
   786  			if changed {
   787  				ireqhdr.Del("Cookie")
   788  				var ss []string
   789  				for _, cs := range icookies {
   790  					for _, c := range cs {
   791  						ss = append(ss, c.Name+"="+c.Value)
   792  					}
   793  				}
   794  				sort.Strings(ss) // Ensure deterministic headers
   795  				ireqhdr.Set("Cookie", strings.Join(ss, "; "))
   796  			}
   797  		}
   798  
   799  		// Copy the initial request's Header values
   800  		// (at least the safe ones).
   801  		for k, vv := range ireqhdr {
   802  			if shouldCopyHeaderOnRedirect(k, preq.URL, req.URL) {
   803  				req.Header[k] = vv
   804  			}
   805  		}
   806  
   807  		preq = req // Update previous Request with the current request
   808  	}
   809  }
   810  
   811  func defaultCheckRedirect(req *Request, via []*Request) error {
   812  	if len(via) >= 10 {
   813  		return errors.New("stopped after 10 redirects")
   814  	}
   815  	return nil
   816  }
   817  
   818  // Post issues a POST to the specified URL.
   819  //
   820  // Caller should close resp.Body when done reading from it.
   821  //
   822  // If the provided body is an io.Closer, it is closed after the
   823  // request.
   824  //
   825  // Post is a wrapper around DefaultClient.Post.
   826  //
   827  // To set custom headers, use NewRequest and DefaultClient.Do.
   828  //
   829  // See the Client.Do method documentation for details on how redirects
   830  // are handled.
   831  //
   832  // To make a request with a specified context.Context, use NewRequestWithContext
   833  // and DefaultClient.Do.
   834  func Post(url, contentType string, body io.Reader) (resp *Response, err error) {
   835  	return DefaultClient.Post(url, contentType, body)
   836  }
   837  
   838  // Post issues a POST to the specified URL.
   839  //
   840  // Caller should close resp.Body when done reading from it.
   841  //
   842  // If the provided body is an io.Closer, it is closed after the
   843  // request.
   844  //
   845  // To set custom headers, use NewRequest and Client.Do.
   846  //
   847  // To make a request with a specified context.Context, use NewRequestWithContext
   848  // and Client.Do.
   849  //
   850  // See the Client.Do method documentation for details on how redirects
   851  // are handled.
   852  func (c *Client) Post(url, contentType string, body io.Reader) (resp *Response, err error) {
   853  	req, err := NewRequest("POST", url, body)
   854  	if err != nil {
   855  		return nil, err
   856  	}
   857  	req.Header.Set("Content-Type", contentType)
   858  	return c.Do(req)
   859  }
   860  
   861  // PostForm issues a POST to the specified URL, with data's keys and
   862  // values URL-encoded as the request body.
   863  //
   864  // The Content-Type header is set to application/x-www-form-urlencoded.
   865  // To set other headers, use NewRequest and DefaultClient.Do.
   866  //
   867  // When err is nil, resp always contains a non-nil resp.Body.
   868  // Caller should close resp.Body when done reading from it.
   869  //
   870  // PostForm is a wrapper around DefaultClient.PostForm.
   871  //
   872  // See the Client.Do method documentation for details on how redirects
   873  // are handled.
   874  //
   875  // To make a request with a specified context.Context, use NewRequestWithContext
   876  // and DefaultClient.Do.
   877  func PostForm(url string, data url.Values) (resp *Response, err error) {
   878  	return DefaultClient.PostForm(url, data)
   879  }
   880  
   881  // PostForm issues a POST to the specified URL,
   882  // with data's keys and values URL-encoded as the request body.
   883  //
   884  // The Content-Type header is set to application/x-www-form-urlencoded.
   885  // To set other headers, use NewRequest and Client.Do.
   886  //
   887  // When err is nil, resp always contains a non-nil resp.Body.
   888  // Caller should close resp.Body when done reading from it.
   889  //
   890  // See the Client.Do method documentation for details on how redirects
   891  // are handled.
   892  //
   893  // To make a request with a specified context.Context, use NewRequestWithContext
   894  // and Client.Do.
   895  func (c *Client) PostForm(url string, data url.Values) (resp *Response, err error) {
   896  	return c.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
   897  }
   898  
   899  // Head issues a HEAD to the specified URL. If the response is one of
   900  // the following redirect codes, Head follows the redirect, up to a
   901  // maximum of 10 redirects:
   902  //
   903  //	301 (Moved Permanently)
   904  //	302 (Found)
   905  //	303 (See Other)
   906  //	307 (Temporary Redirect)
   907  //	308 (Permanent Redirect)
   908  //
   909  // Head is a wrapper around DefaultClient.Head.
   910  //
   911  // To make a request with a specified context.Context, use NewRequestWithContext
   912  // and DefaultClient.Do.
   913  func Head(url string) (resp *Response, err error) {
   914  	return DefaultClient.Head(url)
   915  }
   916  
   917  // Head issues a HEAD to the specified URL. If the response is one of the
   918  // following redirect codes, Head follows the redirect after calling the
   919  // Client's CheckRedirect function:
   920  //
   921  //	301 (Moved Permanently)
   922  //	302 (Found)
   923  //	303 (See Other)
   924  //	307 (Temporary Redirect)
   925  //	308 (Permanent Redirect)
   926  //
   927  // To make a request with a specified context.Context, use NewRequestWithContext
   928  // and Client.Do.
   929  func (c *Client) Head(url string) (resp *Response, err error) {
   930  	req, err := NewRequest("HEAD", url, nil)
   931  	if err != nil {
   932  		return nil, err
   933  	}
   934  	return c.Do(req)
   935  }
   936  
   937  // CloseIdleConnections closes any connections on its Transport which
   938  // were previously connected from previous requests but are now
   939  // sitting idle in a "keep-alive" state. It does not interrupt any
   940  // connections currently in use.
   941  //
   942  // If the Client's Transport does not have a CloseIdleConnections method
   943  // then this method does nothing.
   944  func (c *Client) CloseIdleConnections() {
   945  	type closeIdler interface {
   946  		CloseIdleConnections()
   947  	}
   948  	if tr, ok := c.transport().(closeIdler); ok {
   949  		tr.CloseIdleConnections()
   950  	}
   951  }
   952  
   953  // cancelTimerBody is an io.ReadCloser that wraps rc with two features:
   954  //  1. On Read error or close, the stop func is called.
   955  //  2. On Read failure, if reqDidTimeout is true, the error is wrapped and
   956  //     marked as net.Error that hit its timeout.
   957  type cancelTimerBody struct {
   958  	stop          func() // stops the time.Timer waiting to cancel the request
   959  	rc            io.ReadCloser
   960  	reqDidTimeout func() bool
   961  }
   962  
   963  func (b *cancelTimerBody) Read(p []byte) (n int, err error) {
   964  	n, err = b.rc.Read(p)
   965  	if err == nil {
   966  		return n, nil
   967  	}
   968  	if err == io.EOF {
   969  		return n, err
   970  	}
   971  	if b.reqDidTimeout() {
   972  		err = &httpError{
   973  			err:     err.Error() + " (Client.Timeout or context cancellation while reading body)",
   974  			timeout: true,
   975  		}
   976  	}
   977  	return n, err
   978  }
   979  
   980  func (b *cancelTimerBody) Close() error {
   981  	err := b.rc.Close()
   982  	b.stop()
   983  	return err
   984  }
   985  
   986  func shouldCopyHeaderOnRedirect(headerKey string, initial, dest *url.URL) bool {
   987  	switch CanonicalHeaderKey(headerKey) {
   988  	case "Authorization", "Www-Authenticate", "Cookie", "Cookie2":
   989  		// Permit sending auth/cookie headers from "foo.com"
   990  		// to "sub.foo.com".
   991  
   992  		// Note that we don't send all cookies to subdomains
   993  		// automatically. This function is only used for
   994  		// Cookies set explicitly on the initial outgoing
   995  		// client request. Cookies automatically added via the
   996  		// CookieJar mechanism continue to follow each
   997  		// cookie's scope as set by Set-Cookie. But for
   998  		// outgoing requests with the Cookie header set
   999  		// directly, we don't know their scope, so we assume
  1000  		// it's for *.domain.com.
  1001  
  1002  		ihost := idnaASCIIFromURL(initial)
  1003  		dhost := idnaASCIIFromURL(dest)
  1004  		return isDomainOrSubdomain(dhost, ihost)
  1005  	}
  1006  	// All other headers are copied:
  1007  	return true
  1008  }
  1009  
  1010  // isDomainOrSubdomain reports whether sub is a subdomain (or exact
  1011  // match) of the parent domain.
  1012  //
  1013  // Both domains must already be in canonical form.
  1014  func isDomainOrSubdomain(sub, parent string) bool {
  1015  	if sub == parent {
  1016  		return true
  1017  	}
  1018  	// If sub contains a :, it's probably an IPv6 address (and is definitely not a hostname).
  1019  	// Don't check the suffix in this case, to avoid matching the contents of a IPv6 zone.
  1020  	// For example, "::1%.www.example.com" is not a subdomain of "www.example.com".
  1021  	if strings.ContainsAny(sub, ":%") {
  1022  		return false
  1023  	}
  1024  	// If sub is "foo.example.com" and parent is "example.com",
  1025  	// that means sub must end in "."+parent.
  1026  	// Do it without allocating.
  1027  	if !strings.HasSuffix(sub, parent) {
  1028  		return false
  1029  	}
  1030  	return sub[len(sub)-len(parent)-1] == '.'
  1031  }
  1032  
  1033  func stripPassword(u *url.URL) string {
  1034  	_, passSet := u.User.Password()
  1035  	if passSet {
  1036  		return strings.Replace(u.String(), u.User.String()+"@", u.User.Username()+":***@", 1)
  1037  	}
  1038  	return u.String()
  1039  }