http

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2020 License: MIT Imports: 23 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultXRequestIDKey = "X-Request-ID"

DefaultXRequestIDKey is metadata key name for request ID

Functions

func ContentType

func ContentType(content io.Reader, name string) (ctype string, bufferedContent io.Reader, err error)

func DoesRequestSatisfyTlsTermination

func DoesRequestSatisfyTlsTermination(r *http.Request, whitelistedPaths []string, allowedTLSCIDRs []string) error

DoesRequestSatisfyTlsTermination returns whether the request fulfills tls's constraints, https, path matches any whitelisted paths or ip inclued by any cidr whitelistedPath is http path that does not need to be checked allowedTLSCIDR is the network includes ip.

func Get

func Get(url string) (resp *http.Response, err error)

func GetProxySchemeAndHost

func GetProxySchemeAndHost(r *http.Request, allowForwarded bool) (scheme, host string)

GetProxySchemeAndHost extracts the host and used protocol (either HTTP or HTTPS) from the given request. If `allowForwarded` is set, the X-Forwarded-Host, X-Forwarded-Proto and Forwarded headers will also be checked to support proxies.

func Head(url string) (resp *http.Response, err error)

func NewClient

func NewClient(u string) (*http.Client, error)

func Post

func Post(url, contentType string, body io.Reader) (resp *http.Response, err error)

func PostForm

func PostForm(url string, data url.Values) (resp *http.Response, err error)

func Recover

func Recover(writer io.Writer, req *http.Request, recoverHandler func(err interface{}) interface{}) interface{}

Recover and dump HTTP request if broken pipe

func RecoveryServerInterceptor

func RecoveryServerInterceptor(next http.Handler, out io.Writer, f func(w http.ResponseWriter, r *http.Request, err interface{})) http.Handler

RecoveryServerInterceptor returns a new server interceptors with recovery from panic. affect as recover{f()}; next()

func RejectInsecureServerInterceptor

func RejectInsecureServerInterceptor(next http.Handler, opts ...RejectInsecureOption) *rejectInsecure

RejectInsecureServerInterceptor returns a new server interceptor with tls check. reject the request fulfills tls's constraints,

func ReplacePath

func ReplacePath(path string, h http.Handler) http.Handler

StripPrefix returns a handler that serves HTTP requests by removing the given prefix from the request URL's Path and invoking the handler h. StripPrefix handles a request for a path that doesn't begin with prefix by replying with an HTTP 404 not found error.

func RequestIDFromContext

func RequestIDFromContext(ctx context.Context) string

func ResolveProxyUrl

func ResolveProxyUrl(u *url.URL, r *http.Request, allowForwarded bool) *url.URL

func ServeContent

func ServeContent(w http.ResponseWriter, r *http.Request, name string, modtime time.Time, content io.Reader, size int64)

ServeContent replies to the request using the content in the provided Reader. The main benefit of ServeContent over io.Copy is that it handles Range requests properly, sets the MIME type, and handles If-Match, If-Unmodified-Since, If-None-Match, If-Modified-Since, and If-Range requests.

If the response's Content-Type header is not set, ServeContent first tries to deduce the type from name's file extension and, if that fails, falls back to reading the first block of the content and passing it to DetectContentType. The name is otherwise unused; in particular it can be empty and is never sent in the response.

If modtime is not the zero time or Unix epoch, ServeContent includes it in a Last-Modified header in the response. If the request includes an If-Modified-Since header, ServeContent uses modtime to decide whether the content needs to be sent at all.

If the content's Seek method work: ServeContent uses a seek to the end of the content to determine its size, and the param size is ignored. The same as http.ServeFile If the content's Seek method doesn't work: ServeContent uses the param size to generate a onlySizeSeekable as a pseudo io.ReadSeeker. If size < 0, use chunk or connection close instead

If the caller has set w's ETag header formatted per RFC 7232, section 2.3, ServeContent uses it to handle requests using If-Match, If-None-Match, or If-Range.

Note that *os.File implements the io.ReadSeeker interface.

func XRequestIdServerInterceptor

func XRequestIdServerInterceptor(next http.Handler, keys ...interface{}) http.Handler

XRequestIdServerInterceptor returns a new server interceptor with x-request-id in context and response's Header. keys is context's key

Types

type Client

type Client struct {
	http.Client
}

func (*Client) Use

func (c *Client) Use(h ...RoundTripHandler) *Client

type EmptyRejectInsecureOption

type EmptyRejectInsecureOption struct{}

EmptyRejectInsecureOption does not alter the configuration. It can be embedded in another structure to build custom options.

This API is EXPERIMENTAL.

type RejectInsecureOption

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

A RejectInsecureOption sets options.

func RejectInsecureWithAllowedTlsCidrs

func RejectInsecureWithAllowedTlsCidrs(allowedTLSCIDRs []string) RejectInsecureOption

RejectInsecureWithAllowedTlsCidrs specifies whether to allow any request which client or proxy's ip included a cidr is a CIDR notation IP address and prefix length, like "192.0.2.0/24" or "2001:db8::/32", as defined in RFC 4632 and RFC 4291.

func RejectInsecureWithErrorLog

func RejectInsecureWithErrorLog(l *log.Logger) RejectInsecureOption

RejectInsecureWithErrorLog specifies an optional logger for errors

func RejectInsecureWithForceHttp

func RejectInsecureWithForceHttp(forceHttp bool) RejectInsecureOption

RejectInsecureWithForceHttp specifies whether to allow any request, as a shortcut circuit

func RejectInsecureWithWhitelistedPaths

func RejectInsecureWithWhitelistedPaths(whitelistedPaths []string) RejectInsecureOption

RejectInsecureWithWhitelistedPaths specifies whether to allow any request which http path matches

type RejectInsecureOptionFunc

type RejectInsecureOptionFunc func(*rejectInsecure)

RejectInsecureOptionFunc wraps a function that modifies rejectInsecure into an implementation of the RejectInsecureOption interface.

type RoundTripFunc

type RoundTripFunc func(req *http.Request) (resp *http.Response, err error)

The HandlerFunc type is an adapter to allow the use of ordinary functions as HTTP handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.

func (RoundTripFunc) RoundTrip

func (f RoundTripFunc) RoundTrip(req *http.Request) (resp *http.Response, err error)

type RoundTripHandler

type RoundTripHandler interface {
	RoundTrip(req *http.Request) (resp *http.Response, err error)
}

func RecoveryClientInterceptor

func RecoveryClientInterceptor(next RoundTripHandler, out io.Writer, f func(resp *http.Response, req *http.Request, err interface{})) RoundTripHandler

RecoveryClientInterceptor returns a new client interceptors with recovery from panic. affect as recover{f()}; next()

func XRequestIdClientInterceptor

func XRequestIdClientInterceptor(next RoundTripHandler, keys ...interface{}) RoundTripHandler

XRequestIdClientInterceptor returns a new client interceptors with x-request-id in context and request's Header.

type Transport

type Transport struct {
	// Base is the base RoundTripper used to make HTTP requests.
	// If nil, http.DefaultTransport is used.
	Base http.RoundTripper
	// contains filtered or unexported fields
}

Transport is an http.RoundTripper that makes OAuth 2.0 HTTP requests, wrapping a base RoundTripper and adding an Authorization header with a token from the supplied Sources.

Transport is a low-level mechanism. Most code will use the higher-level Config.Client method instead.

func (*Transport) Abort

func (t *Transport) Abort()

Abort prevents pending handlers from being called. Note that this will not stop the current handler. Let's say you have an authorization middleware that validates that the current request is authorized. If the authorization fails (ex: the password does not match), call Abort to ensure the remaining handlers for this request are not called.

func (*Transport) CancelRequest

func (t *Transport) CancelRequest(req *http.Request)

CancelRequest cancels an in-flight request by closing its connection.

func (*Transport) IsAborted

func (t *Transport) IsAborted() bool

IsAborted returns true if the current context was aborted.

func (*Transport) Next

func (t *Transport) Next(req *http.Request) (resp *http.Response, err error)

Next should be used only inside middleware. It executes the pending handlers in the chain inside the calling handler. See example in GitHub.

func (*Transport) RoundTrip

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

RoundTrip authorizes and authenticates the request with an access token. If no token exists or token is expired, tries to refresh/fetch a new token.

func (*Transport) Use

func (t *Transport) Use(h ...RoundTripHandler) *Transport

Use adds middleware handlers to the transport.

Jump to

Keyboard shortcuts

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