Documentation
¶
Overview ¶
Package httpkit is the HTTP client plumbing a program needs once it talks to more than one site: a size-capped request, typed status and rate-limit errors, one Retry-After reader, redirect policies that keep credentials with their origin, and a Transport that sets a default User-Agent and reports every round trip.
Resumable file downloads live in the download subpackage.
Index ¶
- Constants
- Variables
- func DoCapped(hc *http.Client, req *http.Request, limit int64, ...) ([]byte, error)
- func DoJSON(hc *http.Client, req *http.Request, out any, o JSONOptions) error
- func NoDowngrade(req *http.Request, via []*http.Request) error
- func NoFollow(*http.Request, []*http.Request) error
- func RedactError(err error) error
- func RedactURL(raw string) string
- func RetryAfter(v string) (at time.Time, ok bool)
- func SameOrigin(req *http.Request, via []*http.Request) error
- func StripCrossHost(names ...string) func(*http.Request, []*http.Request) error
- func WithRedirect(hc *http.Client, policy func(*http.Request, []*http.Request) error) *http.Client
- type JSONOptions
- type RateLimitError
- type Reason
- type StatusError
- type Transport
Constants ¶
const DefaultJSONLimit = 16 << 20
DefaultJSONLimit is the body cap DoJSON applies when JSONOptions.Limit is zero.
const DefaultTimeout = 5 * time.Minute
DefaultTimeout bounds a whole request, body included, for a nil client: http.DefaultClient has none, so a server that stalls a body would hold the caller forever.
Variables ¶
var ( // ErrNotFound is matched by a 404 *StatusError. ErrNotFound = errors.New("not found") // ErrRateLimited is matched by a 429 *StatusError and by every *RateLimitError. ErrRateLimited = errors.New("rate limited") )
Functions ¶
func DoCapped ¶
func DoCapped(hc *http.Client, req *http.Request, limit int64, check func(*http.Response) error) ([]byte, error)
DoCapped sends req with hc (nil means a client with DefaultTimeout) and returns the body of a 200 answer, at most limit bytes. check (nil means none) runs on every response before the status test; a non-nil error from it is returned after the body is closed. Any other status is a *StatusError; a body over limit is safefs.ErrTooLarge. The body is always closed.
func DoJSON ¶ added in v0.2.0
DoJSON sends req with hc (nil means a client with DefaultTimeout) and decodes a 200 answer's JSON body into out. A 429 is a *RateLimitError carrying the Retry-After reset, any other status a *StatusError, a body over the limit safefs.ErrTooLarge, and malformed JSON an error wrapping the decoder's. Accept is set to application/json when req names none.
func NoDowngrade ¶
NoDowngrade refuses a redirect from https to any other scheme: net/http keeps the Authorization header across a same-host redirect, scheme change included. A first hop that is not https may redirect freely. A redirect to another host loses every credential-looking header (see StripCrossHost).
func RedactError ¶ added in v0.2.0
RedactError returns err with the URL inside a *url.Error (what net/http wraps a failed request in) redacted by RedactURL.
func RedactURL ¶ added in v0.2.0
RedactURL is raw without its query, fragment and userinfo: a query often carries a signature or an API key, and none of it belongs in an error text or a log.
func RetryAfter ¶
RetryAfter reads a Retry-After value (RFC 9110 section 10.2.3): delta-seconds from now, or an HTTP-date. ok is false when v is empty, unparseable, "0" or negative, or more than 365 days of seconds (so the Duration cannot overflow). A past HTTP-date is returned as is; the caller decides what a reset not after now means.
func SameOrigin ¶
SameOrigin refuses a redirect to another scheme or host than the first request's, so a header such as an API key stays with its origin.
func StripCrossHost ¶ added in v0.2.0
StripCrossHost returns a redirect policy that deletes the named headers, and every header whose name looks like a credential (it holds auth, key, token, secret, cookie, session, credential or password), from a request redirected to a host other than the first request's, or from https to plain http, which would send them in clear text. Once any hop has left that origin they stay stripped for the rest of the chain, as net/http does. It never refuses a redirect.
func WithRedirect ¶
WithRedirect returns a shallow copy of hc (nil means a client with DefaultTimeout) whose CheckRedirect runs hc's own policy first, when it has one, and then policy. hc is never modified. A nil hc gets a client with DefaultTimeout.
Types ¶
type JSONOptions ¶ added in v0.2.0
type JSONOptions struct {
// Limit caps the body read, in bytes; zero means DefaultJSONLimit.
Limit int64
// Check runs on every response before the status test, ahead of the 429 test, so a caller can claim any
// status itself (a 403 that means "rate limited" for one API and "not allowed" for another). A non-nil error
// is returned as is.
Check func(*http.Response) error
}
JSONOptions tunes DoJSON. The zero value is valid.
type RateLimitError ¶
RateLimitError is a server refusing calls until Reset (zero when it did not say).
func (*RateLimitError) Error ¶
func (e *RateLimitError) Error() string
func (*RateLimitError) Is ¶
func (e *RateLimitError) Is(target error) bool
Is matches ErrRateLimited.
type Reason ¶ added in v0.3.0
type Reason string
Reason is why a request did not reach the host.
const ( // ReasonDNS is a name that did not resolve. ReasonDNS Reason = "dns" // ReasonTimeout is a deadline, a connect timeout, or a net.Error that timed out. ReasonTimeout Reason = "timeout" // ReasonRefused is a connect the host rejected, including Windows WSAECONNREFUSED. ReasonRefused Reason = "refused" // ReasonOther is any other failure, including a nil error. ReasonOther Reason = "other" )
type StatusError ¶
StatusError is an answer whose status the caller did not accept.
func (*StatusError) Error ¶
func (e *StatusError) Error() string
func (*StatusError) Is ¶
func (e *StatusError) Is(target error) bool
Is matches ErrNotFound for a 404 and ErrRateLimited for a 429.
type Transport ¶
type Transport struct {
Base http.RoundTripper // Nil means http.DefaultTransport.
UserAgent string // Set only when the request has none.
// OnResult sees every round trip's response or transport error; nil means none. It must not read resp.Body.
OnResult func(req *http.Request, resp *http.Response, err error)
}
Transport sets a default User-Agent and reports every round trip.
func (*Transport) CloseIdleConnections ¶
func (t *Transport) CloseIdleConnections()
CloseIdleConnections forwards to Base when Base has the method, so http.Client.CloseIdleConnections still works.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package download fetches large files over flaky links: a resumable, verified, throttled File, and a RangeReader that reads a remote file's parts without downloading all of it.
|
Package download fetches large files over flaky links: a resumable, verified, throttled File, and a RangeReader that reads a remote file's parts without downloading all of it. |