diff options
author | Emiliano Ciavatta | 2020-09-16 15:57:23 +0000 |
---|---|---|
committer | Emiliano Ciavatta | 2020-09-16 15:57:23 +0000 |
commit | 7907dd6fa6702f79d1be9ef8e483fa5533869eb7 (patch) | |
tree | 4fda7da525d7a224797c8a0997b426d25201bdd8 /parsers/http_response_parser.go | |
parent | 53301d30b56e5c328baf73d3bc512dc3170e49ea (diff) | |
parent | a77f2f97f1df204c663119fe8ccafb6f274cb634 (diff) |
Merge branch 'develop' into feature/frontend
Diffstat (limited to 'parsers/http_response_parser.go')
-rw-r--r-- | parsers/http_response_parser.go | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/parsers/http_response_parser.go b/parsers/http_response_parser.go new file mode 100644 index 0000000..1770116 --- /dev/null +++ b/parsers/http_response_parser.go @@ -0,0 +1,77 @@ +package parsers + +import ( + "bufio" + "bytes" + "compress/gzip" + log "github.com/sirupsen/logrus" + "io/ioutil" + "net/http" +) + +type HttpResponseMetadata struct { + BasicMetadata + Status string `json:"status"` + StatusCode int `json:"status_code"` + Protocol string `json:"protocol"` + Headers map[string]string `json:"headers"` + ConnectionClosed bool `json:"connection_closed"` + Cookies map[string]string `json:"cookies" binding:"omitempty"` + Location string `json:"location" binding:"omitempty"` + Compressed bool `json:"compressed"` + Body string `json:"body" binding:"omitempty"` + Trailer map[string]string `json:"trailer" binding:"omitempty"` +} + +type HttpResponseParser struct { +} + +func (p HttpResponseParser) TryParse(content []byte) Metadata { + reader := bufio.NewReader(bytes.NewReader(content)) + response, err := http.ReadResponse(reader, nil) + if err != nil { + return nil + } + var body string + var compressed bool + switch response.Header.Get("Content-Encoding") { + case "gzip": + if gzipReader, err := gzip.NewReader(response.Body); err == nil { + if buffer, err := ioutil.ReadAll(gzipReader); err == nil { + body = string(buffer) + compressed = true + } else { + log.WithError(err).Error("failed to read gzipped body in http_response_parser") + return nil + } + _ = gzipReader.Close() + } + default: + if buffer, err := ioutil.ReadAll(response.Body); err == nil { + body = string(buffer) + } else { + log.WithError(err).Error("failed to read body in http_response_parser") + return nil + } + } + _ = response.Body.Close() + + var location string + if locationUrl, err := response.Location(); err == nil { + location = locationUrl.String() + } + + return HttpResponseMetadata{ + BasicMetadata: BasicMetadata{"http-response"}, + Status: response.Status, + StatusCode: response.StatusCode, + Protocol: response.Proto, + Headers: JoinArrayMap(response.Header), + ConnectionClosed: response.Close, + Cookies: CookiesMap(response.Cookies()), + Location: location, + Compressed: compressed, + Body: body, + Trailer: JoinArrayMap(response.Trailer), + } +} |