|
| 1 | +package moesifgin |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io/ioutil" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +// Transport implements http.RoundTripper. |
| 14 | +type Transport struct { |
| 15 | + Transport http.RoundTripper |
| 16 | + LogRequest func(req *http.Request) |
| 17 | + LogResponse func(resp *http.Response) |
| 18 | +} |
| 19 | + |
| 20 | +// The default logging transport that wraps http.DefaultTransport. |
| 21 | +var DefaultTransport = &Transport{ |
| 22 | + Transport: http.DefaultTransport, |
| 23 | +} |
| 24 | + |
| 25 | +type contextKey struct { |
| 26 | + name string |
| 27 | +} |
| 28 | + |
| 29 | +var ContextKeyRequestStart = &contextKey{"RequestStart"} |
| 30 | + |
| 31 | +// RoundTrip is the core part of this module and implements http.RoundTripper. |
| 32 | +func (t *Transport) RoundTrip(request *http.Request) (*http.Response, error) { |
| 33 | + ctx := context.WithValue(request.Context(), ContextKeyRequestStart, time.Now()) |
| 34 | + request = request.WithContext(ctx) |
| 35 | + |
| 36 | + // Outgoing Request Time |
| 37 | + outgoingReqTime := time.Now().UTC() |
| 38 | + |
| 39 | + response, err := t.transport().RoundTrip(request) |
| 40 | + if err != nil { |
| 41 | + return response, err |
| 42 | + } |
| 43 | + |
| 44 | + // Outgoing Response Time |
| 45 | + outgoingRspTime := time.Now().UTC() |
| 46 | + |
| 47 | + // Skip capture outgoing event |
| 48 | + shouldSkipOutgoing := false |
| 49 | + if _, found := moesifOption["Should_Skip_Outgoing"]; found { |
| 50 | + shouldSkipOutgoing = moesifOption["Should_Skip_Outgoing"].(func(*http.Request, *http.Response) bool)(request, response) |
| 51 | + } |
| 52 | + |
| 53 | + // Skip / Send event to moesif |
| 54 | + if shouldSkipOutgoing { |
| 55 | + if debug { |
| 56 | + log.Printf("Skip sending the outgoing event to Moesif") |
| 57 | + } |
| 58 | + } else { |
| 59 | + |
| 60 | + // Check if the event is to Moesif |
| 61 | + if !(strings.Contains(request.URL.String(), "moesif.net")) { |
| 62 | + |
| 63 | + if debug { |
| 64 | + log.Printf("Sending the outgoing event to Moesif") |
| 65 | + } |
| 66 | + |
| 67 | + // Get Request Body |
| 68 | + var ( |
| 69 | + outgoingReqBody interface{} |
| 70 | + reqEncoding string |
| 71 | + reqContentLength *int64 |
| 72 | + ) |
| 73 | + |
| 74 | + if logBodyOutgoing && request.Body != nil { |
| 75 | + copyBody, err := request.GetBody() |
| 76 | + if err != nil { |
| 77 | + if debug { |
| 78 | + log.Printf("Error while getting the outgoing request body: %s.\n", err.Error()) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Read the request body |
| 83 | + readReqBody, reqBodyErr := ioutil.ReadAll(copyBody) |
| 84 | + if reqBodyErr != nil { |
| 85 | + if debug { |
| 86 | + log.Printf("Error while reading outgoing request body: %s.\n", reqBodyErr.Error()) |
| 87 | + } |
| 88 | + } |
| 89 | + reqContentLength = getContentLength(request.Header, readReqBody) |
| 90 | + |
| 91 | + // Parse the request Body |
| 92 | + outgoingReqBody, reqEncoding = parseBody(readReqBody, "Request_Body_Masks") |
| 93 | + |
| 94 | + // Return io.ReadCloser while making sure a Close() is available for request body |
| 95 | + request.Body = ioutil.NopCloser(bytes.NewBuffer(readReqBody)) |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + // Get Response Body |
| 100 | + var ( |
| 101 | + outgoingRespBody interface{} |
| 102 | + respEncoding string |
| 103 | + respContentLength *int64 |
| 104 | + ) |
| 105 | + |
| 106 | + if logBodyOutgoing && response.Body != nil { |
| 107 | + // Read the response body |
| 108 | + readRespBody, err := ioutil.ReadAll(response.Body) |
| 109 | + if err != nil { |
| 110 | + if debug { |
| 111 | + log.Printf("Error while reading outgoing response body: %s.\n", err.Error()) |
| 112 | + } |
| 113 | + } |
| 114 | + respContentLength = getContentLength(response.Header, readRespBody) |
| 115 | + |
| 116 | + // Parse the response Body |
| 117 | + outgoingRespBody, respEncoding = parseBody(readRespBody, "Response_Body_Masks") |
| 118 | + |
| 119 | + // Return io.ReadCloser while making sure a Close() is available for response body |
| 120 | + response.Body = ioutil.NopCloser(bytes.NewBuffer(readRespBody)) |
| 121 | + } |
| 122 | + |
| 123 | + // Get Outgoing Event Metadata |
| 124 | + var metadataOutgoing map[string]interface{} = nil |
| 125 | + if _, found := moesifOption["Get_Metadata_Outgoing"]; found { |
| 126 | + metadataOutgoing = moesifOption["Get_Metadata_Outgoing"].(func(*http.Request, *http.Response) map[string]interface{})(request, response) |
| 127 | + } |
| 128 | + |
| 129 | + // Get Outgoing User |
| 130 | + userIdOutgoing := getConfigStringValuesForOutgoingEvent("Identify_User_Outgoing", request, response) |
| 131 | + |
| 132 | + // Get Outgoing Company |
| 133 | + companyIdOutgoing := getConfigStringValuesForOutgoingEvent("Identify_Company_Outgoing", request, response) |
| 134 | + |
| 135 | + // Get Outgoing Session Token |
| 136 | + sessionTokenOutgoing := getConfigStringValuesForOutgoingEvent("Get_Session_Token_Outgoing", request, response) |
| 137 | + |
| 138 | + direction := "Outgoing" |
| 139 | + |
| 140 | + // Mask Request Header |
| 141 | + var requestHeader map[string]interface{} |
| 142 | + requestHeader = maskHeaders(HeaderToMap(request.Header), "Request_Header_Masks") |
| 143 | + |
| 144 | + // Mask Response Header |
| 145 | + var responseHeader map[string]interface{} |
| 146 | + responseHeader = maskHeaders(HeaderToMap(response.Header), "Response_Header_Masks") |
| 147 | + |
| 148 | + // Send Event To Moesif |
| 149 | + sendMoesifAsync(request, outgoingReqTime, requestHeader, nil, outgoingReqBody, &reqEncoding, reqContentLength, |
| 150 | + outgoingRspTime, response.StatusCode, responseHeader, outgoingRespBody, &respEncoding, respContentLength, |
| 151 | + userIdOutgoing, companyIdOutgoing, &sessionTokenOutgoing, metadataOutgoing, &direction) |
| 152 | + |
| 153 | + } else { |
| 154 | + if debug { |
| 155 | + log.Println("Request Skipped since it is Moesif Event") |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return response, err |
| 161 | +} |
| 162 | + |
| 163 | +func (t *Transport) transport() http.RoundTripper { |
| 164 | + if t.Transport != nil { |
| 165 | + return t.Transport |
| 166 | + } |
| 167 | + |
| 168 | + return http.DefaultTransport |
| 169 | +} |
0 commit comments