Skip to content

Commit 1650831

Browse files
authored
HTTPCLIENT-2314: Handle gracefully a failure of DnsResolver to return a list of resolved addresses (#533)
1 parent 67519a1 commit 1650831

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public InetAddress[] resolve(final String host) throws UnknownHostException {
9090
LOG.info("Resolving {} to {}", host, Arrays.deepToString(resolvedAddresses));
9191
}
9292
if(resolvedAddresses == null){
93-
throw new UnknownHostException(host + " cannot be resolved");
93+
throw new UnknownHostException(host);
9494
}
9595
return resolvedAddresses;
9696
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.net.Proxy;
3333
import java.net.Socket;
3434
import java.net.SocketAddress;
35+
import java.net.UnknownHostException;
3536
import java.util.Arrays;
3637

3738
import org.apache.hc.client5.http.ConnectExceptionSupport;
@@ -143,8 +144,12 @@ public void connect(
143144
remoteAddresses = this.dnsResolver.resolve(host.getHostName());
144145

145146
if (LOG.isDebugEnabled()) {
146-
LOG.debug("{} resolved to {}", host.getHostName(), Arrays.asList(remoteAddresses));
147+
LOG.debug("{} resolved to {}", host.getHostName(), remoteAddresses == null ? "null" : Arrays.asList(remoteAddresses));
147148
}
149+
150+
if (remoteAddresses == null || remoteAddresses.length == 0) {
151+
throw new UnknownHostException(host.getHostName());
152+
}
148153
}
149154

150155
final Timeout soTimeout = socketConfig.getSoTimeout();

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ public void cancelled() {
111111
final InetAddress[] remoteAddresses;
112112
try {
113113
remoteAddresses = dnsResolver.resolve(remoteEndpoint.getHostName());
114+
if (remoteAddresses == null || remoteAddresses.length == 0) {
115+
throw new UnknownHostException(remoteEndpoint.getHostName());
116+
}
114117
} catch (final UnknownHostException ex) {
115118
future.failed(ex);
116119
return future;

0 commit comments

Comments
 (0)