gzip.go 777 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package gzip
  2. import (
  3. "compress/gzip"
  4. "github.com/gin-gonic/gin"
  5. )
  6. const (
  7. BestCompression = gzip.BestCompression
  8. BestSpeed = gzip.BestSpeed
  9. DefaultCompression = gzip.DefaultCompression
  10. NoCompression = gzip.NoCompression
  11. )
  12. func Gzip(level int, options ...Option) gin.HandlerFunc {
  13. return newGzipHandler(level, options...).Handle
  14. }
  15. type gzipWriter struct {
  16. gin.ResponseWriter
  17. writer *gzip.Writer
  18. }
  19. func (g *gzipWriter) WriteString(s string) (int, error) {
  20. return g.writer.Write([]byte(s))
  21. }
  22. func (g *gzipWriter) Write(data []byte) (int, error) {
  23. return g.writer.Write(data)
  24. }
  25. // Fix: https://github.com/mholt/caddy/issues/38
  26. func (g *gzipWriter) WriteHeader(code int) {
  27. g.Header().Del("Content-Length")
  28. g.ResponseWriter.WriteHeader(code)
  29. }