supported gzip for req.Header has `Content-Encoding: gzip`

This commit is contained in:
MiskoLee 2017-07-10 21:27:54 +08:00
parent 7ec819deed
commit 29bcd31b27
1 changed files with 13 additions and 1 deletions

View File

@ -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)