Skip to content

Commit 9d4599d

Browse files
committed
1 parent 19fb6c7 commit 9d4599d

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

netty-bp/resolver/src/main/java/io/netty/resolver/DefaultHostsFileEntriesResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.netty.resolver;
1717

1818
import java.net.InetAddress;
19+
import java.util.Locale;
1920
import java.util.Map;
2021

2122
/**
@@ -27,6 +28,6 @@ public final class DefaultHostsFileEntriesResolver implements HostsFileEntriesRe
2728

2829
@Override
2930
public InetAddress address(String inetHost) {
30-
return entries.get(inetHost);
31+
return entries.get(inetHost.toLowerCase(Locale.ENGLISH));
3132
}
3233
}

netty-bp/resolver/src/main/java/io/netty/resolver/HostsFileParser.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.netty.resolver;
1717

18+
import static io.netty.util.internal.ObjectUtil.checkNotNull;
1819
import io.netty.util.NetUtil;
1920
import io.netty.util.internal.PlatformDependent;
2021
import io.netty.util.internal.logging.InternalLogger;
@@ -28,13 +29,12 @@
2829
import java.net.InetAddress;
2930
import java.util.ArrayList;
3031
import java.util.Collections;
31-
import java.util.List;
3232
import java.util.HashMap;
33+
import java.util.List;
34+
import java.util.Locale;
3335
import java.util.Map;
3436
import java.util.regex.Pattern;
3537

36-
import static io.netty.util.internal.ObjectUtil.*;
37-
3838
/**
3939
* A parser for hosts files.
4040
*/
@@ -151,10 +151,11 @@ public static Map<String, InetAddress> parse(Reader reader) throws IOException {
151151
// loop over hostname and aliases
152152
for (int i = 1; i < lineParts.size(); i ++) {
153153
String hostname = lineParts.get(i);
154-
if (!entries.containsKey(hostname)) {
154+
String hostnameLower = hostname.toLowerCase(Locale.ENGLISH);
155+
if (!entries.containsKey(hostnameLower)) {
155156
// trying to map a host to multiple IPs is wrong
156157
// only the first entry is honored
157-
entries.put(hostname, InetAddress.getByAddress(hostname, ipBytes));
158+
entries.put(hostnameLower, InetAddress.getByAddress(hostname, ipBytes));
158159
}
159160
}
160161
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2016 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
/**
17+
* show issue https://github.com/netty/netty/issues/5182
18+
* HostsFileParser tries to resolve hostnames as case-sensitive
19+
*/
20+
package io.netty.resolver;
21+
22+
import org.junit.Test;
23+
24+
import static org.junit.Assert.assertNotNull;
25+
26+
public class DefaultHostsFileEntriesResolverTest {
27+
28+
@Test
29+
public void testLocalhost() {
30+
DefaultHostsFileEntriesResolver resolver = new DefaultHostsFileEntriesResolver();
31+
assertNotNull("localhost doesn't resolve", resolver.address("localhost"));
32+
assertNotNull("LOCALHOST doesn't resolve", resolver.address("LOCALHOST"));
33+
}
34+
}

netty-bp/resolver/src/test/java/io/netty/resolver/HostsFileParserTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public void testParse() throws IOException {
3838
.append("192.168.0.2 host3 #comment").append("\n") // comment after hostname
3939
.append("192.168.0.3 host4 host5 host6").append("\n") // multiple aliases
4040
.append("192.168.0.4 host4").append("\n") // host mapped to a second address, must be ignored
41+
.append("192.168.0.5 HOST7").append("\n") // uppercase host, should match lowercase host
42+
.append("192.168.0.6 host7").append("\n") // should be ignored since we have the uppercase host already
4143
.toString();
4244

4345
Map<String, InetAddress> entries = HostsFileParser.parse(new BufferedReader(new StringReader(hostsString)));
@@ -49,5 +51,7 @@ public void testParse() throws IOException {
4951
assertEquals("192.168.0.3", entries.get("host4").getHostAddress());
5052
assertEquals("192.168.0.3", entries.get("host5").getHostAddress());
5153
assertEquals("192.168.0.3", entries.get("host6").getHostAddress());
54+
assertNotNull("uppercase host doesn't resolve", entries.get("host7"));
55+
assertEquals("192.168.0.5", entries.get("host7").getHostAddress());
5256
}
5357
}

0 commit comments

Comments
 (0)