Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split the HPackEncoder partial class #31210

Merged
merged 1 commit into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal static class HPackHeaderWriter
/// <summary>
/// Begin encoding headers in the first HEADERS frame.
/// </summary>
public static bool BeginEncodeHeaders(int statusCode, HPackEncoder hpackEncoder, Http2HeadersEnumerator headersEnumerator, Span<byte> buffer, out int length)
public static bool BeginEncodeHeaders(int statusCode, DynamicHPackEncoder hpackEncoder, Http2HeadersEnumerator headersEnumerator, Span<byte> buffer, out int length)
{
length = 0;

Expand Down Expand Up @@ -43,7 +43,7 @@ public static bool BeginEncodeHeaders(int statusCode, HPackEncoder hpackEncoder,
/// <summary>
/// Begin encoding headers in the first HEADERS frame.
/// </summary>
public static bool BeginEncodeHeaders(HPackEncoder hpackEncoder, Http2HeadersEnumerator headersEnumerator, Span<byte> buffer, out int length)
public static bool BeginEncodeHeaders(DynamicHPackEncoder hpackEncoder, Http2HeadersEnumerator headersEnumerator, Span<byte> buffer, out int length)
{
length = 0;

Expand All @@ -66,12 +66,12 @@ public static bool BeginEncodeHeaders(HPackEncoder hpackEncoder, Http2HeadersEnu
/// <summary>
/// Continue encoding headers in the next HEADERS frame. The enumerator should already have a current value.
/// </summary>
public static bool ContinueEncodeHeaders(HPackEncoder hpackEncoder, Http2HeadersEnumerator headersEnumerator, Span<byte> buffer, out int length)
public static bool ContinueEncodeHeaders(DynamicHPackEncoder hpackEncoder, Http2HeadersEnumerator headersEnumerator, Span<byte> buffer, out int length)
{
return EncodeHeadersCore(hpackEncoder, headersEnumerator, buffer, throwIfNoneEncoded: true, out length);
}

private static bool EncodeStatusHeader(int statusCode, HPackEncoder hpackEncoder, Span<byte> buffer, out int length)
private static bool EncodeStatusHeader(int statusCode, DynamicHPackEncoder hpackEncoder, Span<byte> buffer, out int length)
{
switch (statusCode)
{
Expand All @@ -91,7 +91,7 @@ private static bool EncodeStatusHeader(int statusCode, HPackEncoder hpackEncoder
}
}

private static bool EncodeHeadersCore(HPackEncoder hpackEncoder, Http2HeadersEnumerator headersEnumerator, Span<byte> buffer, bool throwIfNoneEncoded, out int length)
private static bool EncodeHeadersCore(DynamicHPackEncoder hpackEncoder, Http2HeadersEnumerator headersEnumerator, Span<byte> buffer, bool throwIfNoneEncoded, out int length)
{
var currentLength = 0;
do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal class Http2FrameWriter
private readonly ITimeoutControl _timeoutControl;
private readonly MinDataRate? _minResponseDataRate;
private readonly TimingPipeFlusher _flusher;
private readonly HPackEncoder _hpackEncoder;
private readonly DynamicHPackEncoder _hpackEncoder;

private uint _maxFrameSize = Http2PeerSettings.MinAllowedMaxFrameSize;
private byte[] _headerEncodingBuffer;
Expand Down Expand Up @@ -71,7 +71,7 @@ public Http2FrameWriter(
_outgoingFrame = new Http2Frame();
_headerEncodingBuffer = new byte[_maxFrameSize];

_hpackEncoder = new HPackEncoder(serviceContext.ServerOptions.AllowResponseHeaderCompression);
_hpackEncoder = new DynamicHPackEncoder(serviceContext.ServerOptions.AllowResponseHeaderCompression);
}

public void UpdateMaxHeaderTableSize(uint maxHeaderTableSize)
Expand Down
20 changes: 10 additions & 10 deletions src/Servers/Kestrel/Core/test/Http2HPackEncoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void BeginEncodeHeaders_Status302_NewIndexValue()
var enumerator = new Http2HeadersEnumerator();
enumerator.Initialize(headers);

var hpackEncoder = new HPackEncoder();
var hpackEncoder = new DynamicHPackEncoder();
Assert.True(HPackHeaderWriter.BeginEncodeHeaders(302, hpackEncoder, enumerator, buffer, out var length));

var result = buffer.Slice(0, length).ToArray();
Expand All @@ -49,7 +49,7 @@ public void BeginEncodeHeaders_CacheControlPrivate_NewIndexValue()
var enumerator = new Http2HeadersEnumerator();
enumerator.Initialize(headers);

var hpackEncoder = new HPackEncoder();
var hpackEncoder = new DynamicHPackEncoder();
Assert.True(HPackHeaderWriter.BeginEncodeHeaders(302, hpackEncoder, enumerator, buffer, out var length));

var result = buffer.Slice(5, length - 5).ToArray();
Expand All @@ -75,7 +75,7 @@ public void BeginEncodeHeaders_MaxHeaderTableSizeExceeded_EvictionsToFit()

var enumerator = new Http2HeadersEnumerator();

var hpackEncoder = new HPackEncoder(maxHeaderTableSize: 256);
var hpackEncoder = new DynamicHPackEncoder(maxHeaderTableSize: 256);

// First response
enumerator.Initialize(headers);
Expand Down Expand Up @@ -201,7 +201,7 @@ public void BeginEncodeHeaders_ExcludedHeaders_NotAddedToTable(string headerName
var enumerator = new Http2HeadersEnumerator();
enumerator.Initialize(headers);

var hpackEncoder = new HPackEncoder(maxHeaderTableSize: Http2PeerSettings.DefaultHeaderTableSize);
var hpackEncoder = new DynamicHPackEncoder(maxHeaderTableSize: Http2PeerSettings.DefaultHeaderTableSize);
Assert.True(HPackHeaderWriter.BeginEncodeHeaders(hpackEncoder, enumerator, buffer, out _));

if (neverIndex)
Expand All @@ -227,7 +227,7 @@ public void BeginEncodeHeaders_HeaderExceedHeaderTableSize_NoIndexAndNoHeaderEnt
var enumerator = new Http2HeadersEnumerator();
enumerator.Initialize(headers);

var hpackEncoder = new HPackEncoder();
var hpackEncoder = new DynamicHPackEncoder();
Assert.True(HPackHeaderWriter.BeginEncodeHeaders(200, hpackEncoder, enumerator, buffer, out var length));

Assert.Empty(GetHeaderEntries(hpackEncoder));
Expand Down Expand Up @@ -312,7 +312,7 @@ public void BeginEncodeHeaders_HeaderExceedHeaderTableSize_NoIndexAndNoHeaderEnt
[MemberData(nameof(SinglePayloadData))]
public void EncodesHeadersInSinglePayloadWhenSpaceAvailable(KeyValuePair<string, string>[] headers, byte[] expectedPayload, int? statusCode)
{
HPackEncoder hpackEncoder = new HPackEncoder();
var hpackEncoder = new DynamicHPackEncoder();

var payload = new byte[1024];
var length = 0;
Expand Down Expand Up @@ -376,7 +376,7 @@ public void EncodesHeadersInMultiplePayloadsWhenSpaceNotAvailable(bool exactSize
0x07, 0x4b, 0x65, 0x73, 0x74, 0x72, 0x65, 0x6c
};

var hpackEncoder = new HPackEncoder();
var hpackEncoder = new DynamicHPackEncoder();

Span<byte> payload = new byte[1024];
var offset = 0;
Expand Down Expand Up @@ -415,7 +415,7 @@ public void BeginEncodeHeaders_MaxHeaderTableSizeUpdated_SizeUpdateInHeaders()
{
Span<byte> buffer = new byte[1024 * 16];

var hpackEncoder = new HPackEncoder();
var hpackEncoder = new DynamicHPackEncoder();
hpackEncoder.UpdateMaxHeaderTableSize(100);

var enumerator = new Http2HeadersEnumerator();
Expand Down Expand Up @@ -452,7 +452,7 @@ private static Http2HeadersEnumerator GetHeadersEnumerator(IEnumerable<KeyValueP
return enumerator;
}

private EncoderHeaderEntry GetHeaderEntry(HPackEncoder encoder, int index)
private EncoderHeaderEntry GetHeaderEntry(DynamicHPackEncoder encoder, int index)
{
var entry = encoder.Head;
while (index-- >= 0)
Expand All @@ -462,7 +462,7 @@ private EncoderHeaderEntry GetHeaderEntry(HPackEncoder encoder, int index)
return entry;
}

private List<EncoderHeaderEntry> GetHeaderEntries(HPackEncoder encoder)
private List<EncoderHeaderEntry> GetHeaderEntries(DynamicHPackEncoder encoder)
{
var headers = new List<EncoderHeaderEntry>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Microbenchmarks
public class HPackHeaderWriterBenchmark
{
private Http2HeadersEnumerator _http2HeadersEnumerator;
private HPackEncoder _hpackEncoder;
private DynamicHPackEncoder _hpackEncoder;
private HttpResponseHeaders _knownResponseHeaders;
private HttpResponseHeaders _unknownResponseHeaders;
private byte[] _buffer;
Expand All @@ -25,7 +25,7 @@ public class HPackHeaderWriterBenchmark
public void GlobalSetup()
{
_http2HeadersEnumerator = new Http2HeadersEnumerator();
_hpackEncoder = new HPackEncoder();
_hpackEncoder = new DynamicHPackEncoder();
_buffer = new byte[1024 * 1024];

_knownResponseHeaders = new HttpResponseHeaders
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public abstract class Http2ConnectionBenchmarkBase
private MemoryPool<byte> _memoryPool;
private HttpRequestHeaders _httpRequestHeaders;
private Http2Connection _connection;
private HPackEncoder _hpackEncoder;
private DynamicHPackEncoder _hpackEncoder;
private Http2HeadersEnumerator _requestHeadersEnumerator;
private int _currentStreamId;
private byte[] _headersBuffer;
Expand All @@ -56,7 +56,7 @@ public virtual void GlobalSetup()
_httpRequestHeaders.HeaderAuthority = new StringValues("localhost:80");

_headersBuffer = new byte[1024 * 16];
_hpackEncoder = new HPackEncoder();
_hpackEncoder = new DynamicHPackEncoder();

var serviceContext = TestContextFactory.CreateServiceContext(
serverOptions: new KestrelServerOptions(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void WriteSettings(this PipeWriter writer, Http2PeerSettings clien
writer.Write(payload);
}

public static void WriteStartStream(this PipeWriter writer, int streamId, HPackEncoder hpackEncoder, Http2HeadersEnumerator headers, byte[] headerEncodingBuffer, bool endStream, Http2Frame frame = null)
public static void WriteStartStream(this PipeWriter writer, int streamId, DynamicHPackEncoder hpackEncoder, Http2HeadersEnumerator headers, byte[] headerEncodingBuffer, bool endStream, Http2Frame frame = null)
{
frame ??= new Http2Frame();
frame.PrepareHeaders(Http2HeadersFrameFlags.NONE, streamId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected static IEnumerable<KeyValuePair<string, string>> ReadRateRequestHeader

internal readonly Http2PeerSettings _clientSettings = new Http2PeerSettings();
internal readonly HPackDecoder _hpackDecoder;
internal readonly HPackEncoder _hpackEncoder;
internal readonly DynamicHPackEncoder _hpackEncoder;
private readonly byte[] _headerEncodingBuffer = new byte[Http2PeerSettings.MinAllowedMaxFrameSize];

internal readonly TimeoutControl _timeoutControl;
Expand Down Expand Up @@ -168,7 +168,7 @@ protected static IEnumerable<KeyValuePair<string, string>> ReadRateRequestHeader
public Http2TestBase()
{
_hpackDecoder = new HPackDecoder((int)_clientSettings.HeaderTableSize, MaxRequestHeaderFieldSize);
_hpackEncoder = new HPackEncoder();
_hpackEncoder = new DynamicHPackEncoder();

_timeoutControl = new TimeoutControl(_mockTimeoutHandler.Object);
_mockTimeoutControl = new Mock<MockTimeoutControlBase>(_timeoutControl) { CallBase = true };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace System.Net.Http.HPack
{
internal partial class HPackEncoder
internal class DynamicHPackEncoder
{
public const int DefaultHeaderTableSize = 4096;

Expand All @@ -22,7 +22,7 @@ internal partial class HPackEncoder
private bool _pendingTableSizeUpdate;
private EncoderHeaderEntry? _removed;

public HPackEncoder(bool allowDynamicCompression = true, uint maxHeaderTableSize = DefaultHeaderTableSize)
public DynamicHPackEncoder(bool allowDynamicCompression = true, uint maxHeaderTableSize = DefaultHeaderTableSize)
{
_allowDynamicCompression = allowDynamicCompression;
_maxHeaderTableSize = maxHeaderTableSize;
Expand Down Expand Up @@ -54,7 +54,7 @@ public bool EnsureDynamicTableSizeUpdate(Span<byte> buffer, out int length)
// Check if there is a table size update that should be encoded
if (_pendingTableSizeUpdate)
{
bool success = EncodeDynamicTableSizeUpdate((int)_maxHeaderTableSize, buffer, out length);
bool success = HPackEncoder.EncodeDynamicTableSizeUpdate((int)_maxHeaderTableSize, buffer, out length);
_pendingTableSizeUpdate = false;
return success;
}
Expand All @@ -73,16 +73,16 @@ public bool EncodeHeader(Span<byte> buffer, int staticTableIndex, HeaderEncoding
int index = ResolveDynamicTableIndex(staticTableIndex, name);

return index == -1
? EncodeLiteralHeaderFieldNeverIndexingNewName(name, value, buffer, out bytesWritten)
: EncodeLiteralHeaderFieldNeverIndexing(index, value, buffer, out bytesWritten);
? HPackEncoder.EncodeLiteralHeaderFieldNeverIndexingNewName(name, value, buffer, out bytesWritten)
: HPackEncoder.EncodeLiteralHeaderFieldNeverIndexing(index, value, buffer, out bytesWritten);
}

// No dynamic table. Only use the static table.
if (!_allowDynamicCompression || _maxHeaderTableSize == 0 || encodingHint == HeaderEncodingHint.IgnoreIndex)
{
return staticTableIndex == -1
? EncodeLiteralHeaderFieldWithoutIndexingNewName(name, value, buffer, out bytesWritten)
: EncodeLiteralHeaderFieldWithoutIndexing(staticTableIndex, value, buffer, out bytesWritten);
? HPackEncoder.EncodeLiteralHeaderFieldWithoutIndexingNewName(name, value, buffer, out bytesWritten)
: HPackEncoder.EncodeLiteralHeaderFieldWithoutIndexing(staticTableIndex, value, buffer, out bytesWritten);
}

// Header is greater than the maximum table size.
Expand All @@ -92,8 +92,8 @@ public bool EncodeHeader(Span<byte> buffer, int staticTableIndex, HeaderEncoding
int index = ResolveDynamicTableIndex(staticTableIndex, name);

return index == -1
? EncodeLiteralHeaderFieldWithoutIndexingNewName(name, value, buffer, out bytesWritten)
: EncodeLiteralHeaderFieldWithoutIndexing(index, value, buffer, out bytesWritten);
? HPackEncoder.EncodeLiteralHeaderFieldWithoutIndexingNewName(name, value, buffer, out bytesWritten)
: HPackEncoder.EncodeLiteralHeaderFieldWithoutIndexing(index, value, buffer, out bytesWritten);
}

return EncodeDynamicHeader(buffer, staticTableIndex, name, value, out bytesWritten);
Expand All @@ -117,7 +117,7 @@ private bool EncodeDynamicHeader(Span<byte> buffer, int staticTableIndex, string
{
// Already exists in dynamic table. Write index.
int index = CalculateDynamicTableIndex(headerField.Index);
return EncodeIndexedHeaderField(index, buffer, out bytesWritten);
return HPackEncoder.EncodeIndexedHeaderField(index, buffer, out bytesWritten);
}
else
{
Expand All @@ -126,8 +126,8 @@ private bool EncodeDynamicHeader(Span<byte> buffer, int staticTableIndex, string

int index = ResolveDynamicTableIndex(staticTableIndex, name);
bool success = index == -1
? EncodeLiteralHeaderFieldIndexingNewName(name, value, buffer, out bytesWritten)
: EncodeLiteralHeaderFieldIndexing(index, value, buffer, out bytesWritten);
? HPackEncoder.EncodeLiteralHeaderFieldIndexingNewName(name, value, buffer, out bytesWritten)
: HPackEncoder.EncodeLiteralHeaderFieldIndexing(index, value, buffer, out bytesWritten);

if (success)
{
Expand Down