/// <summary>
        /// Short URLs and moved pages are stored in a database. See whether there's a match, and redirect.
        /// </summary>
        /// <param name="requestedUrl">The requested URL.</param>
        private bool TryShortOrMovedUrl(Uri requestedUrl)
        {
            try
            {
                // Try to match the requested URL to a redirect and, if successful, run handlers for the redirect
                var matchers = new IRedirectMatcher[] { new SqlServerRedirectMatcher()
                                                        {
                                                            ThrowErrorOnMissingConfiguration = false
                                                        } };
                var handlers = new IRedirectHandler[] { new ConvertToAbsoluteUrlHandler(), new PreserveQueryStringHandler(), new DebugInfoHandler(), new GoToUrlHandler() };

                foreach (var matcher in matchers)
                {
                    var redirect = matcher.MatchRedirect(requestedUrl);
                    if (redirect != null)
                    {
                        foreach (var handler in handlers)
                        {
                            redirect = handler.HandleRedirect(redirect);
                        }
                        return(true);
                    }
                }
            }
            catch (SqlException ex)
            {
                // If there's a problem, publish the error and continue to show 404 page
                ex.ToExceptionless().Submit();
            }
            return(false);
        }
Exemple #2
0
 public RequestHandler(
     IRedirectHandler redirectHandler,
     IRequestLogger requestLogger,
     IConfiguration configuration)
 {
     _configuration   = configuration ?? throw new ArgumentNullException(nameof(configuration));
     _requestLogger   = requestLogger ?? throw new ArgumentNullException(nameof(requestLogger));
     _redirectHandler = redirectHandler ?? throw new ArgumentNullException(nameof(redirectHandler));
 }
        private void RedirectsModule_BeginRequest(object sender, EventArgs e)
        {          
            try
            {
                // If the requested path exists, or it's too long for MapPath to handle, do nothing
                try
                {
                    if (File.Exists(HostingEnvironment.MapPath(HttpContext.Current.Request.Url.AbsolutePath)))
                    {
                        return;
                    }
                }
                catch (PathTooLongException)
                {

                    return;
                }

                // If the requested path matches an ignored pattern, do nothing
                if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["Escc.Redirects.IgnorePaths"]))
                {
                    var ignorePaths = ConfigurationManager.AppSettings["Escc.Redirects.IgnorePaths"].Split(',');
                    foreach (var ignorePath in ignorePaths)
                    {
                        if (Regex.IsMatch(HttpContext.Current.Request.Url.PathAndQuery, ignorePath, RegexOptions.IgnoreCase))
                        {
                            return;
                        }
                    }
                }

                // Try to match the requested URL to a redirect and, if successful, run handlers for the redirect
                var matchers = new IRedirectMatcher[] {new SqlServerRedirectMatcher()};
                var handlers = new IRedirectHandler[] {new ConvertToAbsoluteUrlHandler(), new PreserveQueryStringHandler(), new DebugInfoHandler(), new GoToUrlHandler()};

                foreach (var matcher in matchers)
                {
                    var redirect = matcher.MatchRedirect(HttpContext.Current.Request.Url);
                    if (redirect != null)
                    {
                        foreach (var handler in handlers)
                        {
                            redirect = handler.HandleRedirect(redirect);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // If there's a problem, publish the error and continue
                ex.ToExceptionless().Submit();
            }
        }
        private void RedirectsModule_BeginRequest(object sender, EventArgs e)
        {
            try
            {
                // If the requested path exists, or it's too long for MapPath to handle, do nothing
                try
                {
                    if (File.Exists(HostingEnvironment.MapPath(HttpContext.Current.Request.Url.AbsolutePath)))
                    {
                        return;
                    }
                }
                catch (PathTooLongException)
                {
                    return;
                }

                // If the requested path matches an ignored pattern, do nothing
                if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["Escc.Redirects.IgnorePaths"]))
                {
                    var ignorePaths = ConfigurationManager.AppSettings["Escc.Redirects.IgnorePaths"].Split(',');
                    foreach (var ignorePath in ignorePaths)
                    {
                        if (Regex.IsMatch(HttpContext.Current.Request.Url.PathAndQuery, ignorePath, RegexOptions.IgnoreCase))
                        {
                            return;
                        }
                    }
                }

                // Try to match the requested URL to a redirect and, if successful, run handlers for the redirect
                var matchers = new IRedirectMatcher[] { new SqlServerRedirectMatcher() };
                var handlers = new IRedirectHandler[] { new ConvertToAbsoluteUrlHandler(), new PreserveQueryStringHandler(), new DebugInfoHandler(), new GoToUrlHandler() };

                foreach (var matcher in matchers)
                {
                    var redirect = matcher.MatchRedirect(HttpContext.Current.Request.Url);
                    if (redirect != null)
                    {
                        foreach (var handler in handlers)
                        {
                            redirect = handler.HandleRedirect(redirect);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // If there's a problem, publish the error and continue
                ex.ToExceptionless().Submit();
            }
        }
Exemple #5
0
 public RequestHandlerTests()
 {
     _redirectHandler = A.Fake <IRedirectHandler>();
     _requestLogger   = A.Fake <IRequestLogger>();
     _configuration   = A.Fake <IConfiguration>();
     _sut             = A.Fake <RequestHandler>(
         o => o.WithArgumentsForConstructor(new object[] { _redirectHandler, _requestLogger, _configuration })
         .CallsBaseMethods());
     _httpContext = new FakeHttpContext();
     _httpContext.Response.StatusCode = 404;
     WhenIsRemote();
     WhenIsNotResourceFile();
 }