Skip to content

Commit 969f96e

Browse files
authored
feat: gzip decompress-only supports (#90)
1 parent aa0f078 commit 969f96e

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

gzip_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,75 @@ func TestDecompressGzipWithIncorrectData(t *testing.T) {
247247

248248
assert.Equal(t, http.StatusBadRequest, w.Code)
249249
}
250+
251+
func TestDecompressOnly(t *testing.T) {
252+
buf := &bytes.Buffer{}
253+
gz, _ := gzip.NewWriterLevel(buf, gzip.DefaultCompression)
254+
if _, err := gz.Write([]byte(testResponse)); err != nil {
255+
gz.Close()
256+
t.Fatal(err)
257+
}
258+
gz.Close()
259+
260+
req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", buf)
261+
req.Header.Add("Content-Encoding", "gzip")
262+
263+
router := gin.New()
264+
router.Use(Gzip(NoCompression, WithDecompressOnly(true), WithDecompressFn(DefaultDecompressHandle)))
265+
router.POST("/", func(c *gin.Context) {
266+
if v := c.Request.Header.Get("Content-Encoding"); v != "" {
267+
t.Errorf("unexpected `Content-Encoding`: %s header", v)
268+
}
269+
if v := c.Request.Header.Get("Content-Length"); v != "" {
270+
t.Errorf("unexpected `Content-Length`: %s header", v)
271+
}
272+
data, err := c.GetRawData()
273+
if err != nil {
274+
t.Fatal(err)
275+
}
276+
c.Data(200, "text/plain", data)
277+
})
278+
279+
w := httptest.NewRecorder()
280+
router.ServeHTTP(w, req)
281+
282+
assert.Equal(t, http.StatusOK, w.Code)
283+
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
284+
assert.Equal(t, "", w.Header().Get("Vary"))
285+
assert.Equal(t, testResponse, w.Body.String())
286+
assert.Equal(t, "", w.Header().Get("Content-Length"))
287+
}
288+
289+
func TestGzipWithDecompressOnly(t *testing.T) {
290+
buf := &bytes.Buffer{}
291+
gz, _ := gzip.NewWriterLevel(buf, gzip.DefaultCompression)
292+
if _, err := gz.Write([]byte(testResponse)); err != nil {
293+
gz.Close()
294+
t.Fatal(err)
295+
}
296+
gz.Close()
297+
298+
req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", buf)
299+
req.Header.Add("Content-Encoding", "gzip")
300+
req.Header.Add("Accept-Encoding", "gzip")
301+
302+
r := gin.New()
303+
r.Use(Gzip(NoCompression, WithDecompressOnly(true), WithDecompressFn(DefaultDecompressHandle)))
304+
r.POST("/", func(c *gin.Context) {
305+
assert.Equal(t, c.Request.Header.Get("Content-Encoding"), "")
306+
assert.Equal(t, c.Request.Header.Get("Content-Length"), "")
307+
body, err := c.GetRawData()
308+
if err != nil {
309+
t.Fatal(err)
310+
}
311+
assert.Equal(t, testResponse, string(body))
312+
c.String(200, testResponse)
313+
})
314+
315+
w := httptest.NewRecorder()
316+
r.ServeHTTP(w, req)
317+
318+
assert.Equal(t, w.Code, 200)
319+
assert.Equal(t, w.Header().Get("Content-Encoding"), "")
320+
assert.Equal(t, w.Body.String(), testResponse)
321+
}

handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (g *gzipHandler) Handle(c *gin.Context) {
4141
fn(c)
4242
}
4343

44-
if !g.shouldCompress(c.Request) {
44+
if g.DecompressOnly || !g.shouldCompress(c.Request) {
4545
return
4646
}
4747

options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Options struct {
2323
ExcludedPaths ExcludedPaths
2424
ExcludedPathesRegexs ExcludedPathesRegexs
2525
DecompressFn func(c *gin.Context)
26+
DecompressOnly bool
2627
}
2728

2829
type Option func(*Options)
@@ -51,6 +52,13 @@ func WithDecompressFn(decompressFn func(c *gin.Context)) Option {
5152
}
5253
}
5354

55+
// disable compression, only decompress incoming request
56+
func WithDecompressOnly(decompressOnly bool) Option {
57+
return func(o *Options) {
58+
o.DecompressOnly = decompressOnly
59+
}
60+
}
61+
5462
// Using map for better lookup performance
5563
type ExcludedExtensions map[string]struct{}
5664

0 commit comments

Comments
 (0)