Documentation
¶
Overview ¶
Package httpx is the HTTP layer: a shared client, retrying JSON reads and resumable-by-existence downloads.
The retry policy is ported from core/http.py's tenacity decorators rather than re-chosen. GitHub's API is polled unauthenticated here, so it is rate-limited, and loosening the policy would fail a sync that used to work.
Index ¶
Constants ¶
const ( // UserAgent identifies the toolkit to the registries it polls. UserAgent = "icon-sets-sync (+https://github.com/ichava/icon-sets-sync)" // DefaultTimeout and DefaultDownloadTimeout match core/http.py. DefaultTimeout = 30 * time.Second DefaultDownloadTimeout = 300 * time.Second // Attempts and the backoff bounds are tenacity's: // stop_after_attempt(4), wait_exponential(multiplier=2, min=2, max=30) // for JSON, and max=60 for downloads. Attempts = 4 // DefaultMaxJSONBytes caps a version-discovery or registry response. The // largest real one is a few hundred KiB; 32 MiB is two orders above that. DefaultMaxJSONBytes int64 = 32 << 20 // DefaultMaxDownloadBytes caps one archive download (S6). The largest // upstream archives are a few hundred MiB; 1 GiB leaves headroom without // letting a compromised upstream fill a runner's disk. DefaultMaxDownloadBytes int64 = 1 << 30 )
Variables ¶
This section is empty.
Functions ¶
func CheckScheme ¶ added in v0.1.3
CheckScheme enforces the https-only rule (P2-G6) on a configured URL. It knows nothing about hosts, so it is safe to run when a config loads, before the run-wide allowlist is in place.
func DefaultAllowedHosts ¶ added in v0.1.3
func DefaultAllowedHosts() []string
DefaultAllowedHosts returns the built-in allowlist, sorted. It is a copy.
func ParseHostList ¶ added in v0.1.3
ParseHostList splits the `allowed_hosts` setting (comma or space separated) into lowercase, de-duplicated host names.
func SetExtraAllowedHosts ¶ added in v0.1.3
func SetExtraAllowedHosts(hosts []string)
SetExtraAllowedHosts sets the hosts every client allows beyond the defaults. It is the run-wide `allowed_hosts` setting, applied once at startup the way the private config directory is. Passing nil clears it.
Types ¶
type Client ¶
type Client struct {
// Transport is the round tripper both the JSON and download clients are
// built on. Tests supply a stub here rather than standing up an
// httptest.Server: the sandbox this is developed in refuses to bind a
// listening socket, and a suite that silently skips there would be the
// same shape of false green this estate has been bitten by before.
Transport http.RoundTripper
// Sleep is swapped by tests so the retry path can be exercised without
// waiting out the real backoff.
Sleep func(time.Duration)
// MaxJSONBytes and MaxDownloadBytes cap what one response body may hold.
// Zero means the defaults below. Tests set them small.
MaxJSONBytes int64
MaxDownloadBytes int64
// AllowedHosts extends the built-in upstream allowlist for this client
// only. The run-wide `allowed_hosts` setting reaches every client through
// SetExtraAllowedHosts instead.
AllowedHosts []string
}
Client performs the toolkit's HTTP requests.
func (*Client) CheckURL ¶ added in v0.1.3
CheckURL is the rule for the first request of a fetch: https (or http to loopback), and a host on the allowlist (or loopback). GetJSON and Download run it before sending anything; it is exported so a test can ask the same question about a URL without fetching it.
func (*Client) Download ¶
Download fetches url to dest and returns dest.
Idempotent by existence: a non-empty file already at dest is kept unless force is set. The write goes to a sibling .tmp and is renamed, so an interrupted download never leaves a truncated file that the next run would treat as a complete cache hit.