Files
kubeshark/tap/extensions/http/structs.go
2021-08-20 03:00:55 +03:00

42 lines
1.0 KiB
Go

package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/google/martian/har"
"github.com/romana/rlog"
)
type HTTPPayload struct {
Type string
Data interface{}
}
type HTTPPayloader interface {
MarshalJSON() ([]byte, error)
}
func (h HTTPPayload) MarshalJSON() ([]byte, error) {
switch h.Type {
case "http_request":
harRequest, err := har.NewRequest(h.Data.(*http.Request), false)
if err != nil {
rlog.Debugf("convert-request-to-har", "Failed converting request to HAR %s (%v,%+v)", err, err, err)
return nil, errors.New("Failed converting request to HAR")
}
return json.Marshal(harRequest)
case "http_response":
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)
return nil, errors.New("Failed converting response to HAR")
}
return json.Marshal(harResponse)
default:
panic(fmt.Sprintf("HTTP payload cannot be marshaled: %s\n", h.Type))
}
}