From 75635f539da3786b9676fad5e69cd30731dd37a8 Mon Sep 17 00:00:00 2001 From: Mzack9999 Date: Thu, 14 Aug 2025 19:28:03 +0200 Subject: [PATCH 1/2] adding sync cookiejar --- http/cookiejar.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 http/cookiejar.go diff --git a/http/cookiejar.go b/http/cookiejar.go new file mode 100644 index 0000000..5fecddc --- /dev/null +++ b/http/cookiejar.go @@ -0,0 +1,65 @@ +package httputil + +import ( + "net/http" + "net/url" + "sync" +) + +// Option represents a configuration option for the cookie jar +type Option func(*CookieJar) + +// WithCookieJar sets an existing cookie jar to wrap +func WithCookieJar(jar http.CookieJar) Option { + return func(cj *CookieJar) { + cj.jar = jar + } +} + +// CookieJar is a thread-safe wrapper around http.CookieJar +type CookieJar struct { + jar http.CookieJar + mu sync.RWMutex +} + +// New creates a new thread-safe cookie jar with the given options +// If no jar is provided, creates a simple in-memory cookie jar +func New(opts ...Option) *CookieJar { + cj := &CookieJar{} + + // Apply options + for _, opt := range opts { + opt(cj) + } + + // If no jar was provided, create a simple in-memory one + if cj.jar == nil { + cj.jar = &memoryCookieJar{ + cookies: make(map[string][]*http.Cookie), + } + } + + return cj +} + +// SetCookies implements http.CookieJar.SetCookies +func (cj *CookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) { + if cj.jar == nil { + return + } + + cj.mu.Lock() + defer cj.mu.Unlock() + cj.jar.SetCookies(u, cookies) +} + +// Cookies implements http.CookieJar.Cookies +func (cj *CookieJar) Cookies(u *url.URL) []*http.Cookie { + if cj.jar == nil { + return nil + } + + cj.mu.RLock() + defer cj.mu.RUnlock() + return cj.jar.Cookies(u) +} From b5718c7ae269c4d240bbac91acf0e1fc7a073804 Mon Sep 17 00:00:00 2001 From: Mzack9999 Date: Thu, 14 Aug 2025 19:31:32 +0200 Subject: [PATCH 2/2] . --- http/cookiejar.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/http/cookiejar.go b/http/cookiejar.go index 5fecddc..b87977b 100644 --- a/http/cookiejar.go +++ b/http/cookiejar.go @@ -2,6 +2,7 @@ package httputil import ( "net/http" + "net/http/cookiejar" "net/url" "sync" ) @@ -24,7 +25,7 @@ type CookieJar struct { // New creates a new thread-safe cookie jar with the given options // If no jar is provided, creates a simple in-memory cookie jar -func New(opts ...Option) *CookieJar { +func NewCookieJar(opts ...Option) (*CookieJar, error) { cj := &CookieJar{} // Apply options @@ -32,14 +33,16 @@ func New(opts ...Option) *CookieJar { opt(cj) } - // If no jar was provided, create a simple in-memory one + // If no jar was provided, create a new one if cj.jar == nil { - cj.jar = &memoryCookieJar{ - cookies: make(map[string][]*http.Cookie), + jar, err := cookiejar.New(nil) + if err != nil { + return nil, err } + cj.jar = jar } - return cj + return cj, nil } // SetCookies implements http.CookieJar.SetCookies