Skip to content

Commit

Permalink
Merge pull request #537 from Orckestra/dev
Browse files Browse the repository at this point in the history
Fix #535 Custom 404 page resolution does not take the language specified in URL
  • Loading branch information
napernik authored Jan 25, 2018
2 parents 146aa7c + 6a61f81 commit c5bfe27
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
44 changes: 25 additions & 19 deletions Composite/Core/Routing/HostnameBindingsFacade.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Globalization;
using System.Linq;
using System.Web;
Expand All @@ -8,6 +8,7 @@
using Composite.Core.WebClient;
using Composite.Data;
using Composite.Data.Types;
using Composite.Plugins.Routing.Pages;

namespace Composite.Core.Routing
{
Expand Down Expand Up @@ -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<IHostnameBinding>().AsEnumerable().FirstOrDefault(b => b.Hostname == host);
Expand All @@ -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<IHostnameBinding>(true).AsEnumerable())
{
Expand All @@ -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())
{
Expand All @@ -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;
}
Expand All @@ -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))
{
Expand All @@ -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))
{
Expand All @@ -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("/"))
Expand Down
4 changes: 2 additions & 2 deletions Composite/Plugins/Routing/Pages/DefaultPageUrlProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit c5bfe27

Please sign in to comment.