-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookies.go
More file actions
33 lines (30 loc) · 736 Bytes
/
cookies.go
File metadata and controls
33 lines (30 loc) · 736 Bytes
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
package quick
import (
"net/http"
"strings"
)
// defined []http.Cookie alias Cookies
type Cookies = []*http.Cookie
// You should init it by using NewCookiesWithString like this:
// cookies := quick.NewCookiesWithString(
// "key1=value1; key2=value2; key3=value3"
// )
// Note: param is cookie string
func NewCookiesWithString(rawstr string) Cookies {
if len(rawstr) == 0 {
return nil
}
strs := strings.Split(rawstr, ";")
cookies := make(Cookies, 0, len(strs))
for i := 0; i < len(strs); i++ {
cookie := strings.Split(strs[i], "=")
if len(cookie) != 2 {
continue
}
cookies = append(cookies, &http.Cookie{
Name: strings.TrimSpace(cookie[0]),
Value: strings.TrimSpace(cookie[1]),
})
}
return cookies
}