httpclient

package
v0.7.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2023 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBodyTooLarge = errors.New("body size too large")

ErrBodyTooLarge is returned when a received response body is above predefined limit (default 40MB).

View Source
var ErrInvalidNetwork = errors.New("invalid network type")

ErrInvalidNetwork is returned if the request would not be performed over TCP

View Source
var ErrInvalidRequest = errors.New("invalid http request")

ErrInvalidRequest is returned if a given HTTP request is invalid and cannot be performed.

View Source
var ErrReservedAddr = errors.New("dial within blocked / reserved IP range")

ErrReservedAddr is returned if a dialed address resolves to an IP within a blocked or reserved net.

Functions

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
  • limit number of concurrent requests, else blocking until a slot is available (context channels still respected)

func New

func New(cfg Config) *Client

New returns a new instance of Client initialized using configuration.

func (*Client) Do

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

Do will perform given request when an available slot in the queue is available, and block until this time. For returned values, this follows the same semantics as the standard http.Client{}.Do() implementation except that response body will be wrapped by an io.LimitReader() to limit response body sizes.

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
}

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.

Jump to

Keyboard shortcuts

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