View Javadoc
1   /*
2    * Copyright 2015 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  package io.netty.resolver;
17  
18  import io.netty.util.CharsetUtil;
19  import io.netty.util.internal.PlatformDependent;
20  import io.netty.util.internal.UnstableApi;
21  
22  import java.net.Inet4Address;
23  import java.net.Inet6Address;
24  import java.net.InetAddress;
25  import java.nio.charset.Charset;
26  import java.util.Locale;
27  import java.util.Map;
28  
29  /**
30   * Default {@link HostsFileEntriesResolver} that resolves hosts file entries only once.
31   */
32  @UnstableApi
33  public final class DefaultHostsFileEntriesResolver implements HostsFileEntriesResolver {
34  
35      private final Map<String, Inet4Address> inet4Entries;
36      private final Map<String, Inet6Address> inet6Entries;
37  
38      public DefaultHostsFileEntriesResolver() {
39          this(parseEntries());
40      }
41  
42      // for testing purpose only
43      DefaultHostsFileEntriesResolver(HostsFileEntries entries) {
44          inet4Entries = entries.inet4Entries();
45          inet6Entries = entries.inet6Entries();
46      }
47  
48      @Override
49      public InetAddress address(String inetHost, ResolvedAddressTypes resolvedAddressTypes) {
50          String normalized = normalize(inetHost);
51          switch (resolvedAddressTypes) {
52              case IPV4_ONLY:
53                  return inet4Entries.get(normalized);
54              case IPV6_ONLY:
55                  return inet6Entries.get(normalized);
56              case IPV4_PREFERRED:
57                  Inet4Address inet4Address = inet4Entries.get(normalized);
58                  return inet4Address != null? inet4Address : inet6Entries.get(normalized);
59              case IPV6_PREFERRED:
60                  Inet6Address inet6Address = inet6Entries.get(normalized);
61                  return inet6Address != null? inet6Address : inet4Entries.get(normalized);
62              default:
63                  throw new IllegalArgumentException("Unknown ResolvedAddressTypes " + resolvedAddressTypes);
64          }
65      }
66  
67      // package-private for testing purposes
68      String normalize(String inetHost) {
69          return inetHost.toLowerCase(Locale.ENGLISH);
70      }
71  
72      private static HostsFileEntries parseEntries() {
73          if (PlatformDependent.isWindows()) {
74              // Ony windows there seems to be no standard for the encoding used for the hosts file, so let us
75              // try multiple until we either were able to parse it or there is none left and so we return an
76              // empty intstance.
77              return HostsFileParser.parseSilently(Charset.defaultCharset(), CharsetUtil.UTF_16, CharsetUtil.UTF_8);
78          }
79          return HostsFileParser.parseSilently();
80      }
81  }