Documentation
¶
Overview ¶
Package httpclient is the shared HTTP client used by aptcache, aptrepo, apkindex, and pacmandb for short metadata fetches.
It exposes one *http.Client with a global timeout so a stalled mirror cannot hang the build, plus helpers for size-capped body reads and a 2xx status check. Large package downloads use grab directly and don't route through this client.
Index ¶
- Constants
- Variables
- func AtomicWrite(destPath string, fn func(io.Writer) error) error
- func CheckStatus(resp *http.Response, fetchURL string) error
- func Client() *http.Client
- func FetchBytes(ctx context.Context, url string, maxBytes int64) ([]byte, error)
- func FetchToFile(ctx context.Context, url, destPath string, maxBytes int64) error
- func LimitedBody(resp *http.Response) io.Reader
- func LimitedBodyN(resp *http.Response, maxBytes int64) io.Reader
- type HTTPStatusError
Constants ¶
const DefaultMaxBytes int64 = 2 << 30 // 2 GiB
DefaultMaxBytes caps individual HTTP responses to 2 GiB. Package indexes are typically a few MB; .deb / .apk / .rpm files top out around a few hundred MB. The cap prevents OOM from a malicious or buggy mirror serving an unbounded stream.
const DefaultTimeout = 10 * time.Minute
DefaultTimeout bounds a complete HTTP request (connection, TLS handshake, header read, body read) at 10 minutes. Large .deb downloads over slow links should still fit; stalled mirrors will fail rather than hang.
Variables ¶
var ErrTooLarge = errors.New("httpclient: response body exceeds size cap")
ErrTooLarge is returned when a response body exceeds the configured cap.
Functions ¶
func AtomicWrite ¶
AtomicWrite writes to destPath via a temp file + rename so readers never see a partial file. fn receives a writer for the temp file; if fn returns an error the temp file is removed.
func CheckStatus ¶
CheckStatus returns a descriptive error if resp.StatusCode is not 2xx. The caller still owns resp.Body.Close().
func Client ¶
Client returns the package-wide *http.Client. Reuse by all aptcache / aptrepo / apkindex / pacmandb / aptinstall callers so timeout and redirect behaviour stays consistent.
func FetchBytes ¶
FetchBytes downloads url and returns the body, capped at maxBytes. A non-positive maxBytes falls back to DefaultMaxBytes.
func FetchToFile ¶
FetchToFile downloads url and writes it atomically to destPath, capped at maxBytes. Content-Length is checked as a cheap preflight when available. A non-positive maxBytes falls back to DefaultMaxBytes.
func LimitedBody ¶
LimitedBody wraps resp.Body in an io.LimitReader with DefaultMaxBytes. Callers should still defer resp.Body.Close() on the original response.
Types ¶
type HTTPStatusError ¶
HTTPStatusError is returned by CheckStatus when the response is not 2xx. Callers can use errors.As to extract the status code without parsing the Error() string.
func (*HTTPStatusError) Error ¶
func (e *HTTPStatusError) Error() string
Error returns the error message for HTTPStatusError.
func (*HTTPStatusError) IsClientError ¶
func (e *HTTPStatusError) IsClientError() bool
IsClientError reports whether the status is in the 4xx range. Useful for classifying transient repo failures (auth, rate-limit, not-found) as non-fatal vs systemic failures (network, 5xx, disk full).