Skip to content

Commit

Permalink
Read HTTP_PORTS and HTTPS_PORTS into IServerAddresses (#44194)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher authored Sep 29, 2022
1 parent 38d3043 commit 0117723
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Hosting/Abstractions/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
static readonly Microsoft.AspNetCore.Hosting.WebHostDefaults.HttpPortsKey -> string!
static readonly Microsoft.AspNetCore.Hosting.WebHostDefaults.HttpsPortsKey -> string!
10 changes: 10 additions & 0 deletions src/Hosting/Abstractions/src/WebHostDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public static class WebHostDefaults
/// </summary>
public static readonly string ServerUrlsKey = "urls";

/// <summary>
/// The configuration key associated with the "http_ports" configuration.
/// </summary>
public static readonly string HttpPortsKey = "http_ports";

/// <summary>
/// The configuration key associated with the "https_ports" configuration.
/// </summary>
public static readonly string HttpsPortsKey = "https_ports";

/// <summary>
/// The configuration key associated with the "ContentRoot" configuration.
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions src/Hosting/Hosting/src/GenericHost/GenericWebHostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ public async Task StartAsync(CancellationToken cancellationToken)
urls = Options.WebHostOptions.ServerUrls;
}

var httpPorts = Configuration[WebHostDefaults.HttpPortsKey] ?? string.Empty;
var httpsPorts = Configuration[WebHostDefaults.HttpsPortsKey] ?? string.Empty;
if (string.IsNullOrEmpty(urls))
{
// HTTP_PORTS and HTTPS_PORTS, these are lower priority than Urls.
static string ExpandPorts(string ports, string scheme)
{
return string.Join(';',
ports.Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
.Select(port => $"{scheme}://*:{port}"));
}

var httpUrls = ExpandPorts(httpPorts, Uri.UriSchemeHttp);
var httpsUrls = ExpandPorts(httpsPorts, Uri.UriSchemeHttps);
urls = $"{httpUrls};{httpsUrls}";
}
else if (!string.IsNullOrEmpty(httpPorts) || !string.IsNullOrEmpty(httpsPorts))
{
Logger.PortsOverridenByUrls(httpPorts, httpsPorts, urls);
}

if (!string.IsNullOrEmpty(urls))
{
// We support reading "preferHostingUrls" from app configuration
Expand Down
7 changes: 7 additions & 0 deletions src/Hosting/Hosting/src/Internal/HostingLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,12 @@ public static void ApplicationError(this ILogger logger, EventId eventId, string
message: message,
exception: exception);
}

public static void PortsOverridenByUrls(this ILogger logger, string httpPorts, string httpsPorts, string urls)
{
logger.LogWarning(eventId: LoggerEventIds.PortsOverridenByUrls,
message: "Overriding HTTP_PORTS '{http}' and HTTPS_PORTS '{https}'. Binding to values defined by URLS instead '{urls}'.",
httpPorts, httpsPorts, urls);
}
}

1 change: 1 addition & 0 deletions src/Hosting/Hosting/src/Internal/LoggerEventIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ internal static class LoggerEventIds
public const int ServerShutdownException = 12;
public const int HostingStartupAssemblyLoaded = 13;
public const int ServerListeningOnAddresses = 14;
public const int PortsOverridenByUrls = 15;
}
41 changes: 41 additions & 0 deletions src/Hosting/Hosting/test/GenericWebHostBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Linq;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http.Features;
Expand Down Expand Up @@ -87,6 +88,46 @@ public void UseUrlsWorksAfterHostConfigurationSourcesAreCleared()
Assert.Equal("TEST_URL", server.Addresses.Single());
}

[Theory]
[InlineData(null, null, null, "")]
[InlineData("", "", "", "")]
[InlineData("http://urls", "", "", "http://urls")]
[InlineData("http://urls", "5000", "", "http://urls")]
[InlineData("http://urls", "", "5001", "http://urls")]
[InlineData("http://urls", "5000", "5001", "http://urls")]
[InlineData("", "5000", "", "http://*:5000")]
[InlineData("", "5000;5002;5004", "", "http://*:5000;http://*:5002;http://*:5004")]
[InlineData("", "", "5001", "https://*:5001")]
[InlineData("", "", "5001;5003;5005", "https://*:5001;https://*:5003;https://*:5005")]
[InlineData("", "5000", "5001", "http://*:5000;https://*:5001")]
[InlineData("", "5000;5002", "5001;5003", "http://*:5000;http://*:5002;https://*:5001;https://*:5003")]
public void ReadsUrlsOrPorts(string urls, string httpPorts, string httpsPorts, string expected)
{
var server = new TestServer();

using var host = new HostBuilder()
.ConfigureHostConfiguration(config =>
{
config.AddInMemoryCollection(new[]
{
new KeyValuePair<string, string>("urls", urls),
new KeyValuePair<string, string>("http_ports", httpPorts),
new KeyValuePair<string, string>("https_ports", httpsPorts),
});
})
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseServer(server)
.Configure(_ => { });
})
.Build();

host.Start();

Assert.Equal(expected, string.Join(';', server.Addresses));
}

private class TestServer : IServer, IServerAddressesFeature
{
public TestServer()
Expand Down

0 comments on commit 0117723

Please sign in to comment.