httpcommon

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: Apache-2.0 Imports: 15 Imported by: 38

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrResponseLimit = errors.New("HTTP response length limit was reached")
)

Functions

func HeaderRoundTripper

func HeaderRoundTripper(rt http.RoundTripper, headers map[string]string) http.RoundTripper

HeaderRoundTripper will return a RoundTripper that sets header KVs if the key is not present.

func ReadAll added in v0.8.0

func ReadAll(resp *http.Response) ([]byte, error)

ReadAll returns the whole response body as bytes. This is an optimized version of `io.ReadAll`.

Use `ReadAllWithLimit` with a reasonable limit when possible! Avoid reading HTTP responses without a limit! A malicious server might serve a `Content-Length` header with a value too high to handle or the server might serve a response body that is too long and can crash the client with OOM.

func ReadAllWithLimit added in v0.8.0

func ReadAllWithLimit(resp *http.Response, limit int64) ([]byte, error)

ReadAllWithLimit returns the whole response body as bytes respecting the given limit. This is an optimized version of `io.ReadAll`.

If the `limit` is 0, an empty byte slice is returned. If the `limit` is a negative value, e.g `-1`, the limit is ignored and the entire response body is returned. If the `Content-Length` header was served and its value exceeds the limit, the `ErrResponseLimit` error is returned. If the body length is not known in advance, it reads from the body up to the set limit and returns a partial response without an error.

Avoid reading HTTP responses without a limit and use a reasonable limit instead! A malicious server might serve a `Content-Length` header with a value too high to handle or the server might serve a response body that is too long and can crash the client with OOM.

Types

type HTTPClientProxySettings

type HTTPClientProxySettings struct {
	// Proxy URL to use for http connections. If the proxy url is configured,
	// it is used for all connection attempts. All proxy related environment
	// variables are ignored.
	URL *ProxyURI `config:"proxy_url" yaml:"proxy_url,omitempty"`

	// Headers configures additional headers that are send to the proxy
	// during CONNECT requests.
	Headers ProxyHeaders `config:"proxy_headers" yaml:"proxy_headers,omitempty"`

	// Disable HTTP proxy support. Configured URLs and environment variables
	// are ignored.
	Disable bool `config:"proxy_disable" yaml:"proxy_disable,omitempty"`
}

HTTPClientProxySettings provides common HTTP proxy setup support.

Proxy usage will be disabled in general if Disable is set. If URL is not set, the proxy configuration will default to HTTP_PROXY, HTTPS_PROXY, and NO_PROXY.

The default (and zero) value of HTTPClientProxySettings has Proxy support enabled, and will select the proxy per URL based on the environment variables.

func DefaultHTTPClientProxySettings

func DefaultHTTPClientProxySettings() HTTPClientProxySettings

DefaultHTTPClientProxySettings returns the default HTTP proxy setting.

func NewHTTPClientProxySettings

func NewHTTPClientProxySettings(url string, headers map[string]string, disable bool) (*HTTPClientProxySettings, error)

NewHTTPClientProxySettings creates a new proxy settings based on provided proxy information.

func (*HTTPClientProxySettings) ProxyFunc

func (settings *HTTPClientProxySettings) ProxyFunc() func(*http.Request) (*url.URL, error)

ProxyFunc creates a function that can be used with http.Transport in order to configure the HTTP proxy functionality.

func (*HTTPClientProxySettings) Unpack

func (settings *HTTPClientProxySettings) Unpack(cfg *config.C) error

Unpack sets the proxy settings from a config object. Note: Unpack is automatically used by the configuration system if `cfg.Unpack(&x)` is and X contains a field of type HTTPClientProxySettings.

type HTTPTransportSettings

type HTTPTransportSettings struct {
	// TLS provides ssl/tls setup settings
	TLS *tlscommon.Config `config:"ssl" yaml:"ssl,omitempty" json:"ssl,omitempty"`

	// Timeout configures the `(http.Transport).Timeout`.
	Timeout time.Duration `config:"timeout" yaml:"timeout,omitempty" json:"timeout,omitempty"`

	Proxy HTTPClientProxySettings `config:",inline" yaml:",inline"`

	IdleConnTimeout time.Duration `config:"idle_connection_timeout" yaml:"idle_connection_timeout,omitempty" json:"idle_connection_timeout,omitempty"`
}

HTTPTransportSettings provides common HTTP settings for HTTP clients.

func DefaultHTTPTransportSettings

func DefaultHTTPTransportSettings() HTTPTransportSettings

DefaultHTTPTransportSettings returns the default HTTP transport setting.

func (HTTPTransportSettings) Client

func (settings HTTPTransportSettings) Client(opts ...TransportOption) (*http.Client, error)

Client creates a new http.Client with configured Transport. The transport is instrumented using apmhttp.WrapRoundTripper.

func (*HTTPTransportSettings) RoundTripper

func (settings *HTTPTransportSettings) RoundTripper(opts ...TransportOption) (http.RoundTripper, error)

RoundTripper creates a http.RoundTripper for use with http.Client.

The dialers will registers with stats if given. Stats is used to collect metrics for io errors, bytes in, and bytes out.

func (*HTTPTransportSettings) Unpack

func (settings *HTTPTransportSettings) Unpack(cfg *config.C) error

Unpack reads a config object into the settings.

type ProxyHeaders

type ProxyHeaders map[string]string

ProxyHeaders is a headers for proxy serialized as a map[string]string.

func (ProxyHeaders) Headers

func (p ProxyHeaders) Headers() http.Header

URI returns conventional url.URL structure.

func (ProxyHeaders) MarshalJSON

func (p ProxyHeaders) MarshalJSON() ([]byte, error)

MarshalJSON serializes URI as a string.

func (ProxyHeaders) MarshalYAML

func (p ProxyHeaders) MarshalYAML() (interface{}, error)

MarshalYAML serializes URI as a string.

func (*ProxyHeaders) UnmarshalJSON

func (p *ProxyHeaders) UnmarshalJSON(b []byte) error

UnmarshalJSON unpacks string into an proxy URI.

func (*ProxyHeaders) UnmarshalYAML

func (p *ProxyHeaders) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML unpacks string into an proxy URI.

func (*ProxyHeaders) Unpack

func (p *ProxyHeaders) Unpack(cfg *config.C) error

Unpack unpacks string into an proxy URI.

type ProxyURI

type ProxyURI url.URL

ProxyURI is a URL used for proxy serialized as a string.

func NewProxyURIFromString

func NewProxyURIFromString(s string) (*ProxyURI, error)

func NewProxyURIFromURL

func NewProxyURIFromURL(u url.URL) *ProxyURI

func (*ProxyURI) MarshalJSON

func (p *ProxyURI) MarshalJSON() ([]byte, error)

MarshalJSON serializes URI as a string.

func (*ProxyURI) MarshalYAML

func (p *ProxyURI) MarshalYAML() (interface{}, error)

MarshalYAML serializes URI as a string.

func (*ProxyURI) String

func (p *ProxyURI) String() string

MarshalJSON serializes URI as a string.

func (*ProxyURI) URI

func (p *ProxyURI) URI() *url.URL

URI returns conventional url.URL structure.

func (*ProxyURI) UnmarshalJSON

func (p *ProxyURI) UnmarshalJSON(b []byte) error

UnmarshalJSON unpacks string into an proxy URI.

func (*ProxyURI) UnmarshalYAML

func (p *ProxyURI) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML unpacks string into an proxy URI.

func (*ProxyURI) Unpack

func (p *ProxyURI) Unpack(s string) error

Unpack unpacks string into an proxy URI.

type TransportOption

type TransportOption interface {
	// contains filtered or unexported methods
}

TransportOption are applied to the http.RoundTripper to be build from HTTPTransportSettings.

func WithAPMHTTPInstrumentation

func WithAPMHTTPInstrumentation() TransportOption

WithAPMHTTPInstrumentation insruments the HTTP client via apmhttp.WrapRoundTripper. Custom APM round tripper wrappers can be configured via WithModRoundtripper.

func WithBaseDialer

func WithBaseDialer(d transport.Dialer) TransportOption

WithBaseDialer configures the dialer used for TCP and TLS connections.

func WithForceAttemptHTTP2

func WithForceAttemptHTTP2(b bool) TransportOption

WithForceAttemptHTTP2 sets the `http.Tansport.ForceAttemptHTTP2` field.

func WithHTTP2Only

func WithHTTP2Only(b bool) TransportOption

WithHTTP2Only will ensure that a HTTP 2 only roundtripper is created.

func WithHeaderRoundTripper

func WithHeaderRoundTripper(headers map[string]string) TransportOption

WithHeaderRoundTripper instruments the HTTP client via a custom http.RoundTripper. This RoundTripper will add headers to each request if the key is not present.

func WithIOStats

func WithIOStats(stats transport.IOStatser) TransportOption

WithIOStats instruments the RoundTripper dialers with the given statser, such that bytes in, bytes out, and errors can be monitored.

func WithLogger

func WithLogger(logger *logp.Logger) TransportOption

WithLogger sets the internal logger that will be used to log dial or TCP level errors. Logging at the connection level will only happen if the logger has been set.

func WithModRoundtripper

func WithModRoundtripper(w func(http.RoundTripper) http.RoundTripper) TransportOption

WithModRoundtripper allows customization of the roundtipper.

func WithNOProxy

func WithNOProxy() TransportOption

WithNOProxy disables the configured proxy. Proxy environment variables like HTTP_PROXY and HTTPS_PROXY will have no affect.

func WithTransportFunc

func WithTransportFunc(fn func(*http.Transport)) TransportOption

WithTransportFunc register a custom function that is used to apply custom changes to the net.Transport, when the Client is build.

func WithoutProxyEnvironmentVariables

func WithoutProxyEnvironmentVariables() TransportOption

WithoutProxyEnvironmentVariables disables support for the HTTP_PROXY, HTTPS_PROXY and NO_PROXY envionrment variables. Explicitly configured proxy URLs will still applied.

type WithKeepaliveSettings

type WithKeepaliveSettings struct {
	Disable             bool
	MaxIdleConns        int
	MaxIdleConnsPerHost int
	IdleConnTimeout     time.Duration
}

WithKeepaliveSettings options can be used to modify the Keepalive

Jump to

Keyboard shortcuts

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