dotnet / dotnet/aspnetcore

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

Open
#5,984 4 comments 0 reactions 0 assignees View on GitHub
api-suggestion area-middleware enhancement feature-rewrite-middleware help wanted
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

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:

``` c#
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

``` c#
RewriteOptions.AddRewrite(prefix: "/blogs/", pattern: "(.*)", urlResult: "/api/blogs?id=$1")
RewriteOptions.Ignore("/images/")
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.