Skip to content

Commit 8c934ba

Browse files
committed
Normalize IDN hostnames to punycode before DNS resolution to prevent UnknownHostException during connection. (#608)
1 parent 3fa07f1 commit 8c934ba

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

httpclient5/src/main/java/org/apache/hc/client5/http/SystemDefaultDnsResolver.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
*/
2727
package org.apache.hc.client5.http;
2828

29+
import java.net.IDN;
2930
import java.net.InetAddress;
3031
import java.net.UnknownHostException;
3132

33+
import org.apache.hc.core5.util.TextUtils;
34+
3235
/**
3336
* DNS resolver that uses the default OS implementation for resolving host names.
3437
*
@@ -44,8 +47,18 @@ public class SystemDefaultDnsResolver implements DnsResolver {
4447
@Override
4548
public InetAddress[] resolve(final String host) throws UnknownHostException {
4649
try {
50+
String normalizedHost;
51+
if (TextUtils.isAllASCII(host)) {
52+
normalizedHost = host;
53+
} else {
54+
try {
55+
normalizedHost = IDN.toASCII(host);
56+
} catch (final IllegalArgumentException e) {
57+
normalizedHost = host; // Fall back to original hostname
58+
}
59+
}
4760
// Try resolving using the default resolver
48-
return InetAddress.getAllByName(host);
61+
return InetAddress.getAllByName(normalizedHost);
4962
} catch (final UnknownHostException e) {
5063
// If default resolver fails, try stripping the IPv6 zone ID and resolving again
5164
String strippedHost = null;

0 commit comments

Comments
 (0)