Add IPv6 loopback address by default #1434

This commit is contained in:
John Luo 2017-03-04 22:03:27 -08:00
parent 11c7eb5665
commit 06134bc6e0
3 changed files with 33 additions and 12 deletions

View File

@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure
/// <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.

View File

@ -142,8 +142,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel
}
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)
{

View File

@ -132,16 +132,29 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
[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())
@ -150,12 +163,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
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));
}
}
}
@ -373,8 +386,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
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();