From 3c787861f0c2e6390973dfc964474c6344c6f82c Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Sun, 5 Jan 2025 20:12:44 +0100 Subject: [PATCH] Normalize IDN hostnames to punycode before DNS resolution to prevent UnknownHostException during connection. --- .../hc/client5/http/SystemDefaultDnsResolver.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/SystemDefaultDnsResolver.java b/httpclient5/src/main/java/org/apache/hc/client5/http/SystemDefaultDnsResolver.java index 6a4d293573..0b27fbd7c1 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/SystemDefaultDnsResolver.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/SystemDefaultDnsResolver.java @@ -26,9 +26,12 @@ */ package org.apache.hc.client5.http; +import java.net.IDN; import java.net.InetAddress; import java.net.UnknownHostException; +import org.apache.hc.core5.util.TextUtils; + /** * DNS resolver that uses the default OS implementation for resolving host names. * @@ -44,8 +47,18 @@ public class SystemDefaultDnsResolver implements DnsResolver { @Override public InetAddress[] resolve(final String host) throws UnknownHostException { try { + String normalizedHost; + if (TextUtils.isAllASCII(host)) { + normalizedHost = host; + } else { + try { + normalizedHost = IDN.toASCII(host); + } catch (final IllegalArgumentException e) { + normalizedHost = host; // Fall back to original hostname + } + } // Try resolving using the default resolver - return InetAddress.getAllByName(host); + return InetAddress.getAllByName(normalizedHost); } catch (final UnknownHostException e) { // If default resolver fails, try stripping the IPv6 zone ID and resolving again String strippedHost = null;