content-encoding deflate support

addon-dailer
lqqyt2423 4 years ago
parent 44d7815e68
commit 9b0ebfaa79

@ -2,6 +2,7 @@ package flow
import ( import (
"bytes" "bytes"
"compress/flate"
"compress/gzip" "compress/gzip"
"errors" "errors"
"io" "io"
@ -77,24 +78,32 @@ func (r *Response) RemoveEncodingHeader() {
func Decode(enc string, body []byte) ([]byte, error) { func Decode(enc string, body []byte) ([]byte, error) {
if enc == "gzip" { if enc == "gzip" {
zr, err := gzip.NewReader(bytes.NewReader(body)) dreader, err := gzip.NewReader(bytes.NewReader(body))
if err != nil { if err != nil {
return nil, err return nil, err
} }
buf := bytes.NewBuffer(make([]byte, 0)) buf := bytes.NewBuffer(make([]byte, 0))
_, err = io.Copy(buf, zr) _, err = io.Copy(buf, dreader)
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = zr.Close() err = dreader.Close()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return buf.Bytes(), nil return buf.Bytes(), nil
} else if enc == "br" { } else if enc == "br" {
brr := brotli.NewReader(bytes.NewReader(body)) dreader := brotli.NewReader(bytes.NewReader(body))
buf := bytes.NewBuffer(make([]byte, 0)) 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 { if err != nil {
return nil, err return nil, err
} }

Loading…
Cancel
Save