/
models.go
305 lines (251 loc) · 7.26 KB
/
models.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package main
import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
log "github.com/Sirupsen/logrus"
"github.com/elazarl/goproxy"
"github.com/garyburd/redigo/redis"
)
type DBClient struct {
cache Cache
http *http.Client
}
// request holds structure for request
type request struct {
details requestDetails
}
var emptyResp = &http.Response{}
// requestDetails stores information about request, it's used for creating unique hash and also as a payload structure
type requestDetails struct {
Path string `json:"path"`
Method string `json:"method"`
Destination string `json:"destination"`
Query string `json:"query"`
}
// hash returns unique hash key for request
func (r *request) hash() string {
h := md5.New()
io.WriteString(h, fmt.Sprintf("%s%s%s%s", r.details.Destination, r.details.Path, r.details.Method, r.details.Query))
return fmt.Sprintf("%x", h.Sum(nil))
}
// res structure hold response body from external service, body is not decoded and is supposed
// to be bytes, however headers should provide all required information for later decoding
// by the client.
type response struct {
Status int `json:"status"`
Body []byte `json:"body"`
Headers map[string][]string `json:"headers"`
}
// Payload structure holds request and response structure
type Payload struct {
Response response `json:"response"`
Request requestDetails `json:"request"`
ID string `json:"id"`
}
// recordRequest saves request for later playback
func (d *DBClient) recordRequest(req *http.Request) (*http.Response, error) {
// forwarding request
resp, err := d.doRequest(req)
if err == nil {
// getting response body
body, err := httputil.DumpResponse(resp, true)
if err != nil {
// copying the response body did not work
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Failed to copy response body.")
}
}
// saving response body with request/response meta to cache
go d.save(req, resp, body)
}
// return new response or error here
return resp, err
}
// doRequest performs original request and returns response that should be returned to client and error (if there is one)
func (d *DBClient) doRequest(request *http.Request) (*http.Response, error) {
// We can't have this set. And it only contains "/pkg/net/http/" anyway
request.RequestURI = ""
resp, err := d.http.Do(request)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
"host": request.Host,
"method": request.Method,
"path": request.URL.Path,
}).Error("Could not forward request.")
return nil, err
}
log.WithFields(log.Fields{
"host": request.Host,
"method": request.Method,
"path": request.URL.Path,
}).Info("Response got successfuly!")
resp.Header.Set("Gen-proxy", "Was-Here")
return resp, nil
}
// save gets request fingerprint, extracts request body, status code and headers, then saves it to cache
func (d *DBClient) save(req *http.Request, resp *http.Response, respBody []byte) {
// record request here
key := getRequestFingerprint(req)
if resp == nil {
resp = emptyResp
} else {
responseObj := response{
Status: resp.StatusCode,
Body: respBody,
// Headers: getHeadersMap(resp.Header),
Headers: resp.Header,
}
log.WithFields(log.Fields{
"path": req.URL.Path,
"rawQuery": req.URL.RawQuery,
"requestMethod": req.Method,
"destination": req.Host,
"hashKey": key,
}).Info("Recording")
requestObj := requestDetails{
Path: req.URL.Path,
Method: req.Method,
Destination: req.Host,
Query: req.URL.RawQuery,
}
payload := Payload{
Response: responseObj,
Request: requestObj,
ID: key,
}
// converting it to json bytes
bts, err := json.Marshal(payload)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Failed to marshal json")
} else {
d.cache.set(key, bts)
}
}
}
// getAllRecordsRaw returns raw (json string) for all records
func (d *DBClient) getAllRecordsRaw() ([]string, error) {
keys, err := d.cache.getAllKeys()
if err == nil {
// checking if there are any records
if len(keys) == 0 {
return nil, nil
}
jsonStrs, err := d.cache.getAllValues(keys)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Failed to get all values (raw)")
return nil, err
} else {
return jsonStrs, nil
}
} else {
return nil, err
}
}
// getAllRecords returns all stored
func (d *DBClient) getAllRecords() ([]Payload, error) {
var payloads []Payload
jsonStrs, err := d.getAllRecordsRaw()
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Failed to get all values")
} else {
if jsonStrs != nil {
for _, v := range jsonStrs {
var pl Payload
err = json.Unmarshal([]byte(v), &pl)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
"json": v,
}).Warning("Failed to deserialize json")
} else {
payloads = append(payloads, pl)
}
}
}
}
return payloads, err
}
// deleteAllRecords deletes all recorded requests
func (d *DBClient) deleteAllRecords() error {
keys, err := d.cache.getAllKeys()
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Warning("Failed to get keys, cannot delete all records")
return err
} else {
for _, v := range keys {
d.cache.delete(v)
}
return nil
}
}
// getRequestFingerprint returns request hash
func getRequestFingerprint(req *http.Request) string {
details := requestDetails{Path: req.URL.Path, Method: req.Method, Destination: req.Host, Query: req.URL.RawQuery}
r := request{details: details}
return r.hash()
}
// getResponse returns stored response from cache
func (d *DBClient) getResponse(req *http.Request) *http.Response {
log.Info("Returning response")
key := getRequestFingerprint(req)
var payload Payload
payloadBts, err := redis.Bytes(d.cache.get(key))
if err == nil {
log.Info("Decoding bytes")
// getting cache response
err = json.Unmarshal(payloadBts, &payload)
if err != nil {
log.Error(err)
// what now?
}
newResponse := &http.Response{}
newResponse.Request = req
// adding headers
newResponse.Header = make(http.Header)
if len(payload.Response.Headers) > 0 {
for k, values := range payload.Response.Headers {
// headers is a map, appending each value
for _, v := range values {
newResponse.Header.Add(k, v)
}
}
}
newResponse.Header.Set("Gen-Proxy", "Playback")
// adding body
buf := bytes.NewBuffer(payload.Response.Body)
newResponse.ContentLength = int64(buf.Len())
newResponse.Body = ioutil.NopCloser(buf)
newResponse.StatusCode = payload.Response.Status
log.WithFields(log.Fields{
"key": key,
"status": payload.Response.Status,
"bodyLength": newResponse.ContentLength,
}).Info("Response found, returning")
return newResponse
} else {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Failed to retrieve response from cache")
// return error? if we return nil - proxy forwards request to original destination
return goproxy.NewResponse(req,
goproxy.ContentTypeText, http.StatusPreconditionFailed,
"Coudldn't find recorded request, please record it first!")
}
}