Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Domain on child node with cross site/host redirects #40

Merged
merged 9 commits into from
Jan 28, 2015
Merged
37 changes: 29 additions & 8 deletions Extensions/INodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,36 @@ public static Node GetDomainRootNode(this INode node)
throw new ArgumentNullException("node");

INode parent = node;
INode deepParent = node;
INode deepNode = node;
while (parent != null)
{
Domain[] domains = Domain.GetDomainsById(parent.Id);
if (domains != null && domains.Any()){
node= (Node)parent;
break;
}
node = parent;
parent = node.Parent;
}
if(UrlTrackerSettings.HasDomainOnChildNode){
while (deepParent != null && deepParent.Parent != null)
{
Domain[] domains = Domain.GetDomainsById(deepParent.Parent.Id);
if (domains != null && domains.Any()){
deepNode = (Node)deepParent;
break;
}
deepNode = deepParent;
deepParent = deepNode.Parent;
}
}

while (parent != null)
{
Domain[] domains = Domain.GetDomainsById(parent.Id);
if (domains != null && domains.Any())
return (Node)parent;
node = parent;
parent = node.Parent;
}
if (((Node)deepNode).Parent.Id == node.Id)
{
// if the found node is a child of the normal root, then use this one...
node = deepNode;
}

return (Node)node;
}
Expand Down
Binary file added Extensions/InfoCaster.Umbraco.UrlTracker.dll
Binary file not shown.
19 changes: 19 additions & 0 deletions Helpers/UmbracoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ internal static bool IsReservedPathOrUrl(string url)
if (!pathPart.Contains(".") && !pathPart.EndsWith("/"))
pathPart += "/";

// check if path is longer than one character, then if it does not start with / then add a /
if (pathPart.Length > 1 && pathPart[0] != '/')
{
pathPart = '/' + pathPart; // fix because sometimes there is no leading /... depends on browser...
}

// return true if url starts with an element of the reserved list
return _reservedList.StartsWith(pathPart.ToLowerInvariant());
}
Expand All @@ -95,6 +101,19 @@ internal static List<UrlTrackerDomain> GetDomains()
}
}
_urlTrackerDomains = _urlTrackerDomains.OrderBy(x => x.Name).ToList();

if (UrlTrackerSettings.HasDomainOnChildNode)
{
using (var dr = sqlHelper.ExecuteReader("SELECT * FROM umbracoDomains where CHARINDEX('*',domainName) = 1"))
{
while (dr.Read())
{
_urlTrackerDomains.Add(new UrlTrackerDomain(dr.GetInt("id"), dr.GetInt("domainRootStructureID"), dr.GetString("domainName")));
}
}
}

_urlTrackerDomains = _urlTrackerDomains.OrderBy(x => x.Name).ToList();
}
}
return _urlTrackerDomains;
Expand Down
24 changes: 21 additions & 3 deletions Helpers/UrlTrackerHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using InfoCaster.Umbraco.UrlTracker.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
Expand All @@ -15,7 +16,7 @@ public static string ResolveShortestUrl(string url)
{
if (url.StartsWith("http://") || url.StartsWith("https://"))
{
Uri uri = new Uri(url);
Uri uri = new Uri(url);
url = Uri.UnescapeDataString(uri.PathAndQuery);
}
// The URL should be stored as short as possible (e.g.: /page.aspx -> page | /page/ -> page)
Expand All @@ -25,7 +26,7 @@ public static string ResolveShortestUrl(string url)
url = url.Substring(0, url.Length - "/".Length);
if (url.EndsWith(".aspx"))
url = url.Substring(0, url.Length - ".aspx".Length);
return url;
return url;
}

public static string ResolveUmbracoUrl(string url)
Expand All @@ -46,5 +47,22 @@ public static string ResolveUmbracoUrl(string url)

return url;
}

public static string GetName(UrlTrackerDomain domain)
{
if (UrlTrackerSettings.HasDomainOnChildNode)
{
var result = string.Format("{0}", domain.Node.Parent == null ? domain.Node.Name : domain.Node.Parent.Name + "/" + domain.Node.Name);
if (string.IsNullOrEmpty(result))
{
result = "(root)";
}
return result;
}
else
{
return string.Format("{0} ({1})", domain.Node.Name, domain.Name);
}
}
}
}
31 changes: 30 additions & 1 deletion Models/UrlTrackerDomain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Web;
using umbraco.NodeFactory;
using Umbraco.Web;

namespace InfoCaster.Umbraco.UrlTracker.Models
{
Expand All @@ -17,7 +18,35 @@ public string UrlWithDomain
{
get
{
return string.Format("{0}://{1}", HttpContext.Current != null ? HttpContext.Current.Request.Url.Scheme : "http", Name);
var node = Node;
if (UrlTrackerSettings.HasDomainOnChildNode && node.Parent != null)
{
using (InfoCaster.Umbraco.UrlTracker.Helpers.ContextHelper.EnsureHttpContext())
{
// not sure if this will ever occur because the ensurehttpcontext is now added...
if (UmbracoContext.Current != null)
{
/*var url = new Node(node.Id).Url;
return url;*/
return Node.Url; // do not re-instantiate
}
else
{
return string.Format("{0}{1}{2}", HttpContext.Current != null ? HttpContext.Current.Request.Url.Scheme : Uri.UriSchemeHttp, Uri.SchemeDelimiter, HttpContext.Current.Request.Url.Host + "/" + Node.Parent.UrlName + "/" + Node.UrlName);
}
}
}
else
{
if (Name.Contains(Uri.UriSchemeHttp))
{
return Name;
}
else
{
return string.Format("{0}{1}{2}", HttpContext.Current != null ? HttpContext.Current.Request.Url.Scheme : Uri.UriSchemeHttp, Uri.SchemeDelimiter, Name);
}
}
}
}

Expand Down
89 changes: 85 additions & 4 deletions Models/UrlTrackerModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using InfoCaster.Umbraco.UrlTracker.Helpers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using umbraco.NodeFactory;
Expand All @@ -15,6 +16,7 @@ public enum UrlTrackerViewTypes
}

[Serializable]
[DebuggerDisplay("OUrl = {OldUrl} | Rgx = {OldRegex} | Qs = {OldUrlQueryString} | Root = {RedirectRootNodeId}")]
public class UrlTrackerModel
{
#region Data fields
Expand Down Expand Up @@ -72,16 +74,23 @@ public string CalculatedOldUrlWithDomain
domain = new UrlTrackerDomain(-1, RedirectRootNode.Id, string.Concat(HttpContext.Current.Request.Url.Host, HttpContext.Current.Request.Url.IsDefaultPort && !UrlTrackerSettings.AppendPortNumber ? string.Empty : string.Concat(":", HttpContext.Current.Request.Url.Port)));

Uri domainUri = new Uri(domain.UrlWithDomain);
string domainOnly = string.Format("{0}://{1}{2}", domainUri.Scheme, domainUri.Host, domainUri.IsDefaultPort && !UrlTrackerSettings.AppendPortNumber ? string.Empty : string.Concat(":", domainUri.Port));
string domainOnly = string.Format("{0}{1}{2}{3}", domainUri.Scheme, Uri.SchemeDelimiter, domainUri.Host, domainUri.IsDefaultPort && !UrlTrackerSettings.AppendPortNumber ? string.Empty : string.Concat(":", domainUri.Port));

return string.Format("{0}{1}{2}", new Uri(string.Concat(domainOnly, !domainOnly.EndsWith("/") && !OldUrl.StartsWith("/") ? "/" : string.Empty, UrlTrackerHelper.ResolveUmbracoUrl(OldUrl))), !string.IsNullOrEmpty(OldUrlQueryString) ? "?" : string.Empty, OldUrlQueryString);
if (UrlTrackerSettings.HasDomainOnChildNode)
{
return string.Format("{0}{1}{2}", new Uri(string.Concat(domain.UrlWithDomain, !domain.UrlWithDomain.EndsWith("/") && !OldUrl.StartsWith("/") ? "/" : string.Empty, UrlTrackerHelper.ResolveUmbracoUrl(OldUrl))), !string.IsNullOrEmpty(OldUrlQueryString) ? "?" : string.Empty, OldUrlQueryString);
}
else
{
return string.Format("{0}{1}{2}", new Uri(string.Concat(domainOnly, !domainOnly.EndsWith("/") && !OldUrl.StartsWith("/") ? "/" : string.Empty, UrlTrackerHelper.ResolveUmbracoUrl(OldUrl))), !string.IsNullOrEmpty(OldUrlQueryString) ? "?" : string.Empty, OldUrlQueryString);
}
}
}
public string CalculatedRedirectUrl
{
get
{
string calculatedRedirectUrl = !string.IsNullOrEmpty(RedirectUrl) ?
/*string calculatedRedirectUrl = !string.IsNullOrEmpty(RedirectUrl) ?
RedirectUrl :
!RedirectRootNode.NiceUrl.EndsWith("#") && RedirectNodeId.HasValue ?
new Uri(umbraco.library.NiceUrl(RedirectNodeId.Value).StartsWith("http") ? umbraco.library.NiceUrl(RedirectNodeId.Value) :
Expand All @@ -92,6 +101,69 @@ public string CalculatedRedirectUrl
).AbsolutePath :
string.Empty;
return !calculatedRedirectUrl.StartsWith("/") ? string.Concat("/", calculatedRedirectUrl) : calculatedRedirectUrl;
* */
string calculatedRedirectUri = !string.IsNullOrEmpty(RedirectUrl)?RedirectUrl:null;
if (calculatedRedirectUri != null)
{
if (calculatedRedirectUri.StartsWith(Uri.UriSchemeHttp + Uri.SchemeDelimiter) || calculatedRedirectUri.StartsWith(Uri.UriSchemeHttps + Uri.SchemeDelimiter))
{
return calculatedRedirectUri;
}
else
{
return !calculatedRedirectUri.StartsWith("/") ? string.Concat("/", calculatedRedirectUri) : calculatedRedirectUri;
}
}

if (!RedirectRootNode.NiceUrl.EndsWith("#") && RedirectNodeId.HasValue)
{

List<UrlTrackerDomain> domains = UmbracoHelper.GetDomains();
List<UrlTrackerDomain> siteDomains = domains.Where(x => x.NodeId == RedirectRootNode.Id).ToList();
List<string> hosts =
siteDomains
.Select(n => new Uri(n.UrlWithDomain).Host)
.ToList();
if (hosts.Count == 0)
{
return umbraco.library.NiceUrl(RedirectNodeId.Value);
}
var sourceUrl = new Uri(siteDomains.First().UrlWithDomain);
var targetUrl = new Uri(sourceUrl, umbraco.library.NiceUrl(RedirectNodeId.Value));
if (!targetUrl.Host.Equals(sourceUrl.Host, StringComparison.OrdinalIgnoreCase))
{
return targetUrl.AbsoluteUri;
}
if (hosts.Any(n => n.Equals(sourceUrl.Host, StringComparison.OrdinalIgnoreCase)))
{
string url = umbraco.library.NiceUrl(RedirectNodeId.Value);
// if current host is for this site already... (so no unnessecary redirects to primary domain)
if (url.StartsWith(Uri.UriSchemeHttp))
{
// if url is with domain, strip domain
var uri = new Uri(url);
return uri.AbsolutePath + uri.Fragment;
}
return url;
}
var newUri = new Uri(sourceUrl, umbraco.library.NiceUrl(RedirectNodeId.Value));
if (sourceUrl.Host != newUri.Host)
{
return newUri.AbsoluteUri;
}
else
{
return newUri.AbsolutePath + newUri.Fragment;
}
}

if (RedirectNodeId.HasValue)
{
return umbraco.library.NiceUrl(RedirectNodeId.Value);
//calculatedRedirectUri = new Uri();
}
return string.Empty;

}
}
public Node RedirectRootNode
Expand All @@ -112,7 +184,16 @@ public string RedirectRootNodeName
{
get
{
return RedirectRootNode.Name;
if (UrlTrackerSettings.HasDomainOnChildNode)
{
return RedirectRootNode.Parent == null
? RedirectRootNode.Name
: RedirectRootNode.Parent.Name + "/" + RedirectRootNode.Name;
}
else
{
return RedirectRootNode.Name;
}
}
}
public UrlTrackerViewTypes ViewType
Expand Down
60 changes: 60 additions & 0 deletions Models/axrk0021.nh2
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Web;
using InfoCaster.Umbraco.UrlTracker.Modules;
using umbraco;
using umbraco.NodeFactory;
using Umbraco.Web;

namespace InfoCaster.Umbraco.UrlTracker.Models
{
public class UrlTrackerDomain
{
public int Id { get; set; }
public int NodeId { get; set; }
public string Name { get; set; }

public Node Node { get { return new Node(NodeId); } }
public string UrlWithDomain
{
get
{
var node = Node;
if (UrlTrackerSettings.HasDomainOnChildNode && node.Parent != null)
{
return UrlTrackerModule.CurrentContext.ContentCache.GetById(node.Id).Url;
// return string.Format("{0}{1}{2}", HttpContext.Current != null ? HttpContext.Current.Request.Url.Scheme : Uri.UriSchemeHttp, Uri.SchemeDelimiter, HttpContext.Current.Request.Url.Host + "/" + Node.Parent.UrlName + "/" + Node.UrlName);
//string x = string.Format("{0}{1}{2}", HttpContext.Current != null ? HttpContext.Current.Request.Url.Scheme : Uri.UriSchemeHttp, Uri.SchemeDelimiter, HttpContext.Current.Request.Url.Host + "/" + Node.Parent.UrlName + "/" + Node.UrlName);
////var node = Node; // performance improvement
//var y = node.Url;
//var z = node.NiceUrl;
//return new Uri(HttpContext.Current.Request.Url, node.Parent.Url.TrimEnd('/') + "/" + node.UrlName).AbsoluteUri;
}
else
{
if (Name.Contains(Uri.UriSchemeHttp))
{
return Name;
}
else
{
return string.Format("{0}{1}{2}", HttpContext.Current != null ? HttpContext.Current.Request.Url.Scheme : Uri.UriSchemeHttp, Uri.SchemeDelimiter, Name);
}
}
}
}

public UrlTrackerDomain() { }

public UrlTrackerDomain(int id, int nodeId, string name)
{
Id = id;
NodeId = nodeId;
Name = name;
}

public override string ToString()
{
return UrlWithDomain;
}
}
}
Loading