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 ¶
- Variables
- func DoCapped(hc *http.Client, req *http.Request, limit int64, ...) ([]byte, error)
- func NoDowngrade(req *http.Request, via []*http.Request) error
- func NoFollow(*http.Request, []*http.Request) error
- func RetryAfter(v string) (at time.Time, ok bool)
- func SameOrigin(req *http.Request, via []*http.Request) error
- func WithRedirect(hc *http.Client, policy func(*http.Request, []*http.Request) error) *http.Client
- type RateLimitError
- type StatusError
- type Transport
Constants ¶
This section is empty.
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 http.DefaultClient) 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 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.
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.
Types ¶
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 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.
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. |