fix(middleware): gate only body response modifiers

Replace the rewrite requirement check with a BodyResponseModifier
marker and treat header and body modifiers separately.

This ensures header/status rewrites still apply when body rewriting is
blocked (for binary, encoded, or chunked responses), while body changes
are skipped safely. It also avoids body reset/close side effects and
returns early on passthrough responses.

Update middleware tests to cover split header/body behavior and themed
middleware body-skip scenarios.
This commit is contained in:
yusing
2026-03-01 04:09:50 +08:00
parent 59238adb5b
commit 41de86de75
7 changed files with 118 additions and 144 deletions

View File

@@ -22,9 +22,7 @@ type modifyHTML struct {
var ModifyHTML = NewMiddleware[modifyHTML]()
func (*modifyHTML) requiresBodyRewrite() bool {
return true
}
func (*modifyHTML) isBodyResponseModifier() {}
func (m *modifyHTML) before(_ http.ResponseWriter, req *http.Request) bool {
req.Header.Set("Accept-Encoding", "identity")