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