Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

SELF HOST: Support weak and strong wildcards in URL prefixes #2978

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Nancy.Hosting.Self/HostConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ public sealed class HostConfiguration
/// </summary>
public bool RewriteLocalhost { get; set; }

/// <summary>
/// Gets or sets a property indicating that <see cref="RewriteLocalhost"/>
/// 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.
/// </summary>
public bool UseWeakWildcard { get; set; }

/// <summary>
/// Configuration around automatically creating URL reservations.
/// </summary>
Expand Down Expand Up @@ -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 =>
Expand Down
2 changes: 1 addition & 1 deletion src/Nancy.Hosting.Self/NancyHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ internal IEnumerable<string> GetPrefixes()

if (this.configuration.RewriteLocalhost && !baseUri.Host.Contains("."))
{
prefix = prefix.Replace("localhost", "+");
prefix = prefix.Replace("localhost", this.configuration.UseWeakWildcard ? "*" : "+");
}

yield return prefix;
Expand Down
52 changes: 52 additions & 0 deletions test/Nancy.Hosting.Self.Tests/NancySelfHostFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down