From eab9005e6b09c85cfbe6f6bd2e7829fa119370f0 Mon Sep 17 00:00:00 2001 From: Youssef Victor <31348972+Youssef1313@users.noreply.github.com> Date: Fri, 29 May 2020 23:36:35 +0200 Subject: [PATCH] Make H3StaticTable static class (#21705) * Update H3StaticTable.cs * Update QPackEncoder.cs * Update Http3TestBase.cs * Update EncoderStreamReader.cs * Update QPackDecoder.cs * Update Http3Stream.cs * Use s_ prefix for static fields --- .../Core/src/Internal/Http3/Http3Stream.cs | 4 +-- .../Http3/QPack/EncoderStreamReader.cs | 2 +- .../Http3/Http3TestBase.cs | 4 +-- .../runtime/Http3/QPack/H3StaticTable.cs | 25 +++++++------------ .../runtime/Http3/QPack/QPackDecoder.cs | 2 +- .../runtime/Http3/QPack/QPackEncoder.cs | 2 +- 6 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs b/src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs index 576443150c..292d2c499d 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs @@ -116,13 +116,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http3 public void OnStaticIndexedHeader(int index) { - var knownHeader = H3StaticTable.Instance[index]; + var knownHeader = H3StaticTable.GetHeaderFieldAt(index); OnHeader(knownHeader.Name, knownHeader.Value); } public void OnStaticIndexedHeader(int index, ReadOnlySpan value) { - var knownHeader = H3StaticTable.Instance[index]; + var knownHeader = H3StaticTable.GetHeaderFieldAt(index); OnHeader(knownHeader.Name, value); } diff --git a/src/Servers/Kestrel/Core/src/Internal/Http3/QPack/EncoderStreamReader.cs b/src/Servers/Kestrel/Core/src/Internal/Http3/QPack/EncoderStreamReader.cs index 54ebad55db..b235415902 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http3/QPack/EncoderStreamReader.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http3/QPack/EncoderStreamReader.cs @@ -322,7 +322,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http3.QPack { try { - return _s ? H3StaticTable.Instance[index] : _dynamicTable[index]; + return _s ? H3StaticTable.GetHeaderFieldAt(index) : _dynamicTable[index]; } catch (IndexOutOfRangeException ex) { diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/Http3/Http3TestBase.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/Http3/Http3TestBase.cs index f767fc0871..1419c9cc8e 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/Http3/Http3TestBase.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/Http3/Http3TestBase.cs @@ -355,13 +355,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests public void OnStaticIndexedHeader(int index) { - var knownHeader = H3StaticTable.Instance[index]; + var knownHeader = H3StaticTable.GetHeaderFieldAt(index); _decodedHeaders[((Span)knownHeader.Name).GetAsciiStringNonNullCharacters()] = HttpUtilities.GetAsciiOrUTF8StringNonNullCharacters(knownHeader.Value); } public void OnStaticIndexedHeader(int index, ReadOnlySpan value) { - _decodedHeaders[((Span)H3StaticTable.Instance[index].Name).GetAsciiStringNonNullCharacters()] = value.GetAsciiOrUTF8StringNonNullCharacters(); + _decodedHeaders[((Span)H3StaticTable.GetHeaderFieldAt(index).Name).GetAsciiStringNonNullCharacters()] = value.GetAsciiOrUTF8StringNonNullCharacters(); } internal async Task WaitForStreamErrorAsync(Http3ErrorCode protocolError, string expectedErrorMessage) diff --git a/src/Shared/runtime/Http3/QPack/H3StaticTable.cs b/src/Shared/runtime/Http3/QPack/H3StaticTable.cs index 13fc509cc6..c4ca224742 100644 --- a/src/Shared/runtime/Http3/QPack/H3StaticTable.cs +++ b/src/Shared/runtime/Http3/QPack/H3StaticTable.cs @@ -7,10 +7,9 @@ using System.Text; namespace System.Net.Http.QPack { - // TODO: make class static. - internal class H3StaticTable + internal static class H3StaticTable { - private readonly Dictionary _statusIndex = new Dictionary + private static readonly Dictionary s_statusIndex = new Dictionary { [103] = 24, [200] = 25, @@ -28,7 +27,7 @@ namespace System.Net.Http.QPack [500] = 71, }; - private readonly Dictionary _methodIndex = new Dictionary + private static readonly Dictionary s_methodIndex = new Dictionary { // TODO connect is internal to system.net.http [HttpMethod.Delete] = 16, @@ -39,21 +38,15 @@ namespace System.Net.Http.QPack [HttpMethod.Put] = 21, }; - private H3StaticTable() - { - } - - public static H3StaticTable Instance { get; } = new H3StaticTable(); - - public int Count => _staticTable.Length; - - public HeaderField this[int index] => _staticTable[index]; + public static int Count => s_staticTable.Length; // TODO: just use Dictionary directly to avoid interface dispatch. - public IReadOnlyDictionary StatusIndex => _statusIndex; - public IReadOnlyDictionary MethodIndex => _methodIndex; + public static IReadOnlyDictionary StatusIndex => s_statusIndex; + public static IReadOnlyDictionary MethodIndex => s_methodIndex; - private readonly HeaderField[] _staticTable = new HeaderField[] + public static HeaderField GetHeaderFieldAt(int index) => s_staticTable[index]; + + private static readonly HeaderField[] s_staticTable = new HeaderField[] { CreateHeaderField(":authority", ""), // 0 CreateHeaderField(":path", "/"), // 1 diff --git a/src/Shared/runtime/Http3/QPack/QPackDecoder.cs b/src/Shared/runtime/Http3/QPack/QPackDecoder.cs index e3b85872ed..0ac27cda1c 100644 --- a/src/Shared/runtime/Http3/QPack/QPackDecoder.cs +++ b/src/Shared/runtime/Http3/QPack/QPackDecoder.cs @@ -413,7 +413,7 @@ namespace System.Net.Http.QPack if (_index is int index) { - Debug.Assert(index >= 0 && index <= H3StaticTable.Instance.Count, $"The index should be a valid static index here. {nameof(QPackDecoder)} should have previously thrown if it read a dynamic index."); + Debug.Assert(index >= 0 && index <= H3StaticTable.Count, $"The index should be a valid static index here. {nameof(QPackDecoder)} should have previously thrown if it read a dynamic index."); handler.OnStaticIndexedHeader(index, headerValueSpan); _index = null; diff --git a/src/Shared/runtime/Http3/QPack/QPackEncoder.cs b/src/Shared/runtime/Http3/QPack/QPackEncoder.cs index c956095910..49cfd84633 100644 --- a/src/Shared/runtime/Http3/QPack/QPackEncoder.cs +++ b/src/Shared/runtime/Http3/QPack/QPackEncoder.cs @@ -416,7 +416,7 @@ namespace System.Net.Http.QPack case 404: case 500: // TODO this isn't safe, some index can be larger than 64. Encoded here! - buffer[0] = (byte)(0xC0 | H3StaticTable.Instance.StatusIndex[statusCode]); + buffer[0] = (byte)(0xC0 | H3StaticTable.StatusIndex[statusCode]); return 1; default: // Send as Literal Header Field Without Indexing - Indexed Name