Skip to content

Commit

Permalink
Use InetSocketAddress instead
Browse files Browse the repository at this point in the history
  • Loading branch information
arturobernalg committed Nov 16, 2024
1 parent 7b678ce commit 95bb898
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -68,13 +67,13 @@ public interface DnsResolver {
String resolveCanonicalHostname(String host) throws UnknownHostException;

/**
* Returns a list of {@link SocketAddress} for the given host with the given port.
* Returns a list of {@link InetSocketAddress} for the given host with the given port.
*
* @see SocketAddress
* @see InetSocketAddress
*
* @since 5.5
*/
default List<SocketAddress> resolve(String host, int port) throws UnknownHostException {
default List<InetSocketAddress> resolve(String host, int port) throws UnknownHostException {
final InetAddress[] inetAddresses = resolve(host);
if (inetAddresses == null) {
return Collections.singletonList(InetSocketAddress.createUnresolved(host, port));
Expand All @@ -83,5 +82,4 @@ default List<SocketAddress> resolve(String host, int port) throws UnknownHostExc
.map(e -> new InetSocketAddress(e, port))
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void connect(
final SocketAddress socksProxyAddress = socketConfig.getSocksProxyAddress();
final Proxy socksProxy = socksProxyAddress != null ? new Proxy(Proxy.Type.SOCKS, socksProxyAddress) : null;

final List<SocketAddress> remoteAddresses;
final List<InetSocketAddress> remoteAddresses;
if (endpointHost.getAddress() != null) {
remoteAddresses = Collections.singletonList(
new InetSocketAddress(endpointHost.getAddress(), this.schemePortResolver.resolve(endpointHost.getSchemeName(), endpointHost)));
Expand All @@ -172,7 +172,7 @@ public void connect(
remoteAddresses = this.dnsResolver.resolve(endpointHost.getHostName(), port);
}
for (int i = 0; i < remoteAddresses.size(); i++) {
final InetSocketAddress remoteAddress = (InetSocketAddress) remoteAddresses.get(i);
final InetSocketAddress remoteAddress = remoteAddresses.get(i);
final boolean last = i == remoteAddresses.size() - 1;
onBeforeSocketConnect(context, endpointHost);
if (LOG.isDebugEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void cancelled() {
LOG.debug("{} resolving remote address", remoteEndpoint.getHostName());
}

final List<SocketAddress> remoteAddresses;
final List<InetSocketAddress> remoteAddresses;
try {
remoteAddresses = dnsResolver.resolve(remoteEndpoint.getHostName(), remoteEndpoint.getPort());
if (remoteAddresses == null || remoteAddresses.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -93,7 +92,7 @@ void testConnect() throws Exception {
final InetAddress ip1 = InetAddress.getByAddress(new byte[] {127, 0, 0, 1});
final InetAddress ip2 = InetAddress.getByAddress(new byte[] {127, 0, 0, 2});
final int port = 80;
final List<SocketAddress> resolvedAddresses = Arrays.asList(
final List<InetSocketAddress> resolvedAddresses = Arrays.asList(
new InetSocketAddress(ip1, port),
new InetSocketAddress(ip2, port)
);
Expand Down Expand Up @@ -137,7 +136,7 @@ void testConnectWithTLSUpgrade() throws Exception {
.setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_1)
.build();

final List<SocketAddress> resolvedAddresses = Arrays.asList(
final List<InetSocketAddress> resolvedAddresses = Arrays.asList(
new InetSocketAddress(ip1, port),
new InetSocketAddress(ip2, port)
);
Expand Down Expand Up @@ -173,7 +172,7 @@ void testConnectTimeout() throws Exception {
final int port = 80;
final InetAddress ip1 = InetAddress.getByAddress(new byte[] {10, 0, 0, 1});
final InetAddress ip2 = InetAddress.getByAddress(new byte[] {10, 0, 0, 2});
final List<SocketAddress> resolvedAddresses = Arrays.asList(
final List<InetSocketAddress> resolvedAddresses = Arrays.asList(
new InetSocketAddress(ip1, port),
new InetSocketAddress(ip2, port)
);
Expand All @@ -194,7 +193,7 @@ void testConnectFailure() throws Exception {
final InetAddress ip1 = InetAddress.getByAddress(new byte[] {10, 0, 0, 1});
final InetAddress ip2 = InetAddress.getByAddress(new byte[] {10, 0, 0, 2});
final int port = 80;
final List<SocketAddress> resolvedAddresses = Arrays.asList(
final List<InetSocketAddress> resolvedAddresses = Arrays.asList(
new InetSocketAddress(ip1, port),
new InetSocketAddress(ip2, port)
);
Expand Down Expand Up @@ -311,7 +310,7 @@ void testConnectWithDisableDnsResolution() throws Exception {
final InetAddress local = InetAddress.getByAddress(new byte[]{127, 0, 0, 0});
final int port = 80;

final List<SocketAddress> resolvedAddresses = Collections.singletonList(
final List<InetSocketAddress> resolvedAddresses = Collections.singletonList(
InetSocketAddress.createUnresolved(host.getHostName(), port)
);
Mockito.when(dnsResolver.resolve(host.getHostName(), port)).thenReturn(resolvedAddresses);
Expand Down Expand Up @@ -354,7 +353,7 @@ void testConnectWithDnsResolutionAndFallback() throws Exception {
final InetAddress ip2 = InetAddress.getByAddress(new byte[] {10, 0, 0, 2});

// Update to match the new `resolve` implementation that returns a list of SocketAddress
final List<SocketAddress> resolvedAddresses = Arrays.asList(
final List<InetSocketAddress> resolvedAddresses = Arrays.asList(
new InetSocketAddress(ip1, port),
new InetSocketAddress(ip2, port)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -73,7 +72,7 @@ void setUp() {
void testConnectWithMultipleAddresses() throws Exception {
final InetAddress address1 = InetAddress.getByAddress(new byte[]{10, 0, 0, 1});
final InetAddress address2 = InetAddress.getByAddress(new byte[]{10, 0, 0, 2});
final List<SocketAddress> remoteAddresses = Arrays.asList(
final List<InetSocketAddress> remoteAddresses = Arrays.asList(
new InetSocketAddress(address1, 8080),
new InetSocketAddress(address2, 8080)
);
Expand Down Expand Up @@ -115,7 +114,7 @@ void testConnectWithMultipleAddresses() throws Exception {
void testConnectSuccessfulAfterRetries() throws Exception {
final InetAddress address1 = InetAddress.getByAddress(new byte[]{10, 0, 0, 1});
final InetAddress address2 = InetAddress.getByAddress(new byte[]{10, 0, 0, 2});
final List<SocketAddress> remoteAddresses = Arrays.asList(
final List<InetSocketAddress> remoteAddresses = Arrays.asList(
new InetSocketAddress(address1, 8080),
new InetSocketAddress(address2, 8080)
);
Expand Down

0 comments on commit 95bb898

Please sign in to comment.