public object Patch([FromBody] dynamic model, string id)
        {
            var preConditionId = new PreConditionId(id);

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

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

            OutboundRulesSection section      = OutboundRulesHelper.GetSection(site, preConditionId.Path);
            PreCondition         precondition = section.PreConditions.FirstOrDefault(pc => pc.Name.Equals(preConditionId.Name, StringComparison.OrdinalIgnoreCase));

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

            OutboundRulesHelper.UpdatePreCondition(model, precondition, section);

            ManagementUnit.Current.Commit();

            dynamic updatedPreCondition = OutboundRulesHelper.PreConditionToJsonModel(precondition, site, preConditionId.Path, Context.Request.GetFields(), true);

            if (updatedPreCondition.id != id)
            {
                return(LocationChanged(OutboundRulesHelper.GetPreConditionLocation(updatedPreCondition.id), updatedPreCondition));
            }

            return(updatedPreCondition);
        }
Ejemplo n.º 2
0
        public object Patch([FromBody] dynamic model, string id)
        {
            var outboundRuleId = new OutboundRuleId(id);

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

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

            OutboundRulesSection section = OutboundRulesHelper.GetSection(site, outboundRuleId.Path);
            OutboundRule         rule    = (OutboundRule)section.Rules.FirstOrDefault(r => r.Name.Equals(outboundRuleId.Name, StringComparison.OrdinalIgnoreCase));

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

            OutboundRulesHelper.UpdateRule(model, rule, section);

            ManagementUnit.Current.Commit();

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

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

            return(updatedRule);
        }
        public static void AddRule(OutboundRule rule, OutboundRulesSection section, dynamic model)
        {
            if (rule == null)
            {
                throw new ArgumentNullException(nameof(rule));
            }

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

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

            try {
                section.Rules.Add(rule);

                UpdatePriority(model, rule, section);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
        public static void AddCustomTags(TagsElement tags, OutboundRulesSection section)
        {
            if (tags == null)
            {
                throw new ArgumentNullException(nameof(tags));
            }

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

            if (section.PreConditions.Any(r => r.Name.Equals(tags.Name)))
            {
                throw new AlreadyExistsException("name");
            }

            try
            {
                section.Tags.Add(tags);
            }
            catch (FileLoadException e)
            {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e)
            {
                throw new ConfigScopeNotFoundException(e);
            }
        }
        public static void UpdateSection(dynamic model, Site site, string path, string configPath = null)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            OutboundRulesSection section = GetSection(site, path, configPath);

            try {
                DynamicHelper.If <bool>((object)model.rewrite_before_cache, v => section.RewriteBeforeCache = v);

                if (model.metadata != null)
                {
                    DynamicHelper.If <OverrideMode>((object)model.metadata.override_mode, v => {
                        section.OverrideMode = v;
                    });
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
        public static void AddPreCondition(PreCondition precondition, OutboundRulesSection section)
        {
            if (precondition == null)
            {
                throw new ArgumentNullException(nameof(precondition));
            }

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

            if (section.PreConditions.Any(r => r.Name.Equals(precondition.Name)))
            {
                throw new AlreadyExistsException("name");
            }

            try {
                section.PreConditions.Add(precondition);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
Ejemplo n.º 7
0
        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");
            }

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

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

            TagsElement tags = OutboundRulesHelper.CreateCustomTags(model, section);

            OutboundRulesHelper.AddCustomTags(tags, section);

            ManagementUnit.Current.Commit();

            dynamic pc = OutboundRulesHelper.TagsToJsonModel(tags, site, parentId.Path, Context.Request.GetFields(), true);

            return(Created(OutboundRulesHelper.GetCustomTagsLocation(pc.id), pc));
        }
        public static OutboundRule CreateRule(dynamic model, OutboundRulesSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            if (string.IsNullOrEmpty(DynamicHelper.Value(model.name)))
            {
                throw new ApiArgumentException("name");
            }

            if (string.IsNullOrEmpty(DynamicHelper.Value(model.pattern)))
            {
                throw new ApiArgumentException("pattern");
            }

            if (string.IsNullOrEmpty(DynamicHelper.Value(model.match_type)))
            {
                throw new ApiArgumentException("match_type");
            }

            var rule = (OutboundRule)section.Rules.CreateElement();

            //
            // Default to rewrite rule
            rule.Action.Type   = OutboundActionType.Rewrite;
            rule.PatternSyntax = PatternSyntax.ECMAScript;

            SetRule(model, rule, section);

            return(rule);
        }
Ejemplo n.º 9
0
        public object Patch([FromBody] dynamic model, string id)
        {
            var customTagsId = new CustomTagsId(id);

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

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

            OutboundRulesSection section = OutboundRulesHelper.GetSection(site, customTagsId.Path);
            TagsElement          tags    = section.Tags.FirstOrDefault(t => t.Name.Equals(customTagsId.Name, StringComparison.OrdinalIgnoreCase));

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

            OutboundRulesHelper.UpdateCustomTags(model, tags, section);

            ManagementUnit.Current.Commit();

            dynamic updatedCustomTags = OutboundRulesHelper.TagsToJsonModel(tags, site, customTagsId.Path, Context.Request.GetFields(), true);

            if (updatedCustomTags.id != id)
            {
                return(LocationChanged(OutboundRulesHelper.GetCustomTagsLocation(updatedCustomTags.id), updatedCustomTags));
            }

            return(updatedCustomTags);
        }
        public static void DeleteCustomTags(TagsElement tags, OutboundRulesSection section)
        {
            if (tags == null)
            {
                return;
            }

            tags = section.Tags.FirstOrDefault(t => t.Name.Equals(tags.Name));

            if (tags != null)
            {
                try
                {
                    section.Tags.Remove(tags);
                }
                catch (FileLoadException e)
                {
                    throw new LockedException(section.SectionPath, e);
                }
                catch (DirectoryNotFoundException e)
                {
                    throw new ConfigScopeNotFoundException(e);
                }
            }
        }
 private static void SetRule(dynamic model, OutboundRule rule, OutboundRulesSection section)
 {
     try {
         AssignRuleFromModel(model, rule, section);
     }
     catch (FileLoadException e) {
         throw new LockedException(section.SectionPath, e);
     }
     catch (DirectoryNotFoundException e) {
         throw new ConfigScopeNotFoundException(e);
     }
 }
        private static void UpdatePriority(dynamic model, OutboundRule rule, OutboundRulesSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            DynamicHelper.If((object)model.priority, 0, int.MaxValue, v => {
                v = v >= section.Rules.Count ? section.Rules.Count - 1 : v;
                if (section.Rules.IndexOf(rule) != -1)
                {
                    section.Rules.Move(rule, (int)v);
                }
            });
        }
        public static TagsElement CreateCustomTags(dynamic model, OutboundRulesSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            if (string.IsNullOrEmpty(DynamicHelper.Value(model.name)))
            {
                throw new ApiArgumentException("name");
            }

            TagsElement tags = section.Tags.CreateElement();

            SetCustomTags(model, tags, section);

            return(tags);
        }
        public static PreCondition CreatePreCondition(dynamic model, OutboundRulesSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            if (string.IsNullOrEmpty(DynamicHelper.Value(model.name)))
            {
                throw new ApiArgumentException("name");
            }

            var precondition = section.PreConditions.CreateElement();

            SetPreCondition(model, precondition, section);

            return(precondition);
        }
        public static void DeleteRule(OutboundRule rule, OutboundRulesSection section)
        {
            if (rule == null)
            {
                return;
            }

            rule = (OutboundRule)section.Rules.FirstOrDefault(r => r.Name.Equals(rule.Name));

            if (rule != null)
            {
                try {
                    section.Rules.Remove(rule);
                }
                catch (FileLoadException e) {
                    throw new LockedException(section.SectionPath, e);
                }
                catch (DirectoryNotFoundException e) {
                    throw new ConfigScopeNotFoundException(e);
                }
            }
        }
        public static void DeletePreCondition(PreCondition precondition, OutboundRulesSection section)
        {
            if (precondition == null)
            {
                return;
            }

            precondition = section.PreConditions.FirstOrDefault(r => r.Name.Equals(precondition.Name));

            if (precondition != null)
            {
                try {
                    section.PreConditions.Remove(precondition);
                }
                catch (FileLoadException e) {
                    throw new LockedException(section.SectionPath, e);
                }
                catch (DirectoryNotFoundException e) {
                    throw new ConfigScopeNotFoundException(e);
                }
            }
        }
 public static void UpdateCustomTags(dynamic model, TagsElement tags, OutboundRulesSection section)
 {
     SetCustomTags(model, tags, section);
 }
 public static void UpdateRule(dynamic model, OutboundRule rule, OutboundRulesSection section)
 {
     SetRule(model, rule, section);
 }
 public static void UpdatePreCondition(dynamic model, PreCondition rule, OutboundRulesSection section)
 {
     SetPreCondition(model, rule, section);
 }
        private static void SetPreCondition(dynamic model, PreCondition precondition, OutboundRulesSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            try {
                string name = DynamicHelper.Value(model.name);
                if (!string.IsNullOrEmpty(name))
                {
                    if (!name.Equals(precondition.Name, StringComparison.OrdinalIgnoreCase) &&
                        section.PreConditions.Any(pc => pc.Name.Equals(name, StringComparison.OrdinalIgnoreCase)))
                    {
                        throw new AlreadyExistsException("name");
                    }

                    precondition.Name = name;
                }

                //
                // Match (Logical Grouping)
                DynamicHelper.If((object)model.match, v => {
                    switch (v.ToLowerInvariant())
                    {
                    case "all":
                        precondition.LogicalGrouping = LogicalGrouping.MatchAll;
                        break;

                    case "any":
                        precondition.LogicalGrouping = LogicalGrouping.MatchAny;
                        break;

                    default:
                        throw new ApiArgumentException("match");
                    }
                });

                // Pattern Syntax
                DynamicHelper.If((object)model.pattern_syntax, v => precondition.PatternSyntax = PatternSyntaxHelper.FromJsonModel(v));

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

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

                    precondition.Conditions.Clear();

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

                        string input = DynamicHelper.Value(requirement.input);

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

                        var req = precondition.Conditions.CreateElement();
                        req.Input      = input;
                        req.Pattern    = DynamicHelper.Value(requirement.pattern);
                        req.Negate     = DynamicHelper.To <bool>(requirement.negate);
                        req.IgnoreCase = DynamicHelper.To <bool>(requirement.ignore_case);

                        // Schema only specifies pattern match type for outbound rules
                        req.MatchType = PreConditionMatchType.Pattern;

                        precondition.Conditions.Add(req);
                    }
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
        private static void AssignRuleFromModel(dynamic model, OutboundRule rule, OutboundRulesSection section)
        {
            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.Rules.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.enabled, v => rule.Action.Type            = v ? OutboundActionType.Rewrite : OutboundActionType.None);
            DynamicHelper.If((object)model.rewrite_value, v => rule.Action.RewriteValue     = 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));

            //
            // Server Variable
            DynamicHelper.If((object)model.server_variable, v => rule.Match.ServerVariable = v);
            DynamicHelper.If <bool>((object)model.replace_server_variable, v => rule.Action.ReplaceServerVariable = v);

            //
            // Html Tags
            dynamic tagFilters = null;
            dynamic customTags = null;

            if (model.tag_filters != null)
            {
                tagFilters = model.tag_filters;

                if (!(tagFilters is JObject))
                {
                    throw new ApiArgumentException("tag_filters", ApiArgumentException.EXPECTED_OBJECT);
                }

                customTags = tagFilters.custom;

                // Clear custom tags
                rule.Match.CustomTags    = null;
                rule.Match.FilterByTags &= ~FilterByTags.CustomTags;
            }

            // Set standard tags
            if (tagFilters != null)
            {
                FilterByTags ruleTags = rule.Match.FilterByTags;

                DynamicHelper.If <bool>((object)tagFilters.a, v => SetTagFlag(ref ruleTags, FilterByTags.A, v));
                DynamicHelper.If <bool>((object)tagFilters.area, v => SetTagFlag(ref ruleTags, FilterByTags.Area, v));
                DynamicHelper.If <bool>((object)tagFilters.@base, v => SetTagFlag(ref ruleTags, FilterByTags.Base, v));
                DynamicHelper.If <bool>((object)tagFilters.form, v => SetTagFlag(ref ruleTags, FilterByTags.Form, v));
                DynamicHelper.If <bool>((object)tagFilters.frame, v => SetTagFlag(ref ruleTags, FilterByTags.Frame, v));
                DynamicHelper.If <bool>((object)tagFilters.head, v => SetTagFlag(ref ruleTags, FilterByTags.Head, v));
                DynamicHelper.If <bool>((object)tagFilters.iframe, v => SetTagFlag(ref ruleTags, FilterByTags.IFrame, v));
                DynamicHelper.If <bool>((object)tagFilters.img, v => SetTagFlag(ref ruleTags, FilterByTags.Img, v));
                DynamicHelper.If <bool>((object)tagFilters.input, v => SetTagFlag(ref ruleTags, FilterByTags.Input, v));
                DynamicHelper.If <bool>((object)tagFilters.link, v => SetTagFlag(ref ruleTags, FilterByTags.Link, v));
                DynamicHelper.If <bool>((object)tagFilters.script, v => SetTagFlag(ref ruleTags, FilterByTags.Script, v));

                rule.Match.FilterByTags = ruleTags;
            }

            // Set custom tags
            if (customTags != null)
            {
                if (!(customTags is JObject))
                {
                    throw new ApiArgumentException("tags.custom", ApiArgumentException.EXPECTED_OBJECT);
                }

                string ctId = DynamicHelper.Value(customTags.id);

                if (string.IsNullOrEmpty(ctId))
                {
                    throw new ArgumentException("tags.custom.id", "required");
                }

                TagsElement targetCustomTags = section.Tags.FirstOrDefault(t => t.Name.Equals(new CustomTagsId(ctId).Name, StringComparison.OrdinalIgnoreCase));

                if (targetCustomTags == null)
                {
                    throw new NotFoundException("tags.custom");
                }

                rule.Match.FilterByTags |= FilterByTags.CustomTags;
                rule.Match.CustomTags    = targetCustomTags.Name;
            }

            if (model.precondition != null)
            {
                dynamic precondition = model.precondition;

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

                string id = DynamicHelper.Value(precondition.id);

                if (string.IsNullOrEmpty(id))
                {
                    throw new ApiArgumentException("precondition.id");
                }

                PreConditionId preconditionId = new PreConditionId(id);

                PreCondition pc = section.PreConditions.FirstOrDefault(p => p.Name.Equals(preconditionId.Name, StringComparison.OrdinalIgnoreCase));

                if (pc == null)
                {
                    throw new NotFoundException("precondition.id");
                }

                rule.PreCondition = pc.Name;
            }

            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("server_variables.item");
                    }

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

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

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

                    // Schema only specifies pattern match type for outbound rules
                    con.MatchType = MatchType.Pattern;

                    rule.Conditions.Add(con);
                }
            }

            //
            // Set match type
            string type = DynamicHelper.Value(model.match_type);
            OutboundRuleMatchType matchType = string.IsNullOrEmpty(type) ? GetMatchType(rule) : OutboundMatchTypeHelper.FromJsonModel(type);

            if (matchType == OutboundRuleMatchType.Response)
            {
                rule.Match.ServerVariable = null;
            }
            else
            {
                rule.Match.FilterByTags = FilterByTags.None;
            }

            UpdatePriority(model, rule, section);
        }
        private static void SetCustomTags(dynamic model, TagsElement tagsSet, OutboundRulesSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            try {
                string name = DynamicHelper.Value(model.name);
                if (!string.IsNullOrEmpty(name))
                {
                    if (!name.Equals(tagsSet.Name, StringComparison.OrdinalIgnoreCase) &&
                        section.PreConditions.Any(pc => pc.Name.Equals(name, StringComparison.OrdinalIgnoreCase)))
                    {
                        throw new AlreadyExistsException("name");
                    }

                    tagsSet.Name = name;
                }

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

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

                    tagsSet.Tags.Clear();

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

                        string tagName   = DynamicHelper.Value(requirement.name);
                        string attribute = DynamicHelper.Value(requirement.attribute);

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

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

                        var t = tagsSet.Tags.CreateElement();
                        t.Name      = tagName;
                        t.Attribute = attribute;

                        tagsSet.Tags.Add(t);
                    }
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }