aboutsummaryrefslogtreecommitdiff
path: root/utils.go
diff options
context:
space:
mode:
authorEmiliano Ciavatta2020-09-14 20:17:39 +0000
committerGitHub2020-09-14 20:17:39 +0000
commit4f70dbfb5519ae2a6e68869ecba0a9e4cfb3013b (patch)
tree8d79b5cdc99bceee4282327440fe961a79211283 /utils.go
parent1e8a07f770c85a2ed6159798d1fb00dbebe08e98 (diff)
parente7c2428e24d0fc6aebf524130531800a89332829 (diff)
Merge pull request #13 from therealbobo/feature/decode-gzip
added support to http response body decoding
Diffstat (limited to 'utils.go')
-rw-r--r--utils.go82
1 files changed, 82 insertions, 0 deletions
diff --git a/utils.go b/utils.go
index a14fdca..b07244d 100644
--- a/utils.go
+++ b/utils.go
@@ -13,6 +13,11 @@ import (
"net"
"os"
"time"
+ "net/http"
+ "bufio"
+ "strings"
+ "io/ioutil"
+ "compress/gzip"
)
func Sha256Sum(fileName string) (string, error) {
@@ -108,6 +113,83 @@ 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 {
+ body := []byte{}
+ reader := bufio.NewReader(strings.NewReader(raw))
+ resp,err := http.ReadResponse(reader, &http.Request{})
+ if err != nil{
+ log.Info("Reading response: ",resp)
+ return ""
+ }
+
+ defer resp.Body.Close()
+
+ if resp.StatusCode >= 200 && resp.StatusCode < 300 {
+ 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)
+ }
+ 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))
+ }
+ }
+ }
+ return GetHeader(raw) + "\r\n\r\n"+ string(body)
+}
+
func CopyFile(dst, src string) error {
in, err := os.Open(src)
if err != nil {