Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert TLS connection adapter to connection middleware #11109

Merged
merged 5 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class AdaptedPipeline : IDuplexPipe
public AdaptedPipeline(IDuplexPipe transport,
Pipe inputPipe,
Pipe outputPipe,
IKestrelTrace log,
ILogger log,
int minAllocBufferSize)
{
TransportStream = new RawStream(transport.Input, transport.Output, throwOnCancelled: true);
Expand All @@ -37,7 +37,7 @@ public AdaptedPipeline(IDuplexPipe transport,

public Pipe Output { get; }

public IKestrelTrace Log { get; }
public ILogger Log { get; }

PipeReader IDuplexPipe.Input => Input.Reader;

Expand Down
7 changes: 7 additions & 0 deletions src/Servers/Kestrel/Core/src/HttpsConnectionAdapterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO.Pipelines;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
Expand Down Expand Up @@ -96,5 +97,11 @@ public TimeSpan HandshakeTimeout
_handshakeTimeout = value != Timeout.InfiniteTimeSpan ? value : TimeSpan.MaxValue;
}
}

internal PipeScheduler Scheduler { get; set; } = PipeScheduler.ThreadPool;

internal long? MaxInputBufferSize { get; set; }

internal long? MaxOutputBufferSize { get; set; }
}
}
74 changes: 53 additions & 21 deletions src/Servers/Kestrel/Core/src/Internal/HttpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,64 +215,90 @@ private HttpConnectionContext CreateDerivedContext(IDuplexPipe transport)

private void StopProcessingNextRequest()
{
ProtocolSelectionState previousState;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need these changes?

lock (_protocolSelectionLock)
{
previousState = _protocolSelectionState;

switch (_protocolSelectionState)
{
case ProtocolSelectionState.Initializing:
CloseUninitializedConnection(new ConnectionAbortedException(CoreStrings.ServerShutdownDuringConnectionInitialization));
_protocolSelectionState = ProtocolSelectionState.Aborted;
break;
case ProtocolSelectionState.Selected:
_requestProcessor.StopProcessingNextRequest();
break;
case ProtocolSelectionState.Aborted:
break;
}
}

switch (previousState)
{
case ProtocolSelectionState.Initializing:
CloseUninitializedConnection(new ConnectionAbortedException(CoreStrings.ServerShutdownDuringConnectionInitialization));
break;
case ProtocolSelectionState.Selected:
_requestProcessor.StopProcessingNextRequest();
break;
case ProtocolSelectionState.Aborted:
break;
}
}

private void OnInputOrOutputCompleted()
{
ProtocolSelectionState previousState;
lock (_protocolSelectionLock)
{
previousState = _protocolSelectionState;

switch (_protocolSelectionState)
{
case ProtocolSelectionState.Initializing:
// OnReader/WriterCompleted callbacks are not wired until after leaving the Initializing state.
Debug.Assert(false);

CloseUninitializedConnection(new ConnectionAbortedException("HttpConnection.OnInputOrOutputCompleted() called while in the ProtocolSelectionState.Initializing state!?"));
_protocolSelectionState = ProtocolSelectionState.Aborted;
break;
case ProtocolSelectionState.Selected:
_requestProcessor.OnInputOrOutputCompleted();
break;
case ProtocolSelectionState.Aborted:
break;
}
}

switch (previousState)
{
case ProtocolSelectionState.Initializing:
// ConnectionClosed callback is not wired up until after leaving the Initializing state.
Debug.Assert(false);

CloseUninitializedConnection(new ConnectionAbortedException("HttpConnection.OnInputOrOutputCompleted() called while in the ProtocolSelectionState.Initializing state!?"));
break;
case ProtocolSelectionState.Selected:
_requestProcessor.OnInputOrOutputCompleted();
break;
case ProtocolSelectionState.Aborted:
break;
}
}

private void Abort(ConnectionAbortedException ex)
{
ProtocolSelectionState previousState;

lock (_protocolSelectionLock)
{
switch (_protocolSelectionState)
{
case ProtocolSelectionState.Initializing:
CloseUninitializedConnection(ex);
break;
case ProtocolSelectionState.Selected:
_requestProcessor.Abort(ex);
break;
case ProtocolSelectionState.Aborted:
break;
}

previousState = _protocolSelectionState;
_protocolSelectionState = ProtocolSelectionState.Aborted;
}

switch (previousState)
{
case ProtocolSelectionState.Initializing:
CloseUninitializedConnection(ex);
break;
case ProtocolSelectionState.Selected:
_requestProcessor.Abort(ex);
break;
case ProtocolSelectionState.Aborted:
break;
}
}

private async Task<Stream> ApplyConnectionAdaptersAsync(RawStream stream)
Expand Down Expand Up @@ -365,6 +391,12 @@ private void Tick()
private void CloseUninitializedConnection(ConnectionAbortedException abortReason)
{
_context.ConnectionContext.Abort(abortReason);

if (_context.ConnectionAdapters.Count > 0)
{
_adaptedTransport.Input.Complete();
_adaptedTransport.Output.Complete();
}
}

public void OnTimeout(TimeoutReason reason)
Expand Down
Loading