@@ -46,62 +46,59 @@ func (fn netDialerFunc) DialContext(ctx context.Context, network, addr string) (
46
46
return fn (ctx , network , addr )
47
47
}
48
48
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
+ }
60
56
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
+ }
67
64
}
68
- }
69
65
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
+ }
76
72
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
+ }
81
77
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
+ }
90
86
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 ()
100
96
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
105
103
}
106
- return conn , nil
107
104
}
0 commit comments