From bb4f0aec8d1d096566071871330da117c94533a7 Mon Sep 17 00:00:00 2001 From: WeidiDeng Date: Fri, 20 Dec 2024 09:10:37 +0800 Subject: [PATCH] add comments --- modules/caddyhttp/encode/encode.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/modules/caddyhttp/encode/encode.go b/modules/caddyhttp/encode/encode.go index 4a53db07941..01c8f8ded42 100644 --- a/modules/caddyhttp/encode/encode.go +++ b/modules/caddyhttp/encode/encode.go @@ -347,6 +347,7 @@ func (rw *responseWriter) Write(p []byte) (int, error) { } } +// used to mask ReadFrom method type writerOnly struct { io.Writer } @@ -368,14 +369,19 @@ func (rw *responseWriter) ReadFrom(r io.Reader) (int64, error) { rf, ok := rw.ResponseWriter.(io.ReaderFrom) // sendfile can't be used anyway if !ok { + // mask ReadFrom to avoid infinite recursion return io.Copy(writerOnly{rw}, r) } var ns int64 // try to sniff the content type and determine if the response should be compressed if !rw.wroteHeader && rw.config.MinLength > 0 { - var err error - ns, err = io.Copy(writerOnly{rw}, io.LimitReader(r, sniffLen)) + var ( + err error + buf [sniffLen]byte + ) + // mask ReadFrom to let Write determine if the response should be compressed + ns, err = io.CopyBuffer(writerOnly{rw}, io.LimitReader(r, sniffLen), buf[:]) if err != nil || ns < sniffLen { return ns, err }