nytimes / gziphandler

Go middleware to gzip HTTP responses
https://godoc.org/github.com/NYTimes/gziphandler
Apache License 2.0
868 stars 129 forks source link

missing io.ReaderFrom for http1.1 #35

Open romainmenke opened 7 years ago

romainmenke commented 7 years ago

Validation with https://middleware.vet#github.com/NYTimes/gziphandler shows that GzipResponseWriter is not implementing io.ReaderFrom for http1.1

Is it possible to implement these only for http1.1 and not for http2 while still reusing response writers?

tmthrgd commented 7 years ago

The io.ReaderFrom is implemented by *net/http.response for HTTP 1.x connections. If the underlying connection is a TCPConn then it uses the optimised sendfile system call (see (*TCPConn).readFrom) which, according to the man page:

sendfile() copies data between one file descriptor and another. Because this copying is done within the kernel, sendfile() is more efficient than the combination of read(2) and write(2), which would require transferring data to and from user space.

Unfortunately this optimisation is not possible because gziphandler has to gzip the response body. There is just no straight path from file descriptor to file descriptor.