diff --git a/src/Nancy.Hosting.Self/HostConfiguration.cs b/src/Nancy.Hosting.Self/HostConfiguration.cs index 4660c1d13b..7a6eb33b3d 100644 --- a/src/Nancy.Hosting.Self/HostConfiguration.cs +++ b/src/Nancy.Hosting.Self/HostConfiguration.cs @@ -17,6 +17,16 @@ public sealed class HostConfiguration /// public bool RewriteLocalhost { get; set; } + /// + /// Gets or sets a property indicating that + /// should use the weak wildcard (*) instead of the strong wildcard (+). + /// If you use the weak wildcard then Nancy will only receive requests that + /// have not matched any other URL prefix registration, in other words + /// requests that no other application wants to handle. + /// Defaults to false. + /// + public bool UseWeakWildcard { get; set; } + /// /// Configuration around automatically creating URL reservations. /// @@ -98,6 +108,7 @@ private static int ProcessorThreadCount public HostConfiguration() { this.RewriteLocalhost = true; + this.UseWeakWildcard = false; this.UrlReservations = new UrlReservations(); this.AllowChunkedEncoding = true; this.UnhandledExceptionCallback = e => diff --git a/src/Nancy.Hosting.Self/NancyHost.cs b/src/Nancy.Hosting.Self/NancyHost.cs index abbccece7b..b636506916 100644 --- a/src/Nancy.Hosting.Self/NancyHost.cs +++ b/src/Nancy.Hosting.Self/NancyHost.cs @@ -241,7 +241,7 @@ internal IEnumerable GetPrefixes() if (this.configuration.RewriteLocalhost && !baseUri.Host.Contains(".")) { - prefix = prefix.Replace("localhost", "+"); + prefix = prefix.Replace("localhost", this.configuration.UseWeakWildcard ? "*" : "+"); } yield return prefix; diff --git a/test/Nancy.Hosting.Self.Tests/NancySelfHostFixture.cs b/test/Nancy.Hosting.Self.Tests/NancySelfHostFixture.cs index 5ef262b2a3..b0d79d6151 100644 --- a/test/Nancy.Hosting.Self.Tests/NancySelfHostFixture.cs +++ b/test/Nancy.Hosting.Self.Tests/NancySelfHostFixture.cs @@ -248,6 +248,58 @@ public void Should_include_default_port_in_uri_prefixes() prefix.ShouldEqual("http://+:80/"); } + [Fact] + public void Should_use_strong_wildcard_in_url_prefixes_by_default() + { + // Given + var host = new NancyHost( + new HostConfiguration(), + new Uri("http://localhost/") + ); + + // When + var prefix = host.GetPrefixes().Single(); + + // Then + prefix.ShouldStartWith("http://+:"); + } + + [Fact] + public void Should_use_strong_wildcard_in_url_prefixes_when_configured() + { + // Given + var host = new NancyHost( + new HostConfiguration() { + UseWeakWildcard = false, + }, + new Uri("http://localhost/") + ); + + // When + var prefix = host.GetPrefixes().Single(); + + // Then + prefix.ShouldStartWith("http://+:"); + } + + [Fact] + public void Should_use_weak_wildcard_in_url_prefixes_when_configured() + { + // Given + var host = new NancyHost( + new HostConfiguration() { + UseWeakWildcard = true, + }, + new Uri("http://localhost/") + ); + + // When + var prefix = host.GetPrefixes().Single(); + + // Then + prefix.ShouldStartWith("http://*:"); + } + [Fact] public void Should_not_throw_when_disposed_without_starting() {