-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie.go
61 lines (51 loc) · 1.12 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
package fhttpc
import (
"sync"
"github.com/valyala/fasthttp"
)
var cookiePool = sync.Pool{
New: func() interface{} {
return &Jar{}
},
}
// AcquireCookieJar returns an empty CookieJar object from pool
func AcquireJar() *Jar {
return cookiePool.Get().(*Jar)
}
// ReleaseCookieJar returns CookieJar to the pool
func ReleaseJar(c *Jar) {
for k, v := range *c {
fasthttp.ReleaseCookie(v)
delete(*c, k)
}
cookiePool.Put(c)
}
// Jar denotes a cookie jar
type Jar map[string]*fasthttp.Cookie
// Peek returns the value bytes of the cookie with the provided key (nil if it doesn't exist)
func (j *Jar) Peek(key string) []byte {
cookie, ok := (*j)[key]
if !ok {
return nil
}
return cookie.Value()
}
// SetCookie sets a cookie in the jar
func (j *Jar) SetCookie(key, value string) {
c, ok := (*j)[key]
if !ok {
c = fasthttp.AcquireCookie()
}
c.SetKey(key)
c.SetValue(value)
(*j)[key] = c
}
// Len returns the number of objects in the cookie jar
func (j *Jar) Len() int {
return len(*j)
}
func (j *Jar) populateRequest(r *fasthttp.Request) {
for _, c := range *j {
r.Header.SetCookieBytesKV(c.Key(), c.Value())
}
}