Added timeout to regex

This commit is contained in:
Ajay Bhargav Baaskaran 2015-09-10 11:04:23 -07:00
parent 87360d861f
commit ee9945f06d
2 changed files with 12 additions and 2 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Text.RegularExpressions;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
@ -49,7 +50,11 @@ namespace RoutingSample.Web
routeBuilder.MapRoute("regexRoute",
"api/r2constraint/{controller}",
new { foo = "Bar2" },
new { controller = new RegexRouteConstraint(new Regex("^(my.*)$")) });
new
{
controller = new RegexRouteConstraint(
new Regex("^(my.*)$", RegexOptions.None, TimeSpan.FromSeconds(10)))
});
routeBuilder.MapRoute("parameterConstraintRoute",
"api/{controller}/{*extra}",

View File

@ -12,6 +12,8 @@ namespace Microsoft.AspNet.Routing.Constraints
{
public class RegexRouteConstraint : IRouteConstraint
{
private static readonly TimeSpan RegexMatchTimeout = TimeSpan.FromSeconds(10);
public RegexRouteConstraint([NotNull] Regex regex)
{
Constraint = regex;
@ -19,7 +21,10 @@ namespace Microsoft.AspNet.Routing.Constraints
public RegexRouteConstraint([NotNull] string regexPattern)
{
Constraint = new Regex(regexPattern, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
Constraint = new Regex(
regexPattern,
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase,
RegexMatchTimeout);
}
public Regex Constraint { get; private set; }