-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjar.go
170 lines (129 loc) · 2.69 KB
/
jar.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
package client
import (
http "github.com/valyala/fasthttp"
"sync"
"time"
"encoding/gob"
"bytes"
)
type Jar struct {
mu sync.Mutex
cookies map[string]*http.Cookie
}
func NewJar() *Jar {
return &Jar{cookies: make(map[string]*http.Cookie)}
}
func (j *Jar) PeekValue(key string) []byte{
c, ok := j.cookies[key]
if ok{
return c.Value()
}
return nil
}
func (j *Jar) Peek(key string) *http.Cookie{
j.mu.Lock()
defer j.mu.Unlock()
return j.cookies[key]
}
func (j *Jar) ReleaseCookie(key string){
j.mu.Lock()
defer j.mu.Unlock()
c, ok := j.cookies[key]
if ok{
http.ReleaseCookie(c)
delete(j.cookies, key)
}
}
func (j *Jar) MarshalJSON() ([]byte, error){
cookies, err := j.makeEncodable()
if err != nil {
return nil, err
}
data, err := cookies.MarshalJSON()
return data, err
}
func (j *Jar) UnmarshalJSON(data []byte) error {
cooks := cookies{}
err := cooks.UnmarshalJSON(data)
if err != nil {
return err
}
err = j.decode(cooks)
return err
}
func (j *Jar) EncodeGOB() ([]byte, error) {
cookies, err := j.makeEncodable()
if err != nil {
return nil, err
}
var buf bytes.Buffer
err = gob.NewEncoder(&buf).Encode(cookies)
return buf.Bytes(), err
}
func (j *Jar) DecodeGOB(data []byte) error{
cooks := &cookies{}
var buf bytes.Buffer
buf.Write(data)
err := gob.NewDecoder(&buf).Decode(cooks)
if err != nil {
return err
}
err = j.decode(*cooks)
return err
}
func (j *Jar) decode( cooks cookies ) error {
for _, v := range cooks{
expire := new(time.Time)
err := expire.UnmarshalText([]byte(v.Expire))
if err != nil {
return err
}
c := http.AcquireCookie()
c.SetKey(v.Key)
c.SetValue(v.Value)
c.SetExpire(*expire)
c.SetMaxAge(v.MaxAge)
c.SetDomain(v.Domain)
c.SetPath(v.Path)
c.SetHTTPOnly(v.HTTPOnly)
c.SetSecure(v.Secure)
c.SetSameSite(v.SameSite)
j.cookies[v.Key] = c
}
return nil
}
func (j *Jar) makeEncodable() (cookies, error) {
cookies := cookies{}
for _, v := range j.cookies {
expire, err := v.Expire().MarshalText()
if err != nil {
return nil, err
}
c := cookie{
Key: string(v.Key()),
Value: string(v.Value()),
Expire: string(expire),
MaxAge: v.MaxAge(),
Domain: string(v.Domain()),
Path: string(v.Path()),
HTTPOnly: v.HTTPOnly(),
Secure: v.Secure(),
SameSite: v.SameSite(),
}
cookies = append(cookies, c)
}
return cookies, nil
}
//easyjson:json
type cookies []cookie
type cookie struct {
Key string `json:"key"`
Value string `json:"value"`
Expire string `json:"time"`
MaxAge int `json:"max_age"`
Domain string `json:"domain"`
Path string `json:"path"`
HTTPOnly bool `json:"http_only"`
Secure bool `json:"secure"`
SameSite http.CookieSameSite `json:"same_site"`
}