| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_DNS_DNS_HOSTS_H_ | 5 #ifndef NET_DNS_DNS_HOSTS_H_ |
| 6 #define NET_DNS_DNS_HOSTS_H_ | 6 #define NET_DNS_DNS_HOSTS_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 inline size_t hash_value(const net::DnsHostsKey& key) { | 37 inline size_t hash_value(const net::DnsHostsKey& key) { |
| 38 return hash_value(key.first) + key.second; | 38 return hash_value(key.first) + key.second; |
| 39 } | 39 } |
| 40 | 40 |
| 41 #endif // COMPILER | 41 #endif // COMPILER |
| 42 | 42 |
| 43 } // namespace BASE_HASH_NAMESPACE | 43 } // namespace BASE_HASH_NAMESPACE |
| 44 | 44 |
| 45 namespace net { | 45 namespace net { |
| 46 | 46 |
| 47 // There are OS-specific variations in how commas in the hosts file behave. |
| 48 enum ParseHostsCommaMode { |
| 49 // Comma is treated as part of a hostname: |
| 50 // "127.0.0.1 foo,bar" parses as "foo,bar" mapping to "127.0.0.1". |
| 51 PARSE_HOSTS_COMMA_IS_TOKEN, |
| 52 |
| 53 // Comma is treated as a hostname separator: |
| 54 // "127.0.0.1 foo,bar" parses as "foo" and "bar" both mapping to "127.0.0.1". |
| 55 PARSE_HOSTS_COMMA_IS_WHITESPACE, |
| 56 }; |
| 57 |
| 47 // Parsed results of a Hosts file. | 58 // Parsed results of a Hosts file. |
| 48 // | 59 // |
| 49 // Although Hosts files map IP address to a list of domain names, for name | 60 // Although Hosts files map IP address to a list of domain names, for name |
| 50 // resolution the desired mapping direction is: domain name to IP address. | 61 // resolution the desired mapping direction is: domain name to IP address. |
| 51 // When parsing Hosts, we apply the "first hit" rule as Windows and glibc do. | 62 // When parsing Hosts, we apply the "first hit" rule as Windows and glibc do. |
| 52 // With a Hosts file of: | 63 // With a Hosts file of: |
| 53 // 300.300.300.300 localhost # bad ip | 64 // 300.300.300.300 localhost # bad ip |
| 54 // 127.0.0.1 localhost | 65 // 127.0.0.1 localhost |
| 55 // 10.0.0.1 localhost | 66 // 10.0.0.1 localhost |
| 56 // The expected resolution of localhost is 127.0.0.1. | 67 // The expected resolution of localhost is 127.0.0.1. |
| 57 #if !defined(OS_ANDROID) | 68 #if !defined(OS_ANDROID) |
| 58 typedef base::hash_map<DnsHostsKey, IPAddressNumber> DnsHosts; | 69 typedef base::hash_map<DnsHostsKey, IPAddressNumber> DnsHosts; |
| 59 #else | 70 #else |
| 60 // Android's hash_map doesn't support ==, so fall back to map. (Chromium on | 71 // Android's hash_map doesn't support ==, so fall back to map. (Chromium on |
| 61 // Android doesn't use the built-in DNS resolver anyway, so it's irrelevant.) | 72 // Android doesn't use the built-in DNS resolver anyway, so it's irrelevant.) |
| 62 typedef std::map<DnsHostsKey, IPAddressNumber> DnsHosts; | 73 typedef std::map<DnsHostsKey, IPAddressNumber> DnsHosts; |
| 63 #endif | 74 #endif |
| 64 | 75 |
| 65 // Parses |contents| (as read from /etc/hosts or equivalent) and stores results | 76 // Parses |contents| (as read from /etc/hosts or equivalent) and stores results |
| 66 // in |dns_hosts|. Invalid lines are ignored (as in most implementations). | 77 // in |dns_hosts|. Invalid lines are ignored (as in most implementations). |
| 78 // Overrides the OS-specific default handling of commas, so unittests can test |
| 79 // both modes. |
| 80 void NET_EXPORT_PRIVATE ParseHostsWithCommaModeForTesting( |
| 81 const std::string& contents, |
| 82 DnsHosts* dns_hosts, |
| 83 ParseHostsCommaMode comma_mode); |
| 84 |
| 85 // Parses |contents| (as read from /etc/hosts or equivalent) and stores results |
| 86 // in |dns_hosts|. Invalid lines are ignored (as in most implementations). |
| 67 void NET_EXPORT_PRIVATE ParseHosts(const std::string& contents, | 87 void NET_EXPORT_PRIVATE ParseHosts(const std::string& contents, |
| 68 DnsHosts* dns_hosts); | 88 DnsHosts* dns_hosts); |
| 69 | 89 |
| 70 // As above but reads the file pointed to by |path|. | 90 // As above but reads the file pointed to by |path|. |
| 71 bool NET_EXPORT_PRIVATE ParseHostsFile(const base::FilePath& path, | 91 bool NET_EXPORT_PRIVATE ParseHostsFile(const base::FilePath& path, |
| 72 DnsHosts* dns_hosts); | 92 DnsHosts* dns_hosts); |
| 73 | 93 |
| 74 | 94 |
| 75 | 95 |
| 76 } // namespace net | 96 } // namespace net |
| 77 | 97 |
| 78 #endif // NET_DNS_DNS_HOSTS_H_ | 98 #endif // NET_DNS_DNS_HOSTS_H_ |
| 79 | 99 |
| OLD | NEW |