From 6bd0344880314778a7c774759402f90d21a0a0cc Mon Sep 17 00:00:00 2001 From: Cesar Blum Silveira Date: Mon, 3 Apr 2017 15:09:03 -0700 Subject: [PATCH] Add Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions project (#1582). - Changes the design to have one ConnectionHandler per endpoint. --- KestrelHttpServer.sln | 17 ++++- .../Internal/ConnectionHandler.cs | 10 +-- .../Internal/FrameConnection.cs | 2 +- .../Internal/Http/Frame.cs | 2 +- .../Internal/Http/FrameContext.cs | 2 +- .../Internal/Http/FrameOfT.cs | 2 +- .../Infrastructure/KestrelEventSource.cs | 6 +- .../KestrelServer.cs | 14 ++-- .../ListenOptions.cs | 3 +- ...soft.AspNetCore.Server.Kestrel.Core.csproj | 4 ++ .../Exceptions/AddressInUseException.cs | 2 +- .../Exceptions/ConnectionResetException.cs | 2 +- .../IConnectionContext.cs | 2 +- .../IConnectionHandler.cs | 2 +- .../IConnectionInformation.cs | 4 +- .../IEndPointInformation.cs | 39 +++++++++++ .../ITimeoutControl.cs | 2 +- .../ITransport.cs | 2 +- .../ITransportFactory.cs | 4 +- .../ListenType.cs | 4 +- ...rver.Kestrel.Transport.Abstractions.csproj | 19 +++++ .../TimeoutAction.cs | 2 +- .../Internal/Http/Connection.cs | 3 +- .../Internal/Http/ConnectionContext.cs | 3 +- .../Internal/Http/Listener.cs | 19 ++--- .../Internal/Http/ListenerContext.cs | 7 +- .../Internal/Http/ListenerPrimary.cs | 5 +- .../Internal/Http/ListenerSecondary.cs | 5 +- .../Internal/LibuvTransportContext.cs | 1 + .../KestrelEngine.cs | 19 +++-- .../LibuvTransportFactory.cs | 5 +- .../WebHostBuilderLibuvExtensions.cs | 2 +- .../Mocks/MockConnectionInformation.cs | 4 +- .../EngineTests.cs | 4 +- .../FrameTests.cs | 3 +- .../KestrelServerTests.cs | 6 +- .../ListenerPrimaryTests.cs | 70 ++++++------------- .../PipeOptionsTests.cs | 4 +- .../TestHelpers/MockConnectionHandler.cs | 2 +- .../TestInput.cs | 1 + test/shared/TestServer.cs | 3 +- test/shared/TestServiceContext.cs | 15 ---- 42 files changed, 181 insertions(+), 146 deletions(-) rename src/{Microsoft.AspNetCore.Server.Kestrel.Core/Transport => Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions}/Exceptions/AddressInUseException.cs (87%) rename src/{Microsoft.AspNetCore.Server.Kestrel.Core/Transport => Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions}/Exceptions/ConnectionResetException.cs (87%) rename src/{Microsoft.AspNetCore.Server.Kestrel.Core/Transport => Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions}/IConnectionContext.cs (89%) rename src/{Microsoft.AspNetCore.Server.Kestrel.Core/Transport => Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions}/IConnectionHandler.cs (81%) rename src/{Microsoft.AspNetCore.Server.Kestrel.Core/Transport => Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions}/IConnectionInformation.cs (79%) create mode 100644 src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/IEndPointInformation.cs rename src/{Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http => Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions}/ITimeoutControl.cs (85%) rename src/{Microsoft.AspNetCore.Server.Kestrel.Core/Transport => Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions}/ITransport.cs (84%) rename src/{Microsoft.AspNetCore.Server.Kestrel.Core/Transport => Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions}/ITransportFactory.cs (57%) rename src/{Microsoft.AspNetCore.Server.Kestrel.Core => Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions}/ListenType.cs (69%) create mode 100644 src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.csproj rename src/{Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http => Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions}/TimeoutAction.cs (79%) diff --git a/KestrelHttpServer.sln b/KestrelHttpServer.sln index 24a8e79020..5e243d4c7c 100644 --- a/KestrelHttpServer.sln +++ b/KestrelHttpServer.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26228.9 +VisualStudioVersion = 15.0.26327.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7972A5D6-3385-4127-9277-428506DD44FF}" ProjectSection(SolutionItems) = preProject @@ -65,6 +65,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Server EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Server.Kestrel", "src\Microsoft.AspNetCore.Server.Kestrel\Microsoft.AspNetCore.Server.Kestrel.csproj", "{56139957-5C29-4E7D-89BD-7D20598B4EAF}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions", "src\Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions\Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.csproj", "{2E9CB89D-EC8F-4DD9-A72B-08D5BABF752D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -195,6 +197,18 @@ Global {56139957-5C29-4E7D-89BD-7D20598B4EAF}.Release|x64.Build.0 = Release|Any CPU {56139957-5C29-4E7D-89BD-7D20598B4EAF}.Release|x86.ActiveCfg = Release|Any CPU {56139957-5C29-4E7D-89BD-7D20598B4EAF}.Release|x86.Build.0 = Release|Any CPU + {2E9CB89D-EC8F-4DD9-A72B-08D5BABF752D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2E9CB89D-EC8F-4DD9-A72B-08D5BABF752D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2E9CB89D-EC8F-4DD9-A72B-08D5BABF752D}.Debug|x64.ActiveCfg = Debug|Any CPU + {2E9CB89D-EC8F-4DD9-A72B-08D5BABF752D}.Debug|x64.Build.0 = Debug|Any CPU + {2E9CB89D-EC8F-4DD9-A72B-08D5BABF752D}.Debug|x86.ActiveCfg = Debug|Any CPU + {2E9CB89D-EC8F-4DD9-A72B-08D5BABF752D}.Debug|x86.Build.0 = Debug|Any CPU + {2E9CB89D-EC8F-4DD9-A72B-08D5BABF752D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2E9CB89D-EC8F-4DD9-A72B-08D5BABF752D}.Release|Any CPU.Build.0 = Release|Any CPU + {2E9CB89D-EC8F-4DD9-A72B-08D5BABF752D}.Release|x64.ActiveCfg = Release|Any CPU + {2E9CB89D-EC8F-4DD9-A72B-08D5BABF752D}.Release|x64.Build.0 = Release|Any CPU + {2E9CB89D-EC8F-4DD9-A72B-08D5BABF752D}.Release|x86.ActiveCfg = Release|Any CPU + {2E9CB89D-EC8F-4DD9-A72B-08D5BABF752D}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -212,5 +226,6 @@ Global {2822C132-BFFB-4D53-AC5B-E7E47DD81A6E} = {0EF2ACDF-012F-4472-A13A-4272419E2903} {A76B8C8C-0DC5-4DD3-9B1F-02E51A0915F4} = {2D5D5227-4DBD-499A-96B1-76A36B03B750} {56139957-5C29-4E7D-89BD-7D20598B4EAF} = {2D5D5227-4DBD-499A-96B1-76A36B03B750} + {2E9CB89D-EC8F-4DD9-A72B-08D5BABF752D} = {2D5D5227-4DBD-499A-96B1-76A36B03B750} EndGlobalSection EndGlobal diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/ConnectionHandler.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/ConnectionHandler.cs index 933e04b8ab..23ef1630eb 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/ConnectionHandler.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/ConnectionHandler.cs @@ -5,17 +5,19 @@ using System.IO.Pipelines; using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Server.Kestrel.Internal.Http; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; -using Microsoft.AspNetCore.Server.Kestrel.Transport; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; namespace Microsoft.AspNetCore.Server.Kestrel.Internal { public class ConnectionHandler : IConnectionHandler { + private readonly ListenOptions _listenOptions; private readonly ServiceContext _serviceContext; private readonly IHttpApplication _application; - public ConnectionHandler(ServiceContext serviceContext, IHttpApplication application) + public ConnectionHandler(ListenOptions listenOptions, ServiceContext serviceContext, IHttpApplication application) { + _listenOptions = listenOptions; _serviceContext = serviceContext; _application = application; } @@ -44,7 +46,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal ConnectionId = connectionId, ServiceContext = _serviceContext, PipeFactory = connectionInfo.PipeFactory, - ConnectionAdapters = connectionInfo.ListenOptions.ConnectionAdapters, + ConnectionAdapters = _listenOptions.ConnectionAdapters, Frame = frame, Input = inputPipe, Output = outputPipe, @@ -52,7 +54,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal }); _serviceContext.Log.ConnectionStart(connectionId); - KestrelEventSource.Log.ConnectionStart(connection, connectionInfo); + KestrelEventSource.Log.ConnectionStart(_listenOptions, connection, connectionInfo); // Since data cannot be added to the inputPipe by the transport until OnConnection returns, // Frame.RequestProcessingAsync is guaranteed to unblock the transport thread before calling diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/FrameConnection.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/FrameConnection.cs index af5454f581..8c4805c839 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/FrameConnection.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/FrameConnection.cs @@ -10,7 +10,7 @@ using Microsoft.AspNetCore.Server.Kestrel.Adapter; using Microsoft.AspNetCore.Server.Kestrel.Adapter.Internal; using Microsoft.AspNetCore.Server.Kestrel.Internal.Http; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; -using Microsoft.AspNetCore.Server.Kestrel.Transport; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Microsoft.Extensions.Internal; using Microsoft.Extensions.Logging; diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/Frame.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/Frame.cs index e5b10948e2..6c83f20b3e 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/Frame.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/Frame.cs @@ -16,7 +16,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Server.Kestrel.Adapter; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; -using Microsoft.AspNetCore.Server.Kestrel.Transport; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Microsoft.Extensions.Internal; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Primitives; diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/FrameContext.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/FrameContext.cs index 134afd77b7..cbccf8684a 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/FrameContext.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/FrameContext.cs @@ -1,7 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using Microsoft.AspNetCore.Server.Kestrel.Transport; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http { diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/FrameOfT.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/FrameOfT.cs index dbf6a629e2..75edc1659c 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/FrameOfT.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/FrameOfT.cs @@ -7,7 +7,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; -using Microsoft.AspNetCore.Server.Kestrel.Transport.Exceptions; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Infrastructure/KestrelEventSource.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Infrastructure/KestrelEventSource.cs index 84d7702503..18f1ab33fa 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Infrastructure/KestrelEventSource.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Infrastructure/KestrelEventSource.cs @@ -4,7 +4,7 @@ using System.Diagnostics.Tracing; using System.Runtime.CompilerServices; using Microsoft.AspNetCore.Server.Kestrel.Internal.Http; -using Microsoft.AspNetCore.Server.Kestrel.Transport; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure { @@ -26,14 +26,14 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure // - Avoid renaming methods or parameters marked with EventAttribute. EventSource uses these to form the event object. [NonEvent] - public void ConnectionStart(IConnectionContext context, IConnectionInformation information) + public void ConnectionStart(ListenOptions listenOptions, IConnectionContext context, IConnectionInformation information) { // avoid allocating strings unless this event source is enabled if (IsEnabled()) { ConnectionStart( context.ConnectionId, - information.ListenOptions.Scheme, + listenOptions.Scheme, information.LocalEndPoint.ToString(), information.RemoteEndPoint.ToString()); } diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs index 3958755ea1..9d50baa24f 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs @@ -16,8 +16,7 @@ using Microsoft.AspNetCore.Server.Kestrel.Core.Internal; using Microsoft.AspNetCore.Server.Kestrel.Internal; using Microsoft.AspNetCore.Server.Kestrel.Internal.Http; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; -using Microsoft.AspNetCore.Server.Kestrel.Transport; -using Microsoft.AspNetCore.Server.Kestrel.Transport.Exceptions; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -110,8 +109,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel ServerOptions = Options }; - var connectionHandler = new ConnectionHandler(serviceContext, application); - var listenOptions = Options.ListenOptions; var hasListenOptions = listenOptions.Any(); var hasServerAddresses = _serverAddresses.Addresses.Any(); @@ -128,7 +125,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel _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(connectionHandler, ServerAddress.FromUrl(Constants.DefaultServerAddress)); + StartLocalhost(ServerAddress.FromUrl(Constants.DefaultServerAddress), serviceContext, application); // If StartLocalhost doesn't throw, there is at least one listener. // The port cannot change for "localhost". @@ -173,7 +170,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel if (string.Equals(parsedAddress.Host, "localhost", StringComparison.OrdinalIgnoreCase)) { // "localhost" for both IPv4 and IPv6 can't be represented as an IPEndPoint. - StartLocalhost(connectionHandler, parsedAddress); + StartLocalhost(parsedAddress, serviceContext, application); // If StartLocalhost doesn't throw, there is at least one listener. // The port cannot change for "localhost". @@ -193,6 +190,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel foreach (var endPoint in listenOptions) { + var connectionHandler = new ConnectionHandler(endPoint, serviceContext, application); var transport = _transportFactory.Create(endPoint, connectionHandler); _transports.Add(transport); @@ -256,7 +254,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel } } - private void StartLocalhost(ConnectionHandler connectionHandler, ServerAddress parsedAddress) + private void StartLocalhost(ServerAddress parsedAddress, ServiceContext serviceContext, IHttpApplication application) { if (parsedAddress.Port == 0) { @@ -272,6 +270,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel Scheme = parsedAddress.Scheme, }; + var connectionHandler = new ConnectionHandler(ipv4ListenOptions, serviceContext, application); var transport = _transportFactory.Create(ipv4ListenOptions, connectionHandler); _transports.Add(transport); transport.BindAsync().Wait(); @@ -293,6 +292,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel Scheme = parsedAddress.Scheme, }; + var connectionHandler = new ConnectionHandler(ipv6ListenOptions, serviceContext, application); var transport = _transportFactory.Create(ipv6ListenOptions, connectionHandler); _transports.Add(transport); transport.BindAsync().Wait(); diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/ListenOptions.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Core/ListenOptions.cs index d20fb5d0bf..5fadb48fb4 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/ListenOptions.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/ListenOptions.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Net; using Microsoft.AspNetCore.Server.Kestrel.Adapter; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; namespace Microsoft.AspNetCore.Server.Kestrel { @@ -12,7 +13,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel /// Describes either an , Unix domain socket path, or a file descriptor for an already open /// socket that Kestrel should bind to or open. /// - public class ListenOptions + public class ListenOptions : IEndPointInformation { internal ListenOptions(IPEndPoint endPoint) { diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Microsoft.AspNetCore.Server.Kestrel.Core.csproj b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Microsoft.AspNetCore.Server.Kestrel.Core.csproj index 931898c08b..8c520b639b 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Microsoft.AspNetCore.Server.Kestrel.Core.csproj +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Core/Microsoft.AspNetCore.Server.Kestrel.Core.csproj @@ -25,6 +25,10 @@ + + + + diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/Exceptions/AddressInUseException.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/Exceptions/AddressInUseException.cs similarity index 87% rename from src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/Exceptions/AddressInUseException.cs rename to src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/Exceptions/AddressInUseException.cs index 2d9d3e10ae..8144c4a9a3 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/Exceptions/AddressInUseException.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/Exceptions/AddressInUseException.cs @@ -3,7 +3,7 @@ using System; -namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Exceptions +namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions { public class AddressInUseException : InvalidOperationException { diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/Exceptions/ConnectionResetException.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/Exceptions/ConnectionResetException.cs similarity index 87% rename from src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/Exceptions/ConnectionResetException.cs rename to src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/Exceptions/ConnectionResetException.cs index 87235175e0..32626d97ef 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/Exceptions/ConnectionResetException.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/Exceptions/ConnectionResetException.cs @@ -4,7 +4,7 @@ using System; using System.IO; -namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Exceptions +namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions { public class ConnectionResetException : IOException { diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/IConnectionContext.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/IConnectionContext.cs similarity index 89% rename from src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/IConnectionContext.cs rename to src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/IConnectionContext.cs index 1ef633d445..cac404e67e 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/IConnectionContext.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/IConnectionContext.cs @@ -5,7 +5,7 @@ using System; using System.IO.Pipelines; using System.Threading.Tasks; -namespace Microsoft.AspNetCore.Server.Kestrel.Transport +namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions { public interface IConnectionContext { diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/IConnectionHandler.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/IConnectionHandler.cs similarity index 81% rename from src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/IConnectionHandler.cs rename to src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/IConnectionHandler.cs index ba575773e3..81143f611f 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/IConnectionHandler.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/IConnectionHandler.cs @@ -1,7 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -namespace Microsoft.AspNetCore.Server.Kestrel.Transport +namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions { public interface IConnectionHandler { diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/IConnectionInformation.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/IConnectionInformation.cs similarity index 79% rename from src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/IConnectionInformation.cs rename to src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/IConnectionInformation.cs index 911b83a0df..ee02110c0a 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/IConnectionInformation.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/IConnectionInformation.cs @@ -3,13 +3,11 @@ using System.IO.Pipelines; using System.Net; -using Microsoft.AspNetCore.Server.Kestrel.Internal.Http; -namespace Microsoft.AspNetCore.Server.Kestrel.Transport +namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions { public interface IConnectionInformation { - ListenOptions ListenOptions { get; } IPEndPoint RemoteEndPoint { get; } IPEndPoint LocalEndPoint { get; } diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/IEndPointInformation.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/IEndPointInformation.cs new file mode 100644 index 0000000000..9fff985300 --- /dev/null +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/IEndPointInformation.cs @@ -0,0 +1,39 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Net; + +namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions +{ + public interface IEndPointInformation + { + /// + /// The type of interface being described: either an , Unix domain socket path, or a file descriptor. + /// + ListenType Type { get; } + + // IPEndPoint is mutable so port 0 can be updated to the bound port. + /// + /// The to bind to. + /// Only set if is . + /// + IPEndPoint IPEndPoint { get; set; } + + /// + /// The absolute path to a Unix domain socket to bind to. + /// Only set if is . + /// + string SocketPath { get; } + + /// + /// A file descriptor for the socket to open. + /// Only set if is . + /// + ulong FileHandle { get; } + + /// + /// Set to false to enable Nagle's algorithm for all connections. + /// + bool NoDelay { get; } + } +} diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/ITimeoutControl.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/ITimeoutControl.cs similarity index 85% rename from src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/ITimeoutControl.cs rename to src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/ITimeoutControl.cs index 4fb389b545..824263da13 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/ITimeoutControl.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/ITimeoutControl.cs @@ -1,7 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http +namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions { public interface ITimeoutControl { diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/ITransport.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/ITransport.cs similarity index 84% rename from src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/ITransport.cs rename to src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/ITransport.cs index 7aeea940f6..704101726c 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/ITransport.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/ITransport.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; -namespace Microsoft.AspNetCore.Server.Kestrel.Transport +namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions { public interface ITransport { diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/ITransportFactory.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/ITransportFactory.cs similarity index 57% rename from src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/ITransportFactory.cs rename to src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/ITransportFactory.cs index 74eef3d3d6..44608be30f 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Transport/ITransportFactory.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/ITransportFactory.cs @@ -1,10 +1,10 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -namespace Microsoft.AspNetCore.Server.Kestrel.Transport +namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions { public interface ITransportFactory { - ITransport Create(ListenOptions listenOptions, IConnectionHandler handler); + ITransport Create(IEndPointInformation endPointInformation, IConnectionHandler handler); } } diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/ListenType.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/ListenType.cs similarity index 69% rename from src/Microsoft.AspNetCore.Server.Kestrel.Core/ListenType.cs rename to src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/ListenType.cs index 7ead1d91e0..3842d15545 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/ListenType.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/ListenType.cs @@ -1,10 +1,10 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -namespace Microsoft.AspNetCore.Server.Kestrel +namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions { /// - /// Enumerates the types. + /// Enumerates the types. /// public enum ListenType { diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.csproj b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.csproj new file mode 100644 index 0000000000..127f06c668 --- /dev/null +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.csproj @@ -0,0 +1,19 @@ + + + + + + Transport abstractions for the ASP.NET Core Kestrel cross-platform web server. + netstandard1.3 + true + aspnetcore;kestrel + CS1591;$(NoWarn) + false + + + + + + + + diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/TimeoutAction.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/TimeoutAction.cs similarity index 79% rename from src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/TimeoutAction.cs rename to src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/TimeoutAction.cs index 33d832ce01..06aae8627e 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Core/Internal/Http/TimeoutAction.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions/TimeoutAction.cs @@ -1,7 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http +namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions { public enum TimeoutAction { diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/Connection.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/Connection.cs index bff2e1021a..213f54db7b 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/Connection.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/Connection.cs @@ -9,8 +9,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking; -using Microsoft.AspNetCore.Server.Kestrel.Transport; -using Microsoft.AspNetCore.Server.Kestrel.Transport.Exceptions; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/ConnectionContext.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/ConnectionContext.cs index 869935033e..25b64697b9 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/ConnectionContext.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/ConnectionContext.cs @@ -3,7 +3,7 @@ using System.IO.Pipelines; using System.Net; -using Microsoft.AspNetCore.Server.Kestrel.Transport; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http { @@ -20,7 +20,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http public ListenerContext ListenerContext { get; set; } - public ListenOptions ListenOptions => ListenerContext.ListenOptions; public IPEndPoint RemoteEndPoint { get; set; } public IPEndPoint LocalEndPoint { get; set; } diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/Listener.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/Listener.cs index 9666be8181..4f1243bc19 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/Listener.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/Listener.cs @@ -5,6 +5,7 @@ using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal; using Microsoft.Extensions.Logging; @@ -26,10 +27,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http public IKestrelTrace Log => TransportContext.Log; public Task StartAsync( - ListenOptions listenOptions, + IEndPointInformation endPointInformation, KestrelThread thread) { - ListenOptions = listenOptions; + EndPointInformation = endPointInformation; Thread = thread; var tcs = new TaskCompletionSource(this); @@ -58,7 +59,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http /// private UvStreamHandle CreateListenSocket() { - switch (ListenOptions.Type) + switch (EndPointInformation.Type) { case ListenType.IPEndPoint: case ListenType.FileHandle: @@ -67,18 +68,18 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http try { socket.Init(Thread.Loop, Thread.QueueCloseHandle); - socket.NoDelay(ListenOptions.NoDelay); + socket.NoDelay(EndPointInformation.NoDelay); - if (ListenOptions.Type == ListenType.IPEndPoint) + if (EndPointInformation.Type == ListenType.IPEndPoint) { - socket.Bind(ListenOptions.IPEndPoint); + socket.Bind(EndPointInformation.IPEndPoint); // If requested port was "0", replace with assigned dynamic port. - ListenOptions.IPEndPoint = socket.GetSockIPEndPoint(); + EndPointInformation.IPEndPoint = socket.GetSockIPEndPoint(); } else { - socket.Open((IntPtr)ListenOptions.FileHandle); + socket.Open((IntPtr)EndPointInformation.FileHandle); } } catch @@ -94,7 +95,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http try { pipe.Init(Thread.Loop, Thread.QueueCloseHandle, false); - pipe.Bind(ListenOptions.SocketPath); + pipe.Bind(EndPointInformation.SocketPath); } catch { diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/ListenerContext.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/ListenerContext.cs index ac0d07f37f..f0bb7b5b3a 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/ListenerContext.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/ListenerContext.cs @@ -3,6 +3,7 @@ using System; using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal; namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http @@ -16,7 +17,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http public LibuvTransportContext TransportContext { get; set; } - public ListenOptions ListenOptions { get; set; } + public IEndPointInformation EndPointInformation { get; set; } public KestrelThread Thread { get; set; } @@ -25,13 +26,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http /// protected UvStreamHandle CreateAcceptSocket() { - switch (ListenOptions.Type) + switch (EndPointInformation.Type) { case ListenType.IPEndPoint: case ListenType.FileHandle: var tcpHandle = new UvTcpHandle(TransportContext.Log); tcpHandle.Init(Thread.Loop, Thread.QueueCloseHandle); - tcpHandle.NoDelay(ListenOptions.NoDelay); + tcpHandle.NoDelay(EndPointInformation.NoDelay); return tcpHandle; case ListenType.SocketPath: var pipeHandle = new UvPipeHandle(TransportContext.Log); diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/ListenerPrimary.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/ListenerPrimary.cs index 8d47e7871e..eebcd96e6d 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/ListenerPrimary.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/ListenerPrimary.cs @@ -8,6 +8,7 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal; using Microsoft.Extensions.Logging; @@ -39,7 +40,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http public async Task StartAsync( string pipeName, byte[] pipeMessage, - ListenOptions listenOptions, + IEndPointInformation endPointInformation, KestrelThread thread) { _pipeName = pipeName; @@ -52,7 +53,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http Marshal.StructureToPtr(fileCompletionInfo, _fileCompletionInfoPtr, false); } - await StartAsync(listenOptions, thread).ConfigureAwait(false); + await StartAsync(endPointInformation, thread).ConfigureAwait(false); await Thread.PostAsync(state => ((ListenerPrimary)state).PostCallback(), this).ConfigureAwait(false); diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/ListenerSecondary.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/ListenerSecondary.cs index 243a72472a..1171484142 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/ListenerSecondary.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/Http/ListenerSecondary.cs @@ -7,6 +7,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal; using Microsoft.Extensions.Logging; @@ -36,14 +37,14 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http public Task StartAsync( string pipeName, byte[] pipeMessage, - ListenOptions listenOptions, + IEndPointInformation endPointInformation, KestrelThread thread) { _pipeName = pipeName; _pipeMessage = pipeMessage; _buf = thread.Loop.Libuv.buf_init(_ptr, 4); - ListenOptions = listenOptions; + EndPointInformation = endPointInformation; Thread = thread; DispatchPipe = new UvPipeHandle(Log); diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/LibuvTransportContext.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/LibuvTransportContext.cs index 5a94ff302d..e5400244cf 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/LibuvTransportContext.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/Internal/LibuvTransportContext.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal { diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/KestrelEngine.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/KestrelEngine.cs index d4d9a48d85..3d883354ca 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/KestrelEngine.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/KestrelEngine.cs @@ -9,8 +9,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Server.Kestrel.Internal.Http; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking; -using Microsoft.AspNetCore.Server.Kestrel.Transport; -using Microsoft.AspNetCore.Server.Kestrel.Transport.Exceptions; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv; using Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal; using Microsoft.Extensions.Logging; @@ -19,21 +18,21 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal { public class KestrelEngine : ITransport { - private readonly ListenOptions _listenOptions; + private readonly IEndPointInformation _endPointInformation; private readonly List _listeners = new List(); - public KestrelEngine(LibuvTransportContext context, ListenOptions listenOptions) - : this(new LibuvFunctions(), context, listenOptions) + public KestrelEngine(LibuvTransportContext context, IEndPointInformation endPointInformation) + : this(new LibuvFunctions(), context, endPointInformation) { } // For testing - public KestrelEngine(LibuvFunctions uv, LibuvTransportContext context, ListenOptions listenOptions) + public KestrelEngine(LibuvFunctions uv, LibuvTransportContext context, IEndPointInformation endPointInformation) { Libuv = uv; TransportContext = context; - _listenOptions = listenOptions; + _endPointInformation = endPointInformation; } public LibuvFunctions Libuv { get; } @@ -92,7 +91,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal { var listener = new Listener(TransportContext); _listeners.Add(listener); - await listener.StartAsync(_listenOptions, Threads[0]).ConfigureAwait(false); + await listener.StartAsync(_endPointInformation, Threads[0]).ConfigureAwait(false); } else { @@ -101,13 +100,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal var listenerPrimary = new ListenerPrimary(TransportContext); _listeners.Add(listenerPrimary); - await listenerPrimary.StartAsync(pipeName, pipeMessage, _listenOptions, Threads[0]).ConfigureAwait(false); + await listenerPrimary.StartAsync(pipeName, pipeMessage, _endPointInformation, Threads[0]).ConfigureAwait(false); foreach (var thread in Threads.Skip(1)) { var listenerSecondary = new ListenerSecondary(TransportContext); _listeners.Add(listenerSecondary); - await listenerSecondary.StartAsync(pipeName, pipeMessage, _listenOptions, thread).ConfigureAwait(false); + await listenerSecondary.StartAsync(pipeName, pipeMessage, _endPointInformation, thread).ConfigureAwait(false); } } } diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/LibuvTransportFactory.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/LibuvTransportFactory.cs index 8985247f95..526a77f0fa 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/LibuvTransportFactory.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/LibuvTransportFactory.cs @@ -5,6 +5,7 @@ using System; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Server.Kestrel.Internal; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -65,7 +66,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv }; } - public ITransport Create(ListenOptions listenOptions, IConnectionHandler handler) + public ITransport Create(IEndPointInformation endPointInformation, IConnectionHandler handler) { var transportContext = new LibuvTransportContext { @@ -75,7 +76,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv ConnectionHandler = handler }; - return new KestrelEngine(transportContext, listenOptions); + return new KestrelEngine(transportContext, endPointInformation); } } } diff --git a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/WebHostBuilderLibuvExtensions.cs b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/WebHostBuilderLibuvExtensions.cs index 0cfa830f99..cadb2e7e83 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/WebHostBuilderLibuvExtensions.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv/WebHostBuilderLibuvExtensions.cs @@ -2,7 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using Microsoft.AspNetCore.Server.Kestrel.Transport; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv; using Microsoft.Extensions.DependencyInjection; diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.Performance/Mocks/MockConnectionInformation.cs b/test/Microsoft.AspNetCore.Server.Kestrel.Performance/Mocks/MockConnectionInformation.cs index 9faa8d2f63..744ab1a601 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.Performance/Mocks/MockConnectionInformation.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.Performance/Mocks/MockConnectionInformation.cs @@ -3,14 +3,12 @@ using System.IO.Pipelines; using System.Net; -using Microsoft.AspNetCore.Server.Kestrel.Internal.Http; -using Microsoft.AspNetCore.Server.Kestrel.Transport; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; namespace Microsoft.AspNetCore.Server.Kestrel.Performance { public class MockConnectionInformation : IConnectionInformation { - public ListenOptions ListenOptions { get; } public IPEndPoint RemoteEndPoint { get; } public IPEndPoint LocalEndPoint { get; } diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/EngineTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/EngineTests.cs index 076729e510..eed4aac30e 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/EngineTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/EngineTests.cs @@ -53,7 +53,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests public async Task ListenerCanCreateAndDispose(ListenOptions listenOptions) { var testContext = new TestServiceContext(); - testContext.App = TestApp.EchoApp; + testContext.TransportContext.ConnectionHandler = new ConnectionHandler(listenOptions, testContext, new DummyApplication(TestApp.EchoApp)); var engine = new KestrelEngine(testContext.TransportContext, listenOptions); await engine.BindAsync(); @@ -66,7 +66,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests public async Task ConnectionCanReadAndWrite(ListenOptions listenOptions) { var testContext = new TestServiceContext(); - testContext.App = TestApp.EchoApp; + testContext.TransportContext.ConnectionHandler = new ConnectionHandler(listenOptions, testContext, new DummyApplication(TestApp.EchoApp)); var engine = new KestrelEngine(testContext.TransportContext, listenOptions); await engine.BindAsync(); diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/FrameTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/FrameTests.cs index 70f63f8a72..5f5f353bb4 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/FrameTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/FrameTests.cs @@ -16,7 +16,7 @@ using Microsoft.AspNetCore.Server.Kestrel; using Microsoft.AspNetCore.Server.Kestrel.Internal; using Microsoft.AspNetCore.Server.Kestrel.Internal.Http; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; -using Microsoft.AspNetCore.Server.Kestrel.Transport; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Microsoft.AspNetCore.Testing; using Microsoft.Extensions.Internal; using Microsoft.Extensions.Logging; @@ -712,7 +712,6 @@ namespace Microsoft.AspNetCore.Server.KestrelTests private class MockConnectionInformation : IConnectionInformation { - public ListenOptions ListenOptions { get; } public IPEndPoint RemoteEndPoint { get; } public IPEndPoint LocalEndPoint { get; } public PipeFactory PipeFactory { get; } diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/KestrelServerTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/KestrelServerTests.cs index 2f103ff376..24218aa8f7 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/KestrelServerTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/KestrelServerTests.cs @@ -4,17 +4,17 @@ using System; using System.Linq; using System.Net; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.AspNetCore.Server.Kestrel; -using Microsoft.AspNetCore.Server.Kestrel.Transport; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Microsoft.AspNetCore.Testing; using Microsoft.Extensions.Internal; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Moq; using Xunit; -using Microsoft.AspNetCore.Builder; namespace Microsoft.AspNetCore.Server.KestrelTests { @@ -166,7 +166,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests private class MockTransportFactory : ITransportFactory { - public ITransport Create(ListenOptions listenOptions, IConnectionHandler handler) + public ITransport Create(IEndPointInformation endPointInformation, IConnectionHandler handler) { return Mock.Of(); } diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/ListenerPrimaryTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/ListenerPrimaryTests.cs index 8045cb651a..48685f4890 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/ListenerPrimaryTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/ListenerPrimaryTests.cs @@ -6,9 +6,7 @@ using System.IO; using System.Linq; using System.Net; using System.Threading.Tasks; -using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Server.Kestrel; using Microsoft.AspNetCore.Server.Kestrel.Internal; using Microsoft.AspNetCore.Server.Kestrel.Internal.Http; @@ -26,18 +24,16 @@ namespace Microsoft.AspNetCore.Server.KestrelTests public async Task ConnectionsGetRoundRobinedToSecondaryListeners() { var libuv = new LibuvFunctions(); - - var serviceContextPrimary = new TestServiceContext - { - App = c => c.Response.WriteAsync("Primary") - }; - - var serviceContextSecondary = new TestServiceContext - { - App = c => c.Response.WriteAsync("Secondary") - }; - var listenOptions = new ListenOptions(new IPEndPoint(IPAddress.Loopback, 0)); + + var serviceContextPrimary = new TestServiceContext(); + serviceContextPrimary.TransportContext.ConnectionHandler = new ConnectionHandler( + listenOptions, serviceContextPrimary, new DummyApplication(c => c.Response.WriteAsync("Primary"))); + + var serviceContextSecondary = new TestServiceContext(); + serviceContextSecondary.TransportContext.ConnectionHandler = new ConnectionHandler( + listenOptions, serviceContextSecondary, new DummyApplication(c => c.Response.WriteAsync("Secondary"))); + var kestrelEngine = new KestrelEngine(libuv, serviceContextPrimary.TransportContext, listenOptions); var pipeName = (libuv.IsWindows ? @"\\.\pipe\kestrel_" : "/tmp/kestrel_") + Guid.NewGuid().ToString("n"); @@ -80,11 +76,11 @@ namespace Microsoft.AspNetCore.Server.KestrelTests public async Task NonListenerPipeConnectionsAreLoggedAndIgnored() { var libuv = new LibuvFunctions(); + var listenOptions = new ListenOptions(new IPEndPoint(IPAddress.Loopback, 0)); - var serviceContextPrimary = new TestServiceContext - { - App = c => c.Response.WriteAsync("Primary") - }; + var serviceContextPrimary = new TestServiceContext(); + serviceContextPrimary.TransportContext.ConnectionHandler = new ConnectionHandler( + listenOptions, serviceContextPrimary, new DummyApplication(c => c.Response.WriteAsync("Primary"))); var serviceContextSecondary = new TestServiceContext { @@ -92,10 +88,10 @@ namespace Microsoft.AspNetCore.Server.KestrelTests ServerOptions = serviceContextPrimary.ServerOptions, ThreadPool = serviceContextPrimary.ThreadPool, HttpParserFactory = serviceContextPrimary.HttpParserFactory, - App = c => c.Response.WriteAsync("Secondary") }; + serviceContextSecondary.TransportContext.ConnectionHandler = new ConnectionHandler( + listenOptions, serviceContextSecondary, new DummyApplication(c => c.Response.WriteAsync("Secondary"))); - var listenOptions = new ListenOptions(new IPEndPoint(IPAddress.Loopback, 0)); var kestrelEngine = new KestrelEngine(libuv, serviceContextPrimary.TransportContext, listenOptions); var pipeName = (libuv.IsWindows ? @"\\.\pipe\kestrel_" : "/tmp/kestrel_") + Guid.NewGuid().ToString("n"); @@ -187,11 +183,11 @@ namespace Microsoft.AspNetCore.Server.KestrelTests public async Task PipeConnectionsWithWrongMessageAreLoggedAndIgnored() { var libuv = new LibuvFunctions(); + var listenOptions = new ListenOptions(new IPEndPoint(IPAddress.Loopback, 0)); - var serviceContextPrimary = new TestServiceContext - { - App = c => c.Response.WriteAsync("Primary") - }; + var serviceContextPrimary = new TestServiceContext(); + serviceContextPrimary.TransportContext.ConnectionHandler = new ConnectionHandler( + listenOptions, serviceContextPrimary, new DummyApplication(c => c.Response.WriteAsync("Primary"))); var serviceContextSecondary = new TestServiceContext { @@ -199,10 +195,10 @@ namespace Microsoft.AspNetCore.Server.KestrelTests ServerOptions = serviceContextPrimary.ServerOptions, ThreadPool = serviceContextPrimary.ThreadPool, HttpParserFactory = serviceContextPrimary.HttpParserFactory, - App = c => c.Response.WriteAsync("Secondary") }; + serviceContextSecondary.TransportContext.ConnectionHandler = new ConnectionHandler( + listenOptions, serviceContextSecondary, new DummyApplication(c => c.Response.WriteAsync("Secondary"))); - var listenOptions = new ListenOptions(new IPEndPoint(IPAddress.Loopback, 0)); var kestrelEngine = new KestrelEngine(libuv, serviceContextPrimary.TransportContext, listenOptions); var pipeName = (libuv.IsWindows ? @"\\.\pipe\kestrel_" : "/tmp/kestrel_") + Guid.NewGuid().ToString("n"); @@ -271,29 +267,5 @@ namespace Microsoft.AspNetCore.Server.KestrelTests Assert.True(false, $"'{address}' failed to respond with '{expected}' in {maxRetries} retries."); } - - private class TestApplication : IHttpApplication - { - private readonly Func _app; - - public TestApplication(Func app) - { - _app = app; - } - - public DefaultHttpContext CreateContext(IFeatureCollection contextFeatures) - { - return new DefaultHttpContext(contextFeatures); - } - - public Task ProcessRequestAsync(DefaultHttpContext context) - { - return _app(context); - } - - public void DisposeContext(DefaultHttpContext context, Exception exception) - { - } - } } } diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/PipeOptionsTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/PipeOptionsTests.cs index 924a4200e5..ec5ac31ff1 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/PipeOptionsTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/PipeOptionsTests.cs @@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests serviceContext.ServerOptions.Limits.MaxResponseBufferSize = maxResponseBufferSize; serviceContext.ThreadPool = new LoggingThreadPool(null); - var connectionHandler = new ConnectionHandler(serviceContext, application: null); + var connectionHandler = new ConnectionHandler(listenOptions: null, serviceContext: serviceContext, application: null); var mockScheduler = Mock.Of(); var outputPipeOptions = connectionHandler.GetOutputPipeOptions(mockScheduler); @@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests serviceContext.ServerOptions.Limits.MaxRequestBufferSize = maxRequestBufferSize; serviceContext.ThreadPool = new LoggingThreadPool(null); - var connectionHandler = new ConnectionHandler(serviceContext, application: null); + var connectionHandler = new ConnectionHandler(listenOptions: null, serviceContext: serviceContext, application: null); var mockScheduler = Mock.Of(); var inputPipeOptions = connectionHandler.GetInputPipeOptions(mockScheduler); diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockConnectionHandler.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockConnectionHandler.cs index f50f3572c6..a7747457ac 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockConnectionHandler.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockConnectionHandler.cs @@ -4,7 +4,7 @@ using System; using System.IO.Pipelines; using System.Threading.Tasks; -using Microsoft.AspNetCore.Server.Kestrel.Transport; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Xunit; namespace Microsoft.AspNetCore.Server.KestrelTests.TestHelpers diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/TestInput.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/TestInput.cs index cfa87eb55e..e017c983dd 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/TestInput.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/TestInput.cs @@ -7,6 +7,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Server.Kestrel.Internal.Http; +using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions; using Microsoft.AspNetCore.Testing; using Microsoft.Extensions.Internal; diff --git a/test/shared/TestServer.cs b/test/shared/TestServer.cs index 3763c61c53..fc4e91b195 100644 --- a/test/shared/TestServer.cs +++ b/test/shared/TestServer.cs @@ -43,8 +43,7 @@ namespace Microsoft.AspNetCore.Testing _listenOptions = listenOptions; Context = context; - Context.App = app; - Context.TransportContext.ConnectionHandler = new ConnectionHandler(Context, new DummyApplication(app, httpContextFactory)); + Context.TransportContext.ConnectionHandler = new ConnectionHandler(listenOptions, Context, new DummyApplication(app, httpContextFactory)); try { diff --git a/test/shared/TestServiceContext.cs b/test/shared/TestServiceContext.cs index 522acbd745..e01316b622 100644 --- a/test/shared/TestServiceContext.cs +++ b/test/shared/TestServiceContext.cs @@ -14,8 +14,6 @@ namespace Microsoft.AspNetCore.Testing { public class TestServiceContext : ServiceContext { - private RequestDelegate _app; - public TestServiceContext() { Log = new TestKestrelTrace(); @@ -43,18 +41,5 @@ namespace Microsoft.AspNetCore.Testing public string DateHeaderValue { get; } public LibuvTransportContext TransportContext { get; } - - public RequestDelegate App - { - get - { - return _app; - } - set - { - TransportContext.ConnectionHandler = new ConnectionHandler(this, new DummyApplication(value)); - _app = value; - } - } } }