diff options
| author | Jeff Carr <[email protected]> | 2024-10-11 23:01:55 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2024-10-11 23:01:55 -0500 |
| commit | 25f7aa1d22b332a168edbcc8bcd46308aca9c767 (patch) | |
| tree | 0c42c38b34e09718f104781d457e64cb893bcaa9 /jsonClient.go | |
| parent | f71da2ef59a51788f32b5fe5eb600ce40dbbdd75 (diff) | |
add http listen
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'jsonClient.go')
| -rw-r--r-- | jsonClient.go | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/jsonClient.go b/jsonClient.go new file mode 100644 index 0000000..4dc9b10 --- /dev/null +++ b/jsonClient.go @@ -0,0 +1,108 @@ +package main + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "net/http" +) + +// RequestInfo holds the extracted data from http.Request +type RequestInfo struct { + Host string `json:"host"` + URL string `json:"url"` + Proto string `json:"proto"` + Addr string `json:"addr"` + Agent string `json:"agent"` + Headers map[string][]string `json:"headers"` + Cookies map[string]string `json:"cookies"` + QueryParams map[string][]string `json:"queryParams"` + // Add other fields as needed + Body string `json:"body"` +} + +// dumpClient returns a string with JSON formatted http.Request information +func dumpJsonClient(r *http.Request) (string, error) { + // Extracting Cookies + cookieMap := make(map[string]string) + for _, cookie := range r.Cookies() { + cookieMap[cookie.Name] = cookie.Value + } + + // Read the body (Ensure to do this first) + var bodyBytes []byte + if r.Body != nil { // Read the body if it's not nil + bodyBytes, _ = ioutil.ReadAll(r.Body) + r.Body.Close() // Close the body when done reading + r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) // Reset the body + } + + info := RequestInfo{ + Host: r.Host, + URL: r.URL.String(), + Proto: r.Proto, + Addr: r.RemoteAddr, + Agent: r.UserAgent(), + Headers: r.Header, + Cookies: cookieMap, + QueryParams: r.URL.Query(), + Body: string(bodyBytes), + } + + // Marshal the struct to a JSON string + jsonString, err := json.Marshal(info) + if err != nil { + return "", err // Return the error to the caller + } + + var jsonData interface{} + err = json.Unmarshal([]byte(jsonString), &jsonData) + if err != nil { + return "", err // Return the error to the caller + } + + formattedJSON, err := json.MarshalIndent(jsonData, "", " ") + if err != nil { + return "", err // Return the error to the caller + } + + return string(formattedJSON), nil +} + +/* +package main + +import ( + "bytes" + "encoding/json" + "io" + "io/ioutil" + "net/http" +) + +type RequestInfo struct { + // ... (other fields) + Body string `json:"body"` + // ... (other fields) +} + +func dumpClient(r *http.Request) (string, error) { + // ... (rest of your code to collect other request info) + + info := RequestInfo{ + // ... (other fields) + Body: string(bodyBytes), + // ... (other fields) + } + + // Marshal the struct to a JSON string + jsonString, err := json.Marshal(info) + if err != nil { + return "", err + } + + return string(jsonString), nil +} + +// ... (rest of your code) +*/ |
