Skip to content

Commit eab9005

Browse files
authored
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
1 parent bf099ab commit eab9005

File tree

6 files changed

+16
-23
lines changed

6 files changed

+16
-23
lines changed

src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ public void OnHeadersComplete(bool endStream)
116116

117117
public void OnStaticIndexedHeader(int index)
118118
{
119-
var knownHeader = H3StaticTable.Instance[index];
119+
var knownHeader = H3StaticTable.GetHeaderFieldAt(index);
120120
OnHeader(knownHeader.Name, knownHeader.Value);
121121
}
122122

123123
public void OnStaticIndexedHeader(int index, ReadOnlySpan<byte> value)
124124
{
125-
var knownHeader = H3StaticTable.Instance[index];
125+
var knownHeader = H3StaticTable.GetHeaderFieldAt(index);
126126
OnHeader(knownHeader.Name, value);
127127
}
128128

src/Servers/Kestrel/Core/src/Internal/Http3/QPack/EncoderStreamReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ private System.Net.Http.QPack.HeaderField GetHeader(int index)
322322
{
323323
try
324324
{
325-
return _s ? H3StaticTable.Instance[index] : _dynamicTable[index];
325+
return _s ? H3StaticTable.GetHeaderFieldAt(index) : _dynamicTable[index];
326326
}
327327
catch (IndexOutOfRangeException ex)
328328
{

src/Servers/Kestrel/test/InMemory.FunctionalTests/Http3/Http3TestBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,13 @@ public void OnHeadersComplete(bool endHeaders)
355355

356356
public void OnStaticIndexedHeader(int index)
357357
{
358-
var knownHeader = H3StaticTable.Instance[index];
358+
var knownHeader = H3StaticTable.GetHeaderFieldAt(index);
359359
_decodedHeaders[((Span<byte>)knownHeader.Name).GetAsciiStringNonNullCharacters()] = HttpUtilities.GetAsciiOrUTF8StringNonNullCharacters(knownHeader.Value);
360360
}
361361

362362
public void OnStaticIndexedHeader(int index, ReadOnlySpan<byte> value)
363363
{
364-
_decodedHeaders[((Span<byte>)H3StaticTable.Instance[index].Name).GetAsciiStringNonNullCharacters()] = value.GetAsciiOrUTF8StringNonNullCharacters();
364+
_decodedHeaders[((Span<byte>)H3StaticTable.GetHeaderFieldAt(index).Name).GetAsciiStringNonNullCharacters()] = value.GetAsciiOrUTF8StringNonNullCharacters();
365365
}
366366

367367
internal async Task WaitForStreamErrorAsync(Http3ErrorCode protocolError, string expectedErrorMessage)

src/Shared/runtime/Http3/QPack/H3StaticTable.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77

88
namespace System.Net.Http.QPack
99
{
10-
// TODO: make class static.
11-
internal class H3StaticTable
10+
internal static class H3StaticTable
1211
{
13-
private readonly Dictionary<int, int> _statusIndex = new Dictionary<int, int>
12+
private static readonly Dictionary<int, int> s_statusIndex = new Dictionary<int, int>
1413
{
1514
[103] = 24,
1615
[200] = 25,
@@ -28,7 +27,7 @@ internal class H3StaticTable
2827
[500] = 71,
2928
};
3029

31-
private readonly Dictionary<HttpMethod, int> _methodIndex = new Dictionary<HttpMethod, int>
30+
private static readonly Dictionary<HttpMethod, int> s_methodIndex = new Dictionary<HttpMethod, int>
3231
{
3332
// TODO connect is internal to system.net.http
3433
[HttpMethod.Delete] = 16,
@@ -39,21 +38,15 @@ internal class H3StaticTable
3938
[HttpMethod.Put] = 21,
4039
};
4140

42-
private H3StaticTable()
43-
{
44-
}
45-
46-
public static H3StaticTable Instance { get; } = new H3StaticTable();
47-
48-
public int Count => _staticTable.Length;
49-
50-
public HeaderField this[int index] => _staticTable[index];
41+
public static int Count => s_staticTable.Length;
5142

5243
// TODO: just use Dictionary directly to avoid interface dispatch.
53-
public IReadOnlyDictionary<int, int> StatusIndex => _statusIndex;
54-
public IReadOnlyDictionary<HttpMethod, int> MethodIndex => _methodIndex;
44+
public static IReadOnlyDictionary<int, int> StatusIndex => s_statusIndex;
45+
public static IReadOnlyDictionary<HttpMethod, int> MethodIndex => s_methodIndex;
46+
47+
public static HeaderField GetHeaderFieldAt(int index) => s_staticTable[index];
5548

56-
private readonly HeaderField[] _staticTable = new HeaderField[]
49+
private static readonly HeaderField[] s_staticTable = new HeaderField[]
5750
{
5851
CreateHeaderField(":authority", ""), // 0
5952
CreateHeaderField(":path", "/"), // 1

src/Shared/runtime/Http3/QPack/QPackDecoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ private void ProcessHeaderValue(IHttpHeadersHandler handler)
413413

414414
if (_index is int index)
415415
{
416-
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.");
416+
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.");
417417
handler.OnStaticIndexedHeader(index, headerValueSpan);
418418
_index = null;
419419

src/Shared/runtime/Http3/QPack/QPackEncoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ private int EncodeStatusCode(int statusCode, Span<byte> buffer)
416416
case 404:
417417
case 500:
418418
// TODO this isn't safe, some index can be larger than 64. Encoded here!
419-
buffer[0] = (byte)(0xC0 | H3StaticTable.Instance.StatusIndex[statusCode]);
419+
buffer[0] = (byte)(0xC0 | H3StaticTable.StatusIndex[statusCode]);
420420
return 1;
421421
default:
422422
// Send as Literal Header Field Without Indexing - Indexed Name

0 commit comments

Comments
 (0)