Skip to content

Commit b6b0c71

Browse files
committed
Read HTTP_PORTS and HTTPS_PORTS into IServerAddresses dotnet#43135
1 parent 41bed3f commit b6b0c71

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
static readonly Microsoft.AspNetCore.Hosting.WebHostDefaults.HttpPortKey -> string!
3+
static readonly Microsoft.AspNetCore.Hosting.WebHostDefaults.HttpsPortKey -> string!

src/Hosting/Abstractions/src/WebHostDefaults.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ public static class WebHostDefaults
5353
/// </summary>
5454
public static readonly string ServerUrlsKey = "urls";
5555

56+
/// <summary>
57+
/// The configuration key associated with the "http_port" configuration.
58+
/// </summary>
59+
public static readonly string HttpPortKey = "http_port";
60+
61+
/// <summary>
62+
/// The configuration key associated with the "https_port" configuration.
63+
/// </summary>
64+
public static readonly string HttpsPortKey = "https_port";
65+
5666
/// <summary>
5767
/// The configuration key associated with the "ContentRoot" configuration.
5868
/// </summary>

src/Hosting/Hosting/src/GenericHost/GenericWebHostService.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ public async Task StartAsync(CancellationToken cancellationToken)
7373
urls = Options.WebHostOptions.ServerUrls;
7474
}
7575

76+
if (string.IsNullOrEmpty(urls))
77+
{
78+
// HTTP_PORTS and HTTPS_PORTS, these are lower priority than Urls.
79+
var httpPorts = Configuration[WebHostDefaults.HttpPortKey];
80+
var httpUrls = httpPorts?.Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
81+
.Select(port => $"http://*:{port}").Aggregate((s1, s2) => string.Concat(s1, ";", s2));
82+
var httpsPorts = Configuration[WebHostDefaults.HttpsPortKey];
83+
var httpsUrls = httpsPorts?.Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
84+
.Select(port => $"https://*:{port}").Aggregate((s1, s2) => string.Concat(s1, ";", s2));
85+
urls = string.Join(';', httpUrls, httpsUrls);
86+
}
87+
7688
if (!string.IsNullOrEmpty(urls))
7789
{
7890
// We support reading "preferHostingUrls" from app configuration

src/Hosting/Hosting/test/GenericWebHostBuilderTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Linq;
45
using Microsoft.AspNetCore.Hosting.Server;
56
using Microsoft.AspNetCore.Hosting.Server.Features;
67
using Microsoft.AspNetCore.Http.Features;
@@ -87,6 +88,46 @@ public void UseUrlsWorksAfterHostConfigurationSourcesAreCleared()
8788
Assert.Equal("TEST_URL", server.Addresses.Single());
8889
}
8990

91+
[Theory]
92+
[InlineData(null, null, null, "")]
93+
[InlineData("", "", "", "")]
94+
[InlineData("http://urls", "", "", "http://urls")]
95+
[InlineData("http://urls", "5000", "", "http://urls")]
96+
[InlineData("http://urls", "", "5001", "http://urls")]
97+
[InlineData("http://urls", "5000", "5001", "http://urls")]
98+
[InlineData("", "5000", "", "http://*:5000")]
99+
[InlineData("", "5000;5002;5004", "", "http://*:5000;http://*:5002;http://*:5004")]
100+
[InlineData("", "", "5001", "https://*:5001")]
101+
[InlineData("", "", "5001;5003;5005", "https://*:5001;https://*:5003;https://*:5005")]
102+
[InlineData("", "5000", "5001", "http://*:5000;https://*:5001")]
103+
[InlineData("", "5000;5002", "5001;5003", "http://*:5000;http://*:5002;https://*:5001;https://*:5003")]
104+
public void ReadsUrlsOrPorts(string urls, string httpPort, string httpsPort, string expected)
105+
{
106+
var server = new TestServer();
107+
108+
using var host = new HostBuilder()
109+
.ConfigureHostConfiguration(config =>
110+
{
111+
config.AddInMemoryCollection(new[]
112+
{
113+
new KeyValuePair<string, string>("urls", urls),
114+
new KeyValuePair<string, string>("http_port", httpPort),
115+
new KeyValuePair<string, string>("https_port", httpsPort),
116+
});
117+
})
118+
.ConfigureWebHost(webHostBuilder =>
119+
{
120+
webHostBuilder
121+
.UseServer(server)
122+
.Configure(_ => { });
123+
})
124+
.Build();
125+
126+
host.Start();
127+
128+
Assert.Equal(expected, string.Join(';', server.Addresses));
129+
}
130+
90131
private class TestServer : IServer, IServerAddressesFeature
91132
{
92133
public TestServer()

0 commit comments

Comments
 (0)