diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Connection.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Connection.cs index 7494362185..877e66fac0 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Connection.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Connection.cs @@ -66,7 +66,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http } _frame = FrameFactory(this); - _lastTimestamp = Thread.Loop.Now(); } @@ -75,6 +74,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http { } + public KestrelServerOptions ServerOptions => ListenerContext.ServiceContext.ServerOptions; + private Func FrameFactory => ListenerContext.ServiceContext.FrameFactory; + private IKestrelTrace Log => ListenerContext.ServiceContext.Log; + private IThreadPool ThreadPool => ListenerContext.ServiceContext.ThreadPool; + private ServerAddress ServerAddress => ListenerContext.ServerAddress; + private KestrelThread Thread => ListenerContext.Thread; + public void Start() { Log.ConnectionStart(ConnectionId); diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ConnectionContext.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ConnectionContext.cs index f66f95d5ab..7ee603e4c5 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ConnectionContext.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ConnectionContext.cs @@ -7,26 +7,18 @@ using Microsoft.AspNetCore.Http.Features; namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http { - public class ConnectionContext : ListenerContext + public class ConnectionContext { public ConnectionContext() { } - public ConnectionContext(ListenerContext context) : base(context) + public ConnectionContext(ListenerContext context) { + ListenerContext = context; } - public ConnectionContext(ConnectionContext context) : base(context) - { - SocketInput = context.SocketInput; - SocketOutput = context.SocketOutput; - ConnectionControl = context.ConnectionControl; - RemoteEndPoint = context.RemoteEndPoint; - LocalEndPoint = context.LocalEndPoint; - ConnectionId = context.ConnectionId; - PrepareRequest = context.PrepareRequest; - } + public ListenerContext ListenerContext { get; set; } public SocketInput SocketInput { get; set; } diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Frame.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Frame.cs index af4d8882ef..dde74b0f56 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Frame.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Frame.cs @@ -11,6 +11,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; using Microsoft.Extensions.Internal; using Microsoft.Extensions.Logging; @@ -20,7 +21,7 @@ using Microsoft.Extensions.Primitives; namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http { - public abstract partial class Frame : ConnectionContext, IFrameControl + public abstract partial class Frame : IFrameControl { private static readonly ArraySegment _endChunkedResponseBytes = CreateAsciiByteArraySegment("0\r\n\r\n"); private static readonly ArraySegment _continueBytes = CreateAsciiByteArraySegment("HTTP/1.1 100 Continue\r\n\r\n"); @@ -72,14 +73,45 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http protected readonly long _keepAliveMilliseconds; public Frame(ConnectionContext context) - : base(context) { - _pathBase = context.ServerAddress.PathBase; + ConnectionContext = context; + SocketInput = context.SocketInput; + SocketOutput = context.SocketOutput; + + ServerOptions = context.ListenerContext.ServiceContext.ServerOptions; + + _pathBase = ServerAddress.PathBase; FrameControl = this; _keepAliveMilliseconds = (long)ServerOptions.Limits.KeepAliveTimeout.TotalMilliseconds; } + public ConnectionContext ConnectionContext { get; } + public SocketInput SocketInput { get; set; } + public ISocketOutput SocketOutput { get; set; } + public Action PrepareRequest + { + get + { + return ConnectionContext.PrepareRequest; + } + set + { + ConnectionContext.PrepareRequest = value; + } + } + + protected IConnectionControl ConnectionControl => ConnectionContext.ConnectionControl; + protected IKestrelTrace Log => ConnectionContext.ListenerContext.ServiceContext.Log; + + private DateHeaderValueManager DateHeaderValueManager => ConnectionContext.ListenerContext.ServiceContext.DateHeaderValueManager; + private ServerAddress ServerAddress => ConnectionContext.ListenerContext.ServerAddress; + // Hold direct reference to ServerOptions since this is used very often in the request processing path + private KestrelServerOptions ServerOptions { get; } + private IPEndPoint LocalEndPoint => ConnectionContext.LocalEndPoint; + private IPEndPoint RemoteEndPoint => ConnectionContext.RemoteEndPoint; + private string ConnectionId => ConnectionContext.ConnectionId; + public string ConnectionIdFeature { get; set; } public IPAddress RemoteIpAddress { get; set; } public int RemotePort { get; set; } @@ -736,7 +768,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http responseHeaders.SetRawContentLength("0", _bytesContentLengthZero); } } - else if(_keepAlive) + else if (_keepAlive) { // Note for future reference: never change this to set _autoChunk to true on HTTP/1.0 // connections, even if we were to infer the client supports it because an HTTP/1.0 request diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Listener.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Listener.cs index a40515d66f..f66485a5ed 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Listener.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Listener.cs @@ -23,6 +23,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http protected UvStreamHandle ListenSocket { get; private set; } + public IKestrelTrace Log => ServiceContext.Log; + public Task StartAsync( ServerAddress address, KestrelThread thread) diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ListenerContext.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ListenerContext.cs index f7c0dd1dac..8ecee653f0 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ListenerContext.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ListenerContext.cs @@ -1,32 +1,21 @@ // 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.Collections.Generic; -using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; -using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking; - namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http { - public class ListenerContext : ServiceContext + public class ListenerContext { - public ListenerContext() + public ListenerContext(ServiceContext serviceContext) { + ServiceContext = serviceContext; } - public ListenerContext(ServiceContext serviceContext) - : base(serviceContext) - { - } - - public ListenerContext(ListenerContext listenerContext) - : base(listenerContext) - { - ServerAddress = listenerContext.ServerAddress; - Thread = listenerContext.Thread; - } + public ServiceContext ServiceContext { get; set; } public ServerAddress ServerAddress { get; set; } public KestrelThread Thread { get; set; } + + public KestrelServerOptions ServerOptions => ServiceContext.ServerOptions; } } diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ListenerSecondary.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ListenerSecondary.cs index f3acef1f1d..3fd1db29be 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ListenerSecondary.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ListenerSecondary.cs @@ -12,7 +12,7 @@ using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http { /// - /// A secondary listener is delegated requests from a primary listener via a named pipe or + /// A secondary listener is delegated requests from a primary listener via a named pipe or /// UNIX domain socket. /// public abstract class ListenerSecondary : ListenerContext, IAsyncDisposable @@ -29,6 +29,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http UvPipeHandle DispatchPipe { get; set; } + public IKestrelTrace Log => ServiceContext.Log; + public Task StartAsync( string pipeName, ServerAddress address, diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/KestrelEngine.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/KestrelEngine.cs index c611580ae4..1f7c4c7a3c 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/KestrelEngine.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/KestrelEngine.cs @@ -5,13 +5,15 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +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.Extensions.Logging; namespace Microsoft.AspNetCore.Server.Kestrel.Internal { - public class KestrelEngine : ServiceContext, IDisposable + public class KestrelEngine : IDisposable { public KestrelEngine(ServiceContext context) : this(new Libuv(), context) @@ -19,15 +21,21 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal // For testing internal KestrelEngine(Libuv uv, ServiceContext context) - : base(context) { Libuv = uv; + ServiceContext = context; Threads = new List(); } public Libuv Libuv { get; private set; } + public ServiceContext ServiceContext { get; set; } public List Threads { get; private set; } + public IApplicationLifetime AppLifetime => ServiceContext.AppLifetime; + public IKestrelTrace Log => ServiceContext.Log; + public IThreadPool ThreadPool => ServiceContext.ThreadPool; + public KestrelServerOptions ServerOptions => ServiceContext.ServerOptions; + public void Start(int count) { for (var index = 0; index < count; index++) @@ -71,16 +79,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal if (single) { var listener = usingPipes ? - (Listener) new PipeListener(this) : - new TcpListener(this); + (Listener) new PipeListener(ServiceContext) : + new TcpListener(ServiceContext); listeners.Add(listener); listener.StartAsync(address, thread).Wait(); } else if (first) { var listener = usingPipes - ? (ListenerPrimary) new PipeListenerPrimary(this) - : new TcpListenerPrimary(this); + ? (ListenerPrimary) new PipeListenerPrimary(ServiceContext) + : new TcpListenerPrimary(ServiceContext); listeners.Add(listener); listener.StartAsync(pipeName, address, thread).Wait(); @@ -88,8 +96,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal else { var listener = usingPipes - ? (ListenerSecondary) new PipeListenerSecondary(this) - : new TcpListenerSecondary(this); + ? (ListenerSecondary) new PipeListenerSecondary(ServiceContext) + : new TcpListenerSecondary(ServiceContext); listeners.Add(listener); listener.StartAsync(pipeName, address, thread).Wait(); } diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/ServiceContext.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/ServiceContext.cs index 77932ecf15..10ee1378f7 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/ServiceContext.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/ServiceContext.cs @@ -10,20 +10,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal { public class ServiceContext { - public ServiceContext() - { - } - - public ServiceContext(ServiceContext context) - { - AppLifetime = context.AppLifetime; - Log = context.Log; - ThreadPool = context.ThreadPool; - FrameFactory = context.FrameFactory; - DateHeaderValueManager = context.DateHeaderValueManager; - ServerOptions = context.ServerOptions; - } - public IApplicationLifetime AppLifetime { get; set; } public IKestrelTrace Log { get; set; } diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/ConnectionTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/ConnectionTests.cs index 0d42df12f0..a987904831 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/ConnectionTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/ConnectionTests.cs @@ -27,10 +27,13 @@ namespace Microsoft.AspNetCore.Server.KestrelTests engine.Start(count: 1); var trace = new TestKestrelTrace(); - var context = new ListenerContext(new TestServiceContext()) + var serviceContext = new TestServiceContext { FrameFactory = connectionContext => new Frame( new DummyApplication(httpContext => TaskCache.CompletedTask), connectionContext), + }; + var context = new ListenerContext(serviceContext) + { ServerAddress = ServerAddress.FromUrl("http://127.0.0.1:0"), Thread = engine.Threads[0] }; diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/FrameResponseHeadersTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/FrameResponseHeadersTests.cs index bfca1e9158..c57ddff257 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/FrameResponseHeadersTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/FrameResponseHeadersTests.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Server.Kestrel; +using Microsoft.AspNetCore.Server.Kestrel.Internal; using Microsoft.AspNetCore.Server.Kestrel.Internal.Http; using Microsoft.Extensions.Primitives; using Xunit; @@ -18,12 +19,16 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { var serverOptions = new KestrelServerOptions(); - var connectionContext = new ConnectionContext + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), - ServerOptions = serverOptions, + ServerOptions = serverOptions }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); var frame = new Frame(application: null, context: connectionContext); diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/FrameTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/FrameTests.cs index 09bffeee85..4854a2d369 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/FrameTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/FrameTests.cs @@ -27,12 +27,17 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), - ServerOptions = new KestrelServerOptions(), + ServerOptions = new KestrelServerOptions() }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); + var frame = new Frame(application: null, context: connectionContext); frame.Reset(); frame.InitializeHeaders(); @@ -70,12 +75,17 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), - ServerOptions = new KestrelServerOptions(), + ServerOptions = new KestrelServerOptions() }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); + var frame = new Frame(application: null, context: connectionContext); frame.Reset(); frame.InitializeHeaders(); @@ -112,12 +122,17 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), - ServerOptions = new KestrelServerOptions(), + ServerOptions = new KestrelServerOptions() }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); + var frame = new Frame(application: null, context: connectionContext); frame.Reset(); frame.InitializeHeaders(); @@ -153,12 +168,18 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), + Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); + var frame = new Frame(application: null, context: connectionContext); frame.Reset(); frame.InitializeHeaders(); @@ -194,13 +215,18 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); + var frame = new Frame(application: null, context: connectionContext); frame.Reset(); frame.InitializeHeaders(); @@ -222,13 +248,18 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); + var frame = new Frame(application: null, context: connectionContext); frame.Reset(); frame.InitializeHeaders(); @@ -259,13 +290,18 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); + var frame = new Frame(application: null, context: connectionContext); frame.Reset(); frame.InitializeHeaders(); @@ -290,13 +326,18 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); + var frame = new Frame(application: null, context: connectionContext); frame.Reset(); frame.InitializeHeaders(); @@ -322,13 +363,18 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); + var frame = new Frame(application: null, context: connectionContext); frame.Reset(); frame.InitializeHeaders(); @@ -358,13 +404,18 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); + var frame = new Frame(application: null, context: connectionContext); frame.Reset(); frame.InitializeHeaders(); @@ -389,13 +440,18 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); + var frame = new Frame(application: null, context: connectionContext); frame.Reset(); frame.InitializeHeaders(); @@ -422,13 +478,17 @@ namespace Microsoft.AspNetCore.Server.KestrelTests var options = new KestrelServerOptions(); options.Limits.MaxRequestHeadersTotalSize = headerLine.Length - 1; - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = options, Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); var frame = new Frame(application: null, context: connectionContext); frame.Reset(); @@ -456,13 +516,17 @@ namespace Microsoft.AspNetCore.Server.KestrelTests var options = new KestrelServerOptions(); options.Limits.MaxRequestHeaderCount = 1; - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = options, Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); var frame = new Frame(application: null, context: connectionContext); frame.Reset(); @@ -491,12 +555,17 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), - ServerOptions = new KestrelServerOptions(), + ServerOptions = new KestrelServerOptions() }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); + var frame = new Frame(application: null, context: connectionContext); frame.Reset(); frame.InitializeHeaders(); @@ -519,12 +588,17 @@ namespace Microsoft.AspNetCore.Server.KestrelTests public void ResetResetsScheme() { // Arrange - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), - ServerOptions = new KestrelServerOptions(), + ServerOptions = new KestrelServerOptions() }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); + var frame = new Frame(application: null, context: connectionContext); frame.Scheme = "https"; @@ -550,12 +624,16 @@ namespace Microsoft.AspNetCore.Server.KestrelTests options.Limits.MaxRequestHeadersTotalSize = headerLine1.Length; options.Limits.MaxRequestHeaderCount = 1; - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = options }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); var frame = new Frame(application: null, context: connectionContext); frame.Reset(); @@ -583,13 +661,20 @@ namespace Microsoft.AspNetCore.Server.KestrelTests public void ThrowsWhenStatusCodeIsSetAfterResponseStarted() { // Arrange - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), - ServerOptions = new KestrelServerOptions(), + ServerOptions = new KestrelServerOptions() + }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext) + { SocketOutput = new MockSocketOuptut() }; + var frame = new Frame(application: null, context: connectionContext); frame.InitializeHeaders(); @@ -605,13 +690,20 @@ namespace Microsoft.AspNetCore.Server.KestrelTests public void ThrowsWhenReasonPhraseIsSetAfterResponseStarted() { // Arrange - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), - ServerOptions = new KestrelServerOptions(), + ServerOptions = new KestrelServerOptions() + }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext) + { SocketOutput = new MockSocketOuptut() }; + var frame = new Frame(application: null, context: connectionContext); frame.InitializeHeaders(); @@ -627,13 +719,20 @@ namespace Microsoft.AspNetCore.Server.KestrelTests public void ThrowsWhenOnStartingIsSetAfterResponseStarted() { // Arrange - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), - ServerOptions = new KestrelServerOptions(), + ServerOptions = new KestrelServerOptions() + }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext) + { SocketOutput = new MockSocketOuptut() }; + var frame = new Frame(application: null, context: connectionContext); frame.InitializeHeaders(); frame.Write(new ArraySegment(new byte[1])); @@ -647,13 +746,20 @@ namespace Microsoft.AspNetCore.Server.KestrelTests public void InitializeHeadersResetsRequestHeaders() { // Arrange - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), - ServerOptions = new KestrelServerOptions(), + ServerOptions = new KestrelServerOptions() + }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext) + { SocketOutput = new MockSocketOuptut() }; + var frame = new Frame(application: null, context: connectionContext); frame.InitializeHeaders(); @@ -671,13 +777,20 @@ namespace Microsoft.AspNetCore.Server.KestrelTests public void InitializeHeadersResetsResponseHeaders() { // Arrange - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), - ServerOptions = new KestrelServerOptions(), + ServerOptions = new KestrelServerOptions() + }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext) + { SocketOutput = new MockSocketOuptut() }; + var frame = new Frame(application: null, context: connectionContext); frame.InitializeHeaders(); @@ -695,13 +808,20 @@ namespace Microsoft.AspNetCore.Server.KestrelTests public void InitializeStreamsResetsStreams() { // Arrange - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), - ServerOptions = new KestrelServerOptions(), + ServerOptions = new KestrelServerOptions() + }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext) + { SocketOutput = new MockSocketOuptut() }; + var frame = new Frame(application: null, context: connectionContext); frame.InitializeHeaders(); @@ -732,14 +852,20 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { - ConnectionControl = new Mock().Object, DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), + }; + var connectionContext = new ConnectionContext(listenerContext) + { + ConnectionControl = new Mock().Object + }; var frame = new Frame(application: null, context: connectionContext); frame.Reset(); @@ -779,14 +905,20 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { - ConnectionControl = new Mock().Object, DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), + }; + var connectionContext = new ConnectionContext(listenerContext) + { + ConnectionControl = new Mock().Object + }; var frame = new Frame(application: null, context: connectionContext); frame.Reset(); @@ -806,15 +938,21 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionControl = new Mock(); - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { - ConnectionControl = connectionControl.Object, DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), + }; + var connectionControl = new Mock(); + var connectionContext = new ConnectionContext(listenerContext) + { + ConnectionControl = connectionControl.Object + }; var frame = new Frame(application: null, context: connectionContext); frame.Reset(); @@ -834,15 +972,21 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionControl = new Mock(); - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { - ConnectionControl = connectionControl.Object, DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionControl = new Mock(); + var connectionContext = new ConnectionContext(listenerContext) + { + ConnectionControl = connectionControl.Object + }; var frame = new Frame(application: null, context: connectionContext); frame.Reset(); @@ -859,11 +1003,9 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { - ConnectionControl = Mock.Of(), DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions() { Limits = @@ -873,6 +1015,14 @@ namespace Microsoft.AspNetCore.Server.KestrelTests }, Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext) + { + ConnectionControl = Mock.Of() + }; var frame = new Frame(application: null, context: connectionContext); frame.Reset(); @@ -904,14 +1054,20 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { - ConnectionControl = Mock.Of(), DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext) + { + ConnectionControl = Mock.Of() + }; var frame = new Frame(application: null, context: connectionContext); frame.Reset(); @@ -932,14 +1088,20 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { - ConnectionControl = Mock.Of(), DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext) + { + ConnectionControl = Mock.Of(), + }; var frame = new Frame(application: null, context: connectionContext); frame.Reset(); @@ -960,13 +1122,17 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext); var frame = new Frame(application: null, context: connectionContext); frame.Reset(); frame.InitializeHeaders(); @@ -1013,13 +1179,17 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), + }; + var connectionContext = new ConnectionContext(listenerContext); var frame = new Frame(application: null, context: connectionContext); frame.Reset(); frame.InitializeHeaders(); @@ -1039,20 +1209,26 @@ namespace Microsoft.AspNetCore.Server.KestrelTests using (var pool = new MemoryPool()) using (var socketInput = new SocketInput(pool, ltp)) { - var connectionControl = new Mock(); - var connectionContext = new ConnectionContext() + var serviceContext = new ServiceContext { - ConnectionControl = connectionControl.Object, DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), ServerOptions = new KestrelServerOptions(), Log = trace }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionControl = new Mock(); + var connectionContext = new ConnectionContext(listenerContext) + { + ConnectionControl = connectionControl.Object + }; var frame = new Frame(application: null, context: connectionContext); frame.Reset(); var requestProcessingTask = frame.RequestProcessingAsync(); - connectionControl.Verify(cc => cc.SetTimeout((long)connectionContext.ServerOptions.Limits.KeepAliveTimeout.TotalMilliseconds)); + connectionControl.Verify(cc => cc.SetTimeout((long)serviceContext.ServerOptions.Limits.KeepAliveTimeout.TotalMilliseconds)); frame.Stop(); socketInput.IncomingFin(); diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockConnection.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockConnection.cs index 7a095cfcff..ee781a3935 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockConnection.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockConnection.cs @@ -5,6 +5,7 @@ using System; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Server.Kestrel; +using Microsoft.AspNetCore.Server.Kestrel.Internal; using Microsoft.AspNetCore.Server.Kestrel.Internal.Http; namespace Microsoft.AspNetCore.Server.KestrelTests.TestHelpers @@ -17,7 +18,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests.TestHelpers { ConnectionControl = this; RequestAbortedSource = new CancellationTokenSource(); - ServerOptions = options; + ListenerContext = new ListenerContext(new ServiceContext { ServerOptions = options }); } public override void Abort(Exception error = null) diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/TestInput.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/TestInput.cs index a9b357d139..47fb05c169 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/TestInput.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/TestInput.cs @@ -22,19 +22,22 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { var trace = new KestrelTrace(new TestKestrelTrace()); var ltp = new LoggingThreadPool(trace); - var connectionContext = new ConnectionContext() - { - ServerAddress = new ServerAddress(), - ServerOptions = new KestrelServerOptions() - }; - var context = new Frame(null, connectionContext) + var serviceContext = new ServiceContext { DateHeaderValueManager = new DateHeaderValueManager(), - ServerAddress = ServerAddress.FromUrl("http://localhost:5000"), - ConnectionControl = this, - FrameControl = this + ServerOptions = new KestrelServerOptions() }; + var listenerContext = new ListenerContext(serviceContext) + { + ServerAddress = ServerAddress.FromUrl("http://localhost:5000") + }; + var connectionContext = new ConnectionContext(listenerContext) + { + ConnectionControl = this + }; + var context = new Frame(null, connectionContext); FrameContext = context; + FrameContext.FrameControl = this; _memoryPool = new MemoryPool(); FrameContext.SocketInput = new SocketInput(_memoryPool, ltp);