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

gzip decompress-only supports #90

Open
wants to merge 1 commit 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
72 changes: 72 additions & 0 deletions gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,75 @@ func TestDecompressGzipWithIncorrectData(t *testing.T) {

assert.Equal(t, http.StatusBadRequest, w.Code)
}

func TestDecompressOnly(t *testing.T) {
buf := &bytes.Buffer{}
gz, _ := gzip.NewWriterLevel(buf, gzip.DefaultCompression)
if _, err := gz.Write([]byte(testResponse)); err != nil {
gz.Close()
t.Fatal(err)
}
gz.Close()

req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", buf)
req.Header.Add("Content-Encoding", "gzip")

router := gin.New()
router.Use(Gzip(NoCompression, WithDecompressOnly(true), WithDecompressFn(DefaultDecompressHandle)))
router.POST("/", func(c *gin.Context) {
if v := c.Request.Header.Get("Content-Encoding"); v != "" {
t.Errorf("unexpected `Content-Encoding`: %s header", v)
}
if v := c.Request.Header.Get("Content-Length"); v != "" {
t.Errorf("unexpected `Content-Length`: %s header", v)
}
data, err := c.GetRawData()
if err != nil {
t.Fatal(err)
}
c.Data(200, "text/plain", data)
})

w := httptest.NewRecorder()
router.ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
assert.Equal(t, "", w.Header().Get("Vary"))
assert.Equal(t, testResponse, w.Body.String())
assert.Equal(t, "", w.Header().Get("Content-Length"))
}

func TestGzipWithDecompressOnly(t *testing.T) {
buf := &bytes.Buffer{}
gz, _ := gzip.NewWriterLevel(buf, gzip.DefaultCompression)
if _, err := gz.Write([]byte(testResponse)); err != nil {
gz.Close()
t.Fatal(err)
}
gz.Close()

req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", buf)
req.Header.Add("Content-Encoding", "gzip")
req.Header.Add("Accept-Encoding", "gzip")

r := gin.New()
r.Use(Gzip(NoCompression, WithDecompressOnly(true), WithDecompressFn(DefaultDecompressHandle)))
r.POST("/", func(c *gin.Context) {
assert.Equal(t, c.Request.Header.Get("Content-Encoding"), "")
assert.Equal(t, c.Request.Header.Get("Content-Length"), "")
body, err := c.GetRawData()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, testResponse, string(body))
c.String(200, testResponse)
})

w := httptest.NewRecorder()
r.ServeHTTP(w, req)

assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Header().Get("Content-Encoding"), "")
assert.Equal(t, w.Body.String(), testResponse)
}
2 changes: 1 addition & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (g *gzipHandler) Handle(c *gin.Context) {
fn(c)
}

if !g.shouldCompress(c.Request) {
if g.DecompressOnly || !g.shouldCompress(c.Request) {
return
}

Expand Down
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Options struct {
ExcludedPaths ExcludedPaths
ExcludedPathesRegexs ExcludedPathesRegexs
DecompressFn func(c *gin.Context)
DecompressOnly bool
}

type Option func(*Options)
Expand Down Expand Up @@ -51,6 +52,13 @@ func WithDecompressFn(decompressFn func(c *gin.Context)) Option {
}
}

// disable compression, only decompress incoming request
func WithDecompressOnly(decompressOnly bool) Option {
return func(o *Options) {
o.DecompressOnly = decompressOnly
}
}

// Using map for better lookup performance
type ExcludedExtensions map[string]bool

Expand Down