Exemplo n.º 1
0
        public MiddlerMapBuilder On(string scheme, string hostname, string path, Action <IMiddlerMapActionsBuilder> actions, Action <MiddlerMapRuleEnhancer> options = null)
        {
            var rule = new MiddlerRule();

            rule.Scheme = scheme is null ? new List <string>() : new List <string>()
            {
                scheme
            };
            rule.Path     = path;
            rule.Hostname = hostname;

            var actionBuilder = new MiddlerMapActionsBuilder(ServiceProvider);

            actions.Invoke(actionBuilder);

            rule.Actions = actionBuilder.MiddlerActions;

            if (options != null)
            {
                var enhancer = new MiddlerMapRuleEnhancer(rule);
                options.Invoke(enhancer);
                rule = enhancer;
            }


            Map.AddRule(rule);
            return(this);
        }
Exemplo n.º 2
0
        public static MapItem FromRule(MiddlerRule rule)
        {
            var mi = new MapItem();

            mi.ItemType = MapItemType.Rule;
            mi.Rule     = rule;
            return(mi);
        }
Exemplo n.º 3
0
        private MiddlerRuleMatch CheckMatch(IMiddlerOptions middlerOptions, MiddlerRule rule, MiddlerContext middlerContext)
        {
            var allowedHttpMethods = (rule.HttpMethods?.IgnoreNullOrWhiteSpace().Any() == true ? rule.HttpMethods.IgnoreNullOrWhiteSpace() : middlerOptions.DefaultHttpMethods).ToList();

            if (allowedHttpMethods.Any() && !allowedHttpMethods.Contains(middlerContext.Request.HttpMethod, StringComparer.OrdinalIgnoreCase))
            {
                return(null);
            }

            //var uri = new Uri(context.Request.GetEncodedUrl());

            var allowedSchemes = (rule.Scheme?.IgnoreNullOrWhiteSpace().Any() == true ? rule.Scheme.IgnoreNullOrWhiteSpace() : middlerOptions.DefaultScheme).ToList();

            if (allowedSchemes.Any() && !allowedSchemes.Any(scheme => Wildcard.Match(middlerContext.MiddlerRequestContext.Uri.Scheme, scheme)))
            {
                return(null);
            }

            if (!Wildcard.Match($"{middlerContext.MiddlerRequestContext.Uri.Host}:{middlerContext.MiddlerRequestContext.Uri.Port}", rule.Hostname ?? "*"))
            {
                return(null);
            }

            if (String.IsNullOrWhiteSpace(rule.Path))
            {
                rule.Path = "{**path}";
            }

            var parsedTemplate = TemplateParser.Parse(rule.Path);

            var defaults = parsedTemplate.Parameters.Where(p => p.DefaultValue != null)
                           .Aggregate(new RouteValueDictionary(), (current, next) =>
            {
                current.Add(next.Name, next.DefaultValue);
                return(current);
            });

            var matcher = new TemplateMatcher(parsedTemplate, defaults);
            var rd      = middlerContext.MiddlerRequestContext.GetRouteData();
            var router  = rd.Routers.FirstOrDefault() ?? new RouteCollection();

            if (matcher.TryMatch(middlerContext.MiddlerRequestContext.Uri.AbsolutePath, rd.Values))
            {
                var constraints = GetConstraints(middlerContext.RequestServices.GetRequiredService <IInlineConstraintResolver>(), parsedTemplate, null);
                if (MiddlerRouteConstraintMatcher.Match(constraints, rd.Values, router, RouteDirection.IncomingRequest, ConstraintLogger))
                {
                    middlerContext.SetRouteData(constraints);

                    return(new MiddlerRuleMatch
                    {
                        MiddlerRule = rule,
                        AccessMode = rule.AccessAllowed(middlerContext.Request) ?? middlerOptions.DefaultAccessMode
                    });
                }
            }

            return(null);
        }
Exemplo n.º 4
0
        public static MiddlerRule ToMiddlerRule(this MiddlerRuleDbModel dbModel)
        {
            var rule = new MiddlerRule();

            rule.Scheme      = dbModel.Scheme;
            rule.Hostname    = dbModel.Hostname;
            rule.Path        = dbModel.Path;
            rule.HttpMethods = dbModel.HttpMethods;
            rule.Actions     = dbModel.Actions;

            return(rule);
        }
Exemplo n.º 5
0
        public static MiddlerRule ToMiddlerRule(this EndpointRuleEntity entity)
        {
            if (entity == null)
            {
                return(null);
            }

            var mRule = new MiddlerRule();

            mRule.Name        = entity.Name;
            mRule.Hostname    = entity.Hostname;
            mRule.Path        = entity.Path;
            mRule.Scheme      = MappingHelper.Split(entity.Scheme);
            mRule.HttpMethods = MappingHelper.Split(entity.HttpMethods);
            mRule.Actions     = entity.Actions.OrderBy(p => p.Order).Select(ToMiddlerAction).ToList();
            mRule.Permissions = entity.Permissions.OrderBy(p => p.Order).Select(ToMiddlerPermissionRule).ToList();

            return(mRule);
        }
Exemplo n.º 6
0
 internal MiddlerMapRuleEnhancer(MiddlerRule rule)
 {
     MiddlerRule = rule;
 }
Exemplo n.º 7
0
 public static AccessMode?AccessAllowed(this MiddlerRule middlerMiddler, IMiddlerRequestContext middlerRequestContext)
 {
     return(AccessAllowed(middlerMiddler.Permissions, middlerRequestContext));
 }