enhance: thread-safe CookieJar, SameSite cookie attr, dynamic Google countries
- Wrap staticCookieJar in struct with sync.RWMutex for thread safety - Add SameSite field to Cookie struct with Strict/Lax/None constants - Update Playwright cookie conversion functions for SameSite - Replace hardcoded 4-country switch with dynamic country code generation Closes #20, #22, #23 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,21 +6,32 @@ import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type staticCookieJar []Cookie
|
||||
type staticCookieJar struct {
|
||||
mu sync.RWMutex
|
||||
cookies []Cookie
|
||||
}
|
||||
|
||||
// GetAll will return all cookies in the jar.
|
||||
func (s *staticCookieJar) GetAll() ([]Cookie, error) {
|
||||
return *s, nil
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
out := make([]Cookie, len(s.cookies))
|
||||
copy(out, s.cookies)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Get will, given a URL, return all cookies that are valid for that URL.
|
||||
func (s *staticCookieJar) Get(target string) ([]Cookie, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
var validCookies []Cookie
|
||||
|
||||
for _, cookie := range *s {
|
||||
for _, cookie := range s.cookies {
|
||||
if match, err := cookie.IsTargetMatch(target); err != nil {
|
||||
return nil, err
|
||||
} else if match {
|
||||
@@ -32,22 +43,28 @@ func (s *staticCookieJar) Get(target string) ([]Cookie, error) {
|
||||
}
|
||||
|
||||
func (s *staticCookieJar) Set(cookie Cookie) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
// see if the cookie already exists
|
||||
for i, c := range *s {
|
||||
for i, c := range s.cookies {
|
||||
if c.Name == cookie.Name && c.Host == cookie.Host && c.Path == cookie.Path {
|
||||
(*s)[i] = cookie
|
||||
s.cookies[i] = cookie
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
*s = append(*s, cookie)
|
||||
s.cookies = append(s.cookies, cookie)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *staticCookieJar) Delete(cookie Cookie) error {
|
||||
for i, c := range *s {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
for i, c := range s.cookies {
|
||||
if c.Name == cookie.Name && c.Host == cookie.Host && c.Path == cookie.Path {
|
||||
*s = append((*s)[:i], (*s)[i+1:]...)
|
||||
s.cookies = append(s.cookies[:i], s.cookies[i+1:]...)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -66,7 +83,7 @@ func LoadCookiesFile(path string) (CookieJar, error) {
|
||||
_ = cl.Close()
|
||||
}(fp)
|
||||
|
||||
var cookies staticCookieJar
|
||||
var cookies []Cookie
|
||||
|
||||
scanner := bufio.NewScanner(fp)
|
||||
|
||||
@@ -102,5 +119,5 @@ func LoadCookiesFile(path string) (CookieJar, error) {
|
||||
})
|
||||
}
|
||||
|
||||
return &cookies, nil
|
||||
return &staticCookieJar{cookies: cookies}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user