Exemplo n.º 1
0
 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);
 }
Exemplo n.º 2
0
 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);
     }
 }
Exemplo n.º 3
0
        /// <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));
            }
        }
Exemplo n.º 4
0
        public void AddConditionFromParts(
            Pattern pattern,
            ParsedModRewriteInput input,
            Flags flags)
        {
            if (_conditions == null)
            {
                _conditions = new List <Condition>();
            }

            var orNext = flags.HasFlag(FlagType.Or);

            UrlMatch match;

            switch (input.ConditionType)
            {
            case ConditionType.Regex:
                Debug.Assert(input.Operand != null);
                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);
                }
                break;

            case ConditionType.IntComp:
                Debug.Assert(input.Operand != null);
                switch (input.OperationType)
                {
                case OperationType.Equal:
                    match = new IntegerMatch(input.Operand, IntegerOperationType.Equal);
                    break;

                case OperationType.Greater:
                    match = new IntegerMatch(input.Operand, IntegerOperationType.Greater);
                    break;

                case OperationType.GreaterEqual:
                    match = new IntegerMatch(input.Operand, IntegerOperationType.GreaterEqual);
                    break;

                case OperationType.Less:
                    match = new IntegerMatch(input.Operand, IntegerOperationType.Less);
                    break;

                case OperationType.LessEqual:
                    match = new IntegerMatch(input.Operand, IntegerOperationType.LessEqual);
                    break;

                case OperationType.NotEqual:
                    match = new IntegerMatch(input.Operand, IntegerOperationType.NotEqual);
                    break;

                default:
                    throw new ArgumentException("Invalid operation for integer comparison.");
                }
                break;

            case ConditionType.StringComp:
                Debug.Assert(input.Operand != null);
                switch (input.OperationType)
                {
                case OperationType.Equal:
                    match = new StringMatch(input.Operand, StringOperationType.Equal, input.Invert);
                    break;

                case OperationType.Greater:
                    match = new StringMatch(input.Operand, StringOperationType.Greater, input.Invert);
                    break;

                case OperationType.GreaterEqual:
                    match = new StringMatch(input.Operand, StringOperationType.GreaterEqual, input.Invert);
                    break;

                case OperationType.Less:
                    match = new StringMatch(input.Operand, StringOperationType.Less, input.Invert);
                    break;

                case OperationType.LessEqual:
                    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:
                    match = new IsDirectoryMatch(input.Invert);
                    break;

                case OperationType.RegularFile:
                    match = new IsFileMatch(input.Invert);
                    break;

                case OperationType.ExistingFile:
                    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:
                    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;
            }

            var condition = new Condition(pattern, match, orNext);

            _conditions.Add(condition);
        }