aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorWeidiDeng <[email protected]>2024-12-21 05:37:16 +0800
committerGitHub <[email protected]>2024-12-20 21:37:16 +0000
commit5ba1e06fd661aac2cbaab6d4a2ef63a9eb877a46 (patch)
tree67c1cb2071628adc7c735b5646a0e0274c40364d
parentc216cf551dcbd2de1da1b9fe8a7e179b76827753 (diff)
downloadcaddy-master.tar.gz
caddy-master.zip
encode: try to use sendfile when compression is not used (#6749)HEADmaster
* try to use sendfile when encode is enabled * change variable name * add comments * remove connect check since it's done in Write method
-rw-r--r--modules/caddyhttp/encode/encode.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/modules/caddyhttp/encode/encode.go b/modules/caddyhttp/encode/encode.go
index 597772ccc..bea86083a 100644
--- a/modules/caddyhttp/encode/encode.go
+++ b/modules/caddyhttp/encode/encode.go
@@ -347,6 +347,49 @@ func (rw *responseWriter) Write(p []byte) (int, error) {
}
}
+// used to mask ReadFrom method
+type writerOnly struct {
+ io.Writer
+}
+
+// copied from stdlib
+const sniffLen = 512
+
+// ReadFrom will try to use sendfile to copy from the reader to the response writer.
+// It's only used if the response writer implements io.ReaderFrom and the data can't be compressed.
+// It's based on stdlin http1.1 response writer implementation.
+// https://github.com/golang/go/blob/f4e3ec3dbe3b8e04a058d266adf8e048bab563f2/src/net/http/server.go#L586
+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
+ 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
+ }
+ }
+
+ // the response will be compressed, no sendfile support
+ if rw.w != nil {
+ nr, err := io.Copy(rw.w, r)
+ return nr + ns, err
+ }
+ nr, err := rf.ReadFrom(r)
+ return nr + ns, err
+}
+
// Close writes any remaining buffered response and
// deallocates any active resources.
func (rw *responseWriter) Close() error {