aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortherealbobo2020-09-11 16:00:19 +0000
committertherealbobo2020-09-11 16:00:19 +0000
commit55806495242672ccf18e6bd96a73956bce61366f (patch)
tree7d9cc232558508daac7bc4136ba65d3b20cade44
parent1e8a07f770c85a2ed6159798d1fb00dbebe08e98 (diff)
added support to http response body decoding
-rw-r--r--connection_streams_controller.go8
-rw-r--r--utils.go41
2 files changed, 48 insertions, 1 deletions
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 {