Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
WeidiDeng committed Dec 20, 2024
1 parent 703aad1 commit bb4f0ae
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions modules/caddyhttp/encode/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ func (rw *responseWriter) Write(p []byte) (int, error) {
}
}

// used to mask ReadFrom method
type writerOnly struct {
io.Writer
}
Expand All @@ -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
}
Expand Down

0 comments on commit bb4f0ae

Please sign in to comment.