-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcookies.go
62 lines (49 loc) · 879 Bytes
/
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
package facebook
import (
"net/http"
"sync"
)
type cookies struct {
mutex *sync.Mutex
cookies []*http.Cookie
}
func newCookies(cs []*http.Cookie) *cookies {
return &cookies{
mutex: new(sync.Mutex),
cookies: cs,
}
}
func (cks *cookies) merge(newCs []*http.Cookie) {
cks.mutex.Lock()
for _, newC := range newCs {
found := false
for i, c := range cks.cookies {
if newC.Name == c.Name {
cks.cookies[i] = newC
found = true
break
}
}
if !found {
cks.cookies = append(cks.cookies, newC)
}
}
cks.mutex.Unlock()
}
func (cks *cookies) getAll() []*http.Cookie {
cks.mutex.Lock()
c := cks.cookies
cks.mutex.Unlock()
return c
}
func (cks *cookies) getByName(name string) *http.Cookie {
cks.mutex.Lock()
var c *http.Cookie
for _, c = range cks.cookies {
if c.Name == name {
break
}
}
cks.mutex.Unlock()
return c
}