httpclient

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httpclient provides a shared HTTP client for all external API calls. The client is safe for concurrent use and reuses connections. Call Init once during startup to set the timeout; the default is 10s.

internal/verifier's reads outside the document, such as the keyring read and the sources' identity reads of SPEC.md section 10.2, step 0 (CONFIGURATION), do not use the shared client: each goes through a SingleRead, one attempt that follows no redirect and ends within one deadline (SPEC.md sections 8.3 (External verifiers) and 10.4 (Chain and source reads)). The keys commands' keyring read, through internal/external, uses the shared client and its redirect policy.

Index

Constants

View Source
const (
	// DefaultRetryAfter is the wait before the one retry of a 429 that
	// named no delay at all.
	DefaultRetryAfter = 2 * time.Second

	// MaxRetryAfter bounds the wait the CLI will sit through before its
	// one retry. The per-minute limiters name at most about a minute; a
	// refusal asking for longer (a daily quota, say) is surfaced at once,
	// with the wait it named, rather than parking the command.
	MaxRetryAfter = 60 * time.Second

	// RetryJitter is the most random delay added on top of the wait a
	// refusal named. The server's per-address guard (and the OAuth
	// endpoints' per-address limit) is a window aligned to the clock
	// minute, so every client it refuses in a given minute is told the
	// same second and, retrying on that exact second, would arrive at the
	// boundary together with all the others (truestamp-v2
	// kb/api/json-api.md: "add a little random jitter to its Retry-After
	// rather than retrying on the exact second"). The per-caller budget
	// is a token bucket whose Retry-After is the caller's own short wait,
	// where the draw is merely harmless. A uniform draw from
	// [0, RetryJitter) spreads the herd over a couple of seconds without
	// making a short wait meaningfully longer. It is applied after
	// BoundRetry has decided, so it never turns a retry into a refusal.
	RetryJitter = 2 * time.Second
)
View Source
const DefaultMaxDownloadSize = 200 << 20

DefaultMaxDownloadSize caps a single DownloadCtx response at 200 MB, comfortably larger than any truestamp release tarball today, small enough to prevent a runaway redirect from filling the disk.

View Source
const MaxResponseSize = 1 << 20

MaxResponseSize limits HTTP response bodies to 1 MB to prevent OOM.

Variables

View Source
var ErrRedirectDowngrade = errors.New("refusing redirect that downgrades https to http")

ErrRedirectDowngrade is returned (wrapped in a *url.Error by net/http) when a server tries to redirect an https request to a plaintext http URL. Following such a redirect would strip TLS mid-chain, which for the keyring fetch in particular is a total compromise: an attacker who can substitute the keyring can substitute signing keys, and every downstream signature then validates against their key. See FetchKeyring's trust model in internal/external/keyring.go.

Functions

func BoundRetry added in v0.16.0

func BoundRetry(delay time.Duration, named bool) (time.Duration, bool)

BoundRetry decides whether a 429 is retried, and after how long. named is false when the refusal named no delay, in which case DefaultRetryAfter applies. The result is false when the wait is over MaxRetryAfter; the delay is still returned so a caller can show it.

func Discard added in v0.16.0

func Discard(resp *http.Response)

Discard drains (up to 64 KiB) and closes a response body that is being thrown away, so the connection can be reused for the retry.

func Do

func Do(req *http.Request) (*http.Response, error)

Do executes an HTTP request using the shared client. The request's existing context.Context (if any) is respected; callers that want cancellation should attach one via http.Request.WithContext before calling. A request whose context WithBound made is sent without the shared client's timeout, under its context's bound alone, with the same transport and redirect policy. If SetUserAgent has been called and the request has no User-Agent header, the configured value is applied.

func DownloadAtMostCtx added in v0.17.0

func DownloadAtMostCtx(ctx context.Context, url string, limit int64) ([]byte, error)

DownloadAtMostCtx is DownloadBytesCtx for a caller that bounds its input itself: it reads at most limit octets of the body and refuses none for its length, so a longer body gives its first limit octets. `truestamp verify` reads a proof this way, at most MAX_READ_OCTETS + 1 octets, so that J1 alone decides that a proof is too large (SPEC.md section 10.1).

func DownloadBytesCtx added in v0.3.1

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

DownloadBytesCtx is like DownloadCtx but returns the body in memory. Intended for small artifacts (checksums.txt, signature bundles) that exceed MaxResponseSize occasionally but are still known to be small (<1 MB). Pass 0 for maxBytes to default to 1 MB.

func DownloadCtx added in v0.3.1

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

DownloadCtx streams the body of a GET request to destPath. The destination is created (or truncated) with 0644 permissions. The response body is read through an io.LimitReader capped at maxBytes; pass 0 (or a negative value) to use DefaultMaxDownloadSize.

Unlike GetJSONCtx, this function is safe for multi-MB responses and never buffers the full body in memory.

Returns the number of bytes written on success.

func GetJSON

func GetJSON(url string) ([]byte, error)

GetJSON performs a GET request with context.Background and returns the response body. Prefer GetJSONCtx when a cancellable context is available (e.g. from Cobra's cmd.Context()).

func GetJSONCtx added in v0.3.0

func GetJSONCtx(ctx context.Context, url string) ([]byte, error)

GetJSONCtx performs a context-aware GET request and returns the response body. Errors are typed: *TransportError before a response exists, *StatusError for a non-2xx status, *TruncatedError for a body over MaxResponseSize. Their Error() strings are unchanged from the untyped versions they replaced.

func Init

func Init(timeout time.Duration)

Init creates a new HTTP client with the given timeout. Must be called once during startup before any external calls. The redirect policy in [checkRedirect] is reinstalled here; building a bare http.Client instead would silently drop it.

func IsTransport added in v0.12.0

func IsTransport(err error) bool

IsTransport reports whether err is (or wraps) a *TransportError.

func IsTruncated added in v0.12.0

func IsTruncated(err error) bool

IsTruncated reports whether err is (or wraps) a *TruncatedError.

func Jitter added in v0.16.0

func Jitter(delay time.Duration) time.Duration

Jitter returns delay plus a random amount in [0, RetryJitter). Both retry sites, internal/jsonapi.Send and the transport below, apply it to the wait they are about to sleep.

func NewAttemptTransport added in v0.16.0

func NewAttemptTransport(timeout time.Duration) http.RoundTripper

NewAttemptTransport returns a copy of the default transport that gives up on a single attempt whose response headers have not arrived within timeout. It is the per-attempt bound to pair with NewRetryAfterTransport.

func NewRetryAfterTransport added in v0.16.0

func NewRetryAfterTransport(base http.RoundTripper) http.RoundTripper

NewRetryAfterTransport wraps base (http.DefaultTransport when nil) with the retry-once-on-429 behaviour above. internal/auth installs it on the clients that call the OAuth token and revocation endpoints. Give the client no http.Client.Timeout: that bound spans the whole call, wait included, and would cancel the very wait the refusal asked for; bound each attempt on the base transport instead (see NewAttemptTransport).

func ParseRetryAfter added in v0.16.0

func ParseRetryAfter(value string, now time.Time) (delay time.Duration, ok bool)

ParseRetryAfter parses a Retry-After header value (RFC 9110 §10.2.3): a non-negative integer number of seconds, or an HTTP-date, in which case the delay is measured from now. ok is false for an absent or malformed value. A date already in the past parses as a zero delay.

func Rewind added in v0.16.0

func Rewind(req *http.Request) (*http.Request, bool)

Rewind returns a copy of req that can be sent again, or false when the body was streamed and cannot be reproduced. Requests built by http.NewRequest from a bytes or strings reader carry GetBody and rewind; an arbitrary io.Reader does not.

func SetTransport added in v0.9.0

func SetTransport(rt http.RoundTripper)

SetTransport installs rt as the round-tripper for the shared client. The CLI uses it to layer the auth package's reactive 401 → refresh → retry-once behavior on top of the default transport. Call after Init, which replaces the client.

func SetUserAgent added in v0.7.0

func SetUserAgent(version string)

SetUserAgent configures the User-Agent header stamped onto every outbound request through Do. Typical value: "truestamp-cli/<version> (<os>/<arch>)". Pass an empty string to disable the override (requests keep whatever UA the caller set, or Go's default).

func Status added in v0.12.0

func Status(err error) int

Status returns the HTTP status carried by err, or 0 when err is not (and does not wrap) a *StatusError.

func Truncate

func Truncate(s string, maxLen int) string

Truncate shortens a string to maxLen characters, appending "..." if truncated.

func Wait added in v0.16.0

func Wait(ctx context.Context, d time.Duration) error

Wait blocks for d, or until ctx is done, whichever comes first, and returns ctx.Err() in the second case.

func WithBound added in v0.17.0

func WithBound(ctx context.Context, bound time.Duration) (context.Context, context.CancelFunc)

WithBound returns a copy of ctx that ends after bound, and that Do sends a request under alone: the shared client's timeout, the configured http_timeout, does not apply to a request made with it, whether bound is longer or shorter. The caller calls cancel once the answer is read.

Types

type SingleRead added in v0.17.0

type SingleRead struct {
	// contains filtered or unexported fields
}

SingleRead makes the reads a verifier makes outside the document, such as the keyring read of SPEC.md section 10.2, step 0 (CONFIGURATION), by the rules SPEC.md section 8.3 (External verifiers) and section 10.4 (Chain and source reads) need of a read whose answer decides a verdict. It is modeled on the Truestamp application's Truestamp.SourceCache.ReadBounds (lib/truestamp/source_cache/read_bounds.ex in truestamp-v2), which gives every verification read one attempt, no redirect followed and one deadline over the whole read:

  • one GET, with no retry;
  • no redirect followed, to any host: a 3xx is the answer, an HTTP error like any status but 200 OK, so the origin a read names is the only one that can answer it;
  • one deadline over the whole read: the connect, a TLS handshake, the wait for the headers and every wait for the body, so a source that answers slowly, or trickles its answer, ends the read when the deadline passes;
  • a bound on the body, so an answer larger than its caller can use is refused unread past the bound.

It has its own client, apart from the shared one, so neither the shared client's redirect policy nor the transport SetTransport installs (the auth package's 401 retry) applies to it. SingleRead.Get sends no credential; SingleRead.Post sends only the headers its caller gives, such as a Bitcoin Core node's RPC credentials in an Authorization header, never in the URL. An error's text can carry bytes the server sent, which a server could fill with the headers it was sent: a *StatusError quotes at most 80 octets of the body of a status that is not 200 OK, and a *TransportError can quote the URL and whatever the transport's own error quotes, such as a malformed status line or header line. So a caller that sends credentials never quotes the text of either (internal/verifier/source gives fixed reasons, naming a status by its code alone).

func NewSingleRead added in v0.17.0

func NewSingleRead(transport http.RoundTripper, deadline time.Duration) *SingleRead

NewSingleRead returns a SingleRead whose reads go through transport, http.DefaultTransport when it is nil, and each end within deadline.

func (*SingleRead) Get added in v0.17.0

func (r *SingleRead) Get(ctx context.Context, url string, limit int) ([]byte, error)

Get reads url once, within the reader's deadline, and returns the body of a 200 OK answer of at most limit octets. Errors are typed as GetJSONCtx's are: a *TransportError for a failure before the whole body arrived (the deadline passing included), a *StatusError for any status but 200 OK (a 3xx included, unfollowed), and a *TruncatedError for a body over limit, of which at most limit + 1 octets are read.

func (*SingleRead) Post added in v0.17.0

func (r *SingleRead) Post(ctx context.Context, url string, body []byte, header http.Header, limit int) ([]byte, error)

Post sends body to url once, with header, within the reader's deadline, and returns the body of a 200 OK answer of at most limit octets, with the errors of SingleRead.Get. It is the read of a JSON-RPC call, such as a Bitcoin Core node's: header holds the call's Content-Type and any Authorization, which travel only in the request.

func (*SingleRead) PostRPC added in v0.17.0

func (r *SingleRead) PostRPC(ctx context.Context, url string, body []byte, header http.Header, limit int) ([]byte, error)

PostRPC is SingleRead.Post for a JSON-RPC 1.0 call, such as a Bitcoin Core node's: it also returns the body of a 404 Not Found or a 500 Internal Server Error answer, of at most limit octets, since such a server gives an error there and its body names the error, which the caller reads as it reads a 200 OK's. Every other status is a *StatusError, as for Post.

type StatusError added in v0.12.0

type StatusError struct {
	StatusCode int
	URL        string
	Body       string // truncated to 80 bytes
	HTML       bool   // the body looked like an HTML error page
}

StatusError is returned for any non-2xx response. It preserves the status code so a caller can tell a definitive answer (404) from an availability problem (429, 5xx).

func (*StatusError) Error added in v0.12.0

func (e *StatusError) Error() string

type TransportError added in v0.12.0

type TransportError struct {
	URL string
	Err error
}

TransportError wraps a failure that happened before any HTTP response was received: request construction, DNS resolution, dial, TLS handshake, client timeout, context cancellation, or a body that could not be read. It is kept apart from a *StatusError so that a caller can tell a server that never answered from one that refused (IsTransport), instead of the two collapsing into one opaque error.

func (*TransportError) Error added in v0.12.0

func (e *TransportError) Error() string

func (*TransportError) Unwrap added in v0.12.0

func (e *TransportError) Unwrap() error

type TruncatedError added in v0.12.0

type TruncatedError struct {
	URL   string
	Limit int
}

TruncatedError is returned when a 2xx body exceeded MaxResponseSize. Without it the cut-off bytes reach the caller's JSON decoder and surface as a syntax error, which reads as "the server sent junk" when the real cause is our own cap.

func (*TruncatedError) Error added in v0.12.0

func (e *TruncatedError) Error() string

Jump to

Keyboard shortcuts

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