Ignore inline comments when parsing nameservers (#9894)
Motivation: The resolv.conf file may contain inline comments which should be ignored Modifications: - Detect if we have a comment after the ipaddress and if so skip it - Add unit test Result: Fixes https://github.com/netty/netty/issues/9889
This commit is contained in:
parent
607cf801d2
commit
7931501769
@ -543,7 +543,7 @@ public final class StringUtil {
|
||||
*
|
||||
* @param seq The string to search.
|
||||
* @param offset The offset to start searching at.
|
||||
* @return the index of the first non-white space character or <{@code 0} if none was found.
|
||||
* @return the index of the first non-white space character or <{@code -1} if none was found.
|
||||
*/
|
||||
public static int indexOfNonWhiteSpace(CharSequence seq, int offset) {
|
||||
for (; offset < seq.length(); ++offset) {
|
||||
@ -554,6 +554,22 @@ public final class StringUtil {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the index of the first white space character in {@code s} starting at {@code offset}.
|
||||
*
|
||||
* @param seq The string to search.
|
||||
* @param offset The offset to start searching at.
|
||||
* @return the index of the first white space character or <{@code -1} if none was found.
|
||||
*/
|
||||
public static int indexOfWhiteSpace(CharSequence seq, int offset) {
|
||||
for (; offset < seq.length(); ++offset) {
|
||||
if (Character.isWhitespace(seq.charAt(offset))) {
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if {@code c} lies within the range of values defined for
|
||||
* <a href="http://unicode.org/glossary/#surrogate_code_point">Surrogate Code Point</a>.
|
||||
|
@ -36,6 +36,8 @@ import java.util.regex.Pattern;
|
||||
|
||||
import static io.netty.resolver.dns.DefaultDnsServerAddressStreamProvider.DNS_PORT;
|
||||
import static io.netty.util.internal.StringUtil.indexOfNonWhiteSpace;
|
||||
import static io.netty.util.internal.StringUtil.indexOfWhiteSpace;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
/**
|
||||
@ -179,7 +181,20 @@ public final class UnixResolverDnsServerAddressStreamProvider implements DnsServ
|
||||
" in file " + etcResolverFile + ". value: " + line);
|
||||
}
|
||||
|
||||
String maybeIP = line.substring(i);
|
||||
String maybeIP;
|
||||
int x = indexOfWhiteSpace(line, i);
|
||||
if (x == -1) {
|
||||
maybeIP = line.substring(i);
|
||||
} else {
|
||||
// ignore comments
|
||||
int idx = indexOfNonWhiteSpace(line, x);
|
||||
if (idx == -1 || line.charAt(idx) != '#') {
|
||||
throw new IllegalArgumentException("error parsing label " + NAMESERVER_ROW_LABEL +
|
||||
" in file " + etcResolverFile + ". value: " + line);
|
||||
}
|
||||
maybeIP = line.substring(i, x);
|
||||
}
|
||||
|
||||
// There may be a port appended onto the IP address so we attempt to extract it.
|
||||
if (!NetUtil.isValidIpV4Address(maybeIP) && !NetUtil.isValidIpV6Address(maybeIP)) {
|
||||
i = maybeIP.lastIndexOf('.');
|
||||
|
@ -188,6 +188,17 @@ public class UnixResolverDnsServerAddressStreamProviderTest {
|
||||
return f;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ignoreComments() throws Exception {
|
||||
File f = buildFile("domain linecorp.local\n" +
|
||||
"nameserver 127.0.0.2 #somecomment\n");
|
||||
UnixResolverDnsServerAddressStreamProvider p =
|
||||
new UnixResolverDnsServerAddressStreamProvider(f, null);
|
||||
|
||||
DnsServerAddressStream stream = p.nameServerAddressStream("somehost");
|
||||
assertHostNameEquals("127.0.0.2", stream.next());
|
||||
}
|
||||
|
||||
private static void assertHostNameEquals(String expectedHostname, InetSocketAddress next) {
|
||||
assertEquals("unexpected hostname: " + next, expectedHostname, next.getHostString());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user