Skip to content

Read HTTP_PORTS and HTTPS_PORTS into IServerAddresses #44194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 15 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,21 @@ public async Task StartAsync(CancellationToken cancellationToken)
urls = Options.WebHostOptions.ServerUrls;
}

if (string.IsNullOrEmpty(urls))
{
// HTTP_PORTS and HTTPS_PORTS, these are lower priority than Urls.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we log a warning if we ignore HTTP_PORTS and/or HTTPS_PORTS becuase URLS is set?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warn: Microsoft.AspNetCore.Hosting.Diagnostics[15]
      Overriding HTTP_PORTS '5002' and HTTPS_PORTS ''. Binding to values defined by URLS instead 'http://*:5003'.
warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Overriding address(es) 'http://*:5003'. Binding to endpoints defined via IConfiguration and/or UseKestrel() instead.
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://[::]:5004

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of warnings.......... 😄

static string ExpandPorts(string ports, string scheme)
{
return string.Join(';',
ports.Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
.Select(port => $"{scheme}://*:{port}"));
}

var httpUrls = ExpandPorts(Configuration[WebHostDefaults.HttpPortsKey] ?? string.Empty, Uri.UriSchemeHttp);
var httpsUrls = ExpandPorts(Configuration[WebHostDefaults.HttpsPortsKey] ?? string.Empty, Uri.UriSchemeHttps);
urls = $"{httpUrls};{httpsUrls}";
}

if (!string.IsNullOrEmpty(urls))
{
// We support reading "preferHostingUrls" from app configuration
Expand Down
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