Пример #1
0
        public static Uri GetElectionPageUri(string state, string election, bool openAll = false, bool forIFrame = false)
        {
            var page = forIFrame
        ? "ElectionForIFrame.aspx"
        : "Election.aspx";

            if (StateCache.IsValidStateCode(state))
            {
                var qsc = new QueryStringCollection();
                AddNonEmptyParm(qsc, "State", state);
                AddNonEmptyParm(qsc, "Election", election);
                if (openAll)
                {
                    AddNonEmptyParm(qsc, "openall", "Y");
                }
                return(GetStateUri(state, page, qsc));
            }
            else
            {
                var qsc = new QueryStringCollection();
                AddNonEmptyParm(qsc, "Election", election);
                if (openAll)
                {
                    AddNonEmptyParm(qsc, "openall", "Y");
                }
                return(GetStateUri(G.State_Code(), page, qsc));
            }
        }
Пример #2
0
        private Uri AdjustCanonicalUri(Uri uri)
        {
            uri = GetCanonicalPath(uri);
            // For info pages, use the site domain
            if (_IsCanonicalUsa)
            {
                var query = uri.Query;
                if (query.StartsWith("?", StringComparison.Ordinal))
                {
                    query = query.Substring(1);
                }
                uri = UrlManager.GetSiteUri(uri.AbsolutePath, query);
            }
            else if (IsUsaPage(uri)) // eliminate query string
            {
                uri = UrlManager.GetSiteUri(uri.AbsolutePath);
            }
            else
            {
                uri = CheckForPresidentialPrimary(uri);
            }

            // If there's a site parameter, remove it
            if (!string.IsNullOrEmpty(UrlManager.CurrentQuerySiteId))
            {
                var ub  = new UriBuilder(uri);
                var qsc = QueryStringCollection.Parse(ub.Query);
                qsc.Remove("site");
                ub.Query = qsc.ToString();
                uri      = ub.Uri;
            }

            return(uri);
        }
Пример #3
0
        private void RedirectIfSuperfluousQueryString()
        {
            var hasQueryString =
                !IsNullOrWhiteSpace(Request.QueryString.ToString());

            if (!hasQueryString)
            {
                return;
            }
            var superfluousParameters = SuperfluousQueryParameters;

            if (superfluousParameters == null)
            {
                return;
            }
            var qsc           = QueryStringCollection.Parse(Request.QueryString.ToString());
            var startingCount = qsc.Count;

            foreach (var superfluousParameter in superfluousParameters)
            {
                qsc.Remove(superfluousParameter);
            }

            if (qsc.Count != startingCount)
            {
                Response.Redirect(qsc.AddToPath(Request.Path));
            }
        }
Пример #4
0
 private void Initialize(Uri originalUri)
 {
     OriginalUri = originalUri;
     _OriginalQueryCollection = GetOriginalQueryCollectionCopy();
     NormalizedUri            = null;
     CanonicalUri             = null;
     ErrorMessage             = "All_rebuild_options_failed";
 }
Пример #5
0
        public static Uri GetIntroPageUri(string id)
        {
            var stateCode = Politicians.GetStateCodeFromKey(id);
            var qsc       = new QueryStringCollection();

            AddNonEmptyParm(qsc, "State", stateCode);
            AddNonEmptyParm(qsc, "Id", id);
            return(GetStateUri(stateCode, "Intro.aspx", qsc));
        }
Пример #6
0
        public static Uri GetReferendumPageUri(
            string state, string election, string referendum)
        {
            var qsc = new QueryStringCollection();

            AddNonEmptyParm(qsc, "State", state);
            AddNonEmptyParm(qsc, "Election", election);
            AddNonEmptyParm(qsc, "Referendum", referendum);
            return(GetStateUri(state, "Referendum.aspx", qsc));
        }
Пример #7
0
        public static Uri GetPoliticianIssuePageUri(
            string state, string id, string issue)
        {
            var qsc = new QueryStringCollection();

            AddNonEmptyParm(qsc, "State", state);
            AddNonEmptyParm(qsc, "Id", id);
            AddNonEmptyParm(qsc, "Issue", issue);
            return(GetStateUri(state, "PoliticianIssue.aspx", qsc));
        }
Пример #8
0
        public static Uri GetForVotersPageUri(string state)
        {
            if (!StateCache.IsValidStateCode(state))
            {
                return(GetSiteUri("forVoters.aspx"));
            }
            var qsc = new QueryStringCollection();

            AddNonEmptyParm(qsc, "State", state);
            return(GetStateUri(state, "forVoters.aspx", qsc));
        }
Пример #9
0
        public static Uri GetIssue2PageUri(
            string state, string election, string office, string issue)
        {
            var qsc = new QueryStringCollection();

            AddNonEmptyParm(qsc, "State", state);
            AddNonEmptyParm(qsc, "Election", election);
            AddNonEmptyParm(qsc, "Office", office);
            AddNonEmptyParm(qsc, "Issue", issue);
            return(GetStateUri(state, "Issue2.aspx", qsc));
        }
Пример #10
0
        public static void RedirectToSignInPage(string returnUrl,
                                                QueryStringCollection qsc)
        {
            string queryString = null;

            if ((qsc != null) && (qsc.Count > 0))
            {
                queryString = qsc.ToString();
            }
            RedirectToSignInPage(returnUrl, queryString);
        }
Пример #11
0
        public static Uri GetElectedPageUri(
            string state, string congress, string stateSenate, string stateHouse,
            string county)
        {
            var qsc = new QueryStringCollection();

            AddNonEmptyParm(qsc, "State", state);
            AddNonEmptyParm(qsc, "Congress", congress);
            AddNonEmptyParm(qsc, "StateSenate", stateSenate);
            AddNonEmptyParm(qsc, "StateHouse", stateHouse);
            AddNonEmptyParm(qsc, "County", county);
            return(GetStateUri(state, "Elected.aspx", qsc));
        }
Пример #12
0
        public static Uri GetCompareCandidatesPageUri(
            string state, string election, string office)
        {
            var qsc = new QueryStringCollection();

            AddNonEmptyParm(qsc, "State", state);
            AddNonEmptyParm(qsc, "Election", election);
            AddNonEmptyParm(qsc, "Office", office);
            //AddNonEmptyParm(qsc, "Issue", issue);
            var uri = GetStateUri(state, "CompareCandidates.aspx", qsc);

            //if (!string.IsNullOrEmpty(issue))
            //  uri = new UriBuilder(uri) {Fragment = "Issue_" + issue}.Uri;
            return(uri);
        }
Пример #13
0
        public static Uri GetIssue2PageUri(
            string state, string election, string congress, string stateSenate,
            string stateHouse, string county, string office = null)
        {
            var qsc = new QueryStringCollection();

            AddNonEmptyParm(qsc, "State", state);
            AddNonEmptyParm(qsc, "Election", election);
            AddNonEmptyParm(qsc, "Congress", congress);
            AddNonEmptyParm(qsc, "StateSenate", stateSenate);
            AddNonEmptyParm(qsc, "StateHouse", stateHouse);
            AddNonEmptyParm(qsc, "County", county);
            AddNonEmptyParm(qsc, "Office", office);
            return(GetStateUri(state, "Issue2.aspx", qsc));
        }
Пример #14
0
 public static string GetPoliticianFolderPageUrl(string pageName,
                                                 params string[] queryParametersAndValues)
 {
     if (queryParametersAndValues.Length == 0 &&
         UserSecurityClass != PoliticianSecurityClass)
     {
         var id = PoliticianKeyFromSecurePoliticianPage;
         if (!IsNullOrWhiteSpace(id))
         {
             queryParametersAndValues = new[] { "id", id }
         }
         ;
     }
     return(QueryStringCollection.FromPairs(queryParametersAndValues)
            .AddToPath("/politician/" + pageName + ".aspx"));
 }
Пример #15
0
        private string MakeSetCookiesUrl(Uri uri, string address = null, bool remember = true)
        {
            var qsc = new QueryStringCollection
            {
                { "Congress", Congress },
                { "StateSenate", StateSenate },
                { "StateHouse", StateHouse },
                { "County", County },
                { "Page", HttpUtility.UrlEncode(uri.ToString()) },
                { "Address", address }
            };

            if (!remember)
            {
                qsc.Add("Remember", "n");
            }
            return(UrlManager.GetStateUri(StateCode, "SetCookies.aspx", qsc).ToString());
        }
        public static QueryStringCollection FromPairs(IEnumerable <string> pairs)
        {
            var list = pairs as IList <string> ?? pairs.ToArray();

            if (list.Count % 2 != 0)
            {
                throw new ArgumentException("pairs entries not paired");
            }
            var qsc = new QueryStringCollection();

            for (var inx = 0; inx < list.Count; inx += 2)
            {
                if (!IsNullOrWhiteSpace(list[inx + 1]))
                {
                    qsc.Add(list[inx], list[inx + 1]);
                }
            }
            return(qsc);
        }
Пример #17
0
        private static Uri CheckForPresidentialPrimary(Uri uri)
        {
            var path = uri.AbsolutePath.ToLower();

            if (path == "/election.aspx" || path == "/electionforiframe.aspx" || path == "/issue.aspx" || path == "/issue2.aspx")
            {
                var qsc            = QueryStringCollection.Parse(uri.Query);
                var newElectionKey = MemCache.GetCanonicalElectionKey(qsc["election"]);
                // if we have a canonical key, substitute it
                // and switch to the non-state domain
                if (!string.IsNullOrWhiteSpace(newElectionKey))
                {
                    qsc["election"] = newElectionKey;
                    // Remove state from query string
                    qsc.Remove("state");
                    uri = UrlManager.GetSiteUri(path, qsc);
                }
            }
            return(uri);
        }
Пример #18
0
 public static Uri GetOfficialsPageUri(string report, string county = null, string local = null)
 {
     if (report != null && StateCache.IsValidStateCode(report))
     {
         var qsc = new QueryStringCollection();
         AddNonEmptyParm(qsc, "State", report);
         AddNonEmptyParm(qsc, "Report", report);
         AddNonEmptyParm(qsc, "County", county);
         AddNonEmptyParm(qsc, "Local", local);
         return(GetStateUri(report, "Officials.aspx", qsc));
     }
     else
     {
         var qsc = new QueryStringCollection();
         AddNonEmptyParm(qsc, "Report", report);
         return(HttpContext.Current == null
   ? GetSiteUri("Officials.aspx", qsc)
   : GetStateUri(G.State_Code(), "Officials.aspx", qsc));
     }
 }
        private static QueryStringCollection Parse(string queryString, bool urldecoded)
        {
            var qsc = new QueryStringCollection();

            if (queryString?.StartsWith("?", StringComparison.Ordinal) == true)
            {
                queryString = queryString.Substring(1);
            }
            if (!IsNullOrEmpty(queryString)) // empty
            {
                var parameters = queryString.Split('&');
                foreach (var parameter in parameters)
                {
                    string key;
                    string value;
                    var    equalPos = parameter.IndexOf('=');
                    if (equalPos < 0) // null key
                    {
                        key   = null;
                        value = parameter;
                    }
                    else
                    {
                        key   = parameter.Substring(0, equalPos);
                        value = parameter.Substring(equalPos + 1);
                    }
                    if (urldecoded)
                    {
                        key   = HttpUtility.UrlDecode(key);
                        value = HttpUtility.UrlDecode(value);
                    }
                    Debug.Assert(key != null, "key != null");
                    Debug.Assert(value != null, "value != null");
                    qsc.Add(key, value);
                }
            }
            return(qsc);
        }
Пример #20
0
        private static string GetAdminFolderPageUrl(string pageName, bool addAdminCodes,
                                                    params string[] queryParametersAndValues)
        {
            IEnumerable <string> queries = queryParametersAndValues;

            if (addAdminCodes)
            {
                var page = GetPage <SecureAdminPage>();
                var list = new List <string>(queryParametersAndValues);
                queries = list;
                list.Add("state", page.StateCode);
                if (page.CountyCodeExists)
                {
                    list.Add("county", page.CountyCode);
                }
                if (page.LocalKeyExists)
                {
                    list.Add("local", page.LocalKey);
                }
            }
            return(UrlManager.ToAdminUrl(QueryStringCollection.FromPairs(queries)
                                         .AddToPath("/admin/" + pageName + ".aspx")));
        }
Пример #21
0
 public static Uri GetSiteUri(string path, QueryStringCollection qsc)
 {
     return(GetSiteUri(path, qsc.ToString()));
 }
Пример #22
0
        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);
        }
Пример #23
0
 public static void RedirectToSignInPage(QueryStringCollection qsc)
 {
     RedirectToSignInPage(null, qsc);
 }
Пример #24
0
 private QueryStringCollection GetOriginalQueryCollectionCopy()
 {
     return(QueryStringCollection.Parse(OriginalUri.Query));
 }
Пример #25
0
 public static Uri GetCurrentPathUri(QueryStringCollection qsc)
 {
     return(GetCurrentPathUri(qsc.ToString()));
 }
Пример #26
0
 public static QueryStringCollection GetCurrentQueryCollection()
 {
     return(QueryStringCollection.Parse(GetCurrentQueryString()));
 }
Пример #27
0
 public static Uri GetCurrentSiteUri(QueryStringCollection qsc)
 {
     return(GetCurrentSiteUri(null, qsc.ToString()));
 }
Пример #28
0
 public static Uri GetDomainDesignCodeUri(
     string designCode, string path, QueryStringCollection qsc)
 {
     return(GetDomainDesignCodeUri(designCode, path, qsc.ToString()));
 }