Realigned the behavior of TLS upgrade in the classic and async connection operators

This commit is contained in:
Oleg Kalnichevski 2024-01-22 12:24:11 +01:00
parent 9d75647eca
commit 9d225a6517
3 changed files with 19 additions and 13 deletions

View File

@ -53,7 +53,6 @@ import org.apache.hc.core5.annotation.Internal;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.http.ConnectionClosedException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.config.Lookup;
import org.apache.hc.core5.http.io.SocketConfig;
import org.apache.hc.core5.http.protocol.HttpContext;
@ -226,7 +225,7 @@ public class DefaultHttpClientConnectionOperator implements HttpClientConnection
host.getHostName(), host.getPort(), localAddress, remoteAddress, ConnPoolSupport.getId(conn));
}
final TlsSocketStrategy tlsSocketStrategy = tlsSocketStrategyLookup != null ? tlsSocketStrategyLookup.lookup(host.getSchemeName()) : null;
if (tlsSocketStrategy != null && URIScheme.HTTPS.same(host.getSchemeName())) {
if (tlsSocketStrategy != null) {
final Socket upgradedSocket = tlsSocketStrategy.upgrade(socket, host.getHostName(), port, attachment, context);
conn.bind(upgradedSocket);
}
@ -266,18 +265,18 @@ public class DefaultHttpClientConnectionOperator implements HttpClientConnection
final HttpHost host,
final Object attachment,
final HttpContext context) throws IOException {
final TlsSocketStrategy tlsSocketStrategy = tlsSocketStrategyLookup != null ? tlsSocketStrategyLookup.lookup(host.getSchemeName()) : null;
if (tlsSocketStrategy == null) {
throw new UnsupportedSchemeException(host.getSchemeName() +
" protocol is not supported");
}
final Socket socket = conn.getSocket();
if (socket == null) {
throw new ConnectionClosedException("Connection is closed");
}
final int port = this.schemePortResolver.resolve(host);
final SSLSocket upgradedSocket = tlsSocketStrategy.upgrade(socket, host.getHostName(), port, attachment, context);
conn.bind(upgradedSocket);
final TlsSocketStrategy tlsSocketStrategy = tlsSocketStrategyLookup != null ? tlsSocketStrategyLookup.lookup(host.getSchemeName()) : null;
if (tlsSocketStrategy != null) {
final int port = this.schemePortResolver.resolve(host);
final SSLSocket upgradedSocket = tlsSocketStrategy.upgrade(socket, host.getHostName(), port, attachment, context);
conn.bind(upgradedSocket);
} else {
throw new UnsupportedSchemeException(host.getSchemeName() + " protocol is not supported");
}
}
}

View File

@ -34,6 +34,7 @@ import java.util.concurrent.Future;
import org.apache.hc.client5.http.DnsResolver;
import org.apache.hc.client5.http.SchemePortResolver;
import org.apache.hc.client5.http.UnsupportedSchemeException;
import org.apache.hc.client5.http.config.TlsConfig;
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
import org.apache.hc.client5.http.nio.AsyncClientConnectionOperator;
@ -44,7 +45,6 @@ import org.apache.hc.core5.concurrent.ComplexFuture;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.concurrent.FutureContribution;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.config.Lookup;
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
import org.apache.hc.core5.http.protocol.HttpContext;
@ -109,7 +109,7 @@ final class DefaultAsyncClientConnectionOperator implements AsyncClientConnectio
public void completed(final IOSession session) {
final DefaultManagedAsyncClientConnection connection = new DefaultManagedAsyncClientConnection(session);
final TlsStrategy tlsStrategy = tlsStrategyLookup != null ? tlsStrategyLookup.lookup(host.getSchemeName()) : null;
if (tlsStrategy != null && URIScheme.HTTPS.same(host.getSchemeName())) {
if (tlsStrategy != null) {
try {
final Timeout socketTimeout = connection.getSocketTimeout();
final Timeout handshakeTimeout = tlsConfig.getHandshakeTimeout();
@ -191,8 +191,9 @@ final class DefaultAsyncClientConnectionOperator implements AsyncClientConnectio
}
});
} else {
callback.failed(new UnsupportedSchemeException(host.getSchemeName() + " protocol is not supported"));
}
}
}

View File

@ -262,6 +262,9 @@ public class TestHttpClientConnectionOperator {
final HttpContext context = new BasicHttpContext();
final HttpHost host = new HttpHost("httpsssss", "somehost", -1);
Mockito.when(conn.isOpen()).thenReturn(true);
Mockito.when(conn.getSocket()).thenReturn(socket);
Assertions.assertThrows(UnsupportedSchemeException.class, () ->
connectionOperator.upgrade(conn, host, context));
}
@ -271,6 +274,9 @@ public class TestHttpClientConnectionOperator {
final HttpContext context = new BasicHttpContext();
final HttpHost host = new HttpHost("http", "somehost", -1);
Mockito.when(conn.isOpen()).thenReturn(true);
Mockito.when(conn.getSocket()).thenReturn(socket);
Assertions.assertThrows(UnsupportedSchemeException.class, () ->
connectionOperator.upgrade(conn, host, context));
}