From 9b0ebfaa79cbd7708c1e696c9c384f98354c73d6 Mon Sep 17 00:00:00 2001 From: lqqyt2423 <974923609@qq.com> Date: Thu, 4 Feb 2021 14:33:15 +0800 Subject: [PATCH] content-encoding deflate support --- flow/encoding.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/flow/encoding.go b/flow/encoding.go index f185f69..769a63f 100644 --- a/flow/encoding.go +++ b/flow/encoding.go @@ -2,6 +2,7 @@ package flow import ( "bytes" + "compress/flate" "compress/gzip" "errors" "io" @@ -77,24 +78,32 @@ func (r *Response) RemoveEncodingHeader() { func Decode(enc string, body []byte) ([]byte, error) { if enc == "gzip" { - zr, err := gzip.NewReader(bytes.NewReader(body)) + dreader, err := gzip.NewReader(bytes.NewReader(body)) if err != nil { return nil, err } buf := bytes.NewBuffer(make([]byte, 0)) - _, err = io.Copy(buf, zr) + _, err = io.Copy(buf, dreader) if err != nil { return nil, err } - err = zr.Close() + err = dreader.Close() if err != nil { return nil, err } return buf.Bytes(), nil } else if enc == "br" { - brr := brotli.NewReader(bytes.NewReader(body)) + dreader := brotli.NewReader(bytes.NewReader(body)) buf := bytes.NewBuffer(make([]byte, 0)) - _, err := io.Copy(buf, brr) + _, err := io.Copy(buf, dreader) + if err != nil { + return nil, err + } + return buf.Bytes(), nil + } else if enc == "deflate" { + dreader := flate.NewReader(bytes.NewReader(body)) + buf := bytes.NewBuffer(make([]byte, 0)) + _, err := io.Copy(buf, dreader) if err != nil { return nil, err }