-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.go
124 lines (121 loc) · 3.39 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
package tengohttp
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/d5/tengo/v2"
)
func (s *server) request(args ...tengo.Object) (tengo.Object, error) {
var (
ctp string
met = "GET"
brd io.Reader
opt *tengo.Map
req *http.Request
)
switch len(args) {
case 2:
switch v := args[1].(type) {
case *tengo.String:
met = v.Value
case *tengo.Bytes:
brd, met, ctp = bytes.NewReader(v.Value), "POST", "text/plain"
case *tengo.Map:
opt = v
if o, k := opt.Value["body"]; k {
met, ctp = "POST", "text/plain"
switch v := o.(type) {
case *tengo.Map:
brd, ctp = strings.NewReader(url.Values(map2vals(v)).Encode()), "application/x-www-form-urlencoded"
case *tengo.Bytes:
brd = bytes.NewReader(v.Value)
default:
s, _ := tengo.ToString(o)
brd = strings.NewReader(s)
}
}
if o, k := opt.Value["method"]; k {
met, _ = tengo.ToString(o)
met = strings.ToUpper(met)
}
default:
return nil, tengo.ErrInvalidArgumentType{Name: "second", Expected: "map/bytes/string", Found: args[1].TypeName()}
}
fallthrough
case 1:
var e error
s, _ := tengo.ToString(args[0])
if req, e = http.NewRequest(met, s, brd); e != nil {
return &tengo.Error{Value: &tengo.String{Value: e.Error()}}, nil
} else if ctp != "" {
req.Header.Set("Content-Type", ctp)
}
default:
return nil, tengo.ErrWrongNumArguments
}
if opt != nil {
if o, k := opt.Value["query"]; k {
if q, k := o.(*tengo.Map); k {
req.URL.RawQuery = url.Values(map2vals(q)).Encode()
} else if s, _ := tengo.ToString(o); s != "" {
req.URL.RawQuery = s
}
}
if o, k := opt.Value["header"].(*tengo.Map); k {
for h, hv := range map2vals(o) {
req.Header[h] = hv
}
}
if o, k := opt.Value["cookies"].(*tengo.Array); k {
for _, v := range o.Value {
if m, k := v.(*tengo.Map); k {
if n, _ := tengo.ToString(m.Value["name"]); n != "" {
s, _ := tengo.ToString(m.Value["value"])
req.AddCookie(&http.Cookie{Name: n, Value: s})
}
}
}
}
if u, k := opt.Value["user"].(*tengo.String); k && u.Value != "" {
if p, k := opt.Value["pass"].(*tengo.String); k && p.Value != "" {
req.SetBasicAuth(u.Value, p.Value)
}
}
if o, k := opt.Value["follow"]; k {
if o.IsFalsy() {
s.c.CheckRedirect = func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }
}
}
if o, k := opt.Value["timeout"].(*tengo.Int); k && o.Value > 0 {
s.c.Timeout = time.Duration(o.Value) * time.Second
}
}
rsp, e := s.c.Do(req)
if e != nil {
return &tengo.Error{Value: &tengo.String{Value: e.Error()}}, nil
}
defer rsp.Body.Close()
bs, e := ioutil.ReadAll(rsp.Body)
if e != nil {
return &tengo.Error{Value: &tengo.String{Value: e.Error()}}, nil
}
cs := new(tengo.Array)
for _, cc := range rsp.Cookies() {
cs.Value = append(cs.Value, &tengo.Map{Value: map[string]tengo.Object{"name": &tengo.String{Value: cc.Name}, "value": &tengo.String{Value: cc.Value}}})
}
au, ap, _ := rsp.Request.BasicAuth()
return &tengo.Map{Value: map[string]tengo.Object{
"status": &tengo.Int{Value: int64(rsp.StatusCode)},
"user": &tengo.String{Value: au},
"pass": &tengo.String{Value: ap},
"header": vals2map(rsp.Header),
"cookies": cs,
"body": &tengo.Bytes{Value: bs},
"size": &tengo.Int{Value: rsp.ContentLength},
"url": &tengo.String{Value: rsp.Request.URL.String()},
}}, nil
}