From 06134bc6e0795f0908c6dcdccec5915c3185dce3 Mon Sep 17 00:00:00 2001 From: John Luo Date: Sat, 4 Mar 2017 22:03:27 -0800 Subject: [PATCH] Add IPv6 loopback address by default #1434 --- .../Internal/Infrastructure/Constants.cs | 2 +- .../KestrelServer.cs | 12 +++++-- .../AddressRegistrationTests.cs | 31 +++++++++++++------ 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/Constants.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/Constants.cs index e0f592154b..2c201fea7c 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/Constants.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/Constants.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure /// /// The IPEndPoint Kestrel will bind to if nothing else is specified. /// - public static readonly IPEndPoint DefaultIPEndPoint = new IPEndPoint(IPAddress.Loopback, 5000); + public static readonly string DefaultServerAddress = "http://localhost:5000"; /// /// Prefix of host name used to specify Unix sockets in the configuration. diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/KestrelServer.cs b/src/Microsoft.AspNetCore.Server.Kestrel/KestrelServer.cs index 1d05a0dd2b..a36da353f4 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/KestrelServer.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/KestrelServer.cs @@ -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) { diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs index fd7b950e24..572b45aa68 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/AddressRegistrationTests.cs @@ -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 addresses) { var testLogger = new TestApplicationErrorLogger(); var hostBuilder = new WebHostBuilder() .UseKestrel() .ConfigureServices(services => - { - services.AddSingleton(new KestrelTestLoggerFactory(testLogger)); - }) + { + services.AddSingleton(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>(); // 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();