/
http.go
452 lines (397 loc) · 13.1 KB
/
http.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
// Package http provides tools for integrating HTTP request handling within Go-based web servers and clients with Starlark scripting capabilities.
// This enables dynamic inspection and modification of HTTP requests and responses through scripts, enhancing flexibility and control over processing.
//
// Migrated from: https://github.com/qri-io/starlib/tree/master/http with modifications.
package http
import (
"bytes"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/1set/starlet/dataconv"
itn "github.com/1set/starlet/internal"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
)
// ModuleName defines the expected name for this Module when used in starlark's load() function, eg: load('http', 'get')
const ModuleName = "http"
var (
// UserAgent is the default user agent for http requests, override with a custom value before calling LoadModule.
UserAgent = "Starlet-http-client/" + itn.StarletVersion
// TimeoutSecond is the default timeout in seconds for http requests, override with a custom value before calling LoadModule.
TimeoutSecond = 30.0
// SkipInsecureVerify controls whether to skip TLS verification, override with a custom value before calling LoadModule.
SkipInsecureVerify = false
// DisableRedirect controls whether to follow redirects, override with a custom value before calling LoadModule.
DisableRedirect = false
// Client is the http client used to create the http module, override with a custom client before calling LoadModule.
Client *http.Client
// Guard is a global RequestGuard used in LoadModule, override with a custom implementation before calling LoadModule.
Guard RequestGuard
// ConfigLock is a global lock for settings, use it to ensure thread safety when setting.
ConfigLock sync.RWMutex
)
// RequestGuard controls access to http by checking before making requests
// if Allowed returns an error the request will be denied
type RequestGuard interface {
Allowed(thread *starlark.Thread, req *http.Request) (*http.Request, error)
}
// LoadModule creates an http Module
func LoadModule() (starlark.StringDict, error) {
var m = &Module{}
if Client != nil {
m.cli = Client
}
if Guard != nil {
m.rg = Guard
}
ns := starlark.StringDict{
ModuleName: m.Struct(),
}
return ns, nil
}
// Module joins http tools to a dataset, allowing dataset
// to follow along with http requests
type Module struct {
cli *http.Client
rg RequestGuard
}
// Struct returns this module's methods as a starlark Struct
func (m *Module) Struct() *starlarkstruct.Struct {
return starlarkstruct.FromStringDict(starlarkstruct.Default, m.StringDict())
}
// StringDict returns all module methods in a starlark.StringDict
func (m *Module) StringDict() starlark.StringDict {
methods := []string{"get", "put", "post", "postForm", "delete", "head", "patch", "options"}
sd := make(starlark.StringDict, len(methods))
for _, name := range methods {
sd[name] = starlark.NewBuiltin(ModuleName+"."+name, m.reqMethod(name))
}
sd["set_timeout"] = starlark.NewBuiltin(ModuleName+".set_timeout", setRequestTimeout)
sd["get_timeout"] = starlark.NewBuiltin(ModuleName+".get_timeout", getRequestTimeout)
return sd
}
// setRequestTimeout sets the timeout for http requests
func setRequestTimeout(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var timeout itn.FloatOrInt
if err := starlark.UnpackArgs(b.Name(), args, kwargs, "timeout", &timeout); err != nil {
return nil, err
}
if timeout < 0 {
return nil, fmt.Errorf("%s: timeout must be non-negative", b.Name())
}
// update the global TimeoutSecond variable which influences all future HTTP requests.
ConfigLock.Lock()
defer ConfigLock.Unlock()
TimeoutSecond = float64(timeout)
return starlark.None, nil
}
// getRequestTimeout returns the current timeout for http requests
func getRequestTimeout(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
// check the arguments: no arguments
if err := starlark.UnpackPositionalArgs(b.Name(), args, kwargs, 0); err != nil {
return nil, err
}
ConfigLock.RLock()
defer ConfigLock.RUnlock()
return starlark.Float(TimeoutSecond), nil
}
// reqMethod is a factory function for generating starlark builtin functions for different http request methods
func (m *Module) reqMethod(method string) func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
return func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var (
urlv starlark.String
params = &starlark.Dict{}
headers = &starlark.Dict{}
formBody = &starlark.Dict{}
formEncoding starlark.String
auth starlark.Tuple
body itn.StringOrBytes
jsonBody starlark.Value
timeout = itn.FloatOrInt(TimeoutSecond)
allowRedirect = starlark.Bool(!DisableRedirect)
verifySSL = starlark.Bool(!SkipInsecureVerify)
)
if err := starlark.UnpackArgs(b.Name(), args, kwargs, "url", &urlv, "params?", ¶ms, "headers", &headers, "body", &body, "form_body", &formBody, "form_encoding", &formEncoding, "json_body", &jsonBody,
"auth", &auth, "timeout?", &timeout, "allow_redirects?", &allowRedirect, "verify?", &verifySSL); err != nil {
return nil, err
}
rawURL, err := AsString(urlv)
if err != nil {
return nil, err
}
if err = setQueryParams(&rawURL, params); err != nil {
return nil, err
}
req, err := http.NewRequest(strings.ToUpper(method), rawURL, nil)
if err != nil {
return nil, err
}
if m.rg != nil {
req, err = m.rg.Allowed(thread, req)
if err != nil {
return nil, err
}
}
if err = setHeaders(req, headers); err != nil {
return nil, err
}
if err = setAuth(req, auth); err != nil {
return nil, err
}
if err = setBody(req, body.StarlarkString(), formBody, formEncoding, jsonBody); err != nil {
return nil, err
}
cli := m.getHTTPClient(float64(timeout), bool(allowRedirect), bool(verifySSL))
res, err := cli.Do(req)
if err != nil {
return nil, err
}
r := &Response{*res}
return r.Struct(), nil
}
}
func (m *Module) getHTTPClient(timeoutSec float64, allowRedirect, verifySSL bool) *http.Client {
// return existing client if set
if m.cli != nil {
return m.cli
}
// set timeout to 30 seconds if it's negative
if timeoutSec < 0 {
timeoutSec = 30
}
cli := &http.Client{Timeout: time.Duration(timeoutSec * float64(time.Second))}
// skip TLS verification if set
if !verifySSL {
tr := http.DefaultTransport.(*http.Transport).Clone()
tr.TLSClientConfig.InsecureSkipVerify = true
cli.Transport = tr
}
// disable redirects if set
if !allowRedirect {
cli.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
return cli
}
// AsString unquotes a starlark string value
func AsString(x starlark.Value) (string, error) {
return strconv.Unquote(x.String())
}
// Encodings for form data.
// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
const (
formEncodingMultipart = "multipart/form-data"
formEncodingURL = "application/x-www-form-urlencoded"
)
func setQueryParams(rawurl *string, params *starlark.Dict) error {
keys := params.Keys()
if len(keys) == 0 {
return nil
}
u, err := url.Parse(*rawurl)
if err != nil {
return err
}
q := u.Query()
for _, key := range keys {
keystr, err := AsString(key)
if err != nil {
return err
}
val, _, err := params.Get(key)
if err != nil {
return err
}
if val.Type() != "string" {
return fmt.Errorf("expected param value for key '%s' to be a string. got: '%s'", key, val.Type())
}
valstr, err := AsString(val)
if err != nil {
return err
}
q.Set(keystr, valstr)
}
u.RawQuery = q.Encode()
*rawurl = u.String()
return nil
}
func setAuth(req *http.Request, auth starlark.Tuple) error {
if len(auth) == 0 {
return nil
} else if len(auth) == 2 {
username, err := AsString(auth[0])
if err != nil {
return fmt.Errorf("parsing auth username string: %s", err.Error())
}
password, err := AsString(auth[1])
if err != nil {
return fmt.Errorf("parsing auth password string: %s", err.Error())
}
req.SetBasicAuth(username, password)
return nil
}
return fmt.Errorf("expected two values for auth params tuple")
}
func setHeaders(req *http.Request, headers *starlark.Dict) error {
var (
keys = headers.Keys()
UAKey = "User-Agent"
isUASet = false
)
for _, key := range keys {
keystr, err := AsString(key)
if err != nil {
return err
}
val, _, err := headers.Get(key)
if err != nil {
return err
}
if val.Type() != "string" {
return fmt.Errorf("expected param value for key '%s' to be a string. got: '%s'", key, val.Type())
}
valstr, err := AsString(val)
if err != nil {
return err
}
req.Header.Add(keystr, valstr)
if keystr == UAKey {
isUASet = true
}
}
if UserAgent != "" && !isUASet {
req.Header.Set(UAKey, UserAgent)
}
return nil
}
func setBody(req *http.Request, body starlark.String, formData *starlark.Dict, formEncoding starlark.String, jsondata starlark.Value) error {
if !dataconv.IsEmptyString(body) {
uq, err := strconv.Unquote(body.String())
if err != nil {
return err
}
req.Body = ioutil.NopCloser(strings.NewReader(uq))
// Specifying the Content-Length ensures that https://go.dev/src/net/http/transfer.go doesnt specify Transfer-Encoding: chunked which is not supported by some endpoints.
// This is required when using ioutil.NopCloser method for the request body (see ShouldSendChunkedRequestBody() in the library mentioned above).
req.ContentLength = int64(len(uq))
return nil
}
if jsondata != nil && jsondata.String() != "" {
req.Header.Set("Content-Type", "application/json")
data, err := dataconv.MarshalStarlarkJSON(jsondata, 0)
if err != nil {
return err
}
req.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(data)))
req.ContentLength = int64(len(data))
}
if formData != nil && formData.Len() > 0 {
form := url.Values{}
for _, key := range formData.Keys() {
keystr, err := AsString(key)
if err != nil {
return err
}
val, _, err := formData.Get(key)
if err != nil {
return err
}
if val.Type() != "string" {
return fmt.Errorf("expected param value for key '%s' to be a string. got: '%s'", key, val.Type())
}
valstr, err := AsString(val)
if err != nil {
return err
}
form.Add(keystr, valstr)
}
var contentType string
switch formEncoding {
case formEncodingURL, "":
contentType = formEncodingURL
req.Body = ioutil.NopCloser(strings.NewReader(form.Encode()))
req.ContentLength = int64(len(form.Encode()))
case formEncodingMultipart:
var b bytes.Buffer
mw := multipart.NewWriter(&b)
defer mw.Close()
contentType = mw.FormDataContentType()
for k, values := range form {
for _, v := range values {
w, err := mw.CreateFormField(k)
if err != nil {
return err
}
if _, err := w.Write([]byte(v)); err != nil {
return err
}
}
}
req.Body = ioutil.NopCloser(&b)
default:
return fmt.Errorf("unknown form encoding: %s", formEncoding)
}
if req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", contentType)
}
}
return nil
}
// Response represents an HTTP response, wrapping a go http.Response with
// starlark methods
type Response struct {
http.Response
}
// Struct turns a response into a *starlark.Struct
func (r *Response) Struct() *starlarkstruct.Struct {
return starlarkstruct.FromStringDict(starlarkstruct.Default, starlark.StringDict{
"url": starlark.String(r.Request.URL.String()),
"status_code": starlark.MakeInt(r.StatusCode),
"headers": r.HeadersDict(),
"encoding": starlark.String(strings.Join(r.TransferEncoding, ",")),
"body": starlark.NewBuiltin("body", r.Text),
"json": starlark.NewBuiltin("json", r.JSON),
})
}
// HeadersDict flops
func (r *Response) HeadersDict() *starlark.Dict {
d := new(starlark.Dict)
for key, vals := range r.Header {
if err := d.SetKey(starlark.String(key), starlark.String(strings.Join(vals, ","))); err != nil {
panic(err)
}
}
return d
}
// Text returns the raw data as a string
func (r *Response) Text(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
// reset reader to allow multiple calls
_ = r.Body.Close()
r.Body = ioutil.NopCloser(bytes.NewReader(data))
// wraps as result
return starlark.String(data), nil
}
// JSON attempts to parse the response body as JSON
func (r *Response) JSON(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
// reset reader to allow multiple calls
_ = r.Body.Close()
r.Body = ioutil.NopCloser(bytes.NewReader(body))
// use internal marshaler to support starlark types
return dataconv.UnmarshalStarlarkJSON(body)
}