private void HandleOfficialsPage() { var reportCode = string.Empty; if (StateCache.IsValidStateOrFederalCode(GetQueryParm("Report"), false)) { reportCode = GetQueryParm("Report") .ToUpper(); } else { var stateCode = UrlManager.FindStateCode(); if (StateCache.IsValidStateCode(stateCode) || StateCache.IsValidFederalCode(stateCode, false)) { reportCode = stateCode; } } if (!string.IsNullOrEmpty(reportCode)) { NormalizedUri = UrlManager.GetOfficialsPageUri(reportCode, GetQueryParm("County"), GetQueryParm("Local")); _IsCanonicalUsa = !StateCache.IsValidStateCode(reportCode); } else { ErrorMessage = "The Report code could not be identified."; } }
private string GetLatestViewableElectionKey() { if (!string.IsNullOrEmpty(GetQueryParm("Office"))) { return (ElectionsOffices.GetLatestViewableElectionKeyStateByOfficeKey( GetQueryParm("Office"), string.Empty)); } return (Elections.GetLatestViewableElectionKeyByStateCode( UrlManager.FindStateCode(), String.Empty)); }
private void HandleElectedPage() { var stateCode = UrlManager.FindStateCode(); if (StateCache.IsValidStateCode(stateCode)) { NormalizedUri = UrlManager.GetElectedPageUri(stateCode, GetQueryParm("Congress"), GetQueryParm("StateSenate"), GetQueryParm("StateHouse"), GetQueryParm("County")); } else { ErrorMessage = "Invalid_StateCode"; } }
private bool Normalize(Uri originalUri) { #region Note // This method rebuilds the old requested url // and a new correct url based on the StateCode for the page. // Both are reconstructed so that the QueryString // parameters are in the same order. // Then they are compared (case insensitive). // // If there is any sort of failure, the method returns false. // The ErrorMessage property contains an explanation. // // If the method returns true (success), the // normalized Uri is available through the NormalizedUri // property. If it is null, no redirect is required. #endregion Note Initialize(originalUri); if (IsIPAddress) // any explicit IP address redirects to main site home page { NormalizedUri = UrlManager.SiteUri; return(true); } // Save the caching values and remove them so they don't trigger a // redirect var noCacheValue = GetQueryParm(VotePage.NoCacheParameter); var cacheDefeatValue = GetQueryParm("X"); var openAllValue = GetQueryParm("openall"); var publicValue = GetQueryParm("public"); if (!string.IsNullOrWhiteSpace(noCacheValue) || !string.IsNullOrWhiteSpace(cacheDefeatValue) || !string.IsNullOrWhiteSpace(openAllValue) || !string.IsNullOrWhiteSpace(publicValue)) { if (!string.IsNullOrWhiteSpace(noCacheValue)) { _OriginalQueryCollection.Remove(VotePage.NoCacheParameter); } if (!string.IsNullOrWhiteSpace(cacheDefeatValue)) { _OriginalQueryCollection.Remove("X"); } if (!string.IsNullOrWhiteSpace(openAllValue)) { _OriginalQueryCollection.Remove("openall"); } if (!string.IsNullOrWhiteSpace(publicValue)) { _OriginalQueryCollection.Remove("public"); } var ub = new UriBuilder(OriginalUri) { Query = _OriginalQueryCollection.ToString() }; OriginalUri = ub.Uri; } // for ballot page, save any friend and choices parameters and remove them string friendValue = null; string choicesValue = null; if (OriginalUri.AbsolutePath.ToLower() == "/ballot.aspx") { friendValue = GetQueryParm("friend"); choicesValue = GetQueryParm("choices"); if (!string.IsNullOrWhiteSpace(friendValue) || !string.IsNullOrWhiteSpace(choicesValue)) { if (!string.IsNullOrWhiteSpace(friendValue)) { _OriginalQueryCollection.Remove("friend"); } if (!string.IsNullOrWhiteSpace(choicesValue)) { _OriginalQueryCollection.Remove("choices"); } var ub = new UriBuilder(OriginalUri) { Query = _OriginalQueryCollection.ToString() }; OriginalUri = ub.Uri; } } if (ScriptHasQueryStringParms()) { if (!string.IsNullOrEmpty(GetQueryParm("Election"))) { if (!string.IsNullOrEmpty(GetQueryParm("Office")) && !string.IsNullOrEmpty(GetQueryParm("Issue"))) { HandleIssuePage(); } else if (!string.IsNullOrEmpty(GetQueryParm("Referendum"))) { HandleReferendumPage(); } else if (!string.IsNullOrEmpty(GetQueryParm("Congress"))) { if (OriginalUri.AbsolutePath.ToLower() == "/issue2.aspx") { HandleIssue2Page(); } else { HandleBallotPage(); } } else if (!string.IsNullOrEmpty(GetQueryParm("Office"))) { HandleCompareCandidatesPage(); } else { HandleElectionPage(); } } else if (!string.IsNullOrEmpty(GetQueryParm("Id"))) { if (!string.IsNullOrEmpty(GetQueryParm("Issue"))) { HandlePoliticianIssuePage(); } else { HandleIntroPage(); } } else if (!string.IsNullOrEmpty(GetQueryParm("Congress"))) { HandleElectedPage(); } else if (!string.IsNullOrEmpty(GetQueryParm("Report")) || !string.IsNullOrEmpty(UrlManager.FindStateCode())) { HandleOfficialsPage(); } } if (NormalizedUri == null) // nothing yet, keep trying { if (ScriptCanHaveStateParm()) { HandlePagesWithOnlyStateCode(); } } if (NormalizedUri == null) // nothing yet, keep trying { if (ScriptHasNoParms()) { HandlePagesWithNoStateCode(); } } // Ensure a canonical host name if (NormalizedUri != null) { if (!UrlManager.IsCanonicalHostName(NormalizedUri.Host)) // replace host { var ub = new UriBuilder(NormalizedUri) { Host = UrlManager.GetCanonicalHostName(NormalizedUri.Host) }; NormalizedUri = ub.Uri; } } // Note: logging has been moved out of the mainstream processing so that // this class can be easily tested without logging if (NormalizedUri == null) // We couldn't do anything with it, error return { return(false); } // The canonical Uri is all lower case CanonicalUri = new Uri(NormalizedUri.ToString() .ToLowerInvariant()); CanonicalUri = AdjustCanonicalUri(CanonicalUri); if ( Uri.Compare(NormalizedUri, OriginalUri, UriComponents.AbsoluteUri | UriComponents.Fragment, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase) == 0) { NormalizedUri = null; // meaning no redirect is necessary } // suppress redirect for /default.aspx if everything else is the same if (NormalizedUri != null && OriginalUri.AbsolutePath.Equals("/default.aspx", StringComparison.OrdinalIgnoreCase)) { var ub = new UriBuilder(OriginalUri) { Path = "/" }; if ( Uri.Compare(NormalizedUri, ub.Uri, UriComponents.AbsoluteUri, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase) == 0) { NormalizedUri = null; // meaning no redirect is necessary } } // Restore NoCache and browser cache defeat parameters. // Note: we do not restore browser cache defeat unless NoCache is present too. if (NormalizedUri != null && (!string.IsNullOrWhiteSpace(noCacheValue) || !string.IsNullOrWhiteSpace(openAllValue) || !string.IsNullOrWhiteSpace(publicValue))) { var ub = new UriBuilder(NormalizedUri); var qsc = QueryStringCollection.Parse(ub.Query); if (!string.IsNullOrWhiteSpace(noCacheValue)) { qsc.Add(VotePage.NoCacheParameter, noCacheValue); } if (!string.IsNullOrWhiteSpace(noCacheValue) && !string.IsNullOrWhiteSpace(cacheDefeatValue)) { qsc.Add("X", cacheDefeatValue); } if (!string.IsNullOrWhiteSpace(openAllValue)) { qsc.Add("openall", openAllValue); } if (!string.IsNullOrWhiteSpace(publicValue)) { qsc.Add("public", publicValue); } ub.Query = qsc.ToString(); NormalizedUri = ub.Uri; } // restore friend and choices (ballot page only) // must both be present if (NormalizedUri != null && (!string.IsNullOrWhiteSpace(friendValue) && !string.IsNullOrWhiteSpace(choicesValue))) { var ub = new UriBuilder(NormalizedUri); var qsc = QueryStringCollection.Parse(ub.Query); qsc.Add("friend", friendValue); qsc.Add("choices", choicesValue); ub.Query = qsc.ToString(); NormalizedUri = ub.Uri; } // This return indicates lack of failure. The null-ness of NormalizedUri // indicates whether or not to redirect. return(true); }
private void HandlePagesWithOnlyStateCode() { var stateCode = UrlManager.FindStateCode(); if (string.IsNullOrEmpty(stateCode) || StateCache.IsValidStateCode(stateCode)) { switch (OriginalUri.AbsolutePath.ToLower()) { case "/": case "/default.aspx": NormalizedUri = UrlManager.GetDefaultPageUri(stateCode); break; case "/aboutus.aspx": NormalizedUri = UrlManager.GetAboutUsPageUri(stateCode); break; case "/contactus.aspx": NormalizedUri = UrlManager.GetContactUsPageUri(stateCode); break; case "/donate.aspx": NormalizedUri = UrlManager.GetDonatePageUri(stateCode); break; case "/forcandidates.aspx": NormalizedUri = UrlManager.GetForCandidatesPageUri(stateCode); break; case "/forresearch.aspx": NormalizedUri = UrlManager.GetForResearchPageUri(stateCode); break; case "/forpartners.aspx": NormalizedUri = UrlManager.GetForPartnersPageUri(stateCode); break; case "/forpoliticalparties.aspx": NormalizedUri = UrlManager.GetForPoliticalPartiesPageUri(stateCode); break; case "/forelectionauthorities.aspx": NormalizedUri = UrlManager.GetForElectionAuthoritiesPageUri(stateCode); break; case "/forvolunteers.aspx": NormalizedUri = UrlManager.GetForVolunteersPageUri(stateCode); break; case "/forvoters.aspx": NormalizedUri = UrlManager.GetForVotersPageUri(stateCode); break; case "/issuelist.aspx": NormalizedUri = UrlManager.GetIssueListPageUri(stateCode); break; case "/privacy.aspx": NormalizedUri = UrlManager.GetPrivacyPageUri(stateCode); break; } } else { ErrorMessage = "Invalid_StateCode"; } }