-
Notifications
You must be signed in to change notification settings - Fork 23
/
body.go
347 lines (337 loc) · 8.27 KB
/
body.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
package requests
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"reflect"
"github.com/gospider007/gson"
"github.com/gospider007/tools"
"golang.org/x/exp/maps"
)
const (
readType = iota
mapType
)
type OrderMap struct {
data map[string]any
keys []string
}
func NewOrderMap() *OrderMap {
return &OrderMap{
data: make(map[string]any),
keys: []string{},
}
}
func (obj *OrderMap) Set(key string, val any) {
obj.Del(key)
obj.data[key] = val
obj.keys = append(obj.keys, key)
}
func (obj *OrderMap) Del(key string) {
delete(obj.data, key)
obj.keys = tools.DelSliceVals(obj.keys, key)
}
func (obj *OrderMap) Keys() []string {
return obj.keys
}
func (obj *OrderMap) parseHeaders() (map[string][]string, []string) {
head := make(http.Header)
for _, kk := range obj.keys {
if vvs, ok := obj.data[kk].([]any); ok {
for _, vv := range vvs {
head.Add(kk, fmt.Sprint(vv))
}
} else {
head.Add(kk, fmt.Sprint(obj.data[kk]))
}
}
return head, obj.keys
}
func formWrite(writer *multipart.Writer, key string, val any) (err error) {
switch value := val.(type) {
case File:
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, escapeQuotes(key), escapeQuotes(value.FileName)))
if value.ContentType == "" {
switch content := value.Content.(type) {
case []byte:
h.Set("Content-Type", http.DetectContentType(content))
case string:
h.Set("Content-Type", http.DetectContentType(tools.StringToBytes(content)))
case io.Reader:
h.Set("Content-Type", "application/octet-stream")
default:
con, err := gson.Encode(content)
if err != nil {
return err
}
h.Set("Content-Type", http.DetectContentType(con))
}
} else {
h.Set("Content-Type", value.ContentType)
}
var wp io.Writer
if wp, err = writer.CreatePart(h); err != nil {
return
}
switch content := value.Content.(type) {
case []byte:
_, err = wp.Write(content)
case string:
_, err = wp.Write(tools.StringToBytes(content))
case io.Reader:
_, err = io.Copy(wp, content)
default:
con, err := gson.Encode(content)
if err != nil {
return err
}
_, err = wp.Write(con)
if err != nil {
return err
}
}
case []byte:
err = writer.WriteField(key, tools.BytesToString(value))
case string:
err = writer.WriteField(key, value)
default:
con, _ := gson.Decode(val)
err = writer.WriteField(key, con.Raw())
if err != nil {
return err
}
}
return
}
func (obj *OrderMap) parseForm(ctx context.Context) (io.Reader, string, bool, error) {
if len(obj.keys) == 0 || len(obj.data) == 0 {
return nil, "", false, nil
}
if obj.isformPip() {
pr, pw := pipe(ctx)
writer := multipart.NewWriter(pw)
go func() {
err := obj.formWriteMain(writer)
if err == nil {
err = io.EOF
}
pr.Close(err)
pw.Close(err)
}()
return pr, writer.FormDataContentType(), true, nil
}
body := bytes.NewBuffer(nil)
writer := multipart.NewWriter(body)
err := obj.formWriteMain(writer)
if err != nil {
return nil, writer.FormDataContentType(), false, err
}
return bytes.NewReader(body.Bytes()), writer.FormDataContentType(), false, err
}
func (obj *OrderMap) isformPip() bool {
if len(obj.keys) == 0 || len(obj.data) == 0 {
return false
}
for _, key := range obj.keys {
if vals, ok := obj.data[key].([]any); ok {
for _, val := range vals {
if file, ok := val.(File); ok {
if _, ok := file.Content.(io.Reader); ok {
return true
}
}
}
} else {
if file, ok := obj.data[key].(File); ok {
if _, ok := file.Content.(io.Reader); ok {
return true
}
}
}
}
return false
}
func (obj *OrderMap) formWriteMain(writer *multipart.Writer) (err error) {
for _, key := range obj.keys {
if vals, ok := obj.data[key].([]any); ok {
for _, val := range vals {
if err = formWrite(writer, key, val); err != nil {
return
}
}
} else {
if err = formWrite(writer, key, obj.data[key]); err != nil {
return
}
}
}
return writer.Close()
}
func paramsWrite(buf *bytes.Buffer, key string, val any) {
if buf.Len() > 0 {
buf.WriteByte('&')
}
buf.WriteString(url.QueryEscape(key))
buf.WriteByte('=')
if v, err := gson.Decode(val); err == nil {
buf.WriteString(url.QueryEscape(v.Raw()))
} else {
buf.WriteString(url.QueryEscape(fmt.Sprintf("%v", val)))
}
}
func (obj *OrderMap) parseParams() *bytes.Buffer {
buf := bytes.NewBuffer(nil)
for _, k := range obj.keys {
if vals, ok := obj.data[k].([]any); ok {
for _, v := range vals {
paramsWrite(buf, k, v)
}
} else {
paramsWrite(buf, k, obj.data[k])
}
}
return buf
}
func (obj *OrderMap) parseData() *bytes.Reader {
val := obj.parseParams().Bytes()
if val == nil {
return nil
}
return bytes.NewReader(val)
}
func (obj *OrderMap) MarshalJSON() ([]byte, error) {
buf := bytes.NewBuffer(nil)
err := buf.WriteByte('{')
if err != nil {
return nil, err
}
for i, k := range obj.keys {
if i > 0 {
if err = buf.WriteByte(','); err != nil {
return nil, err
}
}
key, err := gson.Encode(k)
if err != nil {
return nil, err
}
if _, err = buf.Write(key); err != nil {
return nil, err
}
if err = buf.WriteByte(':'); err != nil {
return nil, err
}
val, err := gson.Encode(obj.data[k])
if err != nil {
return nil, err
}
if _, err = buf.Write(val); err != nil {
return nil, err
}
}
if err = buf.WriteByte('}'); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func any2Map(val any) map[string]any {
if reflect.TypeOf(val).Kind() != reflect.Map {
return nil
}
mapValue := reflect.ValueOf(val)
result := make(map[string]any)
for _, key := range mapValue.MapKeys() {
valueData := mapValue.MapIndex(key).Interface()
sliceValue := reflect.ValueOf(valueData)
if sliceValue.Kind() == reflect.Slice {
valueData2 := []any{}
for i := 0; i < sliceValue.Len(); i++ {
valueData2 = append(valueData2, sliceValue.Index(i).Interface())
}
result[fmt.Sprint(key.Interface())] = valueData2
} else {
result[fmt.Sprint(key.Interface())] = valueData
}
}
return result
}
func (obj *RequestOption) newBody(val any, valType int) (reader io.Reader, parseOrderMap *OrderMap, orderKey []string, err error) {
var isOrderMap bool
parseOrderMap, isOrderMap = val.(*OrderMap)
if isOrderMap {
if valType == readType {
readCon, err := parseOrderMap.MarshalJSON()
return bytes.NewReader(readCon), nil, nil, err
} else {
return nil, parseOrderMap, parseOrderMap.Keys(), nil
}
}
if valType == readType {
switch value := val.(type) {
case io.Reader:
obj.once = true
return value, nil, nil, nil
case string:
return bytes.NewReader(tools.StringToBytes(value)), nil, nil, nil
case []byte:
return bytes.NewReader(value), nil, nil, nil
default:
enData, err := gson.Encode(val)
if err != nil {
return nil, nil, nil, err
}
return bytes.NewReader(enData), nil, nil, nil
}
}
if mapData := any2Map(val); mapData != nil {
val = mapData
}
mapL:
switch value := val.(type) {
case *gson.Client:
if !value.IsObject() {
return nil, nil, nil, errors.New("body-type error")
}
orderMap := NewOrderMap()
for kk, vv := range value.Map() {
if vv.IsArray() {
valData := make([]any, len(vv.Array()))
for i, v := range vv.Array() {
valData[i] = v.Value()
}
orderMap.Set(kk, valData)
} else {
orderMap.Set(kk, vv.Value())
}
}
return nil, orderMap, nil, nil
case *OrderMap:
if mapData := any2Map(value.data); mapData != nil {
value.data = mapData
}
return nil, value, nil, nil
case map[string]any:
orderMap := NewOrderMap()
orderMap.data = value
orderMap.keys = maps.Keys(value)
return nil, orderMap, nil, nil
}
if val, err = gson.Decode(val); err != nil {
switch value := val.(type) {
case string:
return bytes.NewReader(tools.StringToBytes(value)), nil, nil, nil
case []byte:
return bytes.NewReader(value), nil, nil, nil
default:
return nil, nil, nil, err
}
}
goto mapL
}