1
1
// cookiejar/cookiejar.go
2
2
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.*/
8
41
9
42
package cookiejar
10
43
@@ -31,6 +64,19 @@ func SetupCookieJar(client *http.Client, enableCookieJar bool, log logger.Logger
31
64
return nil
32
65
}
33
66
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
+
34
80
// SetSpecificCookies sets specific cookies provided in the configuration on the HTTP request.
35
81
func SetSpecificCookies (req * http.Request , cookies map [string ]string ) {
36
82
for name , value := range cookies {
0 commit comments