PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Framework/UI/Helpers/UrlParser.cs

http://century.codeplex.com
C# | 190 lines | 134 code | 33 blank | 23 comment | 28 complexity | 4c73798ae69a4488c089619c14cdffd8 MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using GKFX.Common.Business.BOL.Common;
  6. using Century.Model;
  7. using System.Web;
  8. using GKFX.Business.Repository;
  9. namespace GKFX.Common.UI.Helpers
  10. {
  11. /// <summary>
  12. /// Class to parse url for GKFX
  13. /// Site path is depend on Country Site Table
  14. /// </summary>
  15. public class UrlParser
  16. {
  17. #region [ Constructor(s) ]
  18. public UrlParser(string url)
  19. {
  20. Parse(new System.Uri(url));
  21. }
  22. public UrlParser(System.Uri uri)
  23. {
  24. Parse(uri);
  25. }
  26. #endregion
  27. #region [ Private Method(s) ]
  28. private void Parse(System.Uri uri)
  29. {
  30. // get the port
  31. Port = uri.Port;
  32. // get the host name (my.domain.com)
  33. Host = uri.Host;
  34. // get the protocol
  35. Protocol = uri.Scheme;
  36. // get the domain and subdomain
  37. if (uri.AbsoluteUri.ToLower().Contains("localhost"))
  38. {
  39. Domain = "localhost";
  40. }
  41. else if (uri.HostNameType == UriHostNameType.IPv4 || uri.HostNameType == UriHostNameType.IPv6)
  42. {
  43. Domain = Host;
  44. }
  45. else
  46. {
  47. string subDomainPattern = @"^(?<subdomain>[\w\.\-]+\.)*(?<domain>[\w\-]+\.)(?<com>[\w]{2,3})(?<countryextension>\.[\w]{2})?$";
  48. Regex regex = new Regex(subDomainPattern);
  49. Match match1 = Regex.Match(uri.Host, subDomainPattern, RegexOptions.RightToLeft);
  50. if (match1.Success)
  51. {
  52. SubDomain = match1.Groups["subdomain"].ToString();
  53. Domain = match1.Groups["domain"].ToString() + match1.Groups["com"].ToString() + match1.Groups["countryextension"];
  54. }
  55. }
  56. if (Port == 80)
  57. {
  58. CleanURL = string.Format("{0}://{1}/", uri.Scheme, uri.GetComponents(UriComponents.Host, UriFormat.UriEscaped));
  59. }
  60. else
  61. {
  62. CleanURL = string.Format("{0}://{1}/", uri.Scheme, uri.GetComponents(UriComponents.HostAndPort, UriFormat.UriEscaped));
  63. }
  64. // get site path
  65. string path = UrlPath = SitePath = string.Empty;
  66. if (uri.Segments.Length > 1)
  67. {
  68. path = uri.Segments[1].Replace("/", string.Empty);
  69. }
  70. //AvailableCountrySites = new CountrySiteBSO().GetAvailabeSites();
  71. if (!string.IsNullOrEmpty(path))
  72. {
  73. if (!PersistData.AvailableSites.Any(c => c.Path.ToLower() == path.ToLower()))
  74. {
  75. path = string.Empty;
  76. }
  77. else
  78. {
  79. UrlPath = path;
  80. }
  81. }
  82. if (string.IsNullOrEmpty(path))
  83. {
  84. HttpCookie cookie = HttpContext.Current.Request.Cookies[ConfManager.Instance.CookieManager.CountryCookieName.ConfigValue];
  85. if (cookie != null && !string.IsNullOrEmpty(cookie.Value))
  86. {
  87. path = cookie.Value;
  88. }
  89. }
  90. if (!string.IsNullOrEmpty(path))
  91. {
  92. CountrySite site = PersistData.AvailableSites.FirstOrDefault(c => c.Path.ToLower() == path.ToLower());
  93. if (site != null)
  94. {
  95. SitePath = path;
  96. //get real site address for current path
  97. SiteAddress = site.SiteAddress;
  98. if (System.Web.HttpContext.Current.Session == null)
  99. {
  100. HttpContext.Current.Response.Redirect(SiteAddress);
  101. return;
  102. }
  103. System.Web.HttpContext.Current.Session["CurrentSiteId"] = site.Id;
  104. }
  105. }
  106. string postFix = string.Empty;
  107. if (uri.AbsolutePath.ToLower().Contains("/" + UrlPath.ToLower() + "/"))
  108. {
  109. postFix = "/";
  110. }
  111. CurrentAddress = CleanURL.ToLower().Replace("www.", string.Empty) + (string.IsNullOrEmpty(UrlPath) ? string.Empty : UrlPath + postFix);
  112. LocalPath = uri.LocalPath.Replace("/" + SitePath + "/", string.Empty);
  113. PathAndQuery = uri.PathAndQuery.ToLower().Replace("/" + SitePath.ToLower() + "/", string.Empty);
  114. if (PathAndQuery.ToLower() == "/" + SitePath.ToLower())
  115. {
  116. PathAndQuery = string.Empty;
  117. }
  118. if (PathAndQuery.ToLower().StartsWith("/" + SitePath.ToLower() + "?"))
  119. {
  120. PathAndQuery = PathAndQuery.ToLower().Replace("/" + SitePath.ToLower(), string.Empty);
  121. }
  122. if (PathAndQuery.StartsWith("/"))
  123. {
  124. PathAndQuery = PathAndQuery.Substring(1);
  125. }
  126. Query = uri.Query;
  127. }
  128. #endregion
  129. #region [ Propert(ies) ]
  130. public int Port { get; set; }
  131. public string Host { get; set; }
  132. public string Protocol { get; set; }
  133. public string CleanURL { get; set; }
  134. /// <summary>
  135. /// SitePath on Country Site Table
  136. /// </summary>
  137. public string SitePath { get; set; }
  138. /// <summary>
  139. /// Domain of url
  140. /// </summary>
  141. public string Domain { get; set; }
  142. /// <summary>
  143. /// Sub domain or sub domains of url
  144. /// </summary>
  145. public string SubDomain { get; set; }
  146. /// <summary>
  147. /// Site Address of a Site Path
  148. /// </summary>
  149. public string SiteAddress { get; set; }
  150. public string CurrentAddress { get; set; }
  151. public string UrlPath { get; set; }
  152. public string LocalPath { get; set; }
  153. public string PathAndQuery { get; set; }
  154. public string Query { get; set; }
  155. #endregion
  156. }
  157. }