From 2f96485b9aaabcb501d7d3ed5106b75dfeed9a5f Mon Sep 17 00:00:00 2001 From: LawyZHENG Date: Thu, 20 Jan 2022 10:53:48 +0800 Subject: [PATCH 1/7] fix: try to fix replicate gzip --- handler.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/handler.go b/handler.go index 7de33eb..2d90437 100644 --- a/handler.go +++ b/handler.go @@ -45,11 +45,14 @@ func (g *gzipHandler) Handle(c *gin.Context) { return } + if c.Writer.Header().Get("Content-Encoding") == "gzip" { + return + } + gz := g.gzPool.Get().(*gzip.Writer) defer g.gzPool.Put(gz) defer gz.Reset(ioutil.Discard) gz.Reset(c.Writer) - c.Header("Content-Encoding", "gzip") c.Header("Vary", "Accept-Encoding") c.Writer = &gzipWriter{c.Writer, gz} From 015ffb4540359b9971de09e1f061d555ba7a033f Mon Sep 17 00:00:00 2001 From: LawyZHENG Date: Thu, 20 Jan 2022 11:16:01 +0800 Subject: [PATCH 2/7] Revert "fix: try to fix replicate gzip" This reverts commit 2f96485b9aaabcb501d7d3ed5106b75dfeed9a5f. --- handler.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/handler.go b/handler.go index 2d90437..7de33eb 100644 --- a/handler.go +++ b/handler.go @@ -45,14 +45,11 @@ func (g *gzipHandler) Handle(c *gin.Context) { return } - if c.Writer.Header().Get("Content-Encoding") == "gzip" { - return - } - gz := g.gzPool.Get().(*gzip.Writer) defer g.gzPool.Put(gz) defer gz.Reset(ioutil.Discard) gz.Reset(c.Writer) + c.Header("Content-Encoding", "gzip") c.Header("Vary", "Accept-Encoding") c.Writer = &gzipWriter{c.Writer, gz} From e8e72f9480206a182b7b0c35ede5d643fc4a7226 Mon Sep 17 00:00:00 2001 From: LawyZHENG Date: Thu, 20 Jan 2022 17:04:19 +0800 Subject: [PATCH 3/7] fix: try to fix double compress --- gzip.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/gzip.go b/gzip.go index 529c62d..9ec57f8 100644 --- a/gzip.go +++ b/gzip.go @@ -28,6 +28,10 @@ func (g *gzipWriter) WriteString(s string) (int, error) { } func (g *gzipWriter) Write(data []byte) (int, error) { + if isGzipped(data) { + return g.ResponseWriter.Write(data) + } + g.Header().Del("Content-Length") return g.writer.Write(data) } @@ -37,3 +41,10 @@ func (g *gzipWriter) WriteHeader(code int) { g.Header().Del("Content-Length") g.ResponseWriter.WriteHeader(code) } + +func isGzipped(input []byte) bool { + if len(input) < 2 { + return false + } + return input[0] == 0x1f && input[1] == 0x8b +} From 8cafbfccbefe86f02a8fe8a35629a2a8a88f1d15 Mon Sep 17 00:00:00 2001 From: LawyZHENG Date: Fri, 21 Jan 2022 10:35:40 +0800 Subject: [PATCH 4/7] fix: gzip duplicated --- gzip.go | 10 +++++++--- handler.go | 4 +--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/gzip.go b/gzip.go index 9ec57f8..fc44714 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,10 +29,13 @@ func (g *gzipWriter) WriteString(s string) (int, error) { } func (g *gzipWriter) Write(data []byte) (int, error) { - if isGzipped(data) { + g.gzipped = g.isGzipped(data) || g.gzipped + if g.isGzipped(data) { 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) } @@ -42,7 +46,7 @@ func (g *gzipWriter) WriteHeader(code int) { g.ResponseWriter.WriteHeader(code) } -func isGzipped(input []byte) bool { +func (g *gzipWriter) isGzipped(input []byte) bool { if len(input) < 2 { return false } diff --git a/handler.go b/handler.go index 7de33eb..abf8b33 100644 --- a/handler.go +++ b/handler.go @@ -50,9 +50,7 @@ func (g *gzipHandler) Handle(c *gin.Context) { defer gz.Reset(ioutil.Discard) gz.Reset(c.Writer) - 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())) From 7eef04d130e67e6774fe1bf93aae59e2e2359353 Mon Sep 17 00:00:00 2001 From: LawyZHENG Date: Fri, 21 Jan 2022 10:39:58 +0800 Subject: [PATCH 5/7] fix: write multiple times bug --- gzip.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gzip.go b/gzip.go index fc44714..85f571d 100644 --- a/gzip.go +++ b/gzip.go @@ -30,7 +30,7 @@ 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.isGzipped(data) { + if g.gzipped { return g.ResponseWriter.Write(data) } From 5f42a0204014daf5c2a5b87eb9d44aa46ef041ac Mon Sep 17 00:00:00 2001 From: LawyZHENG Date: Fri, 21 Jan 2022 15:39:16 +0800 Subject: [PATCH 6/7] fix: sometimes leaks the headers --- gzip.go | 2 ++ handler.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/gzip.go b/gzip.go index 85f571d..e07fbc2 100644 --- a/gzip.go +++ b/gzip.go @@ -42,6 +42,8 @@ func (g *gzipWriter) Write(data []byte) (int, error) { // 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) } diff --git a/handler.go b/handler.go index abf8b33..0144228 100644 --- a/handler.go +++ b/handler.go @@ -50,6 +50,8 @@ func (g *gzipHandler) Handle(c *gin.Context) { defer gz.Reset(ioutil.Discard) gz.Reset(c.Writer) + c.Header("Content-Encoding", "gzip") + c.Header("Vary", "Accept-Encoding") c.Writer = &gzipWriter{c.Writer, gz, false} defer func() { gz.Close() From 1be31611a4d4b4986c7291c373e001666ffb197c Mon Sep 17 00:00:00 2001 From: LawyZHENG Date: Wed, 23 Nov 2022 12:02:01 +0800 Subject: [PATCH 7/7] change module name --- README.md | 18 +++++++++--------- example/example.go | 2 +- go.mod | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) 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