From 6a61f81980ba4097625e15a20498200d3e904295 Mon Sep 17 00:00:00 2001 From: Dmitry Dzygin Date: Thu, 25 Jan 2018 15:40:49 +0100 Subject: [PATCH] Fix #535 Custom 404 page resolution does not take the language specified in URL into account --- .../Core/Routing/HostnameBindingsFacade.cs | 44 +++++++++++-------- .../Routing/Pages/DefaultPageUrlProvider.cs | 4 +- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/Composite/Core/Routing/HostnameBindingsFacade.cs b/Composite/Core/Routing/HostnameBindingsFacade.cs index b69543ef37..80d2b40348 100644 --- a/Composite/Core/Routing/HostnameBindingsFacade.cs +++ b/Composite/Core/Routing/HostnameBindingsFacade.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Globalization; using System.Linq; using System.Web; @@ -8,6 +8,7 @@ using Composite.Core.WebClient; using Composite.Data; using Composite.Data.Types; +using Composite.Plugins.Routing.Pages; namespace Composite.Core.Routing { @@ -67,14 +68,14 @@ public static void Initialize() internal static IHostnameBinding GetBindingForCurrentRequest() { - var httpContext = HttpContext.Current; - if(httpContext == null) - { - return null; - } + return GetHostnameBinding(HttpContext.Current); + } + private static IHostnameBinding GetHostnameBinding(HttpContext httpContext) + { + if(httpContext == null) return null; - string host = HttpContext.Current.Request.Url.Host; + string host = httpContext.Request.Url.Host; // TODO: optimize? return DataFacade.GetData().AsEnumerable().FirstOrDefault(b => b.Hostname == host); @@ -87,7 +88,7 @@ internal static IHostnameBinding GetAliasBinding(HttpContext httpContext) return null; } - string hostname = HttpContext.Current.Request.Url.Host.ToLowerInvariant(); + string hostname = httpContext.Request.Url.Host.ToLowerInvariant(); foreach (var hostnameBinding in DataFacade.GetData(true).AsEnumerable()) { @@ -105,13 +106,13 @@ internal static IHostnameBinding GetAliasBinding(HttpContext httpContext) internal static bool IsPageNotFoundRequest() { - HttpContext context = HttpContext.Current; + var context = HttpContext.Current; if(context == null) { return false; } - string customPageNotFoundUrl = HostnameBindingsFacade.GetCustomPageNotFoundUrl(); + string customPageNotFoundUrl = GetCustomPageNotFoundUrl(context); if (customPageNotFoundUrl.IsNullOrEmpty()) { @@ -132,10 +133,15 @@ internal static bool IsPageNotFoundRequest() || request.Url.PathAndQuery.StartsWith(customPageNotFoundUrl + "?"); } - internal static string GetCustomPageNotFoundUrl() + internal static string GetCustomPageNotFoundUrl() => GetCustomPageNotFoundUrl(HttpContext.Current); + + + private static string GetCustomPageNotFoundUrl(HttpContext httpContext) { - var binding = GetBindingForCurrentRequest(); - if(binding == null || string.IsNullOrEmpty(binding.PageNotFoundUrl)) + if (httpContext == null) return null; + + var binding = GetHostnameBinding(httpContext); + if(string.IsNullOrEmpty(binding?.PageNotFoundUrl)) { return null; } @@ -144,10 +150,10 @@ internal static string GetCustomPageNotFoundUrl() var defaultCulture = DataLocalizationFacade.DefaultLocalizationCulture; - var pageUrlData = C1PageRoute.PageUrlData; - CultureInfo localeFromRequest = pageUrlData != null - ? pageUrlData.LocalizationScope - : defaultCulture; + CultureInfo localeFromRequest = + C1PageRoute.PageUrlData?.LocalizationScope + ?? DefaultPageUrlProvider.GetCultureInfo(httpContext.Request.FilePath, binding, out _) + ?? defaultCulture; using (new DataConnection(localeFromRequest)) { @@ -171,7 +177,7 @@ internal static bool ServeCustomPageNotFoundPage(HttpContext httpContext) { string rawUrl = httpContext.Request.RawUrl; - string customPageNotFoundUrl = HostnameBindingsFacade.GetCustomPageNotFoundUrl(); + string customPageNotFoundUrl = GetCustomPageNotFoundUrl(httpContext); if (string.IsNullOrEmpty(customPageNotFoundUrl)) { @@ -180,7 +186,7 @@ internal static bool ServeCustomPageNotFoundPage(HttpContext httpContext) if (rawUrl == customPageNotFoundUrl || httpContext.Request.Url.PathAndQuery == customPageNotFoundUrl) { - throw new HttpException(404, "'Page not found' wasn't handled. Url: '{0}'".FormatWith(rawUrl)); + throw new HttpException(404, $"'Page not found' wasn't handled. Url: '{rawUrl}'"); } if (HttpRuntime.UsingIntegratedPipeline && customPageNotFoundUrl.StartsWith("/")) diff --git a/Composite/Plugins/Routing/Pages/DefaultPageUrlProvider.cs b/Composite/Plugins/Routing/Pages/DefaultPageUrlProvider.cs index 14dbc8315c..03d982189c 100644 --- a/Composite/Plugins/Routing/Pages/DefaultPageUrlProvider.cs +++ b/Composite/Plugins/Routing/Pages/DefaultPageUrlProvider.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; @@ -598,7 +598,7 @@ private static string RemoveUrlMarkers(string filePath, UrlSpace urlSpace) return filePath; } - CultureInfo GetCultureInfo(string requestPath, IHostnameBinding hostnameBinding, out string pathWithoutLanguageAndAppRoot) + internal static CultureInfo GetCultureInfo(string requestPath, IHostnameBinding hostnameBinding, out string pathWithoutLanguageAndAppRoot) { int startIndex = requestPath.IndexOf('/', UrlUtils.PublicRootPath.Length) + 1;