forked from bogdanfinn/tls-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjar.go
More file actions
254 lines (208 loc) · 6.84 KB
/
jar.go
File metadata and controls
254 lines (208 loc) · 6.84 KB
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
package httpkit
import (
"fmt"
"net/url"
"strings"
"sync"
"maps"
http "github.com/bogdanfinn/fhttp"
"github.com/bogdanfinn/fhttp/cookiejar"
)
const CookieExpired = -1
// CookieJarOption is a function that modifies the configuration of a cookieJar.
type CookieJarOption func(config *cookieJarConfig)
type cookieJarConfig struct {
logger Logger
skipExisting bool
debug bool
allowEmpty bool
}
// WithSkipExisting returns a CookieJarOption that skips existing cookies in the jar (default: false).
// This is useful when you want to add new cookies without overwriting existing ones.
func WithSkipExisting() CookieJarOption {
return func(config *cookieJarConfig) {
config.skipExisting = true
}
}
// WithAllowEmpty returns a CookieJarOption that allows empty cookies in the jar (default: false).
// Note: this is not recommended as empty cookies are usually not valid.
func WithAllowEmpty() CookieJarOption {
return func(config *cookieJarConfig) {
config.allowEmpty = true
}
}
// WithDebugLogger returns a CookieJarOption that enables debug logging for the cookie jar.
func WithDebugLogger() CookieJarOption {
return func(config *cookieJarConfig) {
config.debug = true
}
}
// WithLogger returns a CookieJarOption that sets a custom logger for the cookie jar.
func WithLogger(logger Logger) CookieJarOption {
return func(config *cookieJarConfig) {
config.logger = logger
}
}
// CookieJar is the interface that wraps the basic CookieJar methods, including additional helpers.
type CookieJar interface {
http.CookieJar
CookiesMap() map[string][]*http.Cookie
Cookie(u *url.URL, name string) *http.Cookie
}
type cookieJar struct {
jar *cookiejar.Jar
config *cookieJarConfig
cookies map[string][]*http.Cookie
sync.RWMutex
}
// NewCookieJar creates a new empty cookie jar with the given options.
// Returns nil if the underlying cookiejar.Jar cannot be created.
func NewCookieJar(options ...CookieJarOption) CookieJar {
underlyingJar, err := cookiejar.New(nil)
if err != nil {
return nil
}
config := &cookieJarConfig{}
for _, opt := range options {
opt(config)
}
if config.logger == nil {
config.logger = NewNoopLogger()
}
if config.debug {
config.logger = NewDebugLogger(config.logger)
}
return &cookieJar{
jar: underlyingJar,
config: config,
cookies: make(map[string][]*http.Cookie),
}
}
// SetCookies sets the cookies for the given URL according to the rules defined in the config.
func (jar *cookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) {
jar.Lock()
defer jar.Unlock()
cookies = jar.nonEmpty(cookies)
cookies = jar.unique(cookies)
hostKey := jar.buildCookieHostKey(u)
existing := jar.cookies[hostKey]
if jar.config.skipExisting {
var newCookies []*http.Cookie
for _, cookie := range cookies {
if findCookieByName(existing, cookie.Name) != nil {
jar.config.logger.Debug("[SetCookies] Cookie '%s' already exists in jar. Skipping.", cookie.Name)
continue
}
jar.config.logger.Debug("[SetCookies] Adding new cookie '%s' to jar.", cookie.Name)
newCookies = append(newCookies, cookie)
}
cookies = append(existing, newCookies...)
} else {
var existingCookies []*http.Cookie
for _, cookie := range existing {
if findCookieByName(cookies, cookie.Name) != nil {
jar.config.logger.Debug("[SetCookies] Cookie '%s' already exists in jar. Skipping.", cookie.Name)
continue
}
jar.config.logger.Debug("[SetCookies] Adding existing cookie '%s' to jar.", cookie.Name)
existingCookies = append(existingCookies, cookie)
}
cookies = append(existingCookies, cookies...)
}
jar.jar.SetCookies(u, cookies)
jar.cookies[hostKey] = cookies
}
// Cookies returns the cookies for the given url, filtering out expired cookies.
func (jar *cookieJar) Cookies(u *url.URL) []*http.Cookie {
jar.RLock()
defer jar.RUnlock()
hostKey := jar.buildCookieHostKey(u)
cookies := jar.cookies[hostKey]
return jar.notExpired(cookies)
}
// CookiesMap returns all cookies in the jar, grouped by host key.
func (jar *cookieJar) CookiesMap() map[string][]*http.Cookie {
jar.RLock()
defer jar.RUnlock()
copied := make(map[string][]*http.Cookie)
maps.Copy(copied, jar.cookies)
return copied
}
// Cookie returns the cookie with the given name for the given url or nil if not found.
func (jar *cookieJar) Cookie(u *url.URL, name string) *http.Cookie {
jar.RLock()
defer jar.RUnlock()
hostKey := jar.buildCookieHostKey(u)
cookies := jar.cookies[hostKey]
for _, cookie := range cookies {
if cookie.Name == name && cookie.MaxAge > CookieExpired {
return cookie
}
}
return nil
}
// buildCookieHostKey builds a host key for the cookie jar based on the URL.
// It uses the last two parts of the host (e.g. "example.com" or "sub.example.com") as the key.
func (jar *cookieJar) buildCookieHostKey(u *url.URL) string {
hostParts := strings.Split(u.Host, ".")
if len(hostParts) >= 2 {
return fmt.Sprintf("%s.%s", hostParts[len(hostParts)-2], hostParts[len(hostParts)-1])
}
return u.Host
}
// unique filters out duplicate cookies by name and keeps the last one.
func (jar *cookieJar) unique(cookies []*http.Cookie) []*http.Cookie {
seen := make(map[string]bool, len(cookies))
filteredCookies := make([]*http.Cookie, 0, len(cookies))
for i := len(cookies) - 1; i >= 0; i-- {
c := cookies[i]
if seen[c.Name] {
continue
}
filteredCookies = append(filteredCookies, c)
seen[c.Name] = true
}
return filteredCookies
}
// nonEmpty filters out empty cookies if allowEmpty is false.
func (jar *cookieJar) nonEmpty(cookies []*http.Cookie) []*http.Cookie {
if jar.config.allowEmpty {
return cookies
}
filteredCookies := cookies[:0]
for _, cookie := range cookies {
if cookie.Value == "" {
jar.config.logger.Debug("[nonEmpty] Cookie '%s' is empty and will be filtered out.", cookie.Name)
continue
}
filteredCookies = append(filteredCookies, cookie)
}
return filteredCookies
}
// notExpired filters out expired cookies.
func (jar *cookieJar) notExpired(cookies []*http.Cookie) []*http.Cookie {
filteredCookies := cookies[:0]
for _, cookie := range cookies {
if cookie.MaxAge <= CookieExpired {
jar.config.logger.Debug("[notExpired] Cookie '%s' in jar has max age <= 0. Will be excluded from request.", cookie.Name)
continue
}
// TODO: The cookie parser does not parse the Expires field correctly from the Set-Cookie header.
// Once fixed, consider also filtering cookies by expiration date here.
// if cookie.Expires.Before(now) {
// jar.config.logger.Debug("[notExpired] Cookie '%s' in jar expired. Will be excluded from request.", cookie.Name)
// continue
// }
filteredCookies = append(filteredCookies, cookie)
}
return filteredCookies
}
// findCookieByName returns the cookie with the given name if it exists in the slice.
func findCookieByName(cookies []*http.Cookie, name string) *http.Cookie {
for _, c := range cookies {
if c.Name == name {
return c
}
}
return nil
}