Details
-
Bug
-
Status: Resolved
-
Minor
-
Resolution: Fixed
-
5.1.3
-
None
Description
I am passing HttpHost instance with predefined IP to execute method of CloseableAsyncHttpClient.
This works and the dns resolver is not used for the request. But it only works if port is specified in the HttpHost. When I specify -1 as a port, then default is correctly used, but IP address from HttpHost is lost during request preprocessing -> DnsResolver is called and IP is resolved again.
I think the problem is within the org.apache.hc.client5.http.routing.RoutingSupport#normalize method. Here the port is set to default, according to scheme, but the address field is not transferred to the newly created HttpHost instance
Simple testing example:
Here when port is set to -1, dns resolving happens and custom dns resolver is called - NOK
When port is set to be > 0, dns resolving is correctly skipped and predefined localhost address is used - OK
public static void main(String[] args) throws UnknownHostException, ExecutionException, InterruptedException { PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create() .setDnsResolver( new DnsResolver() { @Override public InetAddress[] resolve(String host) throws UnknownHostException { throw new UnknownHostException("Trying to resolve IP address"); } @Override public String resolveCanonicalHostname(String host) throws UnknownHostException { throw new UnknownHostException("Trying to resolve IP address"); } }) .build(); CloseableHttpAsyncClient client = HttpAsyncClients.custom().setConnectionManager(connectionManager).build(); try { client.start(); SimpleHttpRequest request = SimpleHttpRequest.create("GET", "/"); HttpHost httpHost = new HttpHost("https", InetAddress.getLocalHost(), "google.com", -1); HttpClientContext context = HttpClientContext.create(); SimpleResponseConsumer responseConsumer = SimpleResponseConsumer.create(); client .execute( httpHost, SimpleRequestProducer.create(request), responseConsumer, null, context, null) .get(); } finally { client.close(CloseMode.GRACEFUL); } }
Thanks for checking