From 55806495242672ccf18e6bd96a73956bce61366f Mon Sep 17 00:00:00 2001 From: therealbobo Date: Fri, 11 Sep 2020 18:00:19 +0200 Subject: added support to http response body decoding --- connection_streams_controller.go | 8 +++++++- utils.go | 41 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/connection_streams_controller.go b/connection_streams_controller.go index 251e842..000e3d4 100644 --- a/connection_streams_controller.go +++ b/connection_streams_controller.go @@ -110,9 +110,15 @@ func (csc ConnectionStreamsController) GetConnectionPayload(c context.Context, c } size := uint64(end - start) + content := DecodeBytes(serverStream.Payload[start:end], format.Format) + // check if is encoded + if format.Format == "default" { + content = DecodeHttpResponse(content) + } + payload = Payload{ FromClient: false, - Content: DecodeBytes(serverStream.Payload[start:end], format.Format), + Content: content, Index: start, Timestamp: serverStream.BlocksTimestamps[serverBlocksIndex], IsRetransmitted: serverStream.BlocksLoss[serverBlocksIndex], diff --git a/utils.go b/utils.go index a14fdca..e222b17 100644 --- a/utils.go +++ b/utils.go @@ -13,6 +13,12 @@ import ( "net" "os" "time" + //"net/textproto" + "net/http" + "bufio" + "strings" + "io/ioutil" + "compress/gzip" ) func Sha256Sum(fileName string) (string, error) { @@ -108,6 +114,41 @@ func DecodeBytes(buffer []byte, format string) string { } } +func DecodeHttpResponse(raw string) string { + var header string + trailer := "\n" + reader := bufio.NewReader(strings.NewReader(raw)) + resp,err := http.ReadResponse(reader, &http.Request{}) + if err != nil{ + log.Info("Reading response: ",resp) + return raw + trailer + } + + defer resp.Body.Close() + + if resp.StatusCode == http.StatusOK { + var bodyReader io.ReadCloser + switch resp.Header.Get("Content-Encoding") { + case "gzip": + bodyReader, err = gzip.NewReader(resp.Body) + if err != nil { + log.Error("Gunzipping body: ",err) + } + header = "\n[==== GUNZIPPED ====]\n" + trailer = "\n[===================]\n" + defer bodyReader.Close() + default: + bodyReader = resp.Body + } + body, err := ioutil.ReadAll(bodyReader) + if err != nil{ + log.Error("Reading body: ",err) + } + return raw + header + string(body) + trailer + } + return raw + trailer +} + func CopyFile(dst, src string) error { in, err := os.Open(src) if err != nil { -- cgit v1.2.3-70-g09d2