Documentation ¶
Overview ¶
Package wraps provides middleware for http://github.com/go-on/wrap.
A walk through some of the middlewares can be found at http://metakeule.github.io/article/tour-de-wrap-middleware.html.
Content
body writer: - EscapeHTML - GZip - ReadSeeker error handling: - Error - Catch - Defer caching: - ETag - IfNoneMatch - IfMatch combinators: - After - Before - Around - Fallback - First - Guard dispatching: - Dispatch - Map - MethodHandler - And - Or - Match(Method|Path|Host|Scheme|Query|Header) REST: - Head - MethodOverride - (GET|POST|PUT|DELETE|PATCH|OPTIONS|HEAD)Handler http.Handler: - (Text|JSON|CSS|HTML|JavaScript)String header manipulation: - ContentType - (Remove|Set)(Request|Response)Header
Integration of 3rd party middleware (http://godoc.org/github.com/go-on/wrap-contrib/third-party)
- wrapnosurf (github.com/justinas/nosurf)
- wraphttpauth (github.com/abbot/go-http-auth)
- wrapsession (github.com/gorilla/sessions)
Contributions ¶
Yes, please! Make a pull request and let me see. If it is not matured consider adding it to http://github.com/go-on/wrap-contrib-testing.
More Middleware ¶
More (WIP,API may change) middleware can be found at http://github.com/go-on/wrap-contrib-testing
Index ¶
- Variables
- func After(h http.Handler) wrap.Wrapper
- func Around(before, after http.Handler) wrap.Wrapper
- func AroundFunc(before, after func(http.ResponseWriter, *http.Request)) wrap.Wrapper
- func Before(h http.Handler) wrap.Wrapper
- func Catch(c Catcher) wrap.Wrapper
- func DELETEHandler(path string, handler http.Handler) *matchHandler
- func Defer(h http.Handler) wrap.Wrapper
- func DeferFunc(fn func(http.ResponseWriter, *http.Request)) wrap.Wrapper
- func ErrorHandler(h http.Handler) wrap.Wrapper
- func ErrorHandlerFunc(h http.HandlerFunc) wrap.Wrapper
- func Fallback(ignoreCodes []int, handler ...http.Handler) wrap.Wrapper
- func FallbackFunc(ignoreCodes []int, handlerFn ...func(w http.ResponseWriter, r *http.Request)) wrap.Wrapper
- func FilterBody(m method.Method) wrap.Wrapper
- func First(handler ...http.Handler) wrap.Wrapper
- func FirstFunc(handlerFn ...func(w http.ResponseWriter, r *http.Request)) wrap.Wrapper
- func GETHandler(path string, handler http.Handler) *matchHandler
- func Guard(h http.Handler) wrap.Wrapper
- func HEADHandler(path string, handler http.Handler) *matchHandler
- func Head() wrap.Wrapper
- func IfMatch(h http.Handler) wrap.Wrapper
- func MethodOverride() methodOverride
- func OPTIONSHandler(path string, handler http.Handler) *matchHandler
- func PATCHHandler(path string, handler http.Handler) *matchHandler
- func POSTHandler(path string, handler http.Handler) *matchHandler
- func PUTHandler(path string, handler http.Handler) *matchHandler
- func PrepareLikeMux() wrap.Wrapper
- func ReadSeeker(r io.ReadSeeker) wrap.Wrapper
- func SetRequestHeader(key, val string) wrap.Wrapper
- func SetResponseHeader(key, val string) wrap.Wrapper
- type AfterFunc
- type BeforeFunc
- type CSSString
- type CatchFunc
- type Catcher
- type ContentType
- type DispatchFunc
- func (df DispatchFunc) Dispatch(r *http.Request) http.Handler
- func (df DispatchFunc) ServeHTTP(wr http.ResponseWriter, req *http.Request)
- func (df DispatchFunc) ServeHTTPNext(next http.Handler, wr http.ResponseWriter, req *http.Request)
- func (df DispatchFunc) Wrap(next http.Handler) (out http.Handler)
- type Dispatcher
- type Error
- type GuardFunc
- type HTMLString
- type JSONString
- type JavaScriptString
- type MatchFunc
- type MatchHost
- type MatchMethod
- type MatchPath
- type MatchScheme
- type Matcher
- type MethodHandler
- type MethodOverrideByField
- type RemoveRequestHeader
- type RemoveResponseHeader
- type String
- type TextString
Constants ¶
This section is empty.
Variables ¶
var ( JSONContentType = ContentType("application/json; charset=utf-8") TextContentType = ContentType("text/plain; charset=utf-8") CSSContentType = ContentType("text/css; charset=utf-8") HTMLContentType = ContentType("text/html; charset=utf-8") JavaScriptContentType = ContentType("application/javascript; charset=utf-8") RSSFeedContentType = ContentType("application/rss+xml; charset=utf-8") AtomFeedContentType = ContentType("application/atom+xml; charset=utf-8") )
var ETag = etag{}
ETag buffers the body writes of the next handler and calculates a md5 Hash based on the Content-Type + Body combination and sets it as etag in the response header. It does so only for GET and HEAD requests. For GET requests the buffered body is flushed to the underlying response writer.
var EscapeHTML = escapeHTML{}
EscapeHTML wraps the next handler by replacing the response writer with an EscapeHTMLResponseWriter that escapes html special chars while writing to the underlying response writer
var GZip = _gzip{}
GZip compresses the body written by the next handlers on the fly if the client did set the request header tAccept-Encoding header to gzip. It also sets the response header Content-Encoding to gzip if it did the compression.
var IfNoneMatch = ifNoneMatch{}
IfNoneMatch only acts upon HEAD and GET requests that have a If-None-Match header set. It then runs the next handler and checks if an Etag header is set and if it matches the If-None-Match. It it does, the status code 304 (not modified) is sent (without body). Otherwise the response is flushed as is. The combination of Etag and IfNoneMatch can be used to trigger effective client side caching. But IfNoneMatch may also be used with a custom handler that sets the ETag header.
var Stop = stop{}
Stop is a wrapper that does no processing but simply prevents further execution of next wrappers
Functions ¶
func Around ¶
Around returns a wrapper that calls the given before and after handler before and after the next handler when serving
func AroundFunc ¶
AroundFunc returns a wrapper that acts like Around, but for HandleFuncs
func DELETEHandler ¶
func ErrorHandler ¶
ErrorHandler returns a wrapper that requires the response writer to implement the wrap.Contexter interface and to support the *error type. When serving, it makes a fake run on the next handler and checks, if there is an error context and if so, it runs the given handler. Otherwise the writes of the next handler are flushed to the response writer
func ErrorHandlerFunc ¶
func ErrorHandlerFunc(h http.HandlerFunc) wrap.Wrapper
ErrorHandlerFunc is the same as ErrorHandler but for a http.HandlerFunc
func Fallback ¶
Fallback will try all given handler until the first one writes to the ResponseWriter body. It is similar to First, but ignores writes that did set one of the ignore status codes
func FallbackFunc ¶
func FallbackFunc(ignoreCodes []int, handlerFn ...func(w http.ResponseWriter, r *http.Request)) wrap.Wrapper
FallbackFunc is like Fallback but for http.HandlerFuncs
func FilterBody ¶
Filter the body for the given method
func GETHandler ¶
func HEADHandler ¶
func Head ¶
Head will check if the request method is HEAD. if so, it will change the method to GET, call the handler with a ResponseBuffer and return only the header information to the client.
For non HEAD methods, it simply pass the request handling through to the http.handler
func IfMatch ¶
IfMatch only acts upon GET, PUT, DELETE or PATCH requests, that have the If-Match header set. It then issues a HEAD request to the given handler in order to get the etag from the header and compares the etag to the one given via the If-Match header. If it matches, the next handler is called, otherwise status 412 (precondition failed) is returned IfMatch may be used in combination with a middleware stack that uses ETag or any custom handler that sets the ETag header.
func MethodOverride ¶
func MethodOverride() methodOverride
func OPTIONSHandler ¶
func PATCHHandler ¶
func POSTHandler ¶
func PUTHandler ¶
func PrepareLikeMux ¶
PrepareLikeMux prepares a request the same way that net/http ServeMux does
func ReadSeeker ¶
func ReadSeeker(r io.ReadSeeker) wrap.Wrapper
ReadSeeker provides a wrap.Wrapper for a io.ReadSeeker
func SetRequestHeader ¶
SetRequestHeader sets a request header
func SetResponseHeader ¶
SetResponseHeader sets a response header
Types ¶
type AfterFunc ¶
type AfterFunc func(http.ResponseWriter, *http.Request)
AfterFunc is of the type http.HandlerFunc and provides a wrap.Wrapper that calls itself after the inner handler has been called
func (AfterFunc) ServeHTTPNext ¶
type BeforeFunc ¶
type BeforeFunc func(http.ResponseWriter, *http.Request)
BeforeFunc is of the type http.HandlerFunc and provides a wrap.Wrapper that calls itself before the next handler has been called
func (BeforeFunc) ServeHTTPNext ¶
func (b BeforeFunc) ServeHTTPNext(next http.Handler, wr http.ResponseWriter, req *http.Request)
type CSSString ¶
type CSSString string
CSSString is an utf-8 string that is a http.Handler and a wrap.Wrapper
type CatchFunc ¶
type CatchFunc func(recovered interface{}, w http.ResponseWriter, r *http.Request)
CatchFunc is a function fullfilling the Catcher interface
func (CatchFunc) Catch ¶
func (c CatchFunc) Catch(recovered interface{}, w http.ResponseWriter, r *http.Request)
Catch fullfills the Catcher interface
func (CatchFunc) ServeHTTPNext ¶
ServeHTTPNext serves the given request by letting the next serve a ResponseBuffer and catching any panics. If no panic happened, the ResponseBuffer is flushed to the ResponseWriter Otherwise the CatchFunc is called.
type Catcher ¶
type Catcher interface { // Catch is called if a http.Handler recovered from a panic // It is given the responsewriter and request and the return // value from the recover() call // The given ResponseWriter is only to enable Catch to write on it // and should not expected to have any bits written so far, Catch // should not examinate the ResponseWriter. Catch(recovered interface{}, w http.ResponseWriter, r *http.Request) }
Catcher provides a Catch method that is called if a http.Handler recovered from a panic
type ContentType ¶
type ContentType string
ContentType writes the content type if the next handler was successful and did not set a content-type
func (ContentType) ServeHTTPNext ¶
func (c ContentType) ServeHTTPNext(next http.Handler, wr http.ResponseWriter, req *http.Request)
ServeHandle serves the given request with the next handler and after that writes the content type, if the next handler was successful and did not set a content-type
func (ContentType) SetContentType ¶
func (c ContentType) SetContentType(w http.ResponseWriter)
SetContentType sets the content type in the given ResponseWriter
func (ContentType) String ¶
func (c ContentType) String() string
type DispatchFunc ¶
func Dispatch ¶
func Dispatch(d Dispatcher) DispatchFunc
func Map ¶
func Map(data ...interface{}) DispatchFunc
data should be pairs of Matcher and http.Handler
func (DispatchFunc) ServeHTTP ¶
func (df DispatchFunc) ServeHTTP(wr http.ResponseWriter, req *http.Request)
func (DispatchFunc) ServeHTTPNext ¶
func (df DispatchFunc) ServeHTTPNext(next http.Handler, wr http.ResponseWriter, req *http.Request)
type Error ¶
type Error error
Error a type based on error that should be saved by a wrap.Contexter (response writer)
type GuardFunc ¶
type GuardFunc func(http.ResponseWriter, *http.Request)
GuardFunc is a wrap.Wapper and http.HandlerFunc that may operate on the ResponseWriter If it does so, the wrapper prevents the next Handler from serving.
func (GuardFunc) ServeHTTPNext ¶
ServeHTTPNext lets the GuardFunc serve to a ResponseBuffer and if it changed something the Response is send to the ResponseWriter, preventing the next http.Handler from executing. Otherwise the next handler serves the request.
type HTMLString ¶
type HTMLString string
HTMLString is an utf-8 string that is a http.Handler and a wrap.Wrapper
func (HTMLString) ServeHTTP ¶
func (t HTMLString) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP writes the HTMLString to the http.ResponseWriter and sets Content-Type header to text/html; charset=utf-8
type JSONString ¶
type JSONString string
JSONString is an utf-8 string that is a http.Handler and a wrap.Wrapper
func (JSONString) ServeHTTP ¶
func (t JSONString) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP writes the JSONString to the http.ResponseWriter and sets Content-Type header to application/json; charset=utf-8
type JavaScriptString ¶
type JavaScriptString string
JavaScriptString is a type alias for string that is a http.Handler and a wrap.Wrapper
func (JavaScriptString) ServeHTTP ¶
func (t JavaScriptString) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP writes the JavaScriptString to the http.ResponseWriter and sets Content-Type header to application/javascript; charset=utf-8
type MatchMethod ¶
type MatchMethod string
MatchMethod matches based on the request method (like GET, POST etc.)
type Matcher ¶
Matcher is an interface for types that can match against a http.Request
func And ¶
And logically combines different matchers to a single matcher that only matches if all matchers match.
func MatchHeader ¶
MatchHeader matches based on the request header
func MatchHostRegex ¶
MatchHostRegex matches the request host against a reqular expression
func MatchPathRegex ¶
MatchPathRegex matches the request path against a reqular expression
func MatchQuery ¶
MatchQuery matches based on the request query
type MethodHandler ¶
func (*MethodHandler) ServeHTTP ¶
func (mh *MethodHandler) ServeHTTP(wr http.ResponseWriter, req *http.Request)
func (*MethodHandler) ServeHTTPNext ¶
func (mh *MethodHandler) ServeHTTPNext(next http.Handler, wr http.ResponseWriter, req *http.Request)
type MethodOverrideByField ¶
type MethodOverrideByField string
MethodOverrideByField overrides the request method by looking for a field that contains the target method. It only acts on POST requests and on post bodies.
func (MethodOverrideByField) ServeHTTP ¶
func (m MethodOverrideByField) ServeHTTP(w http.ResponseWriter, r *http.Request)
type RemoveRequestHeader ¶
type RemoveRequestHeader string
RemoveRequestHeader removes request headers that are identical to the string or have it as prefix
func (RemoveRequestHeader) ServeHTTP ¶
func (rh RemoveRequestHeader) ServeHTTP(w http.ResponseWriter, r *http.Request)
func (RemoveRequestHeader) ServeHTTPNext ¶
func (rh RemoveRequestHeader) ServeHTTPNext(inner http.Handler, w http.ResponseWriter, r *http.Request)
ServeHTTPNext removes request headers that are identical to the string or have it as prefix. Then the inner http.Handler is called
type RemoveResponseHeader ¶
type RemoveResponseHeader string
RemoveResponseHeader removes response headers that are identical to the string or have if as prefix
func (RemoveResponseHeader) ServeHTTPNext ¶
func (rh RemoveResponseHeader) ServeHTTPNext(next http.Handler, w http.ResponseWriter, r *http.Request)
ServeHTTPNext removes the response headers that are identical to the string or have if as prefix after the next handler is run
type String ¶
type String string
String is an utf-8 string that is a http.Handler and a wrap.Wrapper
type TextString ¶
type TextString string
TextString is an utf-8 string that is a http.Handler and a wrap.Wrapper
func (TextString) ServeHTTP ¶
func (t TextString) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP writes the TextString to the http.ResponseWriter and sets Content-Type header to text/plain; charset=utf-8
Source Files ¶
- after.go
- around.go
- before.go
- catch.go
- content_type.go
- defer.go
- dispatch.go
- doc.go
- error.go
- escape_html.go
- etag.go
- fallback.go
- filter_body.go
- first.go
- guard.go
- gzip.go
- head.go
- match.go
- method_override.go
- panic_on_wrong_write_order.go
- prepare_like_mux.go
- readseeker.go
- remove_request_header.go
- remove_response_header.go
- set_request_header.go
- set_response_header.go
- stop.go
- string.go