httpclient

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: AGPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidRequest is returned if a given HTTP request is invalid and cannot be performed.
	ErrInvalidRequest = errors.New("invalid http request")

	// ErrInvalidNetwork is returned if the request would not be performed over TCP
	ErrInvalidNetwork = errors.New("invalid network type")

	// ErrReservedAddr is returned if a dialed address resolves to an IP within a blocked or reserved net.
	ErrReservedAddr = errors.New("dial within blocked / reserved IP range")

	// ErrBodyTooLarge is returned when a received response body is above predefined limit (default 40MB).
	ErrBodyTooLarge = errors.New("body size too large")
)

Functions

func SafeIP added in v0.10.0

func SafeIP(ip netip.Addr) bool

SafeIP returns whether ip is an IPv4/6 address in a non-reserved, public range.

func ValidateRequest

func ValidateRequest(r *http.Request) error

ValidateRequest performs the same request validation logic found in the default net/http.Transport{}.roundTrip() function, but pulls it out into this separate function allowing validation errors to be wrapped under a single error type.

Types

type Client

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

Client wraps an underlying http.Client{} to provide the following:

  • setting a maximum received request body size, returning error on large content lengths, and using a limited reader in all other cases to protect against forged / unknown content-lengths
  • protection from server side request forgery (SSRF) by only dialing out to known public IP prefixes, configurable with allows/blocks
  • retry-backoff logic for error temporary HTTP error responses
  • optional request signing
  • request logging

func New

func New(cfg Config) *Client

New returns a new instance of Client initialized using configuration.

func (*Client) Do

func (c *Client) Do(r *http.Request) (*http.Response, error)

Do will essentially perform http.Client{}.Do() with retry-backoff functionality.

func (*Client) DoSigned added in v0.9.0

func (c *Client) DoSigned(r *http.Request, sign SignFunc) (rsp *http.Response, err error)

DoSigned will essentially perform http.Client{}.Do() with retry-backoff functionality and requesting signing..

type Config

type Config struct {
	// MaxOpenConnsPerHost limits the max number of open connections to a host.
	MaxOpenConnsPerHost int

	// MaxIdleConns: see http.Transport{}.MaxIdleConns.
	MaxIdleConns int

	// ReadBufferSize: see http.Transport{}.ReadBufferSize.
	ReadBufferSize int

	// WriteBufferSize: see http.Transport{}.WriteBufferSize.
	WriteBufferSize int

	// MaxBodySize determines the maximum fetchable body size.
	MaxBodySize int64

	// Timeout: see http.Client{}.Timeout.
	Timeout time.Duration

	// DisableCompression: see http.Transport{}.DisableCompression.
	DisableCompression bool

	// AllowRanges allows outgoing communications to given IP nets.
	AllowRanges []netip.Prefix

	// BlockRanges blocks outgoing communiciations to given IP nets.
	BlockRanges []netip.Prefix

	// TLSInsecureSkipVerify can be set to true to
	// skip validation of remote TLS certificates.
	//
	// THIS SHOULD BE USED FOR TESTING ONLY, IF YOU
	// TURN THIS ON WHILE RUNNING IN PRODUCTION YOU
	// ARE LEAVING YOUR SERVER WIDE OPEN TO ATTACKS!
	TLSInsecureSkipVerify bool
}

Config provides configuration details for setting up a new instance of httpclient.Client{}. Within are a subset of the configuration values passed to initialized http.Transport{} and http.Client{}, along with httpclient.Client{} specific.

type Sanitizer added in v0.10.0

type Sanitizer struct {
	Allow []netip.Prefix
	Block []netip.Prefix
}

func (*Sanitizer) Sanitize added in v0.10.0

func (s *Sanitizer) Sanitize(ntwrk, addr string, _ syscall.RawConn) error

Sanitize implements the required net.Dialer.Control function signature.

type SignFunc added in v0.9.0

type SignFunc func(r *http.Request) error

SignFunc is a function signature that provides request signing.

Jump to

Keyboard shortcuts

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