-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpdump.go
80 lines (63 loc) · 1.65 KB
/
httpdump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sort"
"strings"
"github.com/fatih/color"
"github.com/hokaccha/go-prettyjson"
)
func dump(w http.ResponseWriter, r *http.Request) {
var jsonData interface{}
defer r.Body.Close()
r.Header.Set("Host", r.Host)
for _, header := range sortHeaderKeys(r.Header) {
fmt.Printf("%s %s: %s\n", color.YellowString("[H]"), header, r.Header.Get(header))
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Printf("error: unable to read request body: %s", err)
return
}
if getContentType(r) == "application/json" {
if err := json.Unmarshal(body, &jsonData); err != nil {
fmt.Printf("%s error: request Content-Type is application/json, but unable to process JSON %s\n", color.RedString("[!]"), err)
fmt.Printf("%s %s\n", color.CyanString("[B]"), body)
} else {
jf := prettyjson.NewFormatter()
j, _ := jf.Format(body)
fmt.Printf("%s %s\n", color.CyanString("[B]"), j)
}
} else {
fmt.Printf("%s %s\n", color.CyanString("[B]"), body)
}
}
func getContentType(input interface{}) string {
var header http.Header
switch input.(type) {
case *http.Request:
header = input.(*http.Request).Header
case *http.Response:
header = input.(*http.Response).Header
case http.ResponseWriter:
header = input.(http.ResponseWriter).Header()
default:
return ""
}
contentType := header.Get("Content-Type")
index := strings.Index(contentType, ";")
if index != -1 {
return contentType[:index]
}
return contentType
}
func sortHeaderKeys(headers http.Header) []string {
var keys []string
for k := range headers {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}