httpc

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

README

pkg/httpc -- HTTP Client Adapter

Thin adapter over github.com/oakwood-commons/httpc that adds scafctl-specific behaviour:

  • XDG cache directory via pkg/paths instead of os.UserCacheDir
  • App-name-derived cache key prefix via pkg/settings (for example, scafctl: for the default binary name)
  • OTel metrics bridge -- OTelMetrics implements the upstream Metrics interface using pkg/metrics OTel instruments
  • Context-based SSRF checks -- PrivateIPsAllowed(ctx) reads config.FromContext(ctx) to decide whether private IPs are allowed
  • config.HTTPClientConfig bridge -- NewClientFromAppConfig converts the string-based app config to a typed ClientConfig

Usage

All consumers import this package as before:

import "github.com/oakwood-commons/scafctl/pkg/httpc"

client := httpc.NewClient(nil)            // scafctl defaults
client := httpc.NewClient(&httpc.ClientConfig{...})
client := httpc.NewClientFromAppConfig(cfg, logger)
config := httpc.DefaultConfig()

Re-exported symbols

Types, constants, errors, and functions from the upstream library are re-exported via type aliases and var assignments. Consumers should not need to import github.com/oakwood-commons/httpc directly.

SSRF protection

The upstream library has transport-level SSRF protection (AllowPrivateIPs field on ClientConfig). This adapter disables it (AllowPrivateIPs = true on every client) because scafctl performs SSRF validation at the application layer via PrivateIPsAllowed(ctx) and ValidateURLNotPrivate(url) in the providers that need it (httpprovider, parameterprovider).

Documentation

Overview

Package httpc is a thin adapter over the standalone github.com/oakwood-commons/httpc library, adding scafctl-specific defaults (XDG cache paths, app-name-based cache key prefix) and bridging application-level concerns (OTel metrics, config.FromContext, etc.).

All consumer code continues to import "github.com/oakwood-commons/scafctl/pkg/httpc" with no changes required.

Index

Constants

View Source
const (
	CacheTypeMemory     = upstream.CacheTypeMemory
	CacheTypeFilesystem = upstream.CacheTypeFilesystem
)

Cache type constants.

View Source
const DefaultMaxRedirects = upstream.DefaultMaxRedirects

Constant re-exports.

Variables

View Source
var (
	ErrCircuitBreakerOpen        = upstream.ErrCircuitBreakerOpen
	ErrCacheSizeLimitExceeded    = upstream.ErrCacheSizeLimitExceeded
	ErrDecompressionBombDetected = upstream.ErrDecompressionBombDetected
	ErrResponseBodyTooLarge      = upstream.ErrResponseBodyTooLarge
)

Sentinel errors.

View Source
var (
	NewFileCache                = upstream.NewFileCache
	DefaultCircuitBreakerConfig = upstream.DefaultCircuitBreakerConfig
	ValidateURLNotPrivate       = upstream.ValidateURLNotPrivate
)

Function re-exports that have no scafctl-specific behaviour.

Functions

func BuildNamedBackoff added in v0.5.0

func BuildNamedBackoff(strategy string, initialWait, maxWait time.Duration) retryablehttp.Backoff

BuildNamedBackoff returns a retryablehttp.Backoff function for the named strategy.

func BuildStatusCodeCheckRetry added in v0.5.0

func BuildStatusCodeCheckRetry(statusCodes []int) retryablehttp.CheckRetry

BuildStatusCodeCheckRetry returns a retryablehttp.CheckRetry function that retries on the given HTTP status codes.

func PrivateIPsAllowed added in v0.6.0

func PrivateIPsAllowed(ctx context.Context) bool

PrivateIPsAllowed returns true when the application config stored in ctx permits HTTP requests to private/loopback/link-local IP addresses. Returns false (deny) when no config is present -- secure by default.

Types

type CacheStats

type CacheStats = upstream.CacheStats

Type aliases re-export upstream types so consumers need no import changes.

type CacheType

type CacheType = upstream.CacheType

Type aliases re-export upstream types so consumers need no import changes.

type CircuitBreakerConfig

type CircuitBreakerConfig = upstream.CircuitBreakerConfig

Type aliases re-export upstream types so consumers need no import changes.

type Client

type Client = upstream.Client

Type aliases re-export upstream types so consumers need no import changes.

func NewClient

func NewClient(cfg *ClientConfig) *Client

NewClient creates a new HTTP client with scafctl-specific defaults injected. When cfg is nil, DefaultConfig() is used. nil Metrics, empty CacheDir and empty CacheKeyPrefix are filled with scafctl defaults.

The caller's config is not mutated; a shallow copy is made internally.

AllowPrivateIPs is set to true on the upstream client because scafctl handles SSRF protection via context-based checks (PrivateIPsAllowed + ValidateURLNotPrivate) at the call sites that need it (httpprovider, parameterprovider, etc.).

func NewClientFromAppConfig

func NewClientFromAppConfig(cfg *config.HTTPClientConfig, logger logr.Logger) *Client

NewClientFromAppConfig creates an httpc.Client from a scafctl config.HTTPClientConfig. It uses scafctl-specific defaults (XDG cache dir, app-name-based prefix, OTel metrics) as the base, then overlays the string-based config values.

The cfg parameter can be nil, in which case scafctl defaults are used.

type ClientConfig

type ClientConfig = upstream.ClientConfig

Type aliases re-export upstream types so consumers need no import changes.

func DefaultConfig

func DefaultConfig() *ClientConfig

DefaultConfig returns a ClientConfig with scafctl-specific defaults: XDG-based cache directory, app-name-based cache key prefix, and OTel metrics adapter.

type FileCache

type FileCache = upstream.FileCache

Type aliases re-export upstream types so consumers need no import changes.

type FileCacheConfig

type FileCacheConfig = upstream.FileCacheConfig

Type aliases re-export upstream types so consumers need no import changes.

type Metrics added in v0.10.1

type Metrics = upstream.Metrics

Type aliases re-export upstream types so consumers need no import changes.

type NoopMetrics added in v0.10.1

type NoopMetrics = upstream.NoopMetrics

Type aliases re-export upstream types so consumers need no import changes.

type OTelMetrics added in v0.10.1

type OTelMetrics struct{}

OTelMetrics implements the upstream httpc.Metrics interface by delegating to the scafctl pkg/metrics OTel instruments. This bridges the external library's generic metrics interface to scafctl's application-level OTel setup.

func (OTelMetrics) DecrementConcurrentRequests added in v0.10.1

func (OTelMetrics) DecrementConcurrentRequests(ctx context.Context)

func (OTelMetrics) IncrementCacheHits added in v0.10.1

func (OTelMetrics) IncrementCacheHits(ctx context.Context)

func (OTelMetrics) IncrementCacheMisses added in v0.10.1

func (OTelMetrics) IncrementCacheMisses(ctx context.Context)

func (OTelMetrics) IncrementConcurrentRequests added in v0.10.1

func (OTelMetrics) IncrementConcurrentRequests(ctx context.Context)

func (OTelMetrics) IncrementErrorsTotal added in v0.10.1

func (OTelMetrics) IncrementErrorsTotal(ctx context.Context, method, host, pathTemplate, errorType string)

func (OTelMetrics) IncrementRequestsTotal added in v0.10.1

func (OTelMetrics) IncrementRequestsTotal(ctx context.Context, method, host, pathTemplate string, statusCode int)

func (OTelMetrics) IncrementRetries added in v0.10.1

func (OTelMetrics) IncrementRetries(ctx context.Context, method, host, pathTemplate string)

func (OTelMetrics) RecordRequestDuration added in v0.10.1

func (OTelMetrics) RecordRequestDuration(ctx context.Context, method, host, pathTemplate string, statusCode int, duration time.Duration)

func (OTelMetrics) RecordRequestSize added in v0.10.1

func (OTelMetrics) RecordRequestSize(ctx context.Context, method, host, pathTemplate string, bytes float64)

func (OTelMetrics) RecordResponseSize added in v0.10.1

func (OTelMetrics) RecordResponseSize(ctx context.Context, method, host, pathTemplate string, bytes float64)

func (OTelMetrics) SetCacheSizeBytes added in v0.10.1

func (OTelMetrics) SetCacheSizeBytes(bytes int64)

func (OTelMetrics) SetCircuitBreakerState added in v0.10.1

func (OTelMetrics) SetCircuitBreakerState(host string, state float64)

type RequestHook

type RequestHook = upstream.RequestHook

Type aliases re-export upstream types so consumers need no import changes.

type ResponseHook

type ResponseHook = upstream.ResponseHook

Type aliases re-export upstream types so consumers need no import changes.

Jump to

Keyboard shortcuts

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