Skip to content

Commit 022b2ae

Browse files
committed
Refactor cookie handling and apply custom cookies in HTTP client code
1 parent 6818db3 commit 022b2ae

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

cookiejar/cookiejar.go

+51-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
11
// cookiejar/cookiejar.go
22

3-
/* The cookiejar package provides utility functions for managing cookies within an HTTP client
4-
context in Go. This package aims to enhance HTTP client functionalities by offering cookie
5-
handling capabilities, including initialization of a cookie jar, redaction of sensitive cookies,
6-
and parsing of cookies from HTTP headers. Below is an explanation of the core functionalities
7-
provided by this package*/
3+
/* When both the cookie jar is enabled and specific cookies are set for an HTTP client in
4+
your scenario, here’s what generally happens during the request processing:
5+
6+
Cookie Jar Initialization: If the cookie jar is enabled through your SetupCookieJar function,
7+
an instance of http.cookiejar.Jar is created and associated with your HTTP client. This
8+
cookie jar will automatically handle incoming and outgoing cookies for all requests made
9+
using this client. It manages storing cookies and automatically sending them with subsequent
10+
requests to the domains for which they're valid.
11+
12+
Setting Specific Cookies: The ApplyCustomCookies function checks for any user-defined specific
13+
cookies (from the CustomCookies map). If found, these cookies are explicitly added to the
14+
outgoing HTTP request headers via the SetSpecificCookies function.
15+
16+
Interaction between Cookie Jar and Specific Cookies:
17+
Cookie Precedence: When a specific cookie (added via SetSpecificCookies) shares the same name
18+
as a cookie already handled by the cookie jar for a given domain, the behavior depends on the
19+
implementation of the HTTP client's cookie handling and the server's cookie management rules.
20+
Generally, the explicitly set cookie in the HTTP request header (from SetSpecificCookies)
21+
should override any similar cookie managed by the cookie jar for that single request.
22+
23+
Subsequent Requests: For subsequent requests, if the specific cookies are not added again
24+
via ApplyCustomCookies, the cookies in the jar that were stored from previous responses
25+
will take precedence again, unless overwritten by subsequent responses or explicit setting
26+
again.
27+
28+
Practical Usage:
29+
30+
This setup allows flexibility:
31+
32+
Use Cookie Jar: For general session management where cookies are automatically managed across
33+
requests.
34+
Use Specific Cookies: For overriding or adding specific cookies for particular requests where
35+
customized control is necessary (such as testing scenarios, special authentication cookies,
36+
etc.).
37+
Logging and Debugging: Your setup also includes logging functionalities which can be very
38+
useful to debug and verify which cookies are being sent and managed. This is crucial for
39+
maintaining visibility into how cookies are influencing the behavior of your HTTP client
40+
interactions.*/
841

942
package cookiejar
1043

@@ -31,6 +64,19 @@ func SetupCookieJar(client *http.Client, enableCookieJar bool, log logger.Logger
3164
return nil
3265
}
3366

67+
// ApplyCustomCookies checks and applies custom cookies to the HTTP request if any are configured.
68+
// It logs the names of the custom cookies being applied without exposing their values.
69+
func ApplyCustomCookies(req *http.Request, cookies map[string]string, log logger.Logger) {
70+
if len(cookies) > 0 {
71+
cookieNames := make([]string, 0, len(cookies))
72+
for name := range cookies {
73+
cookieNames = append(cookieNames, name)
74+
}
75+
log.Debug("Applying custom cookies", zap.Strings("Cookies", cookieNames))
76+
SetSpecificCookies(req, cookies)
77+
}
78+
}
79+
3480
// SetSpecificCookies sets specific cookies provided in the configuration on the HTTP request.
3581
func SetSpecificCookies(req *http.Request, cookies map[string]string) {
3682
for name, value := range cookies {

httpclient/request.go

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/deploymenttheory/go-api-http-client/authenticationhandler"
11+
"github.com/deploymenttheory/go-api-http-client/cookiejar"
1112
"github.com/deploymenttheory/go-api-http-client/headers"
1213
"github.com/deploymenttheory/go-api-http-client/httpmethod"
1314
"github.com/deploymenttheory/go-api-http-client/logger"
@@ -159,6 +160,9 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
159160
return nil, err
160161
}
161162

163+
// Apply custom cookies if configured
164+
cookiejar.ApplyCustomCookies(req, c.clientConfig.ClientOptions.Cookies.CustomCookies, log)
165+
162166
// Set request headers
163167
headerHandler := headers.NewHeaderHandler(req, c.Logger, c.APIHandler, c.AuthTokenHandler)
164168
headerHandler.SetRequestHeaders(endpoint)
@@ -320,6 +324,9 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{})
320324
return nil, err
321325
}
322326

327+
// Apply custom cookies if configured
328+
cookiejar.ApplyCustomCookies(req, c.clientConfig.ClientOptions.Cookies.CustomCookies, log)
329+
323330
// Set request headers
324331
headerHandler := headers.NewHeaderHandler(req, c.Logger, c.APIHandler, c.AuthTokenHandler)
325332
headerHandler.SetRequestHeaders(endpoint)

0 commit comments

Comments
 (0)