Small cleanup to HttpConnection (#12346)
- Remove code that detects the Initializing state. We're already selected before any of those callbacks can run. - Added asserts
This commit is contained in:
parent
09dfe83aa9
commit
ac7f30e63b
|
|
@ -18,7 +18,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
|
||||||
{
|
{
|
||||||
internal class HttpConnection : ITimeoutHandler
|
internal class HttpConnection : ITimeoutHandler
|
||||||
{
|
{
|
||||||
private static readonly ReadOnlyMemory<byte> Http2Id = new[] { (byte)'h', (byte)'2' };
|
// Use C#7.3's ReadOnlySpan<byte> optimization for static data https://vcsjones.com/2019/02/01/csharp-readonly-span-bytes-static/
|
||||||
|
private static ReadOnlySpan<byte> Http2Id => new[] { (byte)'h', (byte)'2' };
|
||||||
|
|
||||||
private readonly HttpConnectionContext _context;
|
private readonly HttpConnectionContext _context;
|
||||||
private readonly ISystemClock _systemClock;
|
private readonly ISystemClock _systemClock;
|
||||||
|
|
@ -67,7 +68,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
|
||||||
break;
|
break;
|
||||||
case HttpProtocols.None:
|
case HttpProtocols.None:
|
||||||
// An error was already logged in SelectProtocol(), but we should close the connection.
|
// An error was already logged in SelectProtocol(), but we should close the connection.
|
||||||
Abort(new ConnectionAbortedException(CoreStrings.ProtocolSelectionFailed));
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// SelectProtocol() only returns Http1, Http2 or None.
|
// SelectProtocol() only returns Http1, Http2 or None.
|
||||||
|
|
@ -127,12 +127,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
|
||||||
lock (_protocolSelectionLock)
|
lock (_protocolSelectionLock)
|
||||||
{
|
{
|
||||||
previousState = _protocolSelectionState;
|
previousState = _protocolSelectionState;
|
||||||
|
Debug.Assert(previousState != ProtocolSelectionState.Initializing, "The state should never be initializing");
|
||||||
|
|
||||||
switch (_protocolSelectionState)
|
switch (_protocolSelectionState)
|
||||||
{
|
{
|
||||||
case ProtocolSelectionState.Initializing:
|
|
||||||
_protocolSelectionState = ProtocolSelectionState.Aborted;
|
|
||||||
break;
|
|
||||||
case ProtocolSelectionState.Selected:
|
case ProtocolSelectionState.Selected:
|
||||||
case ProtocolSelectionState.Aborted:
|
case ProtocolSelectionState.Aborted:
|
||||||
break;
|
break;
|
||||||
|
|
@ -141,9 +139,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
|
||||||
|
|
||||||
switch (previousState)
|
switch (previousState)
|
||||||
{
|
{
|
||||||
case ProtocolSelectionState.Initializing:
|
|
||||||
_context.ConnectionContext.Abort(new ConnectionAbortedException(CoreStrings.ServerShutdownDuringConnectionInitialization));
|
|
||||||
break;
|
|
||||||
case ProtocolSelectionState.Selected:
|
case ProtocolSelectionState.Selected:
|
||||||
_requestProcessor.StopProcessingNextRequest();
|
_requestProcessor.StopProcessingNextRequest();
|
||||||
break;
|
break;
|
||||||
|
|
@ -158,12 +153,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
|
||||||
lock (_protocolSelectionLock)
|
lock (_protocolSelectionLock)
|
||||||
{
|
{
|
||||||
previousState = _protocolSelectionState;
|
previousState = _protocolSelectionState;
|
||||||
|
Debug.Assert(previousState != ProtocolSelectionState.Initializing, "The state should never be initializing");
|
||||||
|
|
||||||
switch (_protocolSelectionState)
|
switch (_protocolSelectionState)
|
||||||
{
|
{
|
||||||
case ProtocolSelectionState.Initializing:
|
|
||||||
_protocolSelectionState = ProtocolSelectionState.Aborted;
|
|
||||||
break;
|
|
||||||
case ProtocolSelectionState.Selected:
|
case ProtocolSelectionState.Selected:
|
||||||
case ProtocolSelectionState.Aborted:
|
case ProtocolSelectionState.Aborted:
|
||||||
break;
|
break;
|
||||||
|
|
@ -172,12 +165,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
|
||||||
|
|
||||||
switch (previousState)
|
switch (previousState)
|
||||||
{
|
{
|
||||||
case ProtocolSelectionState.Initializing:
|
|
||||||
// ConnectionClosed callback is not wired up until after leaving the Initializing state.
|
|
||||||
Debug.Assert(false);
|
|
||||||
|
|
||||||
_context.ConnectionContext.Abort(new ConnectionAbortedException("HttpConnection.OnInputOrOutputCompleted() called while in the ProtocolSelectionState.Initializing state!?"));
|
|
||||||
break;
|
|
||||||
case ProtocolSelectionState.Selected:
|
case ProtocolSelectionState.Selected:
|
||||||
_requestProcessor.OnInputOrOutputCompleted();
|
_requestProcessor.OnInputOrOutputCompleted();
|
||||||
break;
|
break;
|
||||||
|
|
@ -193,14 +180,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
|
||||||
lock (_protocolSelectionLock)
|
lock (_protocolSelectionLock)
|
||||||
{
|
{
|
||||||
previousState = _protocolSelectionState;
|
previousState = _protocolSelectionState;
|
||||||
|
Debug.Assert(previousState != ProtocolSelectionState.Initializing, "The state should never be initializing");
|
||||||
|
|
||||||
_protocolSelectionState = ProtocolSelectionState.Aborted;
|
_protocolSelectionState = ProtocolSelectionState.Aborted;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (previousState)
|
switch (previousState)
|
||||||
{
|
{
|
||||||
case ProtocolSelectionState.Initializing:
|
|
||||||
_context.ConnectionContext.Abort(ex);
|
|
||||||
break;
|
|
||||||
case ProtocolSelectionState.Selected:
|
case ProtocolSelectionState.Selected:
|
||||||
_requestProcessor.Abort(ex);
|
_requestProcessor.Abort(ex);
|
||||||
break;
|
break;
|
||||||
|
|
@ -224,7 +210,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
|
||||||
error = CoreStrings.EndPointRequiresAtLeastOneProtocol;
|
error = CoreStrings.EndPointRequiresAtLeastOneProtocol;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!http1Enabled && http2Enabled && hasTls && !Http2Id.Span.SequenceEqual(applicationProtocol.Span))
|
if (!http1Enabled && http2Enabled && hasTls && !Http2Id.SequenceEqual(applicationProtocol.Span))
|
||||||
{
|
{
|
||||||
error = CoreStrings.EndPointHttp2NotNegotiated;
|
error = CoreStrings.EndPointHttp2NotNegotiated;
|
||||||
}
|
}
|
||||||
|
|
@ -241,7 +227,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
|
||||||
return HttpProtocols.Http1;
|
return HttpProtocols.Http1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return http2Enabled && (!hasTls || Http2Id.Span.SequenceEqual(applicationProtocol.Span)) ? HttpProtocols.Http2 : HttpProtocols.Http1;
|
return http2Enabled && (!hasTls || Http2Id.SequenceEqual(applicationProtocol.Span)) ? HttpProtocols.Http2 : HttpProtocols.Http1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Tick()
|
private void Tick()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue