ntfy/util/gzip_handler.go

52 lines
1.1 KiB
Go
Raw Normal View History

2022-03-11 02:55:56 +00:00
package util
import (
"compress/gzip"
"io"
"net/http"
"strings"
"sync"
)
// Gzip is a HTTP middleware to transparently compress responses using gzip.
// Original code from https://gist.github.com/CJEnright/bc2d8b8dc0c1389a9feeddb110f822d7 (MIT)
func Gzip(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
next.ServeHTTP(w, r)
return
}
w.Header().Set("Content-Encoding", "gzip")
gz := gzPool.Get().(*gzip.Writer)
defer gzPool.Put(gz)
gz.Reset(w)
defer gz.Close()
r.Header.Del("Accept-Encoding") // prevent double-gzipping
next.ServeHTTP(&gzipResponseWriter{ResponseWriter: w, Writer: gz}, r)
})
}
var gzPool = sync.Pool{
2022-10-01 20:31:48 +00:00
New: func() any {
2022-09-27 16:45:43 +00:00
w := gzip.NewWriter(io.Discard)
2022-03-11 02:55:56 +00:00
return w
},
}
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
func (w *gzipResponseWriter) WriteHeader(status int) {
w.Header().Del("Content-Length")
w.ResponseWriter.WriteHeader(status)
}
func (w *gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}