Skip to content

Commit 689b052

Browse files
authored
Convert TLS connection adapter to connection middleware (#11109)
1 parent 0b5d4ba commit 689b052

19 files changed

+411
-364
lines changed

src/Servers/Kestrel/Core/src/Adapter/Internal/AdaptedPipeline.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal class AdaptedPipeline : IDuplexPipe
2121
public AdaptedPipeline(IDuplexPipe transport,
2222
Pipe inputPipe,
2323
Pipe outputPipe,
24-
IKestrelTrace log,
24+
ILogger log,
2525
int minAllocBufferSize)
2626
{
2727
TransportStream = new RawStream(transport.Input, transport.Output, throwOnCancelled: true);
@@ -37,7 +37,7 @@ public AdaptedPipeline(IDuplexPipe transport,
3737

3838
public Pipe Output { get; }
3939

40-
public IKestrelTrace Log { get; }
40+
public ILogger Log { get; }
4141

4242
PipeReader IDuplexPipe.Input => Input.Reader;
4343

src/Servers/Kestrel/Core/src/HttpsConnectionAdapterOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.IO.Pipelines;
56
using System.Net.Security;
67
using System.Security.Authentication;
78
using System.Security.Cryptography.X509Certificates;
@@ -96,5 +97,11 @@ public TimeSpan HandshakeTimeout
9697
_handshakeTimeout = value != Timeout.InfiniteTimeSpan ? value : TimeSpan.MaxValue;
9798
}
9899
}
100+
101+
internal PipeScheduler Scheduler { get; set; } = PipeScheduler.ThreadPool;
102+
103+
internal long? MaxInputBufferSize { get; set; }
104+
105+
internal long? MaxOutputBufferSize { get; set; }
99106
}
100107
}

src/Servers/Kestrel/Core/src/Internal/HttpConnection.cs

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -215,64 +215,90 @@ private HttpConnectionContext CreateDerivedContext(IDuplexPipe transport)
215215

216216
private void StopProcessingNextRequest()
217217
{
218+
ProtocolSelectionState previousState;
218219
lock (_protocolSelectionLock)
219220
{
221+
previousState = _protocolSelectionState;
222+
220223
switch (_protocolSelectionState)
221224
{
222225
case ProtocolSelectionState.Initializing:
223-
CloseUninitializedConnection(new ConnectionAbortedException(CoreStrings.ServerShutdownDuringConnectionInitialization));
224226
_protocolSelectionState = ProtocolSelectionState.Aborted;
225227
break;
226228
case ProtocolSelectionState.Selected:
227-
_requestProcessor.StopProcessingNextRequest();
228-
break;
229229
case ProtocolSelectionState.Aborted:
230230
break;
231231
}
232232
}
233+
234+
switch (previousState)
235+
{
236+
case ProtocolSelectionState.Initializing:
237+
CloseUninitializedConnection(new ConnectionAbortedException(CoreStrings.ServerShutdownDuringConnectionInitialization));
238+
break;
239+
case ProtocolSelectionState.Selected:
240+
_requestProcessor.StopProcessingNextRequest();
241+
break;
242+
case ProtocolSelectionState.Aborted:
243+
break;
244+
}
233245
}
234246

235247
private void OnInputOrOutputCompleted()
236248
{
249+
ProtocolSelectionState previousState;
237250
lock (_protocolSelectionLock)
238251
{
252+
previousState = _protocolSelectionState;
253+
239254
switch (_protocolSelectionState)
240255
{
241256
case ProtocolSelectionState.Initializing:
242-
// OnReader/WriterCompleted callbacks are not wired until after leaving the Initializing state.
243-
Debug.Assert(false);
244-
245-
CloseUninitializedConnection(new ConnectionAbortedException("HttpConnection.OnInputOrOutputCompleted() called while in the ProtocolSelectionState.Initializing state!?"));
246257
_protocolSelectionState = ProtocolSelectionState.Aborted;
247258
break;
248259
case ProtocolSelectionState.Selected:
249-
_requestProcessor.OnInputOrOutputCompleted();
250-
break;
251260
case ProtocolSelectionState.Aborted:
252261
break;
253262
}
263+
}
264+
265+
switch (previousState)
266+
{
267+
case ProtocolSelectionState.Initializing:
268+
// ConnectionClosed callback is not wired up until after leaving the Initializing state.
269+
Debug.Assert(false);
254270

271+
CloseUninitializedConnection(new ConnectionAbortedException("HttpConnection.OnInputOrOutputCompleted() called while in the ProtocolSelectionState.Initializing state!?"));
272+
break;
273+
case ProtocolSelectionState.Selected:
274+
_requestProcessor.OnInputOrOutputCompleted();
275+
break;
276+
case ProtocolSelectionState.Aborted:
277+
break;
255278
}
256279
}
257280

258281
private void Abort(ConnectionAbortedException ex)
259282
{
283+
ProtocolSelectionState previousState;
284+
260285
lock (_protocolSelectionLock)
261286
{
262-
switch (_protocolSelectionState)
263-
{
264-
case ProtocolSelectionState.Initializing:
265-
CloseUninitializedConnection(ex);
266-
break;
267-
case ProtocolSelectionState.Selected:
268-
_requestProcessor.Abort(ex);
269-
break;
270-
case ProtocolSelectionState.Aborted:
271-
break;
272-
}
273-
287+
previousState = _protocolSelectionState;
274288
_protocolSelectionState = ProtocolSelectionState.Aborted;
275289
}
290+
291+
switch (previousState)
292+
{
293+
case ProtocolSelectionState.Initializing:
294+
CloseUninitializedConnection(ex);
295+
break;
296+
case ProtocolSelectionState.Selected:
297+
_requestProcessor.Abort(ex);
298+
break;
299+
case ProtocolSelectionState.Aborted:
300+
break;
301+
}
276302
}
277303

278304
private async Task<Stream> ApplyConnectionAdaptersAsync(RawStream stream)
@@ -365,6 +391,12 @@ private void Tick()
365391
private void CloseUninitializedConnection(ConnectionAbortedException abortReason)
366392
{
367393
_context.ConnectionContext.Abort(abortReason);
394+
395+
if (_context.ConnectionAdapters.Count > 0)
396+
{
397+
_adaptedTransport.Input.Complete();
398+
_adaptedTransport.Output.Complete();
399+
}
368400
}
369401

370402
public void OnTimeout(TimeoutReason reason)

0 commit comments

Comments
 (0)