public ProcessInboundRulesResult GetUrlResults(string url, Database database, NameValueCollection serverVariables = null) { var rewriter = new InboundRewriter(); var rulesEngine = new RulesEngine(database); var inboundRules = rulesEngine.GetInboundRules(); var requestUri = new Uri(url); var siteContext = SiteContextFactory.GetSiteContext(requestUri.Host, requestUri.AbsolutePath, requestUri.Port); rewriter.RequestServerVariables = serverVariables ?? new NameValueCollection(); if (!rewriter.RequestServerVariables.AllKeys.Contains("HTTP_HOST")) { rewriter.RequestServerVariables.Add("HTTP_HOST", requestUri.Host); } if (!rewriter.RequestServerVariables.AllKeys.Contains("HTTPS")) { rewriter.RequestServerVariables.Add("HTTPS", requestUri.Scheme.Equals(Uri.UriSchemeHttps) ? "on" : "off"); } if (!rewriter.RequestServerVariables.AllKeys.Contains("QUERY_STRING") && requestUri.Query.Length > 0) { rewriter.RequestServerVariables.Add("QUERY_STRING", requestUri.Query.Remove(0, 1)); } var results = rewriter.ProcessRequestUrl(requestUri, inboundRules, siteContext, database); return(results); }
public override void Process(HttpRequestArgs args) { var db = Sitecore.Context.Database; try { if (args.Context == null || db == null) { return; } var httpContext = new HttpContextWrapper(args.Context); var requestUri = httpContext.Request.Url; if (requestUri == null || Configuration.IgnoreUrlPrefixes.Length > 0 && Configuration.IgnoreUrlPrefixes.Any(prefix => requestUri.PathAndQuery.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase))) { return; } var urlRewriter = new InboundRewriter(httpContext.Request.ServerVariables, httpContext.Request.Headers); var requestResult = ProcessUri(requestUri, db, urlRewriter); if (requestResult == null || !requestResult.MatchedAtLeastOneRule) { return; } httpContext.Items["urlrewrite:db"] = db.Name; httpContext.Items["urlrewrite:result"] = requestResult; var urlRewriterItem = Sitecore.Context.Database.GetItem(new ID(Constants.UrlRewriter_ItemId)); if (urlRewriterItem != null) { Sitecore.Context.Item = urlRewriterItem; } else { Log.Warn(this, db, "Unable to find UrlRewriter item {0}.", Constants.UrlRewriter_ItemId); } //Check for static extension and then process if (args.CustomData.ContainsKey(Constants.StaticExtensionKey) && requestResult.MatchedAtLeastOneRule) { HandleSpecialExtensions(httpContext, requestResult); } } catch (ThreadAbortException) { // swallow this exception because we may have called Response.End } catch (Exception ex) { if (ex is ThreadAbortException) { return; } Log.Error(this, ex, db, "Exception occured"); } }
public ProcessInboundRulesResult GetUrlResults(string url, Database database, NameValueCollection serverVariables = null) { var rewriter = new InboundRewriter(); var rulesEngine = new RulesEngine(database); var inboundRules = rulesEngine.GetInboundRules(); var requestUri = new Uri(url); var siteContext = SiteContextFactory.GetSiteContext(requestUri.Host, requestUri.AbsolutePath, requestUri.Port); rewriter.RequestServerVariables = serverVariables ?? new NameValueCollection(); if (!rewriter.RequestServerVariables.AllKeys.Contains("HTTP_HOST")) { rewriter.RequestServerVariables.Add("HTTP_HOST", requestUri.Host); } if (!rewriter.RequestServerVariables.AllKeys.Contains("HTTPS")) { rewriter.RequestServerVariables.Add("HTTPS", requestUri.Scheme.Equals(Uri.UriSchemeHttps) ? "on" : "off"); } if (!rewriter.RequestServerVariables.AllKeys.Contains("QUERY_STRING") && requestUri.Query.Length > 0) { rewriter.RequestServerVariables.Add("QUERY_STRING", requestUri.Query.Remove(0, 1)); } var results = rewriter.ProcessRequestUrl(requestUri, inboundRules, siteContext, database); return results; }
internal ProcessInboundRulesResult ProcessUri(Uri requestUri, Database db, InboundRewriter urlRewriter) { var inboundRules = GetInboundRules(db); if (inboundRules == null) { return(null); } return(urlRewriter.ProcessRequestUrl(requestUri, inboundRules)); }
internal ProcessInboundRulesResult ProcessUri(Uri requestUri, Database db, InboundRewriter urlRewriter) { var inboundRules = GetInboundRules(db); if (inboundRules == null) { return null; } return urlRewriter.ProcessRequestUrl(requestUri, inboundRules); }
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&title=2"); Assert.AreEqual(expectedUri, rewriteResult.RewrittenUri); }
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); }
public override void Process(HttpRequestArgs args) { var db = Sitecore.Data.Database.GetDatabase("web"); // Sitecore.Context.Database; Log.Info(this, db, "InboundRewriteProcessor Process"); try { if (args.Context == null || db == null) return; var httpContext = new HttpContextWrapper(args.Context); var requestUri = httpContext.Request.Url; if (requestUri == null || Configuration.IgnoreUrlPrefixes.Length > 0 && Configuration.IgnoreUrlPrefixes.Any(prefix => requestUri.PathAndQuery.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase))) { return; } var urlRewriter = new InboundRewriter(httpContext.Request.ServerVariables, httpContext.Request.Headers); Log.Info(this, db, "InboundRewriteProcessor Process ProcessUri {0}", requestUri); var requestResult = ProcessUri(requestUri, db, urlRewriter); if (requestResult == null || !requestResult.MatchedAtLeastOneRule) return; httpContext.Items["urlrewrite:db"] = db.Name; httpContext.Items["urlrewrite:result"] = requestResult; var urlRewriterItem = Sitecore.Context.Database.GetItem(new ID(Constants.UrlRewriter_ItemId)); if (urlRewriterItem != null) { Sitecore.Context.Item = urlRewriterItem; } else { Log.Warn(this, db, "Unable to find UrlRewriter item {0}.", Constants.UrlRewriter_ItemId); } } catch (ThreadAbortException) { // swallow this exception because we may have called Response.End } catch (Exception ex) { if (ex is ThreadAbortException) return; Log.Error(this, ex, db, "Exception occured"); } }
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); }
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); } } }
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); }
public override void Process(HttpRequestArgs args) { var db = Sitecore.Context.Database; try { if (args.Context == null || db == null) return; var httpContext = new HttpContextWrapper(args.Context); var requestUri = httpContext.Request.Url; if (requestUri == null || Configuration.IgnoreUrlPrefixes.Length > 0 && Configuration.IgnoreUrlPrefixes.Any(prefix => requestUri.PathAndQuery.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase))) { return; } var urlRewriter = new InboundRewriter(httpContext.Request.ServerVariables, httpContext.Request.Headers); var requestResult = ProcessUri(requestUri, db, urlRewriter); if (requestResult == null || !requestResult.MatchedAtLeastOneRule) return; //TODO: Curently this only reflects the result of Redirect Actions - make this call to logging reflect all action types Log.Info(this, db, "Redirecting {0} to {1} [{2}]", requestResult.OriginalUri, requestResult.RewrittenUri, requestResult.StatusCode); urlRewriter.ExecuteResult(httpContext, requestResult); } catch (ThreadAbortException) { // swallow this exception because we may have called Response.End } catch (Exception ex) { if (ex is ThreadAbortException) return; Log.Error(this, ex, db, "Exception occured"); } }
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); }
public override void Process(HttpRequestArgs args) { var db = Sitecore.Context.Database; try { if (args.Context == null || db == null) { return; } var httpContext = new HttpContextWrapper(args.Context); var uriBuilder = new UriBuilder(httpContext.Request.Url); bool?alreadyRun = httpContext.Items["urlrewrite:alreadyrun"] as bool?; if (!(alreadyRun.HasValue && alreadyRun.Value)) { if (Sitecore.Context.Data.FilePathLanguage != null) { uriBuilder.Path = "/" + Sitecore.Context.Data.FilePathLanguage.Name.ToLower() + uriBuilder.Path; } httpContext.Items["urlrewrite:alreadyrun"] = true as bool?; } var requestUri = uriBuilder.Uri; if (requestUri == null || Configuration.IgnoreUrlPrefixes.Length > 0 && Configuration.IgnoreUrlPrefixes.Any(prefix => requestUri.PathAndQuery.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase))) { return; } var urlRewriter = new InboundRewriter(httpContext.Request.ServerVariables, httpContext.Request.Headers); var requestResult = ProcessUri(requestUri, db, urlRewriter); if (requestResult == null || !requestResult.MatchedAtLeastOneRule) { return; } httpContext.Items["urlrewrite:db"] = db.Name; httpContext.Items["urlrewrite:result"] = requestResult; var urlRewriterItem = Sitecore.Context.Database.GetItem(new ID(Constants.UrlRewriter_ItemId)); if (urlRewriterItem != null) { Sitecore.Context.Item = urlRewriterItem; } else { Log.Warn(this, db, "Unable to find UrlRewriter item {0}.", Constants.UrlRewriter_ItemId); } return; } catch (ThreadAbortException) { // swallow this exception because we may have called Response.End } catch (Exception ex) { if (ex is ThreadAbortException) { return; } Log.Error(this, ex, db, "Exception occured"); } }
private static void HandleSpecialExtensions(HttpContextWrapper context, ProcessInboundRulesResult result) { var inboundRewriter = new InboundRewriter(); inboundRewriter.ExecuteResult(context, result, false); }
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); }
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); }