Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
Add IPv6 loopback address by default #1434
Browse files Browse the repository at this point in the history
  • Loading branch information
JunTaoLuo committed Mar 7, 2017
1 parent b409746 commit 6c8fa02
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static class Constants
/// <summary>
/// The IPEndPoint Kestrel will bind to if nothing else is specified.
/// </summary>
public static readonly IPEndPoint DefaultIPEndPoint = new IPEndPoint(IPAddress.Loopback, 5000);
public static readonly string DefaultServerAddress = "http://localhost:5000";

/// <summary>
/// Prefix of host name used to specify Unix sockets in the configuration.
Expand Down
12 changes: 10 additions & 2 deletions src/Microsoft.AspNetCore.Server.Kestrel/KestrelServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,16 @@ public void Start<TContext>(IHttpApplication<TContext> application)
}
else if (!hasListenOptions && !hasServerAddresses)
{
_logger.LogDebug($"No listening endpoints were configured. Binding to {Constants.DefaultIPEndPoint} by default.");
listenOptions.Add(new ListenOptions(Constants.DefaultIPEndPoint));
_logger.LogDebug($"No listening endpoints were configured. Binding to {Constants.DefaultServerAddress} by default.");

// "localhost" for both IPv4 and IPv6 can't be represented as an IPEndPoint.
StartLocalhost(engine, ServerAddress.FromUrl(Constants.DefaultServerAddress));

// If StartLocalhost doesn't throw, there is at least one listener.
// The port cannot change for "localhost".
_serverAddresses.Addresses.Add(Constants.DefaultServerAddress);

return;
}
else if (!hasListenOptions)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,29 @@ private async Task RegisterIPEndPoint_Success(IPEndPoint endPoint, Func<IPEndPoi

[ConditionalFact]
[PortSupportedCondition(5000)]
public async Task DefaultsToPort5000()
public Task DefaultsServerAddress_BindsToIPv4()
{
return RegisterDefaultServerAddresses_Success(new[] { "http://127.0.0.1:5000" });
}

[ConditionalFact]
[IPv6SupportedCondition]
[PortSupportedCondition(5000)]
public Task DefaultsServerAddress_BindsToIPv6()
{
return RegisterDefaultServerAddresses_Success(new[] { "http://127.0.0.1:5000", "http://[::1]:5000" });
}

private async Task RegisterDefaultServerAddresses_Success(IEnumerable<string> addresses)
{
var testLogger = new TestApplicationErrorLogger();

var hostBuilder = new WebHostBuilder()
.UseKestrel()
.ConfigureServices(services =>
{
services.AddSingleton<ILoggerFactory>(new KestrelTestLoggerFactory(testLogger));
})
{
services.AddSingleton<ILoggerFactory>(new KestrelTestLoggerFactory(testLogger));
})
.Configure(ConfigureEchoAddress);

using (var host = hostBuilder.Build())
Expand All @@ -150,12 +163,12 @@ public async Task DefaultsToPort5000()

Assert.Equal(5000, host.GetPort());
Assert.Single(testLogger.Messages, log => log.LogLevel == LogLevel.Debug &&
string.Equals($"No listening endpoints were configured. Binding to {Constants.DefaultIPEndPoint} by default.",
string.Equals($"No listening endpoints were configured. Binding to {Constants.DefaultServerAddress} by default.",
log.Message, StringComparison.Ordinal));

foreach (var testUrl in new[] { "http://127.0.0.1:5000", /* "http://[::1]:5000" */})
foreach (var address in addresses)
{
Assert.Equal(new Uri(testUrl).ToString(), await HttpClientSlim.GetStringAsync(testUrl));
Assert.Equal(new Uri(address).ToString(), await HttpClientSlim.GetStringAsync(address));
}
}
}
Expand Down Expand Up @@ -373,8 +386,8 @@ public static TheoryData<string, Func<IServerAddressesFeature, string[]>> Addres
var dataset = new TheoryData<string, Func<IServerAddressesFeature, string[]>>();

// Default host and port
dataset.Add(null, _ => new[] { "http://127.0.0.1:5000/", /*"http://[::1]:5000/"*/ });
dataset.Add(string.Empty, _ => new[] { "http://127.0.0.1:5000/", /*"http://[::1]:5000/"*/ });
dataset.Add(null, _ => new[] { "http://127.0.0.1:5000/", "http://[::1]:5000/" });
dataset.Add(string.Empty, _ => new[] { "http://127.0.0.1:5000/", "http://[::1]:5000/" });

// Static ports
var port = GetNextPort();
Expand Down

0 comments on commit 6c8fa02

Please sign in to comment.