4141import org .apache .hc .client5 .http .SchemePortResolver ;
4242import org .apache .hc .client5 .http .SystemDefaultDnsResolver ;
4343import org .apache .hc .client5 .http .UnsupportedSchemeException ;
44+ import org .apache .hc .client5 .http .config .TlsConfig ;
4445import org .apache .hc .client5 .http .impl .ConnPoolSupport ;
4546import org .apache .hc .client5 .http .impl .DefaultSchemePortResolver ;
4647import org .apache .hc .client5 .http .io .DetachedSocketFactory ;
@@ -160,7 +161,6 @@ public void connect(
160161 Args .notNull (socketConfig , "Socket config" );
161162 Args .notNull (context , "Context" );
162163
163- final Timeout soTimeout = socketConfig .getSoTimeout ();
164164 final SocketAddress socksProxyAddress = socketConfig .getSocksProxyAddress ();
165165 final Proxy socksProxy = socksProxyAddress != null ? new Proxy (Proxy .Type .SOCKS , socksProxyAddress ) : null ;
166166
@@ -186,8 +186,9 @@ public void connect(
186186 socket .bind (localAddress );
187187 }
188188 conn .bind (socket );
189- if (soTimeout != null ) {
190- socket .setSoTimeout (soTimeout .toMillisecondsIntBound ());
189+ final Timeout socketTimeout = socketConfig .getSoTimeout ();
190+ if (socketTimeout != null ) {
191+ socket .setSoTimeout (socketTimeout .toMillisecondsIntBound ());
191192 }
192193 socket .setReuseAddress (socketConfig .isSoReuseAddress ());
193194 socket .setTcpNoDelay (socketConfig .isTcpNoDelay ());
@@ -217,16 +218,22 @@ public void connect(
217218 if (LOG .isDebugEnabled ()) {
218219 LOG .debug ("{} {} connected {}->{}" , ConnPoolSupport .getId (conn ), endpointHost , conn .getLocalAddress (), conn .getRemoteAddress ());
219220 }
220- conn .setSocketTimeout (soTimeout );
221221 final TlsSocketStrategy tlsSocketStrategy = tlsSocketStrategyLookup != null ? tlsSocketStrategyLookup .lookup (endpointHost .getSchemeName ()) : null ;
222222 if (tlsSocketStrategy != null ) {
223223 final NamedEndpoint tlsName = endpointName != null ? endpointName : endpointHost ;
224224 onBeforeTlsHandshake (context , endpointHost );
225225 if (LOG .isDebugEnabled ()) {
226226 LOG .debug ("{} {} upgrading to TLS" , ConnPoolSupport .getId (conn ), tlsName );
227227 }
228+ final TlsConfig tlsConfig = attachment instanceof TlsConfig ? (TlsConfig ) attachment : TlsConfig .DEFAULT ;
229+ final int soTimeout = socket .getSoTimeout ();
230+ final Timeout handshakeTimeout = tlsConfig .getHandshakeTimeout () != null ? tlsConfig .getHandshakeTimeout () : connectTimeout ;
231+ if (handshakeTimeout != null ) {
232+ socket .setSoTimeout (handshakeTimeout .toMillisecondsIntBound ());
233+ }
228234 final SSLSocket sslSocket = tlsSocketStrategy .upgrade (socket , tlsName .getHostName (), tlsName .getPort (), attachment , context );
229235 conn .bind (sslSocket , socket );
236+ socket .setSoTimeout (soTimeout );
230237 onAfterTlsHandshake (context , endpointHost );
231238 if (LOG .isDebugEnabled ()) {
232239 LOG .debug ("{} {} upgraded to TLS" , ConnPoolSupport .getId (conn ), tlsName );
0 commit comments