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

Vectorized HttpCharacters (and used IndexOfAnyValues in other places found) #45300

Merged
merged 7 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 0 additions & 3 deletions src/Servers/Kestrel/Core/src/Internal/KestrelServerImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
Expand Down Expand Up @@ -89,8 +88,6 @@ internal KestrelServerImpl(
Features.Set<IServerAddressesFeature>(_serverAddresses);

_transportManager = new TransportManager(_transportFactories, _multiplexedTransportFactories, ServiceContext);

HttpCharacters.Initialize();
}

private static ServiceContext CreateServiceContext(IOptions<KestrelServerOptions> options, ILoggerFactory loggerFactory, DiagnosticSource? diagnosticSource)
Expand Down
8 changes: 5 additions & 3 deletions src/Shared/HttpSys/RequestProcessing/HeaderCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,12 @@ public static void ValidateHeaderCharacters(string headerCharacters)
{
if (headerCharacters != null)
{
var invalid = HttpCharacters.IndexOfInvalidFieldValueCharExtended(headerCharacters);
if (invalid >= 0)
var invalidIndex = HttpCharacters.IndexOfInvalidFieldValueCharExtended(headerCharacters);
if (invalidIndex >= 0)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Invalid control character in header: 0x{0:X2}", headerCharacters[invalid]));
Throw(headerCharacters, invalidIndex);
static void Throw(string headerCharacters, int invalidIndex)
=> throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Invalid control character in header: 0x{0:X2}", headerCharacters[invalidIndex]));
}
}
}
Expand Down
243 changes: 55 additions & 188 deletions src/Shared/ServerInfrastructure/HttpCharacters.cs
Original file line number Diff line number Diff line change
@@ -1,224 +1,91 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using System.Buffers;
using System.Diagnostics;

namespace Microsoft.AspNetCore.Http;

internal static class HttpCharacters
{
private const int _tableSize = 128;
private static readonly bool[] _alphaNumeric = InitializeAlphaNumeric();
private static readonly bool[] _authority = InitializeAuthority();
private static readonly bool[] _token = InitializeToken();
private static readonly bool[] _host = InitializeHost();
private static readonly bool[] _fieldValue = InitializeFieldValue();

internal static void Initialize()
{
// Access _alphaNumeric to initialize static fields
var _ = _alphaNumeric;
}

private static bool[] InitializeAlphaNumeric()
{
// ALPHA and DIGIT https://tools.ietf.org/html/rfc5234#appendix-B.1
var alphaNumeric = new bool[_tableSize];
for (var c = '0'; c <= '9'; c++)
{
alphaNumeric[c] = true;
}
for (var c = 'A'; c <= 'Z'; c++)
{
alphaNumeric[c] = true;
}
for (var c = 'a'; c <= 'z'; c++)
{
alphaNumeric[c] = true;
}
return alphaNumeric;
}

private static bool[] InitializeAuthority()
{
// Authority https://tools.ietf.org/html/rfc3986#section-3.2
// Examples:
// microsoft.com
// hostname:8080
// [::]:8080
// [fe80::]
// 127.0.0.1
// [email protected]
// user:[email protected]
var authority = new bool[_tableSize];
Array.Copy(_alphaNumeric, authority, _tableSize);
authority[':'] = true;
authority['.'] = true;
authority['-'] = true;
authority['['] = true;
authority[']'] = true;
authority['@'] = true;
return authority;
}

private static bool[] InitializeToken()
{
// tchar https://tools.ietf.org/html/rfc7230#appendix-B
var token = new bool[_tableSize];
Array.Copy(_alphaNumeric, token, _tableSize);
amcasey marked this conversation as resolved.
Show resolved Hide resolved
token['!'] = true;
token['#'] = true;
token['$'] = true;
token['%'] = true;
token['&'] = true;
token['\''] = true;
token['*'] = true;
token['+'] = true;
token['-'] = true;
token['.'] = true;
token['^'] = true;
token['_'] = true;
token['`'] = true;
token['|'] = true;
token['~'] = true;
return token;
}

private static bool[] InitializeHost()
{
// Matches Http.Sys
// Matches RFC 3986 except "*" / "+" / "," / ";" / "=" and "%" HEXDIG HEXDIG which are not allowed by Http.Sys
var host = new bool[_tableSize];
Array.Copy(_alphaNumeric, host, _tableSize);
host['!'] = true;
host['$'] = true;
host['&'] = true;
host['\''] = true;
host['('] = true;
host[')'] = true;
host['-'] = true;
host['.'] = true;
host['_'] = true;
host['~'] = true;
return host;
}

private static bool[] InitializeFieldValue()
// ALPHA and DIGIT https://tools.ietf.org/html/rfc5234#appendix-B.1
private const string AlphaNumeric = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

// Authority https://tools.ietf.org/html/rfc3986#section-3.2
// Examples:
// microsoft.com
// hostname:8080
// [::]:8080
// [fe80::]
// 127.0.0.1
// [email protected]
// user:[email protected]
private static readonly IndexOfAnyValues<byte> _allowedAuthorityBytes = IndexOfAnyValues.Create(":.-[]@0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"u8);

// Matches Http.Sys
// Matches RFC 3986 except "*" / "+" / "," / ";" / "=" and "%" HEXDIG HEXDIG which are not allowed by Http.Sys
private static readonly IndexOfAnyValues<char> _allowedHostChars = IndexOfAnyValues.Create("!$&'()-._~" + AlphaNumeric);

// tchar https://tools.ietf.org/html/rfc7230#appendix-B
private static readonly IndexOfAnyValues<char> _allowedTokenChars = IndexOfAnyValues.Create("!#$%&'*+-.^_`|~" + AlphaNumeric);
private static readonly IndexOfAnyValues<byte> _allowedTokenBytes = IndexOfAnyValues.Create("!#$%&'*+-.^_`|~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"u8);

// field-value https://tools.ietf.org/html/rfc7230#section-3.2
private static readonly IndexOfAnyValues<char> _allowedFieldChars = CreateAllowedFieldChars();
gfoidl marked this conversation as resolved.
Show resolved Hide resolved

private static IndexOfAnyValues<char> CreateAllowedFieldChars()
{
// field-value https://tools.ietf.org/html/rfc7230#section-3.2
var fieldValue = new bool[_tableSize];

fieldValue[0x9] = true; // HTAB
Span<char> tmp = stackalloc char[128];
tmp[0] = (char)0x9; // HTAB
var count = 1;

for (var c = 0x20; c <= 0x7e; c++) // VCHAR and SP
for (var c = 0x20; c <= 0x7E; ++c) // VCHAR and SP
{
fieldValue[c] = true;
tmp[count++] = (char)c;
}
return fieldValue;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ContainsInvalidAuthorityChar(Span<byte> s)
{
var authority = _authority;

for (var i = 0; i < s.Length; i++)
{
var c = s[i];
if (c >= (uint)authority.Length || !authority[c])
{
return true;
}
}

return false;
Debug.Assert(count <= tmp.Length);
return IndexOfAnyValues.Create(tmp[..count]);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOfInvalidHostChar(string s)
{
var host = _host;

for (var i = 0; i < s.Length; i++)
{
var c = s[i];
if (c >= (uint)host.Length || !host[c])
{
return i;
}
}
public static bool ContainsInvalidAuthorityChar(ReadOnlySpan<byte> span) => span.IndexOfAnyExcept(_allowedAuthorityBytes) >= 0;
stephentoub marked this conversation as resolved.
Show resolved Hide resolved

return -1;
}
public static int IndexOfInvalidHostChar(ReadOnlySpan<char> span) => span.IndexOfAnyExcept(_allowedHostChars);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOfInvalidTokenChar(string s)
{
var token = _token;
public static int IndexOfInvalidTokenChar(ReadOnlySpan<char> span) => span.IndexOfAnyExcept(_allowedTokenChars);

for (var i = 0; i < s.Length; i++)
{
var c = s[i];
if (c >= (uint)token.Length || !token[c])
{
return i;
}
}
public static int IndexOfInvalidTokenChar(ReadOnlySpan<byte> span) => span.IndexOfAnyExcept(_allowedTokenBytes);

return -1;
}
// Follows field-value rules in https://tools.ietf.org/html/rfc7230#section-3.2
// Disallows characters > 0x7E.
public static int IndexOfInvalidFieldValueChar(ReadOnlySpan<char> span) => span.IndexOfAnyExcept(_allowedFieldChars);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOfInvalidTokenChar(ReadOnlySpan<byte> span)
// Follows field-value rules for chars <= 0x7F. Allows extended characters > 0x7F.
public static int IndexOfInvalidFieldValueCharExtended(ReadOnlySpan<char> span)
gfoidl marked this conversation as resolved.
Show resolved Hide resolved
{
var token = _token;
var idx = span.IndexOfAnyExcept(_allowedFieldChars);

for (var i = 0; i < span.Length; i++)
{
var c = span[i];
if (c >= (uint)token.Length || !token[c])
{
return i;
}
}

return -1;
return idx < 0 ? -1 : IndexOfInvalidFieldValueCharExtended(span, idx);
}

// Follows field-value rules in https://tools.ietf.org/html/rfc7230#section-3.2
// Disallows characters > 0x7E.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOfInvalidFieldValueChar(string s)
private static int IndexOfInvalidFieldValueCharExtended(ReadOnlySpan<char> span, int idx)
{
var fieldValue = _fieldValue;

for (var i = 0; i < s.Length; i++)
while (true)
{
var c = s[i];
if (c >= (uint)fieldValue.Length || !fieldValue[c])
if (span[idx] <= 0x7F)
{
return i;
return idx;
}
}

return -1;
}

// Follows field-value rules for chars <= 0x7F. Allows extended characters > 0x7F.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOfInvalidFieldValueCharExtended(string s)
{
var fieldValue = _fieldValue;

for (var i = 0; i < s.Length; i++)
{
var c = s[i];
if (c < (uint)fieldValue.Length && !fieldValue[c])
var tmpIdx = span.Slice(idx + 1).IndexOfAnyExcept(_allowedFieldChars);
if (tmpIdx < 0)
{
return i;
return -1;
}
}

return -1;
idx += 1 + tmpIdx;
}
}
}