Skip to content

Commit

Permalink
Allow Regex engine to be trimmed (#46813)
Browse files Browse the repository at this point in the history
* Allow Regex engine to be trimmed

Move the lazy Regex creation code to a delegate that is only set with the RegexRouteConstraint constructor that takes a string regexPattern. This allows for the Regex engine to be trimmed when the regexPattern constructor is trimmed.

Fix #46142
  • Loading branch information
eerhardt committed Feb 23, 2023
1 parent 33e7a9a commit ce8e0ce
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/Http/Routing/src/Constraints/RegexRouteConstraint.cs
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.RegularExpressions;
Expand All @@ -15,7 +16,7 @@ namespace Microsoft.AspNetCore.Routing.Constraints;
public class RegexRouteConstraint : IRouteConstraint, IParameterLiteralNodeMatchingPolicy
{
private static readonly TimeSpan RegexMatchTimeout = TimeSpan.FromSeconds(10);
private readonly string _regexPattern;
private readonly Func<Regex>? _regexFactory;
private Regex? _constraint;

/// <summary>
Expand All @@ -27,20 +28,24 @@ public RegexRouteConstraint(Regex regex)
ArgumentNullException.ThrowIfNull(regex);

_constraint = regex;
_regexPattern = regex.ToString();
}

/// <summary>
/// Constructor for a <see cref="RegexRouteConstraint"/> given a <paramref name="regexPattern"/>.
/// </summary>
/// <param name="regexPattern">A string containing the regex pattern.</param>
public RegexRouteConstraint(
[StringSyntax(StringSyntaxAttribute.Regex, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)]
[StringSyntax(StringSyntaxAttribute.Regex, RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase)]
string regexPattern)
{
ArgumentNullException.ThrowIfNull(regexPattern);

_regexPattern = regexPattern;
// Create regex instance lazily to avoid compiling regexes at app startup. Delay creation until Constraint is first evaluated.
// The regex instance is created by a delegate here to allow the regex engine to be trimmed when this constructor is trimmed.
_regexFactory = () => new Regex(
regexPattern,
RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase,
RegexMatchTimeout);
}

/// <summary>
Expand All @@ -50,12 +55,13 @@ public Regex Constraint
{
get
{
// Create regex instance lazily to avoid compiling regexes at app startup. Delay creation until constraint is first evaluated.
// This is not thread safe. No side effect but multiple instances of a regex instance could be created from a burst of requests.
_constraint ??= new Regex(
_regexPattern,
RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase,
RegexMatchTimeout);
if (_constraint is null)
{
Debug.Assert(_regexFactory is not null);

// This is not thread-safe. No side effect, but multiple instances of a regex instance could be created from a burst of requests.
_constraint = _regexFactory();
}

return _constraint;
}
Expand Down

0 comments on commit ce8e0ce

Please sign in to comment.