Add WithMaxResponseBody option to prevent client-side OOM
Wraps response body with io.LimitedReader when configured, preventing unbounded reads from io.ReadAll in Response.Bytes(). Protects against upstream services returning unexpectedly large responses. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
15
response.go
15
response.go
@@ -97,3 +97,18 @@ func (r *Response) BodyReader() io.Reader {
|
||||
}
|
||||
return r.Body
|
||||
}
|
||||
|
||||
// limitedReadCloser wraps an io.LimitedReader with a separate Closer
|
||||
// so the original body can be closed.
|
||||
type limitedReadCloser struct {
|
||||
R io.LimitedReader
|
||||
C io.Closer
|
||||
}
|
||||
|
||||
func (l *limitedReadCloser) Read(p []byte) (int, error) {
|
||||
return l.R.Read(p)
|
||||
}
|
||||
|
||||
func (l *limitedReadCloser) Close() error {
|
||||
return l.C.Close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user