@@ -25,12 +25,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Https.Internal
2525{
2626 internal class HttpsConnectionMiddleware
2727 {
28+ private const string EnableWindows81Http2 = "Microsoft.AspNetCore.Server.Kestrel.EnableWindows81Http2" ;
2829 private readonly ConnectionDelegate _next ;
2930 private readonly HttpsConnectionAdapterOptions _options ;
3031 private readonly ILogger _logger ;
3132 private readonly X509Certificate2 _serverCertificate ;
3233 private readonly Func < ConnectionContext , string , X509Certificate2 > _serverCertificateSelector ;
33- private const string EnableWindows81Http2 = "Microsoft.AspNetCore.Server.Kestrel.EnableWindows81Http2" ;
3434
3535 public HttpsConnectionMiddleware ( ConnectionDelegate next , HttpsConnectionAdapterOptions options )
3636 : this ( next , options , loggerFactory : NullLoggerFactory . Instance )
@@ -52,31 +52,17 @@ public HttpsConnectionMiddleware(ConnectionDelegate next, HttpsConnectionAdapter
5252 {
5353 if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
5454 {
55- throw new NotSupportedException ( CoreStrings . HTTP2NoTlsOsx ) ;
55+ throw new NotSupportedException ( CoreStrings . Http2NoTlsOsx ) ;
5656 }
57- else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
57+ else if ( IsWindowsVersionIncompatible ( ) )
5858 {
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 ) ;
6560 }
6661 }
67-
68- if ( options . HttpProtocols == HttpProtocols . Http1AndHttp2 )
62+ else if ( options . HttpProtocols == HttpProtocols . Http1AndHttp2 && IsWindowsVersionIncompatible ( ) )
6963 {
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 ;
8066 }
8167
8268 _next = next ;
@@ -318,6 +304,21 @@ private static X509Certificate2 ConvertToX509Certificate2(X509Certificate certif
318304
319305 return new X509Certificate2 ( certificate ) ;
320306 }
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+ }
321322 }
322323
323324 internal static class HttpsConnectionMiddlewareLoggerExtensions
@@ -344,15 +345,15 @@ internal static class HttpsConnectionMiddlewareLoggerExtensions
344345 private static readonly Action < ILogger , Exception > _http2DefaultCiphersInsufficient =
345346 LoggerMessage . Define (
346347 logLevel : LogLevel . Information ,
347- eventId : new EventId ( 4 , "HTTP2DefaultCiphersInsufficient " ) ,
348- formatString : CoreStrings . HTTP2DefaultCiphersInsufficient ) ;
348+ eventId : new EventId ( 4 , "Http2DefaultCiphersInsufficient " ) ,
349+ formatString : CoreStrings . Http2DefaultCiphersInsufficient ) ;
349350
350351 public static void AuthenticationFailed ( this ILogger logger , Exception exception ) => _authenticationFailed ( logger , exception ) ;
351352
352353 public static void AuthenticationTimedOut ( this ILogger logger ) => _authenticationTimedOut ( logger , null ) ;
353354
354355 public static void HttpsConnectionEstablished ( this ILogger logger , string connectionId , SslProtocols sslProtocol ) => _httpsConnectionEstablished ( logger , connectionId , sslProtocol , null ) ;
355356
356- public static void HTTP2DefaultCiphersInsufficient ( this ILogger logger ) => _http2DefaultCiphersInsufficient ( logger , null ) ;
357+ public static void Http2DefaultCiphersInsufficient ( this ILogger logger ) => _http2DefaultCiphersInsufficient ( logger , null ) ;
357358 }
358359}
0 commit comments