Example #1
0
        private void ProcessRewriteAction(InboundRule inboundRule, Uri uri, Match inboundRuleMatch, Match lastConditionMatch, InboundRuleResult ruleResult)
        {
            var redirectAction = inboundRule.Action as Rewrite;

            var rewriteUrl        = redirectAction.RewriteUrl;
            var rewriteItemAnchor = redirectAction.RewriteItemAnchor;

            if (string.IsNullOrEmpty(rewriteUrl))
            {
                ruleResult.RuleMatched = false;
                return;
            }

            // process token replacements
            var replacements = new RewriteHelper.Replacements
            {
                RequestHeaders         = RequestHeaders,
                RequestServerVariables = RequestServerVariables
            };

            rewriteUrl = RewriteHelper.ReplaceTokens(replacements, rewriteUrl);

            if (redirectAction.AppendQueryString)
            {
                rewriteUrl += uri.Query;
            }

            rewriteUrl = RewriteHelper.ReplaceRuleBackReferences(inboundRuleMatch, rewriteUrl);
            rewriteUrl = RewriteHelper.ReplaceConditionBackReferences(lastConditionMatch, rewriteUrl);

            ruleResult.RewrittenUri   = new Uri(rewriteUrl);
            ruleResult.StopProcessing = redirectAction.StopProcessingOfSubsequentRules;
        }
Example #2
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);
        }
Example #3
0
        private void ProcessItemQueryRedirectAction(InboundRule inboundRule, Uri uri, Match inboundRuleMatch, Match lastConditionMatch, InboundRuleResult ruleResult)
        {
            var redirectAction = inboundRule.Action as ItemQueryRedirect;

            var itemQuery = redirectAction.ItemQuery;

            if (string.IsNullOrEmpty(itemQuery))
            {
                ruleResult.RuleMatched = false;
                return;
            }

            // process token replacements in the item query
            itemQuery = RewriteHelper.ReplaceRuleBackReferences(inboundRuleMatch, itemQuery);
            itemQuery = RewriteHelper.ReplaceConditionBackReferences(lastConditionMatch, itemQuery);

            var rewriteItemId = ExecuteItemQuery(itemQuery);

            if (!rewriteItemId.HasValue)
            {
                ruleResult.RuleMatched = false;
                return;
            }

            string rewriteUrl = GetRewriteUrlFromItemId(rewriteItemId.Value, null, null);


            // process token replacements
            var replacements = new RewriteHelper.Replacements
            {
                RequestHeaders         = RequestHeaders,
                RequestServerVariables = RequestServerVariables
            };

            rewriteUrl = RewriteHelper.ReplaceTokens(replacements, rewriteUrl);
            rewriteUrl = RewriteHelper.ReplaceRuleBackReferences(inboundRuleMatch, rewriteUrl);
            rewriteUrl = RewriteHelper.ReplaceConditionBackReferences(lastConditionMatch, rewriteUrl);

            ruleResult.RewrittenUri   = new Uri(rewriteUrl);
            ruleResult.StopProcessing = redirectAction.StopProcessingOfSubsequentRules;
        }
Example #4
0
        private static string ProcessRuleReplacementsWithMatchTags(string responseString, Using?outboundRuleUsing,
                                                                   IEnumerable <MatchTag> matchTags, string rewritePattern, string rewriteValue)
        {
            const string startKey      = "start";
            const string innerKey      = "inner";
            const string endKey        = "end";
            const string nameKey       = "name";
            const string startquoteKey = "startquote";
            const string valueKey      = "value";
            const string endquoteKey   = "endquote";

            const string tagPatternFormat =
                @"(?<" + startKey + @"><{0}\s+)(?<" + innerKey + @">.*?{1}=(?:""|').*?)(?<" + endKey + @">\s*/?>)";

            const string attributePatternFormat =
                @"(?<" + nameKey + @">{0}=)(?<" + startquoteKey + @">""|')(?<" + valueKey + @">.*?)(?<" +
                endquoteKey + @">""|')";

            var output = responseString;

            foreach (var matchTag in matchTags)
            {
                var tag        = matchTag.Tag;
                var attribute  = matchTag.Attribute;
                var tagPattern = string.Format(tagPatternFormat, tag, attribute);
                var tagRegex   = new Regex(tagPattern);

                output = tagRegex.Replace(responseString, tagMatch =>
                {
                    var tagMatchGroups   = tagMatch.Groups;
                    var tagStart         = tagMatchGroups[startKey].Value;
                    var tagInnards       = tagMatchGroups[innerKey].Value;
                    var tagEnd           = tagMatchGroups[endKey].Value;
                    var attributePattern = string.Format(attributePatternFormat, attribute);
                    var attributeRegex   = new Regex(attributePattern);

                    var newTagInnards = attributeRegex.Replace(tagInnards, attributeMatch =>
                    {
                        var attributeMatchGroups = attributeMatch.Groups;
                        var attributeValue       = attributeMatchGroups[valueKey].Value;

                        var attributeValueRegex = new Regex(rewritePattern);
                        var attributeValueMatch = attributeValueRegex.Match(attributeValue);

                        if (attributeValueMatch.Success)
                        {
                            var attributeName       = attributeMatchGroups[nameKey].Value;
                            var attributeStartQuote = attributeMatchGroups[startquoteKey].Value;
                            var attributeEndQuote   = attributeMatchGroups[endquoteKey].Value;

                            // need to determine where the match occurs within the original string
                            var attributeValueMatchIndex  = attributeValueMatch.Index;
                            var attributeValueMatchLength = attributeValueMatch.Length;
                            string attributeValueReplaced;

                            if (outboundRuleUsing == Using.ExactMatch)
                            {
                                attributeValueReplaced = attributeValueMatch.Value.Replace(
                                    attributeValueMatch.Value, rewriteValue);
                            }
                            else
                            {
                                attributeValueReplaced = RewriteHelper.ReplaceRuleBackReferences(attributeValueMatch,
                                                                                                 rewriteValue);
                            }

                            var newAttributeValue = attributeValue.Substring(0, attributeValueMatchIndex) +
                                                    attributeValueReplaced +
                                                    attributeValue.Substring(attributeValueMatchIndex +
                                                                             attributeValueMatchLength);

                            var attributeOutput = attributeName + attributeStartQuote + newAttributeValue +
                                                  attributeEndQuote;

                            return(attributeOutput);
                        }

                        return(attributeMatch.Value);
                    });

                    var tagOutput = tagStart + newTagInnards + tagEnd;

                    return(tagOutput);
                });
            }
            return(output);
        }