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 --- utils.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'utils.go') 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 From ad7e9226bac7524462755f5916b076408ebd86cb Mon Sep 17 00:00:00 2001 From: therealbobo Date: Sat, 12 Sep 2020 17:40:45 +0200 Subject: better decode handling --- utils.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 13 deletions(-) (limited to 'utils.go') diff --git a/utils.go b/utils.go index e222b17..b07244d 100644 --- a/utils.go +++ b/utils.go @@ -13,7 +13,6 @@ import ( "net" "os" "time" - //"net/textproto" "net/http" "bufio" "strings" @@ -114,19 +113,52 @@ func DecodeBytes(buffer []byte, format string) string { } } +func ReadRequest(raw string) http.Request { + reader := bufio.NewReader(strings.NewReader(raw)) + req,err := http.ReadRequest(reader) + if err != nil{ + log.Info("Reading request: ",req) + return http.Request{} + } + return *req +} + +func GetHeader(raw string) string{ + tmp := strings.Split(raw,"\r\n") + end := len(tmp) + for i, line := range tmp{ + if line == ""{ + end = i + break + } + } + return strings.Join(tmp[:end],"\r\n") +} + +func GetBody(raw string) string{ + tmp := strings.Split(raw,"\r\n") + start := 0 + for i, line := range tmp{ + if line == ""{ + start = i + 2 + break + } + } + return strings.Join(tmp[start:],"\r\n") +} + func DecodeHttpResponse(raw string) string { - var header string - trailer := "\n" + body := []byte{} reader := bufio.NewReader(strings.NewReader(raw)) resp,err := http.ReadResponse(reader, &http.Request{}) if err != nil{ log.Info("Reading response: ",resp) - return raw + trailer + return "" } defer resp.Body.Close() - if resp.StatusCode == http.StatusOK { + if resp.StatusCode >= 200 && resp.StatusCode < 300 { var bodyReader io.ReadCloser switch resp.Header.Get("Content-Encoding") { case "gzip": @@ -134,19 +166,28 @@ func DecodeHttpResponse(raw string) string { if err != nil { log.Error("Gunzipping body: ",err) } - header = "\n[==== GUNZIPPED ====]\n" - trailer = "\n[===================]\n" defer bodyReader.Close() + body, err = ioutil.ReadAll(bodyReader) + if err != nil{ + log.Error("Reading gzipped body: ",err) + // if the response is malformed + // or the connection is closed + fallbackReader, _ := gzip.NewReader(strings.NewReader(GetBody(raw))) + body, err = ioutil.ReadAll(fallbackReader) + if err != nil{ + log.Error(string(body)) + } + } default: bodyReader = resp.Body + body, err = ioutil.ReadAll(bodyReader) + if err != nil{ + log.Error("Reading body: ",err) + body = []byte(GetBody(raw)) + } } - body, err := ioutil.ReadAll(bodyReader) - if err != nil{ - log.Error("Reading body: ",err) - } - return raw + header + string(body) + trailer } - return raw + trailer + return GetHeader(raw) + "\r\n\r\n"+ string(body) } func CopyFile(dst, src string) error { -- cgit v1.2.3-70-g09d2