static bool CheckForHostnameAliasRedirect(HttpContext httpContext)
        {
            if (UrlUtils.IsAdminConsoleRequest(httpContext) ||
                UrlUtils.IsRendererRequest(httpContext) ||
                new UrlSpace(httpContext).ForceRelativeUrls)
            {
                return(false);
            }

            var hostnameBinding = HostnameBindingsFacade.GetAliasBinding(httpContext);

            if (hostnameBinding == null)
            {
                return(false);
            }

            string hostname = httpContext.Request.Url.Host.ToLowerInvariant();

            var request = httpContext.Request;

            string newUrl = request.Url.AbsoluteUri.Replace("://" + hostname, "://" + hostnameBinding.Hostname);

            httpContext.Response.Redirect(newUrl, false);
            httpContext.ApplicationInstance.CompleteRequest();
            return(true);
        }
        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            if (!SystemSetupFacade.IsSystemFirstTimeInitialized)
            {
                return;
            }

            // Left for backward compatibility with Contrib master pages support, to be removed
            // when support for master pages is implemented in C1
            // RenderingContext.PreRenderRedirectCheck() does the same logic
            var httpContext = (sender as HttpApplication).Context;

            var page = httpContext.Handler as System.Web.UI.Page;

            if (page == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(C1PageRoute.GetPathInfo()))
            {
                page.PreRender += (a, b) => CheckThatPathInfoHasBeenUsed(httpContext, page);
            }

            // Setting 404 response code if it is a request to a custom "Page not found" page
            if (HostnameBindingsFacade.IsPageNotFoundRequest())
            {
                page.PreRender += (a, b) =>
                {
                    httpContext.Response.TrySkipIisCustomErrors = true;
                    httpContext.Response.StatusCode             = 404;
                };
            }
        }
        /// <exclude />
        public static void ApplicationStartInitialize(bool displayDebugInfo = false)
        {
            ThreadDataManager.InitializeThroughHttpContext();

            if (displayDebugInfo)
            {
                Log.LogVerbose("Global.asax", "--- Web Application Start, {0} Id = {1} ---", DateTime.Now.ToLongTimeString(), AppDomain.CurrentDomain.Id);
            }

            PerformanceCounterFacade.SystemStartupIncrement();

            using (GlobalInitializerFacade.GetPreInitHandlersScope())
            {
                ApplicationStartupFacade.FireConfigureServices(ServiceLocator.ServiceCollection);

                ServiceLocator.BuildServiceProvider();
                ServiceLocator.CreateRequestServicesScope(HttpContext.Current);

                ApplicationStartupFacade.FireBeforeSystemInitialize(ServiceLocator.ServiceProvider);
            }

            TempDirectoryFacade.OnApplicationStart();

            HostnameBindingsFacade.Initialize();

            ApplicationStartupFacade.FireSystemInitialized(ServiceLocator.ServiceProvider);

            ThreadDataManager.FinalizeThroughHttpContext();
        }
示例#4
0
        /// <summary>
        /// Redirects to 404 page if PathInfo wasn't used. Adds 404 status code if the current page is specified as 404 page in hostname binding.
        /// Note that all C1 functions on page have to be executed before calling this method.
        /// </summary>
        /// <returns><c>True</c> if the request was transferred to a 404 page and rendering should be stopped.</returns>
        public bool PreRenderRedirectCheck()
        {
            var httpContext = HttpContext.Current;

            if (!C1PageRoute.GetPathInfo().IsNullOrEmpty() &&
                !C1PageRoute.PathInfoUsed)
            {
                // Redirecting to PageNotFoundUrl or setting 404 response code if PathInfo url part hasn't been used
                if (HostnameBindingsFacade.ServeCustomPageNotFoundPage(httpContext))
                {
                    return(true);
                }

                httpContext.Response.StatusCode = 404;
                httpContext.Response.End();
            }

            // Setting 404 response code if it is a request to a custom "Page not found" page
            if (HostnameBindingsFacade.IsPageNotFoundRequest())
            {
                httpContext.Response.StatusCode = 404;
            }

            return(false);
        }
        static void SetCultureByHostname()
        {
            IHostnameBinding hostnameBinding = HostnameBindingsFacade.GetBindingForCurrentRequest();

            if (hostnameBinding != null && !hostnameBinding.Culture.IsNullOrEmpty())
            {
                var cultureInfo = new CultureInfo(hostnameBinding.Culture);
                var thread      = System.Threading.Thread.CurrentThread;
                thread.CurrentCulture   = cultureInfo;
                thread.CurrentUICulture = cultureInfo;
            }
        }
        private static void CheckThatPathInfoHasBeenUsed(HttpContext httpContext, System.Web.UI.Page page)
        {
            if (C1PageRoute.PathInfoUsed)
            {
                return;
            }

            // Redirecting to PageNotFoundUrl or setting 404 response code if PathInfo url part hasn't been used
            if (!HostnameBindingsFacade.ServeCustomPageNotFoundPage(httpContext))
            {
                page.Response.StatusCode = 404;
            }

            page.Response.End();
        }
示例#7
0
        private static string GetPagePreviewUrl(PageUrlData pageUrlData)
        {
            var httpContext = HttpContext.Current;

            var urlSpace = new UrlSpace();

            if (HostnameBindingsFacade.GetBindingForCurrentRequest() != null ||
                HostnameBindingsFacade.GetAliasBinding(httpContext) != null)
            {
                urlSpace.ForceRelativeUrls = true;
            }

            return(PageUrls.BuildUrl(pageUrlData, UrlKind.Public, urlSpace)
                   ?? PageUrls.BuildUrl(pageUrlData, UrlKind.Renderer, urlSpace));
        }
        void context_BeginRequest(object sender, EventArgs e)
        {
            if (!SystemSetupFacade.IsSystemFirstTimeInitialized)
            {
                return;
            }

            ThreadDataManager.InitializeThroughHttpContext();

            var httpContext = (sender as HttpApplication).Context;

            if (CheckForHostnameAliasRedirect(httpContext))
            {
                return;
            }

            IHostnameBinding hostnameBinding = HostnameBindingsFacade.GetBindingForCurrentRequest();

            if (hostnameBinding != null &&
                hostnameBinding.EnforceHttps &&
                !httpContext.Request.IsSecureConnection)
            {
                RedirectToHttps(httpContext);
                return;
            }

            if (HandleMediaRequest(httpContext))
            {
                return;
            }

            SetCultureByHostname(hostnameBinding);

            PrettifyPublicMarkup(httpContext);

            HandleRootRequestInClassicMode(httpContext);
        }
        /// <exclude />
        public static void Application_Error(object sender, EventArgs e)
        {
            var       httpApplication = (HttpApplication)sender;
            Exception exception       = httpApplication.Server.GetLastError();

            var eventType = TraceEventType.Error;

            var httpContext = httpApplication.Context;

            if (httpContext != null)
            {
                bool is404 = exception is HttpException httpException &&
                             httpException.GetHttpCode() == 404;

                if (is404)
                {
                    string rawUrl = httpContext.Request.RawUrl;

                    if (!UrlUtils.IsAdminConsoleRequest(rawUrl))
                    {
                        string customPageNotFoundUrl = HostnameBindingsFacade.GetCustomPageNotFoundUrl();

                        if (!customPageNotFoundUrl.IsNullOrEmpty())
                        {
                            if (rawUrl == customPageNotFoundUrl)
                            {
                                throw new HttpException(500, $"'Page not found' url isn't handled. Url: '{rawUrl}'");
                            }

                            httpContext.Server.ClearError();
                            httpContext.Response.Clear();

                            httpContext.Response.Redirect(customPageNotFoundUrl, true);

                            return;
                        }

                        eventType = TraceEventType.Verbose;
                    }
                }

                // Logging request url
                if (LogApplicationLevelErrors)
                {
                    HttpRequest request = null;

                    try
                    {
                        request = httpContext.Request;
                    }
                    catch
                    {
                        // Request may not be available at this point
                    }

                    if (request != null)
                    {
                        LoggingService.LogEntry("Application Error",
                                                $"Failed to process '{request.RequestType}' request to url '{request.RawUrl}'",
                                                LoggingService.Category.General, eventType);
                    }
                }
            }

            if (LogApplicationLevelErrors)
            {
                while (exception != null)
                {
                    LoggingService.LogEntry("Application Error", exception.ToString(), LoggingService.Category.General, eventType);
                    exception = exception.InnerException;
                }
            }
        }