@@ -131,44 +131,41 @@ class PooledDialer {
131131 // Factory: full CONNECT tunnel handshake sequence.
132132 // DNS resolve proxy -> TCP -> [TLS to proxy] -> CONNECT handshake -> TLS to target.
133133 ISocketStream* make_tunnel_stream (const StoredURL& proxy_url, std::string_view target_host,
134- uint16_t target_port, const Headers* headers, uint64_t timeout) {
134+ uint16_t target_port, const Headers* headers) {
135+ LOG_DEBUG (" CONNECT tunnel: begin, proxy=`:` target=`:`" ,
136+ proxy_url.host_no_port (), proxy_url.port (), target_host, target_port);
135137 auto proxy_ip = resolver->resolve (proxy_url.host_no_port ());
136138 if (proxy_ip.undefined ())
137139 LOG_ERROR_RETURN (ENOENT , nullptr , " CONNECT tunnel: DNS resolve failed for proxy `" , proxy_url.host_no_port ());
138140 EndPoint proxy_ep (proxy_ip, proxy_url.port ());
139- tcpsock->timeout (timeout);
140141 auto stream = tcpsock->connect (proxy_ep);
141142 if (!stream) {
142143 resolver->discard_cache (proxy_url.host_no_port (), proxy_ip);
143144 LOG_ERRNO_RETURN (0 , nullptr , " CONNECT tunnel: failed to connect to proxy" );
144145 }
145- stream-> timeout (timeout );
146+ LOG_DEBUG ( " CONNECT tunnel: connected to proxy ` " , proxy_ep );
146147 if (proxy_url.secure ()) {
147- auto tls = new_tls_stream (tls_ctx, stream, SecurityRole::Client, true );
148- if (!tls) {
149- delete stream;
148+ stream = new_tls_stream (tls_ctx, stream, SecurityRole::Client, true );
149+ if (!stream)
150150 LOG_ERRNO_RETURN (0 , nullptr , " CONNECT tunnel: TLS to proxy failed" );
151- }
152- stream = tls;
153151 tls_stream_set_hostname (stream, std::string (proxy_url.host_no_port ()).c_str ());
154152 }
155- if (do_connect_handshake (stream, target_host, target_port, headers, timeout ) != 0 ) {
153+ if (do_connect_handshake (stream, target_host, target_port, headers) != 0 ) {
156154 delete stream;
157155 return nullptr ;
158156 }
159- auto tls = new_tls_stream (tls_ctx, stream, SecurityRole::Client, true );
160- if (!tls) {
161- delete stream;
157+ LOG_DEBUG ( " CONNECT tunnel: handshake done, wrapping TLS to target ` " , target_host );
158+ stream = new_tls_stream (tls_ctx, stream, SecurityRole::Client, true );
159+ if (! stream)
162160 LOG_ERRNO_RETURN (0 , nullptr , " CONNECT tunnel: TLS to target failed" );
163- }
164- stream = tls;
165161 tls_stream_set_hostname (stream, std::string (target_host).c_str ());
162+ LOG_DEBUG (" CONNECT tunnel: established to target `" , target_host);
166163 return stream;
167164 }
168165
169166 // Send CONNECT request and verify 2xx response.
170167 int do_connect_handshake (ISocketStream* stream, std::string_view target_host,
171- uint16_t target_port, const Headers* headers, uint64_t timeout ) {
168+ uint16_t target_port, const Headers* headers) {
172169 char buf[4096 ];
173170 estring authority;
174171 authority.appends (target_host, " :" , target_port);
@@ -179,14 +176,16 @@ class PooledDialer {
179176 }
180177 if (req.send_header (stream) < 0 )
181178 LOG_ERRNO_RETURN (0 , -1 , " CONNECT tunnel: failed to send CONNECT request" );
179+ LOG_DEBUG (" CONNECT tunnel: sent CONNECT `" , authority);
182180
183181 // Response needs room for receive_bytes' MAX_TRANSFER_BYTES + RESERVED_INDEX_SIZE.
184182 char rbuf[8192 ];
185183 Response resp (rbuf, (uint16_t )sizeof (rbuf));
186184 resp.reset (stream, false );
187- if (resp.receive_header (timeout ) != 0 )
185+ if (resp.receive_header () != 0 )
188186 LOG_ERRNO_RETURN (0 , -1 , " CONNECT tunnel: failed to read proxy response" );
189187 auto sc = resp.status_code ();
188+ LOG_DEBUG (" CONNECT tunnel: proxy response status `" , sc);
190189 if (sc < 200 || sc >= 300 )
191190 LOG_ERROR_RETURN (EINVAL , -1 , " CONNECT tunnel: proxy returned status `" , sc);
192191 return 0 ;
@@ -200,7 +199,7 @@ ISocketStream* PooledDialer::dial(Client::Operation* op, Timeout tmo) {
200199 if (proxy && op->req .secure ()) {
201200 auto key = make_tunnel_key (*proxy, op->req .host_no_port (), op->req .port (), &op->proxy_header );
202201 return tcp_pool->connect (key, [&]() -> ISocketStream* {
203- return make_tunnel_stream (*proxy, op->req .host_no_port (), op->req .port (), &op->proxy_header , tmo. timeout () );
202+ return make_tunnel_stream (*proxy, op->req .host_no_port (), op->req .port (), &op->proxy_header );
204203 });
205204 }
206205
0 commit comments