Ejemplo n.º 1
0
        public RoutingBenchmark()
        {
            var handler = new RouteHandler((next) => Task.FromResult <object>(null));

            var treeBuilder = new TreeRouteBuilder(
                NullLoggerFactory.Instance,
                new RoutePatternBinderFactory(UrlEncoder.Default, new DefaultObjectPoolProvider()),
                new DefaultInlineConstraintResolver(Options.Create(new RouteOptions())));

            treeBuilder.MapInbound(handler, TemplateParser.Parse("api/Widgets"), "default", 0);
            treeBuilder.MapInbound(handler, TemplateParser.Parse("api/Widgets/{id}"), "default", 0);
            treeBuilder.MapInbound(handler, TemplateParser.Parse("api/Widgets/search/{term}"), "default", 0);
            treeBuilder.MapInbound(handler, TemplateParser.Parse("admin/users/{id}"), "default", 0);
            treeBuilder.MapInbound(handler, TemplateParser.Parse("admin/users/{id}/manage"), "default", 0);

            _treeRouter = treeBuilder.Build();

            _requests = new RequestEntry[NumberOfRequestTypes];

            _requests[0].HttpContext = new DefaultHttpContext();
            _requests[0].HttpContext.Request.Path = "/api/Widgets/5";
            _requests[0].IsMatch = true;
            _requests[0].Values  = new RouteValueDictionary(new { id = 5 });

            _requests[1].HttpContext = new DefaultHttpContext();
            _requests[1].HttpContext.Request.Path = "/admin/users/17/mAnage";
            _requests[1].IsMatch = true;
            _requests[1].Values  = new RouteValueDictionary(new { id = 17 });

            _requests[2].HttpContext = new DefaultHttpContext();
            _requests[2].HttpContext.Request.Path = "/api/Widgets/search/dldldldldld/ddld";
            _requests[2].IsMatch = false;
            _requests[2].Values  = new RouteValueDictionary();
        }
Ejemplo n.º 2
0
        public RoutingBenchmark()
        {
            var handler = new RouteHandler((next) => Task.FromResult <object>(null));

            var treeBuilder = new TreeRouteBuilder(
                NullLoggerFactory.Instance,
                new DefaultObjectPool <UriBuildingContext>(new UriBuilderContextPooledObjectPolicy()),
                new DefaultInlineConstraintResolver(new OptionsManager <RouteOptions>(new OptionsFactory <RouteOptions>(Enumerable.Empty <IConfigureOptions <RouteOptions> >(), Enumerable.Empty <IPostConfigureOptions <RouteOptions> >()))));

            treeBuilder.MapInbound(handler, TemplateParser.Parse("api/Widgets"), "default", 0);
            treeBuilder.MapInbound(handler, TemplateParser.Parse("api/Widgets/{id}"), "default", 0);
            treeBuilder.MapInbound(handler, TemplateParser.Parse("api/Widgets/search/{term}"), "default", 0);
            treeBuilder.MapInbound(handler, TemplateParser.Parse("admin/users/{id}"), "default", 0);
            treeBuilder.MapInbound(handler, TemplateParser.Parse("admin/users/{id}/manage"), "default", 0);

            _treeRouter = treeBuilder.Build();

            _requests = new RequestEntry[NumberOfRequestTypes];

            _requests[0].HttpContext = new DefaultHttpContext();
            _requests[0].HttpContext.Request.Path = "/api/Widgets/5";
            _requests[0].IsMatch = true;
            _requests[0].Values  = new RouteValueDictionary(new { id = 5 });

            _requests[1].HttpContext = new DefaultHttpContext();
            _requests[1].HttpContext.Request.Path = "/admin/users/17/mAnage";
            _requests[1].IsMatch = true;
            _requests[1].Values  = new RouteValueDictionary(new { id = 17 });

            _requests[2].HttpContext = new DefaultHttpContext();
            _requests[2].HttpContext.Request.Path = "/api/Widgets/search/dldldldldld/ddld";
            _requests[2].IsMatch = false;
            _requests[2].Values  = new RouteValueDictionary();
        }
Ejemplo n.º 3
0
        private TreeRouter BuildRoute(ActionDescriptorCollection actions)
        {
            var routeBuilder = new TreeRouteBuilder(_target, _loggerFactory);
            var routeInfos   = GetRouteInfos(_constraintResolver, actions.Items);

            // We're creating one AttributeRouteGenerationEntry per action. This allows us to match the intended
            // action by expected route values, and then use the TemplateBinder to generate the link.
            foreach (var routeInfo in routeInfos)
            {
                routeBuilder.Add(new TreeRouteLinkGenerationEntry()
                {
                    Binder               = new TemplateBinder(_urlEncoder, _contextPool, routeInfo.ParsedTemplate, routeInfo.Defaults),
                    Defaults             = routeInfo.Defaults,
                    Constraints          = routeInfo.Constraints,
                    Order                = routeInfo.Order,
                    GenerationPrecedence = routeInfo.GenerationPrecedence,
                    RequiredLinkValues   = routeInfo.ActionDescriptor.RouteValueDefaults,
                    RouteGroup           = routeInfo.RouteGroup,
                    Template             = routeInfo.ParsedTemplate,
                    Name = routeInfo.Name,
                });
            }

            // We're creating one AttributeRouteMatchingEntry per group, so we need to identify the distinct set of
            // groups. It's guaranteed that all members of the group have the same template and precedence,
            // so we only need to hang on to a single instance of the RouteInfo for each group.
            var distinctRouteInfosByGroup = GroupRouteInfosByGroupId(routeInfos);

            foreach (var routeInfo in distinctRouteInfosByGroup)
            {
                routeBuilder.Add(new TreeRouteMatchingEntry()
                {
                    Order           = routeInfo.Order,
                    Precedence      = routeInfo.MatchPrecedence,
                    Target          = _target,
                    RouteName       = routeInfo.Name,
                    RouteTemplate   = TemplateParser.Parse(routeInfo.RouteTemplate),
                    TemplateMatcher = new TemplateMatcher(
                        routeInfo.ParsedTemplate,
                        new RouteValueDictionary(StringComparer.OrdinalIgnoreCase)
                    {
                        { TreeRouter.RouteGroupKey, routeInfo.RouteGroup }
                    }),
                    Constraints = routeInfo.Constraints
                });
            }

            return(routeBuilder.Build(actions.Version));
        }
Ejemplo n.º 4
0
        private TreeRouter BuildRoute(AttributeRouteEntries entries, int version)
        {
            var routeBuilder = new TreeRouteBuilder(_target, _loggerFactory);

            foreach (var entry in entries.LinkGenerationEntries)
            {
                routeBuilder.Add(entry);
            }

            foreach (var entry in entries.MatchingEntries)
            {
                routeBuilder.Add(entry);
            }

            return(routeBuilder.Build(version));
        }
Ejemplo n.º 5
0
        public override Matcher Build()
        {
#pragma warning disable CS0618 // Type or member is obsolete
            var builder = new TreeRouteBuilder(
                NullLoggerFactory.Instance,
                new DefaultObjectPool <UriBuildingContext>(new UriBuilderContextPooledObjectPolicy()),
                new DefaultInlineConstraintResolver(Options.Create(new RouteOptions()), new TestServiceProvider()));
#pragma warning restore CS0618 // Type or member is obsolete

            var selector = new DefaultEndpointSelector();

            var groups = _endpoints
                         .GroupBy(e => (e.Order, e.RoutePattern.InboundPrecedence, e.RoutePattern.RawText))
                         .OrderBy(g => g.Key.Order)
                         .ThenBy(g => g.Key.InboundPrecedence);

            var routes = new RouteCollection();

            foreach (var group in groups)
            {
                var candidates = group.ToArray();

                // RouteEndpoint.Values contains the default values parsed from the template
                // as well as those specified with a literal. We need to separate those
                // for legacy cases.
                var endpoint = group.First();
                var defaults = new RouteValueDictionary(endpoint.RoutePattern.Defaults);
                for (var i = 0; i < endpoint.RoutePattern.Parameters.Count; i++)
                {
                    var parameter = endpoint.RoutePattern.Parameters[i];
                    if (parameter.Default != null)
                    {
                        defaults.Remove(parameter.Name);
                    }
                }

                builder.MapInbound(
                    new SelectorRouter(selector, candidates),
                    new RouteTemplate(endpoint.RoutePattern),
                    routeName: null,
                    order: endpoint.Order);
            }

            return(new TreeRouterMatcher(builder.Build()));
        }
Ejemplo n.º 6
0
        internal override Matcher CreateMatcher(MatcherEndpoint endpoint)
        {
            var builder = new TreeRouteBuilder(
                NullLoggerFactory.Instance,
                new DefaultObjectPool <UriBuildingContext>(new UriBuilderContextPooledObjectPolicy()),
                new DefaultInlineConstraintResolver(Options.Create(new RouteOptions())));

            var handler = new RouteHandler(c =>
            {
                var feature      = c.Features.Get <IEndpointFeature>();
                feature.Endpoint = endpoint;
                feature.Invoker  = MatcherEndpoint.EmptyInvoker;

                return(Task.CompletedTask);
            });

            builder.MapInbound(handler, TemplateParser.Parse(endpoint.Template), "default", 0);

            return(new TreeRouterMatcher(builder.Build()));
        }
Ejemplo n.º 7
0
 public override Matcher Build()
 {
     return(new TreeRouterMatcher(_inner.Build()));
 }