mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-26 10:18:29 +02:00
fix(middleware): skip body rewriters when buffering fails
Prevent response modifiers that require body rewriting from running when the body rewrite gate blocks buffering (for example, chunked transfer encoding). Add an explicit `requiresBodyRewrite` capability and implement it for HTML/theme/error-page modifiers, including bypass delegation. Also add a regression test to ensure the original response body remains readable and is not closed prematurely when rewrite is blocked. This commit fixeds the "http: read on closed response body" with empty page error happens when body-rewriting middleware (like themed) runs on responses where body rewrite is blocked (e.g. chunked), then the gate restores an already-closed original body.
This commit is contained in:
@@ -13,6 +13,10 @@ type middlewareChain struct {
|
||||
modResps []ResponseModifier
|
||||
}
|
||||
|
||||
type bodyRewriteRequired interface {
|
||||
requiresBodyRewrite() bool
|
||||
}
|
||||
|
||||
// TODO: check conflict or duplicates.
|
||||
func NewMiddlewareChain(name string, chain []*Middleware) *Middleware {
|
||||
chainMid := &middlewareChain{}
|
||||
@@ -59,6 +63,9 @@ func modifyResponseWithBodyRewriteGate(mr ResponseModifier, resp *http.Response)
|
||||
originalBody := resp.Body
|
||||
originalContentLength := resp.ContentLength
|
||||
allowBodyRewrite := canBufferAndModifyResponseBody(responseHeaderForBodyRewriteGate(resp))
|
||||
if !allowBodyRewrite && requiresBodyRewrite(mr) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := mr.modifyResponse(resp); err != nil {
|
||||
return err
|
||||
@@ -87,6 +94,11 @@ func modifyResponseWithBodyRewriteGate(mr ResponseModifier, resp *http.Response)
|
||||
return nil
|
||||
}
|
||||
|
||||
func requiresBodyRewrite(mr ResponseModifier) bool {
|
||||
required, ok := mr.(bodyRewriteRequired)
|
||||
return ok && required.requiresBodyRewrite()
|
||||
}
|
||||
|
||||
func responseHeaderForBodyRewriteGate(resp *http.Response) http.Header {
|
||||
h := resp.Header.Clone()
|
||||
if len(resp.TransferEncoding) > 0 && len(h.Values("Transfer-Encoding")) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user