Skip to content
Open
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
4 changes: 2 additions & 2 deletions Hpack/Huffman.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ public static string Decode(ArraySegment<byte> input, ArrayPool<byte> pool)
}

// Convert the buffer into a string
// TODO: Check if encoding is really correct
var str = Encoding.ASCII.GetString(outBuf, 0, byteCount);
// UTF-8 encoding should be used to support non-ASCII header names and values
var str = Encoding.UTF8.GetString(outBuf, 0, byteCount);
pool.Return(outBuf);
return str;
}
Expand Down
4 changes: 2 additions & 2 deletions Hpack/StringDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ private int DecodeContByteData(ArraySegment<byte> buf)
}
else
{
// TODO: Check if encoding is really correct
// UTF-8 encoding should be used to support non-ASCII header names and values
this.Result =
Encoding.ASCII.GetString(view.Array, view.Offset, view.Count);
Encoding.UTF8.GetString(view.Array, view.Offset, view.Count);
}
// TODO: Optionally check here for valid HTTP/2 header names
this.Done = true;
Expand Down
16 changes: 8 additions & 8 deletions Hpack/StringEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static class StringEncoder
/// </summary>
public static int GetByteLength(string value)
{
return Encoding.ASCII.GetByteCount(value);
return Encoding.UTF8.GetByteCount(value);
}

/// <summary>
Expand Down Expand Up @@ -61,7 +61,7 @@ public static int EncodeInto(
// Check if the string should be reencoded with huffman encoding
if (huffman == HuffmanStrategy.Always || huffman == HuffmanStrategy.IfSmaller)
{
huffmanInputBuf = Encoding.ASCII.GetBytes(value);
huffmanInputBuf = Encoding.UTF8.GetBytes(value);
requiredHuffmanBytes = Huffman.EncodedLength(
new ArraySegment<byte>(huffmanInputBuf));
if (huffman == HuffmanStrategy.IfSmaller && requiredHuffmanBytes < encodedByteLen)
Expand Down Expand Up @@ -97,8 +97,8 @@ public static int EncodeInto(
else
{
if (free < valueByteLen) return -1;
// Use ASCII encoder to write bytes to target buffer
used = Encoding.ASCII.GetBytes(
// Use UTF-8 encoder for maximum compatibility when writing bytes to the target buffer
used = Encoding.UTF8.GetBytes(
value, 0, value.Length, buf.Array, offset);
offset += used;
}
Expand All @@ -117,17 +117,17 @@ public static int EncodeInto(
public static byte[] Encode(string value, HuffmanStrategy huffman)
{
// Estimate the size of the buffer
var asciiSize = Encoding.ASCII.GetByteCount(value);
var estimatedHeaderLength = IntEncoder.RequiredBytes(asciiSize, 0, 7);
var estimatedBufferSize = estimatedHeaderLength + asciiSize;
var utf8Size = Encoding.UTF8.GetByteCount(value);
var estimatedHeaderLength = IntEncoder.RequiredBytes(utf8Size, 0, 7);
var estimatedBufferSize = estimatedHeaderLength + utf8Size;

while (true)
{
// Create a buffer with some headroom
var buf = new byte[estimatedBufferSize + 16];
// Try to serialize value in there
var size = EncodeInto(
new ArraySegment<byte>(buf), value, asciiSize, huffman);
new ArraySegment<byte>(buf), value, utf8Size, huffman);
if (size != -1)
{
// Serialization was performed
Expand Down