Beispiel #1
0
        internal PreconditionResult CheckPreconditions(HttpContextBase httpContext, List <OutboundRule> outboundRules)
        {
            Match lastConditionMatch  = null;
            bool  isPreconditionMatch = false;

            var preconditions = outboundRules.Select(p => p.Precondition)
                                .Where(p => p != null);

            foreach (var precondition in preconditions)
            {
                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;
                }
            }

            var result = new PreconditionResult {
                Passed = isPreconditionMatch
            };

            return(result);
        }
Beispiel #2
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);
        }
Beispiel #3
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);
        }
Beispiel #4
0
        private InboundRuleResult ProcessRegularExpressionInboundRule(Uri originalUri, InboundRule inboundRule)
        {
            var ruleResult = new InboundRuleResult
            {
                OriginalUri  = originalUri,
                RewrittenUri = originalUri
            };

            Match inboundRuleMatch,
                  lastConditionMatch = null;

            // test rule match
            var isInboundRuleMatch = TestRuleMatches(inboundRule, originalUri, out inboundRuleMatch);
            ConditionMatchResult conditionMatchResult = null;

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

                conditionMatchResult = RewriteHelper.TestConditionMatches(inboundRule, replacements, out lastConditionMatch);
                isInboundRuleMatch   = conditionMatchResult.Matched;
            }

            // test site name restrictions
            if (isInboundRuleMatch && !string.IsNullOrEmpty(inboundRule.SiteNameRestriction))
            {
                isInboundRuleMatch = TestSiteNameRestriction(inboundRule);
            }

            if (isInboundRuleMatch && inboundRule.Action != null)
            {
                ruleResult.RuleMatched = true;

                if (inboundRule.ResponseHeaders.Any())
                {
                    ruleResult.ResponseHeaders = inboundRule.ResponseHeaders;
                }

                Log.Debug(this, "INBOUND RULE MATCH - requestUri: {0} inboundRule: {1}", originalUri, inboundRule.Name);

                // TODO: Need to implement Rewrite, None

                if (inboundRule.Action is Redirect)
                {
                    ProcessRedirectAction(inboundRule, originalUri, inboundRuleMatch, lastConditionMatch, ruleResult);
                }
                else if (inboundRule.Action is Rewrite)
                {
                    ProcessRewriteAction(inboundRule, originalUri, inboundRuleMatch, lastConditionMatch, ruleResult);
                }
                else if (inboundRule.Action is ItemQueryRedirect)
                {
                    ProcessItemQueryRedirectAction(inboundRule, originalUri, inboundRuleMatch, lastConditionMatch, ruleResult);
                }
                else if (inboundRule.Action is AbortRequest || inboundRule.Action is CustomResponse)
                {
                    ProcessActionProcessing(ruleResult);
                }
                else
                {
                    throw new NotImplementedException("Redirect Action, Custome Response and Abort Reqeust Action are the only supported type of redirects");
                }

                ruleResult.ResultAction         = inboundRule.Action;
                ruleResult.ConditionMatchResult = conditionMatchResult;
            }
            else if (inboundRule.Action == null)
            {
                Log.Warn(this, "Inbound Rule has no Action set - inboundRule: {0} inboundRule ItemId: {1}", inboundRule.Name, inboundRule.ItemId);

                // we are going to skip this because we don't know what to do with it during processing
                ruleResult.RuleMatched = false;
            }

            return(ruleResult);
        }