Beispiel #1
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));
            }
        }
        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);
        }
        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);
        }
        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 QueryStringCollection GetOriginalQueryCollectionCopy()
 {
     return(QueryStringCollection.Parse(OriginalUri.Query));
 }
Beispiel #6
0
 public static QueryStringCollection GetCurrentQueryCollection()
 {
     return(QueryStringCollection.Parse(GetCurrentQueryString()));
 }