/// <summary> /// /// </summary> /// <param name="conditions"></param> /// <param name="action"></param> /// <param name="flags"></param> /// <returns></returns> public IRule MockRule(IList <ICondition> conditions, IRuleAction action, IRuleFlagProcessor flags) { // create properties if (conditions == null) { conditions = new Mock <IList <ICondition> >().Object; } if (action == null) { action = new Mock <IRuleAction>().Object; } if (flags == null) { var flagsMock = new Mock <IRuleFlagProcessor>(); flagsMock.Expect(x => x.GetEnumerator()).Returns(() => { return(new List <IRuleFlag>(0).GetEnumerator()); }); flags = flagsMock.Object; } var ruleMock = new Mock <IRule>(); ruleMock.SetupGet(r => r.Conditions).Returns(conditions); ruleMock.SetupGet(r => r.Action).Returns(action); ruleMock.SetupGet(r => r.Flags).Returns(flags); var rule = ruleMock.Object; return(rule); }
/// <summary> /// /// </summary> /// <param name="conditions"></param> /// <param name="action"></param> /// <param name="flags"></param> /// <returns></returns> public IRule MockRule(IList<ICondition> conditions, IRuleAction action, IRuleFlagProcessor flags) { // create properties if (conditions == null) conditions = new Mock<IList<ICondition>>().Object; if (action == null) action = new Mock<IRuleAction>().Object; if (flags == null) { var flagsMock = new Mock<IRuleFlagProcessor>(); flagsMock.Expect(x => x.GetEnumerator()).Returns(() => { return new List<IRuleFlag>(0).GetEnumerator(); }); flags = flagsMock.Object; } var ruleMock = new Mock<IRule>(); ruleMock.SetupGet(r => r.Conditions).Returns(conditions); ruleMock.SetupGet(r => r.Action).Returns(action); ruleMock.SetupGet(r => r.Flags).Returns(flags); var rule = ruleMock.Object; return rule; }
/// <summary> /// Determines whether [has not for internal sub requests] [the specified flags]. /// </summary> /// <param name="flags">The flags.</param> /// <returns> /// <see langword="true"/> if [has not for internal sub requests] [the specified flags]; otherwise, <see langword="false"/>. /// </returns> public static bool HasNotForInternalSubRequests(IRuleFlagProcessor flags) { foreach (IRuleFlag flag in flags) { if (flag is NotForInternalSubRequestsFlag) { return(true); } } return(false); }
/// <summary> /// Determines whether [has no case] [the specified flags]. /// </summary> /// <param name="flags">The flags.</param> /// <returns> /// <see langword="true"/> if [has no case] [the specified flags]; otherwise, <see langword="false"/>. /// </returns> public static bool HasChain(IRuleFlagProcessor flags) { foreach (IRuleFlag flag in flags) { if (flag is ChainFlag) { return(true); } } return(false); }
/// <summary> /// Determines whether [has no escape] [the specified flags]. /// </summary> /// <param name="flags">The flags.</param> /// <returns> /// <see langword="true"/> if [has no escape] [the specified flags]; otherwise, <see langword="false"/>. /// </returns> public static bool HasNoEscape(IRuleFlagProcessor flags) { foreach (IRuleFlag flag in flags) { if (flag is NoEscapeFlag) { return(true); } } return(false); }
/// <summary> /// Gets the match. /// </summary> /// <param name="matchElement">The match element.</param> /// <returns></returns> private Pattern GetMatch(XmlNode matchElement, ref IRuleFlagProcessor ruleFlags) { string url = null; bool ignoreCase = true; // from schema definition bool negate = false; // from schema definition if (matchElement.Attributes["url"] != null) { url = matchElement.Attributes["url"].Value; } if (matchElement.Attributes["ignoreCase"] != null) { ignoreCase = XmlConvert.ToBoolean(matchElement.Attributes["ignoreCase"].Value); } if (matchElement.Attributes["negate"] != null) { negate = XmlConvert.ToBoolean(matchElement.Attributes["negate"].Value); } // validationType="nonEmptyString" if (String.IsNullOrEmpty(url)) { throw new RuleSetException("Match URL must be a non-empty value."); } var patternOptions = Manager.RuleOptions; if (ignoreCase) { ruleFlags.Add(new RF.NoCaseFlag()); patternOptions |= RegexOptions.IgnoreCase; } return(new Pattern(url, negate, patternOptions)); }
/// <summary> /// Initializes a new instance of the <see cref="DefaultRule"/> class. /// </summary> /// <param name="conditions">The conditions.</param> /// <param name="pattern">The pattern.</param> /// <param name="substitution">The substitution.</param> /// <param name="options">The options.</param> /// <param name="flags">The flags.</param> public DefaultRule(IEnumerable<ICondition> conditions, string pattern, string substitution, RegexOptions options, IRuleFlagProcessor flags) : this(conditions, new DefaultRuleAction(pattern, substitution, options), flags) { }
/// <summary> /// Initializes a new instance of the <see cref="DefaultRule"/> class. /// </summary> /// <param name="conditions">The conditions.</param> /// <param name="action">The action.</param> /// <param name="flags">The flags.</param> public DefaultRule(IEnumerable<ICondition> conditions, IRuleAction action, IRuleFlagProcessor flags) { ((IRule)this).Init(conditions, action, flags); }
/// <summary> /// Determines whether [has no escape] [the specified flags]. /// </summary> /// <param name="flags">The flags.</param> /// <returns> /// <see langword="true"/> if [has no escape] [the specified flags]; otherwise, <see langword="false"/>. /// </returns> public static bool HasNoEscape(IRuleFlagProcessor flags) { return flags.OfType<NoEscapeFlag>().Any(); }
/// <summary> /// Determines whether [has not for internal sub requests] [the specified flags]. /// </summary> /// <param name="flags">The flags.</param> /// <returns> /// <see langword="true"/> if [has not for internal sub requests] [the specified flags]; otherwise, <see langword="false"/>. /// </returns> public static bool HasNotForInternalSubRequests(IRuleFlagProcessor flags) { return flags.OfType<NotForInternalSubRequestsFlag>().Any(); }
/// <summary> /// Initializes a new instance of the <see cref="DefaultRule"/> class. /// </summary> /// <param name="conditions">The conditions.</param> /// <param name="pattern">The pattern.</param> /// <param name="substitution">The substitution.</param> /// <param name="options">The options.</param> /// <param name="flags">The flags.</param> public DefaultRule(IEnumerable <ICondition> conditions, string pattern, string substitution, RegexOptions options, IRuleFlagProcessor flags) : this(conditions, new DefaultRuleAction(pattern, substitution, options), flags) { }
/// <summary> /// Determines whether [has no case] [the specified flags]. /// </summary> /// <param name="flags">The flags.</param> /// <returns> /// <see langword="true"/> if [has no case] [the specified flags]; otherwise, <see langword="false"/>. /// </returns> public static bool HasChain(IRuleFlagProcessor flags) { return flags.OfType<ChainFlag>().Any(); }
/// <summary> /// Inits the specified conditions. /// </summary> /// <param name="conditions">The conditions.</param> /// <param name="action">The action.</param> /// <param name="flags">The flags.</param> void IRule.Init(IEnumerable <ICondition> conditions, IRuleAction action, IRuleFlagProcessor flags) { _conditions = new List <ICondition>(conditions ?? new ICondition[0]); _action = action; _flags = flags ?? new RuleFlagProcessor(); }
/// <summary> /// Initializes a new instance of the <see cref="DefaultRule"/> class. /// </summary> /// <param name="conditions">The conditions.</param> /// <param name="pattern">The pattern.</param> /// <param name="substitution">The substitution.</param> /// <param name="flags">The flags.</param> public DefaultRule(IEnumerable<ICondition> conditions, string pattern, string substitution, IRuleFlagProcessor flags) : this(conditions, pattern, substitution, UrlRewriterManager.RuleOptions, flags) { }
/// <summary> /// Gets the action. /// </summary> /// <param name="actionElement">The action element.</param> /// <param name="ruleFlags">The rule flags.</param> /// <returns></returns> private IRuleAction GetAction(XmlNode actionElement, Pattern pattern, ref IRuleFlagProcessor ruleFlags) { ActionType type = ActionType.None; string url = null; bool appendQueryString = true; // from schema definition RedirectType redirectType = RedirectType.Permanent; uint statusCode = 0U; uint subStatusCode = 0U; // from schema definition string statusReason = null; string statusDescription = null; if (actionElement.Attributes["type"] != null) { try { type = (ActionType)Enum.Parse(typeof(ActionType), actionElement.Attributes["type"].Value, true); } catch (Exception exc) { Manager.Log("Action: " + exc.Message, "Error"); } } if (actionElement.Attributes["url"] != null) { url = actionElement.Attributes["url"].Value; } if (actionElement.Attributes["appendQueryString"] != null) { appendQueryString = XmlConvert.ToBoolean(actionElement.Attributes["appendQueryString"].Value); } if (actionElement.Attributes["redirectType"] != null) { try { redirectType = (RedirectType)Enum.Parse(typeof(RedirectType), actionElement.Attributes["redirectType"].Value, true); } catch (Exception exc) { Manager.Log("Action: " + exc.Message, "Error"); } } if (actionElement.Attributes["statusCode"] != null) { statusCode = XmlConvert.ToUInt32(actionElement.Attributes["statusCode"].Value); } if (actionElement.Attributes["subStatusCode"] != null) { subStatusCode = XmlConvert.ToUInt32(actionElement.Attributes["subStatusCode"].Value); } if (actionElement.Attributes["statusReason"] != null) { statusReason = actionElement.Attributes["statusReason"].Value; } if (actionElement.Attributes["statusDescription"] != null) { statusDescription = actionElement.Attributes["statusDescription"].Value; } if (String.IsNullOrEmpty(url)) { throw new RuleSetException("Action URL must be a non-empty value."); } // validationType="requireTrimmedString" url = url.Trim(); if (type == ActionType.Redirect) { ruleFlags.Add(new RF.RedirectFlag((int)redirectType)); } else if (statusCode > 0U) { // validationType="integerRange" validationParameter="300,307,exclude" if (statusCode >= 300U && statusCode <= 307U) { throw new RuleSetException("Action Status Code should not be an int between 300 - 307, use the redirectType for this range."); } if (statusCode < 1U || statusCode > 999U) { throw new RuleSetException("Action Status Code should be between 1 - 999."); } if (subStatusCode < 0U || subStatusCode > 999U) { throw new RuleSetException("Action Sub Status Code should be between 0 - 999."); } ruleFlags.Add(new RF.ResponseStatusFlag(statusCode, subStatusCode, statusReason, statusDescription)); } IRuleAction substitution = new DefaultRuleAction(); substitution.Init(pattern, url); return(substitution); }
/// <summary> /// Determines whether [has no case] [the specified flags]. /// </summary> /// <param name="flags">The flags.</param> /// <returns> /// <see langword="true"/> if [has no case] [the specified flags]; otherwise, <see langword="false"/>. /// </returns> public static bool HasChain(IRuleFlagProcessor flags) { foreach (IRuleFlag flag in flags) if (flag is ChainFlag) return true; return false; }
/// <summary> /// Gets the match. /// </summary> /// <param name="matchElement">The match element.</param> /// <returns></returns> private Pattern GetMatch(XmlNode matchElement, ref IRuleFlagProcessor ruleFlags) { string url = null; var ignoreCase = true; // from schema definition var negate = false; // from schema definition if (matchElement.Attributes["url"] != null) { url = matchElement.Attributes["url"].Value; } if (matchElement.Attributes["ignoreCase"] != null) { ignoreCase = XmlConvert.ToBoolean(matchElement.Attributes["ignoreCase"].Value); } if (matchElement.Attributes["negate"] != null) { negate = XmlConvert.ToBoolean(matchElement.Attributes["negate"].Value); } // validationType="nonEmptyString" if (string.IsNullOrEmpty(url)) { throw new RuleSetException("Match URL must be a non-empty value."); } var patternOptions = UrlRewriterManager.RuleOptions; if (ignoreCase) { ruleFlags.Add(new NoCaseFlag()); patternOptions |= RegexOptions.IgnoreCase; } return new Pattern(url, negate, patternOptions); }
/// <summary> /// Gets the action. /// </summary> /// <param name="actionElement">The action element.</param> /// <param name="ruleFlags">The rule flags.</param> /// <returns></returns> private IRuleAction GetAction(XmlNode actionElement, Pattern pattern, ref IRuleFlagProcessor ruleFlags) { var type = ActionType.None; string url = null; var appendQueryString = true; // from schema definition var redirectType = RedirectType.Permanent; var statusCode = 0U; var subStatusCode = 0U; // from schema definition string statusReason = null; string statusDescription = null; if (actionElement.Attributes["type"] != null) { try { type = (ActionType)Enum.Parse(typeof(ActionType), actionElement.Attributes["type"].Value, true); } catch (Exception exc) { Logger.Error("Action: " + exc.Message, exc); } } if (actionElement.Attributes["url"] != null) { url = actionElement.Attributes["url"].Value; } if (actionElement.Attributes["appendQueryString"] != null) { appendQueryString = XmlConvert.ToBoolean(actionElement.Attributes["appendQueryString"].Value); } if (actionElement.Attributes["redirectType"] != null) { try { redirectType = (RedirectType)Enum.Parse(typeof(RedirectType), actionElement.Attributes["redirectType"].Value, true); } catch (Exception exc) { Logger.Error("Action: " + exc.Message, exc); } } if (actionElement.Attributes["statusCode"] != null) { statusCode = XmlConvert.ToUInt32(actionElement.Attributes["statusCode"].Value); } if (actionElement.Attributes["subStatusCode"] != null) { subStatusCode = XmlConvert.ToUInt32(actionElement.Attributes["subStatusCode"].Value); } if (actionElement.Attributes["statusReason"] != null) statusReason = actionElement.Attributes["statusReason"].Value; if (actionElement.Attributes["statusDescription"] != null) { statusDescription = actionElement.Attributes["statusDescription"].Value; } if (string.IsNullOrEmpty(url)) { throw new RuleSetException("Action URL must be a non-empty value."); } // validationType="requireTrimmedString" url = url.Trim(); if (type == ActionType.Redirect) { ruleFlags.Add(new RedirectFlag((int)redirectType)); } else if (statusCode > 0U) { // validationType="integerRange" validationParameter="300,307,exclude" if (statusCode >= 300U && statusCode <= 307U) { throw new RuleSetException("Action Status Code should not be an int between 300 - 307, use the redirectType for this range."); } if (statusCode < 1U || statusCode > 999U) { throw new RuleSetException("Action Status Code should be between 1 - 999."); } if (subStatusCode < 0U || subStatusCode > 999U) { throw new RuleSetException("Action Sub Status Code should be between 0 - 999."); } ruleFlags.Add(new ResponseStatusFlag(statusCode, subStatusCode, statusReason, statusDescription)); } IRuleAction substitution = new DefaultRuleAction(); substitution.Init(pattern, url); return substitution; }
/// <summary> /// Initializes a new instance of the <see cref="DefaultRule"/> class. /// </summary> /// <param name="pattern">The pattern.</param> /// <param name="substitution">The substitution.</param> /// <param name="flags">The flags.</param> public DefaultRule(string pattern, string substitution, IRuleFlagProcessor flags) : this(null, pattern, substitution, Manager.RuleOptions, flags) { }
/// <summary> /// Inits the specified conditions. /// </summary> /// <param name="conditions">The conditions.</param> /// <param name="action">The action.</param> /// <param name="flags">The flags.</param> void IRule.Init(IEnumerable<ICondition> conditions, IRuleAction action, IRuleFlagProcessor flags) { _conditions = new List<ICondition>(conditions ?? new ICondition[0]); _action = action; _flags = flags ?? new RuleFlagProcessor(); }
/// <summary> /// Initializes a new instance of the <see cref="DefaultRule"/> class. /// </summary> /// <param name="conditions">The conditions.</param> /// <param name="action">The action.</param> /// <param name="flags">The flags.</param> public DefaultRule(IEnumerable <ICondition> conditions, IRuleAction action, IRuleFlagProcessor flags) { ((IRule)this).Init(conditions, action, flags); }
/// <summary> /// Initializes a new instance of the <see cref="DefaultRule"/> class. /// </summary> /// <param name="pattern">The pattern.</param> /// <param name="substitution">The substitution.</param> /// <param name="flags">The flags.</param> public DefaultRule(string pattern, string substitution, IRuleFlagProcessor flags) : this(null, pattern, substitution, UrlRewriterManager.RuleOptions, flags) { }
/// <summary> /// Determines whether [has no escape] [the specified flags]. /// </summary> /// <param name="flags">The flags.</param> /// <returns> /// <see langword="true"/> if [has no escape] [the specified flags]; otherwise, <see langword="false"/>. /// </returns> public static bool HasNoEscape(IRuleFlagProcessor flags) { foreach (IRuleFlag flag in flags) if (flag is NoEscapeFlag) return true; return false; }
/// <summary> /// Determines whether [has not for internal sub requests] [the specified flags]. /// </summary> /// <param name="flags">The flags.</param> /// <returns> /// <see langword="true"/> if [has not for internal sub requests] [the specified flags]; otherwise, <see langword="false"/>. /// </returns> public static bool HasNotForInternalSubRequests(IRuleFlagProcessor flags) { foreach (IRuleFlag flag in flags) if (flag is NotForInternalSubRequestsFlag) return true; return false; }
/// <summary> /// Initializes a new instance of the <see cref="DefaultRule"/> class. /// </summary> /// <param name="conditions">The conditions.</param> /// <param name="pattern">The pattern.</param> /// <param name="substitution">The substitution.</param> /// <param name="flags">The flags.</param> public DefaultRule(IEnumerable <ICondition> conditions, string pattern, string substitution, IRuleFlagProcessor flags) : this(conditions, pattern, substitution, Manager.RuleOptions, flags) { }