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); }
public object Get(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()); } PreCondition precondition = OutboundRulesHelper.GetSection(site, preConditionId.Path).PreConditions.FirstOrDefault(pc => pc.Name.Equals(preConditionId.Name, StringComparison.OrdinalIgnoreCase)); if (precondition == null) { return(NotFound()); } return(OutboundRulesHelper.PreConditionToJsonModel(precondition, site, preConditionId.Path, Context.Request.GetFields())); }
public void Delete(string id) { PreCondition preCondition = null; var preConditionId = new PreConditionId(id); Site site = preConditionId.SiteId == null ? null : SiteHelper.GetSite(preConditionId.SiteId.Value); if (preConditionId.SiteId == null || site != null) { preCondition = OutboundRulesHelper.GetSection(site, preConditionId.Path).PreConditions.FirstOrDefault(pc => pc.Name.Equals(preConditionId.Name, StringComparison.OrdinalIgnoreCase)); } if (preCondition != null) { var section = OutboundRulesHelper.GetSection(site, preConditionId.Path, ManagementUnit.ResolveConfigScope()); OutboundRulesHelper.DeletePreCondition(preCondition, section); ManagementUnit.Current.Commit(); } Context.Response.StatusCode = (int)HttpStatusCode.NoContent; }
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); }