Skip to content

Commit

Permalink
Make H3StaticTable static class (#21705)
Browse files Browse the repository at this point in the history
* Update H3StaticTable.cs

* Update QPackEncoder.cs

* Update Http3TestBase.cs

* Update EncoderStreamReader.cs

* Update QPackDecoder.cs

* Update Http3Stream.cs

* Use s_ prefix for static fields
  • Loading branch information
Youssef1313 authored May 29, 2020
1 parent bf099ab commit eab9005
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ public void OnHeadersComplete(bool endStream)

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<byte> value)
{
var knownHeader = H3StaticTable.Instance[index];
var knownHeader = H3StaticTable.GetHeaderFieldAt(index);
OnHeader(knownHeader.Name, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ private System.Net.Http.QPack.HeaderField GetHeader(int index)
{
try
{
return _s ? H3StaticTable.Instance[index] : _dynamicTable[index];
return _s ? H3StaticTable.GetHeaderFieldAt(index) : _dynamicTable[index];
}
catch (IndexOutOfRangeException ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,13 @@ public void OnHeadersComplete(bool endHeaders)

public void OnStaticIndexedHeader(int index)
{
var knownHeader = H3StaticTable.Instance[index];
var knownHeader = H3StaticTable.GetHeaderFieldAt(index);
_decodedHeaders[((Span<byte>)knownHeader.Name).GetAsciiStringNonNullCharacters()] = HttpUtilities.GetAsciiOrUTF8StringNonNullCharacters(knownHeader.Value);
}

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

internal async Task WaitForStreamErrorAsync(Http3ErrorCode protocolError, string expectedErrorMessage)
Expand Down
25 changes: 9 additions & 16 deletions src/Shared/runtime/Http3/QPack/H3StaticTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

namespace System.Net.Http.QPack
{
// TODO: make class static.
internal class H3StaticTable
internal static class H3StaticTable
{
private readonly Dictionary<int, int> _statusIndex = new Dictionary<int, int>
private static readonly Dictionary<int, int> s_statusIndex = new Dictionary<int, int>
{
[103] = 24,
[200] = 25,
Expand All @@ -28,7 +27,7 @@ internal class H3StaticTable
[500] = 71,
};

private readonly Dictionary<HttpMethod, int> _methodIndex = new Dictionary<HttpMethod, int>
private static readonly Dictionary<HttpMethod, int> s_methodIndex = new Dictionary<HttpMethod, int>
{
// TODO connect is internal to system.net.http
[HttpMethod.Delete] = 16,
Expand All @@ -39,21 +38,15 @@ internal class H3StaticTable
[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<int, int> StatusIndex => _statusIndex;
public IReadOnlyDictionary<HttpMethod, int> MethodIndex => _methodIndex;
public static IReadOnlyDictionary<int, int> StatusIndex => s_statusIndex;
public static IReadOnlyDictionary<HttpMethod, int> MethodIndex => s_methodIndex;

public static HeaderField GetHeaderFieldAt(int index) => s_staticTable[index];

private readonly HeaderField[] _staticTable = new HeaderField[]
private static readonly HeaderField[] s_staticTable = new HeaderField[]
{
CreateHeaderField(":authority", ""), // 0
CreateHeaderField(":path", "/"), // 1
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/runtime/Http3/QPack/QPackDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ private void ProcessHeaderValue(IHttpHeadersHandler handler)

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;

Expand Down
2 changes: 1 addition & 1 deletion src/Shared/runtime/Http3/QPack/QPackEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ private int EncodeStatusCode(int statusCode, Span<byte> buffer)
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
Expand Down

0 comments on commit eab9005

Please sign in to comment.