Skip to content

Commit 6e10637

Browse files
committed
Playing around
1 parent 7843321 commit 6e10637

9 files changed

+31
-38
lines changed

src/Servers/Kestrel/Core/src/HttpsConfigurationService.cs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
46
using System.IO.Pipelines;
57
using System.Net;
68
using System.Net.Security;
@@ -10,8 +12,6 @@
1012
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
1113
using Microsoft.AspNetCore.Server.Kestrel.Https;
1214
using Microsoft.AspNetCore.Server.Kestrel.Https.Internal;
13-
using Microsoft.Extensions.Hosting;
14-
using Microsoft.Extensions.Logging;
1515

1616
namespace Microsoft.AspNetCore.Server.Kestrel.Core;
1717

@@ -50,10 +50,7 @@ public HttpsConfigurationService(IInitializer initializer)
5050
bool IHttpsConfigurationService.IsInitialized => _isInitialized || _initializer is not null;
5151

5252
/// <inheritdoc/>
53-
public void Initialize(
54-
IHostEnvironment hostEnvironment,
55-
ILogger<KestrelServer> serverLogger,
56-
ILogger<HttpsConnectionMiddleware> httpsLogger)
53+
public void Initialize(TlsConfigurationLoader tlsConfigurationLoader)
5754
{
5855
if (_isInitialized)
5956
{
@@ -62,7 +59,7 @@ public void Initialize(
6259

6360
_isInitialized = true;
6461

65-
_tlsConfigurationLoader = new TlsConfigurationLoader(hostEnvironment, serverLogger, httpsLogger);
62+
_tlsConfigurationLoader = tlsConfigurationLoader;
6663
_populateMultiplexedTransportFeatures = PopulateMultiplexedTransportFeaturesWorker;
6764
_useHttpsWithDefaults = UseHttpsWithDefaultsWorker;
6865
}
@@ -76,41 +73,42 @@ public void ApplyHttpsConfiguration(
7673
ConfigurationReader configurationReader)
7774
{
7875
EnsureInitialized();
79-
_tlsConfigurationLoader!.ApplyHttpsConfiguration(httpsOptions, endpoint, serverOptions, defaultCertificateConfig, configurationReader);
76+
_tlsConfigurationLoader.ApplyHttpsConfiguration(httpsOptions, endpoint, serverOptions, defaultCertificateConfig, configurationReader);
8077
}
8178

8279
/// <inheritdoc/>
8380
public ListenOptions UseHttpsWithSni(ListenOptions listenOptions, HttpsConnectionAdapterOptions httpsOptions, EndpointConfig endpoint)
8481
{
8582
EnsureInitialized();
86-
return _tlsConfigurationLoader!.UseHttpsWithSni(listenOptions, httpsOptions, endpoint);
83+
return _tlsConfigurationLoader.UseHttpsWithSni(listenOptions, httpsOptions, endpoint);
8784
}
8885

8986
/// <inheritdoc/>
9087
public CertificateAndConfig? LoadDefaultCertificate(ConfigurationReader configurationReader)
9188
{
9289
EnsureInitialized();
93-
return _tlsConfigurationLoader!.LoadDefaultCertificate(configurationReader);
90+
return _tlsConfigurationLoader.LoadDefaultCertificate(configurationReader);
9491
}
9592

9693
/// <inheritdoc/>
9794
public void PopulateMultiplexedTransportFeatures(FeatureCollection features, ListenOptions listenOptions)
9895
{
9996
EnsureInitialized();
100-
_populateMultiplexedTransportFeatures!.Invoke(features, listenOptions);
97+
_populateMultiplexedTransportFeatures.Invoke(features, listenOptions);
10198
}
10299

103100
/// <inheritdoc/>
104101
public ListenOptions UseHttpsWithDefaults(ListenOptions listenOptions)
105102
{
106103
EnsureInitialized();
107-
return _useHttpsWithDefaults!.Invoke(listenOptions);
104+
return _useHttpsWithDefaults.Invoke(listenOptions);
108105
}
109106

110107
/// <summary>
111108
/// If this instance has not been initialized, initialize it if possible and throw otherwise.
112109
/// </summary>
113110
/// <exception cref="InvalidOperationException">If initialization is not possible.</exception>
111+
[MemberNotNull(nameof(_useHttpsWithDefaults), nameof(_tlsConfigurationLoader), nameof(_populateMultiplexedTransportFeatures))]
114112
private void EnsureInitialized()
115113
{
116114
if (!_isInitialized)
@@ -124,6 +122,10 @@ private void EnsureInitialized()
124122
throw new InvalidOperationException(CoreStrings.NeedHttpsConfiguration);
125123
}
126124
}
125+
126+
Debug.Assert(_useHttpsWithDefaults != null);
127+
Debug.Assert(_tlsConfigurationLoader != null);
128+
Debug.Assert(_populateMultiplexedTransportFeatures != null);
127129
}
128130

129131
/// <summary>
@@ -228,24 +230,17 @@ internal interface IInitializer
228230
/// <inheritdoc/>
229231
internal sealed class Initializer : IInitializer
230232
{
231-
private readonly IHostEnvironment _hostEnvironment;
232-
private readonly ILogger<KestrelServer> _serverLogger;
233-
private readonly ILogger<HttpsConnectionMiddleware> _httpsLogger;
234-
235-
public Initializer(
236-
IHostEnvironment hostEnvironment,
237-
ILogger<KestrelServer> serverLogger,
238-
ILogger<HttpsConnectionMiddleware> httpsLogger)
233+
private readonly TlsConfigurationLoader _configurationLoader;
234+
235+
public Initializer(TlsConfigurationLoader configurationLoader)
239236
{
240-
_hostEnvironment = hostEnvironment;
241-
_serverLogger = serverLogger;
242-
_httpsLogger = httpsLogger;
237+
_configurationLoader = configurationLoader;
243238
}
244239

245240
/// <inheritdoc/>
246241
public void Initialize(IHttpsConfigurationService httpsConfigurationService)
247242
{
248-
httpsConfigurationService.Initialize(_hostEnvironment, _serverLogger, _httpsLogger);
243+
httpsConfigurationService.Initialize(_configurationLoader);
249244
}
250245
}
251246
}

src/Servers/Kestrel/Core/src/IHttpsConfigurationService.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
using Microsoft.AspNetCore.Http.Features;
66
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
77
using Microsoft.AspNetCore.Server.Kestrel.Https;
8-
using Microsoft.AspNetCore.Server.Kestrel.Https.Internal;
9-
using Microsoft.Extensions.Hosting;
10-
using Microsoft.Extensions.Logging;
118

129
namespace Microsoft.AspNetCore.Server.Kestrel.Core;
1310

@@ -28,10 +25,7 @@ internal interface IHttpsConfigurationService
2825
/// <summary>
2926
/// Replaces the implementations off all other methods with functioning (as opposed to throwing) versions.
3027
/// </summary>
31-
void Initialize(
32-
IHostEnvironment hostEnvironment,
33-
ILogger<KestrelServer> serverLogger,
34-
ILogger<HttpsConnectionMiddleware> httpsLogger);
28+
void Initialize(TlsConfigurationLoader tlsConfigurationLoader);
3529

3630
/// <summary>
3731
/// Applies various configuration settings to <paramref name="httpsOptions"/> and <paramref name="endpoint"/>.

src/Servers/Kestrel/Core/src/KestrelServer.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
99
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
1010
using Microsoft.AspNetCore.Server.Kestrel.Https;
11-
using Microsoft.AspNetCore.Server.Kestrel.Https.Internal;
12-
using Microsoft.Extensions.Hosting;
1311
using Microsoft.Extensions.Logging;
1412
using Microsoft.Extensions.Metrics;
1513
using Microsoft.Extensions.Options;
@@ -80,7 +78,7 @@ private sealed class SimpleHttpsConfigurationService : IHttpsConfigurationServic
8078
{
8179
public bool IsInitialized => true;
8280

83-
public void Initialize(IHostEnvironment hostEnvironment, ILogger<KestrelServer> serverLogger, ILogger<HttpsConnectionMiddleware> httpsLogger)
81+
public void Initialize(TlsConfigurationLoader tlsConfigurationLoader)
8482
{
8583
// Already initialized
8684
}

src/Servers/Kestrel/Core/src/KestrelServerOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ internal void EnableHttpsConfiguration()
291291
var hostEnvironment = ApplicationServices.GetRequiredService<IHostEnvironment>();
292292
var logger = ApplicationServices.GetRequiredService<ILogger<KestrelServer>>();
293293
var httpsLogger = ApplicationServices.GetRequiredService<ILogger<HttpsConnectionMiddleware>>();
294-
httpsConfigurationService.Initialize(hostEnvironment, logger, httpsLogger);
294+
httpsConfigurationService.Initialize(new TlsConfigurationLoader(hostEnvironment, logger, httpsLogger));
295295
}
296296
}
297297

src/Servers/Kestrel/Core/test/KestrelServerTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,17 +296,19 @@ private static KestrelServerImpl CreateKestrelServer(
296296
var httpsConfigurationService = new HttpsConfigurationService();
297297
if (options?.ApplicationServices is IServiceProvider serviceProvider)
298298
{
299-
httpsConfigurationService.Initialize(
299+
var tlsConfigurationLoader = new TlsConfigurationLoader(
300300
serviceProvider.GetRequiredService<IHostEnvironment>(),
301301
serviceProvider.GetRequiredService<ILogger<KestrelServer>>(),
302302
serviceProvider.GetRequiredService<ILogger<HttpsConnectionMiddleware>>());
303+
304+
httpsConfigurationService.Initialize(tlsConfigurationLoader);
303305
}
304-
306+
305307
return new KestrelServerImpl(
306308
Options.Create<KestrelServerOptions>(options),
307309
transportFactories,
308310
multiplexedFactories,
309-
httpsConfigurationService,
311+
httpsConfigurationService,
310312
loggerFactory ?? new LoggerFactory(new[] { new KestrelTestLoggerProvider() }),
311313
metrics ?? new KestrelMetrics(new TestMeterFactory()));
312314
}

src/Servers/Kestrel/Kestrel/src/WebHostBuilderKestrelExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static IWebHostBuilder UseHttpsConfiguration(this IWebHostBuilder hostBui
3838
return hostBuilder.ConfigureServices(services =>
3939
{
4040
services.AddSingleton<HttpsConfigurationService.IInitializer, HttpsConfigurationService.Initializer>();
41+
services.AddSingleton<TlsConfigurationLoader>();
4142
});
4243
}
4344

src/Servers/Kestrel/Kestrel/test/KestrelConfigurationLoaderTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ private KestrelServerOptions CreateServerOptions()
2828
.AddSingleton<IHostEnvironment>(env)
2929
.AddSingleton(new KestrelMetrics(new TestMeterFactory()))
3030
.AddSingleton<IHttpsConfigurationService, HttpsConfigurationService>()
31+
.AddSingleton<TlsConfigurationLoader>()
3132
.AddSingleton<HttpsConfigurationService.IInitializer, HttpsConfigurationService.Initializer>()
3233
.BuildServiceProvider();
3334
return serverOptions;

src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpsConnectionMiddlewareTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ private static KestrelServerOptions CreateServerOptions()
4242
serverOptions.ApplicationServices = new ServiceCollection()
4343
.AddLogging()
4444
.AddSingleton<IHttpsConfigurationService, HttpsConfigurationService>()
45+
.AddSingleton<TlsConfigurationLoader>()
4546
.AddSingleton<HttpsConfigurationService.IInitializer, HttpsConfigurationService.Initializer>()
4647
.AddSingleton(env.Object)
4748
.AddSingleton(new KestrelMetrics(new TestMeterFactory()))

src/Servers/Kestrel/test/InMemory.FunctionalTests/TestTransport/TestServer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public TestServer(RequestDelegate app, TestServiceContext context, Action<Kestre
8888
services.AddSingleton(context.LoggerFactory);
8989
services.AddSingleton(context.Metrics);
9090
services.AddSingleton<IHttpsConfigurationService, HttpsConfigurationService>();
91+
services.AddSingleton<TlsConfigurationLoader>();
9192
services.AddSingleton<HttpsConfigurationService.IInitializer, HttpsConfigurationService.Initializer>();
9293

9394
services.AddSingleton<IServer>(sp =>

0 commit comments

Comments
 (0)