From 7c0033654fd06c9d31d4d968c8fb38b5d67640de Mon Sep 17 00:00:00 2001 From: lqqyt2423 <974923609@qq.com> Date: Thu, 4 Feb 2021 17:17:51 +0800 Subject: [PATCH] addon/decoder.go --- addon/decoder.go | 13 +++++++++++++ flow/encoding.go | 13 ++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 addon/decoder.go 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) {