@@ -25,12 +25,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Https.Internal
25
25
{
26
26
internal class HttpsConnectionMiddleware
27
27
{
28
+ private const string EnableWindows81Http2 = "Microsoft.AspNetCore.Server.Kestrel.EnableWindows81Http2" ;
28
29
private readonly ConnectionDelegate _next ;
29
30
private readonly HttpsConnectionAdapterOptions _options ;
30
31
private readonly ILogger _logger ;
31
32
private readonly X509Certificate2 _serverCertificate ;
32
33
private readonly Func < ConnectionContext , string , X509Certificate2 > _serverCertificateSelector ;
33
- private const string EnableWindows81Http2 = "Microsoft.AspNetCore.Server.Kestrel.EnableWindows81Http2" ;
34
34
35
35
public HttpsConnectionMiddleware ( ConnectionDelegate next , HttpsConnectionAdapterOptions options )
36
36
: this ( next , options , loggerFactory : NullLoggerFactory . Instance )
@@ -52,31 +52,17 @@ public HttpsConnectionMiddleware(ConnectionDelegate next, HttpsConnectionAdapter
52
52
{
53
53
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
54
54
{
55
- throw new NotSupportedException ( CoreStrings . HTTP2NoTlsOsx ) ;
55
+ throw new NotSupportedException ( CoreStrings . Http2NoTlsOsx ) ;
56
56
}
57
- else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
57
+ else if ( IsWindowsVersionIncompatible ( ) )
58
58
{
59
- var enableHttp2OnWindows81 = AppContext . TryGetSwitch ( EnableWindows81Http2 , out var enabled ) && enabled ;
60
- if ( Environment . OSVersion . Version < new Version ( 6 , 3 )
61
- || ( Environment . OSVersion . Version < new Version ( 10 , 0 ) && ! enableHttp2OnWindows81 ) )
62
- {
63
- throw new NotSupportedException ( CoreStrings . HTTP2NoTlsWin81 ) ;
64
- }
59
+ throw new NotSupportedException ( CoreStrings . Http2NoTlsWin81 ) ;
65
60
}
66
61
}
67
-
68
- if ( options . HttpProtocols == HttpProtocols . Http1AndHttp2 )
62
+ else if ( options . HttpProtocols == HttpProtocols . Http1AndHttp2 && IsWindowsVersionIncompatible ( ) )
69
63
{
70
- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
71
- {
72
- var enableHttp2OnWindows81 = AppContext . TryGetSwitch ( EnableWindows81Http2 , out var enabled ) && enabled ;
73
- if ( Environment . OSVersion . Version < new Version ( 6 , 3 )
74
- || ( Environment . OSVersion . Version < new Version ( 10 , 0 ) && ! enableHttp2OnWindows81 ) )
75
- {
76
- _logger . HTTP2DefaultCiphersInsufficient ( ) ;
77
- options . HttpProtocols = HttpProtocols . Http1 ;
78
- }
79
- }
64
+ _logger . Http2DefaultCiphersInsufficient ( ) ;
65
+ options . HttpProtocols = HttpProtocols . Http1 ;
80
66
}
81
67
82
68
_next = next ;
@@ -318,6 +304,21 @@ private static X509Certificate2 ConvertToX509Certificate2(X509Certificate certif
318
304
319
305
return new X509Certificate2 ( certificate ) ;
320
306
}
307
+
308
+ private static bool IsWindowsVersionIncompatible ( )
309
+ {
310
+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
311
+ {
312
+ var enableHttp2OnWindows81 = AppContext . TryGetSwitch ( EnableWindows81Http2 , out var enabled ) && enabled ;
313
+ if ( Environment . OSVersion . Version < new Version ( 6 , 3 )
314
+ || ( Environment . OSVersion . Version < new Version ( 10 , 0 ) && ! enableHttp2OnWindows81 ) )
315
+ {
316
+ return true ;
317
+ }
318
+ }
319
+
320
+ return false ;
321
+ }
321
322
}
322
323
323
324
internal static class HttpsConnectionMiddlewareLoggerExtensions
@@ -344,15 +345,15 @@ internal static class HttpsConnectionMiddlewareLoggerExtensions
344
345
private static readonly Action < ILogger , Exception > _http2DefaultCiphersInsufficient =
345
346
LoggerMessage . Define (
346
347
logLevel : LogLevel . Information ,
347
- eventId : new EventId ( 4 , "HTTP2DefaultCiphersInsufficient " ) ,
348
- formatString : CoreStrings . HTTP2DefaultCiphersInsufficient ) ;
348
+ eventId : new EventId ( 4 , "Http2DefaultCiphersInsufficient " ) ,
349
+ formatString : CoreStrings . Http2DefaultCiphersInsufficient ) ;
349
350
350
351
public static void AuthenticationFailed ( this ILogger logger , Exception exception ) => _authenticationFailed ( logger , exception ) ;
351
352
352
353
public static void AuthenticationTimedOut ( this ILogger logger ) => _authenticationTimedOut ( logger , null ) ;
353
354
354
355
public static void HttpsConnectionEstablished ( this ILogger logger , string connectionId , SslProtocols sslProtocol ) => _httpsConnectionEstablished ( logger , connectionId , sslProtocol , null ) ;
355
356
356
- public static void HTTP2DefaultCiphersInsufficient ( this ILogger logger ) => _http2DefaultCiphersInsufficient ( logger , null ) ;
357
+ public static void Http2DefaultCiphersInsufficient ( this ILogger logger ) => _http2DefaultCiphersInsufficient ( logger , null ) ;
357
358
}
358
359
}
0 commit comments