Skip to content

Commit b67b3d3

Browse files
committed
refactor: update customhttp client logic
1 parent 605b152 commit b67b3d3

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

pkg/customhttp/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package customhttp
22

33
import (
4+
"context"
45
"errors"
6+
"net"
57
"net/http"
68
"net/http/cookiejar"
79
"net/url"
@@ -131,6 +133,9 @@ type ClientConfig struct {
131133
// proxy settings.
132134
DialerConfig *network.DialerConfig
133135

136+
// Optional custom dial context for advanced connection control.
137+
DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
138+
134139
// Specifies the cookie jar for the client. If nil, cookies are not managed.
135140
CookieJar http.CookieJar
136141

@@ -176,6 +181,7 @@ func NewBrowserClientConfig() *ClientConfig {
176181

177182
return &ClientConfig{
178183
DialerConfig: network.NewDialerConfig(),
184+
DialContext: nil, // Default to nil; can be set by user
179185
CookieJar: jar,
180186
RequestTimeout: 30 * time.Second,
181187
IdleConnTimeout: 90 * time.Second, // Standard browser idle timeout

pkg/customhttp/h1client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ func (c *H1Client) Connect(ctx context.Context) error {
8585

8686
// Clone the dialer config to modify it safely.
8787
dialerConfig := c.Config.DialerConfig.Clone()
88+
89+
// Use custom DialContext if provided in ClientConfig
90+
if c.Config.DialContext != nil {
91+
dialerConfig.DialContext = c.Config.DialContext
92+
}
93+
8894
// Ensure NoDelay is set for responsiveness, crucial for H1.
8995
dialerConfig.NoDelay = true
9096

pkg/customhttp/h2client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ func (c *H2Client) Connect(ctx context.Context) error {
257257
func (c *H2Client) dialH2Connection(ctx context.Context) (net.Conn, error) {
258258
dialerConfig := c.Config.DialerConfig.Clone()
259259

260+
// Use custom DialContext if provided in ClientConfig
261+
if c.Config.DialContext != nil {
262+
dialerConfig.DialContext = c.Config.DialContext
263+
}
264+
260265
if dialerConfig.TLSConfig == nil {
261266
dialerConfig.TLSConfig = network.NewDialerConfig().TLSConfig.Clone()
262267
}

0 commit comments

Comments
 (0)