Пример #1
0
        static void ParseAndApplyFlags(string[] lineTokens, RewriteRule rule)
        {
            if (lineTokens.Length != 4)
            {
                return;
            }

            var flagStr = lineTokens[3].ToLowerInvariant();

            flagStr = flagStr.Trim('[', ']');
            var flagArray = flagStr.Split(',');

            foreach (var flag in flagArray)
            {
                switch (flag)
                {
                case "f":
                case "forbidden":
                    rule.StatusCode = HttpStatusCode.Forbidden;
                    continue;

                case "g":
                case "gone":
                    rule.StatusCode = HttpStatusCode.Gone;
                    continue;

                case "nc":
                case "nocase":
                    rule.Flags |= RewriteRuleFlags.CaseInsensitive;
                    continue;

                case "qsa":
                case "qsappend":
                    rule.Flags |= RewriteRuleFlags.QueryStringAppend;
                    continue;

                case "qsd":
                case "qsdrop":
                    rule.Flags |= RewriteRuleFlags.QueryStringDrop;
                    continue;

                case "permanent":
                    rule.StatusCode = HttpStatusCode.MovedPermanently;
                    continue;

                case "seeother":
                    rule.StatusCode = HttpStatusCode.SeeOther;
                    continue;

                case "temp":
                    rule.StatusCode = HttpStatusCode.TemporaryRedirect;
                    continue;
                }

                if (flag.StartsWith("r="))
                {
                    rule.StatusCode = (HttpStatusCode)int.Parse(flag.Substring(2));
                }
            }
        }
Пример #2
0
        public static RewriteRule Parse(string line, string[] lineTokens, RewriteOptions options)
        {
            var rewriteRule = new RewriteRule
            {
                RewrittenUrl = lineTokens[2],
                Source       = line,
                Options      = options,
                StatusCode   = options.DefaultResponseStatusCode
            };

            ParseAndApplyFlags(lineTokens, rewriteRule);

            var regexOptions = rewriteRule.Flags.HasFlag(RewriteRuleFlags.CaseInsensitive) || options.GlobalCaseInsensitivity ? RegexOptions.IgnoreCase : RegexOptions.None;

            rewriteRule.PathRegex = new Regex(lineTokens[1], regexOptions, TimeSpan.FromMilliseconds(100));


            return(rewriteRule);
        }
Пример #3
0
        public static IEnumerable <Ruleset> Parse(string rules)
        {
            var lines = rules.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);

            var rulesets = new List <Ruleset>();

            var ruleset = new Ruleset();

            var options = new RewriteOptions();

            foreach (var line in lines)
            {
                try
                {
                    var tokens = line.Trim().Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                    if (tokens[0] == "RewriteCond")
                    {
                        ruleset.Conditions.Add(RewriteCond.Parse(line, tokens, options));
                    }
                    else if (tokens[0] == "RewriteRule")
                    {
                        ruleset.Rule = RewriteRule.Parse(line, tokens, options);

                        rulesets.Add(ruleset);
                        ruleset = new Ruleset();
                    }
                    else if (tokens[0] == "RewriteOptions")
                    {
                        options = RewriteOptions.Parse(line, tokens);
                    }
                }
                catch
                {
                }
            }

            return(rulesets);
        }