From 842122b5a1ce1975af35cb3a01060212c7103447 Mon Sep 17 00:00:00 2001 From: "M. Mert Yildiran" Date: Mon, 23 Aug 2021 17:06:35 +0300 Subject: [PATCH] Refactor the protocol payload structs --- tap/extensions/amqp/helpers.go | 1 - tap/extensions/amqp/structs.go | 10 +--------- tap/extensions/http/main.go | 5 +++++ tap/extensions/http/matcher.go | 4 ++-- tap/extensions/http/structs.go | 6 +++--- tap/extensions/kafka/helpers.go | 1 - tap/extensions/kafka/response.go | 2 -- 7 files changed, 11 insertions(+), 18 deletions(-) diff --git a/tap/extensions/amqp/helpers.go b/tap/extensions/amqp/helpers.go index 0b5ec7bbc..b4eab04a4 100644 --- a/tap/extensions/amqp/helpers.go +++ b/tap/extensions/amqp/helpers.go @@ -98,7 +98,6 @@ func emitAMQP(event interface{}, _type string, method string, connectionInfo *ap IsRequest: true, CaptureTime: time.Now(), Payload: AMQPPayload{ - Type: _type, Data: &AMQPWrapper{ Method: method, Url: "", diff --git a/tap/extensions/amqp/structs.go b/tap/extensions/amqp/structs.go index 49c4e921d..4f7bb93df 100644 --- a/tap/extensions/amqp/structs.go +++ b/tap/extensions/amqp/structs.go @@ -5,9 +5,7 @@ import ( ) type AMQPPayload struct { - Type string - Method string - Data interface{} + Data interface{} } type AMQPPayloader interface { @@ -16,10 +14,4 @@ type AMQPPayloader interface { func (h AMQPPayload) MarshalJSON() ([]byte, error) { return json.Marshal(h.Data) - // switch h.Type { - // case "amqp_request": - // return json.Marshal(h.Data) - // default: - // panic(fmt.Sprintf("AMQP payload cannot be marshaled: %s\n", h.Type)) - // } } diff --git a/tap/extensions/http/main.go b/tap/extensions/http/main.go index b551442ac..3d26584d1 100644 --- a/tap/extensions/http/main.go +++ b/tap/extensions/http/main.go @@ -39,6 +39,11 @@ var http2Protocol api.Protocol = api.Protocol{ Ports: []string{"80", "8080"}, } +const ( + TypeHttpRequest = iota + TypeHttpResponse +) + func init() { log.Println("Initializing HTTP extension.") requestCounter = 0 diff --git a/tap/extensions/http/matcher.go b/tap/extensions/http/matcher.go index fcb7c22b5..e32149805 100644 --- a/tap/extensions/http/matcher.go +++ b/tap/extensions/http/matcher.go @@ -32,7 +32,7 @@ func (matcher *requestResponseMatcher) registerRequest(ident string, request *ht IsRequest: true, CaptureTime: captureTime, Payload: HTTPPayload{ - Type: "http_request", + Type: TypeHttpRequest, Data: request, }, } @@ -61,7 +61,7 @@ func (matcher *requestResponseMatcher) registerResponse(ident string, response * IsRequest: false, CaptureTime: captureTime, Payload: HTTPPayload{ - Type: "http_response", + Type: TypeHttpResponse, Data: response, }, } diff --git a/tap/extensions/http/structs.go b/tap/extensions/http/structs.go index bd231461a..6ec868832 100644 --- a/tap/extensions/http/structs.go +++ b/tap/extensions/http/structs.go @@ -11,7 +11,7 @@ import ( ) type HTTPPayload struct { - Type string + Type uint8 Data interface{} } @@ -27,7 +27,7 @@ type HTTPWrapper struct { func (h HTTPPayload) MarshalJSON() ([]byte, error) { switch h.Type { - case "http_request": + case TypeHttpRequest: harRequest, err := har.NewRequest(h.Data.(*http.Request), true) if err != nil { rlog.Debugf("convert-request-to-har", "Failed converting request to HAR %s (%v,%+v)", err, err, err) @@ -38,7 +38,7 @@ func (h HTTPPayload) MarshalJSON() ([]byte, error) { Url: "", Details: harRequest, }) - case "http_response": + case TypeHttpResponse: harResponse, err := har.NewResponse(h.Data.(*http.Response), true) if err != nil { rlog.Debugf("convert-response-to-har", "Failed converting response to HAR %s (%v,%+v)", err, err, err) diff --git a/tap/extensions/kafka/helpers.go b/tap/extensions/kafka/helpers.go index d95044cb8..98c514c1d 100644 --- a/tap/extensions/kafka/helpers.go +++ b/tap/extensions/kafka/helpers.go @@ -7,7 +7,6 @@ import ( ) type KafkaPayload struct { - Type string Data interface{} } diff --git a/tap/extensions/kafka/response.go b/tap/extensions/kafka/response.go index f465bc283..8b0821264 100644 --- a/tap/extensions/kafka/response.go +++ b/tap/extensions/kafka/response.go @@ -257,7 +257,6 @@ func ReadResponse(r io.Reader, tcpID *api.TcpID, emitter api.Emitter) (err error IsRequest: true, CaptureTime: time.Now(), Payload: KafkaPayload{ - Type: "kafka_request", Data: &KafkaWrapper{ Method: apiNames[apiKey], Url: "", @@ -269,7 +268,6 @@ func ReadResponse(r io.Reader, tcpID *api.TcpID, emitter api.Emitter) (err error IsRequest: false, CaptureTime: time.Now(), Payload: KafkaPayload{ - Type: "kafka_response", Data: &KafkaWrapper{ Method: apiNames[apiKey], Url: "",