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