forked from bogdanfinn/tls-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jar.go
257 lines (193 loc) · 5.39 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
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
package tls_client
import (
"fmt"
"net/url"
"strings"
"sync"
http "github.com/bogdanfinn/fhttp"
"github.com/bogdanfinn/fhttp/cookiejar"
)
type CookieJarOption func(config *cookieJarConfig)
type cookieJarConfig struct {
skipExisting bool
debug bool
allowEmptyCookies bool
logger Logger
}
func WithSkipExisting() CookieJarOption {
return func(config *cookieJarConfig) {
config.skipExisting = true
}
}
func WithAllowEmptyCookies() CookieJarOption {
return func(config *cookieJarConfig) {
config.allowEmptyCookies = true
}
}
func WithDebugLogger() CookieJarOption {
return func(config *cookieJarConfig) {
config.debug = true
}
}
func WithLogger(logger Logger) CookieJarOption {
return func(config *cookieJarConfig) {
config.logger = logger
}
}
type CookieJar interface {
http.CookieJar
GetAllCookies() map[string][]*http.Cookie
}
type cookieJar struct {
jar *cookiejar.Jar
config *cookieJarConfig
allCookies map[string][]*http.Cookie
sync.RWMutex
}
func NewCookieJar(options ...CookieJarOption) CookieJar {
realJar, _ := cookiejar.New(nil)
config := &cookieJarConfig{}
for _, opt := range options {
opt(config)
}
if config.logger == nil {
config.logger = NewNoopLogger()
}
if config.debug {
config.logger = NewDebugLogger(config.logger)
}
c := &cookieJar{
jar: realJar,
config: config,
allCookies: make(map[string][]*http.Cookie),
}
return c
}
func (jar *cookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) {
jar.Lock()
defer jar.Unlock()
notEmptyCookies := jar.nonEmpty(cookies)
uniqueCookies := jar.unique(notEmptyCookies)
hostKey := jar.buildCookieHostKey(u)
if !jar.config.skipExisting {
existingCookies := jar.allCookies[hostKey]
var remainingExistingCookies []*http.Cookie
for _, existingCookie := range existingCookies {
shouldOverwrite := false
for _, cookie := range uniqueCookies {
shouldOverwrite = existingCookie.Name == cookie.Name
if shouldOverwrite {
break
}
}
if shouldOverwrite {
continue
}
remainingExistingCookies = append(remainingExistingCookies, existingCookie)
}
newCookies := append(remainingExistingCookies, uniqueCookies...)
jar.jar.SetCookies(u, newCookies)
jar.allCookies[hostKey] = newCookies
return
}
var newNonExistentCookies []*http.Cookie
existingCookies := jar.allCookies[hostKey]
for _, cookie := range uniqueCookies {
alreadyInJar := false
for _, existingCookie := range existingCookies {
alreadyInJar = cookie.Name == existingCookie.Name
if alreadyInJar {
break
}
}
if alreadyInJar {
jar.config.logger.Debug("cookie %s is already in jar, skipping", cookie.Name)
continue
}
jar.config.logger.Debug("cookie %s is not in jar yet, adding", cookie.Name)
newNonExistentCookies = append(newNonExistentCookies, cookie)
}
newCookies := append(existingCookies, newNonExistentCookies...)
jar.jar.SetCookies(u, newCookies)
jar.allCookies[hostKey] = newCookies
}
func (jar *cookieJar) Cookies(u *url.URL) []*http.Cookie {
jar.RLock()
defer jar.RUnlock()
hostKey := jar.buildCookieHostKey(u)
allCookies := jar.allCookies[hostKey]
return jar.notExpired(allCookies)
}
func (jar *cookieJar) GetAllCookies() map[string][]*http.Cookie {
jar.RLock()
defer jar.RUnlock()
copied := make(map[string][]*http.Cookie)
for u, c := range jar.allCookies {
copied[u] = c
}
return copied
}
func (jar *cookieJar) buildCookieHostKey(u *url.URL) string {
host := u.Host
hostParts := strings.Split(host, ".")
switch len(hostParts) {
case 3:
return fmt.Sprintf("%s.%s", hostParts[len(hostParts)-2], hostParts[len(hostParts)-1])
case 2:
return fmt.Sprintf("%s.%s", hostParts[len(hostParts)-2], hostParts[len(hostParts)-1])
default:
return host
}
}
func (jar *cookieJar) unique(cookies []*http.Cookie) []*http.Cookie {
var filteredCookies []*http.Cookie
var uniqueCookies []string
for i := len(cookies) - 1; i >= 0; i-- {
c := cookies[i]
if inSlice(uniqueCookies, c.Name) {
continue
}
filteredCookies = append(filteredCookies, c)
uniqueCookies = append(uniqueCookies, c.Name)
}
return filteredCookies
}
func (jar *cookieJar) nonEmpty(cookies []*http.Cookie) []*http.Cookie {
if jar.config.allowEmptyCookies {
return cookies
}
var filteredCookies []*http.Cookie
for _, c := range cookies {
if c.Value == "" {
jar.config.logger.Debug("cookie %s is empty and will be filtered out", c.Name)
continue
}
filteredCookies = append(filteredCookies, c)
}
return filteredCookies
}
func (jar *cookieJar) notExpired(cookies []*http.Cookie) []*http.Cookie {
var filteredCookies []*http.Cookie
for _, c := range cookies {
// we misuse the max age here for "deletion" reasons. To be 100% correct a MaxAge equals 0 should also be deleted but we do not do it for now.
if c.MaxAge < 0 {
jar.config.logger.Debug("cookie %s in jar max age set to 0 or below. will be excluded from request", c.Name)
continue
}
// TODO: this is currently commented out as the cookie parser does not parse the expire correctly out of the Set-Cookie header.
/*if c.Expires.Before(now) {
jar.config.logger.Debug("cookie %s in jar expired. will be excluded from request", c.Name)
continue
}*/
filteredCookies = append(filteredCookies, c)
}
return filteredCookies
}
func inSlice(slice []string, elem string) bool {
for _, e := range slice {
if e == elem {
return true
}
}
return false
}