mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-18 07:00:00 +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:
@@ -1,6 +1,7 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -30,6 +31,29 @@ type testResponseRewrite struct {
|
||||
Body string `json:"body"`
|
||||
}
|
||||
|
||||
type closeSensitiveBody struct {
|
||||
data []byte
|
||||
offset int
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (b *closeSensitiveBody) Read(p []byte) (int, error) {
|
||||
if b.closed {
|
||||
return 0, errors.New("http: read on closed response body")
|
||||
}
|
||||
if b.offset >= len(b.data) {
|
||||
return 0, io.EOF
|
||||
}
|
||||
n := copy(p, b.data[b.offset:])
|
||||
b.offset += n
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (b *closeSensitiveBody) Close() error {
|
||||
b.closed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t testResponseRewrite) modifyResponse(resp *http.Response) error {
|
||||
resp.StatusCode = t.StatusCode
|
||||
resp.Header.Set(t.HeaderKey, t.HeaderVal)
|
||||
@@ -226,3 +250,34 @@ func TestMiddlewareResponseRewriteGateServeHTTP(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMiddlewareResponseRewriteGateSkipsBodyRewriterWhenRewriteBlocked(t *testing.T) {
|
||||
originalBody := &closeSensitiveBody{
|
||||
data: []byte("<html><body>original</body></html>"),
|
||||
}
|
||||
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
|
||||
resp := &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Header: http.Header{
|
||||
"Content-Type": []string{"text/html; charset=utf-8"},
|
||||
"Transfer-Encoding": []string{"chunked"},
|
||||
},
|
||||
Body: originalBody,
|
||||
ContentLength: -1,
|
||||
TransferEncoding: []string{"chunked"},
|
||||
Request: req,
|
||||
}
|
||||
|
||||
themedMid, err := Themed.New(OptionsRaw{
|
||||
"theme": DarkTheme,
|
||||
})
|
||||
expect.NoError(t, err)
|
||||
|
||||
respMod, ok := themedMid.impl.(ResponseModifier)
|
||||
expect.True(t, ok)
|
||||
expect.NoError(t, modifyResponseWithBodyRewriteGate(respMod, resp))
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
expect.NoError(t, err)
|
||||
expect.Equal(t, string(data), "<html><body>original</body></html>")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user