Esempio n. 1
0
        public void ProcessRequestUrlWithCustomResponse()
        {
            var rewriter       = new InboundRewriter();
            var newInboundRule = new InboundRule()
            {
                Name    = "Custom Response Rule",
                Pattern = "customresponse",
                Using   = Using.ExactMatch,
                Action  = new CustomResponse()
                {
                    Name             = "Custom Response Action",
                    StatusCode       = 550,
                    SubStatusCode    = 100,
                    ErrorDescription = "Custom Response Because I Said So",
                    Reason           = "Custom Response 550"
                },
                MatchType = MatchType.MatchesThePattern
            };

            InboundRules.Insert(0, newInboundRule);

            var rewriteResult  = rewriter.ProcessRequestUrl(new Uri("http://fictitioussite.com/customresponse"), InboundRules);
            var customResponse = rewriteResult.FinalAction as CustomResponse;

            Assert.IsNotNull(customResponse);
            Assert.AreEqual(customResponse.StatusCode, 550);
            Assert.AreEqual(customResponse.SubStatusCode, 100);
            Assert.IsTrue(rewriteResult.ProcessedResults.Count == 1);
        }
Esempio n. 2
0
        public void ProcessRequestUrlWithMultipleConditionMatchBackReferences()
        {
            var serverVariables = new NameValueCollection
            {
                { "HTTP_HOST", "fictitioussite.com" },
                { "QUERY_STRING", "var1=1&var2=2" }
            };
            var rewriter = new InboundRewriter(serverVariables, null);

            InboundRules = new List <InboundRule>()
            {
                new InboundRule()
                {
                    Name    = "Multiple Variable Rules",
                    Pattern = "^(.*)$",
                    Using   = Using.RegularExpressions,
                    Action  = new Redirect()
                    {
                        Name = "Redirect to C1 and C2",
                        AppendQueryString = false,
                        HttpCacheability  = HttpCacheability.NoCache,
                        StatusCode        = RedirectStatusCode.Permanent,
                        RewriteUrl        = "http://{HTTP_HOST}/newpage/{C:1}/{C:2}"
                    },
                    MatchType = MatchType.MatchesThePattern,
                    ConditionLogicalGrouping = LogicalGrouping.MatchAll,
                    Conditions = new List <Condition>()
                    {
                        new Condition()
                        {
                            Name = "C1",
                            CheckIfInputString = CheckIfInputString.MatchesThePattern,
                            InputString        = Tokens.QUERY_STRING.Formatted(),
                            Pattern            = @"(?:^|&)var1=(\d+)(?:&|$)",
                            IgnoreCase         = true
                        },
                        new Condition()
                        {
                            Name = "C2",
                            CheckIfInputString = CheckIfInputString.MatchesThePattern,
                            InputString        = "%{C:1}%_{QUERY_STRING}",
                            Pattern            = @"%(.+)%_.*var2=(\d+)(?:&|$)",
                            IgnoreCase         = true
                        }
                    }
                }
            };

            var rewriteResult = rewriter.ProcessRequestUrl(new Uri("http://fictitioussite.com/?var1=1&var2=2"), InboundRules);

            var expectedUrl = new Uri("http://fictitioussite.com/newpage/1/2");

            Assert.AreEqual(expectedUrl, rewriteResult.RewrittenUri);
        }
Esempio n. 3
0
        public void ProcessRequestUrlWithHttpsToHttp()
        {
            var serverVariables = new NameValueCollection
            {
                { "HTTP_HOST", "www.secured.com" },
                { "HTTPS", "on" }
            };
            var rewriter = new InboundRewriter(serverVariables, null);

            InboundRules = new List <InboundRule>()
            {
                new InboundRule()
                {
                    Name   = "Redirect HTTPS to HTTP",
                    Action = new Redirect()
                    {
                        Name = "Redirect",
                        AppendQueryString = true,
                        StatusCode        = RedirectStatusCode.Permanent,
                        HttpCacheability  = HttpCacheability.NoCache,
                        StopProcessingOfSubsequentRules = true,
                        RewriteUrl = "http://{HTTP_HOST}/{R:1}"
                    },
                    Conditions = new List <Condition>()
                    {
                        new Condition()
                        {
                            Name = "HTTPS",
                            CheckIfInputString = CheckIfInputString.DoesNotMatchThePattern,
                            IgnoreCase         = true,
                            InputString        = "{HTTPS}",
                            Pattern            = "off"
                        }
                    },
                    Enabled    = true,
                    IgnoreCase = true,
                    Pattern    = "(.*)",
                    ConditionLogicalGrouping = LogicalGrouping.MatchAll,
                    Using     = Using.RegularExpressions,
                    MatchType = MatchType.MatchesThePattern
                }
            };

            var inputUrl    = "www.secured.com/blah/asdfadsf.htm?test=test";
            var inputUri    = new Uri("https://" + inputUrl);
            var result      = rewriter.ProcessRequestUrl(inputUri, InboundRules);
            var expectedUri = new Uri("http://" + inputUrl);

            var rewrittenUri = result.RewrittenUri;

            Assert.IsNotNull(rewrittenUri);
            Assert.AreEqual(rewrittenUri, expectedUri);
        }
Esempio n. 4
0
        public void ProcessRequestUrlWithHttpHostReplacement()
        {
            var serverVariables = new NameValueCollection
            {
                { "HTTP_HOST", "fictitioussite.com" }
            };
            var rewriter      = new InboundRewriter(serverVariables, null);
            var rewriteResult = rewriter.ProcessRequestUrl(new Uri("http://fictitioussite.com/hostreplacement"), InboundRules);

            var expectedUri = new Uri("http://fictitioussite.com/hostreplaced");

            Assert.AreEqual(expectedUri, rewriteResult.RewrittenUri);
        }
Esempio n. 5
0
        public void ProcessRequestUrlWithCaptureGroups()
        {
            var serverVariables = new NameValueCollection
            {
                { "HTTP_HOST", "fictitioussite.com" }
            };
            var rewriter      = new InboundRewriter(serverVariables, null);
            var rewriteResult = rewriter.ProcessRequestUrl(new Uri("http://fictitioussite.com/article/1/2"), InboundRules);

            var expectedUri = new Uri("http://fictitioussite.com/article.aspx?id=1&amp;title=2");

            Assert.AreEqual(expectedUri, rewriteResult.RewrittenUri);
        }
Esempio n. 6
0
        public void ProcessRequestUrlTest()
        {
            var serverVariables = new NameValueCollection
            {
                { "HTTP_HOST", "fictitioussite.com" }
            };
            var rewriter      = new InboundRewriter(serverVariables, null);
            var rewriteResult = rewriter.ProcessRequestUrl(new Uri("http://fictitioussite.com/john"), InboundRules);

            var firstInboundRule = InboundRules.First();
            var actionToCompare  = firstInboundRule.Action as Redirect;
            var actionRewriteUrl = actionToCompare.RewriteUrl;
            var actionRewriteUri = new Uri(actionRewriteUrl);

            Assert.AreEqual(actionRewriteUri, rewriteResult.RewrittenUri);
        }
Esempio n. 7
0
        protected void Page_Load(object sender, EventArgs e)
        {
            using (new SecurityDisabler())
            {
                var urlRewriter = new InboundRewriter(Request.ServerVariables, Request.Headers);

                var db            = Sitecore.Data.Database.GetDatabase(Context.Items["urlrewrite:db"] as string);
                var requestResult = Context.Items["urlrewrite:result"] as ProcessInboundRulesResult;

                if (requestResult != null)
                {
                    //TODO: Curently this only reflects the result of Redirect Actions - make this call to logging reflect all action types
                    Hi.UrlRewrite.Log.Info(this, db, "Redirecting {0} to {1} [{2}]", requestResult.OriginalUri, requestResult.RewrittenUri,
                                           requestResult.StatusCode);

                    urlRewriter.ExecuteResult(new HttpContextWrapper(Context), requestResult);
                }
            }
        }
Esempio n. 8
0
        public void ProcessRequestUrlWithAbort()
        {
            var rewriter       = new InboundRewriter();
            var newInboundRule = new InboundRule()
            {
                Name    = "Abort Rule",
                Pattern = "^abort$",
                Using   = Using.RegularExpressions,
                Action  = new AbortRequest()
                {
                    Name = "Abort Action"
                },
                MatchType = MatchType.MatchesThePattern
            };

            InboundRules.Insert(1, newInboundRule);

            var rewriteResult = rewriter.ProcessRequestUrl(new Uri("http://fictitioussite.com/abort"), InboundRules);

            Assert.IsInstanceOfType(rewriteResult.FinalAction, typeof(AbortRequest));
            Assert.IsTrue(rewriteResult.ProcessedResults.Count == 2);
        }