-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie.go
259 lines (219 loc) · 8.01 KB
/
cookie.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
/**
* @Author: leafney
* @Date: 2023-03-04 17:06
* @Project: rose
* @HomePage: https://github.com/leafney
* @Description: cookies
*/
package rose
import (
"fmt"
"net/http"
"net/url"
"os"
"strings"
"time"
)
// CookieFromNV 通过键值设置 Cookie,默认不对值作编码处理
func CookieFromNV(name, value string) *http.Cookie {
return CookieFromNVEscape(name, value, false)
}
// CookieFromNVEscape 通过键值设置 Cookie,可选是否对值作编码处理
func CookieFromNVEscape(name, value string, escape bool) *http.Cookie {
val := value
if escape {
// cookie值可能包含特殊字符
val = url.QueryEscape(value)
}
return &http.Cookie{
Name: name,
Value: val,
}
}
// CookieFromNVs 通过 map 集合设置 Cookie,默认不对值作编码处理
func CookieFromNVs(cookies map[string]string) []*http.Cookie {
return CookieFromNVsEscape(cookies, false)
}
// CookieFromNVsEscape 通过 map 集合设置 Cookie,可选是否对值作编码处理
func CookieFromNVsEscape(cookies map[string]string, escape bool) []*http.Cookie {
newCookies := make([]*http.Cookie, 0)
for k, v := range cookies {
val := v
if escape {
// cookie值可能包含特殊字符
val = url.QueryEscape(v)
}
newCookies = append(newCookies, &http.Cookie{
Name: k,
Value: val,
})
}
return newCookies
}
// CookieFromStr 将 Cookie 字符串转换为 []*http.Cookie,默认不对值作编码处理
// Cookie 字符串格式:key1=value1; key2=value2; key3=value3;
func CookieFromStr(cookieStr string) []*http.Cookie {
return CookieFromStrEscape(cookieStr, false)
}
// CookieFromStrEscape 将 Cookie 字符串转换为 []*http.Cookie,可选是否对值作编码处理
// Cookie 字符串格式:key1=value1; key2=value2; key3=value3;
func CookieFromStrEscape(cookieStr string, escape bool) []*http.Cookie {
header := http.Header{}
header.Add("Cookie", cookieStr)
request := http.Request{Header: header}
newCookies := make([]*http.Cookie, 0)
for _, c := range request.Cookies() {
val := c.Value
if escape {
// cookie值可能包含特殊字符
val = url.QueryEscape(c.Value)
}
newCookies = append(newCookies, &http.Cookie{
Name: c.Name,
Value: val,
})
}
return newCookies
}
// CookieFromStrWithDPE 将 Cookie 字符串转换为 []*http.Cookie,同时设置 Domain、Path、Expires 信息,默认不对值作编码处理
// duration: 从当前时间开始的持续时间
// Cookie 字符串格式:key1=value1; key2=value2; key3=value3;
func CookieFromStrWithDPE(cookieStr string, domain string, path string, duration time.Duration) []*http.Cookie {
return CookieFromStrWithDPEEscape(cookieStr, domain, path, duration, false)
}
// CookieFromStrWithDPEEscape 将 Cookie 字符串转换为 []*http.Cookie,同时设置 Domain、Path、Expires 信息,可选是否对值作编码处理
// duration: 从当前时间开始的持续时间
// Cookie 字符串格式:key1=value1; key2=value2; key3=value3;
func CookieFromStrWithDPEEscape(cookieStr string, domain string, path string, duration time.Duration, escape bool) []*http.Cookie {
header := http.Header{}
header.Add("Cookie", cookieStr)
request := http.Request{Header: header}
expr := time.Now().Add(duration)
newCookies := make([]*http.Cookie, 0)
for _, c := range request.Cookies() {
val := c.Value
if escape {
// cookie值可能包含特殊字符
val = url.QueryEscape(c.Value)
}
newCookies = append(newCookies, &http.Cookie{
Name: c.Name,
Value: val,
Expires: expr,
Domain: domain,
Path: path,
})
}
return newCookies
}
// CookieFromFile 从文件中获取 Cookie 字符串并转换为 []*http.Cookie,默认不对值作编码处理
func CookieFromFile(cookieFilePath string) ([]*http.Cookie, error) {
return CookieFromFileEscape(cookieFilePath, false)
}
// CookieFromFileEscape 从文件中获取 Cookie 字符串并转换为 []*http.Cookie,可选是否对值作编码处理
func CookieFromFileEscape(cookieFilePath string, escape bool) ([]*http.Cookie, error) {
cookieBytes, err := os.ReadFile(cookieFilePath)
if err != nil {
return nil, err
}
return CookieFromStrEscape(string(cookieBytes), escape), nil
}
// CookieFromFileWithDPE 从文件中获取 Cookie 字符串并转换为 []*http.Cookie,同时设置 Domain、Path、Expires 信息,默认不对值作编码处理
// duration: 从当前时间开始的持续时间
func CookieFromFileWithDPE(cookieFilePath string, domain string, path string, duration time.Duration) ([]*http.Cookie, error) {
return CookieFromFileWithDPEEscape(cookieFilePath, domain, path, duration, false)
}
// CookieFromFileWithDPEEscape 从文件中获取 Cookie 字符串并转换为 []*http.Cookie,同时设置 Domain、Path、Expires 信息,可选是否对值作编码处理
// duration: 从当前时间开始的持续时间
func CookieFromFileWithDPEEscape(cookieFilePath string, domain string, path string, duration time.Duration, escape bool) ([]*http.Cookie, error) {
cookieBytes, err := os.ReadFile(cookieFilePath)
if err != nil {
return nil, err
}
return CookieFromStrWithDPEEscape(string(cookieBytes), domain, path, duration, escape), nil
}
// CookieToStrNV 将 []*http.Cookie 转换成 Cookie 字符串,格式为 name1=value1; name2=value2; name3=value3;
func CookieToStrNV(cookies []*http.Cookie) string {
return CookieToStrNVUnEscape(cookies, false)
}
// CookieToStrNVUnEscape 将 []*http.Cookie 转换成 Cookie 字符串,格式为 name1=value1; name2=value2; name3=value3;
func CookieToStrNVUnEscape(cookies []*http.Cookie, unEscape bool) string {
res := make([]string, 0)
for _, c := range cookies {
val := c.Value
if unEscape {
// 对特殊字符转义
val, _ = url.QueryUnescape(c.Value)
}
res = append(res, fmt.Sprintf("%s=%s", c.Name, val))
}
return strings.Join(res, "; ")
}
type CookieModel struct {
Name string `json:"name"`
Value string `json:"value"`
Path string `json:"path"`
Domain string `json:"domain"`
Expires int64 `json:"expires"` // unix时间戳
}
// CookieToJsonStrNVDPE 将 []*http.Cookie 转换成 Json 格式的 Cookie 字符串,带有 Name、Value、Path、Domain、Expires 参数
//
// 转换后格式为:[{"name":"","value":"","path":"","domain":"","expires":0},{"name":"","value":"","path":"","domain":"","expires":0}]
func CookieToJsonStrNVDPE(cookies []*http.Cookie) string {
return CookieToJsonStrNVDPEUnEscape(cookies, false)
}
// CookieToJsonStrNVDPEUnEscape 将 []*http.Cookie 转换成 Json 格式的 Cookie 字符串,带有 Name、Value、Path、Domain、Expires 参数
//
// 转换后格式为:[{"name":"","value":"","path":"","domain":"","expires":0},{"name":"","value":"","path":"","domain":"","expires":0}]
func CookieToJsonStrNVDPEUnEscape(cookies []*http.Cookie, unEscape bool) string {
res := make([]*CookieModel, 0)
for _, c := range cookies {
val := c.Value
if unEscape {
// 对特殊字符转义
val, _ = url.QueryUnescape(c.Value)
}
res = append(res, &CookieModel{
Name: c.Name,
Value: val,
Path: c.Path,
Domain: c.Domain,
Expires: c.Expires.Unix(),
})
}
return JsonMarshalStr(res)
}
// Deprecated: Use CookieFromStrWithDPE instead.
func CookieFromStrWithDomain(cookieStr string, domain string, path string, expires time.Duration) []*http.Cookie {
return CookieFromStrWithDPE(cookieStr, domain, path, expires)
}
// Deprecated: Use CookieToStrNV instead.
func CookieToStr(cookies []*http.Cookie) string {
return CookieToStrNV(cookies)
}
// CookieParseStr 将 cookie 字符串转换为 []*http.Cookie
func CookieParseStr(cookieStr string) []*http.Cookie {
cookies := make(map[string]string)
// 分割cookie字符串
pairs := strings.Split(cookieStr, ";")
for _, pair := range pairs {
pair = strings.TrimSpace(pair)
if pair == "" {
continue
}
// 分割键值对
parts := strings.SplitN(pair, "=", 2)
if len(parts) != 2 {
continue
}
cookies[parts[0]] = parts[1]
}
result := make([]*http.Cookie, 0)
for name, value := range cookies {
result = append(result, &http.Cookie{
Name: name,
Value: value,
})
}
return result
}