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 3 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
9 changes: 5 additions & 4 deletions src/Components/Components/src/Routing/TemplateParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers;

namespace Microsoft.AspNetCore.Components.Routing;

// This implementation is temporary, in the future we'll want to have
Expand All @@ -18,8 +20,7 @@ namespace Microsoft.AspNetCore.Components.Routing;
// * Catch-all parameters (Like /blog/{*slug})
internal sealed class TemplateParser
{
public static readonly char[] InvalidParameterNameCharacters =
new char[] { '{', '}', '=', '.' };
private static readonly IndexOfAnyValues<char> _invalidParameterNameCharacters = IndexOfAnyValues.Create("{}=.");
stephentoub marked this conversation as resolved.
Show resolved Hide resolved

internal static RouteTemplate ParseTemplate(string template)
{
Expand Down Expand Up @@ -70,8 +71,8 @@ internal static RouteTemplate ParseTemplate(string template)
$"Invalid template '{template}'. Empty parameter name in segment '{segment}' is not allowed.");
}

var invalidCharacter = segment.IndexOfAny(InvalidParameterNameCharacters, 1, segment.Length - 2);
if (invalidCharacter != -1)
var invalidCharacter = segment.AsSpan(1, segment.Length - 2).IndexOfAny(_invalidParameterNameCharacters);
if (invalidCharacter >= 0)
{
throw new InvalidOperationException(
$"Invalid template '{template}'. The character '{segment[invalidCharacter]}' in parameter segment '{segment}' is not allowed.");
Expand Down
12 changes: 3 additions & 9 deletions src/Http/Routing/src/Patterns/RoutePatternParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#nullable disable

using System.Buffers;
using System.Diagnostics;

namespace Microsoft.AspNetCore.Routing.Patterns;
Expand All @@ -16,14 +17,7 @@ internal static class RoutePatternParser
private const char Asterisk = '*';
private const string PeriodString = ".";

internal static readonly char[] InvalidParameterNameChars = new char[]
{
Separator,
OpenBrace,
CloseBrace,
QuestionMark,
Asterisk
};
private static readonly IndexOfAnyValues<char> _invalidParameterNameChars = IndexOfAnyValues.Create("/{}?*");

public static RoutePattern Parse(string pattern)
{
Expand Down Expand Up @@ -431,7 +425,7 @@ private static bool IsSegmentValid(Context context, List<RoutePatternPart> parts

private static bool IsValidParameterName(Context context, string parameterName)
{
if (parameterName.Length == 0 || parameterName.IndexOfAny(InvalidParameterNameChars) >= 0)
if (parameterName.Length == 0 || parameterName.AsSpan().IndexOfAny(_invalidParameterNameChars) >= 0)
{
context.Error = Resources.FormatTemplateRoute_InvalidParameterName(parameterName);
return false;
Expand Down
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
232 changes: 31 additions & 201 deletions src/Shared/ServerInfrastructure/HttpCharacters.cs
Original file line number Diff line number Diff line change
@@ -1,224 +1,54 @@
// 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;

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();
// ALPHA and DIGIT https://tools.ietf.org/html/rfc5234#appendix-B.1
private const string AlphaNumeric = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

internal static void Initialize()
{
// Access _alphaNumeric to initialize static fields
var _ = _alphaNumeric;
}
// 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);

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;
}
// 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);

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;
}
// 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);

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;
}
// field-value https://tools.ietf.org/html/rfc7230#section-3.2
// HTAB, [VCHAR, SP]
private static readonly IndexOfAnyValues<char> _allowedFieldChars = IndexOfAnyValues.Create("\t !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~");
gfoidl marked this conversation as resolved.
Show resolved Hide resolved

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 readonly IndexOfAnyValues<char> _invalidFieldChars = IndexOfAnyValues.Create(
gfoidl marked this conversation as resolved.
Show resolved Hide resolved
"\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u000A\u000B\u000C\u000D\u000E\u000F\u0010" +
"\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F\u007F");

private static bool[] InitializeFieldValue()
{
// field-value https://tools.ietf.org/html/rfc7230#section-3.2
var fieldValue = new bool[_tableSize];
public static bool ContainsInvalidAuthorityChar(ReadOnlySpan<byte> span) => span.IndexOfAnyExcept(_allowedAuthorityBytes) >= 0;
stephentoub marked this conversation as resolved.
Show resolved Hide resolved

fieldValue[0x9] = true; // HTAB
public static int IndexOfInvalidHostChar(ReadOnlySpan<char> span) => span.IndexOfAnyExcept(_allowedHostChars);

for (var c = 0x20; c <= 0x7e; c++) // VCHAR and SP
{
fieldValue[c] = true;
}
return fieldValue;
}
public static int IndexOfInvalidTokenChar(ReadOnlySpan<char> span) => span.IndexOfAnyExcept(_allowedTokenChars);

[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;
}

[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;
}
}

return -1;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOfInvalidTokenChar(string s)
{
var token = _token;

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

return -1;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOfInvalidTokenChar(ReadOnlySpan<byte> span)
{
var token = _token;

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

return -1;
}
public static int IndexOfInvalidTokenChar(ReadOnlySpan<byte> span) => span.IndexOfAnyExcept(_allowedTokenBytes);

// 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)
{
var fieldValue = _fieldValue;

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

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

// 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])
{
return i;
}
}

return -1;
}
public static int IndexOfInvalidFieldValueCharExtended(ReadOnlySpan<char> span) => span.IndexOfAny(_invalidFieldChars);
}