Skip to content

Commit 4ec37b9

Browse files
committed
Fixes: U4-5391 Create config to explicitly set the 'base url' for a website that is not running as a load balanced site - used for scheduled tasks/publishing/keepalive
1 parent 10bc7d0 commit 4ec37b9

19 files changed

+2178
-2096
lines changed

src/Umbraco.Core/Configuration/UmbracoSettings/IScheduledTasksSection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ namespace Umbraco.Core.Configuration.UmbracoSettings
55
public interface IScheduledTasksSection : IUmbracoConfigurationSection
66
{
77
IEnumerable<IScheduledTask> Tasks { get; }
8+
9+
string BaseUrl { get; }
810
}
911
}

src/Umbraco.Core/Configuration/UmbracoSettings/ScheduledTasksElement.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@ IEnumerable<IScheduledTask> IScheduledTasksSection.Tasks
1616
{
1717
get { return Tasks; }
1818
}
19+
20+
[ConfigurationProperty("baseUrl", IsRequired = false, DefaultValue = null)]
21+
public string BaseUrl
22+
{
23+
get { return (string)base["baseUrl"]; }
24+
}
1925
}
2026
}

src/Umbraco.Core/Sync/ServerEnvironmentHelper.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,18 @@ public static string GetCurrentServerUmbracoBaseUrl(ApplicationContext appContex
2626

2727
if (status == CurrentServerEnvironmentStatus.Single)
2828
{
29-
// single install, return null if no original url, else use original url as base
29+
// single install, return null if no config/original url, else use config/original url as base
3030
// use http or https as appropriate
31-
return string.IsNullOrWhiteSpace(appContext.OriginalRequestUrl)
32-
? null // not initialized yet
33-
: string.Format("http{0}://{1}", GlobalSettings.UseSSL ? "s" : "", appContext.OriginalRequestUrl);
31+
return GetBaseUrl(appContext, settings);
3432
}
3533

3634
var servers = settings.DistributedCall.Servers.ToArray();
3735

3836
if (servers.Any() == false)
3937
{
40-
// cannot be determined, return null if no original url, else use original url as base
38+
// cannot be determined, return null if no config/original url, else use config/original url as base
4139
// use http or https as appropriate
42-
return string.IsNullOrWhiteSpace(appContext.OriginalRequestUrl)
43-
? null // not initialized yet
44-
: string.Format("http{0}://{1}", GlobalSettings.UseSSL ? "s" : "", appContext.OriginalRequestUrl);
40+
return GetBaseUrl(appContext, settings);
4541
}
4642

4743
foreach (var server in servers)
@@ -66,11 +62,9 @@ public static string GetCurrentServerUmbracoBaseUrl(ApplicationContext appContex
6662
}
6763
}
6864

69-
// cannot be determined, return null if no original url, else use original url as base
65+
// cannot be determined, return null if no config/original url, else use config/original url as base
7066
// use http or https as appropriate
71-
return string.IsNullOrWhiteSpace(appContext.OriginalRequestUrl)
72-
? null // not initialized yet
73-
: string.Format("http{0}://{1}", GlobalSettings.UseSSL ? "s" : "", appContext.OriginalRequestUrl);
67+
return GetBaseUrl(appContext, settings);
7468
}
7569

7670
/// <summary>
@@ -119,5 +113,21 @@ public static CurrentServerEnvironmentStatus GetStatus(IUmbracoSettingsSection s
119113

120114
return CurrentServerEnvironmentStatus.Slave;
121115
}
116+
117+
private static string GetBaseUrl(ApplicationContext appContext, IUmbracoSettingsSection settings)
118+
{
119+
return (
120+
// is config empty?
121+
settings.ScheduledTasks.BaseUrl.IsNullOrWhiteSpace()
122+
// is the orig req empty?
123+
? appContext.OriginalRequestUrl.IsNullOrWhiteSpace()
124+
// we've got nothing
125+
? null
126+
//the orig req url is not null, use that
127+
: string.Format("http{0}://{1}", GlobalSettings.UseSSL ? "s" : "", appContext.OriginalRequestUrl)
128+
// the config has been specified, use that
129+
: string.Format("http{0}://{1}", GlobalSettings.UseSSL ? "s" : "", settings.ScheduledTasks.BaseUrl))
130+
.EnsureEndsWith('/');
131+
}
122132
}
123133
}
Lines changed: 118 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,119 @@
1-
using System.Configuration;
2-
using System.Web.Routing;
3-
using NUnit.Framework;
4-
using Umbraco.Core.Configuration;
5-
using Umbraco.Core.IO;
6-
using Umbraco.Tests.TestHelpers;
7-
using System.Web.Mvc;
8-
9-
namespace Umbraco.Tests
10-
{
11-
[TestFixture]
12-
public class GlobalSettingsTests : BaseWebTest
13-
{
14-
15-
public override void Initialize()
16-
{
17-
base.Initialize();
18-
SettingsForTests.UmbracoPath = "~/umbraco";
19-
}
20-
21-
public override void TearDown()
22-
{
23-
//ensure this is reset
24-
SystemDirectories.Root = null;
25-
SettingsForTests.UmbracoPath = "~/umbraco";
26-
//reset the app config
27-
base.TearDown();
28-
29-
}
30-
31-
[Test]
32-
public void Is_Debug_Mode()
33-
{
34-
Assert.That(Umbraco.Core.Configuration.GlobalSettings.DebugMode, Is.EqualTo(true));
35-
}
36-
37-
[Ignore]
38-
[Test]
39-
public void Is_Version_From_Assembly_Correct()
40-
{
41-
Assert.That(UmbracoVersion.Current.ToString(3), Is.EqualTo("6.0.0"));
42-
}
43-
44-
[TestCase("~/umbraco", "/", "umbraco")]
45-
[TestCase("~/umbraco", "/MyVirtualDir", "umbraco")]
46-
[TestCase("~/customPath", "/MyVirtualDir/", "custompath")]
47-
[TestCase("~/some-wacky/nestedPath", "/MyVirtualDir", "some-wacky-nestedpath")]
48-
[TestCase("~/some-wacky/nestedPath", "/MyVirtualDir/NestedVDir/", "some-wacky-nestedpath")]
49-
public void Umbraco_Mvc_Area(string path, string rootPath, string outcome)
50-
{
51-
SettingsForTests.UmbracoPath = path;
52-
SystemDirectories.Root = rootPath;
53-
Assert.AreEqual(outcome, Umbraco.Core.Configuration.GlobalSettings.UmbracoMvcArea);
54-
}
55-
56-
[TestCase("/umbraco/umbraco.aspx")]
57-
[TestCase("/umbraco/editContent.aspx")]
58-
[TestCase("/install/default.aspx")]
59-
[TestCase("/install/")]
60-
[TestCase("/install")]
61-
[TestCase("/install/?installStep=asdf")]
62-
[TestCase("/install/test.aspx")]
63-
[TestCase("/config/splashes/booting.aspx")]
64-
public void Is_Reserved_Path_Or_Url(string url)
65-
{
66-
Assert.IsTrue(Umbraco.Core.Configuration.GlobalSettings.IsReservedPathOrUrl(url));
67-
}
68-
69-
[TestCase("/umbraco_client/Tree/treeIcons.css")]
70-
[TestCase("/umbraco_client/Tree/Themes/umbraco/style.css")]
71-
[TestCase("/umbraco_client/scrollingmenu/style.css")]
72-
[TestCase("/base/somebasehandler")]
73-
[TestCase("/")]
74-
[TestCase("/home.aspx")]
75-
[TestCase("/umbraco-test")]
76-
[TestCase("/install-test")]
77-
[TestCase("/install.aspx")]
78-
public void Is_Not_Reserved_Path_Or_Url(string url)
79-
{
80-
Assert.IsFalse(Umbraco.Core.Configuration.GlobalSettings.IsReservedPathOrUrl(url));
81-
}
82-
83-
84-
[TestCase("/Do/Not/match", false)]
85-
[TestCase("/Umbraco/RenderMvcs", false)]
86-
[TestCase("/Umbraco/RenderMvc", true)]
87-
[TestCase("/Umbraco/RenderMvc/Index", true)]
88-
[TestCase("/Umbraco/RenderMvc/Index/1234", true)]
89-
[TestCase("/Umbraco/RenderMvc/Index/1234/9876", false)]
90-
[TestCase("/api", true)]
91-
[TestCase("/api/WebApiTest", true)]
92-
[TestCase("/api/WebApiTest/1234", true)]
93-
[TestCase("/api/WebApiTest/Index/1234", false)]
94-
public void Is_Reserved_By_Route(string url, bool shouldMatch)
95-
{
96-
//reset the app config, we only want to test routes not the hard coded paths
97-
Umbraco.Core.Configuration.GlobalSettings.ReservedPaths = "";
98-
Umbraco.Core.Configuration.GlobalSettings.ReservedUrls = "";
99-
100-
var routes = new RouteCollection();
101-
102-
routes.MapRoute(
103-
"Umbraco_default",
104-
"Umbraco/RenderMvc/{action}/{id}",
105-
new { controller = "RenderMvc", action = "Index", id = UrlParameter.Optional });
106-
routes.MapRoute(
107-
"WebAPI",
108-
"api/{controller}/{id}",
109-
new { controller = "WebApiTestController", action = "Index", id = UrlParameter.Optional });
110-
111-
112-
var context = new FakeHttpContextFactory(url);
113-
114-
115-
Assert.AreEqual(
116-
shouldMatch,
117-
Umbraco.Core.Configuration.GlobalSettings.IsReservedPathOrUrl(url, context.HttpContext, routes));
118-
}
119-
}
1+
using System.Web.Mvc;
2+
using System.Web.Routing;
3+
using NUnit.Framework;
4+
using Umbraco.Core.Configuration;
5+
using Umbraco.Core.IO;
6+
using Umbraco.Tests.TestHelpers;
7+
8+
namespace Umbraco.Tests.Configurations
9+
{
10+
[TestFixture]
11+
public class GlobalSettingsTests : BaseWebTest
12+
{
13+
14+
public override void Initialize()
15+
{
16+
base.Initialize();
17+
SettingsForTests.UmbracoPath = "~/umbraco";
18+
}
19+
20+
public override void TearDown()
21+
{
22+
//ensure this is reset
23+
SystemDirectories.Root = null;
24+
SettingsForTests.UmbracoPath = "~/umbraco";
25+
//reset the app config
26+
base.TearDown();
27+
28+
}
29+
30+
[Test]
31+
public void Is_Debug_Mode()
32+
{
33+
Assert.That(Umbraco.Core.Configuration.GlobalSettings.DebugMode, Is.EqualTo(true));
34+
}
35+
36+
[Ignore]
37+
[Test]
38+
public void Is_Version_From_Assembly_Correct()
39+
{
40+
Assert.That(UmbracoVersion.Current.ToString(3), Is.EqualTo("6.0.0"));
41+
}
42+
43+
[TestCase("~/umbraco", "/", "umbraco")]
44+
[TestCase("~/umbraco", "/MyVirtualDir", "umbraco")]
45+
[TestCase("~/customPath", "/MyVirtualDir/", "custompath")]
46+
[TestCase("~/some-wacky/nestedPath", "/MyVirtualDir", "some-wacky-nestedpath")]
47+
[TestCase("~/some-wacky/nestedPath", "/MyVirtualDir/NestedVDir/", "some-wacky-nestedpath")]
48+
public void Umbraco_Mvc_Area(string path, string rootPath, string outcome)
49+
{
50+
SettingsForTests.UmbracoPath = path;
51+
SystemDirectories.Root = rootPath;
52+
Assert.AreEqual(outcome, Umbraco.Core.Configuration.GlobalSettings.UmbracoMvcArea);
53+
}
54+
55+
[TestCase("/umbraco/umbraco.aspx")]
56+
[TestCase("/umbraco/editContent.aspx")]
57+
[TestCase("/install/default.aspx")]
58+
[TestCase("/install/")]
59+
[TestCase("/install")]
60+
[TestCase("/install/?installStep=asdf")]
61+
[TestCase("/install/test.aspx")]
62+
[TestCase("/config/splashes/booting.aspx")]
63+
public void Is_Reserved_Path_Or_Url(string url)
64+
{
65+
Assert.IsTrue(Umbraco.Core.Configuration.GlobalSettings.IsReservedPathOrUrl(url));
66+
}
67+
68+
[TestCase("/umbraco_client/Tree/treeIcons.css")]
69+
[TestCase("/umbraco_client/Tree/Themes/umbraco/style.css")]
70+
[TestCase("/umbraco_client/scrollingmenu/style.css")]
71+
[TestCase("/base/somebasehandler")]
72+
[TestCase("/")]
73+
[TestCase("/home.aspx")]
74+
[TestCase("/umbraco-test")]
75+
[TestCase("/install-test")]
76+
[TestCase("/install.aspx")]
77+
public void Is_Not_Reserved_Path_Or_Url(string url)
78+
{
79+
Assert.IsFalse(Umbraco.Core.Configuration.GlobalSettings.IsReservedPathOrUrl(url));
80+
}
81+
82+
83+
[TestCase("/Do/Not/match", false)]
84+
[TestCase("/Umbraco/RenderMvcs", false)]
85+
[TestCase("/Umbraco/RenderMvc", true)]
86+
[TestCase("/Umbraco/RenderMvc/Index", true)]
87+
[TestCase("/Umbraco/RenderMvc/Index/1234", true)]
88+
[TestCase("/Umbraco/RenderMvc/Index/1234/9876", false)]
89+
[TestCase("/api", true)]
90+
[TestCase("/api/WebApiTest", true)]
91+
[TestCase("/api/WebApiTest/1234", true)]
92+
[TestCase("/api/WebApiTest/Index/1234", false)]
93+
public void Is_Reserved_By_Route(string url, bool shouldMatch)
94+
{
95+
//reset the app config, we only want to test routes not the hard coded paths
96+
Umbraco.Core.Configuration.GlobalSettings.ReservedPaths = "";
97+
Umbraco.Core.Configuration.GlobalSettings.ReservedUrls = "";
98+
99+
var routes = new RouteCollection();
100+
101+
routes.MapRoute(
102+
"Umbraco_default",
103+
"Umbraco/RenderMvc/{action}/{id}",
104+
new { controller = "RenderMvc", action = "Index", id = UrlParameter.Optional });
105+
routes.MapRoute(
106+
"WebAPI",
107+
"api/{controller}/{id}",
108+
new { controller = "WebApiTestController", action = "Index", id = UrlParameter.Optional });
109+
110+
111+
var context = new FakeHttpContextFactory(url);
112+
113+
114+
Assert.AreEqual(
115+
shouldMatch,
116+
Umbraco.Core.Configuration.GlobalSettings.IsReservedPathOrUrl(url, context.HttpContext, routes));
117+
}
118+
}
120119
}

0 commit comments

Comments
 (0)