gzip_handler.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package util
  2. import (
  3. "compress/gzip"
  4. "io"
  5. "net/http"
  6. "strings"
  7. "sync"
  8. )
  9. // Gzip is a HTTP middleware to transparently compress responses using gzip.
  10. // Original code from https://gist.github.com/CJEnright/bc2d8b8dc0c1389a9feeddb110f822d7 (MIT)
  11. func Gzip(next http.Handler) http.Handler {
  12. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  13. if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  14. next.ServeHTTP(w, r)
  15. return
  16. }
  17. w.Header().Set("Content-Encoding", "gzip")
  18. gz := gzPool.Get().(*gzip.Writer)
  19. defer gzPool.Put(gz)
  20. gz.Reset(w)
  21. defer gz.Close()
  22. r.Header.Del("Accept-Encoding") // prevent double-gzipping
  23. next.ServeHTTP(&gzipResponseWriter{ResponseWriter: w, Writer: gz}, r)
  24. })
  25. }
  26. var gzPool = sync.Pool{
  27. New: func() any {
  28. w := gzip.NewWriter(io.Discard)
  29. return w
  30. },
  31. }
  32. type gzipResponseWriter struct {
  33. io.Writer
  34. http.ResponseWriter
  35. }
  36. func (w *gzipResponseWriter) WriteHeader(status int) {
  37. w.Header().Del("Content-Length")
  38. w.ResponseWriter.WriteHeader(status)
  39. }
  40. func (w *gzipResponseWriter) Write(b []byte) (int, error) {
  41. return w.Writer.Write(b)
  42. }