From 45a35f9197777b954193542b4b22d908827efb51 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sat, 23 Mar 2024 10:31:19 +0100 Subject: [PATCH 1/2] Move a few allocations behind IsHttp3Supported checks --- .../SocketsHttpHandler/HttpConnectionPool.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs index 552661e68bed5c..79309f26050188 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs @@ -229,6 +229,8 @@ public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionK break; } + Debug.Assert(!_http3Enabled || _http2Enabled, "We shouldn't support H3 while disabling H2."); + if (!_http3Enabled) { // Avoid parsing Alt-Svc headers if they won't be used. @@ -254,10 +256,14 @@ public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionK Debug.Assert(Encoding.ASCII.GetString(_hostHeaderLineBytes) == $"Host: {hostHeader}\r\n"); - if (sslHostName == null) + if (sslHostName == null && _http2Enabled) { _http2EncodedAuthorityHostHeader = HPackEncoder.EncodeLiteralHeaderFieldWithoutIndexingToAllocatedArray(H2StaticTable.Authority, hostHeader); - _http3EncodedAuthorityHostHeader = QPackEncoder.EncodeLiteralHeaderFieldWithStaticNameReferenceToArray(H3StaticTable.Authority, hostHeader); + + if (IsHttp3Supported() && _http3Enabled) + { + _http3EncodedAuthorityHostHeader = QPackEncoder.EncodeLiteralHeaderFieldWithStaticNameReferenceToArray(H3StaticTable.Authority, hostHeader); + } } } @@ -289,7 +295,11 @@ public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionK Debug.Assert(hostHeader != null); _http2EncodedAuthorityHostHeader = HPackEncoder.EncodeLiteralHeaderFieldWithoutIndexingToAllocatedArray(H2StaticTable.Authority, hostHeader); - _http3EncodedAuthorityHostHeader = QPackEncoder.EncodeLiteralHeaderFieldWithStaticNameReferenceToArray(H3StaticTable.Authority, hostHeader); + + if (IsHttp3Supported() && _http3Enabled) + { + _http3EncodedAuthorityHostHeader = QPackEncoder.EncodeLiteralHeaderFieldWithStaticNameReferenceToArray(H3StaticTable.Authority, hostHeader); + } } } From e7d8caad6bc965d2376c140f8567360d23355d32 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sat, 23 Mar 2024 15:31:12 +0100 Subject: [PATCH 2/2] Dedup the logic --- .../SocketsHttpHandler/HttpConnectionPool.cs | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs index 79309f26050188..e51669888352bf 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs @@ -229,8 +229,6 @@ public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionK break; } - Debug.Assert(!_http3Enabled || _http2Enabled, "We shouldn't support H3 while disabling H2."); - if (!_http3Enabled) { // Avoid parsing Alt-Svc headers if they won't be used. @@ -255,16 +253,6 @@ public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionK _hostHeaderLineBytes = hostHeaderLine; Debug.Assert(Encoding.ASCII.GetString(_hostHeaderLineBytes) == $"Host: {hostHeader}\r\n"); - - if (sslHostName == null && _http2Enabled) - { - _http2EncodedAuthorityHostHeader = HPackEncoder.EncodeLiteralHeaderFieldWithoutIndexingToAllocatedArray(H2StaticTable.Authority, hostHeader); - - if (IsHttp3Supported() && _http3Enabled) - { - _http3EncodedAuthorityHostHeader = QPackEncoder.EncodeLiteralHeaderFieldWithStaticNameReferenceToArray(H3StaticTable.Authority, hostHeader); - } - } } if (sslHostName != null) @@ -292,14 +280,19 @@ public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionK // by which AllowRenegotiation could be set back to true in that case. // For now, if an HTTP/2 server erroneously issues a renegotiation, we'll // allow it. + } + } - Debug.Assert(hostHeader != null); + if (hostHeader is not null) + { + if (_http2Enabled) + { _http2EncodedAuthorityHostHeader = HPackEncoder.EncodeLiteralHeaderFieldWithoutIndexingToAllocatedArray(H2StaticTable.Authority, hostHeader); + } - if (IsHttp3Supported() && _http3Enabled) - { - _http3EncodedAuthorityHostHeader = QPackEncoder.EncodeLiteralHeaderFieldWithStaticNameReferenceToArray(H3StaticTable.Authority, hostHeader); - } + if (IsHttp3Supported() && _http3Enabled) + { + _http3EncodedAuthorityHostHeader = QPackEncoder.EncodeLiteralHeaderFieldWithStaticNameReferenceToArray(H3StaticTable.Authority, hostHeader); } }