dotnet/aspnetcore

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

開放

#5,984 建立於 2016年8月30日

 (4 則留言) (0 個反應) (0 位負責人)C# (10,653 個分叉)batch import
api-suggestionarea-middlewareenhancementfeature-rewrite-middlewarehelp wanted

倉庫指標

星標
 (37,933 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

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/")

貢獻者指南