httputils

package
v0.0.0-...-03d6fc4 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2019 License: BSD-3-Clause Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DIAL_TIMEOUT    = time.Minute
	REQUEST_TIMEOUT = 5 * time.Minute

	FAST_DIAL_TIMEOUT    = 50 * time.Millisecond
	FAST_REQUEST_TIMEOUT = 100 * time.Millisecond

	// Exponential backoff defaults.
	INITIAL_INTERVAL     = 500 * time.Millisecond
	RANDOMIZATION_FACTOR = 0.5
	BACKOFF_MULTIPLIER   = 1.5
	MAX_INTERVAL         = 60 * time.Second
	MAX_ELAPSED_TIME     = 5 * time.Minute

	MAX_BYTES_IN_RESPONSE_BODY = 10 * 1024 //10 KB

	// SCHEME_AT_LOAD_BALANCER_HEADER is the header, added by the load balancer,
	// the has the scheme [http|https] that the original request was made under.
	SCHEME_AT_LOAD_BALANCER_HEADER = "x-forwarded-proto"
)

Variables

This section is empty.

Functions

func AddMetricsToClient

func AddMetricsToClient(c *http.Client) *http.Client

AddMetricsToClient adds metrics for each request to the http.Client.

func ConfiguredDialTimeout

func ConfiguredDialTimeout(timeout time.Duration) func(string, string) (net.Conn, error)

ConfiguredDialTimeout is a dialer that sets a given timeout.

func CorsCredentialsHandler

func CorsCredentialsHandler(h func(http.ResponseWriter, *http.Request), originSuffix string) func(http.ResponseWriter, *http.Request)

CorsCredentialsHandler is an HTTPS handler function which adds the necessary header for a CORS request using credentials. This allows, for example, status.skia.org to make a request to fuzzer.skia.org using the *.skia.org cookie that is shared between them.

To have the browser use the cookie, the withCredentials on the XMLHttpRequest must be true, or the credentials option must be true when using fetch. In addition to this client-side setting, the server must set Access-Control-Allow-Credentials to be true.

However, Access-Control-Allow-Origin (ACAO) cannot be the wildcard "*", or the browser gets upset (https://stackoverflow.com/q/19743396) The ACAO must be exactly the origin of the request (lists of origins aren't supported [1]). The best practice is to write the ACAO header dynamically, based on the requesting Origin header, which is what is done here, assuming the header ends with the passed in originSuffix (e.g. ".skia.org")

[1] https://www.w3.org/TR/cors/#access-control-allow-origin-response-header

"In practice the origin-list-or-null production is more constrained. Rather
 than allowing a space-separated list of origins, it is either a single origin
 or the string "null"."

func CorsHandler

func CorsHandler(h func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request)

CorsHandler is an HTTP handler function which adds the necessary header for CORS.

func DialTimeout

func DialTimeout(network, addr string) (net.Conn, error)

DialTimeout is a dialer that sets a timeout.

func FastDialTimeout

func FastDialTimeout(network, addr string) (net.Conn, error)

FastDialTimeout is a dialer that sets a timeout.

func GetBaseURL

func GetBaseURL(urlStr string) (string, error)

GetBaseURL strips everything but the scheme and hostname from the given URL e.g.:

https://example.com/some/path/action#abcde => https://example.com

If the input URL cannot be parsed an error is returned.

func HTTPS

func HTTPS(h http.Handler) http.Handler

HTTPS forces traffic to go over HTTPS. See: https://github.com/kubernetes/ingress-gce#redirecting-http-to-https

h - The http.Handler to wrap.

Example:

if !*local {
  h := httputils.HTTPS(h)
}
http.Handle("/", h)

func HealthCheckHandler

func HealthCheckHandler(w http.ResponseWriter, r *http.Request)

HealthCheckHandler returns 200 OK with an empty body, appropriate for a healtcheck endpoint.

func Healthz

func Healthz(h http.Handler) http.Handler

Healthz handles healthchecks at /healthz and GFE healthchecks at /.

Example:

if !*local {
  h := httputils.Healthz(h)
}
http.Handle("/", h)

func HealthzAndHTTPS

func HealthzAndHTTPS(h http.Handler) http.Handler

HealthzAndHTTPS forces traffic to go over HTTPS and also handles healthchecks at /healthz. See: https://github.com/kubernetes/ingress-gce#redirecting-http-to-https

h - The http.Handler to wrap.

Example:

if !*local {
  h := httputils.HealthzAndHTTPS(h)
}
http.Handle("/", h)

func LoggingGzipRequestResponse

func LoggingGzipRequestResponse(h http.Handler) http.Handler

LoggingGzipRequestResponse records parts of the request and the response to the logs and gzips responses when appropriate.

func LoggingRequestResponse

func LoggingRequestResponse(h http.Handler) http.Handler

LoggingRequestResponse records parts of the request and the response to the logs.

func MakeRenamingResourceHandler

func MakeRenamingResourceHandler(resourcesDir string, aliases map[string]string) func(http.ResponseWriter, *http.Request)

MakeRenamingResourceHandler is an HTTP handler function designed for serving files. It takes a map that can be used to alias a url. The primary usecase is to have the url be distinct from the file that will show the content. e.g. /foo/bar can be represented by my-custom-element.html in the passed in resourcesDir

func MakeResourceHandler

func MakeResourceHandler(resourcesDir string) func(http.ResponseWriter, *http.Request)

MakeResourceHandler is an HTTP handler function designed for serving files.

func NewConfiguredBackOffTransport

func NewConfiguredBackOffTransport(config *BackOffConfig, base http.RoundTripper) http.RoundTripper

NewConfiguredBackOffTransport creates a BackOffTransport with the specified config, wrapping the given base RoundTripper.

Example: The default retry_interval is .5 seconds, default randomization_factor is 0.5, default multiplier is 1.5 and the default max_interval is 1 minute. For 10 tries the sequence will be (values in seconds) and assuming we go over the max_elapsed_time on the 10th try:

request#     retry_interval     randomized_interval
1             0.5                [0.25,   0.75]
2             0.75               [0.375,  1.125]
3             1.125              [0.562,  1.687]
4             1.687              [0.8435, 2.53]
5             2.53               [1.265,  3.795]
6             3.795              [1.897,  5.692]
7             5.692              [2.846,  8.538]
8             8.538              [4.269, 12.807]
9            12.807              [6.403, 19.210]
10           19.210              backoff.Stop

func NewConfiguredTimeoutClient

func NewConfiguredTimeoutClient(dialTimeout, reqTimeout time.Duration) *http.Client

NewConfiguredTimeoutClient creates a new http.Client with both a dial timeout and a request timeout.

func NewFastTimeoutClient

func NewFastTimeoutClient() *http.Client

NewFastTimeoutClient creates a new http.Client with both a dial timeout and a request timeout.

func NewMetricsTransport

func NewMetricsTransport(rt http.RoundTripper) http.RoundTripper

NewMetricsTransport returns a MetricsTransport instance which wraps the given http.RoundTripper.

func NewTimeoutClient

func NewTimeoutClient() *http.Client

NewTimeoutClient creates a new http.Client with both a dial timeout and a request timeout.

func PaginationParams

func PaginationParams(query url.Values, defaultOffset, defaultSize, maxSize int) (int, int, error)

PaginationParams is helper function to extract pagination parameters from a URL query string. It assumes that 'offset' and 'size' are the query parameters used for pagination. It parses the values and returns an error if they are not integers. If the params are not set the defaults are proviced. Further it ensures that size is never above max size.

func ParseFormValues

func ParseFormValues(r *http.Request, rv interface{}) error

ParseFormValues reads form values from the http.Request and sets them on the given struct. Follows JSON decoding rules.

func ReadAndClose

func ReadAndClose(r io.ReadCloser) string

ReadAndClose reads the content of a ReadCloser (e.g. http Response), and returns it as a string. If the response was nil or there was a problem, it will return empty string. The reader, if non-null, will be closed by this function.

func ReadyHandleFunc

func ReadyHandleFunc(w http.ResponseWriter, r *http.Request)

ReadyHandleFunc can be used to set up a ready-handler used to check whether a service is ready. Simply returns 'ready'.

func ReportError

func ReportError(w http.ResponseWriter, r *http.Request, err error, message string)

ReportError formats an HTTP error response and also logs the detailed error message. The message parameter is returned in the HTTP response. If it is not provided then "Unknown error" will be returned instead.

func Response2xxOnly

func Response2xxOnly(client *http.Client) *http.Client

Response2xxOnly modifies client so that non-2xx HTTP responses cause a non-nil error return value.

func RunHealthCheckServer

func RunHealthCheckServer(port string)

RunHealthCheckServer is a helper function which runs an HTTP server which only handles health checks. This is used for processes which don't run an HTTP server of their own but still want health checks. Does not return.

Types

type BackOffConfig

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

func DefaultBackOffConfig

func DefaultBackOffConfig() *BackOffConfig

type BackOffTransport

type BackOffTransport struct {
	Transport http.RoundTripper
	// contains filtered or unexported fields
}

func (*BackOffTransport) RoundTrip

func (t *BackOffTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the RoundTripper interface.

type ClientConfig

type ClientConfig struct {
	// DialTimeout, if non-zero, sets the http.Transport's dialer to a net.DialTimeout with the
	// specified timeout.
	DialTimeout time.Duration

	// RequestTimeout, if non-zero, sets the http.Client.Timeout. The timeout applies until the
	// response body is fully read. See more details in the docs for http.Client.Timeout.
	RequestTimeout time.Duration

	// Retries, if non-nil, uses a BackOffTransport to automatically retry requests until receiving a
	// non-5xx response, as specified by the BackOffConfig. See more details in the docs for
	// NewConfiguredBackOffTransport.
	Retries *BackOffConfig

	// TokenSource, if non-nil, uses a oauth2.Transport to authenticate all requests with the
	// specified TokenSource. See auth package for functions to create a TokenSource.
	TokenSource oauth2.TokenSource

	// Response2xxOnly, if true, transforms non-2xx HTTP responses to an error return value.
	Response2xxOnly bool

	// Metrics, if true, logs each request to metrics.
	Metrics bool
}

ClientConfig represents options for the behavior of an http.Client. Each field, when set, modifies the default http.Client behavior.

Example: client := DefaultClientConfig().WithoutRetries().Client()

func DefaultClientConfig

func DefaultClientConfig() ClientConfig

DefaultClientConfig returns a ClientConfig with reasonable defaults.

  • Timeouts are DIAL_TIMEOUT and REQUEST_TIMEOUT.
  • Retries are enabled with the values from DefaultBackOffConfig().
  • Non-2xx responses are not considered errors.
  • Metrics are enabled.

func (ClientConfig) Client

func (c ClientConfig) Client() *http.Client

Client returns a new http.Client as configured by the ClientConfig.

func (ClientConfig) With2xxOnly

func (c ClientConfig) With2xxOnly() ClientConfig

With2xxOnly returns a new ClientConfig where non-2xx responses cause an error.

func (ClientConfig) WithDialTimeout

func (c ClientConfig) WithDialTimeout(dialTimeout time.Duration) ClientConfig

WithDialTimeout returns a new ClientConfig with the DialTimeout set as specified.

func (ClientConfig) WithTokenSource

func (c ClientConfig) WithTokenSource(t oauth2.TokenSource) ClientConfig

WithTokenSource returns a new ClientConfig where requests are authenticated with the given TokenSource.

func (ClientConfig) WithoutRetries

func (c ClientConfig) WithoutRetries() ClientConfig

WithoutRetries returns a new ClientConfig where requests are not retried.

type MetricsTransport

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

MetricsTransport is an http.RoundTripper which logs each request to metrics.

func (*MetricsTransport) RoundTrip

func (mt *MetricsTransport) RoundTrip(req *http.Request) (*http.Response, error)

See docs for http.RoundTripper.

type Response2xxOnlyTransport

type Response2xxOnlyTransport struct {
	http.RoundTripper
}

Response2xxOnlyTransport is a RoundTripper that transforms non-2xx HTTP responses to an error return value. Delegates all requests to the wrapped RoundTripper, which must be non-nil. Add this behavior to an existing client with Response2xxOnly below.

func (Response2xxOnlyTransport) RoundTrip

func (t Response2xxOnlyTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the RoundTripper interface.

type ResponsePagination

type ResponsePagination struct {
	Offset int `json:"offset"`
	Size   int `json:"size"`
	Total  int `json:"total"`
}

Jump to

Keyboard shortcuts

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