-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathhttp.go
381 lines (327 loc) · 9.75 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
package http
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"strconv"
"strings"
util "github.com/qri-io/starlib/util"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
)
// AsString unquotes a starlark string value
func AsString(x starlark.Value) (string, error) {
return strconv.Unquote(x.String())
}
// ModuleName defines the expected name for this Module when used
// in starlark's load() function, eg: load('http.star', 'http')
const ModuleName = "http.star"
var (
// Client is the http client used to create the http module. override with
// a custom client before calling LoadModule
Client = http.DefaultClient
// Guard is a global RequestGuard used in LoadModule. override with a custom
// implementation before calling LoadModule
Guard RequestGuard
)
// 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"
)
// LoadModule creates an http Module
func LoadModule() (starlark.StringDict, error) {
var m = &Module{cli: Client}
if Guard != nil {
m.rg = Guard
}
ns := starlark.StringDict{
"http": m.Struct(),
}
return ns, nil
}
// 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)
}
// 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 {
return starlark.StringDict{
"get": starlark.NewBuiltin("get", m.reqMethod("get")),
"put": starlark.NewBuiltin("put", m.reqMethod("put")),
"post": starlark.NewBuiltin("post", m.reqMethod("post")),
"delete": starlark.NewBuiltin("delete", m.reqMethod("delete")),
"patch": starlark.NewBuiltin("patch", m.reqMethod("patch")),
"options": starlark.NewBuiltin("options", m.reqMethod("options")),
}
}
// 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, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
return func(thread *starlark.Thread, _ *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 starlark.String
jsonBody starlark.Value
)
if err := starlark.UnpackArgs(method, args, kwargs, "url", &urlv, "params?", ¶ms, "headers", &headers, "body", &body, "form_body", &formBody, "form_encoding", &formEncoding, "json_body", &jsonBody, "auth", &auth); 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, formBody, formEncoding, jsonBody); err != nil {
return nil, err
}
res, err := m.cli.Do(req)
if err != nil {
return nil, err
}
r := &Response{*res}
return r.Struct(), nil
}
}
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 {
keys := headers.Keys()
if len(keys) == 0 {
return nil
}
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)
}
return nil
}
func setBody(req *http.Request, body starlark.String, formData *starlark.Dict, formEncoding starlark.String, jsondata starlark.Value) error {
if !util.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")
v, err := util.Unmarshal(jsondata)
if err != nil {
return err
}
data, err := json.Marshal(v)
if err != nil {
return err
}
req.Body = ioutil.NopCloser(bytes.NewBuffer(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
}
r.Body.Close()
// reset reader to allow multiple calls
r.Body = ioutil.NopCloser(bytes.NewReader(data))
return starlark.String(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) {
var data interface{}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &data); err != nil {
return nil, err
}
r.Body.Close()
// reset reader to allow multiple calls
r.Body = ioutil.NopCloser(bytes.NewReader(body))
return util.Marshal(data)
}