Skip to content

Commit 75fbe70

Browse files
committed
move httpProxyDialer to newHTTPProxyDialerFunc()
1 parent dc42337 commit 75fbe70

File tree

2 files changed

+57
-62
lines changed

2 files changed

+57
-62
lines changed

client.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,23 +255,21 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
255255
return nil, nil, err
256256
}
257257
if proxyURL != nil {
258-
netDial, err = func(proxyURL *url.URL, forwardDial netDialerFunc) (netDialerFunc, error) {
259-
if proxyURL.Scheme == "http" {
260-
return (&httpProxyDialer{proxyURL: proxyURL, forwardDial: forwardDial}).DialContext, nil
261-
}
258+
forwardDial := newNetDialerFunc(proxyURL.Scheme, d.NetDial, d.NetDialContext, d.NetDialTLSContext)
259+
if proxyURL.Scheme == "http" || proxyURL.Scheme == "https" {
260+
netDial = newHTTPProxyDialerFunc(proxyURL, forwardDial)
261+
} else {
262262
dialer, err := proxy.FromURL(proxyURL, forwardDial)
263263
if err != nil {
264-
return nil, err
264+
return nil, nil, err
265265
}
266266
if d, ok := dialer.(proxy.ContextDialer); ok {
267-
return d.DialContext, nil
267+
netDial = d.DialContext
268+
} else {
269+
netDial = func(ctx context.Context, net, addr string) (net.Conn, error) {
270+
return dialer.Dial(net, addr)
271+
}
268272
}
269-
return func(ctx context.Context, net, addr string) (net.Conn, error) {
270-
return dialer.Dial(net, addr)
271-
}, nil
272-
}(proxyURL, netDial)
273-
if err != nil {
274-
return nil, nil, err
275273
}
276274
}
277275
}

proxy.go

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -46,62 +46,59 @@ func (fn netDialerFunc) DialContext(ctx context.Context, network, addr string) (
4646
return fn(ctx, network, addr)
4747
}
4848

49-
type httpProxyDialer struct {
50-
proxyURL *url.URL
51-
forwardDial netDialerFunc
52-
}
53-
54-
func (hpd *httpProxyDialer) DialContext(ctx context.Context, network string, addr string) (net.Conn, error) {
55-
hostPort, _ := hostPortNoPort(hpd.proxyURL)
56-
conn, err := hpd.forwardDial(ctx, network, hostPort)
57-
if err != nil {
58-
return nil, err
59-
}
49+
func newHTTPProxyDialerFunc(proxyURL *url.URL, forwardDial netDialerFunc) netDialerFunc {
50+
return func(ctx context.Context, network, addr string) (net.Conn, error) {
51+
hostPort, _ := hostPortNoPort(proxyURL)
52+
conn, err := forwardDial(ctx, network, hostPort)
53+
if err != nil {
54+
return nil, err
55+
}
6056

61-
connectHeader := make(http.Header)
62-
if user := hpd.proxyURL.User; user != nil {
63-
proxyUser := user.Username()
64-
if proxyPassword, passwordSet := user.Password(); passwordSet {
65-
credential := base64.StdEncoding.EncodeToString([]byte(proxyUser + ":" + proxyPassword))
66-
connectHeader.Set("Proxy-Authorization", "Basic "+credential)
57+
connectHeader := make(http.Header)
58+
if user := proxyURL.User; user != nil {
59+
proxyUser := user.Username()
60+
if proxyPassword, passwordSet := user.Password(); passwordSet {
61+
credential := base64.StdEncoding.EncodeToString([]byte(proxyUser + ":" + proxyPassword))
62+
connectHeader.Set("Proxy-Authorization", "Basic "+credential)
63+
}
6764
}
68-
}
6965

70-
connectReq := &http.Request{
71-
Method: http.MethodConnect,
72-
URL: &url.URL{Opaque: addr},
73-
Host: addr,
74-
Header: connectHeader,
75-
}
66+
connectReq := &http.Request{
67+
Method: http.MethodConnect,
68+
URL: &url.URL{Opaque: addr},
69+
Host: addr,
70+
Header: connectHeader,
71+
}
7672

77-
if err := connectReq.Write(conn); err != nil {
78-
conn.Close()
79-
return nil, err
80-
}
73+
if err := connectReq.Write(conn); err != nil {
74+
conn.Close()
75+
return nil, err
76+
}
8177

82-
// Read response. It's OK to use and discard buffered reader here because
83-
// the remote server does not speak until spoken to.
84-
br := bufio.NewReader(conn)
85-
resp, err := http.ReadResponse(br, connectReq)
86-
if err != nil {
87-
conn.Close()
88-
return nil, err
89-
}
78+
// Read response. It's OK to use and discard buffered reader here because
79+
// the remote server does not speak until spoken to.
80+
br := bufio.NewReader(conn)
81+
resp, err := http.ReadResponse(br, connectReq)
82+
if err != nil {
83+
conn.Close()
84+
return nil, err
85+
}
9086

91-
// Close the response body to silence false positives from linters. Reset
92-
// the buffered reader first to ensure that Close() does not read from
93-
// conn.
94-
// Note: Applications must call resp.Body.Close() on a response returned
95-
// http.ReadResponse to inspect trailers or read another response from the
96-
// buffered reader. The call to resp.Body.Close() does not release
97-
// resources.
98-
br.Reset(bytes.NewReader(nil))
99-
_ = resp.Body.Close()
87+
// Close the response body to silence false positives from linters. Reset
88+
// the buffered reader first to ensure that Close() does not read from
89+
// conn.
90+
// Note: Applications must call resp.Body.Close() on a response returned
91+
// http.ReadResponse to inspect trailers or read another response from the
92+
// buffered reader. The call to resp.Body.Close() does not release
93+
// resources.
94+
br.Reset(bytes.NewReader(nil))
95+
_ = resp.Body.Close()
10096

101-
if resp.StatusCode != http.StatusOK {
102-
_ = conn.Close()
103-
f := strings.SplitN(resp.Status, " ", 2)
104-
return nil, errors.New(f[1])
97+
if resp.StatusCode != http.StatusOK {
98+
_ = conn.Close()
99+
f := strings.SplitN(resp.Status, " ", 2)
100+
return nil, errors.New(f[1])
101+
}
102+
return conn, nil
105103
}
106-
return conn, nil
107104
}

0 commit comments

Comments
 (0)