Skip to content

Commit

Permalink
Moves logic of name resolution to DefaultDnsResolver from TcpClientCo…
Browse files Browse the repository at this point in the history
…nnection. New tests for DefaultDnsResolver.
  • Loading branch information
spericas committed Jan 23, 2024
1 parent a4a078e commit f35d335
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
package io.helidon.webclient.api;

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

import io.helidon.webclient.spi.DnsResolver;

Expand Down Expand Up @@ -46,7 +47,15 @@ public boolean useDefaultJavaResolver() {

@Override
public InetAddress resolveAddress(String hostname, DnsAddressLookup dnsAddressLookup) {
throw new IllegalStateException("This DNS resolver is not meant to be used.");
try {
InetAddress[] addresses = InetAddress.getAllByName(hostname);
addresses = dnsAddressLookup.filter(addresses);
if (addresses.length > 0) {
return addresses[0];
}
} catch (UnknownHostException e) {
// falls through
}
throw new IllegalArgumentException("Failed to get address from host: " + hostname);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,8 @@ private String createChannelId(Socket socket) {

private InetSocketAddress inetSocketAddress() {
DnsResolver dnsResolver = connectionKey.dnsResolver();
if (dnsResolver.useDefaultJavaResolver()) {
try {
InetAddress[] addresses = InetAddress.getAllByName(connectionKey.host());
addresses = connectionKey.dnsAddressLookup().filter(addresses);
if (addresses.length > 0) {
return new InetSocketAddress(addresses[0], connectionKey.port());
}
} catch (UnknownHostException e) {
// falls through
}
throw new IllegalArgumentException("Failed to get address from host: " + connectionKey.host());
} else {
InetAddress address = dnsResolver.resolveAddress(connectionKey.host(), connectionKey.dnsAddressLookup());
return new InetSocketAddress(address, connectionKey.port());
}
InetAddress address = dnsResolver.resolveAddress(connectionKey.host(), connectionKey.dnsAddressLookup());
return new InetSocketAddress(address, connectionKey.port());
}

private void debugTls(SSLSocket sslSocket) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.webclient.api;

import java.net.InetAddress;

import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.is;

class DefaultDnsResolverTest {

@Test
void testIpv4Resolution() {
DefaultDnsResolver resolver = DefaultDnsResolver.create();
InetAddress inetAddress = resolver.resolveAddress("localhost", DnsAddressLookup.IPV4_PREFERRED);
assertThat(inetAddress.getHostAddress(), is("127.0.0.1"));
}

@Test
void testIpv6Resolution() {
DefaultDnsResolver resolver = DefaultDnsResolver.create();
InetAddress inetAddress = resolver.resolveAddress("localhost", DnsAddressLookup.IPV6_PREFERRED);
assertThat(inetAddress.getHostAddress(), is("0:0:0:0:0:0:0:1"));
}
}

0 comments on commit f35d335

Please sign in to comment.