Skip to content

Commit 8094bac

Browse files
committed
HTTPCLIENT-2386: Classic transport to use connect timeout as a default if TLS timeout has not been explicitly set
1 parent da4d9a4 commit 8094bac

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/DefaultHttpClientConnectionOperator.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.apache.hc.client5.http.SchemePortResolver;
4242
import org.apache.hc.client5.http.SystemDefaultDnsResolver;
4343
import org.apache.hc.client5.http.UnsupportedSchemeException;
44+
import org.apache.hc.client5.http.config.TlsConfig;
4445
import org.apache.hc.client5.http.impl.ConnPoolSupport;
4546
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
4647
import 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);

httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/DefaultAsyncClientConnectionOperator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void completed(final IOSession session) {
141141
if (tlsStrategy != null) {
142142
try {
143143
final Timeout socketTimeout = connection.getSocketTimeout();
144-
final Timeout handshakeTimeout = tlsConfig.getHandshakeTimeout();
144+
final Timeout handshakeTimeout = tlsConfig.getHandshakeTimeout() != null ? tlsConfig.getHandshakeTimeout() : connectTimeout;
145145
final NamedEndpoint tlsName = endpointName != null ? endpointName : endpointHost;
146146
onBeforeTlsHandshake(context, endpointHost);
147147
if (LOG.isDebugEnabled()) {
@@ -151,7 +151,7 @@ public void completed(final IOSession session) {
151151
connection,
152152
tlsName,
153153
attachment,
154-
handshakeTimeout != null ? handshakeTimeout : connectTimeout,
154+
handshakeTimeout,
155155
new FutureContribution<TransportSecurityLayer>(future) {
156156

157157
@Override

httpclient5/src/main/java/org/apache/hc/client5/http/ssl/AbstractClientTlsStrategy.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,6 @@ private void executeHandshake(
220220
final SSLSocket upgradedSocket,
221221
final String target,
222222
final Object attachment) throws IOException {
223-
final TlsConfig tlsConfig = attachment instanceof TlsConfig ? (TlsConfig) attachment : TlsConfig.DEFAULT;
224-
225223
final SSLParameters sslParameters = upgradedSocket.getSSLParameters();
226224
if (supportedProtocols != null) {
227225
sslParameters.setProtocols(supportedProtocols);
@@ -238,17 +236,11 @@ private void executeHandshake(
238236
}
239237
upgradedSocket.setSSLParameters(sslParameters);
240238

241-
final Timeout handshakeTimeout = tlsConfig.getHandshakeTimeout();
242-
if (handshakeTimeout != null) {
243-
upgradedSocket.setSoTimeout(handshakeTimeout.toMillisecondsIntBound());
244-
}
245-
246239
initializeSocket(upgradedSocket);
247240

248241
if (LOG.isDebugEnabled()) {
249242
LOG.debug("Enabled protocols: {}", (Object) upgradedSocket.getEnabledProtocols());
250243
LOG.debug("Enabled cipher suites: {}", (Object) upgradedSocket.getEnabledCipherSuites());
251-
LOG.debug("Starting handshake ({})", handshakeTimeout);
252244
}
253245
upgradedSocket.startHandshake();
254246
verifySession(target, upgradedSocket.getSession());

0 commit comments

Comments
 (0)