// RecoverMiddleware is the built-in middleware for recovering from errors.
553540
func RecoverMiddleware(next HandlerFunc) HandlerFunc {
	fn := func(ctx *Context) {
		defer func() {
			if err := recover(); err != nil {
				e := NewError(err)
				httpRequest, _ := httputil.DumpRequest(ctx.Request, true)
				log.Printf("[Recovery] panic recovered:\n%s\n%s\n%s", string(httpRequest), err, e.StackTraceString())
				ctx.statusCode = 500
				ctx.Abort(500, map[string]interface{}{
					"Code":        ctx.statusCode,
					"Title":       "Internal Server Error",
					"HTTPRequest": string(httpRequest),
					"Message":     e.Error(),
					"StackTrace":  e.Stack,
				})
			}
		}()
		next(ctx)
	}
	return fn
}
1309122
func (s *Server) authorizationMiddleware(handler httputils.APIFunc) httputils.APIFunc {
	return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
		// FIXME: fill when authN gets in
		// User and UserAuthNMethod are taken from AuthN plugins
		// Currently tracked in https://github.com/docker/docker/pull/13994
		user := ""
		userAuthNMethod := ""
		authCtx := authorization.NewCtx(s.authZPlugins, user, userAuthNMethod, r.Method, r.RequestURI)

		if err := authCtx.AuthZRequest(w, r); err != nil {
			glog.Errorf("AuthZRequest for %s %s returned error: %s", r.Method, r.RequestURI, err)
			return err
		}

		rw := authorization.NewResponseModifier(w)

		if err := handler(ctx, rw, r, vars); err != nil {
			glog.Errorf("Handler for %s %s returned error: %s", r.Method, r.RequestURI, err)
			return err
		}

		if err := authCtx.AuthZResponse(rw, r); err != nil {
			glog.Errorf("AuthZResponse for %s %s returned error: %s", r.Method, r.RequestURI, err)
			return err
		}
		return nil
	}
}
217366
func NewMiddleware() eh.CommandHandlerMiddleware {
	return eh.CommandHandlerMiddleware(func(h eh.CommandHandler) eh.CommandHandler {
		return eh.CommandHandlerFunc(func(ctx context.Context, cmd eh.Command) error {
			// Call the validation method if it exists
			if c, ok := cmd.(Command); ok {
				err := c.Validate()
				if err != nil {
					return err
				}
			}

			// Immediate command execution.
			return h.HandleCommand(ctx, cmd)
		})
	})
}
898627
func (e *Environment) InstallHandlersWithMiddleware(r *router.Router, base router.MiddlewareChain) {
	warmup.InstallHandlers(r, base)

	if e.ExtraHandlers != nil {
		e.ExtraHandlers(r, base)
	}
}
985271
func NewMiddleware(cfg Config) *Middleware {
	m := &Middleware{newGraph(cfg.Scope, cfg.Logger, cfg.ContextExtractor)}

	if lvl := cfg.SuccessLevel; lvl != nil {
		m.graph.succLevel = *lvl
	}
	if lvl := cfg.FailureLevel; lvl != nil {
		m.graph.failLevel = *lvl
	}
	if lvl := cfg.ApplicationErrorLevel; lvl != nil {
		m.graph.appErrLevel = *lvl
	}

	return m
}
1098896
func Middleware() middleware.Middleware {
	return func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			nw := &responseWriter{
				ResponseWriter: w,
				t:              time.Now(),
			}
			h.ServeHTTP(nw, r)
		})
	}
}
853221
func NewMiddleware() (eh.CommandHandlerMiddleware, chan Error) {
	errCh := make(chan Error, 20)
	return eh.CommandHandlerMiddleware(func(h eh.CommandHandler) eh.CommandHandler {
		return eh.CommandHandlerFunc(func(ctx context.Context, cmd eh.Command) error {
			// Delayed command execution if there is time set.
			if c, ok := cmd.(Command); ok && !c.ExecuteAt().IsZero() {
				go func() {
					t := time.NewTimer(c.ExecuteAt().Sub(time.Now()))
					defer t.Stop()

					var err error
					select {
					case <-ctx.Done():
						err = ctx.Err()
					case <-t.C:
						err = h.HandleCommand(ctx, cmd)
					}

					if err != nil {
						// Always try to deliver errors.
						errCh <- Error{err, ctx, cmd}
					}
				}()
				return nil
			}

			// Immediate command execution.
			return h.HandleCommand(ctx, cmd)
		})
	}), errCh
}
727380
func httpRequestMiddleware(event *Event, config *Configuration) error {
	for _, datum := range event.RawData {
		if request, ok := datum.(*http.Request); ok {
			event.MetaData.Update(MetaData{
				"request": {
					"params": request.URL.Query(),
				},
			})
		}
	}
	return nil
}
847292
func NewMiddleware(limiter *limiter.Limiter, options ...Option) gin.HandlerFunc {
	middleware := &Middleware{
		Limiter:        limiter,
		OnError:        DefaultErrorHandler,
		OnLimitReached: DefaultLimitReachedHandler,
		KeyGetter:      DefaultKeyGetter,
	}

	for _, option := range options {
		option.apply(middleware)
	}

	return func(ctx *gin.Context) {
		middleware.Handle(ctx)
	}
}
961205
func applyRegistryMiddleware(ctx context.Context, registry distribution.Namespace, middlewares []configuration.Middleware) (distribution.Namespace, error) {
	for _, mw := range middlewares {
		rmw, err := registrymiddleware.Get(ctx, mw.Name, mw.Options, registry)
		if err != nil {
			return nil, fmt.Errorf("unable to configure registry middleware (%s): %s", mw.Name, err)
		}
		registry = rmw
	}
	return registry, nil

}
936242
func RunMiddleware(c *Context, mc MiddlewareChain, h Handler) {
	runChains(c, mc, MiddlewareChain{}, h)
}
933134
func (r *Route) Middleware(m Middleware) *Route {
	r.middleware = append(r.middleware, &middlewareForVerb{
		mid:  m,
		verb: flagAny,
	})
	return r
}
880227
func WrapMiddleware(m interface{}) Middleware {
	if h, ok := m.(MiddlewareFunc); ok {
		return h
	}
	if h, ok := m.(MiddlewareFuncd); ok {
		return h
	}
	if h, ok := m.(Middleware); ok {
		return h
	}
	if h, ok := m.(HandlerFunc); ok {
		return WrapMiddlewareFromHandler(h)
	}
	if h, ok := m.(func(Context) error); ok {
		return WrapMiddlewareFromHandler(HandlerFunc(h))
	}
	if h, ok := m.(func(Handler) func(Context) error); ok {
		return MiddlewareFunc(func(next Handler) Handler {
			return HandlerFunc(h(next))
		})
	}
	if h, ok := m.(func(Handler) HandlerFunc); ok {
		return MiddlewareFunc(func(next Handler) Handler {
			return h(next)
		})
	}
	if h, ok := m.(func(HandlerFunc) HandlerFunc); ok {
		return MiddlewareFunc(func(next Handler) Handler {
			return h(next.Handle)
		})
	}
	if h, ok := m.(func(Handler) Handler); ok {
		return MiddlewareFunc(h)
	}
	if h, ok := m.(func(func(Context) error) func(Context) error); ok {
		return MiddlewareFunc(func(next Handler) Handler {
			return HandlerFunc(h(next.Handle))
		})
	}
	if v, ok := m.(http.Handler); ok {
		return WrapMiddlewareFromStdHandler(v)
	}
	if v, ok := m.(func(http.ResponseWriter, *http.Request)); ok {
		return WrapMiddlewareFromStdHandleFunc(v)
	}
	if v, ok := m.(func(http.ResponseWriter, *http.Request) error); ok {
		return WrapMiddlewareFromStdHandleFuncd(v)
	}
	panic(`unknown middleware`)
}
692703
func (n NextHTTP) CreateMiddleware(next ContextHandler) ContextHandler {
	return HandlerFunc(func(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
		n(rw, r, http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
			next.ServeHTTPC(ctx, rw, r)
		}))
	})
}
1164187
func (m *HeadersInRequest) CreateMiddleware(next ContextHandler) ContextHandler {
	return HandlerFunc(func(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
		m.ServeHTTPC(ctx, rw, r, next)
	})
}
1372232
func setupMiddlewares(handler http.Handler) http.Handler {
	ourFunc := func(w http.ResponseWriter, r *http.Request) {
		rctx := context.WithValue(r.Context(), ctxResponseWriter, w)
		handler.ServeHTTP(w, r.WithContext(rctx))
	}
	return http.HandlerFunc(ourFunc)

}
818691
func (mw *GinJWTMiddleware) MiddlewareFunc() gin.HandlerFunc {
	return func(c *gin.Context) {
		mw.middlewareImpl(c)
	}
}