Change context relationships from inheritance to composition.

This commit is contained in:
Cesar Blum Silveira 2016-09-05 20:41:43 -07:00
parent 78c859d7ea
commit 375e8b7022
13 changed files with 363 additions and 158 deletions

View File

@ -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<ConnectionContext, Frame> 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);

View File

@ -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; }

View File

@ -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<byte> _endChunkedResponseBytes = CreateAsciiByteArraySegment("0\r\n\r\n");
private static readonly ArraySegment<byte> _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<IFeatureCollection> 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

View File

@ -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)

View File

@ -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;
}
}

View File

@ -12,7 +12,7 @@ using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
{
/// <summary>
/// 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.
/// </summary>
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,

View File

@ -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<KestrelThread>();
}
public Libuv Libuv { get; private set; }
public ServiceContext ServiceContext { get; set; }
public List<KestrelThread> 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();
}

View File

@ -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; }

View File

@ -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<HttpContext>(
new DummyApplication(httpContext => TaskCache.CompletedTask), connectionContext),
};
var context = new ListenerContext(serviceContext)
{
ServerAddress = ServerAddress.FromUrl("http://127.0.0.1:0"),
Thread = engine.Threads[0]
};

View File

@ -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<object>(application: null, context: connectionContext);

View File

@ -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<object>(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<object>(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<object>(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<object>(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<object>(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<object>(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<object>(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<object>(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<object>(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<object>(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<object>(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<object>(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<object>(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<object>(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<object>(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<object>(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<object>(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<object>(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<object>(application: null, context: connectionContext);
frame.InitializeHeaders();
frame.Write(new ArraySegment<byte>(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<object>(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<object>(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<object>(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<IConnectionControl>().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<IConnectionControl>().Object
};
var frame = new Frame<object>(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<IConnectionControl>().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<IConnectionControl>().Object
};
var frame = new Frame<object>(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<IConnectionControl>();
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<IConnectionControl>();
var connectionContext = new ConnectionContext(listenerContext)
{
ConnectionControl = connectionControl.Object
};
var frame = new Frame<object>(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<IConnectionControl>();
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<IConnectionControl>();
var connectionContext = new ConnectionContext(listenerContext)
{
ConnectionControl = connectionControl.Object
};
var frame = new Frame<object>(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<IConnectionControl>(),
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<IConnectionControl>()
};
var frame = new Frame<object>(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<IConnectionControl>(),
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<IConnectionControl>()
};
var frame = new Frame<object>(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<IConnectionControl>(),
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<IConnectionControl>(),
};
var frame = new Frame<object>(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<object>(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<object>(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<IConnectionControl>();
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<IConnectionControl>();
var connectionContext = new ConnectionContext(listenerContext)
{
ConnectionControl = connectionControl.Object
};
var frame = new Frame<object>(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();

View File

@ -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)

View File

@ -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<object>(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<object>(null, connectionContext);
FrameContext = context;
FrameContext.FrameControl = this;
_memoryPool = new MemoryPool();
FrameContext.SocketInput = new SocketInput(_memoryPool, ltp);