summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/gorilla/mux/middleware.go
diff options
context:
space:
mode:
authorMartin Kraft <martinkraft@gmail.com>2018-05-22 12:54:00 -0400
committerMartin Kraft <martinkraft@gmail.com>2018-05-22 12:54:00 -0400
commit4c683aff7627040ff811f065848a820b2cb19d59 (patch)
tree668061e8acfd8827fd02bab18a998b7986944296 /vendor/github.com/gorilla/mux/middleware.go
parenteb78d273f39202046fa71555a5a19b0ec8a95cb3 (diff)
parent1af1bce6199597bb2d41ddcdc00ef0f28a73c83e (diff)
downloadchat-4c683aff7627040ff811f065848a820b2cb19d59.tar.gz
chat-4c683aff7627040ff811f065848a820b2cb19d59.tar.bz2
chat-4c683aff7627040ff811f065848a820b2cb19d59.zip
Merge remote-tracking branch 'origin/master' into advanced-permissions-phase-2
Diffstat (limited to 'vendor/github.com/gorilla/mux/middleware.go')
-rw-r--r--vendor/github.com/gorilla/mux/middleware.go52
1 files changed, 48 insertions, 4 deletions
diff --git a/vendor/github.com/gorilla/mux/middleware.go b/vendor/github.com/gorilla/mux/middleware.go
index 8f898675e..ceb812cee 100644
--- a/vendor/github.com/gorilla/mux/middleware.go
+++ b/vendor/github.com/gorilla/mux/middleware.go
@@ -1,6 +1,9 @@
package mux
-import "net/http"
+import (
+ "net/http"
+ "strings"
+)
// MiddlewareFunc is a function which receives an http.Handler and returns another http.Handler.
// Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed
@@ -12,17 +15,58 @@ type middleware interface {
Middleware(handler http.Handler) http.Handler
}
-// MiddlewareFunc also implements the middleware interface.
+// Middleware allows MiddlewareFunc to implement the middleware interface.
func (mw MiddlewareFunc) Middleware(handler http.Handler) http.Handler {
return mw(handler)
}
// Use appends a MiddlewareFunc to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Router.
-func (r *Router) Use(mwf MiddlewareFunc) {
- r.middlewares = append(r.middlewares, mwf)
+func (r *Router) Use(mwf ...MiddlewareFunc) {
+ for _, fn := range mwf {
+ r.middlewares = append(r.middlewares, fn)
+ }
}
// useInterface appends a middleware to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Router.
func (r *Router) useInterface(mw middleware) {
r.middlewares = append(r.middlewares, mw)
}
+
+// CORSMethodMiddleware sets the Access-Control-Allow-Methods response header
+// on a request, by matching routes based only on paths. It also handles
+// OPTIONS requests, by settings Access-Control-Allow-Methods, and then
+// returning without calling the next http handler.
+func CORSMethodMiddleware(r *Router) MiddlewareFunc {
+ return func(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
+ var allMethods []string
+
+ err := r.Walk(func(route *Route, _ *Router, _ []*Route) error {
+ for _, m := range route.matchers {
+ if _, ok := m.(*routeRegexp); ok {
+ if m.Match(req, &RouteMatch{}) {
+ methods, err := route.GetMethods()
+ if err != nil {
+ return err
+ }
+
+ allMethods = append(allMethods, methods...)
+ }
+ break
+ }
+ }
+ return nil
+ })
+
+ if err == nil {
+ w.Header().Set("Access-Control-Allow-Methods", strings.Join(append(allMethods, "OPTIONS"), ","))
+
+ if req.Method == "OPTIONS" {
+ return
+ }
+ }
+
+ next.ServeHTTP(w, req)
+ })
+ }
+}