public void AddMatch( ParsedModRewriteInput input, Flags flags) { if (flags.HasFlag(FlagType.NoCase)) { _match = new RegexMatch(new Regex(input.Operand, RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase, _regexTimeout), input.Invert); } else { _match = new RegexMatch(new Regex(input.Operand, RegexOptions.CultureInvariant | RegexOptions.Compiled, _regexTimeout), input.Invert); } }
private static bool IsValidActionCondition(ParsedModRewriteInput results) { if (results.ConditionType == ConditionType.IntComp) { // If the type is an integer, verify operand is actually an int int res; if (!int.TryParse(results.Operand, NumberStyles.None, CultureInfo.InvariantCulture, out res)) { return(false); } } return(true); }
public void AddConditionFromParts( Pattern pattern, ParsedModRewriteInput input, Flags flags) { if (_conditions == null) { _conditions = new List <Condition>(); } var condition = new Condition(); condition.OrNext = flags.HasFlag(FlagType.Or); condition.Input = pattern; switch (input.ConditionType) { case ConditionType.Regex: if (flags.HasFlag(FlagType.NoCase)) { condition.Match = new RegexMatch(new Regex(input.Operand, RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase, _regexTimeout), input.Invert); } else { condition.Match = new RegexMatch(new Regex(input.Operand, RegexOptions.CultureInvariant | RegexOptions.Compiled, _regexTimeout), input.Invert); } break; case ConditionType.IntComp: switch (input.OperationType) { case OperationType.Equal: condition.Match = new IntegerMatch(input.Operand, IntegerOperationType.Equal); break; case OperationType.Greater: condition.Match = new IntegerMatch(input.Operand, IntegerOperationType.Greater); break; case OperationType.GreaterEqual: condition.Match = new IntegerMatch(input.Operand, IntegerOperationType.GreaterEqual); break; case OperationType.Less: condition.Match = new IntegerMatch(input.Operand, IntegerOperationType.Less); break; case OperationType.LessEqual: condition.Match = new IntegerMatch(input.Operand, IntegerOperationType.LessEqual); break; case OperationType.NotEqual: condition.Match = new IntegerMatch(input.Operand, IntegerOperationType.NotEqual); break; default: throw new ArgumentException("Invalid operation for integer comparison."); } break; case ConditionType.StringComp: switch (input.OperationType) { case OperationType.Equal: condition.Match = new StringMatch(input.Operand, StringOperationType.Equal, input.Invert); break; case OperationType.Greater: condition.Match = new StringMatch(input.Operand, StringOperationType.Greater, input.Invert); break; case OperationType.GreaterEqual: condition.Match = new StringMatch(input.Operand, StringOperationType.GreaterEqual, input.Invert); break; case OperationType.Less: condition.Match = new StringMatch(input.Operand, StringOperationType.Less, input.Invert); break; case OperationType.LessEqual: condition.Match = new StringMatch(input.Operand, StringOperationType.LessEqual, input.Invert); break; default: throw new ArgumentException("Invalid operation for string comparison."); } break; default: switch (input.OperationType) { case OperationType.Directory: condition.Match = new IsDirectoryMatch(input.Invert); break; case OperationType.RegularFile: condition.Match = new IsFileMatch(input.Invert); break; case OperationType.ExistingFile: condition.Match = new IsFileMatch(input.Invert); break; case OperationType.SymbolicLink: // TODO see if FileAttributes.ReparsePoint works for this? throw new NotImplementedException("Symbolic links are not supported because " + "of cross platform implementation"); case OperationType.Size: condition.Match = new FileSizeMatch(input.Invert); break; case OperationType.ExistingUrl: throw new NotSupportedException("Existing Url lookups not supported because it requires a subrequest"); case OperationType.Executable: throw new NotSupportedException("Executable Property is not supported because Windows " + "requires a pinvoke to get this property"); default: throw new ArgumentException("Invalid operation for property comparison"); } break; } _conditions.Add(condition); }
/// <summary> /// Given a CondPattern, create a ParsedConditionExpression, containing the type of operation /// and value. /// ParsedConditionExpression is an intermediary object, which will be made into a ConditionExpression /// once the flags are parsed. /// </summary> /// <param name="condition">The CondPattern portion of a mod_rewrite RewriteCond.</param> /// <returns>A new parsed condition.</returns> public ParsedModRewriteInput ParseActionCondition(string condition) { if (condition == null) { condition = string.Empty; } var context = new ParserContext(condition); var results = new ParsedModRewriteInput(); if (!context.Next()) { throw new FormatException(Resources.FormatError_InputParserUnrecognizedParameter(condition, context.Index)); } // If we hit a !, invert the condition if (context.Current == Not) { results.Invert = true; if (!context.Next()) { // Dangling ! throw new FormatException(Resources.FormatError_InputParserUnrecognizedParameter(condition, context.Index)); } } // Control Block for strings. Set the operation and type fields based on the sign // Switch on current character switch (context.Current) { case Greater: if (!context.Next()) { // Dangling ">" throw new FormatException(Resources.FormatError_InputParserUnrecognizedParameter(condition, context.Index)); } if (context.Current == EqualSign) { if (!context.Next()) { // Dangling ">=" throw new FormatException(Resources.FormatError_InputParserUnrecognizedParameter(condition, context.Index)); } results.OperationType = OperationType.GreaterEqual; results.ConditionType = ConditionType.StringComp; } else { results.OperationType = OperationType.Greater; results.ConditionType = ConditionType.StringComp; } break; case Less: if (!context.Next()) { // Dangling "<" throw new FormatException(Resources.FormatError_InputParserUnrecognizedParameter(condition, context.Index)); } if (context.Current == EqualSign) { if (!context.Next()) { // Dangling "<=" throw new FormatException(Resources.FormatError_InputParserUnrecognizedParameter(condition, context.Index)); } results.OperationType = OperationType.LessEqual; results.ConditionType = ConditionType.StringComp; } else { results.OperationType = OperationType.Less; results.ConditionType = ConditionType.StringComp; } break; case EqualSign: if (!context.Next()) { // Dangling "=" throw new FormatException(Resources.FormatError_InputParserUnrecognizedParameter(condition, context.Index)); } results.OperationType = OperationType.Equal; results.ConditionType = ConditionType.StringComp; break; case Dash: results = ParseProperty(context, results.Invert); if (results.ConditionType == ConditionType.PropertyTest) { return(results); } context.Next(); break; default: results.ConditionType = ConditionType.Regex; break; } // Capture the rest of the string guarantee validity. results.Operand = condition.Substring(context.GetIndex()); if (IsValidActionCondition(results)) { return(results); } else { throw new FormatException(Resources.FormatError_InputParserUnrecognizedParameter(condition, context.Index)); } }