Skip to content

Commit 2ecb93d

Browse files
committedAug 8, 2023
Improve cookie jar.
* Add SetCookeJarFactory. * Use memoryCookieJarFactory to create cookie jar by default when create Client. * Add some comments.
1 parent e012355 commit 2ecb93d

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed
 

‎client.go

+38-8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Client struct {
5050
AllowGetMethodPayload bool
5151
*Transport
5252

53+
cookiejarFactory func() *cookiejar.Jar
5354
trace bool
5455
disableAutoReadResponse bool
5556
commonErrorType reflect.Type
@@ -1022,9 +1023,13 @@ func (c *Client) EnableTraceAll() *Client {
10221023
return c
10231024
}
10241025

1025-
// SetCookieJar set the `CookeJar` to the underlying `http.Client`, set to nil if you
1026-
// want to disable cookie.
1026+
// SetCookieJar set the cookie jar to the underlying `http.Client`, set to nil if you
1027+
// want to disable cookies.
1028+
// Note: If you use Client.Clone to clone a new Client, the new client will share the same
1029+
// cookie jar as the old Client after cloning. Use SetCookieJarFactory instead if you want
1030+
// to create a new CookieJar automatically when cloning a client.
10271031
func (c *Client) SetCookieJar(jar http.CookieJar) *Client {
1032+
c.cookiejarFactory = nil
10281033
c.httpClient.Jar = jar
10291034
return c
10301035
}
@@ -1042,11 +1047,10 @@ func (c *Client) GetCookies(url string) ([]*http.Cookie, error) {
10421047
}
10431048

10441049
// ClearCookies clears all cookies if cookie is enabled.
1050+
// Note: It has no effect if you called SetCookieJar instead of
1051+
// SetCookieJarFactory.
10451052
func (c *Client) ClearCookies() *Client {
1046-
if c.httpClient.Jar != nil {
1047-
jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
1048-
c.httpClient.Jar = jar
1049-
}
1053+
c.initCookieJar()
10501054
return c
10511055
}
10521056

@@ -1446,6 +1450,7 @@ func (c *Client) Clone() *Client {
14461450
client := *c.httpClient
14471451
client.Transport = cc.Transport
14481452
cc.httpClient = &client
1453+
cc.initCookieJar()
14491454

14501455
// clone client middleware
14511456
if len(cc.roundTripWrappers) > 0 {
@@ -1467,14 +1472,17 @@ func (c *Client) Clone() *Client {
14671472
return &cc
14681473
}
14691474

1475+
func memoryCookieJarFactory() *cookiejar.Jar {
1476+
jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
1477+
return jar
1478+
}
1479+
14701480
// C create a new client.
14711481
func C() *Client {
14721482
t := T()
14731483

1474-
jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
14751484
httpClient := &http.Client{
14761485
Transport: t,
1477-
Jar: jar,
14781486
Timeout: 2 * time.Minute,
14791487
}
14801488
beforeRequest := []RequestMiddleware{
@@ -1496,13 +1504,35 @@ func C() *Client {
14961504
jsonUnmarshal: json.Unmarshal,
14971505
xmlMarshal: xml.Marshal,
14981506
xmlUnmarshal: xml.Unmarshal,
1507+
cookiejarFactory: memoryCookieJarFactory,
14991508
}
15001509
httpClient.CheckRedirect = c.defaultCheckRedirect
1510+
c.initCookieJar()
15011511

15021512
c.initTransport()
15031513
return c
15041514
}
15051515

1516+
// SetCookieJarFactory set the functional factory of cookie jar, which creates
1517+
// cookie jar that store cookies for underlying `http.Client`. After client clone,
1518+
// the cookie jar of the new client will also be regenerated using this factory
1519+
// function.
1520+
func (c *Client) SetCookieJarFactory(factory func() *cookiejar.Jar) *Client {
1521+
c.cookiejarFactory = factory
1522+
c.initCookieJar()
1523+
return c
1524+
}
1525+
1526+
func (c *Client) initCookieJar() {
1527+
if c.cookiejarFactory == nil {
1528+
return
1529+
}
1530+
jar := c.cookiejarFactory()
1531+
if jar != nil {
1532+
c.httpClient.Jar = jar
1533+
}
1534+
}
1535+
15061536
func (c *Client) initTransport() {
15071537
c.Debugf = func(format string, v ...interface{}) {
15081538
if c.DebugLog {

‎client_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77
"errors"
88
"github.com/imroc/req/v3/internal/header"
99
"github.com/imroc/req/v3/internal/tests"
10+
"golang.org/x/net/publicsuffix"
1011
"io"
1112
"net"
1213
"net/http"
14+
"net/http/cookiejar"
1315
"net/url"
1416
"os"
1517
"strings"
@@ -632,3 +634,17 @@ func TestSetResultStateCheckFunc(t *testing.T) {
632634
tests.AssertNoError(t, err)
633635
tests.AssertEqual(t, ErrorState, resp.ResultState())
634636
}
637+
func TestCloneCookieJar(t *testing.T) {
638+
c1 := C()
639+
c2 := c1.Clone()
640+
tests.AssertEqual(t, true, c1.httpClient.Jar != c2.httpClient.Jar)
641+
642+
jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
643+
c1.SetCookieJar(jar)
644+
c2 = c1.Clone()
645+
tests.AssertEqual(t, true, c1.httpClient.Jar == c2.httpClient.Jar)
646+
647+
c2.SetCookieJar(nil)
648+
tests.AssertEqual(t, true, c2.cookiejarFactory == nil)
649+
tests.AssertEqual(t, true, c2.httpClient.Jar == nil)
650+
}

0 commit comments

Comments
 (0)
Please sign in to comment.