Skip to content

Commit

Permalink
Move logic to SystemDefaultDnsResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
arturobernalg committed Jan 6, 2025
1 parent 3726527 commit f900a90
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@
package org.apache.hc.client5.http.impl.io;

import java.io.IOException;
import java.net.IDN;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -165,20 +163,13 @@ public void connect(
final SocketAddress socksProxyAddress = socketConfig.getSocksProxyAddress();
final Proxy socksProxy = socksProxyAddress != null ? new Proxy(Proxy.Type.SOCKS, socksProxyAddress) : null;

final String normalizedHost;
try {
normalizedHost = IDN.toASCII(endpointHost.getHostName());
} catch (final IllegalArgumentException e) {
throw new UnknownHostException("Invalid IDN hostname: " + endpointHost.getHostName());
}

final List<InetSocketAddress> remoteAddresses;
if (endpointHost.getAddress() != null) {
remoteAddresses = Collections.singletonList(
new InetSocketAddress(endpointHost.getAddress(), this.schemePortResolver.resolve(endpointHost.getSchemeName(), endpointHost)));
} else {
final int port = this.schemePortResolver.resolve(endpointHost.getSchemeName(), endpointHost);
remoteAddresses = this.dnsResolver.resolve(normalizedHost, port);
remoteAddresses = this.dnsResolver.resolve(endpointHost.getHostName(), port);
}
for (int i = 0; i < remoteAddresses.size(); i++) {
final InetSocketAddress remoteAddress = remoteAddresses.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
package org.apache.hc.client5.http.impl.nio;

import java.io.IOException;
import java.net.IDN;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
Expand Down Expand Up @@ -108,11 +107,10 @@ public void cancelled() {
if (LOG.isDebugEnabled()) {
LOG.debug("{} resolving remote address", remoteEndpoint.getHostName());
}
final String normalizedHost;

final List<InetSocketAddress> remoteAddresses;
try {
normalizedHost = IDN.toASCII(remoteEndpoint.getHostName());
remoteAddresses = dnsResolver.resolve(normalizedHost, remoteEndpoint.getPort());
remoteAddresses = dnsResolver.resolve(remoteEndpoint.getHostName(), remoteEndpoint.getPort());
if (remoteAddresses == null || remoteAddresses.isEmpty()) {
throw new UnknownHostException(remoteEndpoint.getHostName());
}
Expand Down

0 comments on commit f900a90

Please sign in to comment.