Documentation
¶
Index ¶
- Constants
- Variables
- func Client(follow bool) *http.Client
- func DecodeContent(h http.Header, body []byte) ([]byte, error)
- func DecodeText(h http.Header, body []byte) string
- func DecodingTransport(base http.RoundTripper) http.RoundTripper
- func EnvProxies() map[string]string
- func IsCertificateError(err error) bool
- func JSONBody(v any) ([]byte, error)
- func MergeQuery(rawURL string, query url.Values) string
- func NoProxyBypass(noProxy, host string) bool
- func Proxies() map[string]string
- func Proxy(req *http.Request) (*url.URL, error)
- func ProxyBypass(host string) bool
- func RedirectPolicy(follow bool) func(*http.Request, []*http.Request) error
- func RetryAfter(h http.Header) (time.Duration, bool)
- func RetryAfterValue(v any) (time.Duration, bool)
- func SystemProxies() map[string]string
- func SystemProxyBypass(host string) bool
- func TimeoutTransport(base http.RoundTripper, t Timeout) http.RoundTripper
- func Transport() http.RoundTripper
- func TrustEnvCA(t *http.Transport) http.RoundTripper
- type Response
- type StatusError
- type StreamResponse
- type Timeout
- type TimeoutError
Constants ¶
const ( // Waiting for a free connection from the pool PhasePool = "pool" // Name lookup, dialing, a proxy tunnel and the TLS handshake PhaseConnect = "connect" // Sending the request, the limit restarting each time the connection takes more of the body PhaseWrite = "write" // Waiting for the response, then each read of its body, the limit restarting with every read and not running while the caller holds the body between reads PhaseRead = "read" )
Phases of one exchange a Timeout limits
const MaxRedirects = 30
Redirects a client following them takes before it gives up
Variables ¶
var ErrTooManyRedirects = errors.New("stopped after " + strconv.Itoa(MaxRedirects) + " redirects")
Redirect chain longer than MaxRedirects
Functions ¶
func DecodeContent ¶
Body with the Content-Encoding codings the header lists undone from the last to the first: gzip, deflate, br and zstd; decoding stops at a coding it does not know, which leaves that layer and every one beneath it as they are
func DecodeText ¶
Text of a body in the charset its Content-Type names, UTF-8 when it names none or one that is unknown, undecodable bytes as U+FFFD
func DecodingTransport ¶
func DecodingTransport(base http.RoundTripper) http.RoundTripper
Transport asking for gzip, deflate, br and zstd content when a request names no Accept-Encoding of its own, handing back every response body with those codings undone and its Content-Encoding and Content-Length removed, and sending no Authorization or Proxy-Authorization header on a redirect hop once the chain has left the origin of its first request (an upgrade of the same host from http on port 80 to https on port 443 not counting), whatever redirect policy the client follows
func EnvProxies ¶
Scheme to proxy URL map the environment alone sets, empty when it sets no proxy
func IsCertificateError ¶
Whether err holds a certificate the client refused: an unknown authority, a name or address it does not cover, a validity period the clock is outside of, or another chain check
func JSONBody ¶
Request body of v as compact UTF-8 JSON, object keys in their order, an error for a NaN or infinite number anywhere in it
func MergeQuery ¶
URL with the values merged into its query string, each key given replacing every value the URL carried for it, the query encoded with sorted keys and the fragment kept
func NoProxyBypass ¶
Whether a NO_PROXY style list exempts the host, given with or without a port: comma separated names matching themselves and their subdomains, a leading dot or *. matching subdomains only, IP addresses and CIDR ranges, each with an optional port, * matching every host, loopback hosts always exempt
func Proxies ¶
Scheme to proxy URL map with keys http and https, from the environment when it sets any proxy, else from the operating system
func Proxy ¶
Proxy for a request: the http_proxy, https_proxy and all_proxy variables when any is set, else the operating system settings; nil for a host NO_PROXY, the system bypass list or a loopback address exempts, an error for a proxy setting that cannot be used
func ProxyBypass ¶
Whether the bypass list exempts the host, given with or without a port: NO_PROXY, the operating system list when the environment sets no proxy, and loopback hosts always
func RedirectPolicy ¶
Redirect policy following up to MaxRedirects hops, the Authorization, Proxy-Authorization and Cookie headers the first request carried dropped from every hop once the chain has left that origin (an upgrade of the same host from http on port 80 to https on port 443 not counting); or handing back each redirect response itself when follow is false
func RetryAfter ¶
Wait the Retry-After header asks for, as delay seconds or an HTTP date, never negative; false when the header is absent or unreadable
func RetryAfterValue ¶
Wait a Retry-After value of any decoded type asks for: a number of seconds, or text read as the header is; false for nil, booleans and anything else
func SystemProxies ¶
Scheme to proxy URL map the operating system settings hold, empty on a platform that keeps none
func SystemProxyBypass ¶
Whether the operating system bypass list exempts the host, given with or without a port
func TimeoutTransport ¶
func TimeoutTransport(base http.RoundTripper, t Timeout) http.RoundTripper
Transport holding each request to the phase limits in t, the read limit restarting with every body read and not running between reads; a request that runs out of a limit fails with a *TimeoutError
func Transport ¶
func Transport() http.RoundTripper
Process wide transport: the settings of http.DefaultTransport with the proxy Proxy picks for each request and dials following the address family preference, connections pooled across every caller
func TrustEnvCA ¶
func TrustEnvCA(t *http.Transport) http.RoundTripper
Transport trusting the CA bundle file or directory REQUESTS_CA_BUNDLE or CURL_CA_BUNDLE names on top of the roots t trusts, t itself when neither is set or t skips verification, http.DefaultTransport standing in for a nil t; a bundle that cannot be loaded fails every TLS handshake with the reason
Types ¶
type Response ¶
type Response struct {
StatusCode int
// Status line text after the protocol, such as "404 Not Found", empty when the response was not read from the wire
Status string
Header http.Header
// URL of the request that produced the response, the last hop when redirects were followed
URL string
// Body bytes, the content codings the transport undid already undone
Content []byte
}
Response read whole: the status, the headers, the URL that answered and the body bytes
func Do ¶
Sends req through client with every phase held to its limit in t and reads the whole body; every failure is a *url.Error whose cause is a *TimeoutError when a limit ran out
func (*Response) CheckStatus ¶
Status error of the response, nil when the status is in the 2xx range
type StatusError ¶
type StatusError struct {
StatusCode int
// Status line text such as "404 Not Found", the standard text of the code when empty
Status string
URL string
// Location header of a redirect response, empty otherwise
Location string
}
Response whose status reports a failure: the code, the status line text, the URL that answered and where a redirect points
func (*StatusError) Error ¶
func (e *StatusError) Error() string
Kind of status, the status line, the URL and for a redirect its target, such as "client error 404 Not Found for https://example.com/x", a password in either URL written as xxxxx
type StreamResponse ¶
type StreamResponse struct {
Response
Body io.ReadCloser
// contains filtered or unexported fields
}
Response with its body still open: each read must return within the read limit, time between reads not counted, and Close ends the exchange
type TimeoutError ¶
type TimeoutError struct {
Phase string
// Limit that ran out, zero when unknown
Limit time.Duration
}
Phase of an exchange that outlasted its limit
func (*TimeoutError) Error ¶
func (e *TimeoutError) Error() string
The phase that timed out and its limit, such as "read timed out after 30s"
func (*TimeoutError) Is ¶
func (e *TimeoutError) Is(target error) bool
Whether target is context.DeadlineExceeded or os.ErrDeadlineExceeded, the deadline errors a phase limit stands for
func (*TimeoutError) Temporary ¶
func (e *TimeoutError) Temporary() bool
Always true: the same request may succeed when sent again
func (*TimeoutError) Timeout ¶
func (e *TimeoutError) Timeout() bool
Always true: the error is a timeout