Example #1
0
        private (Uri RedirectUri, Uri CanonicalUri) NormalizeUrl()
        {
            //var redirectUri = NormalizeUrl(out var canonicalUri);
            //return (redirectUri, canonicalUri);

            var urlNormalizer = new UrlNormalizer();

            if (urlNormalizer.Normalize()) // no failure, possible redirect
            {
                return(urlNormalizer.NormalizedUri, urlNormalizer.CanonicalUri);
            }

            // could not normalize the url -- log and report it
            var logMessage = urlNormalizer.ErrorMessage;

            if (No404OnUrlNormalizeError)
            {
                logMessage = $"[no 404] {logMessage}";
            }
            Log404Error(logMessage);
            UrlError = urlNormalizer.ErrorMessage;
            if (!No404OnUrlNormalizeError)
            {
                if (IsDebugging)
                {
                    throw new ApplicationException(urlNormalizer.ErrorMessage);
                }
                else
                {
                    SafeTransferToError404();
                }
            }
            return(null, null);
        }
Example #2
0
        //public static Uri PropagateCurrentSiteId(Uri uri)
        //{
        //  var siteId = CurrentQuerySiteId;
        //  if (string.IsNullOrEmpty(siteId)) return uri;
        //  var ub = new UriBuilder(uri);
        //  //PropagateCurrentSiteId(ub, siteId);
        //  return ub.Uri;
        //}

        //private static void PropagateCurrentSiteId(UriBuilder ub)
        //{
        //  //PropagateCurrentSiteId(ub, CurrentQuerySiteId);
        //}

        //private static void PropagateCurrentSiteId(UriBuilder ub, string siteId)
        //{
        //  if (!string.IsNullOrEmpty(siteId))
        //  {
        //    var qsc = QueryStringCollection.Parse(ub.Query);
        //    qsc["Site"] = siteId;
        //    ub.Query = qsc.ToString();
        //  }
        //}

        #endregion Methods to create canonical urls for public pages

        public static void ForceUsaDomain()
        {
            var uri = UrlNormalizer.BuildCurrentUri();

            if (uri.Host.IsNeIgnoreCase(CanonicalSiteNameInfo.Name))
            {
                var builder = new UriBuilder(uri)
                {
                    Host = CanonicalSiteNameInfo.Name
                };
                HttpContext.Current.Response.Redirect(builder.ToString(), true);
            }
        }
        protected override void OnPreInit(EventArgs e)
        {
            var uri = UrlNormalizer.BuildCurrentUri();

            if (uri.Host.IsNeIgnoreCase(UrlManager.SiteHostName))
            {
                var query = uri.Query;
                if (query.StartsWith("?"))
                {
                    query = query.Substring(1);
                }
                Response.Redirect(UrlManager.GetSiteUri(uri.AbsolutePath, query).ToString());
                Response.End();
            }
            base.OnPreInit(e);
        }
Example #4
0
        private Uri NormalizeUrl(out Uri canonicalUri)
        {
            var urlNormalizer = new UrlNormalizer();

            if (urlNormalizer.Normalize()) // no failure, possible redirect
            {
                var redirectUri = urlNormalizer.NormalizedUri;
                if (redirectUri != null) // log upcoming redirect
                {
                    Log301Redirect(redirectUri.ToString());
                }
                canonicalUri = urlNormalizer.CanonicalUri;
                return(redirectUri);
            }

            // could not normalize the url -- log and report it
            var logMessage = urlNormalizer.ErrorMessage;

            if (No404OnUrlNormalizeError)
            {
                logMessage = "[no 404] " + logMessage;
            }
            Log404Error(logMessage);
            UrlError = urlNormalizer.ErrorMessage;
            if (!No404OnUrlNormalizeError)
            {
                if (IsDebugging)
                {
                    throw new ApplicationException(urlNormalizer.ErrorMessage);
                }
                else
                {
                    SafeTransferToError404();
                }
            }
            canonicalUri = null;
            return(null);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            var congress               = Request.QueryString["Congress"];
            var stateCode              = Request.QueryString["State"];
            var stateSenate            = Request.QueryString["StateSenate"];
            var stateHouse             = Request.QueryString["StateHouse"];
            var county                 = Request.QueryString["County"];
            var district               = Request.QueryString["District"];
            var place                  = Request.QueryString["Place"];
            var elementary             = Request.QueryString["Esd"];
            var secondary              = Request.QueryString["Ssd"];
            var unified                = Request.QueryString["Usd"];
            var cityCouncil            = Request.QueryString["Cc"];
            var countySupervisors      = Request.QueryString["Cs"];
            var schoolDistrictDistrict = Request.QueryString["Sdd"];
            var geo        = Request.QueryString["Geo"];
            var page       = Request.QueryString["Page"];
            var address    = Request.QueryString["Address"];
            var components = Request.QueryString["Components"];
            var remember   = !Request.QueryString["Remember"].IsEqIgnoreCase("n");

            if (!IsNullOrWhiteSpace(congress) && !IsNullOrWhiteSpace(stateSenate) &&
                Offices.IsValidStateHouseDistrict(stateHouse, stateCode) &&
                !IsNullOrWhiteSpace(county) && !IsNullOrWhiteSpace(components) &&
                !IsNullOrWhiteSpace(geo) && !IsNullOrWhiteSpace(page))
            {
                var expiration = DateTime.UtcNow.AddDays(30); // 30 days

                var cookie = new HttpCookie("State", IsNullOrWhiteSpace(stateCode)
          ? DomainData.FromQueryStringOrDomain
          : stateCode);
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                cookie = new HttpCookie("Congress", congress);
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                cookie = new HttpCookie("StateSenate", stateSenate);
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                cookie = new HttpCookie("StateHouse", stateHouse);
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                cookie = new HttpCookie("County", county);
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                cookie = new HttpCookie("District", district);
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                cookie = new HttpCookie("Place", place);
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                cookie = new HttpCookie("Elementary", elementary);
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                cookie = new HttpCookie("Secondary", secondary);
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                cookie = new HttpCookie("Unified", unified);
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                cookie = new HttpCookie("CityCouncil", cityCouncil);
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                cookie = new HttpCookie("CountySupervisors", countySupervisors);
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                cookie = new HttpCookie("SchoolDistrictDistrict", schoolDistrictDistrict);
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                cookie = new HttpCookie("Geo", geo);
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                cookie = new HttpCookie("Address", address);
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                cookie = new HttpCookie("Components", components);
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                cookie = new HttpCookie("CDate", HttpUtility.UrlEncode(expiration.ToString(CultureInfo.InvariantCulture)));
                if (remember)
                {
                    cookie.Expires = expiration;
                }
                Response.Cookies.Add(cookie);

                if (UrlManager.CurrentDomainDataCode == "US")
                {
                    var ub = new UriBuilder(UrlNormalizer.BuildCurrentUri())
                    {
                        Host = UrlManager.GetStateHostName(stateCode)
                    };
                    Response.Redirect(ub.ToString());
                }
                else
                {
                    page = HttpUtility.UrlDecode(page);
                    Debug.Assert(page != null, "page != null");
                    Response.Redirect(page);
                }
            }
            else
            {
                Response.Redirect("/404.aspx");
            }
        }