-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnative_http_recorder.go
171 lines (135 loc) · 3.8 KB
/
native_http_recorder.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package gorr
import (
"bytes"
"compress/gzip"
"fmt"
"github.com/brahma-adshonor/gohook"
"io/ioutil"
"net/http"
"sync"
)
type HttpData struct {
Status int
Body []byte
Header http.Header
}
type HttpRequestFixer func(pattern string, data []byte) []byte
type HttpRecorderHandler func(pattern, desc string, req *HttpData, rsp *HttpData)
type httpHandleGroup struct {
fixer HttpRequestFixer
recorder HttpRecorderHandler
}
var (
lock sync.Mutex
handlerMap = make(map[string]httpHandleGroup)
)
func RegisterHttpRecorder(pattern string, handler HttpRecorderHandler, fix HttpRequestFixer) {
lock.Lock()
defer lock.Unlock()
handlerMap[pattern] = httpHandleGroup{recorder: handler, fixer: fix}
}
type httpResponseWriterWrap struct {
data HttpData
}
func (h *httpResponseWriterWrap) Header() http.Header {
return h.data.Header
}
func (h *httpResponseWriterWrap) Write(d []byte) (int, error) {
if h.data.Body == nil {
h.data.Body = make([]byte, 0, len(d))
}
h.data.Body = append(h.data.Body, d...)
return len(d), nil
}
func (h *httpResponseWriterWrap) WriteHeader(stCode int) {
h.data.Status = stCode
}
type httpRecorder struct {
pattern string
origin http.Handler
handler HttpRecorderHandler
prepare func(string, []byte) []byte
}
func (h *httpRecorder) ServeHTTP(w http.ResponseWriter, r *http.Request) {
reqData, _ := ioutil.ReadAll(r.Body)
if h.prepare != nil {
reqData = h.prepare(h.pattern, reqData)
}
r.Body = ioutil.NopCloser(bytes.NewBuffer(reqData))
dname := r.Header.Get("RegressionName")
// reset user-agent
r.Header.Set("User-Agent", "RegressionTool")
wr := &httpResponseWriterWrap{}
wr.data.Status = -1
wr.data.Header = w.Header()
h.origin.ServeHTTP(wr, r)
req := &HttpData{
Body: reqData,
Header: r.Header,
}
if wr.data.Status != -1 {
w.WriteHeader(wr.data.Status)
}
if len(wr.data.Body) > 0 {
w.Write(wr.data.Body)
}
if wr.data.Header.Get("Content-Encoding") == "gzip" {
reader, _ := gzip.NewReader(bytes.NewBuffer(wr.data.Body))
wr.data.Body, _ = ioutil.ReadAll(reader)
defer reader.Close()
}
if wr.data.Header.Get("HttpResponseType") == "Failure" {
v := fmt.Sprintf("pattern:%s, name:%s", h.pattern, dname)
GlobalMgr.notifier("native http recorder", "ignoring error response", []byte(v))
return
}
p := h.pattern
if len(r.URL.RawQuery) > 0 {
p = p + "?" + r.URL.RawQuery
}
if len(wr.data.Body) > 0 {
h.handler(p, dname, req, &wr.data)
GlobalMgr.notifier("native http recorder", "recording http done", []byte(r.URL.Path+"@@"+r.URL.RawQuery))
} else {
GlobalMgr.notifier("native http recorder", "empty reponse not recorded", []byte(r.URL.Path+"@@"+r.URL.RawQuery))
}
}
func httpHandleHook(s *http.ServeMux, pattern string, handler http.Handler) {
h := handler
{
lock.Lock()
defer lock.Unlock()
if val, ok := handlerMap[pattern]; ok {
h = &httpRecorder{
prepare: val.fixer,
handler: val.recorder,
origin: handler,
pattern: pattern,
}
}
}
httpHandleHookTramp(s, pattern, h)
}
// go:noinline
func httpHandleHookTramp(s *http.ServeMux, pattern string, handler http.Handler) {
// make sure handler will be heap allocated.
fmt.Printf("dummy function for regrestion testing:%s, %v", pattern, handler)
for i := 0; i < 100000; i++ {
fmt.Printf("id:%d\n", i)
go func() { fmt.Printf("hello world\n") }()
}
if handler != nil {
panic("trampoline Http Post function is not allowed to be called")
}
}
///////////////////////// setup hoook //////////////////////////////////
func HookHttpServerHandler() error {
//return gohook.Hook(http.Handle, httpHandleHook, httpHandleHookTramp)
var s *http.ServeMux
return gohook.HookMethod(s, "Handle", httpHandleHook, httpHandleHookTramp)
}
func UnHookHttpServerHandler() error {
//return gohook.UnHook(http.Handle)
var s *http.ServeMux
return gohook.UnHookMethod(s, "Handle")
}