-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
request.go
541 lines (484 loc) · 15.6 KB
/
request.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package http
import (
"bytes"
"context"
"fmt"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"reflect"
"strings"
"time"
"github.com/dop251/goja"
"gopkg.in/guregu/null.v3"
"github.com/loadimpact/k6/js/common"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/netext/httpext"
"github.com/loadimpact/k6/lib/types"
)
// ErrHTTPForbiddenInInitContext is used when a http requests was made in the init context
var ErrHTTPForbiddenInInitContext = common.NewInitContextError("Making http requests in the init context is not supported")
// ErrBatchForbiddenInInitContext is used when batch was made in the init context
var ErrBatchForbiddenInInitContext = common.NewInitContextError("Using batch in the init context is not supported")
// Get makes an HTTP GET request and returns a corresponding response by taking goja.Values as arguments
func (h *HTTP) Get(ctx context.Context, url goja.Value, args ...goja.Value) (*Response, error) {
// The body argument is always undefined for GETs and HEADs.
args = append([]goja.Value{goja.Undefined()}, args...)
return h.Request(ctx, HTTP_METHOD_GET, url, args...)
}
// Head makes an HTTP HEAD request and returns a corresponding response by taking goja.Values as arguments
func (h *HTTP) Head(ctx context.Context, url goja.Value, args ...goja.Value) (*Response, error) {
// The body argument is always undefined for GETs and HEADs.
args = append([]goja.Value{goja.Undefined()}, args...)
return h.Request(ctx, HTTP_METHOD_HEAD, url, args...)
}
// Post makes an HTTP POST request and returns a corresponding response by taking goja.Values as arguments
func (h *HTTP) Post(ctx context.Context, url goja.Value, args ...goja.Value) (*Response, error) {
return h.Request(ctx, HTTP_METHOD_POST, url, args...)
}
// Put makes an HTTP PUT request and returns a corresponding response by taking goja.Values as arguments
func (h *HTTP) Put(ctx context.Context, url goja.Value, args ...goja.Value) (*Response, error) {
return h.Request(ctx, HTTP_METHOD_PUT, url, args...)
}
// Patch makes a patch request and returns a corresponding response by taking goja.Values as arguments
func (h *HTTP) Patch(ctx context.Context, url goja.Value, args ...goja.Value) (*Response, error) {
return h.Request(ctx, HTTP_METHOD_PATCH, url, args...)
}
// Del makes an HTTP DELETE and returns a corresponding response by taking goja.Values as arguments
func (h *HTTP) Del(ctx context.Context, url goja.Value, args ...goja.Value) (*Response, error) {
return h.Request(ctx, HTTP_METHOD_DELETE, url, args...)
}
// Options makes an HTTP OPTIONS request and returns a corresponding response by taking goja.Values as arguments
func (h *HTTP) Options(ctx context.Context, url goja.Value, args ...goja.Value) (*Response, error) {
return h.Request(ctx, HTTP_METHOD_OPTIONS, url, args...)
}
// Request makes an http request of the provided `method` and returns a corresponding response by
// taking goja.Values as arguments
func (h *HTTP) Request(ctx context.Context, method string, url goja.Value, args ...goja.Value) (*Response, error) {
u, err := ToURL(url)
if err != nil {
return nil, err
}
var body interface{}
var params goja.Value
if len(args) > 0 {
body = args[0].Export()
}
if len(args) > 1 {
params = args[1]
}
req, err := h.parseRequest(ctx, method, u, body, params)
if err != nil {
return nil, err
}
resp, err := httpext.MakeRequest(ctx, req)
if err != nil {
return nil, err
}
return responseFromHttpext(resp), nil
}
//TODO break this function up
//nolint: gocyclo
func (h *HTTP) parseRequest(
ctx context.Context, method string, reqURL httpext.URL, body interface{}, params goja.Value,
) (*httpext.ParsedHTTPRequest, error) {
rt := common.GetRuntime(ctx)
state := lib.GetState(ctx)
if state == nil {
return nil, ErrHTTPForbiddenInInitContext
}
result := &httpext.ParsedHTTPRequest{
URL: &reqURL,
Req: &http.Request{
Method: method,
URL: reqURL.GetURL(),
Header: make(http.Header),
},
Timeout: 60 * time.Second,
Throw: state.Options.Throw.Bool,
Redirects: state.Options.MaxRedirects,
Cookies: make(map[string]*httpext.HTTPRequestCookie),
Tags: make(map[string]string),
ResponseCallback: state.HTTPResponseCallback,
}
if state.Options.DiscardResponseBodies.Bool {
result.ResponseType = httpext.ResponseTypeNone
} else {
result.ResponseType = httpext.ResponseTypeText
}
formatFormVal := func(v interface{}) string {
// TODO: handle/warn about unsupported/nested values
return fmt.Sprintf("%v", v)
}
handleObjectBody := func(data map[string]interface{}) error {
if !requestContainsFile(data) {
bodyQuery := make(url.Values, len(data))
for k, v := range data {
bodyQuery.Set(k, formatFormVal(v))
}
result.Body = bytes.NewBufferString(bodyQuery.Encode())
result.Req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return nil
}
// handling multipart request
result.Body = &bytes.Buffer{}
mpw := multipart.NewWriter(result.Body)
// For parameters of type common.FileData, created with open(file, "b"),
// we write the file boundary to the body buffer.
// Otherwise parameters are treated as standard form field.
for k, v := range data {
switch ve := v.(type) {
case FileData:
// writing our own part to handle receiving
// different content-type than the default application/octet-stream
h := make(textproto.MIMEHeader)
escapedFilename := escapeQuotes(ve.Filename)
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
k, escapedFilename))
h.Set("Content-Type", ve.ContentType)
// this writer will be closed either by the next part or
// the call to mpw.Close()
fw, err := mpw.CreatePart(h)
if err != nil {
return err
}
if _, err := fw.Write(ve.Data); err != nil {
return err
}
default:
fw, err := mpw.CreateFormField(k)
if err != nil {
return err
}
if _, err := fw.Write([]byte(formatFormVal(v))); err != nil {
return err
}
}
}
if err := mpw.Close(); err != nil {
return err
}
result.Req.Header.Set("Content-Type", mpw.FormDataContentType())
return nil
}
if body != nil {
switch data := body.(type) {
case map[string]goja.Value:
// TODO: fix forms submission and serialization in k6/html before fixing this..
newData := map[string]interface{}{}
for k, v := range data {
newData[k] = v.Export()
}
if err := handleObjectBody(newData); err != nil {
return nil, err
}
case goja.ArrayBuffer:
result.Body = bytes.NewBuffer(data.Bytes())
case map[string]interface{}:
if err := handleObjectBody(data); err != nil {
return nil, err
}
case string:
result.Body = bytes.NewBufferString(data)
case []byte:
result.Body = bytes.NewBuffer(data)
default:
return nil, fmt.Errorf("unknown request body type %T", body)
}
}
result.Req.Header.Set("User-Agent", state.Options.UserAgent.String)
if state.CookieJar != nil {
result.ActiveJar = state.CookieJar
}
// TODO: ditch goja.Value, reflections and Object and use a simple go map and type assertions?
if params != nil && !goja.IsUndefined(params) && !goja.IsNull(params) {
params := params.ToObject(rt)
for _, k := range params.Keys() {
switch k {
case "cookies":
cookiesV := params.Get(k)
if goja.IsUndefined(cookiesV) || goja.IsNull(cookiesV) {
continue
}
cookies := cookiesV.ToObject(rt)
if cookies == nil {
continue
}
for _, key := range cookies.Keys() {
cookieV := cookies.Get(key)
if goja.IsUndefined(cookieV) || goja.IsNull(cookieV) {
continue
}
switch cookieV.ExportType() {
case reflect.TypeOf(map[string]interface{}{}):
result.Cookies[key] = &httpext.HTTPRequestCookie{Name: key, Value: "", Replace: false}
cookie := cookieV.ToObject(rt)
for _, attr := range cookie.Keys() {
switch strings.ToLower(attr) {
case "replace":
result.Cookies[key].Replace = cookie.Get(attr).ToBoolean()
case "value":
result.Cookies[key].Value = cookie.Get(attr).String()
}
}
default:
result.Cookies[key] = &httpext.HTTPRequestCookie{Name: key, Value: cookieV.String(), Replace: false}
}
}
case "headers":
headersV := params.Get(k)
if goja.IsUndefined(headersV) || goja.IsNull(headersV) {
continue
}
headers := headersV.ToObject(rt)
if headers == nil {
continue
}
for _, key := range headers.Keys() {
str := headers.Get(key).String()
if strings.ToLower(key) == "host" {
result.Req.Host = str
}
result.Req.Header.Set(key, str)
}
case "jar":
jarV := params.Get(k)
if goja.IsUndefined(jarV) || goja.IsNull(jarV) {
continue
}
switch v := jarV.Export().(type) {
case *HTTPCookieJar:
result.ActiveJar = v.jar
}
case "compression":
algosString := strings.TrimSpace(params.Get(k).ToString().String())
if algosString == "" {
continue
}
algos := strings.Split(algosString, ",")
var err error
result.Compressions = make([]httpext.CompressionType, len(algos))
for index, algo := range algos {
algo = strings.TrimSpace(algo)
result.Compressions[index], err = httpext.CompressionTypeString(algo)
if err != nil {
return nil, fmt.Errorf("unknown compression algorithm %s, supported algorithms are %s",
algo, httpext.CompressionTypeValues())
}
}
case "redirects":
result.Redirects = null.IntFrom(params.Get(k).ToInteger())
case "tags":
tagsV := params.Get(k)
if goja.IsUndefined(tagsV) || goja.IsNull(tagsV) {
continue
}
tagObj := tagsV.ToObject(rt)
if tagObj == nil {
continue
}
for _, key := range tagObj.Keys() {
result.Tags[key] = tagObj.Get(key).String()
}
case "auth":
result.Auth = params.Get(k).String()
case "timeout":
t, err := types.GetDurationValue(params.Get(k).Export())
if err != nil {
return nil, fmt.Errorf("invalid timeout value: %w", err)
}
result.Timeout = t
case "throw":
result.Throw = params.Get(k).ToBoolean()
case "responseType":
responseType, err := httpext.ResponseTypeString(params.Get(k).String())
if err != nil {
return nil, err
}
result.ResponseType = responseType
case "responseCallback":
v := params.Get(k).Export()
if v == nil {
result.ResponseCallback = nil
} else if c, ok := v.(*expectedStatuses); ok {
result.ResponseCallback = c.match
} else {
return nil, fmt.Errorf("unsupported responseCallback")
}
}
}
}
if result.ActiveJar != nil {
httpext.SetRequestCookies(result.Req, result.ActiveJar, result.Cookies)
}
return result, nil
}
func (h *HTTP) prepareBatchArray(
ctx context.Context, requests []interface{},
) ([]httpext.BatchParsedHTTPRequest, []*Response, error) {
reqCount := len(requests)
batchReqs := make([]httpext.BatchParsedHTTPRequest, reqCount)
results := make([]*Response, reqCount)
for i, req := range requests {
parsedReq, err := h.parseBatchRequest(ctx, i, req)
if err != nil {
return nil, nil, err
}
response := new(httpext.Response)
batchReqs[i] = httpext.BatchParsedHTTPRequest{
ParsedHTTPRequest: parsedReq,
Response: response,
}
results[i] = &Response{response}
}
return batchReqs, results, nil
}
func (h *HTTP) prepareBatchObject(
ctx context.Context, requests map[string]interface{},
) ([]httpext.BatchParsedHTTPRequest, map[string]*Response, error) {
reqCount := len(requests)
batchReqs := make([]httpext.BatchParsedHTTPRequest, reqCount)
results := make(map[string]*Response, reqCount)
i := 0
for key, req := range requests {
parsedReq, err := h.parseBatchRequest(ctx, key, req)
if err != nil {
return nil, nil, err
}
response := new(httpext.Response)
batchReqs[i] = httpext.BatchParsedHTTPRequest{
ParsedHTTPRequest: parsedReq,
Response: response,
}
results[key] = &Response{response}
i++
}
return batchReqs, results, nil
}
// Batch makes multiple simultaneous HTTP requests. The provideds reqsV should be an array of request
// objects. Batch returns an array of responses and/or error
func (h *HTTP) Batch(ctx context.Context, reqsV goja.Value) (goja.Value, error) {
state := lib.GetState(ctx)
if state == nil {
return nil, ErrBatchForbiddenInInitContext
}
var (
err error
batchReqs []httpext.BatchParsedHTTPRequest
results interface{} // either []*Response or map[string]*Response
)
switch v := reqsV.Export().(type) {
case []interface{}:
batchReqs, results, err = h.prepareBatchArray(ctx, v)
case map[string]interface{}:
batchReqs, results, err = h.prepareBatchObject(ctx, v)
default:
return nil, fmt.Errorf("invalid http.batch() argument type %T", v)
}
if err != nil {
return nil, err
}
reqCount := len(batchReqs)
errs := httpext.MakeBatchRequests(
ctx, batchReqs, reqCount,
int(state.Options.Batch.Int64), int(state.Options.BatchPerHost.Int64),
)
for i := 0; i < reqCount; i++ {
if e := <-errs; e != nil && err == nil { // Save only the first error
err = e
}
}
return common.GetRuntime(ctx).ToValue(results), err
}
func (h *HTTP) parseBatchRequest(
ctx context.Context, key interface{}, val interface{},
) (*httpext.ParsedHTTPRequest, error) {
var (
method = HTTP_METHOD_GET
ok bool
err error
reqURL httpext.URL
body interface{}
params goja.Value
rt = common.GetRuntime(ctx)
)
switch data := val.(type) {
case []interface{}:
// Handling of ["GET", "http://example.com/"]
dataLen := len(data)
if dataLen < 2 {
return nil, fmt.Errorf("invalid batch request '%#v'", data)
}
method, ok = data[0].(string)
if !ok {
return nil, fmt.Errorf("invalid method type '%#v'", data[0])
}
reqURL, err = ToURL(data[1])
if err != nil {
return nil, err
}
if dataLen > 2 {
body = data[2]
}
if dataLen > 3 {
params = rt.ToValue(data[3])
}
case map[string]interface{}:
// Handling of {method: "GET", url: "https://test.k6.io"}
if murl, ok := data["url"]; !ok {
return nil, fmt.Errorf("batch request %q doesn't have an url key", key)
} else if reqURL, err = ToURL(murl); err != nil {
return nil, err
}
body = data["body"] // It's fine if it's missing, the map lookup will return
if newMethod, ok := data["method"]; ok {
if method, ok = newMethod.(string); !ok {
return nil, fmt.Errorf("invalid method type '%#v'", newMethod)
}
method = strings.ToUpper(method)
if method == HTTP_METHOD_GET || method == HTTP_METHOD_HEAD {
body = nil
}
}
if p, ok := data["params"]; ok {
params = rt.ToValue(p)
}
default:
// Handling of "http://example.com/" or http.url`http://example.com/{$id}`
reqURL, err = ToURL(data)
if err != nil {
return nil, err
}
}
return h.parseRequest(ctx, method, reqURL, body, params)
}
func requestContainsFile(data map[string]interface{}) bool {
for _, v := range data {
switch v.(type) {
case FileData:
return true
}
}
return false
}