Skip to content

Commit

Permalink
Normalize IDN hostnames to punycode before DNS resolution to prevent …
Browse files Browse the repository at this point in the history
…UnknownHostException during connection.
  • Loading branch information
arturobernalg committed Jan 6, 2025
1 parent 9e3559e commit 4b06d10
Showing 1 changed file with 3 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/
package org.apache.hc.client5.http;

import java.net.IDN;
import java.net.InetAddress;
import java.net.UnknownHostException;

Expand All @@ -44,8 +45,9 @@ public class SystemDefaultDnsResolver implements DnsResolver {
@Override
public InetAddress[] resolve(final String host) throws UnknownHostException {
try {
final String normalizedHost = IDN.toASCII(host);
// 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;
Expand Down

0 comments on commit 4b06d10

Please sign in to comment.