public virtual bool Accept(EndpointConstraintContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (_httpMethods.Count == 0)
            {
                return(true);
            }

            var request = context.HttpContext.Request;
            var method  = request.Method;

            for (var i = 0; i < _httpMethods.Count; i++)
            {
                var supportedMethod = _httpMethods[i];
                if (string.Equals(supportedMethod, method, StringComparison.OrdinalIgnoreCase))
                {
                    return(true);
                }
            }

            return(false);
        }
 public bool Accept(EndpointConstraintContext context)
 {
     return(Pass);
 }
Exemplo n.º 3
0
        private IReadOnlyList <EndpointSelectorCandidate> EvaluateEndpointConstraintsCore(
            HttpContext context,
            IReadOnlyList <EndpointSelectorCandidate> candidates,
            int?startingOrder)
        {
            // Find the next group of constraints to process. This will be the lowest value of
            // order that is higher than startingOrder.
            int?order = null;

            // Perf: Avoid allocations
            for (var i = 0; i < candidates.Count; i++)
            {
                var candidate = candidates[i];
                if (candidate.Constraints != null)
                {
                    for (var j = 0; j < candidate.Constraints.Count; j++)
                    {
                        var constraint = candidate.Constraints[j];
                        if ((startingOrder == null || constraint.Order > startingOrder) &&
                            (order == null || constraint.Order < order))
                        {
                            order = constraint.Order;
                        }
                    }
                }
            }

            // If we don't find a next then there's nothing left to do.
            if (order == null)
            {
                return(candidates);
            }

            // Since we have a constraint to process, bisect the set of endpoints into those with and without a
            // constraint for the current order.
            var endpointsWithConstraint    = new List <EndpointSelectorCandidate>();
            var endpointsWithoutConstraint = new List <EndpointSelectorCandidate>();

            var constraintContext = new EndpointConstraintContext();

            constraintContext.Candidates  = candidates;
            constraintContext.HttpContext = context;

            // Perf: Avoid allocations
            for (var i = 0; i < candidates.Count; i++)
            {
                var candidate = candidates[i];
                var isMatch   = true;
                var foundMatchingConstraint = false;

                if (candidate.Constraints != null)
                {
                    constraintContext.CurrentCandidate = candidate;
                    for (var j = 0; j < candidate.Constraints.Count; j++)
                    {
                        var constraint = candidate.Constraints[j];
                        if (constraint.Order == order)
                        {
                            foundMatchingConstraint = true;

                            if (!constraint.Accept(constraintContext))
                            {
                                isMatch = false;
                                //_logger.ConstraintMismatch(
                                //    candidate.Endpoint.DisplayName,
                                //    candidate.Endpoint.Id,
                                //    constraint);
                                break;
                            }
                        }
                    }
                }

                if (isMatch && foundMatchingConstraint)
                {
                    endpointsWithConstraint.Add(candidate);
                }
                else if (isMatch)
                {
                    endpointsWithoutConstraint.Add(candidate);
                }
            }

            // If we have matches with constraints, those are better so try to keep processing those
            if (endpointsWithConstraint.Count > 0)
            {
                var matches = EvaluateEndpointConstraintsCore(context, endpointsWithConstraint, order);
                if (matches?.Count > 0)
                {
                    return(matches);
                }
            }

            // If the set of matches with constraints can't work, then process the set without constraints.
            if (endpointsWithoutConstraint.Count == 0)
            {
                return(null);
            }
            else
            {
                return(EvaluateEndpointConstraintsCore(context, endpointsWithoutConstraint, order));
            }
        }