fix(forwarder): enforce match pattern as content filter when rewriter is set

When a rewrite rule has both match and rewriter set, the body is only
sent to the rewriter plugin if it matches the pattern. A nil pattern
(non-empty match) means unconditional rewrite via the plugin.

Coincidence: fix indentation drift in the previous commit's rewrite
condition blocks (tab alignment was off by one level).
This commit is contained in:
ginuerzh
2026-06-27 23:19:54 +08:00
parent 1b07475bf3
commit f63f36805b
2 changed files with 218 additions and 13 deletions
+16 -12
View File
@@ -65,12 +65,14 @@ func rewriteRespBody(ctx context.Context, resp *http.Response, rewrites ...chain
continue
}
if rewrite.Rewriter != nil {
body, err = rewrite.Rewriter.Rewrite(ctx, body)
if err != nil {
return err
}
} else if rewrite.Pattern != nil {
if rewrite.Rewriter != nil {
if rewrite.Pattern == nil || rewrite.Pattern.Match(body) {
body, err = rewrite.Rewriter.Rewrite(ctx, body)
if err != nil {
return err
}
}
} else if rewrite.Pattern != nil {
body = rewrite.Pattern.ReplaceAll(body, rewrite.Replacement)
}
}
@@ -119,12 +121,14 @@ func rewriteReqBody(ctx context.Context, req *http.Request, rewrites ...chain.HT
continue
}
if rewrite.Rewriter != nil {
body, err = rewrite.Rewriter.Rewrite(ctx, body)
if err != nil {
return err
}
} else if rewrite.Pattern != nil {
if rewrite.Rewriter != nil {
if rewrite.Pattern == nil || rewrite.Pattern.Match(body) {
body, err = rewrite.Rewriter.Rewrite(ctx, body)
if err != nil {
return err
}
}
} else if rewrite.Pattern != nil {
body = rewrite.Pattern.ReplaceAll(body, rewrite.Replacement)
}
}