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

Implement Header encoding selectors on SocketsHttpHandler #39468

Merged
merged 15 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -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 @@ -463,16 +467,28 @@ public static bool EncodeStringLiteral(string value, Span<byte> destination, out
// | String Data (Length octets) |
// +-------------------------------+

if (destination.Length != 0)
if (destination.Length > value.Length)
MihaZupan marked this conversation as resolved.
Show resolved Hide resolved
{
destination[0] = 0; // TODO: Use Huffman encoding
if (IntegerEncoder.Encode(value.Length, 7, destination, out int integerLength))

int encodedStringLength = valueEncoding is null || valueEncoding.IsSingleByte ? value.Length : valueEncoding.GetByteCount(value);
MihaZupan marked this conversation as resolved.
Show resolved Hide resolved
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 +518,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 || valueEncoding.IsSingleByte)
{
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;
for (int i = 0; i < values.Length; i++)
{
if (i != 0)
{
EncodeValueStringPart(separator, destination);
destination = destination.Slice(separator.Length);
}

string value = values[i];
EncodeValueStringPart(value, destination);
destination = destination.Slice(value.Length);
}
}

integerLength += encodedLength;

if (!EncodeStringLiteralValue(values[j], destination.Slice(integerLength), out encodedLength))
else
{
return false;
int written;
MihaZupan marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < values.Length; i++)
{
if (i != 0)
{
MihaZupan marked this conversation as resolved.
Show resolved Hide resolved
written = valueEncoding.GetBytes(separator, destination);
destination = destination.Slice(written);
}

written = valueEncoding.GetBytes(values[i], destination);
destination = destination.Slice(written);
}
}

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

bytesWritten = integerLength;
return true;
}
}

Expand Down
Loading