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