Skip to content

Commit

Permalink
Implement Header encoding selectors on SocketsHttpHandler (dotnet#39468)
Browse files Browse the repository at this point in the history
* Expose HeaderEncodingSelectors on SocketsHttpHandler

* Implement header encoding selectors in SocketsHttpHandler

* Add header encoding tests

* Add summaries for new APIs

* Use Stream.Write(byte[], int, int) overloads for framework compat

* Add dummy API implementation to browser target

* HPack/QPack fixes

* Move HeaderEncodingSelector delegate to namespace, add TContext

* Encoding fixes

* Remove unused using

* Simplify test

* HttpConnection PR feedback

* Simplify fast-path detection
  • Loading branch information
MihaZupan committed Jul 30, 2020
1 parent c205ed0 commit 02e872e
Show file tree
Hide file tree
Showing 25 changed files with 656 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#nullable enable
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace System.Net.Http.HPack
{
Expand Down Expand Up @@ -96,7 +97,7 @@ public static bool EncodeLiteralHeaderFieldWithoutIndexing(int index, string val
if (IntegerEncoder.Encode(index, 4, destination, out int indexLength))
{
Debug.Assert(indexLength >= 1);
if (EncodeStringLiteral(value, destination.Slice(indexLength), out int nameLength))
if (EncodeStringLiteral(value, valueEncoding: null, destination.Slice(indexLength), out int nameLength))
{
bytesWritten = indexLength + nameLength;
return true;
Expand Down Expand Up @@ -128,7 +129,7 @@ public static bool EncodeLiteralHeaderFieldNeverIndexing(int index, string value
if (IntegerEncoder.Encode(index, 4, destination, out int indexLength))
{
Debug.Assert(indexLength >= 1);
if (EncodeStringLiteral(value, destination.Slice(indexLength), out int nameLength))
if (EncodeStringLiteral(value, valueEncoding: null, destination.Slice(indexLength), out int nameLength))
{
bytesWritten = indexLength + nameLength;
return true;
Expand Down Expand Up @@ -160,7 +161,7 @@ public static bool EncodeLiteralHeaderFieldIndexing(int index, string value, Spa
if (IntegerEncoder.Encode(index, 6, destination, out int indexLength))
{
Debug.Assert(indexLength >= 1);
if (EncodeStringLiteral(value, destination.Slice(indexLength), out int nameLength))
if (EncodeStringLiteral(value, valueEncoding: null, destination.Slice(indexLength), out int nameLength))
{
bytesWritten = indexLength + nameLength;
return true;
Expand Down Expand Up @@ -276,7 +277,7 @@ private static bool EncodeLiteralHeaderNewNameCore(byte mask, string name, strin
{
destination[0] = mask;
if (EncodeLiteralHeaderName(name, destination.Slice(1), out int nameLength) &&
EncodeStringLiteral(value, destination.Slice(1 + nameLength), out int valueLength))
EncodeStringLiteral(value, valueEncoding: null, destination.Slice(1 + nameLength), out int valueLength))
{
bytesWritten = 1 + nameLength + valueLength;
return true;
Expand All @@ -289,6 +290,11 @@ private static bool EncodeLiteralHeaderNewNameCore(byte mask, string name, strin

/// <summary>Encodes a "Literal Header Field without Indexing - New Name".</summary>
public static bool EncodeLiteralHeaderFieldWithoutIndexingNewName(string name, ReadOnlySpan<string> values, string separator, Span<byte> destination, out int bytesWritten)
{
return EncodeLiteralHeaderFieldWithoutIndexingNewName(name, values, separator, valueEncoding: null, destination, out bytesWritten);
}

public static bool EncodeLiteralHeaderFieldWithoutIndexingNewName(string name, ReadOnlySpan<string> values, string separator, Encoding? valueEncoding, Span<byte> destination, out int bytesWritten)
{
// From https://tools.ietf.org/html/rfc7541#section-6.2.2
// ------------------------------------------------------
Expand All @@ -309,7 +315,7 @@ public static bool EncodeLiteralHeaderFieldWithoutIndexingNewName(string name, R
{
destination[0] = 0;
if (EncodeLiteralHeaderName(name, destination.Slice(1), out int nameLength) &&
EncodeStringLiterals(values, separator, destination.Slice(1 + nameLength), out int valueLength))
EncodeStringLiterals(values, separator, valueEncoding, destination.Slice(1 + nameLength), out int valueLength))
{
bytesWritten = 1 + nameLength + valueLength;
return true;
Expand Down Expand Up @@ -395,27 +401,20 @@ private static bool EncodeLiteralHeaderName(string value, Span<byte> destination
return false;
}

private static bool EncodeStringLiteralValue(string value, Span<byte> destination, out int bytesWritten)
private static void EncodeValueStringPart(string value, Span<byte> destination)
{
if (value.Length <= destination.Length)
Debug.Assert(destination.Length >= value.Length);

for (int i = 0; i < value.Length; i++)
{
for (int i = 0; i < value.Length; i++)
char c = value[i];
if ((c & 0xFF80) != 0)
{
char c = value[i];
if ((c & 0xFF80) != 0)
{
throw new HttpRequestException(SR.net_http_request_invalid_char_encoding);
}

destination[i] = (byte)c;
throw new HttpRequestException(SR.net_http_request_invalid_char_encoding);
}

bytesWritten = value.Length;
return true;
destination[i] = (byte)c;
}

bytesWritten = 0;
return false;
}

public static bool EncodeStringLiteral(ReadOnlySpan<byte> value, Span<byte> destination, out int bytesWritten)
Expand Down Expand Up @@ -453,6 +452,11 @@ public static bool EncodeStringLiteral(ReadOnlySpan<byte> value, Span<byte> dest
}

public static bool EncodeStringLiteral(string value, Span<byte> destination, out int bytesWritten)
{
return EncodeStringLiteral(value, valueEncoding: null, destination, out bytesWritten);
}

public static bool EncodeStringLiteral(string value, Encoding? valueEncoding, Span<byte> destination, out int bytesWritten)
{
// From https://tools.ietf.org/html/rfc7541#section-5.2
// ------------------------------------------------------
Expand All @@ -466,13 +470,28 @@ public static bool EncodeStringLiteral(string value, Span<byte> destination, out
if (destination.Length != 0)
{
destination[0] = 0; // TODO: Use Huffman encoding
if (IntegerEncoder.Encode(value.Length, 7, destination, out int integerLength))

int encodedStringLength = valueEncoding is null || ReferenceEquals(valueEncoding, Encoding.Latin1)
? value.Length
: valueEncoding.GetByteCount(value);

if (IntegerEncoder.Encode(encodedStringLength, 7, destination, out int integerLength))
{
Debug.Assert(integerLength >= 1);

if (EncodeStringLiteralValue(value, destination.Slice(integerLength), out int valueLength))
destination = destination.Slice(integerLength);
if (encodedStringLength <= destination.Length)
{
bytesWritten = integerLength + valueLength;
if (valueEncoding is null)
{
EncodeValueStringPart(value, destination);
}
else
{
int written = valueEncoding.GetBytes(value, destination);
Debug.Assert(written == encodedStringLength);
}

bytesWritten = integerLength + encodedStringLength;
return true;
}
}
Expand Down Expand Up @@ -502,56 +521,87 @@ public static bool EncodeDynamicTableSizeUpdate(int value, Span<byte> destinatio
}

public static bool EncodeStringLiterals(ReadOnlySpan<string> values, string? separator, Span<byte> destination, out int bytesWritten)
{
return EncodeStringLiterals(values, separator, valueEncoding: null, destination, out bytesWritten);
}

public static bool EncodeStringLiterals(ReadOnlySpan<string> values, string? separator, Encoding? valueEncoding, Span<byte> destination, out int bytesWritten)
{
bytesWritten = 0;

if (values.Length == 0)
{
return EncodeStringLiteral("", destination, out bytesWritten);
return EncodeStringLiteral("", valueEncoding: null, destination, out bytesWritten);
}
else if (values.Length == 1)
{
return EncodeStringLiteral(values[0], destination, out bytesWritten);
return EncodeStringLiteral(values[0], valueEncoding, destination, out bytesWritten);
}

if (destination.Length != 0)
{
int valueLength = 0;
Debug.Assert(separator != null);
int valueLength;

// Calculate length of all parts and separators.
foreach (string part in values)
if (valueEncoding is null || ReferenceEquals(valueEncoding, Encoding.Latin1))
{
valueLength = checked((int)(valueLength + part.Length));
valueLength = checked((int)(values.Length - 1) * separator.Length);
foreach (string part in values)
{
valueLength = checked((int)(valueLength + part.Length));
}
}
else
{
valueLength = checked((int)(values.Length - 1) * valueEncoding.GetByteCount(separator));
foreach (string part in values)
{
valueLength = checked((int)(valueLength + valueEncoding.GetByteCount(part)));
}
}

Debug.Assert(separator != null);
valueLength = checked((int)(valueLength + (values.Length - 1) * separator.Length));

destination[0] = 0;
if (IntegerEncoder.Encode(valueLength, 7, destination, out int integerLength))
{
Debug.Assert(integerLength >= 1);

int encodedLength = 0;
for (int j = 0; j < values.Length; j++)
destination = destination.Slice(integerLength);
if (destination.Length >= valueLength)
{
if (j != 0 && !EncodeStringLiteralValue(separator, destination.Slice(integerLength), out encodedLength))
if (valueEncoding is null)
{
return false;
string value = values[0];
EncodeValueStringPart(value, destination);
destination = destination.Slice(value.Length);

for (int i = 1; i < values.Length; i++)
{
EncodeValueStringPart(separator, destination);
destination = destination.Slice(separator.Length);

value = values[i];
EncodeValueStringPart(value, destination);
destination = destination.Slice(value.Length);
}
}
else
{
int written = valueEncoding.GetBytes(values[0], destination);
destination = destination.Slice(written);

integerLength += encodedLength;
for (int i = 1; i < values.Length; i++)
{
written = valueEncoding.GetBytes(separator, destination);
destination = destination.Slice(written);

if (!EncodeStringLiteralValue(values[j], destination.Slice(integerLength), out encodedLength))
{
return false;
written = valueEncoding.GetBytes(values[i], destination);
destination = destination.Slice(written);
}
}

integerLength += encodedLength;
bytesWritten = integerLength + valueLength;
return true;
}

bytesWritten = integerLength;
return true;
}
}

Expand Down
Loading

0 comments on commit 02e872e

Please sign in to comment.