From 29bcd31b27c822043b9d1b42f348483975061cd9 Mon Sep 17 00:00:00 2001 From: MiskoLee Date: Mon, 10 Jul 2017 21:27:54 +0800 Subject: [PATCH] supported gzip for req.Header has `Content-Encoding: gzip` --- context/input.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/context/input.go b/context/input.go index aaf97e85..2c53c601 100644 --- a/context/input.go +++ b/context/input.go @@ -16,6 +16,7 @@ package context import ( "bytes" + "compress/gzip" "errors" "io" "io/ioutil" @@ -350,8 +351,19 @@ func (input *BeegoInput) CopyBody(MaxMemory int64) []byte { if input.Context.Request.Body == nil { return []byte{} } + + var requestbody []byte safe := &io.LimitedReader{R: input.Context.Request.Body, N: MaxMemory} - requestbody, _ := ioutil.ReadAll(safe) + if input.Header("Content-Encoding") == "gzip" { + reader, err := gzip.NewReader(safe) + if err != nil { + return nil + } + requestbody, _ = ioutil.ReadAll(reader) + } else { + requestbody, _ = ioutil.ReadAll(safe) + } + input.Context.Request.Body.Close() bf := bytes.NewBuffer(requestbody) input.Context.Request.Body = http.MaxBytesReader(input.Context.ResponseWriter, ioutil.NopCloser(bf), MaxMemory)