Beispiel #1
0
        public static void DeleteRule(InboundRule rule, InboundRulesSection section)
        {
            if (rule == null)
            {
                return;
            }

            InboundRuleCollection collection = section.InboundRules;

            // To utilize the remove functionality we must pull the element directly from the collection
            rule = (InboundRule)collection.FirstOrDefault(r => r.Name.Equals(rule.Name));

            if (rule != null)
            {
                try {
                    collection.Remove(rule);
                }
                catch (FileLoadException e) {
                    throw new LockedException(section.SectionPath, e);
                }
                catch (DirectoryNotFoundException e) {
                    throw new ConfigScopeNotFoundException(e);
                }
            }
        }
Beispiel #2
0
        public static void UpdateRule(dynamic model, InboundRule rule, Site site, string path, string configPath = null)
        {
            InboundRulesSection           section = GetSection(site, path, configPath);
            AllowedServerVariablesSection serverVariablesSection = ServerVariablesHelper.GetSection(site, path, configPath);

            SetRule(model, rule, section, serverVariablesSection);
        }
Beispiel #3
0
        public static void AddRule(InboundRule rule, InboundRulesSection section, dynamic model)
        {
            if (rule == null)
            {
                throw new ArgumentNullException(nameof(rule));
            }

            if (rule.Name == null)
            {
                throw new ArgumentNullException("rule.Name");
            }

            InboundRuleCollection collection = section.InboundRules;

            if (collection.Any(r => r.Name.Equals(rule.Name)))
            {
                throw new AlreadyExistsException("rule");
            }

            try {
                collection.Add(rule);

                UpdatePriority(model, rule, section);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
        public object Patch([FromBody] dynamic model, string id)
        {
            var inboundRuleId = new InboundRuleId(id);

            Site site = inboundRuleId.SiteId == null ? null : SiteHelper.GetSite(inboundRuleId.SiteId.Value);

            if (inboundRuleId.SiteId != null && site == null)
            {
                return(NotFound());
            }

            InboundRulesSection section = InboundRulesHelper.GetSection(site, inboundRuleId.Path);
            InboundRule         rule    = (InboundRule)section.InboundRules.FirstOrDefault(r => r.Name.Equals(inboundRuleId.Name, StringComparison.OrdinalIgnoreCase));

            if (rule == null)
            {
                return(NotFound());
            }

            InboundRulesHelper.UpdateRule(model, rule, site, inboundRuleId.Path, ManagementUnit.ResolveConfigScope(model));

            ManagementUnit.Current.Commit();

            dynamic updatedRule = InboundRulesHelper.RuleToJsonModel(rule, site, inboundRuleId.Path, Context.Request.GetFields(), true);

            if (updatedRule.id != id)
            {
                return(LocationChanged(InboundRulesHelper.GetRuleLocation(updatedRule.id), updatedRule));
            }

            return(updatedRule);
        }
        public object Post([FromBody] dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            RewriteId parentId = RewriteHelper.GetRewriteIdFromBody(model);

            if (parentId == null)
            {
                throw new ApiArgumentException("url_rewrite");
            }

            // Get site the rule is for if applicable
            Site site = parentId.SiteId == null ? null : SiteHelper.GetSite(parentId.SiteId.Value);

            string configPath           = ManagementUnit.ResolveConfigScope(model);
            InboundRulesSection section = InboundRulesHelper.GetSection(site, parentId.Path, configPath);

            // Create rule
            InboundRule rule = InboundRulesHelper.CreateRule(model, site, parentId.Path, ManagementUnit.ResolveConfigScope(model));

            // Add it
            InboundRulesHelper.AddRule(rule, section, model);

            // Save
            ManagementUnit.Current.Commit();

            //
            // Create response
            dynamic r = InboundRulesHelper.RuleToJsonModel(rule, site, parentId.Path, Context.Request.GetFields(), true);

            return(Created(InboundRulesHelper.GetRuleLocation(r.id), r));
        }
        protected override void CopyInfo(RuleElement source, RuleElement destination)
        {
            base.CopyInfo(source, destination);
            InboundRule sourceRuleElement      = (InboundRule)source;
            InboundRule destinationRuleElement = (InboundRule)destination;

            sourceRuleElement.ServerVariableAssignments.CopyTo(destinationRuleElement.ServerVariableAssignments);
        }
Beispiel #7
0
 public static object RuleToJsonModelRef(InboundRule rule, Site site, string path, Fields fields = null)
 {
     if (fields == null || !fields.HasFields)
     {
         return(RuleToJsonModel(rule, site, path, RuleRefFields, false));
     }
     else
     {
         return(RuleToJsonModel(rule, site, path, fields, false));
     }
 }
Beispiel #8
0
 private static void SetRule(dynamic model, InboundRule rule, InboundRulesSection section, AllowedServerVariablesSection serverVariablesSection)
 {
     try {
         AssignRuleFromModel(model, rule, section, serverVariablesSection);
     }
     catch (FileLoadException e) {
         throw new LockedException(section.SectionPath, e);
     }
     catch (DirectoryNotFoundException e) {
         throw new ConfigScopeNotFoundException(e);
     }
 }
Beispiel #9
0
        private static void UpdatePriority(dynamic model, InboundRule rule, InboundRulesSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            DynamicHelper.If((object)model.priority, 0, int.MaxValue, v => {
                v = v >= section.InboundRules.Count ? section.InboundRules.Count - 1 : v;
                if (section.InboundRules.IndexOf(rule) != -1)
                {
                    section.InboundRules.Move(rule, (int)v);
                }
            });
        }
        public object Get(string id)
        {
            var inboundRuleId = new InboundRuleId(id);

            Site site = inboundRuleId.SiteId == null ? null : SiteHelper.GetSite(inboundRuleId.SiteId.Value);

            if (inboundRuleId.SiteId != null && site == null)
            {
                return(NotFound());
            }

            InboundRule rule = (InboundRule)InboundRulesHelper.GetSection(site, inboundRuleId.Path).InboundRules.FirstOrDefault(r => r.Name.Equals(inboundRuleId.Name, StringComparison.OrdinalIgnoreCase));

            if (rule == null)
            {
                return(NotFound());
            }

            return(InboundRulesHelper.RuleToJsonModel(rule, site, inboundRuleId.Path, Context.Request.GetFields()));
        }
        public void Delete(string id)
        {
            InboundRule rule          = null;
            var         inboundRuleId = new InboundRuleId(id);

            Site site = inboundRuleId.SiteId == null ? null : SiteHelper.GetSite(inboundRuleId.SiteId.Value);

            if (inboundRuleId.SiteId == null || site != null)
            {
                rule = (InboundRule)InboundRulesHelper.GetSection(site, inboundRuleId.Path).InboundRules.FirstOrDefault(r => r.Name.Equals(inboundRuleId.Name, StringComparison.OrdinalIgnoreCase));
            }

            if (rule != null)
            {
                var section = InboundRulesHelper.GetSection(site, inboundRuleId.Path, ManagementUnit.ResolveConfigScope());

                InboundRulesHelper.DeleteRule(rule, section);
                ManagementUnit.Current.Commit();
            }

            Context.Response.StatusCode = (int)HttpStatusCode.NoContent;
        }
Beispiel #12
0
        private static void AssignRuleFromModel(dynamic model, InboundRule rule, InboundRulesSection section, AllowedServerVariablesSection serverVariablesSection)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            //
            // Name, check for already existing name
            string name = DynamicHelper.Value(model.name);

            if (!string.IsNullOrEmpty(name))
            {
                if (!name.Equals(rule.Name, StringComparison.OrdinalIgnoreCase) &&
                    section.InboundRules.Any(r => r.Name.Equals(name, StringComparison.OrdinalIgnoreCase)))
                {
                    throw new AlreadyExistsException("name");
                }

                rule.Name = name;
            }

            DynamicHelper.If((object)model.pattern, v => rule.Match.Pattern = v);
            DynamicHelper.If <bool>((object)model.ignore_case, v => rule.Match.IgnoreCase   = v);
            DynamicHelper.If <bool>((object)model.negate, v => rule.Match.Negate            = v);
            DynamicHelper.If <bool>((object)model.stop_processing, v => rule.StopProcessing = v);
            DynamicHelper.If((object)model.pattern_syntax, v => rule.PatternSyntax          = PatternSyntaxHelper.FromJsonModel(v));

            //
            // Action
            dynamic action = model.action;

            if (action != null)
            {
                if (!(action is JObject))
                {
                    throw new ApiArgumentException("action", ApiArgumentException.EXPECTED_OBJECT);
                }

                DynamicHelper.If((object)action.type, v => rule.Action.Type = ActionTypeHelper.FromJsonModel(v));
                DynamicHelper.If((object)action.url, v => rule.Action.Url   = v);
                DynamicHelper.If <bool>((object)action.append_query_string, v => rule.Action.AppendQueryString = v);
                DynamicHelper.If <bool>((object)action.log_rewritten_url, v => rule.Action.LogRewrittenUrl     = v);
                DynamicHelper.If <long>((object)action.status_code, v => rule.Action.StatusCode        = v);
                DynamicHelper.If <long>((object)action.sub_status_code, v => rule.Action.SubStatusCode = v);
                DynamicHelper.If((object)action.description, v => rule.Action.StatusDescription        = v);
                DynamicHelper.If((object)action.reason, v => rule.Action.StatusReason = v);
                DynamicHelper.If <RedirectType>((object)action.redirect_type, v => rule.Action.RedirectType = v);
            }

            //
            // Server variables
            if (model.server_variables != null)
            {
                IEnumerable <dynamic> serverVariables = model.server_variables as IEnumerable <dynamic>;

                if (serverVariables == null)
                {
                    throw new ApiArgumentException("server_variables", ApiArgumentException.EXPECTED_ARRAY);
                }

                rule.ServerVariableAssignments.Clear();

                foreach (dynamic serverVariable in serverVariables)
                {
                    if (!(serverVariable is JObject))
                    {
                        throw new ApiArgumentException("server_variables.item");
                    }

                    string svName    = DynamicHelper.Value(serverVariable.name);
                    string svValue   = DynamicHelper.Value(serverVariable.value);
                    bool   svReplace = DynamicHelper.To <bool>(serverVariable.replace) ?? false;

                    if (string.IsNullOrEmpty(svName))
                    {
                        throw new ApiArgumentException("server_variables.item.name", "Required");
                    }

                    if (string.IsNullOrEmpty(svValue))
                    {
                        throw new ApiArgumentException("server_variables.item.value", "Required");
                    }

                    var svAssignment = rule.ServerVariableAssignments.CreateElement();
                    svAssignment.Name    = svName;
                    svAssignment.Value   = svValue;
                    svAssignment.Replace = svReplace;

                    AddAllowedServerVariable(serverVariablesSection, svAssignment.Name);

                    rule.ServerVariableAssignments.Add(svAssignment);
                }
            }

            DynamicHelper.If((object)model.condition_match_constraints, v => rule.Conditions.LogicalGrouping = LogicalGroupingHelper.FromJsonModel(v));
            DynamicHelper.If <bool>((object)model.track_all_captures, v => rule.Conditions.TrackAllCaptures  = v);

            //
            // Conditions
            if (model.conditions != null)
            {
                IEnumerable <dynamic> conditions = model.conditions as IEnumerable <dynamic>;

                if (conditions == null)
                {
                    throw new ApiArgumentException("conditions", ApiArgumentException.EXPECTED_ARRAY);
                }

                rule.Conditions.Clear();

                foreach (dynamic condition in conditions)
                {
                    if (!(condition is JObject))
                    {
                        throw new ApiArgumentException("conditions.item");
                    }

                    string input        = DynamicHelper.Value(condition.input);
                    string rawMatchType = DynamicHelper.Value(condition.match_type);

                    if (string.IsNullOrEmpty(input))
                    {
                        throw new ApiArgumentException("conditions.item.input", "Required");
                    }

                    if (string.IsNullOrEmpty(rawMatchType))
                    {
                        throw new ApiArgumentException("conditions.item.match_type", "Required");
                    }

                    MatchType matchType = MatchTypeHelper.FromJsonModel(rawMatchType);

                    var con = rule.Conditions.CreateElement();
                    con.Input      = input;
                    con.MatchType  = matchType;
                    con.Pattern    = DynamicHelper.Value(condition.pattern);
                    con.Negate     = DynamicHelper.To <bool>(condition.negate);
                    con.IgnoreCase = DynamicHelper.To <bool>(condition.ignore_case);

                    rule.Conditions.Add(con);
                }
            }

            if (rule.Schema.HasAttribute(InboundRule.ResponseCacheDirectiveAttribute))
            {
                DynamicHelper.If((object)model.response_cache_directive, v => rule.ResponseCacheDirective = ResponseCacheDirectiveHelper.FromJsonModel(v));
            }

            //
            // Check set to valid state
            if ((rule.Action.Type == ActionType.Redirect || rule.Action.Type == ActionType.Rewrite) && string.IsNullOrEmpty(rule.Action.Url))
            {
                throw new ApiArgumentException("action.url");
            }

            UpdatePriority(model, rule, section);
        }
Beispiel #13
0
        public static object RuleToJsonModel(InboundRule rule, Site site, string path, Fields fields = null, bool full = true)
        {
            if (rule == null)
            {
                return(null);
            }

            if (fields == null)
            {
                fields = Fields.All;
            }

            var inboundRuleId = new InboundRuleId(site?.Id, path, rule.Name);

            dynamic obj = new ExpandoObject();

            //
            // name
            if (fields.Exists("name"))
            {
                obj.name = rule.Name;
            }

            //
            // id
            if (fields.Exists("id"))
            {
                obj.id = inboundRuleId.Uuid;
            }

            //
            // priority
            if (fields.Exists("priority"))
            {
                obj.priority = GetSection(site, path).InboundRules.IndexOf(rule);
            }

            //
            // pattern
            if (fields.Exists("pattern"))
            {
                obj.pattern = rule.Match.Pattern;
            }

            //
            // pattern_syntax
            if (fields.Exists("pattern_syntax"))
            {
                obj.pattern_syntax = PatternSyntaxHelper.ToJsonModel(rule.PatternSyntax);
            }

            //
            // ignore_case
            if (fields.Exists("ignore_case"))
            {
                obj.ignore_case = rule.Match.IgnoreCase;
            }

            //
            // negate
            if (fields.Exists("negate"))
            {
                obj.negate = rule.Match.Negate;
            }

            //
            // stop_processing
            if (fields.Exists("stop_processing"))
            {
                obj.stop_processing = rule.StopProcessing;
            }

            //
            // response_cache_directive
            if (fields.Exists("response_cache_directive") && rule.Schema.HasAttribute(InboundRule.ResponseCacheDirectiveAttribute))
            {
                obj.response_cache_directive = ResponseCacheDirectiveHelper.ToJsonModel(rule.ResponseCacheDirective);
            }

            //
            // condition_match_constraints
            if (fields.Exists("condition_match_constraints"))
            {
                obj.condition_match_constraints = LogicalGroupingHelper.ToJsonModel(rule.Conditions.LogicalGrouping);
            }

            //
            // track_all_captures
            if (fields.Exists("track_all_captures"))
            {
                obj.track_all_captures = rule.Conditions.TrackAllCaptures;
            }

            //
            // action
            if (fields.Exists("action"))
            {
                obj.action = new ExpandoObject();
                dynamic action = obj.action;

                action.type = ActionTypeHelper.ToJsonModel(rule.Action.Type);
                action.url  = rule.Action.Url;
                action.append_query_string = rule.Action.AppendQueryString;
                action.log_rewritten_url   = rule.Action.LogRewrittenUrl;

                if (rule.Action.Type == ActionType.Redirect)
                {
                    action.redirect_type = Enum.GetName(typeof(RedirectType), rule.Action.RedirectType).ToLowerInvariant();
                }

                if (rule.Action.Type == ActionType.CustomResponse)
                {
                    action.status_code     = rule.Action.StatusCode;
                    action.sub_status_code = rule.Action.SubStatusCode;
                    action.description     = rule.Action.StatusDescription;
                    action.reason          = rule.Action.StatusReason;
                }
            }

            //
            // server_variables
            if (fields.Exists("server_variables"))
            {
                obj.server_variables = rule.ServerVariableAssignments.Select(s => new {
                    name    = s.Name,
                    value   = s.Value,
                    replace = s.Replace
                });
            }

            //
            // conditions
            if (fields.Exists("conditions"))
            {
                obj.conditions = rule.Conditions.Select(c => new {
                    input       = c.Input,
                    pattern     = c.Pattern,
                    negate      = c.Negate,
                    ignore_case = c.IgnoreCase,
                    match_type  = MatchTypeHelper.ToJsonModel(c.MatchType)
                });
            }

            //
            // url_rewrite
            if (fields.Exists("url_rewrite"))
            {
                obj.url_rewrite = RewriteHelper.ToJsonModelRef(site, path, fields.Filter("url_rewrite"));
            }

            return(Core.Environment.Hal.Apply(Defines.InboundRulesResource.Guid, obj));
        }
Beispiel #14
0
 public static void UpdateRule(dynamic model, InboundRule rule, InboundRulesSection section)
 {
     SetRule(model, rule, section);
 }