httpext

package
v0.0.0-...-acb6f69 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2025 License: Apache-2.0 Imports: 11 Imported by: 20

Documentation

Overview

Package httpext provides some convenience functions on top of the "net/http" package from the stdlib.

Index

Constants

This section is empty.

Variables

View Source
var ShutdownTimeout = 30 * time.Second

ShutdownTimeout is the timeout that ListenAndServeContext() will impose on server.Shutdown() before forcefully terminating request handlers that are still in progress.

The default timeout is quite lenient to accommodate long-running requests, but it can be lowered for servers running in an interactive terminal session where a quick response to Ctrl-C is more important.

Functions

func ContextWithSIGINT

func ContextWithSIGINT(ctx context.Context, delay time.Duration) context.Context

ContextWithSIGINT creates a new context.Context using the provided Context, and launches a goroutine that cancels the Context when an interrupt signal is caught.

This function is not strictly related to net/http, but fits nicely with func ListenAndServeContext from this package.

If `delay` is not 0, the context will be canceled with this delay after an interrupt signal was caught. This is useful when using the context with ListenAndServeContext(), to give reverse-proxies using this HTTP server some extra delay to notice the pending shutdown of this server.

func GetRequesterIPFor

func GetRequesterIPFor(r *http.Request) string

GetRequesterIPFor inspects an http.Request and returns the IP address of the machine where the request originated (or the empty string if no IP can be found in the request).

func ListenAndServeContext

func ListenAndServeContext(ctx context.Context, addr string, handler http.Handler) error

ListenAndServeContext is a wrapper around http.ListenAndServe() that additionally shuts down the HTTP server gracefully when the context expires, or if an error occurs.

func ListenAndServeTLSContext

func ListenAndServeTLSContext(ctx context.Context, addr, certFile, keyFile string, handler http.Handler) error

ListenAndServeTLSContext is a wrapper around http.ListenAndServeTLS() that additionally shuts down the HTTP server gracefully when the context expires, or if an error occurs.

Types

type WrappedTransport

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

WrappedTransport is a wrapper that adds various global behaviors to an `http.RoundTripper` such as `http.DefaultTransport`.

func WrapTransport

func WrapTransport(transport *http.RoundTripper) *WrappedTransport

WrapTransport replaces the given `http.RoundTripper` with a wrapped version that can be modified using the returned WrappedTransport instance. This usually targets `http.DefaultTransport` like this:

transport := httpext.WrapTransport(&http.DefaultTransport)
transport.SetOverrideUserAgent("example", "1.0")

func (*WrappedTransport) Attach

func (w *WrappedTransport) Attach(wrap func(http.RoundTripper) http.RoundTripper)

Attach adds a custom modifier to the DefaultTransport. The provided function will be used to wrap the existing RoundTripper instance.

func (*WrappedTransport) SetInsecureSkipVerify

func (w *WrappedTransport) SetInsecureSkipVerify(insecure bool)

SetInsecureSkipVerify sets the InsecureSkipVerify flag on the inner Transport's tls.Config. This flag should only be set for testing, esp. to enable capturing of requests made by this application through a tracing proxy like mitmproxy.

func (*WrappedTransport) SetOverrideUserAgent

func (w *WrappedTransport) SetOverrideUserAgent(appName, appVersion string)

SetOverrideUserAgent sets a User-Agent header that will be injected into all HTTP requests that are made with the http.DefaultTransport. The User-Agent string is constructed as "appName/appVersion" from the two provided arguments. The arguments usually come from go-api-declarations/bininfo, for example:

httpext.ModifyDefaultTransport().SetOverrideUserAgent(bininfo.Component(), bininfo.Version())

Jump to

Keyboard shortcuts

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