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 6 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
10 changes: 6 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,9 +71,10 @@ 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)
{
invalidCharacter++; // accommodate the slice above
throw new InvalidOperationException(
$"Invalid template '{template}'. The character '{segment[invalidCharacter]}' in parameter segment '{segment}' is not allowed.");
}
Expand Down
10 changes: 5 additions & 5 deletions src/Http/Routing/src/Patterns/RoutePatternFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ public static RoutePatternParameterPart ParameterPart(string parameterName)
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterName));
}

if (parameterName.IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0)
if (parameterName.AsSpan().IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0)
{
throw new ArgumentException(Resources.FormatTemplateRoute_InvalidParameterName(parameterName));
}
Expand All @@ -833,7 +833,7 @@ public static RoutePatternParameterPart ParameterPart(string parameterName, obje
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterName));
}

if (parameterName.IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0)
if (parameterName.AsSpan().IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0)
{
throw new ArgumentException(Resources.FormatTemplateRoute_InvalidParameterName(parameterName));
}
Expand Down Expand Up @@ -863,7 +863,7 @@ public static RoutePatternParameterPart ParameterPart(
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterName));
}

if (parameterName.IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0)
if (parameterName.AsSpan().IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0)
{
throw new ArgumentException(Resources.FormatTemplateRoute_InvalidParameterName(parameterName));
}
Expand Down Expand Up @@ -900,7 +900,7 @@ public static RoutePatternParameterPart ParameterPart(
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterName));
}

if (parameterName.IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0)
if (parameterName.AsSpan().IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0)
{
throw new ArgumentException(Resources.FormatTemplateRoute_InvalidParameterName(parameterName));
}
Expand Down Expand Up @@ -942,7 +942,7 @@ public static RoutePatternParameterPart ParameterPart(
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(parameterName));
}

if (parameterName.IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0)
if (parameterName.AsSpan().IndexOfAny(RoutePatternParser.InvalidParameterNameChars) >= 0)
{
throw new ArgumentException(Resources.FormatTemplateRoute_InvalidParameterName(parameterName));
}
Expand Down
13 changes: 3 additions & 10 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 @@ -13,17 +14,9 @@ internal static class RoutePatternParser
private const char OpenBrace = '{';
private const char CloseBrace = '}';
private const char QuestionMark = '?';
private const char Asterisk = '*';
private const string PeriodString = ".";

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

public static RoutePattern Parse(string pattern)
{
Expand Down Expand Up @@ -431,7 +424,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
Loading