26
26
*/
27
27
package org .apache .hc .client5 .http .impl .io ;
28
28
29
- import javax .net .ssl .SSLSocket ;
30
29
import java .io .IOException ;
31
30
import java .net .InetSocketAddress ;
32
31
import java .net .Proxy ;
36
35
import java .util .Collections ;
37
36
import java .util .List ;
38
37
39
- import jdk .net .ExtendedSocketOptions ;
40
- import jdk . net . Sockets ;
38
+ import javax .net .ssl . SSLSocket ;
39
+
41
40
import org .apache .hc .client5 .http .ConnectExceptionSupport ;
42
41
import org .apache .hc .client5 .http .DnsResolver ;
43
42
import org .apache .hc .client5 .http .SchemePortResolver ;
44
43
import org .apache .hc .client5 .http .SystemDefaultDnsResolver ;
45
44
import org .apache .hc .client5 .http .UnsupportedSchemeException ;
45
+ import org .apache .hc .client5 .http .config .TlsConfig ;
46
46
import org .apache .hc .client5 .http .impl .ConnPoolSupport ;
47
47
import org .apache .hc .client5 .http .impl .DefaultSchemePortResolver ;
48
48
import org .apache .hc .client5 .http .io .DetachedSocketFactory ;
67
67
import org .slf4j .Logger ;
68
68
import org .slf4j .LoggerFactory ;
69
69
70
+ import jdk .net .ExtendedSocketOptions ;
71
+ import jdk .net .Sockets ;
72
+
70
73
/**
71
74
* Default implementation of {@link HttpClientConnectionOperator} used as default in Http client,
72
75
* when no instance provided by user to {@link BasicHttpClientConnectionManager} or {@link
@@ -178,11 +181,10 @@ public void connect(
178
181
Args .notNull (socketConfig , "Socket config" );
179
182
Args .notNull (context , "Context" );
180
183
181
- final Timeout soTimeout = socketConfig .getSoTimeout ();
182
184
final SocketAddress socksProxyAddress = socketConfig .getSocksProxyAddress ();
183
185
final Proxy socksProxy = socksProxyAddress != null ? new Proxy (Proxy .Type .SOCKS , socksProxyAddress ) : null ;
184
186
if (unixDomainSocket != null ) {
185
- connectToUnixDomainSocket (conn , endpointHost , endpointName , attachment , unixDomainSocket , connectTimeout , socketConfig , context , soTimeout );
187
+ connectToUnixDomainSocket (conn , endpointHost , endpointName , attachment , unixDomainSocket , connectTimeout , socketConfig , context );
186
188
return ;
187
189
}
188
190
@@ -208,17 +210,16 @@ public void connect(
208
210
socket .bind (localAddress );
209
211
}
210
212
conn .bind (socket );
211
- configureSocket (socket , socketConfig , soTimeout );
213
+ configureSocket (socket , socketConfig );
212
214
socket .connect (remoteAddress , TimeValue .isPositive (connectTimeout ) ? connectTimeout .toMillisecondsIntBound () : 0 );
213
215
conn .bind (socket );
214
216
onAfterSocketConnect (context , endpointHost );
215
217
if (LOG .isDebugEnabled ()) {
216
218
LOG .debug ("{} {} connected {}->{}" , ConnPoolSupport .getId (conn ), endpointHost , conn .getLocalAddress (), conn .getRemoteAddress ());
217
219
}
218
- conn .setSocketTimeout (soTimeout );
219
220
final TlsSocketStrategy tlsSocketStrategy = tlsSocketStrategyLookup != null ? tlsSocketStrategyLookup .lookup (endpointHost .getSchemeName ()) : null ;
220
221
if (tlsSocketStrategy != null ) {
221
- upgradeToTls (conn , endpointHost , endpointName , attachment , context , tlsSocketStrategy , socket );
222
+ upgradeToTls (conn , endpointHost , endpointName , connectTimeout , attachment , context , tlsSocketStrategy , socket );
222
223
}
223
224
return ;
224
225
} catch (final RuntimeException ex ) {
@@ -240,15 +241,23 @@ public void connect(
240
241
}
241
242
242
243
private void upgradeToTls (final ManagedHttpClientConnection conn , final HttpHost endpointHost ,
243
- final NamedEndpoint endpointName , final Object attachment , final HttpContext context ,
244
- final TlsSocketStrategy tlsSocketStrategy , final Socket socket ) throws IOException {
244
+ final NamedEndpoint endpointName , final Timeout connectTimeout , final Object attachment ,
245
+ final HttpContext context , final TlsSocketStrategy tlsSocketStrategy , final Socket socket )
246
+ throws IOException {
245
247
final NamedEndpoint tlsName = endpointName != null ? endpointName : endpointHost ;
246
248
onBeforeTlsHandshake (context , endpointHost );
247
249
if (LOG .isDebugEnabled ()) {
248
250
LOG .debug ("{} {} upgrading to TLS" , ConnPoolSupport .getId (conn ), tlsName );
249
251
}
252
+ final TlsConfig tlsConfig = attachment instanceof TlsConfig ? (TlsConfig ) attachment : TlsConfig .DEFAULT ;
253
+ final int soTimeout = socket .getSoTimeout ();
254
+ final Timeout handshakeTimeout = tlsConfig .getHandshakeTimeout () != null ? tlsConfig .getHandshakeTimeout () : connectTimeout ;
255
+ if (handshakeTimeout != null ) {
256
+ socket .setSoTimeout (handshakeTimeout .toMillisecondsIntBound ());
257
+ }
250
258
final SSLSocket sslSocket = tlsSocketStrategy .upgrade (socket , tlsName .getHostName (), tlsName .getPort (), attachment , context );
251
259
conn .bind (sslSocket , socket );
260
+ socket .setSoTimeout (soTimeout );
252
261
onAfterTlsHandshake (context , endpointHost );
253
262
if (LOG .isDebugEnabled ()) {
254
263
LOG .debug ("{} {} upgraded to TLS" , ConnPoolSupport .getId (conn ), tlsName );
@@ -263,8 +272,7 @@ private void connectToUnixDomainSocket(
263
272
final Path unixDomainSocket ,
264
273
final Timeout connectTimeout ,
265
274
final SocketConfig socketConfig ,
266
- final HttpContext context ,
267
- final Timeout soTimeout ) throws IOException {
275
+ final HttpContext context ) throws IOException {
268
276
onBeforeSocketConnect (context , endpointHost );
269
277
if (LOG .isDebugEnabled ()) {
270
278
LOG .debug ("{} connecting to {} ({})" , endpointHost , unixDomainSocket , connectTimeout );
@@ -275,16 +283,15 @@ private void connectToUnixDomainSocket(
275
283
final Socket socket = unixDomainSocketFactory .connectSocket (newSocket , unixDomainSocket ,
276
284
connectTimeout );
277
285
conn .bind (socket );
278
- configureSocket (socket , socketConfig , soTimeout );
286
+ configureSocket (socket , socketConfig );
279
287
onAfterSocketConnect (context , endpointHost );
280
288
if (LOG .isDebugEnabled ()) {
281
289
LOG .debug ("{} {} connected to {}" , ConnPoolSupport .getId (conn ), endpointHost , unixDomainSocket );
282
290
}
283
- conn .setSocketTimeout (soTimeout );
284
291
285
292
final TlsSocketStrategy tlsSocketStrategy = tlsSocketStrategyLookup != null ? tlsSocketStrategyLookup .lookup (endpointHost .getSchemeName ()) : null ;
286
293
if (tlsSocketStrategy != null ) {
287
- upgradeToTls (conn , endpointHost , endpointName , attachment , context , tlsSocketStrategy , socket );
294
+ upgradeToTls (conn , endpointHost , endpointName , connectTimeout , attachment , context , tlsSocketStrategy , socket );
288
295
}
289
296
} catch (final RuntimeException ex ) {
290
297
Closer .closeQuietly (newSocket );
@@ -300,10 +307,10 @@ private void connectToUnixDomainSocket(
300
307
}
301
308
302
309
@ SuppressWarnings ("Since15" )
303
- private static void configureSocket (final Socket socket , final SocketConfig socketConfig ,
304
- final Timeout soTimeout ) throws IOException {
305
- if (soTimeout != null ) {
306
- socket .setSoTimeout (soTimeout .toMillisecondsIntBound ());
310
+ private static void configureSocket (final Socket socket , final SocketConfig socketConfig ) throws IOException {
311
+ final Timeout socketTimeout = socketConfig . getSoTimeout ();
312
+ if (socketTimeout != null ) {
313
+ socket .setSoTimeout (socketTimeout .toMillisecondsIntBound ());
307
314
}
308
315
socket .setReuseAddress (socketConfig .isSoReuseAddress ());
309
316
socket .setTcpNoDelay (socketConfig .isTcpNoDelay ());
0 commit comments