Copy connection middleware when using ListenLocalHost (#2447)

- Connection middleware got removed during Clone when using ListenLocalhost
This commit is contained in:
David Fowler 2018-03-31 07:29:35 -07:00 committed by GitHub
parent 2c2a8dae01
commit 7382198356
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 5 deletions

View File

@ -24,7 +24,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core
private FileHandleType _handleType;
private HttpProtocols _protocols = HttpProtocols.Http1;
internal bool _isHttp2Supported;
private readonly List<Func<ConnectionDelegate, ConnectionDelegate>> _components = new List<Func<ConnectionDelegate, ConnectionDelegate>>();
internal readonly List<Func<ConnectionDelegate, ConnectionDelegate>> _middleware = new List<Func<ConnectionDelegate, ConnectionDelegate>>();
internal ListenOptions(IPEndPoint endPoint)
{
@ -181,7 +181,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core
public IConnectionBuilder Use(Func<ConnectionDelegate, ConnectionDelegate> middleware)
{
_components.Add(middleware);
_middleware.Add(middleware);
return this;
}
@ -192,9 +192,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core
return Task.CompletedTask;
};
for (int i = _components.Count - 1; i >= 0; i--)
for (int i = _middleware.Count - 1; i >= 0; i--)
{
var component = _components[i];
var component = _middleware[i];
app = component(app);
}

View File

@ -72,7 +72,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core
}
// used for cloning to two IPEndpoints
private ListenOptions Clone(IPAddress address)
internal ListenOptions Clone(IPAddress address)
{
var options = new ListenOptions(new IPEndPoint(address, IPEndPoint.Port))
{
@ -81,6 +81,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core
NoDelay = NoDelay,
Protocols = Protocols,
};
options._middleware.AddRange(_middleware);
options.ConnectionAdapters.AddRange(ConnectionAdapters);
return options;
}

View File

@ -3,6 +3,10 @@
using System;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Kestrel.Core.Adapter.Internal;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
@ -25,5 +29,35 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
ex = Assert.Throws<NotSupportedException>(() => listenOptions.Protocols = HttpProtocols.Http2);
Assert.Equal(CoreStrings.Http2NotSupported, ex.Message);
}
[Fact]
public void LocalHostListenOptionsClonesConnectionMiddleware()
{
var localhostListenOptions = new LocalhostListenOptions(1004);
localhostListenOptions.ConnectionAdapters.Add(new PassThroughConnectionAdapter());
var serviceProvider = new ServiceCollection().BuildServiceProvider();
localhostListenOptions.KestrelServerOptions = new KestrelServerOptions()
{
ApplicationServices = serviceProvider
};
var middlewareRan = false;
localhostListenOptions.Use(next =>
{
middlewareRan = true;
return context => Task.CompletedTask;
});
var clone = localhostListenOptions.Clone(IPAddress.IPv6Loopback);
var app = clone.Build();
// Execute the delegate
app(null);
Assert.True(middlewareRan);
Assert.NotNull(clone.KestrelServerOptions);
Assert.NotNull(serviceProvider);
Assert.Same(serviceProvider, clone.ApplicationServices);
Assert.Single(clone.ConnectionAdapters);
}
}
}