httpclient

package
v2.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2026 License: GPL-3.0 Imports: 8 Imported by: 0

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

View Source
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.

View Source
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

View Source
var ErrTooLarge = errors.New("httpclient: response body exceeds size cap")

ErrTooLarge is returned when a response body exceeds the configured cap.

Functions

func AtomicWrite

func AtomicWrite(destPath string, fn func(io.Writer) error) error

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

func CheckStatus(resp *http.Response, fetchURL string) error

CheckStatus returns a descriptive error if resp.StatusCode is not 2xx. The caller still owns resp.Body.Close().

func Client

func Client() *http.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

func FetchBytes(ctx context.Context, url string, maxBytes int64) ([]byte, error)

FetchBytes downloads url and returns the body, capped at maxBytes. A non-positive maxBytes falls back to DefaultMaxBytes.

func FetchToFile

func FetchToFile(ctx context.Context, url, destPath string, maxBytes int64) error

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

func LimitedBody(resp *http.Response) io.Reader

LimitedBody wraps resp.Body in an io.LimitReader with DefaultMaxBytes. Callers should still defer resp.Body.Close() on the original response.

func LimitedBodyN

func LimitedBodyN(resp *http.Response, maxBytes int64) io.Reader

LimitedBodyN wraps resp.Body in an io.LimitReader with the supplied cap. Use when the caller knows a tighter bound (e.g. control.tar is always small). A non-positive cap falls back to DefaultMaxBytes.

Types

type HTTPStatusError

type HTTPStatusError struct {
	Code int
	URL  string
}

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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL