Exemple #1
0
    /// <inheritdoc />
    public bool Match(
        HttpContext?httpContext,
        IRouter?route,
        string routeKey,
        RouteValueDictionary values,
        RouteDirection routeDirection)
    {
        if (routeKey == null)
        {
            throw new ArgumentNullException(nameof(routeKey));
        }

        if (values == null)
        {
            throw new ArgumentNullException(nameof(values));
        }

        if (values.TryGetValue(routeKey, out var obj) && obj != null)
        {
            var value = Convert.ToString(obj, CultureInfo.InvariantCulture);
            return(!FileNameRouteConstraint.IsFileName(value));
        }

        // No value or null value.
        //
        // We want to return true here because the core use-case of the constraint is to *exclude*
        // things that look like file names. There's nothing here that looks like a file name, so
        // let it through.
        return(true);
    }
        /// <inheritdoc />
        public bool Match(
            HttpContext?httpContext,
            IRouter?route,
            string routeKey,
            RouteValueDictionary values,
            RouteDirection routeDirection)
        {
            if (routeKey == null)
            {
                throw new ArgumentNullException(nameof(routeKey));
            }

            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (values.TryGetValue(routeKey, out var value) && value != null)
            {
                if (value is double)
                {
                    return(true);
                }

                var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
                return(double.TryParse(
                           valueString,
                           NumberStyles.Float | NumberStyles.AllowThousands,
                           CultureInfo.InvariantCulture,
                           out _));
            }

            return(false);
        }
Exemple #3
0
        /// <inheritdoc />
        public bool Match(
            HttpContext?httpContext,
            IRouter?route,
            string routeKey,
            RouteValueDictionary values,
            RouteDirection routeDirection)
        {
            if (routeKey == null)
            {
                throw new ArgumentNullException(nameof(routeKey));
            }

            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            foreach (var constraint in Constraints)
            {
                if (!constraint.Match(httpContext, route, routeKey, values, routeDirection))
                {
                    return(false);
                }
            }

            return(true);
        }
        /// <inheritdoc />
        public bool Match(
            HttpContext?httpContext,
            IRouter?route,
            string routeKey,
            RouteValueDictionary values,
            RouteDirection routeDirection)
        {
            if (routeKey == null)
            {
                throw new ArgumentNullException(nameof(routeKey));
            }

            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (values.TryGetValue(routeKey, out var value) && value != null)
            {
                var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
                if (long.TryParse(valueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out var longValue))
                {
                    return(longValue >= Min && longValue <= Max);
                }
            }

            return(false);
        }
        /// <inheritdoc />
        public bool Match(
            HttpContext?httpContext,
            IRouter?route,
            string routeKey,
            RouteValueDictionary values,
            RouteDirection routeDirection)
        {
            if (routeKey == null)
            {
                throw new ArgumentNullException(nameof(routeKey));
            }

            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (values.TryGetValue(routeKey, out var value) && value != null)
            {
                // In routing the empty string is equivalent to null, which is equivalent to an unset value.
                var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
                return(!string.IsNullOrEmpty(valueString));
            }

            return(false);
        }
    /// <inheritdoc />
    public bool Match(
        HttpContext?httpContext,
        IRouter?route,
        string routeKey,
        RouteValueDictionary values,
        RouteDirection routeDirection)
    {
        if (routeKey == null)
        {
            throw new ArgumentNullException(nameof(routeKey));
        }

        if (values == null)
        {
            throw new ArgumentNullException(nameof(values));
        }

        if (values.TryGetValue(routeKey, out var value) && value != null)
        {
            if (value is long)
            {
                return(true);
            }

            var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
            return(CheckConstraintCore(valueString));
        }

        return(false);
    }
Exemple #7
0
        /// <inheritdoc />
        public bool Match(
            HttpContext?httpContext,
            IRouter?route,
            string routeKey,
            RouteValueDictionary values,
            RouteDirection routeDirection)
        {
            if (routeKey == null)
            {
                throw new ArgumentNullException(nameof(routeKey));
            }

            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (values.TryGetValue(routeKey, out var value) && value != null)
            {
                var valueString = Convert.ToString(value, CultureInfo.InvariantCulture) !;
                return(valueString.Length >= MinLength);
            }

            return(false);
        }
Exemple #8
0
    /// <inheritdoc />
    public bool Match(
        HttpContext?httpContext,
        IRouter?route,
        string routeKey,
        RouteValueDictionary values,
        RouteDirection routeDirection)
    {
        if (routeKey == null)
        {
            throw new ArgumentNullException(nameof(routeKey));
        }

        if (values == null)
        {
            throw new ArgumentNullException(nameof(values));
        }

        if (values.TryGetValue(routeKey, out var obj) && obj != null)
        {
            var value = Convert.ToString(obj, CultureInfo.InvariantCulture);
            return(IsFileName(value));
        }

        // No value or null value.
        return(false);
    }
Exemple #9
0
        /// <inheritdoc />
        public bool Match(
            HttpContext?httpContext,
            IRouter?route,
            string routeKey,
            RouteValueDictionary values,
            RouteDirection routeDirection)
        {
            if (routeKey == null)
            {
                throw new ArgumentNullException(nameof(routeKey));
            }

            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (values.TryGetValue(routeKey, out var value))
            {
                return(InnerConstraint.Match(httpContext,
                                             route,
                                             routeKey,
                                             values,
                                             routeDirection));
            }

            return(true);
        }
    public bool Match(HttpContext?httpContext, IRouter?route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
    {
        if (!values.ContainsKey(routeKey))
        {
            return(false);
        }

        return(GetCachedData().Any(m => m.MunicipalityId == (string?)values[routeKey]));
    }
 public bool Match(
     HttpContext?httpContext,
     IRouter?route,
     string routeKey,
     RouteValueDictionary values,
     RouteDirection routeDirection)
 {
     return(true);
 }
Exemple #12
0
            public bool Match(HttpContext?httpContext, IRouter?route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
            {
                var url = (string?)values[routeKey];

                if (url != null && url.StartsWith("api/"))
                {
                    return(false);
                }

                return(true);
            }
        /// <inheritdoc />
        public virtual bool Match(
            HttpContext?httpContext,
            IRouter?route,
            string routeKey,
            RouteValueDictionary values,
            RouteDirection routeDirection)
        {
            if (routeKey == null)
            {
                throw new ArgumentNullException(nameof(routeKey));
            }

            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            switch (routeDirection)
            {
            case RouteDirection.IncomingRequest:
                // Only required for constraining incoming requests
                if (httpContext == null)
                {
                    throw new ArgumentNullException(nameof(httpContext));
                }

                return(AllowedMethods.Contains(httpContext.Request.Method, StringComparer.OrdinalIgnoreCase));

            case RouteDirection.UrlGeneration:
                // We need to see if the user specified the HTTP method explicitly.  Consider these two routes:
                //
                // a) Route: template = "/{foo}", Constraints = { httpMethod = new HttpMethodRouteConstraint("GET") }
                // b) Route: template = "/{foo}", Constraints = { httpMethod = new HttpMethodRouteConstraint("POST") }
                //
                // A user might know ahead of time that a URI he/she is generating might be used with a particular HTTP
                // method.  If a URI will be used for an HTTP POST but we match on (a) while generating the URI, then
                // the HTTP GET-specific route will be used for URI generation, which might have undesired behavior.
                //
                // To prevent this, a user might call GetVirtualPath(..., { httpMethod = "POST" }) to
                // signal that they are generating a URI that will be used for an HTTP POST, so they want the URI
                // generation to be performed by the (b) route instead of the (a) route, consistent with what would
                // happen on incoming requests.
                if (!values.TryGetValue(routeKey, out var obj))
                {
                    return(true);
                }

                return(AllowedMethods.Contains(Convert.ToString(obj), StringComparer.OrdinalIgnoreCase));

            default:
                throw new ArgumentOutOfRangeException(nameof(routeDirection));
            }
        }
Exemple #14
0
    public RouteDataSnapshot PushState(IRouter?router, RouteValueDictionary?values, RouteValueDictionary?dataTokens)
    {
        // Perf: this is optimized for small list sizes, in particular to avoid overhead of a native call in
        // Array.CopyTo inside the List(IEnumerable<T>) constructor.
        List <IRouter>?routers = null;
        var            count   = _routers?.Count;

        if (count > 0)
        {
            Debug.Assert(_routers != null);

            routers = new List <IRouter>(count.Value);
            for (var i = 0; i < count.Value; i++)
            {
                routers.Add(_routers[i]);
            }
        }

        var snapshot = new RouteDataSnapshot(
            this,
            _dataTokens?.Count > 0 ? new RouteValueDictionary(_dataTokens) : null,
            routers,
            _values?.Count > 0 ? new RouteValueDictionary(_values) : null);

        if (router != null)
        {
            Routers.Add(router);
        }

        if (values != null)
        {
            foreach (var kvp in values)
            {
                if (kvp.Value != null)
                {
                    Values[kvp.Key] = kvp.Value;
                }
            }
        }

        if (dataTokens != null)
        {
            foreach (var kvp in dataTokens)
            {
                DataTokens[kvp.Key] = kvp.Value;
            }
        }

        return(snapshot);
    }
Exemple #15
0
        public bool Match(HttpContext?httpContext, IRouter?route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (!values.TryGetValue(routeKey, out var routeValue))
            {
                return(false);
            }

            if (routeValue is string @string)
            {
                // Do not allow whitespaces only and leading and trailing whitespaces.
                return(!string.IsNullOrWhiteSpace(@string) && @string.AsSpan().Trim().Length == @string.Length);
            }

            return(routeValue != null);
        }
Exemple #16
0
    public RouteBuilder(IApplicationBuilder applicationBuilder, IRouter?defaultHandler)
    {
        if (applicationBuilder == null)
        {
            throw new ArgumentNullException(nameof(applicationBuilder));
        }

        if (applicationBuilder.ApplicationServices.GetService(typeof(RoutingMarkerService)) == null)
        {
            throw new InvalidOperationException(Resources.FormatUnableToFindServices(
                                                    nameof(IServiceCollection),
                                                    nameof(RoutingServiceCollectionExtensions.AddRouting),
                                                    "ConfigureServices(...)"));
        }

        ApplicationBuilder = applicationBuilder;
        DefaultHandler     = defaultHandler;
        ServiceProvider    = applicationBuilder.ApplicationServices;

        Routes = new List <IRouter>();
    }
Exemple #17
0
        /// <summary>
        /// Determines whether the route constraint matches the specified criteria.
        /// </summary>
        /// <param name="httpContext">The current <see cref="HttpContext">HTTP context</see>.</param>
        /// <param name="route">The current <see cref="IRouter">route</see>.</param>
        /// <param name="routeKey">The key of the route parameter to match.</param>
        /// <param name="values">The current <see cref="RouteValueDictionary">collection</see> of route values.</param>
        /// <param name="routeDirection">The <see cref="RouteDirection">route direction</see> to match.</param>
        /// <returns>True if the route constraint is matched; otherwise, false.</returns>
        public bool Match(HttpContext?httpContext, IRouter?route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (IsNullOrEmpty(routeKey))
            {
                return(false);
            }

            if (!values.TryGetValue(routeKey, out string value))
            {
                return(false);
            }

            if (routeDirection == UrlGeneration)
            {
                return(!IsNullOrEmpty(value));
            }

            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            var feature = httpContext.ApiVersioningFeature();

            feature.RouteParameter         = routeKey;
            feature.RawRequestedApiVersion = value;

            if (TryParse(value, out var requestedVersion))
            {
                feature.RequestedApiVersion = requestedVersion;
                return(true);
            }

            return(false);
        }
Exemple #18
0
        /// <inheritdoc />
        public bool Match(HttpContext?httpContext, IRouter?route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (routeKey == null)
            {
                throw new ArgumentNullException(nameof(routeKey));
            }

            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (values.TryGetValue(routeKey, out var routeValue) &&
                routeValue != null)
            {
                var parameterValueString = Convert.ToString(routeValue, CultureInfo.InvariantCulture) !;

                return(parameterValueString.Equals(_value, StringComparison.OrdinalIgnoreCase));
            }

            return(false);
        }
            public bool Match(HttpContext?httpContext, IRouter?route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
            {
                if (httpContext == null)
                {
                    return(false);
                }

                // Constraint needs to be valid when a CORS preflight request is received so that CORS middleware will run
                if (GrpcProtocolHelpers.IsCorsPreflightRequest(httpContext))
                {
                    return(true);
                }

                if (!HttpMethods.IsPost(httpContext.Request.Method))
                {
                    return(false);
                }

                return(CommonGrpcProtocolHelpers.IsContentType(GrpcProtocolConstants.GrpcContentType, httpContext.Request.ContentType) ||
                       CommonGrpcProtocolHelpers.IsContentType(GrpcProtocolConstants.GrpcWebContentType, httpContext.Request.ContentType) ||
                       CommonGrpcProtocolHelpers.IsContentType(GrpcProtocolConstants.GrpcWebTextContentType, httpContext.Request.ContentType));
            }
Exemple #20
0
        public bool Match(HttpContext?httpContext, IRouter?route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (httpContext == null)
            {
                return(false);
            }

            if (routeDirection != RouteDirection.IncomingRequest)
            {
                return(true);
            }

            var value = values[routeKey] as string;

            if (string.IsNullOrWhiteSpace(value))
            {
                return(false);
            }

            var lookupService = httpContext.RequestServices.GetRequiredService <ILookupService>();
            var pathSegments  = value.Split("/", StringSplitOptions.RemoveEmptyEntries);

            foreach (var pathSegment in pathSegments)
            {
                if (lookupService.CountryExists(pathSegment))
                {
                    continue;
                }

                if (lookupService.CategoryExists(pathSegment))
                {
                    continue;
                }

                return(false);
            }

            return(true);
        }
Exemple #21
0
        /// <summary>
        /// Determines whether the URL parameter contains a valid value for this constraint.
        /// </summary>
        /// <param name="httpContext">An object that encapsulates information about the HTTP request.</param>
        /// <param name="route">The router that this constraint belongs to.</param>
        /// <param name="routeKey">The name of the parameter that is being checked.</param>
        /// <param name="values">A dictionary that contains the parameters for the URL.</param>
        /// <param name="routeDirection">
        /// An object that indicates whether the constraint check is being performed
        /// when an incoming request is being handled or when a URL is being generated.
        /// </param>
        /// <returns><c>true</c> if the URL parameter contains a valid value; otherwise, <c>false</c>.</returns>
        public bool Match(HttpContext?httpContext, IRouter?route, string routeKey, RouteValueDictionary values,
                          RouteDirection routeDirection)
        {
            if (httpContext != null)
            {
                if (values.TryGetValue(routeKey, out object value))
                {
                    var parameterValueString = Convert.ToString(value,
                                                                CultureInfo.InvariantCulture);
                    if (parameterValueString == null)
                    {
                        return(false);
                    }

                    var secrepo       = httpContext.RequestServices.GetService <ISecurityRepository>();
                    var scopeProvider = httpContext.RequestServices.GetService <IPermissionScope>();
                    var retVal        = secrepo.PermissionScopeExists(parameterValueString);
                    return(retVal);
                }
            }

            return(false);
        }
        public bool Match(
            HttpContext?httpContext,
            IRouter?route,
            string routeKey,
            RouteValueDictionary values,
            RouteDirection routeDirection)
        {
            if (routeKey == null)
            {
                throw new ArgumentNullException(nameof(routeKey));
            }

            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (values.TryGetValue(routeKey, out var obj))
            {
                var value = Convert.ToString(obj, CultureInfo.InvariantCulture);
                if (value != null)
                {
                    var actionDescriptors = GetAndValidateActionDescriptors(httpContext);

                    var allValues = GetAndCacheAllMatchingValues(routeKey, actionDescriptors);
                    foreach (var existingValue in allValues)
                    {
                        if (string.Equals(value, existingValue, StringComparison.OrdinalIgnoreCase))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Exemple #23
0
        public bool Match(HttpContext?httpContext, IRouter?route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
        {
            var val = values[routeKey]?.ToString();

            return(!int.TryParse(val, out _));
        }