Skip to content

Commit

Permalink
HTTP/3: Remove ASP.NET Core specific code from QPackEncoder (#54378)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Jun 18, 2021
1 parent 1401202 commit 5221db9
Showing 1 changed file with 1 addition and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

namespace System.Net.Http.QPack
{
internal sealed class QPackEncoder
internal static class QPackEncoder
{
private IEnumerator<KeyValuePair<string, string>>? _enumerator;

// https://tools.ietf.org/html/draft-ietf-quic-qpack-11#section-4.5.2
// 0 1 2 3 4 5 6 7
// +---+---+---+---+---+---+---+---+
Expand Down Expand Up @@ -403,93 +401,5 @@ private static bool EncodeHeaderBlockPrefix(Span<byte> destination, out int byte

return true;
}

public bool BeginEncode(IEnumerable<KeyValuePair<string, string>> headers, Span<byte> buffer, out int length)
{
_enumerator = headers.GetEnumerator();

bool hasValue = _enumerator.MoveNext();
Debug.Assert(hasValue == true);

buffer[0] = 0;
buffer[1] = 0;

bool doneEncode = Encode(buffer.Slice(2), out length);

// Add two for the first two bytes.
length += 2;
return doneEncode;
}

public bool BeginEncode(int statusCode, IEnumerable<KeyValuePair<string, string>> headers, Span<byte> buffer, out int length)
{
_enumerator = headers.GetEnumerator();

bool hasValue = _enumerator.MoveNext();
Debug.Assert(hasValue == true);

// https://quicwg.org/base-drafts/draft-ietf-quic-qpack.html#header-prefix
buffer[0] = 0;
buffer[1] = 0;

int statusCodeLength = EncodeStatusCode(statusCode, buffer.Slice(2));
bool done = Encode(buffer.Slice(statusCodeLength + 2), throwIfNoneEncoded: false, out int headersLength);
length = statusCodeLength + headersLength + 2;

return done;
}

public bool Encode(Span<byte> buffer, out int length)
{
return Encode(buffer, throwIfNoneEncoded: true, out length);
}

private bool Encode(Span<byte> buffer, bool throwIfNoneEncoded, out int length)
{
length = 0;

do
{
if (!EncodeLiteralHeaderFieldWithoutNameReference(_enumerator!.Current.Key, _enumerator.Current.Value, buffer.Slice(length), out int headerLength))
{
if (length == 0 && throwIfNoneEncoded)
{
throw new QPackEncodingException("TODO sync with corefx" /* CoreStrings.HPackErrorNotEnoughBuffer */);
}
return false;
}

length += headerLength;
} while (_enumerator.MoveNext());

return true;
}

private int EncodeStatusCode(int statusCode, Span<byte> buffer)
{
switch (statusCode)
{
case 200:
case 204:
case 206:
case 304:
case 400:
case 404:
case 500:
EncodeStaticIndexedHeaderField(H3StaticTable.StatusIndex[statusCode], buffer, out var bytesWritten);
return bytesWritten;
default:
// https://tools.ietf.org/html/draft-ietf-quic-qpack-21#section-4.5.4
// Index is 63 - :status
buffer[0] = 0b01011111;
buffer[1] = 0b00110000;

ReadOnlySpan<byte> statusBytes = StatusCodes.ToStatusBytes(statusCode);
buffer[2] = (byte)statusBytes.Length;
statusBytes.CopyTo(buffer.Slice(3));

return 3 + statusBytes.Length;
}
}
}
}

0 comments on commit 5221db9

Please sign in to comment.