request

package
v0.0.0-...-202847b Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2023 License: Apache-2.0 Imports: 14 Imported by: 6

Documentation

Overview

Package request provides functions for http request handling.

TODO(cs) => github.com/streadway/handy

Index

Constants

View Source
const (
	IPForwardedIgnore = 1<<iota + 1
	IPForwardedTrust
)

IPForwarded* must be set as an option to function RealIP() to specify if you trust the forwarded headers.

View Source
const HeaderIDKeyName = "X-Request-Id"

HeaderIDKeyName defines the name of the header used to transmit the request ID.

Variables

ForwardedIPHeaders contains a list of available headers which might contain the client IP address.

Functions

func AcceptsContentType

func AcceptsContentType(r *http.Request, contentType string) bool

AcceptsContentType checks if a request requests a specific content type.

func AcceptsJSON

func AcceptsJSON(r *http.Request) bool

AcceptsJSON returns true if the request requests JSON as a return-format.

func InIPRange

func InIPRange(fromTo ...string) func(*http.Request) bool

InIPRange returns a function which can check if a requests real IP address is available in the provided list of IP ranges. You must provide a from and a to address. Passing imbalanced pairs returns a nil function pointer. The return function fits nicely with the signature of the package type auth.TriggerFunc. IPv4 or IPv6 doesn't matter.

func NotInIPRange

func NotInIPRange(fromTo ...string) func(*http.Request) bool

NotInIPRange returns a function which can check if a requests real IP address is NOT available in the provided list of IP ranges. You must provide a from and a to address. Passing imbalanced pairs returns a nil function pointer. The return function fits nicely with the signature of the package type auth.TriggerFunc. IPv4 or IPv6 doesn't matter.

func RealIP

func RealIP(r *http.Request, opts int) net.IP

RealIP extracts the remote address from a request and takes care of different headers in which an IP address can be stored. Checks if the IP in one of the header fields lies in net.PrivateIPRanges. For the second argument opts please see the constants IPForwarded*. Return value can be nil. A check for the RealIP costs 8 allocs, for now.

Types

type ID

type ID struct {
	// HeaderIDKeyName identifies the key name in the request header. Can be
	// empty and falls back to constant HeaderIDKeyName.
	HeaderIDKeyName string
	// Count defines the optional start value. If nil starts at zero. To access
	// securely "Count" you must use the atomic package.
	Count *uint64
	// NewIDFunc generates a new ID. Can be nil.
	NewIDFunc func(*http.Request) string
	Log       log.Logger
}

ID represents a middleware for request Id generation and adding the ID to the header.

func (*ID) With

func (iw *ID) With() func(h http.Handler) http.Handler

With is a middleware that injects a request ID into the response header of each request. Retrieve it using:

w.Header().Get(HeaderIDKeyName)

If the incoming request has a HeaderIDKeyName header then that value is used otherwise a random value is generated. You can specify your own generator by providing the NewIDFunc in an option. No options uses the default request prefix generator. The returned function is compatible to type mw.Middleware.

Jump to

Keyboard shortcuts

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