public void ProcessRuleReplacementsWithMatchTags()
        {
            // arrange
            var responseString = OutboundRewriterTestData.ProcessRuleReplacementsWithMatchTagsInput;
            var outboundRule = new OutboundRule
            {
                OutboundMatchScope = new MatchResponseTags
                {
                    MatchTheContentWithin = new List<MatchTag>
                    {
                        new MatchTag {Tag = "a", Attribute = "href"}
                    }
                },
                Pattern = @"/article\.aspx\?id=([0-9]+)(?:&|&amp;)title=([_0-9a-z-]+)$",
                Action = new OutboundRewrite()
                {
                    Value = @"/article/{R:1}/{R:2}"
                },
                MatchingScopeType = ScopeType.Response
            };

            // act
            var output = OutboundRewriter.ProcessRuleReplacements(responseString, outboundRule);

            // assert
            Assert.AreEqual(output, OutboundRewriterTestData.ProcessRuleReplacementsWithMatchTagsExpectedOutput);
        }
示例#2
0
        private OutboundRuleResult ProcessOutboundRule(HttpContextBase httpContext, string responseString, OutboundRule outboundRule)
        {
            //Log.Debug(this, "Processing inbound rule - requestUri: {0} inboundRule: {1}", originalUri, inboundRule.Name);

            var ruleResult = new OutboundRuleResult()
            {
                OriginalResponseString = responseString,
                RewrittenResponseString = responseString
            };

            switch (outboundRule.Using)
            {
                case Using.ExactMatch:
                case Using.RegularExpressions:
                case Using.Wildcards:
                    ruleResult = ProcessRegularExpressionOutboundRule(ruleResult, outboundRule);

                    break;
                //case Using.Wildcards:
                //    //TODO: Implement Wildcards
                //    throw new NotImplementedException("Using Wildcards has not been implemented");
                //    break;
            }

            //Log.Debug(this, "Processing inbound rule - requestUri: {0} inboundRule: {1} rewrittenUrl: {2}", ruleResult.OriginalUri, inboundRule.Name, ruleResult.RewrittenUri);

            //ruleResult.ItemId = inboundRule.ItemId;

            return ruleResult;
        }
示例#3
0
        public static OutboundRule ToOutboundRule(this OutboundRuleItem outboundRuleItem)
        {
            if (outboundRuleItem == null) return null;

            var conditionItems = GetBaseConditionItems(outboundRuleItem);

            var outboundRule = new OutboundRule
            {
                ItemId = outboundRuleItem.ID.Guid,
                Name = outboundRuleItem.Name
            };

            SetBaseRule(outboundRuleItem.BaseRuleItem, outboundRule);

            SetOutboundMatch(outboundRuleItem.OutboundMatchItem, outboundRule);

            GetPrecondition(outboundRuleItem.OutboundPreconditionItem, outboundRule);

            if (string.IsNullOrEmpty(outboundRuleItem.BaseRuleItem.BaseMatchItem.MatchPatternItem.Pattern.Value))
            {
                Log.Warn(logObject, outboundRuleItem.Database, "No pattern set on rule with ItemID: {0}", outboundRuleItem.ID);

                return null;
            }

            if (outboundRuleItem.Action == null)
            {
                Log.Warn(logObject, outboundRuleItem.Database, "No action set on rule with ItemID: {0}", outboundRuleItem.ID);

                return null;
            }

            var baseActionItem = outboundRuleItem.Action.TargetItem;
            IBaseAction baseAction = null;

            if (baseActionItem != null)
            {
                var baseActionItemTemplateId = baseActionItem.TemplateID.ToString();

                if (baseActionItemTemplateId.Equals(OutboundRewriteItem.TemplateId, StringComparison.InvariantCultureIgnoreCase))
                {
                    baseAction = new OutboundRewriteItem(baseActionItem).ToOutboundRewriteAction();
                }
            }
            outboundRule.Action = baseAction;

            if (conditionItems != null)
            {
                SetConditions(conditionItems, outboundRule);
            }

            return outboundRule;
        }
        public void ProcessRuleReplacementsWithNoMatchTags()
        {
            // arrange
            var responseString = OutboundRewriterTestData.ProcessRuleReplacementsWithMatchTagsInput;
            var outboundRule = new OutboundRule
            {
                Pattern = @"/article\.aspx\?id=([0-9]+)(?:&|&amp;)title=([_0-9a-z-]+)",
                Action = new OutboundRewrite()
                {
                    Value = @"/article/{R:1}/{R:2}"
                },
                MatchingScopeType = ScopeType.Response
            };

            // act
            var output = OutboundRewriter.ProcessRuleReplacements(responseString, outboundRule);

            // assert
            Assert.AreEqual(output, OutboundRewriterTestData.ProcessRuleReplacementsWithMatchTagsExpectedOutput);
        }
        public void ProcessRuleReplacementsWithExactMatch()
        {
            // arrange
            var responseString = OutboundRewriterTestData.ProcessRuleReplacementsWithMatchTagsInput;
            var outboundRule = new OutboundRule
            {
                Using = Using.ExactMatch,
                Pattern = @"</body>",
                Action = new OutboundRewrite()
                {
                    Value = @"<script type='text/javascript'>//Your web analytics tracking code goes here...</script></body>"
                },
                MatchingScopeType = ScopeType.Response
            };

            // act
            var output = OutboundRewriter.ProcessRuleReplacements(responseString, outboundRule);

            // assert
            Assert.AreEqual(output, OutboundRewriterTestData.ProcessRuleReplacementsWithExactMatchExpectedOutput);
        }
示例#6
0
        private static void GetPrecondition(OutboundPreconditionItem outboundPreconditionItem, OutboundRule outboundRule)
        {
            if (outboundPreconditionItem == null || outboundPreconditionItem.Precondition == null ||
                outboundPreconditionItem.Precondition.TargetItem == null) return;

            var preconditionTargetItem = outboundPreconditionItem.Precondition.TargetItem;
            var preconditionItem = new PreconditionItem(preconditionTargetItem);

            var precondition = new Precondition
            {
                Name = preconditionItem.Name,
            };

            var conditionItems = GetBaseConditionItems(preconditionItem);
            if (conditionItems != null)
            {
                SetConditions(conditionItems, precondition);
            }

            var usingItem = preconditionItem.PreconditionUsingItem.Using.TargetItem;
            Using? usingType = null;
            if (usingItem != null)
            {
                var usingItemId = usingItem.ID.ToString();
                switch (usingItemId)
                {
                    case Constants.UsingType_RegularExpressions_ItemId:
                        usingType = Using.RegularExpressions;
                        break;
                    case Constants.UsingType_Wildcards_ItemId:
                        usingType = Using.Wildcards;
                        break;
                    case Constants.UsingType_ExactMatch_ItemId:
                        usingType = Using.ExactMatch;
                        break;
                    default:
                        break;
                }
            }
            precondition.Using = usingType;

            SetConditionLogicalGrouping(preconditionItem.ConditionLogicalGroupingItem, precondition);

            outboundRule.Precondition = precondition;
        }
示例#7
0
        internal bool CheckPrecondition(HttpContextBase httpContext, OutboundRule outboundRule)
        {

            Match lastConditionMatch = null;
            bool isPreconditionMatch = true;

            if (outboundRule == null) return isPreconditionMatch;
            var precondition = outboundRule.Precondition;

            if (precondition == null) return isPreconditionMatch;
            var conditions = precondition.Conditions;

            // test conditions matches
            if (conditions != null && conditions.Any())
            {
                var replacements = new RewriteHelper.Replacements
                {
                    RequestServerVariables = httpContext.Request.ServerVariables,
                    RequestHeaders = httpContext.Request.Headers,
                    ResponseHeaders = httpContext.Response.Headers
                };
                var conditionMatchResult = RewriteHelper.TestConditionMatches(precondition, replacements, out lastConditionMatch);
                isPreconditionMatch = conditionMatchResult.Matched;
            }

            return isPreconditionMatch;
        }
示例#8
0
        public static string ProcessRuleReplacements(string responseString, OutboundRule outboundRule)
        {
            string output = null;
            var rewritePattern = outboundRule.Pattern;
            // TODO: Not all actions will be OutboundRewriteActions - fix this
            var rewrite = ((OutboundRewrite)outboundRule.Action);
            var rewriteValue = rewrite.Value;
            var rewriteMatchScope = outboundRule.OutboundMatchScope;
            var rewriteMatchScopeType = outboundRule.MatchingScopeType;

            // TODO: catch invalid Regex compilations

            if (rewriteMatchScopeType == ScopeType.Response)
            {
                IEnumerable<MatchTag> matchTags = new List<MatchTag>();

                if (rewriteMatchScope is MatchResponseTags)
                {
                    var matchResponseTags = rewriteMatchScope as MatchResponseTags;
                    matchTags = matchResponseTags.MatchTheContentWithin ?? new List<MatchTag>();
                }

                // if we are not matching on match tags, then we are doing matching on the entire response
                if (matchTags.Any())
                {
                    output = ProcessRuleReplacementsWithMatchTags(responseString, outboundRule.Using, matchTags,
                        rewritePattern, rewriteValue);
                }
                else
                {
                    if (outboundRule.Using == Using.ExactMatch)
                    {
                        output = responseString.Replace(rewritePattern, rewriteValue);
                    }
                    else
                    {
                        var responseRegex = new Regex(rewritePattern);

                        output = responseRegex.Replace(responseString,
                            match => RewriteHelper.ReplaceRuleBackReferences(match, rewriteValue));
                    }
                }
            }
            else if (rewriteMatchScopeType == ScopeType.ServerVariables)
            {

            }

            return output;
        }
示例#9
0
        private OutboundRuleResult ProcessRegularExpressionOutboundRule(OutboundRuleResult ruleResult, OutboundRule outboundRule)
        {
            Match outboundRuleMatch,
                lastConditionMatch = null;

            // test rule match
            var isRuleMatch = true;
            ConditionMatchResult conditionMatchResult = null;

            // test conditions matches
            if (outboundRule.Conditions != null && outboundRule.Conditions.Any())
            {
                var replacements = new RewriteHelper.Replacements
                {
                    RequestHeaders = RequestHeaders,
                    RequestServerVariables = RequestServerVariables,
                    ResponseHeaders = ResponseHeaders
                };

                conditionMatchResult = RewriteHelper.TestConditionMatches(outboundRule, replacements, out lastConditionMatch);
                isRuleMatch = conditionMatchResult.Matched;
            }

            if (isRuleMatch)
            {
                ruleResult.RewrittenResponseString = ProcessRuleReplacements(ruleResult.OriginalResponseString, outboundRule);
                ruleResult.RuleMatched = true;
            }

            return ruleResult;
        }