public static void UpdateSection(dynamic model, Site site, string path, string configPath = null) { if (model == null) { throw new ApiArgumentException("model"); } InboundRulesSection section = GetSection(site, path, configPath); try { // UseOriginalURLEncoding introduced in 2.1 if (section.Schema.HasAttribute(InboundRulesSection.UseOriginalUrlEncodingAttribute)) { DynamicHelper.If <bool>((object)model.use_original_url_encoding, v => section.UseOriginalURLEncoding = 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 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 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); } } }
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); }
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)); }
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); } }
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 static InboundRule CreateRule(dynamic model, Site site, string path, string configPath = null) { 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 (model.action == null) { throw new ApiArgumentException("action"); } if (!(model.action is JObject)) { throw new ApiArgumentException("action", ApiArgumentException.EXPECTED_OBJECT); } if (string.IsNullOrEmpty(DynamicHelper.Value(model.action.url))) { throw new ApiArgumentException("action.url"); } InboundRulesSection section = GetSection(site, path, configPath); AllowedServerVariablesSection serverVariablesSection = ServerVariablesHelper.GetSection(site, path, configPath); var rule = (InboundRule)section.InboundRules.CreateElement(); // // Defaults rule.PatternSyntax = PatternSyntax.ECMAScript; rule.Action.Type = ActionType.Rewrite; SetRule(model, rule, section, serverVariablesSection); return(rule); }
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); }
public static void UpdateRule(dynamic model, InboundRule rule, InboundRulesSection section) { SetRule(model, rule, section); }