Permalink
Browse files

Allowed handling of bucketed items and special extensions via UrlRewr…

…iteHandler.

Example PDF handling in web.config:
<handlers>
 <add name="UrlRewritePdfHandler" path="*.pdf" verb="*" type="Hi.UrlRewrite.Processing.UrlRewriteHandler" resourceType="Unspecified" preCondition="integratedMode" />
</handlers>

Allowed handling of host redirects. Example: test.mysite.com/nicepath => www.mytarget.com
  • Loading branch information...
martinrayenglish committed Dec 9, 2016
1 parent 70d1085 commit d9b649d129b6b49ee7cf3f6beae3a8229750a152

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -53,6 +53,7 @@ public static class Constants
public const string UrlRewriter_ItemId = "{3CF68609-B1F2-4ADE-B7E3-91B5CF74F5B8}";
public const string RedirectEventItemId = "{1d668f23-eeba-4bd3-93b3-94861ed42060}";

public const string StaticExtensionKey = "StaticExtension";

}
}
@@ -50,7 +50,11 @@ public override void Process(HttpRequestArgs args)
Log.Warn(this, db, "Unable to find UrlRewriter item {0}.", Constants.UrlRewriter_ItemId);
}

return;
//Check for static extension and then process
if (args.CustomData.ContainsKey(Constants.StaticExtensionKey) && requestResult.MatchedAtLeastOneRule)
{
HandleSpecialExtensions(httpContext, requestResult);
}
}
catch (ThreadAbortException)
{
@@ -93,5 +97,11 @@ private List<InboundRule> GetInboundRules(Database db)

return inboundRules;
}

private static void HandleSpecialExtensions(HttpContextWrapper context, ProcessInboundRulesResult result)
{
var inboundRewriter = new InboundRewriter();
inboundRewriter.ExecuteResult(context, result, false);
}
}
}
@@ -101,7 +101,7 @@ public ProcessInboundRulesResult ProcessRequestUrl(Uri requestUri, List<InboundR
}
}

public void ExecuteResult(HttpContextBase httpContext, ProcessInboundRulesResult ruleResult)
public void ExecuteResult(HttpContextBase httpContext, ProcessInboundRulesResult ruleResult, bool trackRedirect = true)
{
var httpRequest = httpContext.Request;
var httpResponse = httpContext.Response;
@@ -123,7 +123,7 @@ public void ExecuteResult(HttpContextBase httpContext, ProcessInboundRulesResult

if (ruleResult.FinalAction is IBaseRedirect)
{
if (Configuration.AnalyticsTrackingEnabled)
if (Configuration.AnalyticsTrackingEnabled && trackRedirect)
{
Tracking.TrackRedirect(ruleResult);
}
@@ -186,6 +186,8 @@ public void ExecuteResult(HttpContextBase httpContext, ProcessInboundRulesResult

}

Log.Debug(this, "Processing inbound rule - requestUri: {0} rewrittenUrl: {1} status code: {2}", ruleResult.OriginalUri, ruleResult.RewrittenUri, httpResponse.StatusCode);

httpResponse.End();
}

@@ -233,7 +235,7 @@ private InboundRuleResult ProcessRegularExpressionInboundRule(Uri originalUri, I
lastConditionMatch = null;

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

// test conditions matches
@@ -303,13 +305,25 @@ private InboundRuleResult ProcessRegularExpressionInboundRule(Uri originalUri, I
return ruleResult;
}

private bool TestRuleMatches(InboundRule inboundRule, Uri originalUri, out Match inboundRuleMatch)
{
var absolutePath = originalUri.AbsolutePath;
var uriPath = absolutePath.Substring(1); // remove starting "/"
private bool TestAllRuleMatches(InboundRule inboundRule, Uri originalUri, out Match inboundRuleMatch)
{
var haveMatch = TestRuleMatches(inboundRule, originalUri, out inboundRuleMatch, false);

var escapedAbsolutePath = HttpUtility.UrlDecode(absolutePath);
var escapedUriPath = (escapedAbsolutePath ?? string.Empty).Substring(1); // remove starting "/"
//If no match, let's include the host and try again
if (!haveMatch)
{
haveMatch = TestRuleMatches(inboundRule, originalUri, out inboundRuleMatch, true);
}

return haveMatch;
}

private bool TestRuleMatches(InboundRule inboundRule, Uri originalUri, out Match inboundRuleMatch, bool includeHost)
{
var pathToCheck = includeHost ? originalUri.Host + originalUri.PathAndQuery : originalUri.AbsolutePath.Substring(1);

var escapedPath = HttpUtility.UrlDecode(pathToCheck);
//var escapedUriPath = includeHost ? escapedPath : (escapedPath ?? string.Empty).Substring(1); // remove starting "/"

// TODO : I have only implemented "MatchesThePattern" - need to implement the other types
var matchesThePattern = inboundRule.MatchType.HasValue &&
@@ -330,23 +344,23 @@ private bool TestRuleMatches(InboundRule inboundRule, Uri originalUri, out Match

var inboundRuleRegex = new Regex(pattern, inboundRule.IgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None);

inboundRuleMatch = inboundRuleRegex.Match(uriPath);
inboundRuleMatch = inboundRuleRegex.Match(pathToCheck);
bool isInboundRuleMatch = matchesThePattern ? inboundRuleMatch.Success : !inboundRuleMatch.Success;

Log.Debug(this, "Regex - Pattern: '{0}' Input: '{1}' Success: {2}", pattern, uriPath,
Log.Debug(this, "Regex - Pattern: '{0}' Input: '{1}' Success: {2}", pattern, pathToCheck,
isInboundRuleMatch);

if (!isInboundRuleMatch && !uriPath.Equals(escapedUriPath, StringComparison.InvariantCultureIgnoreCase))
if (!isInboundRuleMatch && !pathToCheck.Equals(escapedPath, StringComparison.InvariantCultureIgnoreCase))
{
inboundRuleMatch = inboundRuleRegex.Match(escapedUriPath);
inboundRuleMatch = inboundRuleRegex.Match(escapedPath);
isInboundRuleMatch = matchesThePattern ? inboundRuleMatch.Success : !inboundRuleMatch.Success;

Log.Debug(this, "Regex - Pattern: '{0}' Input: '{1}' Success: {2}", pattern, escapedUriPath,
Log.Debug(this, "Regex - Pattern: '{0}' Input: '{1}' Success: {2}", pattern, escapedPath,
isInboundRuleMatch);
}
return isInboundRuleMatch;
}

private bool TestSiteNameRestriction(InboundRule inboundRule)
{
var currentSiteName = Sitecore.Context.Site.Name;
@@ -88,7 +88,7 @@ private void AssembleRulesRecursive(Item ruleOrFolderItem, ref List<InboundRule>
rules.Add(inboundRule);
}
}
else if (ruleOrFolderItem.TemplateID == new ID(new Guid(RedirectSubFolderItem.TemplateId))
else if (ruleOrFolderItem.TemplateID == new ID(new Guid(BucketItem.TemplateId)) || ruleOrFolderItem.TemplateID == new ID(new Guid(NodeItem.TemplateId)) || ruleOrFolderItem.TemplateID == new ID(new Guid(RedirectSubFolderItem.TemplateId))
|| ruleOrFolderItem.TemplateID == new ID(new Guid(RedirectFolderItem.TemplateId)))
{
ChildList childRules = ruleOrFolderItem.GetChildren();
@@ -24,6 +24,9 @@ public void ProcessRequest(HttpContext context)
var requestArgs = new HttpRequestArgs(context, HttpRequestType.Begin);
var requestUri = context.Request.Url;

//Pass along the static extension to make the InboundRewriteProcessor aware
requestArgs.CustomData.Add(Constants.StaticExtensionKey, context.Request.CurrentExecutionFilePathExtension);

var siteContext = SiteContextFactory.GetSiteContext(requestUri.Host, requestUri.AbsolutePath,
requestUri.Port);

@@ -0,0 +1,37 @@
using Sitecore.Data.Items;

namespace Hi.UrlRewrite.Templates.Folders
{
public partial class BucketItem : CustomItem
{

public static readonly string TemplateId = "{ADB6CA4F-03EF-4F47-B9AC-9CE2BA53FF97}";


#region Boilerplate CustomItem Code

public BucketItem(Item innerItem)
: base(innerItem)
{

}

public static implicit operator BucketItem(Item innerItem)
{
return innerItem != null ? new BucketItem(innerItem) : null;
}

public static implicit operator Item(BucketItem customItem)
{
return customItem != null ? customItem.InnerItem : null;
}

#endregion //Boilerplate CustomItem Code


#region Field Instance Methods


#endregion //Field Instance Methods
}
}
@@ -0,0 +1,37 @@
using Sitecore.Data.Items;

namespace Hi.UrlRewrite.Templates.Folders
{
public partial class NodeItem : CustomItem
{

public static readonly string TemplateId = "{239F9CF4-E5A0-44E0-B342-0F32CD4C6D8B}";


#region Boilerplate CustomItem Code

public NodeItem(Item innerItem)
: base(innerItem)
{

}

public static implicit operator NodeItem(Item innerItem)
{
return innerItem != null ? new NodeItem(innerItem) : null;
}

public static implicit operator Item(NodeItem customItem)
{
return customItem != null ? customItem.InnerItem : null;
}

#endregion //Boilerplate CustomItem Code


#region Field Instance Methods


#endregion //Field Instance Methods
}
}

0 comments on commit d9b649d

Please sign in to comment.