From f543e3552514c5c420eeddd55c505bbc131f10a6 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Fri, 9 Dec 2022 10:12:37 +0800 Subject: [PATCH] Add transport selector interface to Kestrel sockets and QUIC transports (#44832) --- .../Transport.Quic/src/QuicTransportFactory.cs | 7 ++++++- .../Kestrel/Transport.Quic/test/WebHostTests.cs | 2 +- .../Transport.Sockets/src/PublicAPI.Unshipped.txt | 1 + .../src/SocketTransportFactory.cs | 15 ++++++++++++++- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Servers/Kestrel/Transport.Quic/src/QuicTransportFactory.cs b/src/Servers/Kestrel/Transport.Quic/src/QuicTransportFactory.cs index 61d51540d07a..068ea113f7dc 100644 --- a/src/Servers/Kestrel/Transport.Quic/src/QuicTransportFactory.cs +++ b/src/Servers/Kestrel/Transport.Quic/src/QuicTransportFactory.cs @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Quic; /// /// A factory for QUIC based connections. /// -internal sealed class QuicTransportFactory : IMultiplexedConnectionListenerFactory +internal sealed class QuicTransportFactory : IMultiplexedConnectionListenerFactory, IConnectionListenerFactorySelector { private readonly ILogger _log; private readonly QuicTransportOptions _options; @@ -56,4 +56,9 @@ public async ValueTask BindAsync(EndPoint endpoi return transport; } + + public bool CanBind(EndPoint endpoint) + { + return endpoint is IPEndPoint; + } } diff --git a/src/Servers/Kestrel/Transport.Quic/test/WebHostTests.cs b/src/Servers/Kestrel/Transport.Quic/test/WebHostTests.cs index b96e4d6736b8..7745a24bbf4b 100644 --- a/src/Servers/Kestrel/Transport.Quic/test/WebHostTests.cs +++ b/src/Servers/Kestrel/Transport.Quic/test/WebHostTests.cs @@ -390,7 +390,7 @@ public async Task StartAsync_Http3WithNonIPListener_ThrowError() var ex = await Assert.ThrowsAsync(() => host.StartAsync()).DefaultTimeout(); // Assert - Assert.Equal("QUIC doesn't support listening on the configured endpoint type. Expected IPEndPoint but got UnixDomainSocketEndPoint.", ex.Message); + Assert.Equal("No registered IMultiplexedConnectionListenerFactory supports endpoint UnixDomainSocketEndPoint: /test-path", ex.Message); } private static HttpClient CreateClient() diff --git a/src/Servers/Kestrel/Transport.Sockets/src/PublicAPI.Unshipped.txt b/src/Servers/Kestrel/Transport.Sockets/src/PublicAPI.Unshipped.txt index 7dc5c58110bf..453928d35e79 100644 --- a/src/Servers/Kestrel/Transport.Sockets/src/PublicAPI.Unshipped.txt +++ b/src/Servers/Kestrel/Transport.Sockets/src/PublicAPI.Unshipped.txt @@ -1 +1,2 @@ #nullable enable +Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory.CanBind(System.Net.EndPoint! endpoint) -> bool diff --git a/src/Servers/Kestrel/Transport.Sockets/src/SocketTransportFactory.cs b/src/Servers/Kestrel/Transport.Sockets/src/SocketTransportFactory.cs index afbe252bb97b..939b0a8c32fc 100644 --- a/src/Servers/Kestrel/Transport.Sockets/src/SocketTransportFactory.cs +++ b/src/Servers/Kestrel/Transport.Sockets/src/SocketTransportFactory.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Net; +using System.Net.Sockets; using Microsoft.AspNetCore.Connections; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -11,7 +12,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets; /// /// A factory for socket based connections. /// -public sealed class SocketTransportFactory : IConnectionListenerFactory +public sealed class SocketTransportFactory : IConnectionListenerFactory, IConnectionListenerFactorySelector { private readonly SocketTransportOptions _options; private readonly ILoggerFactory _logger; @@ -40,4 +41,16 @@ public ValueTask BindAsync(EndPoint endpoint, CancellationT transport.Bind(); return new ValueTask(transport); } + + /// + public bool CanBind(EndPoint endpoint) + { + return endpoint switch + { + IPEndPoint _ => true, + UnixDomainSocketEndPoint _ => true, + FileHandleEndPoint _ => true, + _ => false + }; + } }