wraps

package
v2.7.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2018 License: MIT Imports: 12 Imported by: 1

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

Constants

This section is empty.

Variables

View Source
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")
)
View Source
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.

View Source
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

View Source
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.

View Source
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.

View Source
var Stop = stop{}

Stop is a wrapper that does no processing but simply prevents further execution of next wrappers

Functions

func After

func After(h http.Handler) wrap.Wrapper

After returns an AfterFunc for a http.Handler

func Around

func Around(before, after http.Handler) wrap.Wrapper

Around returns a wrapper that calls the given before and after handler before and after the next handler when serving

func AroundFunc

func AroundFunc(before, after func(http.ResponseWriter, *http.Request)) wrap.Wrapper

AroundFunc returns a wrapper that acts like Around, but for HandleFuncs

func Before

func Before(h http.Handler) wrap.Wrapper

Before returns an BeforeFunc for a http.Handler

func Catch

func Catch(c Catcher) wrap.Wrapper

Catch returns a CatchFunc for a Catcher

func DELETEHandler

func DELETEHandler(path string, handler http.Handler) *matchHandler

func Defer

func Defer(h http.Handler) wrap.Wrapper

func DeferFunc

func DeferFunc(fn func(http.ResponseWriter, *http.Request)) wrap.Wrapper

func ErrorHandler

func ErrorHandler(h http.Handler) wrap.Wrapper

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

func Fallback(ignoreCodes []int, handler ...http.Handler) wrap.Wrapper

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

func FilterBody(m method.Method) wrap.Wrapper

Filter the body for the given method

func First

func First(handler ...http.Handler) wrap.Wrapper

First will try all given handler until the first one returns something

func FirstFunc

func FirstFunc(handlerFn ...func(w http.ResponseWriter, r *http.Request)) wrap.Wrapper

FirstFunc is like First but for http.HandlerFuncs

func GETHandler

func GETHandler(path string, handler http.Handler) *matchHandler

func Guard

func Guard(h http.Handler) wrap.Wrapper

Guard returns a GuardFunc for a http.Handler

func HEADHandler

func HEADHandler(path string, handler http.Handler) *matchHandler
func Head() wrap.Wrapper

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

func IfMatch(h http.Handler) wrap.Wrapper

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 OPTIONSHandler(path string, handler http.Handler) *matchHandler

func PATCHHandler

func PATCHHandler(path string, handler http.Handler) *matchHandler

func POSTHandler

func POSTHandler(path string, handler http.Handler) *matchHandler

func PUTHandler

func PUTHandler(path string, handler http.Handler) *matchHandler

func PrepareLikeMux

func PrepareLikeMux() wrap.Wrapper

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

func SetRequestHeader(key, val string) wrap.Wrapper

SetRequestHeader sets a request header

func SetResponseHeader

func SetResponseHeader(key, val string) wrap.Wrapper

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

func (a AfterFunc) ServeHTTPNext(next http.Handler, wr http.ResponseWriter, req *http.Request)

func (AfterFunc) Wrap

func (a AfterFunc) Wrap(next http.Handler) http.Handler

Wrap implements the wrap.Wrapper interface

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)

func (BeforeFunc) Wrap

func (b BeforeFunc) Wrap(next http.Handler) http.Handler

Wrap implements the wrap.Wrapper interface

type CSSString

type CSSString string

CSSString is an utf-8 string that is a http.Handler and a wrap.Wrapper

func (CSSString) ServeHTTP

func (t CSSString) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP writes the CSSString to the http.ResponseWriter and sets Content-Type header to text/css; charset=utf-8

func (CSSString) Wrap

func (t CSSString) Wrap(http.Handler) http.Handler

Wrap implements the wrap.Wrapper interface

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

func (c CatchFunc) ServeHTTPNext(next http.Handler, wr http.ResponseWriter, req *http.Request)

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.

func (CatchFunc) Wrap

func (c CatchFunc) Wrap(next http.Handler) http.Handler

Wrap implements the wrap.Wrapper interface

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

func (ContentType) Wrap

func (c ContentType) Wrap(next http.Handler) http.Handler

Wrap wraps the given next handler with the returned handler

type DispatchFunc

type DispatchFunc func(*http.Request) http.Handler

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) Dispatch

func (df DispatchFunc) Dispatch(r *http.Request) 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)

func (DispatchFunc) Wrap

func (df DispatchFunc) Wrap(next http.Handler) (out http.Handler)

type Dispatcher

type Dispatcher interface {
	Dispatch(*http.Request) http.Handler
}

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

func (g GuardFunc) ServeHTTPNext(next http.Handler, wr http.ResponseWriter, req *http.Request)

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.

func (GuardFunc) Wrap

func (g GuardFunc) Wrap(next http.Handler) http.Handler

Wrap implements the wrap.Wrapper interface

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

func (HTMLString) Wrap

Wrap implements the wrap.Wrapper interface

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

func (JSONString) Wrap

Wrap implements the wrap.Wrapper interface

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

func (JavaScriptString) Wrap

Wrap implements the wrap.Wrapper interface

type MatchFunc

type MatchFunc func(*http.Request) bool

MatchFunc is a Matcher based on a function

func (MatchFunc) Match

func (mf MatchFunc) Match(req *http.Request) bool

Match implements Matcher for the MatchFunc

type MatchHost

type MatchHost string

MatchHost matches based on the request host

func (MatchHost) Match

func (mh MatchHost) Match(r *http.Request) bool

type MatchMethod

type MatchMethod string

MatchMethod matches based on the request method (like GET, POST etc.)

func (MatchMethod) Match

func (mh MatchMethod) Match(r *http.Request) bool

type MatchPath

type MatchPath string

MatchMethod matches based on the request path

func (MatchPath) Match

func (mh MatchPath) Match(r *http.Request) bool

type MatchScheme

type MatchScheme string

MatchScheme matches based on the request scheme

func (MatchScheme) Match

func (m MatchScheme) Match(r *http.Request) bool

type Matcher

type Matcher interface {
	// Match returns if the request matches
	Match(*http.Request) bool
}

Matcher is an interface for types that can match against a http.Request

func And

func And(ms ...Matcher) Matcher

And logically combines different matchers to a single matcher that only matches if all matchers match.

func MatchHeader

func MatchHeader(key, value string) Matcher

MatchHeader matches based on the request header

func MatchHostRegex

func MatchHostRegex(r *regexp.Regexp) Matcher

MatchHostRegex matches the request host against a reqular expression

func MatchPathRegex

func MatchPathRegex(r *regexp.Regexp) Matcher

MatchPathRegex matches the request path against a reqular expression

func MatchQuery

func MatchQuery(key, val string) Matcher

MatchQuery matches based on the request query

func Or

func Or(ms ...Matcher) Matcher

And logically combines different matchers to a single matcher that matches if one of the matchers match.

type MethodHandler

type MethodHandler struct {
	GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD http.Handler
}

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)

func (*MethodHandler) Wrap

func (mh *MethodHandler) Wrap(next http.Handler) (out http.Handler)

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 (MethodOverrideByField) Wrap

func (m MethodOverrideByField) Wrap(next http.Handler) (out http.Handler)

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

func (RemoveRequestHeader) Wrap

Wrap wraps the given next handler with the returned handler

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

func (RemoveResponseHeader) Wrap

Wrap wraps the given next handler with the returned handler

type String

type String string

String is an utf-8 string that is a http.Handler and a wrap.Wrapper

func (String) ServeHTTP

func (s String) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP writes the String to the http.ResponseWriter

func (String) Wrap

func (s String) Wrap(http.Handler) http.Handler

Wrap implements the wrap.Wrapper interface

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

func (TextString) Wrap

Wrap implements the wrap.Wrapper interface

Jump to

Keyboard shortcuts

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