//FlatChainWrap provides a chain wrap for http.Handler with an optional log argument
778274
func FlatChainWrap(h http.HandlerFunc, lg *log.Logger) FlatChains {
	return NewFlatChain(func(c *Context, nx NextHandler) {
		h(c.Res, c.Req)
		nx(c)
	}, lg)
}
113172
func SetWrapHandler(wh func(path string, handler http.Handler, err error) (string, http.Handler, error)) {
	wrapHandler = wh
}
410437
func Wrap(ctx context.Context, constructor alice.Constructor) alice.Constructor {
	return func(next http.Handler) (http.Handler, error) {
		if constructor == nil {
			return nil, nil
		}
		handler, err := constructor(next)
		if err != nil {
			return nil, err
		}

		if tracableHandler, ok := handler.(Tracable); ok {
			name, spanKind := tracableHandler.GetTracingInformation()
			log.FromContext(ctx).WithField(log.MiddlewareName, name).Debug("Adding tracing to middleware")
			return NewWrapper(handler, name, spanKind), nil
		}
		return handler, nil
	}
}
645907
func (retry *RetryRequest) Wrap(innerconnection plugin.Connection) plugin.Connection {
	retry.connection = innerconnection
	return retry
}
190417
func (r *ChainRouter) Handle(w http.ResponseWriter, rq *http.Request, _ Collector) {
	r.ServeHTTP(w, rq)
}
494166
func Unwrap(err error) error {
	for {
		switch specific := err.(type) {
		case *HereError:
			err = specific.error
		case *CauseError:
			err = specific.error
		case *TraceError:
			err = specific.error
		case *ContextError:
			err = specific.error
		case *SubjectError:
			err = specific.error
		default:
			return err
		}
	}
}
157085
func (b *SystemBackend) handleWrappingUnwrap(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
	// If a third party is unwrapping (rather than the calling token being the
	// wrapping token) we detect this so that we can revoke the original
	// wrapping token after reading it
	var thirdParty bool

	token := data.Get("token").(string)
	if token != "" {
		thirdParty = true
	} else {
		token = req.ClientToken
	}

	// Get the policies so we can determine if this is a normal response
	// wrapping request or a control group token.
	//
	// We use lookupTainted here because the token might have already been used
	// by handleRequest(), this happens when it's a normal response wrapping
	// request and the token was provided "first party". We want to inspect the
	// token policies but will not use this token entry for anything else.
	te, err := b.Core.tokenStore.lookupTainted(ctx, token)
	if err != nil {
		return nil, err
	}
	if te == nil {
		return nil, nil
	}
	if len(te.Policies) != 1 {
		return nil, errors.New("token is not a valid unwrap token")
	}

	unwrapNS, err := NamespaceByID(ctx, te.NamespaceID, b.Core)
	if err != nil {
		return nil, err
	}
	unwrapCtx := namespace.ContextWithNamespace(ctx, unwrapNS)

	var response string
	switch te.Policies[0] {
	case controlGroupPolicyName:
		response, err = controlGroupUnwrap(unwrapCtx, b, token, thirdParty)
	case responseWrappingPolicyName:
		response, err = b.responseWrappingUnwrap(unwrapCtx, te, thirdParty)
	}
	if err != nil {
		var respErr *logical.Response
		if len(response) > 0 {
			respErr = logical.ErrorResponse(response)
		}

		return respErr, err
	}

	resp := &logical.Response{
		Data: map[string]interface{}{},
	}

	// Most of the time we want to just send over the marshalled HTTP bytes.
	// However there is a sad separate case: if the original response was using
	// bare values we need to use those or else what comes back is garbled.
	httpResp := &logical.HTTPResponse{}
	err = jsonutil.DecodeJSON([]byte(response), httpResp)
	if err != nil {
		return nil, errwrap.Wrapf("error decoding wrapped response: {{err}}", err)
	}
	if httpResp.Data != nil &&
		(httpResp.Data[logical.HTTPStatusCode] != nil ||
			httpResp.Data[logical.HTTPRawBody] != nil ||
			httpResp.Data[logical.HTTPContentType] != nil) {
		if httpResp.Data[logical.HTTPStatusCode] != nil {
			resp.Data[logical.HTTPStatusCode] = httpResp.Data[logical.HTTPStatusCode]
		}
		if httpResp.Data[logical.HTTPContentType] != nil {
			resp.Data[logical.HTTPContentType] = httpResp.Data[logical.HTTPContentType]
		}

		rawBody := httpResp.Data[logical.HTTPRawBody]
		if rawBody != nil {
			// Decode here so that we can audit properly
			switch rawBody.(type) {
			case string:
				// Best effort decoding; if this works, the original value was
				// probably a []byte instead of a string, but was marshaled
				// when the value was saved, so this restores it as it was
				decBytes, err := base64.StdEncoding.DecodeString(rawBody.(string))
				if err == nil {
					// We end up with []byte, will not be HMAC'd
					resp.Data[logical.HTTPRawBody] = decBytes
				} else {
					// We end up with string, will be HMAC'd
					resp.Data[logical.HTTPRawBody] = rawBody
				}
			default:
				b.Core.Logger().Error("unexpected type of raw body when decoding wrapped token", "type", fmt.Sprintf("%T", rawBody))
			}

			resp.Data[logical.HTTPRawBodyAlreadyJSONDecoded] = true
		}

		return resp, nil
	}

	if len(response) == 0 {
		resp.Data[logical.HTTPStatusCode] = 204
	} else {
		resp.Data[logical.HTTPStatusCode] = 200
		resp.Data[logical.HTTPRawBody] = []byte(response)
		resp.Data[logical.HTTPContentType] = "application/json"
	}

	return resp, nil
}
502653
func WrapHandler(h http.Handler) HandlerFunc {
	return func(c Context) error {
		h.ServeHTTP(c.Response(), c.Request())
		return nil
	}
}
95554
func (retry *RetryRequest) Wrap(innerconnection cloudcontroller.Connection) cloudcontroller.Connection {
	retry.connection = innerconnection
	return retry
}
378843
func (m *Module) WrappedHandle(method, path string, h Handle) {
	m.HTTPRouter.Handle(method, path, m.wrap(h))
}
1272644
func (r *FlatChain) ChainHandleFunc(h http.HandlerFunc) FlatChains {
	fh := FlatChainWrap(h, r.log)
	r.Chain(fh)
	return fh
}
295400
func FlatPass(methods, pattern string, lg *log.Logger) FlatChains {
	return FlatRoute(methods, pattern, func(c *Context, n NextHandler) { n(c) }, lg)
}
679653
func Redirect(path string) FlatChains {
	return NewFlatChain(func(c *Context, nx NextHandler) {
		http.Redirect(c.Res, c.Req, path, http.StatusTemporaryRedirect)
		nx(c)
	}, nil)
}
320393
func Unwrap(err error) error {
	for {
		wrap, ok := err.(Wrapped)
		if !ok {
			return err
		}

		inner := wrap.InnerError()
		if inner == nil {
			break
		}
		err = inner
	}
	return err
}
580862
func (r *FlatChain) Chain(rx FlatChains) FlatChains {
	if r.next == nil {
		r.next = rx
	} else {
		r.next.Chain(rx)
	}
	return r
}
313634
func NextWrap(wrapping NextSink) MiddlewareConstructor {
	return func(sendTo Sink) Sink {
		x := &nextWrapped{
			forwardTo: sendTo,
			wrapping:  wrapping,
		}
		return x
	}
}
932677
func (r *Router) chain() http.Handler {
	var final http.Handler

	final = r.router
	mw := r.allMiddleware()
	for i := len(mw) - 1; i >= 0; i-- {
		final = mw[i](final)
	}

	return final
}