diff --git a/addon/decoder.go b/addon/decoder.go new file mode 100644 index 0000000..d7c6a2a --- /dev/null +++ b/addon/decoder.go @@ -0,0 +1,13 @@ +package addon + +import "github.com/lqqyt2423/go-mitmproxy/flow" + +// decode content-encoding then respond to client + +type Decoder struct { + Base +} + +func (d *Decoder) Response(f *flow.Flow) { + f.Response.ReplaceToDecodedBody() +} diff --git a/flow/encoding.go b/flow/encoding.go index 31d090e..f60dc09 100644 --- a/flow/encoding.go +++ b/flow/encoding.go @@ -6,6 +6,7 @@ import ( "compress/gzip" "errors" "io" + "strconv" "strings" "github.com/andybalholm/brotli" @@ -70,10 +71,16 @@ func (r *Response) DecodedBody() ([]byte, error) { return r.decodedBody, nil } -// 当 Response.Body 替换为解压的内容时调用 -func (r *Response) RemoveEncodingHeader() { +func (r *Response) ReplaceToDecodedBody() { + body, err := r.DecodedBody() + if err != nil || body == nil { + return + } + + r.Body = body r.Header.Del("Content-Encoding") - r.Header.Del("Content-Length") + r.Header.Set("Content-Length", strconv.Itoa(len(body))) + r.Header.Del("Transfer-Encoding") } func Decode(enc string, body []byte) ([]byte, error) {