dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.32k stars 9.97k forks source link

Perf: add API to avoid regex matching rewrite rules unless necessary #5984

Open natemcmaster opened 8 years ago

natemcmaster commented 8 years ago

Regex is an expensive part of the rewrite middleware a lot in order to do pattern matching. It is common for rewrite patterns to be led by a static prefix.

Example: "blogs/(.*)" => "api/blogs?id=$1"

For this rule, the middleware can avoid unnecessary regex allocations by only running the rule when the path begins with "/blogs/".

Possible API design:

RewriteOptions.AddRewrite(string prefix, string pattern, string urlResult) // prefix is static, doesn't container rules. If uri doesn't start with the prefix, skip this rule
RewriteOptions.Ignore(string prefix); // always skip rule processing for urls beginning with this prefix

Usage

RewriteOptions.AddRewrite(prefix: "/blogs/", pattern: "(.*)", urlResult: "/api/blogs?id=$1")
RewriteOptions.Ignore("/images/")
mikaelm12 commented 7 years ago

We settled on something like

RewriteOptions.AddRewriteWithPrefix(string prefix, string regex, string replcement)

right?

Tratcher commented 7 years ago

There was talk of:

RewriteOptions.AddWithPrefix(string prefix, builder => 
{
  builder.AddRewrite(regex, replcement);
});

But we shouldn't do anything here without finding a large set of customer rules that would benifit.