@@ -50,6 +50,7 @@ type Client struct {
50
50
AllowGetMethodPayload bool
51
51
* Transport
52
52
53
+ cookiejarFactory func () * cookiejar.Jar
53
54
trace bool
54
55
disableAutoReadResponse bool
55
56
commonErrorType reflect.Type
@@ -1022,9 +1023,13 @@ func (c *Client) EnableTraceAll() *Client {
1022
1023
return c
1023
1024
}
1024
1025
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.
1027
1031
func (c * Client ) SetCookieJar (jar http.CookieJar ) * Client {
1032
+ c .cookiejarFactory = nil
1028
1033
c .httpClient .Jar = jar
1029
1034
return c
1030
1035
}
@@ -1042,11 +1047,10 @@ func (c *Client) GetCookies(url string) ([]*http.Cookie, error) {
1042
1047
}
1043
1048
1044
1049
// ClearCookies clears all cookies if cookie is enabled.
1050
+ // Note: It has no effect if you called SetCookieJar instead of
1051
+ // SetCookieJarFactory.
1045
1052
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 ()
1050
1054
return c
1051
1055
}
1052
1056
@@ -1446,6 +1450,7 @@ func (c *Client) Clone() *Client {
1446
1450
client := * c .httpClient
1447
1451
client .Transport = cc .Transport
1448
1452
cc .httpClient = & client
1453
+ cc .initCookieJar ()
1449
1454
1450
1455
// clone client middleware
1451
1456
if len (cc .roundTripWrappers ) > 0 {
@@ -1467,14 +1472,17 @@ func (c *Client) Clone() *Client {
1467
1472
return & cc
1468
1473
}
1469
1474
1475
+ func memoryCookieJarFactory () * cookiejar.Jar {
1476
+ jar , _ := cookiejar .New (& cookiejar.Options {PublicSuffixList : publicsuffix .List })
1477
+ return jar
1478
+ }
1479
+
1470
1480
// C create a new client.
1471
1481
func C () * Client {
1472
1482
t := T ()
1473
1483
1474
- jar , _ := cookiejar .New (& cookiejar.Options {PublicSuffixList : publicsuffix .List })
1475
1484
httpClient := & http.Client {
1476
1485
Transport : t ,
1477
- Jar : jar ,
1478
1486
Timeout : 2 * time .Minute ,
1479
1487
}
1480
1488
beforeRequest := []RequestMiddleware {
@@ -1496,13 +1504,35 @@ func C() *Client {
1496
1504
jsonUnmarshal : json .Unmarshal ,
1497
1505
xmlMarshal : xml .Marshal ,
1498
1506
xmlUnmarshal : xml .Unmarshal ,
1507
+ cookiejarFactory : memoryCookieJarFactory ,
1499
1508
}
1500
1509
httpClient .CheckRedirect = c .defaultCheckRedirect
1510
+ c .initCookieJar ()
1501
1511
1502
1512
c .initTransport ()
1503
1513
return c
1504
1514
}
1505
1515
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
+
1506
1536
func (c * Client ) initTransport () {
1507
1537
c .Debugf = func (format string , v ... interface {}) {
1508
1538
if c .DebugLog {
0 commit comments