512KB buildHTTPDumpEvent creates an http-dump event from a proxied request/response pair. The payload matches the http-dump format with additional "response" and "proxy" fields.
(req *capturedRequest, resp *capturedResponse, durationMs float64, proxyErr string)
| 12 | // buildHTTPDumpEvent creates an http-dump event from a proxied request/response pair. |
| 13 | // The payload matches the http-dump format with additional "response" and "proxy" fields. |
| 14 | func buildHTTPDumpEvent(req *capturedRequest, resp *capturedResponse, durationMs float64, proxyErr string) *event.Incoming { |
| 15 | headers := make(map[string][]string) |
| 16 | for k, v := range req.Headers { |
| 17 | headers[k] = v |
| 18 | } |
| 19 | |
| 20 | query := make(map[string]any) |
| 21 | for k, v := range req.Query { |
| 22 | if len(v) == 1 { |
| 23 | query[k] = v[0] |
| 24 | } else { |
| 25 | query[k] = v |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | payload := map[string]any{ |
| 30 | "received_at": time.Now().Format("2006-01-02 15:04:05"), |
| 31 | "host": req.Host, |
| 32 | "proxy": true, |
| 33 | "duration_ms": durationMs, |
| 34 | "request": map[string]any{ |
| 35 | "method": req.Method, |
| 36 | "uri": req.URI, |
| 37 | "headers": headers, |
| 38 | "body": truncateBody(req.Body), |
| 39 | "query": query, |
| 40 | "post": map[string]any{}, |
| 41 | "cookies": map[string]string{}, |
| 42 | "files": []any{}, |
| 43 | }, |
| 44 | } |
| 45 | |
| 46 | if proxyErr != "" { |
| 47 | payload["error"] = proxyErr |
| 48 | } |
| 49 | |
| 50 | if resp != nil { |
| 51 | respHeaders := make(map[string][]string) |
| 52 | for k, v := range resp.Headers { |
| 53 | respHeaders[k] = v |
| 54 | } |
| 55 | payload["response"] = map[string]any{ |
| 56 | "status_code": resp.StatusCode, |
| 57 | "headers": respHeaders, |
| 58 | "body": truncateBody(resp.Body), |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | b, _ := json.Marshal(payload) |
| 63 | |
| 64 | return &event.Incoming{ |
| 65 | UUID: event.GenerateUUID(), |
| 66 | Type: "http-dump", |
| 67 | Payload: json.RawMessage(b), |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | type capturedRequest struct { |
no test coverage detected