Skip to content

Commit

Permalink
[confighttp] Add support for cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed May 18, 2024
1 parent ede9e30 commit cbfa57a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .chloggen/cookie_support.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: confighttp

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add support for cookies in HTTP clients with `cookies_enabled`.

# One or more tracking issues or pull requests related to the change
issues: [10175]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: The method `confighttp.ToClient` will return a client with a `cookiejar.Jar` which will reuse cookies from server responses in subsequent requests.

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
1 change: 1 addition & 0 deletions config/confighttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ README](../configtls/README.md).
- [`disable_keep_alives`](https://golang.org/pkg/net/http/#Transport)
- [`http2_read_idle_timeout`](https://pkg.go.dev/golang.org/x/net/http2#Transport)
- [`http2_ping_timeout`](https://pkg.go.dev/golang.org/x/net/http2#Transport)
- [`cookies_enabled`](https://pkg.go.dev/net/http#CookieJar) if enabled, the client will store cookies from server responses and reuse them in subsequent requests.

Example:

Expand Down
14 changes: 14 additions & 0 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"time"

Expand Down Expand Up @@ -99,6 +100,10 @@ type ClientConfig struct {
// HTTP2PingTimeout if there's no response to the ping within the configured value, the connection will be closed.
// If not set or set to 0, it defaults to 15s.
HTTP2PingTimeout time.Duration `mapstructure:"http2_ping_timeout"`

// CookiesEnabled enables reusing cookies between requests.
// This is useful to manage sticky session cookies.
CookiesEnabled bool `mapstructure:"cookies_enabled"`
}

// NewDefaultClientConfig returns ClientConfig type object with
Expand Down Expand Up @@ -227,9 +232,18 @@ func (hcs *ClientConfig) ToClient(ctx context.Context, host component.Host, sett
}
}

var jar http.CookieJar
if hcs.CookiesEnabled {
jar, err = cookiejar.New(nil)
if err != nil {
return nil, err

Check warning on line 239 in config/confighttp/confighttp.go

View check run for this annotation

Codecov / codecov/patch

config/confighttp/confighttp.go#L239

Added line #L239 was not covered by tests
}
}

return &http.Client{
Transport: clientTransport,
Timeout: hcs.Timeout,
Jar: jar,
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func TestAllHTTPClientSettings(t *testing.T) {
CustomRoundTripper: func(next http.RoundTripper) (http.RoundTripper, error) { return next, nil },
Compression: "",
DisableKeepAlives: true,
CookiesEnabled: true,
HTTP2ReadIdleTimeout: idleConnTimeout,
HTTP2PingTimeout: http2PingTimeout,
},
Expand Down

0 comments on commit cbfa57a

Please sign in to comment.