diff --git a/README.md b/README.md index 2f94d05..9b1b466 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # GZIP gin's middleware -[![Run Tests](https://github.com/gin-contrib/gzip/actions/workflows/go.yml/badge.svg)](https://github.com/gin-contrib/gzip/actions/workflows/go.yml) +[![Run Tests](https://github.com/lawyzheng/gzip/actions/workflows/go.yml/badge.svg)](https://github.com/lawyzheng/gzip/actions/workflows/go.yml) [![codecov](https://codecov.io/gh/gin-contrib/gzip/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-contrib/gzip) -[![Go Report Card](https://goreportcard.com/badge/github.com/gin-contrib/gzip)](https://goreportcard.com/report/github.com/gin-contrib/gzip) -[![GoDoc](https://godoc.org/github.com/gin-contrib/gzip?status.svg)](https://godoc.org/github.com/gin-contrib/gzip) +[![Go Report Card](https://goreportcard.com/badge/github.com/lawyzheng/gzip)](https://goreportcard.com/report/github.com/lawyzheng/gzip) +[![GoDoc](https://godoc.org/github.com/lawyzheng/gzip?status.svg)](https://godoc.org/github.com/lawyzheng/gzip) [![Join the chat at https://gitter.im/gin-gonic/gin](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gin-gonic/gin) Gin middleware to enable `GZIP` support. @@ -13,13 +13,13 @@ Gin middleware to enable `GZIP` support. Download and install it: ```sh -go get github.com/gin-contrib/gzip +go get github.com/lawyzheng/gzip ``` Import it in your code: ```go -import "github.com/gin-contrib/gzip" +import "github.com/lawyzheng/gzip" ``` Canonical example: @@ -32,7 +32,7 @@ import ( "net/http" "time" - "github.com/gin-contrib/gzip" + "github.com/lawyzheng/gzip" "github.com/gin-gonic/gin" ) @@ -60,7 +60,7 @@ import ( "net/http" "time" - "github.com/gin-contrib/gzip" + "github.com/lawyzheng/gzip" "github.com/gin-gonic/gin" ) @@ -88,7 +88,7 @@ import ( "net/http" "time" - "github.com/gin-contrib/gzip" + "github.com/lawyzheng/gzip" "github.com/gin-gonic/gin" ) @@ -116,7 +116,7 @@ import ( "net/http" "time" - "github.com/gin-contrib/gzip" + "github.com/lawyzheng/gzip" "github.com/gin-gonic/gin" ) diff --git a/example/example.go b/example/example.go index e0a930f..59bb4ed 100644 --- a/example/example.go +++ b/example/example.go @@ -6,8 +6,8 @@ import ( "net/http" "time" - "github.com/gin-contrib/gzip" "github.com/gin-gonic/gin" + "github.com/lawyzheng/gzip" ) func main() { diff --git a/go.mod b/go.mod index e7135a6..f0108e5 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/gin-contrib/gzip +module github.com/lawyzheng/gzip require ( github.com/gin-gonic/gin v1.8.1 diff --git a/gzip.go b/gzip.go index 529c62d..e07fbc2 100644 --- a/gzip.go +++ b/gzip.go @@ -19,7 +19,8 @@ func Gzip(level int, options ...Option) gin.HandlerFunc { type gzipWriter struct { gin.ResponseWriter - writer *gzip.Writer + writer *gzip.Writer + gzipped bool } func (g *gzipWriter) WriteString(s string) (int, error) { @@ -28,12 +29,28 @@ func (g *gzipWriter) WriteString(s string) (int, error) { } func (g *gzipWriter) Write(data []byte) (int, error) { + g.gzipped = g.isGzipped(data) || g.gzipped + if g.gzipped { + return g.ResponseWriter.Write(data) + } + + g.Header().Set("Content-Encoding", "gzip") + g.Header().Set("Vary", "Accept-Encoding") g.Header().Del("Content-Length") return g.writer.Write(data) } // Fix: https://github.com/mholt/caddy/issues/38 func (g *gzipWriter) WriteHeader(code int) { + g.Header().Set("Content-Encoding", "gzip") + g.Header().Set("Vary", "Accept-Encoding") g.Header().Del("Content-Length") g.ResponseWriter.WriteHeader(code) } + +func (g *gzipWriter) isGzipped(input []byte) bool { + if len(input) < 2 { + return false + } + return input[0] == 0x1f && input[1] == 0x8b +} diff --git a/handler.go b/handler.go index 7de33eb..0144228 100644 --- a/handler.go +++ b/handler.go @@ -52,7 +52,7 @@ func (g *gzipHandler) Handle(c *gin.Context) { c.Header("Content-Encoding", "gzip") c.Header("Vary", "Accept-Encoding") - c.Writer = &gzipWriter{c.Writer, gz} + c.Writer = &gzipWriter{c.Writer, gz, false} defer func() { gz.Close() c.Header("Content-Length", fmt.Sprint(c.Writer.Size()))