Skip to content

Commit

Permalink
Test System.Net.Connections interfaces in middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
halter73 committed Mar 26, 2020
1 parent 590a124 commit 89a9cbc
Show file tree
Hide file tree
Showing 4 changed files with 415 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/Servers/Kestrel/Core/src/KestrelServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ async Task OnBind(ListenOptions options)
var connectionDispatcher = new ConnectionDispatcher(ServiceContext, connectionDelegate);
var factory = _transportFactories.Last();
var transport = await factory.BindAsync(options.EndPoint, options: null).ConfigureAwait(false);
transport = options.BuildConnectionListener(transport);

var acceptLoopTask = connectionDispatcher.StartAcceptingConnections(transport);

Expand Down
17 changes: 17 additions & 0 deletions src/Servers/Kestrel/Core/src/ListenOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ListenOptions : IConnectionBuilder, IMultiplexedConnectionBuilder
{
internal readonly List<Func<ConnectionDelegate, ConnectionDelegate>> _middleware = new List<Func<ConnectionDelegate, ConnectionDelegate>>();
internal readonly List<Func<MultiplexedConnectionDelegate, MultiplexedConnectionDelegate>> _multiplexedMiddleware = new List<Func<MultiplexedConnectionDelegate, MultiplexedConnectionDelegate>>();
internal readonly List<Func<System.Net.Connections.IConnectionListener, System.Net.Connections.IConnectionListener>> _listenerFilters = new List<Func<System.Net.Connections.IConnectionListener, System.Net.Connections.IConnectionListener>>();

internal ListenOptions(IPEndPoint endPoint)
{
Expand Down Expand Up @@ -130,6 +131,12 @@ IMultiplexedConnectionBuilder IMultiplexedConnectionBuilder.Use(Func<Multiplexed
return this;
}

public ListenOptions UseListenerFilter(Func<System.Net.Connections.IConnectionListener, System.Net.Connections.IConnectionListener> listenerFilter)
{
_listenerFilters.Add(listenerFilter);
return this;
}

public ConnectionDelegate Build()
{
ConnectionDelegate app = context =>
Expand Down Expand Up @@ -162,6 +169,16 @@ MultiplexedConnectionDelegate IMultiplexedConnectionBuilder.Build()
return app;
}

public System.Net.Connections.IConnectionListener BuildConnectionListener(System.Net.Connections.IConnectionListener previous)
{
foreach (var filter in _listenerFilters)
{
previous = filter(previous);
}

return previous;
}

internal virtual async Task BindAsync(AddressBindContext context)
{
await AddressBinder.BindEndpointAsync(this, context).ConfigureAwait(false);
Expand Down
10 changes: 1 addition & 9 deletions src/Servers/Kestrel/Core/src/ListenOptionsHttpsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,7 @@ public static ListenOptions UseHttps(this ListenOptions listenOptions, HttpsConn
var loggerFactory = listenOptions.KestrelServerOptions?.ApplicationServices.GetRequiredService<ILoggerFactory>() ?? NullLoggerFactory.Instance;

listenOptions.IsTls = true;
listenOptions.Use(next =>
{
// Set the list of protocols from listen options
httpsOptions.HttpProtocols = listenOptions.Protocols;
var middleware = new HttpsConnectionMiddleware(next, httpsOptions, loggerFactory);
return middleware.OnConnectionAsync;
});

return listenOptions;
return listenOptions.UseListenerFilter(previous => new HttpsConnectionMiddleware2(previous, httpsOptions, loggerFactory));
}
}
}
Loading

0 comments on commit 89a9cbc

Please sign in to comment.