Skip to content

Commit

Permalink
feat: gzip decompress-only supports (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
yzqzss authored Jan 12, 2025
1 parent aa0f078 commit 969f96e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
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]struct{}

Expand Down

0 comments on commit 969f96e

Please sign in to comment.