-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbind.go
More file actions
200 lines (178 loc) · 4.38 KB
/
Copy pathbind.go
File metadata and controls
200 lines (178 loc) · 4.38 KB
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
package xmux
import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"io"
"net/http"
"reflect"
"strconv"
"strings"
yaml "gopkg.in/yaml.v2"
)
type bindType int
const (
jsonT bindType = 1
yamlT bindType = 2
xmlT bindType = 3
formT bindType = 4
headT bindType = 5
)
const (
MIMEJSON = "application/json"
MIMEXML = "application/xml"
MIMEXML2 = "text/xml"
MIMEPlain = "text/plain"
MIMEPOSTForm = "application/x-www-form-urlencoded"
MIMEMultipartPOSTForm = "multipart/form-data"
MIMEPROTOBUF = "application/x-protobuf"
MIMEMSGPACK = "application/x-msgpack"
MIMEMSGPACK2 = "application/msgpack"
MIMEYAML = "application/x-yaml"
)
func (r *router) unmarshalJson(req *http.Request, fd *FlowData) error {
b, err := io.ReadAll(req.Body)
if err != nil {
return err
}
req.Body.Close()
fd.Body = b
if len(b) > 0 {
err = json.Unmarshal(b, &fd.Data)
}
return err
}
func (r *router) unmarshalYaml(req *http.Request, fd *FlowData) error {
b, err := io.ReadAll(req.Body)
if err != nil {
return err
}
req.Body.Close()
fd.Body = b
if len(b) > 0 {
err = yaml.Unmarshal(b, &fd.Data)
}
return err
}
func (r *router) unmarshalXml(req *http.Request, fd *FlowData) error {
b, err := io.ReadAll(req.Body)
if err != nil {
return err
}
req.Body.Close()
fd.Body = b
if len(b) > 0 {
err = xml.Unmarshal(b, &fd.Data)
}
return err
}
func (r *router) bind(route *rt, w http.ResponseWriter, req *http.Request, fd *FlowData) bool {
// 数据绑定
defer req.Body.Close()
switch route.bindType {
case jsonT:
err := r.unmarshalJson(req, fd)
if err != nil {
return r.UnmarshalError(w, req, err)
}
return true
case yamlT:
err := r.unmarshalYaml(req, fd)
if err != nil {
return r.UnmarshalError(w, req, err)
}
return true
case xmlT:
err := r.unmarshalXml(req, fd)
if err != nil {
return r.UnmarshalError(w, req, err)
}
return true
case formT:
err := r.unmarsharForm(req, fd)
if err != nil {
return r.UnmarshalError(w, req, err)
}
return true
case headT:
// 根据请求头自动解析
ct := req.Header.Get("content-type")
headers := strings.Split(ct, ";")
for _, head := range headers {
head = strings.Trim(head, " ")
if strings.Contains(head, MIMEJSON) {
err := r.unmarshalJson(req, fd)
if err != nil {
return r.UnmarshalError(w, req, err)
}
return true
}
if strings.Contains(head, MIMEXML) || strings.Contains(head, MIMEXML2) {
err := r.unmarshalXml(req, fd)
if err != nil {
return r.UnmarshalError(w, req, err)
}
return true
}
if strings.Contains(head, MIMEPOSTForm) || strings.Contains(head, MIMEMultipartPOSTForm) {
err := r.unmarsharForm(req, fd)
if err != nil {
return r.UnmarshalError(w, req, err)
}
return true
}
}
}
return r.UnmarshalError(w, req, errors.New("unsupport content-type"))
}
func (r *router) unmarsharForm(req *http.Request, fd *FlowData) error {
cl := req.Header.Get("Content-Length")
length, err := strconv.Atoi(cl)
if err == nil && length > 0 {
b, err := io.ReadAll(req.Body)
if err != nil {
return err
}
if length > r.MaxPrintLength {
fd.Body = b[:r.MaxPrintLength]
} else {
fd.Body = b
}
req.Body = io.NopCloser(bytes.NewBuffer(b))
}
tt := reflect.TypeOf(fd.Data).Elem()
vv := reflect.ValueOf(fd.Data).Elem()
l := tt.NumField()
for i := 0; i < l; i++ {
keys := tt.Field(i).Tag.Get("form")
tagkeys := strings.Split(keys, ",")
key := tagkeys[0]
if key == "" {
continue
}
value := req.FormValue(key)
switch tt.Field(i).Type.Kind() {
case reflect.String:
vv.Field(i).Set(reflect.ValueOf(value))
case reflect.Float32:
b32, _ := strconv.ParseFloat(value, 32)
vv.Field(i).Set(reflect.ValueOf(b32))
case reflect.Float64:
b64, _ := strconv.ParseFloat(value, 64)
vv.Field(i).Set(reflect.ValueOf(b64))
case reflect.Bool:
ok, _ := strconv.ParseBool(value)
vv.Field(i).Set(reflect.ValueOf(ok))
case reflect.Int, reflect.Int64, reflect.Int8, reflect.Int16, reflect.Int32:
i64, _ := strconv.ParseInt(value, 10, 64)
vv.Field(i).SetInt(i64)
case reflect.Uint64, reflect.Uint, reflect.Uint16, reflect.Uint8, reflect.Uint32:
i64, _ := strconv.ParseUint(value, 10, 64)
vv.Field(i).SetUint(i64)
default:
return errors.New("not support type, url: " + req.URL.Path)
}
}
return nil
}