/
payload.go
345 lines (303 loc) · 8.88 KB
/
payload.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package models
import (
"bytes"
"crypto/md5"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"sort"
"strings"
v2 "github.com/SpectoLabs/hoverfly/core/handlers/v2"
"github.com/SpectoLabs/hoverfly/core/interfaces"
"github.com/SpectoLabs/hoverfly/core/util"
log "github.com/sirupsen/logrus"
)
// Payload structure holds request and response structure
type RequestResponsePair struct {
Response ResponseDetails
Request RequestDetails
}
func (this *RequestResponsePair) ConvertToRequestResponsePairView() v2.RequestResponsePairViewV1 {
return v2.RequestResponsePairViewV1{Response: this.Response.ConvertToResponseDetailsView(), Request: this.Request.ConvertToRequestDetailsView()}
}
func NewRequestResponsePairFromRequestResponsePairView(pairView interfaces.RequestResponsePair) RequestResponsePair {
return RequestResponsePair{
Response: NewResponseDetailsFromResponse(pairView.GetResponse()),
Request: NewRequestDetailsFromRequest(pairView.GetRequest()),
}
}
func NewRequestDetailsFromRequest(data interfaces.Request) RequestDetails {
query, _ := url.ParseQuery(*data.GetQuery())
return RequestDetails{
Path: util.PointerToString(data.GetPath()),
Method: util.PointerToString(data.GetMethod()),
Destination: util.PointerToString(data.GetDestination()),
Scheme: util.PointerToString(data.GetScheme()),
Query: query,
Body: util.PointerToString(data.GetBody()),
Headers: data.GetHeaders(),
}
}
// RequestDetails stores information about request, it's used for creating unique hash and also as a payload structure
type RequestDetails struct {
Path string
Method string
Destination string
Scheme string
Query map[string][]string
Body string
FormData map[string][]string
Headers map[string][]string
rawQuery string
}
func NewRequestDetailsFromHttpRequest(req *http.Request) (RequestDetails, error) {
if req.Body == nil {
req.Body = ioutil.NopCloser(bytes.NewBuffer([]byte("")))
}
reqBody, err := util.GetRequestBody(req)
req.ParseForm()
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
"mode": "capture",
}).Error("Got error while reading request body")
return RequestDetails{}, err
}
// Request not always have RawPath, but we want to use it if exists for perservind encoding
var urlPath = req.URL.RawPath
if urlPath == "" {
urlPath = req.URL.Path
}
// Proxy tunnel request gives relative URL, and we should manually set scheme to HTTP
var scheme string
if req.URL.IsAbs() {
scheme = req.URL.Scheme
} else {
scheme = "http"
}
requestDetails := RequestDetails{
Path: urlPath,
Method: req.Method,
Destination: strings.ToLower(req.Host),
Scheme: scheme,
Query: parseQuery(req.URL.RawQuery),
Body: reqBody,
FormData: req.PostForm,
Headers: req.Header.Clone(),
rawQuery: req.URL.RawQuery,
}
for key, value := range requestDetails.Query {
if strings.HasPrefix(key, "./") {
requestDetails.Query[key[2:]] = value
delete(requestDetails.Query, key)
}
}
return requestDetails, nil
}
func (this *RequestDetails) ConvertToRequestDetailsView() v2.RequestDetailsView {
queryString := this.QueryString()
body := this.Body
if util.NeedsEncoding(this.Headers, this.Body) {
body = base64.StdEncoding.EncodeToString([]byte(this.Body))
}
return v2.RequestDetailsView{
Path: &this.Path,
Method: &this.Method,
Destination: &this.Destination,
Scheme: &this.Scheme,
Query: &queryString,
QueryMap: this.Query,
Body: &body,
FormData: this.FormData,
Headers: this.Headers,
}
}
// TODO: Remove this
// This only exists as there are parts of Hoverfly that still
// require the request query parameters to be a string and not
// a map
func (this *RequestDetails) QueryString() string {
var buf bytes.Buffer
keys := make([]string, 0, len(this.Query))
for k := range this.Query {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
vs := this.Query[k]
prefix := k + "="
for _, v := range vs {
if buf.Len() > 0 {
buf.WriteByte('&')
}
buf.WriteString(prefix)
buf.WriteString(v)
}
}
return util.SortQueryString(buf.String())
}
func (r *RequestDetails) concatenate(withHost bool) string {
var buffer bytes.Buffer
if withHost {
buffer.WriteString(r.Destination)
}
if len(r.FormData) > 0 {
formData, _ := json.Marshal(r.FormData)
buffer.WriteString(bytes.NewBuffer(formData).String())
}
buffer.WriteString(r.Path)
buffer.WriteString(r.Method)
buffer.WriteString(r.QueryString())
if len(r.Body) > 0 {
buffer.WriteString(r.Body)
}
return buffer.String()
}
func (r *RequestDetails) Hash() string {
h := md5.New()
io.WriteString(h, r.concatenate(true))
return fmt.Sprintf("%x", h.Sum(nil))
}
func (r *RequestDetails) HashWithoutHost() string {
h := md5.New()
io.WriteString(h, r.concatenate(false))
return fmt.Sprintf("%x", h.Sum(nil))
}
type ResponseDetailsLogNormal struct {
Min int
Max int
Mean int
Median int
}
// ResponseDetails 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 ResponseDetails struct {
Status int
Body string
BodyFile string
Headers map[string][]string
Templated bool
TransitionsState map[string]string
RemovesState []string
FixedDelay int
LogNormalDelay *ResponseDetailsLogNormal
PostServeAction string
}
func NewResponseDetailsFromResponse(data interfaces.Response) ResponseDetails {
body := data.GetBody()
if data.GetEncodedBody() == true {
decoded, _ := base64.StdEncoding.DecodeString(data.GetBody())
body = string(decoded)
}
details := ResponseDetails{
Status: data.GetStatus(),
Body: body,
BodyFile: data.GetBodyFile(),
Headers: data.GetHeaders(),
Templated: data.GetTemplated(),
TransitionsState: data.GetTransitionsState(),
RemovesState: data.GetRemovesState(),
FixedDelay: data.GetFixedDelay(),
PostServeAction: data.GetPostServeAction(),
}
if d := data.GetLogNormalDelay(); d != nil {
details.LogNormalDelay = &ResponseDetailsLogNormal{
Min: d.GetMin(),
Max: d.GetMax(),
Mean: d.GetMean(),
Median: d.GetMedian(),
}
}
return details
}
// This function will create a JSON appropriate version of ResponseDetails for the v2 API
// If the response headers indicate that the content is encoded, or it has a non-matching
// supported mimetype, we base64 encode it.
func (r *ResponseDetails) ConvertToResponseDetailsView() v2.ResponseDetailsView {
needsEncoding := util.NeedsEncoding(r.Headers, r.Body)
// If contains gzip, base64 encode
body := r.Body
if needsEncoding {
body = base64.StdEncoding.EncodeToString([]byte(r.Body))
}
return v2.ResponseDetailsView{
Status: r.Status,
Body: body,
Headers: r.Headers,
EncodedBody: needsEncoding,
PostServeAction: r.PostServeAction,
}
}
func (r *ResponseDetails) ConvertToResponseDetailsViewV5() v2.ResponseDetailsViewV5 {
needsEncoding := false
// Check headers for gzip
contentEncodingValues := r.Headers["Content-Encoding"]
if len(contentEncodingValues) > 0 {
needsEncoding = true
} else {
mimeType := http.DetectContentType([]byte(r.Body))
needsEncoding = true
for _, v := range util.SupportedMimeTypes {
if strings.Contains(mimeType, v) {
needsEncoding = false
break
}
}
}
// If contains gzip, base64 encode
body := r.Body
if needsEncoding {
body = base64.StdEncoding.EncodeToString([]byte(r.Body))
}
view := v2.ResponseDetailsViewV5{
Status: r.Status,
Body: body,
BodyFile: r.BodyFile,
Headers: r.Headers,
EncodedBody: needsEncoding,
Templated: r.Templated,
RemovesState: r.RemovesState,
TransitionsState: r.TransitionsState,
FixedDelay: r.FixedDelay,
PostServeAction: r.PostServeAction,
}
if r.LogNormalDelay != nil {
view.LogNormalDelay = &v2.LogNormalDelayOptions{
Min: r.LogNormalDelay.Min,
Max: r.LogNormalDelay.Max,
Mean: r.LogNormalDelay.Mean,
Median: r.LogNormalDelay.Median,
}
}
return view
}
func (this *RequestDetails) GetRawQuery() string {
return this.rawQuery
}
// Similar to req.URL.Query() but allowing compound query params like qq=country=BEL;postalCode=1234;city=SomeCity;street=SomeStreet;houseNumber=25%20a
func parseQuery(query string) map[string][]string {
m := make(map[string][]string)
for query != "" {
var key string
key, query, _ = strings.Cut(query, "&")
if key == "" {
continue
}
key, value, _ := strings.Cut(key, "=")
key, err := url.QueryUnescape(key)
if err != nil {
continue
}
value, err = url.QueryUnescape(value)
if err != nil {
continue
}
m[key] = append(m[key], value)
}
return m
}