-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
103 lines (100 loc) · 2.37 KB
/
server.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
package tengohttp
import (
"io/ioutil"
"net/http"
"strconv"
"github.com/d5/tengo/v2"
)
func (s *server) read(args ...tengo.Object) (tengo.Object, error) {
var pst bool
switch len(args) {
case 2:
pst = !args[1].IsFalsy()
fallthrough
case 1:
switch v := args[0].(type) {
case *tengo.String:
if a := v.Value; a != "" {
if pst {
a = s.r.PostFormValue(a)
} else {
a = s.r.FormValue(a)
}
if a != "" {
return &tengo.String{Value: a}, nil
} else if pst && s.r.PostForm.Has(v.Value) || !pst && s.r.Form.Has(v.Value) {
return &tengo.String{}, nil
}
}
case *tengo.Bool:
pst = !v.IsFalsy()
if pst {
if s.r.PostForm == nil {
s.r.ParseForm()
}
return vals2map(s.r.PostForm), nil
} else {
if s.r.Form == nil {
s.r.ParseForm()
}
return vals2map(s.r.Form), nil
}
default:
return nil, tengo.ErrInvalidArgumentType{Name: "first", Expected: "string/bool", Found: args[0].TypeName()}
}
case 0:
if b, e := ioutil.ReadAll(s.r.Body); e != nil {
return &tengo.Error{Value: &tengo.String{Value: e.Error()}}, nil
} else {
return &tengo.Bytes{Value: b}, nil
}
default:
return nil, tengo.ErrWrongNumArguments
}
return nil, nil
}
func (s *server) write(args ...tengo.Object) (tengo.Object, error) {
c := 0
for n, arg := range args {
switch a := arg.(type) {
case *tengo.Map:
if s.h {
return nil, tengo.ErrInvalidArgumentType{Name: strconv.Itoa(n) + "-th", Expected: "nor map or int", Found: a.TypeName()}
}
for k, vs := range map2vals(a) {
for _, v := range vs {
s.w.Header().Add(k, v)
}
}
case *tengo.Int:
if s.h {
return nil, tengo.ErrInvalidArgumentType{Name: strconv.Itoa(n) + "-th", Expected: "nor map or int", Found: a.TypeName()}
}
s.h, c = true, int(a.Value)
default:
var e error
if c > 0 {
if v, o := tengo.ToString(a); !o {
s.w.WriteHeader(c)
} else if c < 300 {
s.w.WriteHeader(c)
_, e = s.w.Write([]byte(v))
} else if c < 400 {
http.Redirect(s.w, s.r, v, c)
} else {
http.Error(s.w, v, c)
}
} else if v, o := a.(*tengo.Bytes); o {
_, e = s.w.Write(v.Value)
} else if v, o := tengo.ToString(a); o {
_, e = s.w.Write([]byte(v))
}
if e != nil {
return &tengo.Error{Value: &tengo.String{Value: e.Error()}}, nil
} else {
c, s.h = 0, true
}
}
}
return nil, nil
}