Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Double Gzip #71

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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:
Expand All @@ -32,7 +32,7 @@ import (
"net/http"
"time"

"github.com/gin-contrib/gzip"
"github.com/lawyzheng/gzip"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -60,7 +60,7 @@ import (
"net/http"
"time"

"github.com/gin-contrib/gzip"
"github.com/lawyzheng/gzip"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -88,7 +88,7 @@ import (
"net/http"
"time"

"github.com/gin-contrib/gzip"
"github.com/lawyzheng/gzip"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -116,7 +116,7 @@ import (
"net/http"
"time"

"github.com/gin-contrib/gzip"
"github.com/lawyzheng/gzip"
"github.com/gin-gonic/gin"
)

Expand Down
2 changes: 1 addition & 1 deletion example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"net/http"
"time"

"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/lawyzheng/gzip"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/gin-contrib/gzip
module github.com/lawyzheng/gzip

require (
github.com/gin-gonic/gin v1.8.1
Expand Down
19 changes: 18 additions & 1 deletion gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at the func (z *Reader) readHeader() (hdr Header, err error) on compress/gzip/gunzip.go file it should include the deflate option too, hence this should do it:

var gzipHeader = []byte{0x1f, 0x8b, 0x08}

func IsGzip(b []byte) bool {
        if len(b) < 3 {
                return false
        }

        return bytes.Equal(b[0:3], gzipHeader)
}

}
2 changes: 1 addition & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down