From f67e6b3695d1cc488d01ed5a12bd54ed2d0405f9 Mon Sep 17 00:00:00 2001 From: Matteo Piovanelli Date: Fri, 18 Sep 2020 08:40:44 +0200 Subject: [PATCH] Upgrade host resolution (#8387) * Reverted changes to RunningShellTable and then changed the way shells are sorted, so we can correctly give "priority" to tenants based on their prefix. Added test adapter reference to Orchard.Framework.Tests so tests can be run in the latest VS 2017. Fixed a test that was failing to account for the order the shells were being processed. * Removed some stuff from csproj that vs had added --- .../Environment/RunningShellTableTests.cs | 3 ++- .../Orchard.Framework.Tests.csproj | 1 + src/Orchard.Tests/packages.config | 1 + src/Orchard/Environment/RunningShellTable.cs | 27 +++++++++++-------- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/Orchard.Tests/Environment/RunningShellTableTests.cs b/src/Orchard.Tests/Environment/RunningShellTableTests.cs index 0d5097128..88fe962a5 100644 --- a/src/Orchard.Tests/Environment/RunningShellTableTests.cs +++ b/src/Orchard.Tests/Environment/RunningShellTableTests.cs @@ -128,9 +128,10 @@ namespace Orchard.Tests.Environment { var settingsG = new ShellSettings { Name = "Gamma", RequestUrlHost = "wiki.example.com" }; var settingsD = new ShellSettings { Name = "Delta", RequestUrlPrefix = "Quux" }; table.Add(settings); + // add this shell first, because the order the shells where processed used to matter + table.Add(settingsG); table.Add(settingsA); table.Add(settingsB); - table.Add(settingsG); table.Add(settingsD); Assert.That(table.Match(new StubHttpContext("~/foo/bar", "wiki.example.com")), Is.EqualTo(settingsA).Using(new ShellComparer())); diff --git a/src/Orchard.Tests/Orchard.Framework.Tests.csproj b/src/Orchard.Tests/Orchard.Framework.Tests.csproj index 89155c400..a3ccd07a6 100644 --- a/src/Orchard.Tests/Orchard.Framework.Tests.csproj +++ b/src/Orchard.Tests/Orchard.Framework.Tests.csproj @@ -1,5 +1,6 @@  + Debug AnyCPU diff --git a/src/Orchard.Tests/packages.config b/src/Orchard.Tests/packages.config index c43cb3d10..786c65803 100644 --- a/src/Orchard.Tests/packages.config +++ b/src/Orchard.Tests/packages.config @@ -17,4 +17,5 @@ + \ No newline at end of file diff --git a/src/Orchard/Environment/RunningShellTable.cs b/src/Orchard/Environment/RunningShellTable.cs index b5322f7c4..b4c2f1ee7 100644 --- a/src/Orchard/Environment/RunningShellTable.cs +++ b/src/Orchard/Environment/RunningShellTable.cs @@ -84,7 +84,12 @@ namespace Orchard.Environment { .Select(h => new ShellSettings(s) {RequestUrlHost = h})) .GroupBy(s => s.RequestUrlHost ?? string.Empty) .OrderByDescending(g => g.Key.Length) - .ToDictionary(x => x.Key, x => x.AsEnumerable(), StringComparer.OrdinalIgnoreCase); + .ToDictionary( + x => x.Key, + // we want to keep this ordered so that, for the same host, shells with a configured + // RequestUrlPrefix are tested first when trying to match them to coming requests. + x => x.OrderByDescending(ss => (ss.RequestUrlPrefix ?? "").Length).AsEnumerable(), + StringComparer.OrdinalIgnoreCase); if (unqualified.Count() == 1) { // only one shell had no request url criteria @@ -164,20 +169,20 @@ namespace Orchard.Environment { shells = _shellsByHost[subHostKey]; } } - + // looking for a request url prefix match var mostQualifiedMatch = shells.FirstOrDefault(settings => { - if (settings.State == TenantState.Disabled) { - return false; - } + if (settings.State == TenantState.Disabled) { + return false; + } - if (String.IsNullOrWhiteSpace(settings.RequestUrlPrefix)) { - return true; - } - - return key.Equals(host + "/" + settings.RequestUrlPrefix, StringComparison.OrdinalIgnoreCase); - }); + if (String.IsNullOrWhiteSpace(settings.RequestUrlPrefix)) { + return true; + } + return key.Equals(host + "/" + settings.RequestUrlPrefix, StringComparison.OrdinalIgnoreCase); + }); + return mostQualifiedMatch ?? _fallback; });