From f3aead0e4df57716d525f3a25fb11f70bc72b20a Mon Sep 17 00:00:00 2001 From: WeidiDeng Date: Wed, 2 Oct 2024 01:19:03 +0800 Subject: [PATCH] http: ReponseWriter prefer ReadFrom if available (#6565) Co-authored-by: Matt Holt --- modules/caddyhttp/responsewriter.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/caddyhttp/responsewriter.go b/modules/caddyhttp/responsewriter.go index 808d2de3..3c0f89d0 100644 --- a/modules/caddyhttp/responsewriter.go +++ b/modules/caddyhttp/responsewriter.go @@ -42,9 +42,13 @@ func (rww *ResponseWriterWrapper) Push(target string, opts *http.PushOptions) er return ErrNotImplemented } -// ReadFrom implements io.ReaderFrom. It simply calls io.Copy, -// which uses io.ReaderFrom if available. +// ReadFrom implements io.ReaderFrom. It retries to use io.ReaderFrom if available, +// then fallback to io.Copy. +// see: https://github.com/caddyserver/caddy/issues/6546 func (rww *ResponseWriterWrapper) ReadFrom(r io.Reader) (n int64, err error) { + if rf, ok := rww.ResponseWriter.(io.ReaderFrom); ok { + return rf.ReadFrom(r) + } return io.Copy(rww.ResponseWriter, r) }