httpstore

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 19 Imported by: ? 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 httpstore is the objectstore.ObjectStore on a plain HTTP server (static hosting, a CDN). It cannot list. It refuses a redirect to another origin, a lower scheme or an address with credentials, and holds its headers as secrets. No connection kind wires it to a flag source yet.

Index

Constants

View Source
const (
	// MaxHTTPKeyBytes is the longest key of an http store.
	MaxHTTPKeyBytes = 1024
)

Variables

View Source
var (
	// ErrHTTPKeyRefused is the cause of a key that is not a clean relative slash path (see [storekit.ValidKey]) or that
	// is longer than [MaxHTTPKeyBytes].
	ErrHTTPKeyRefused = errors.New("objectstore: http key refused")
	// ErrHTTPAuth is the cause of a 401 or 403 answer.
	ErrHTTPAuth = errors.New("objectstore: http access refused")
	// ErrHTTPStatus is the cause of an answer with a status the store does not map to a result of its own.
	ErrHTTPStatus = errors.New("objectstore: unexpected http status")
	// ErrHTTPEncoding is the cause of an answer with a Content-Encoding other than identity: the store asks for
	// none and never decodes one.
	ErrHTTPEncoding = errors.New("objectstore: http content encoding refused")
	// ErrHTTPUnboundedBody is the cause of an HTTP/1 answer whose end is marked by nothing but the close of the
	// connection (no Content-Length, not chunked): a body cut short would look complete.
	ErrHTTPUnboundedBody = errors.New("objectstore: http body is not delimited")
)

Causes carried by the *objectstore.FetchError of an Store, and by the errors of New. They are for errors.Is; the reader reports only the reason.

Functions

func ValidHTTPKey

func ValidHTTPKey(key string) bool

ValidHTTPKey reports whether key is a clean relative slash path of at most MaxHTTPKeyBytes bytes.

Types

type Config

type Config struct {
	// Tracing are the OpenTelemetry providers of the store: with them enabled every request has a client span and
	// carries the trace context and the baggage of the context of the call. The span is the stock one of the HTTP
	// instrumentation: it records the URL as it is (the base URL has no credentials and no query, and the headers are
	// not recorded). The zero value instruments nothing.
	Tracing tracing.Providers
	// DialContext opens the connections of the store. Nil means a [net.Dialer] bounded by ConnectTimeout, which also
	// refuses a cleartext connection to an address that is not a loopback one (unless AllowCleartext is set). A
	// function set here replaces that dialer and its checks: it is for a caller that owns the transport, such as a
	// test that runs the store over an in-memory connection.
	DialContext func(ctx context.Context, network, address string) (net.Conn, error) `json:"-" yaml:"-"`
	// Headers are sent with every request, for example Authorization. The values are secrets: the store never
	// prints them, and never sends them to another host.
	Headers HTTPHeaders
	// BaseURL is the address the keys are resolved below: https://host/prefix. No credentials, query or fragment.
	BaseURL string
	// CABundlePEM holds certificate authorities trusted in addition to the system roots.
	CABundlePEM []byte
	// ConnectTimeout bounds the dial, and the TLS handshake on its own. Zero means
	// [storekit.DefaultHTTPConnectTimeout]. The timeouts are at most [storekit.MaxHTTPTimeout] and not negative.
	ConnectTimeout time.Duration
	// HeaderTimeout bounds the wait for the response headers. Zero means [storekit.DefaultHTTPHeaderTimeout].
	HeaderTimeout time.Duration
	// RequestTimeout bounds one Get, body included. Zero means [storekit.DefaultHTTPRequestTimeout].
	RequestTimeout time.Duration
	// MaxRetryAfter bounds the Retry-After that is passed on. Zero means [storekit.MaxHTTPRetryAfter].
	MaxRetryAfter time.Duration
	// MaxRedirects is the number of redirects followed: zero follows none. A negative value means
	// [storekit.DefaultHTTPMaxRedirects].
	MaxRedirects int
	// MinTLSVersion is a tls.Version constant. Zero, and anything below TLS 1.2, mean TLS 1.2.
	MinTLSVersion uint16
	// AllowCleartext lets the store use http:// for a host that is not a loopback address.
	AllowCleartext bool
}

Config is the setting of an Store.

type HTTPHeaders

type HTTPHeaders map[string]string

HTTPHeaders are the headers of an Store: their values are secrets, so no formatting, logging or encoding of the map shows one. Only the names and the count are printed.

func (HTTPHeaders) Format

func (h HTTPHeaders) Format(f fmt.State, _ rune)

Format implements fmt.Formatter: every verb prints the count and never a value.

func (HTTPHeaders) LogValue

func (HTTPHeaders) LogValue() slog.Value

LogValue implements slog.LogValuer.

func (HTTPHeaders) MarshalJSON

func (HTTPHeaders) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler: a value is never written.

func (HTTPHeaders) MarshalYAML

func (HTTPHeaders) MarshalYAML() (any, error)

MarshalYAML implements the Marshaler of gopkg.in/yaml.v3, which writes a map without asking for the other hooks.

type Store

type Store struct {
	objectstore.NoLifecycle
	// contains filtered or unexported fields
}

Store is an objectstore.ObjectStore over an HTTP server (a static file host, an object store behind a CDN, an internal service): a Get of a key is a GET of <BaseURL>/<key>. See docs/concepts/control-plane-http-store.md.

The ETag the reader holds is a tag that remembers the validator it came from: the ETag of the server ("etag:..."), the Last-Modified date together with the digest of the body ("lm:...") or the digest of the body alone ("sha256:..."). When the key was served from an address other than the one asked for (a redirect), the first two tags end with a mark that names that address: a validator is only good for the resource it came from, so a 304 or a 200 with the same ETag from another address is no proof (the object is read again, without a condition). A conditional Get sends If-None-Match for the first, If-Modified-Since for the second and nothing for the third. A 304 costs no body. A 200 whose validator equals the one held (a server that ignores conditional requests) is answered with objectstore.ErrNotModified too. A 304 counts only for a request that was conditional, and a Last-Modified date less than two seconds older than the answer is not used as a validator (the digest of the body alone is), so a second write within the same second is seen.

VersionID is not supported: a request for one is answered with objectstore.ErrNotFound. The store holds no state besides its connection pool and is safe for concurrent use.

func New

func New(cfg *Config) (*Store, error)

New checks the setting and returns the store. A cleartext address is accepted only for a loopback host, or when Config.AllowCleartext is set.

func (*Store) CloseIdleConnections

func (s *Store) CloseIdleConnections()

CloseIdleConnections closes the idle connections of the pool.

func (*Store) Format

func (s *Store) Format(f fmt.State, _ rune)

Format implements fmt.Formatter: the store prints its origin and base path, never a header.

func (*Store) Get

Get implements objectstore.ObjectStore. See Store for the rules.

Jump to

Keyboard shortcuts

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