From dc42337df90160188081e7ed45a541c919ec2e34 Mon Sep 17 00:00:00 2001 From: Cooper Oh Date: Thu, 18 Jul 2024 21:43:52 +0900 Subject: [PATCH] move to newNetDialerFunc --- client.go | 14 +------------- proxy.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/client.go b/client.go index 69347e8..130e779 100644 --- a/client.go +++ b/client.go @@ -246,19 +246,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h defer cancel() } - var netDial netDialerFunc - switch { - case u.Scheme == "https" && d.NetDialTLSContext != nil: - netDial = d.NetDialTLSContext - case d.NetDialContext != nil: - netDial = d.NetDialContext - case d.NetDial != nil: - netDial = func(ctx context.Context, net, addr string) (net.Conn, error) { - return d.NetDial(net, addr) - } - default: - netDial = (&net.Dialer{}).DialContext - } + netDial := newNetDialerFunc(u.Scheme, d.NetDial, d.NetDialContext, d.NetDialTLSContext) // If needed, wrap the dial function to connect through a proxy. if d.Proxy != nil { diff --git a/proxy.go b/proxy.go index 8c8a27e..488d3d4 100644 --- a/proxy.go +++ b/proxy.go @@ -16,6 +16,26 @@ import ( "strings" ) +func newNetDialerFunc( + scheme string, + netDial func(network, addr string) (net.Conn, error), + netDialContext func(ctx context.Context, network, addr string) (net.Conn, error), + netDialTLSContext func(ctx context.Context, network, addr string) (net.Conn, error), +) netDialerFunc { + switch { + case scheme == "https" && netDialTLSContext != nil: + return netDialTLSContext + case netDialContext != nil: + return netDialContext + case netDial != nil: + return func(ctx context.Context, net, addr string) (net.Conn, error) { + return netDial(net, addr) + } + default: + return (&net.Dialer{}).DialContext + } +} + type netDialerFunc func(ctx context.Context, network, addr string) (net.Conn, error) func (fn netDialerFunc) Dial(network, addr string) (net.Conn, error) {