Documentation
¶
Index ¶
- Variables
- func Authenticate(username, password, realm string) func(http.Handler) http.Handler
- func ByteSize(str string) (int64, error)
- func CORS(policy CORSPolicy) func(http.Handler) http.Handler
- func Compose(chain ...interface{}) http.Handler
- func ContentSecurity(policy ContentPolicy) func(http.Handler) http.Handler
- func Directory(prefix, directory string) http.Handler
- func Forwarded(useIP, usePort, useProto, fakeTLS bool) func(http.Handler) http.Handler
- func Hostname(host string) string
- func IP(addr string) string
- func Limit(limit int64) func(http.Handler) http.Handler
- func LimitBody(w http.ResponseWriter, r *http.Request, limit int64)
- func Local(handler http.Handler) http.RoundTripper
- func MustByteSize(str string) int64
- func Protect(rate int, duration time.Duration) func(http.Handler) http.Handler
- func Record(h http.Handler, method, url string, headers map[string]string, payload string) *httptest.ResponseRecorder
- func Recover(reporter func(error)) func(h http.Handler) http.Handler
- func Secure(w http.ResponseWriter, r *http.Request, allowInsecure, noFrontend bool, ...) bool
- func Security(allowInsecure, noFrontend bool, stsMaxAge time.Duration) func(http.Handler) http.Handler
- func Throttle(concurrency int) func(http.Handler) http.Handler
- func Timeout(timeout time.Duration) func(http.Handler) http.Handler
- type BodyLimiter
- type CORSPolicy
- type ContentPolicy
Constants ¶
This section is empty.
Variables ¶
var ErrBodyLimitExceeded = errors.New("body limit exceeded")
ErrBodyLimitExceeded is returned if a body is read beyond the set limit.
var ErrInvalidByteSize = errors.New("serve: byte size must be like 4K, 20MiB or 5GB")
ErrInvalidByteSize is returned for invalid byte sizes.
Functions ¶
func Authenticate ¶ added in v0.5.0
Authenticate returns a middleware that enforces a HTTP Basic Authentication.
func ByteSize ¶ added in v0.3.0
ByteSize parses human readable byte sizes (e.g. 4K, 20MiB or 5GB) and returns the amount of bytes they represent. ErrInvalidByteSize is returned if the specified byte size is invalid.
func CORS ¶
func CORS(policy CORSPolicy) func(http.Handler) http.Handler
CORS returns a middleware for enforcing CORS.
func ContentSecurity ¶
func ContentSecurity(policy ContentPolicy) func(http.Handler) http.Handler
ContentSecurity returns a middleware for enforcing content security.
func Directory ¶ added in v0.5.0
Directory constructs a handler that serves a directory found at the specified path. It will serve the index file for not found paths.
func Forwarded ¶ added in v0.3.5
Forwarded is a middleware that will parse the selected "X-Forwarded-X" headers and mutate the request to reflect the conditions described by the headers.
Note: This technique should only be applied to apps that are behind a load balancer that will *always* set the selected headers. Otherwise an attacker may be able to provide false information and circumvent security limitations.
func Hostname ¶ added in v0.1.3
Hostname will return the hostname from the provided host string. This method should be used instead of net.SplitHostPort when attempting to clean the http/Request.Host attribute.
func LimitBody ¶
func LimitBody(w http.ResponseWriter, r *http.Request, limit int64)
LimitBody will limit reading from the body of the supplied request to the specified amount of bytes. Earlier calls to LimitBody will be overwritten which essentially allows callers to increase the limit from a default limit later in the request processing.
func Local ¶ added in v0.3.1
func Local(handler http.Handler) http.RoundTripper
Local returns a round tripper that uses the provided handler to serve the requests. It may be used with http.Client in unit tests.
func MustByteSize ¶ added in v0.3.0
MustByteSize will call ByteSize and panic on errors.
func Protect ¶ added in v0.5.0
Protect will return a middleware that will rate limit requests based on the remote IP address. It will allow up to the specified rate of requests per duration.
func Record ¶
func Record(h http.Handler, method, url string, headers map[string]string, payload string) *httptest.ResponseRecorder
Record will make a request against the specified handler and record the result.
func Recover ¶ added in v0.3.5
Recover is a middleware that recovers panics and forwards the error to the the provided reporter.
func Secure ¶
func Secure(w http.ResponseWriter, r *http.Request, allowInsecure, noFrontend bool, stsMaxAge time.Duration) bool
Secure will enforce various common web security policies. It return whether the request is safe to be further processed. Subsequent handlers should update the headers with a more applicable content security policy.
func Security ¶
func Security(allowInsecure, noFrontend bool, stsMaxAge time.Duration) func(http.Handler) http.Handler
Security will return a middleware that enforce various standard web security techniques.
Types ¶
type BodyLimiter ¶
type BodyLimiter struct {
Length int64
Limit int64
Original io.ReadCloser
Limited io.ReadCloser
}
BodyLimiter wraps a io.ReadCloser and keeps a reference to the original.
func (*BodyLimiter) Close ¶ added in v0.2.0
func (l *BodyLimiter) Close() error
Close will close the body.
type ContentPolicy ¶
ContentPolicy for defining content security.
func (ContentPolicy) String ¶
func (p ContentPolicy) String() string
String will encode the policy as a string.