Source File
client.go
Belonging Package
net/http
package http
import (
)
CheckRedirect func(req *Request, via []*Request) error
var DefaultClient = &Client{}
func ( *Client) ( *Request, time.Time) ( *Response, func() bool, error) {
if .Jar != nil {
for , := range .Jar.Cookies(.URL) {
.AddCookie()
}
}
, , = send(, .transport(), )
if != nil {
return nil, ,
}
if .Jar != nil {
if := .Cookies(); len() > 0 {
.Jar.SetCookies(.URL, )
}
}
return , nil, nil
}
func ( *Client) () time.Time {
if .Timeout > 0 {
return time.Now().Add(.Timeout)
}
return time.Time{}
}
func ( *Client) () RoundTripper {
if .Transport != nil {
return .Transport
}
return DefaultTransport
}
func ( *Request, RoundTripper, time.Time) ( *Response, func() bool, error) {
:= // req is either the original request, or a modified fork
if == nil {
.closeBody()
return nil, alwaysFalse, errors.New("http: no Client.Transport or DefaultTransport")
}
if .URL == nil {
.closeBody()
return nil, alwaysFalse, errors.New("http: nil Request.URL")
}
if .RequestURI != "" {
.closeBody()
return nil, alwaysFalse, errors.New("http: Request.RequestURI can't be set in client requests")
}
if .Header == nil {
()
.Header = make(Header)
}
if := .URL.User; != nil && .Header.Get("Authorization") == "" {
:= .Username()
, := .Password()
()
.Header = cloneOrMakeHeader(.Header)
.Header.Set("Authorization", "Basic "+basicAuth(, ))
}
if !.IsZero() {
()
}
, := setRequestCancel(, , )
, = .RoundTrip()
if != nil {
()
if != nil {
log.Printf("RoundTripper returned a response & error; ignoring response")
}
if .ContentLength > 0 && .Method != "HEAD" {
return nil, , fmt.Errorf("http: RoundTripper implementation (%T) returned a *Response with content length %d but a nil Body", , .ContentLength)
}
.Body = io.NopCloser(strings.NewReader(""))
}
if !.IsZero() {
.Body = &cancelTimerBody{
stop: ,
rc: .Body,
reqDidTimeout: ,
}
}
return , nil, nil
}
func ( RoundTripper, *Request) bool {
switch t := .(type) {
case *Transport:
if := .alternateRoundTripper(); != nil {
return (, )
}
return true
case *http2Transport, http2noDialH2RoundTripper:
return true
func ( *Request, RoundTripper, time.Time) ( func(), func() bool) {
if .IsZero() {
return nop, alwaysFalse
}
:= knownRoundTripperImpl(, )
:= .Context()
if !timeBeforeContextDeadline(, ) {
return nop, alwaysFalse
}
var func()
.ctx, = context.WithDeadline(, )
return , func() bool { return time.Now().After() }
}
:= .Cancel // the user's original Request.Cancel, if any
var func()
if := .Context(); timeBeforeContextDeadline(, ) {
.ctx, = context.WithDeadline(, )
}
:= make(chan struct{})
.Cancel =
func (, string) string {
:= + ":" +
return base64.StdEncoding.EncodeToString([]byte())
}
func ( string) ( *Response, error) {
return DefaultClient.Get()
}
var ErrUseLastResponse = errors.New("net/http: use last response")
func ( *Client) ( *Request, []*Request) error {
:= .CheckRedirect
if == nil {
= defaultCheckRedirect
}
return (, )
}
= false
break
}
= false
}
}
return , ,
}
func ( *Client) ( *Request) (*Response, error) {
return .do()
}
var testHookClientDoResult func(retres *Response, reterr error)
func ( *Client) ( *Request) ( *Response, error) {
if testHookClientDoResult != nil {
defer func() { testHookClientDoResult(, ) }()
}
if .URL == nil {
.closeBody()
return nil, &url.Error{
Op: urlErrorOp(.Method),
Err: errors.New("http: nil Request.URL"),
}
}
var (
= .deadline()
[]*Request
*Response
= .makeHeadersCopier()
= false // have we closed the current req.Body?
()
if := refererForURL([len()-1].URL, .URL); != "" {
.Header.Set("Referer", )
}
= .checkRedirect(, )
if == ErrUseLastResponse {
return , nil
}
const = 2 << 10
if .ContentLength == -1 || .ContentLength <= {
io.CopyN(io.Discard, .Body, )
}
.Body.Close()
if .Jar != nil && != nil {
var bool
:= .Response // The response that caused the upcoming redirect
for , := range .Cookies() {
if , := [.Name]; {
delete(, .Name)
= true
}
}
if {
.Del("Cookie")
var []string
for , := range {
for , := range {
= append(, .Name+"="+.Value)
}
}
sort.Strings() // Ensure deterministic headers
.Set("Cookie", strings.Join(, "; "))
}
}
func ( string) ( *Response, error) {
return DefaultClient.Head()
}
type cancelTimerBody struct {
stop func() // stops the time.Timer waiting to cancel the request
rc io.ReadCloser
reqDidTimeout func() bool
}
func ( *cancelTimerBody) ( []byte) ( int, error) {
, = .rc.Read()
if == nil {
return , nil
}
.stop()
if == io.EOF {
return ,
}
if .reqDidTimeout() {
= &httpError{
err: .Error() + " (Client.Timeout or context cancellation while reading body)",
timeout: true,
}
}
return ,
}
func ( *cancelTimerBody) () error {
:= .rc.Close()
.stop()
return
}
func ( string, , *url.URL) bool {
switch CanonicalHeaderKey() {
:= canonicalAddr()
:= canonicalAddr()
return isDomainOrSubdomain(, )
return true
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |