httputil

package
v0.0.24 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: Apache-2.0 Imports: 8 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckedWrite added in v0.0.7

func CheckedWrite(w io.Writer, d []byte)

CheckedWrite will check to ensure the number of bytes intended to be written were written, and log a warning if not. It will also check the error returned and log the error if there was one.

It may be better to try to write the data that wasn't successfully written.

This is used to log any errors mostly, rather than try to recover.

func NewHTTPClient

func NewHTTPClient(tlsConfig *tls.Config) *http.Client

NewHTTPClient returns a new http.Client that is configured with sane timeouts, a global TLS configuration, and optionally a per-client TLS config.

Generally, the global config will have things like custom CA roots, and we will want to trust those for every outgoing conneciton. A per-client TLS config would be used where we are talking to a specific API, and want to insert our certificates or a custom CA root for just that connection.

Future changes should allow merging tls configs, so we can add to the base default rather than replace it entirely.

func SetClientConfig

func SetClientConfig(c ClientConfig)

SetClientConfig will replace the current clientConfig for all future clients returned by NewHTTPClient(). Generally, this will be set once, and probably not changed per connection. It is not going to be thread-safe, in that setting the config and then calling NewHTTPClient() could be a race.

func SetError

func SetError(w http.ResponseWriter, statusCode int, message interface{})

SetError returns a JSON error message with a Status field set to 'error', a 'code' set to the provided code, and the 'error' set to the provided content.

Prior to calling, nothing should be written to the writer, and afterwards nothing should be written.

The content-type will be set to application/json.

func SetTLSConfig

func SetTLSConfig(tlsconfig *tls.Config)

SetTLSConfig sets the default TLS configuration used by NewHTTPClient(). This will generally be set once for adding custom CA roots or other configuration used throughout the application.

NewHTTPClient() also allows per-client TLS configuration, if desired.

func StatusCodeOK

func StatusCodeOK(statusCode int) bool

StatusCodeOK returns true if the error code provided is between 200 and 2999, inclusive.

Types

type ClientConfig

type ClientConfig struct {
	DialTimeout           int `json:"dialTimeout,omitempty" yaml:"dialTimeout,omitempty"`
	ClientTimeout         int `json:"clientTimeout,omitempty" yaml:"clientTimeout,omitempty"`
	TLSHandshakeTimeout   int `json:"tlsHandshakeTimeout,omitempty" yaml:"tlsHandshakeTimeout,omitempty"`
	ResponseHeaderTimeout int `json:"responseHeaderTimeout,omitempty" yaml:"responseHeaderTimeout,omitempty"`
	MaxIdleConnections    int `json:"maxIdleConnections,omitempty" yaml:"maxIdleConnections,omitempty"`
}

ClientConfig defines various timeouts we will want to change. All times are in seconds. If 0, a default will be used.

Jump to

Keyboard shortcuts

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