-
Notifications
You must be signed in to change notification settings - Fork 18
/
cookies.go
186 lines (176 loc) · 4.02 KB
/
cookies.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
package requests
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/gospider007/gson"
)
// cookies
type Cookies []*http.Cookie
// return cookies with string,join with '; '
func (obj Cookies) String() string {
cooks := []string{}
for _, cook := range obj {
cooks = append(cooks, fmt.Sprintf("%s=%s", cook.Name, cook.Value))
}
return strings.Join(cooks, "; ")
}
// get cookies by name
func (obj Cookies) Gets(name string) Cookies {
var result Cookies
for _, cook := range obj {
if cook.Name == name {
result = append(result, cook)
}
}
return result
}
// get cookie by name
func (obj Cookies) Get(name string) *http.Cookie {
vals := obj.Gets(name)
if i := len(vals); i == 0 {
return nil
} else {
return vals[i-1]
}
}
// get cookie values by name, return []string
func (obj Cookies) GetVals(name string) []string {
var result []string
for _, cook := range obj {
if cook.Name == name {
result = append(result, cook.Value)
}
}
return result
}
// get cookie value by name,return string
func (obj Cookies) GetVal(name string) string {
vals := obj.GetVals(name)
if i := len(vals); i == 0 {
return ""
} else {
return vals[i-1]
}
}
func (obj Cookies) append(cook *http.Cookie) Cookies {
return append(obj, cook)
}
// read cookies or parse cookies,support json,map,[]string,http.Header,string
func ReadCookies(val any) (Cookies, error) {
switch cook := val.(type) {
case *http.Cookie:
return Cookies{
cook,
}, nil
case http.Cookie:
return Cookies{
&cook,
}, nil
case Cookies:
return cook, nil
case []*http.Cookie:
return Cookies(cook), nil
case string:
return http.ParseCookie(cook)
case http.Header:
return nil, errors.New("cookies not support type")
case []string:
return nil, errors.New("cookies not support type")
default:
return any2Cookies(cook)
}
}
// read set cookies or parse set cookies,support json,map,[]string,http.Header,string
//
// func ReadSetCookies(val any) (Cookies, error) {
// switch cook := val.(type) {
// case Cookies:
// return cook, nil
// case []*http.Cookie:
// return Cookies(cook), nil
// case string:
// http.ParseCookie()
// return http.ParseSetCookie(cook)
// case http.Header:
// return readSetCookies(cook), nil
// case []string:
// return readSetCookies(http.Header{"Set-Cookie": cook}), nil
// default:
// return any2Cookies(cook)
// }
// }
func any2Cookies(val any) (Cookies, error) {
switch cooks := val.(type) {
case map[string]string:
cookies := Cookies{}
for kk, vv := range cooks {
cookies = append(cookies, &http.Cookie{
Name: kk,
Value: vv,
})
}
return cookies, nil
case map[string][]string:
cookies := Cookies{}
for kk, vvs := range cooks {
for _, vv := range vvs {
cookies = append(cookies, &http.Cookie{
Name: kk,
Value: vv,
})
}
}
return cookies, nil
case *gson.Client:
if !cooks.IsObject() {
return nil, errors.New("cookies not support type")
}
cookies := Cookies{}
for kk, vvs := range cooks.Map() {
if vvs.IsArray() {
for _, vv := range vvs.Array() {
cookies = append(cookies, &http.Cookie{
Name: kk,
Value: vv.String(),
})
}
} else {
cookies = append(cookies, &http.Cookie{
Name: kk,
Value: vvs.String(),
})
}
}
return cookies, nil
default:
jsonData, err := gson.Decode(cooks)
if err != nil {
return nil, err
}
cookies := Cookies{}
for kk, vvs := range jsonData.Map() {
if vvs.IsArray() {
for _, vv := range vvs.Array() {
cookies = append(cookies, &http.Cookie{
Name: kk,
Value: vv.String(),
})
}
} else {
cookies = append(cookies, &http.Cookie{
Name: kk,
Value: vvs.String(),
})
}
}
return cookies, nil
}
}
func (obj *RequestOption) initCookies() (Cookies, error) {
if obj.Cookies == nil {
return nil, nil
}
return ReadCookies(obj.Cookies)
}