-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugserver.go
124 lines (105 loc) · 2.68 KB
/
debugserver.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package debugserver
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"io"
"log"
"net"
"net/http"
"net/url"
"strings"
)
var storage = NewStorage()
var emptyResponse = []byte("[]\n")
var errNoID = errors.New("no ID provided")
func API(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/buckets/") {
record(w, r)
}
if strings.HasPrefix(r.URL.Path, "/reports/") {
if r.Method == http.MethodDelete {
del(w, r)
}
if r.Method == http.MethodGet {
show(w, r)
}
}
}
type Request struct {
ContentLength int64 `json:"content_length"`
Body string `json:"body"`
Method string `json:"method"`
URL string `json:"url"`
QueryParams string `json:"query_params"`
RemoteIP string `json:"remote_ip"`
Headers map[string][]string `json:"headers"`
}
func requestID(path string) (string, error) {
path = strings.TrimPrefix(path, "/")
chunks := strings.Split(path, "/")
if len(chunks) < 2 {
return "", errNoID
}
id := strings.TrimSuffix(chunks[1], "/")
id = strings.TrimSpace(id)
if id == "" {
return "", errNoID
}
return id, nil
}
func record(w http.ResponseWriter, r *http.Request) {
id, err := requestID(r.URL.Path)
if err != nil {
http.Error(w, "no ID provided", http.StatusBadRequest)
return
}
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Printf("Failed to sptil remote address to IP and Port: %s", err)
}
params, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
http.Error(w, "bad query params", http.StatusBadRequest)
}
bodyReader := http.MaxBytesReader(w, r.Body, 1<<20)
defer bodyReader.Close()
var body bytes.Buffer
if _, err := io.Copy(&body, bodyReader); err != nil {
http.Error(w, "unable to read body", http.StatusInternalServerError)
return
}
storage.Add(id, Request{
RemoteIP: ip,
URL: r.URL.Path,
QueryParams: params.Encode(),
Method: r.Method,
Headers: r.Header,
Body: base64.StdEncoding.EncodeToString(body.Bytes()),
ContentLength: r.ContentLength,
})
}
func show(w http.ResponseWriter, r *http.Request) {
id, err := requestID(r.URL.Path)
if err != nil {
http.Error(w, "no ID provided", http.StatusBadRequest)
return
}
requests := storage.Get(id)
if requests == nil {
w.Write(emptyResponse)
return
}
if err := json.NewEncoder(w).Encode(requests); err != nil {
log.Printf("Failed to serialize payload: %s", err)
}
}
func del(w http.ResponseWriter, r *http.Request) {
id, err := requestID(r.URL.Path)
if err != nil {
http.Error(w, "no ID provided", http.StatusBadRequest)
return
}
storage.Del(id)
}