Skip to content

Commit 7c8ecfe

Browse files
committed
Backport UnknownHostException mentions hostname with search domain added, close AsyncHttpClient#1223
1 parent 39d5f5a commit 7c8ecfe

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

netty-bp/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolverContext.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public void operationComplete(Future<AddressedEnvelope<DnsResponse, InetSocketAd
7070
private final DnsNameResolver parent;
7171
private final DnsServerAddressStream nameServerAddrs;
7272
private final String hostname;
73+
protected String pristineHostname;
7374
private final DnsCache resolveCache;
7475
private final boolean traceEnabled;
7576
private final int maxAllowedQueries;
@@ -117,6 +118,7 @@ public void operationComplete(Future<T> future) throws Exception {
117118
String nextHostname = DnsNameResolverContext.this.hostname + "." + searchDomain;
118119
DnsNameResolverContext<T> nextContext = newResolverContext(parent,
119120
nextHostname, resolveCache);
121+
nextContext.pristineHostname = hostname;
120122
nextContext.internalResolve(nextPromise);
121123
nextPromise.addListener(this);
122124
} else {
@@ -450,8 +452,13 @@ private void finishResolve(Promise<T> promise) {
450452
final int tries = maxAllowedQueries - allowedQueries;
451453
final StringBuilder buf = new StringBuilder(64);
452454

453-
buf.append("failed to resolve '")
454-
.append(hostname).append('\'');
455+
buf.append("failed to resolve '");
456+
if (pristineHostname != null) {
457+
buf.append(pristineHostname);
458+
} else {
459+
buf.append(hostname);
460+
}
461+
buf.append('\'');
455462
if (tries > 1) {
456463
if (tries < maxAllowedQueries) {
457464
buf.append(" after ")

netty-bp/resolver-dns/src/test/java/io/netty/resolver/dns/SearchDomainTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.Test;
2525

2626
import java.net.InetAddress;
27+
import java.net.UnknownHostException;
2728
import java.util.ArrayList;
2829
import java.util.Arrays;
2930
import java.util.Collections;
@@ -34,7 +35,10 @@
3435

3536
import static org.junit.Assert.assertEquals;
3637
import static org.junit.Assert.assertFalse;
38+
import static org.junit.Assert.assertThat;
3739
import static org.junit.Assert.assertTrue;
40+
import static org.hamcrest.Matchers.not;
41+
import static org.hamcrest.core.StringContains.containsString;
3842

3943
public class SearchDomainTest {
4044

@@ -265,4 +269,23 @@ private List<String> assertResolveAll(DnsNameResolver resolver, String inetHost)
265269
}
266270
return list;
267271
}
272+
273+
@Test
274+
public void testExceptionMsgNoSearchDomain() throws Exception {
275+
Set<String> domains = new HashSet<String>();
276+
277+
TestDnsServer.MapRecordStoreA store = new TestDnsServer.MapRecordStoreA(domains);
278+
dnsServer = new TestDnsServer(store);
279+
dnsServer.start();
280+
281+
resolver = newResolver().searchDomains(Collections.singletonList("foo.com")).build();
282+
283+
Future<InetAddress> fut = resolver.resolve("unknown.hostname");
284+
assertTrue(fut.await(10, TimeUnit.SECONDS));
285+
assertFalse(fut.isSuccess());
286+
final Throwable cause = fut.cause();
287+
assertEquals(UnknownHostException.class, cause.getClass());
288+
assertThat("search domain is included in UnknownHostException", cause.getMessage(),
289+
not(containsString("foo.com")));
290+
}
268291
}

0 commit comments

Comments
 (0)