Documentation ¶
Overview ¶
Package request provides functions for http request handling.
TODO(cs) => github.com/streadway/handy
Index ¶
Constants ¶
const ( IPForwardedIgnore = 1<<iota + 1 IPForwardedTrust )
IPForwarded* must be set as an option to function RealIP() to specify if you trust the forwarded headers.
const HeaderIDKeyName = "X-Request-Id"
HeaderIDKeyName defines the name of the header used to transmit the request ID.
Variables ¶
var ForwardedIPHeaders = headers{csnet.XForwarded, csnet.XForwardedFor, csnet.Forwarded, csnet.ForwardedFor, csnet.XRealIP, csnet.ClientIP, csnet.XClusterClientIP}
ForwardedIPHeaders contains a list of available headers which might contain the client IP address.
Functions ¶
func AcceptsContentType ¶
AcceptsContentType checks if a request requests a specific content type.
func AcceptsJSON ¶
AcceptsJSON returns true if the request requests JSON as a return-format.
func InIPRange ¶
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 ¶
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 ¶
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 ¶
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.