Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(418)

Side by Side Diff: net/dns/dns_hosts.h

Issue 1897033002: Reland 'Convert //net and //chromecast to std::unordered_*' (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix TestDownloadRequestHandler Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/disk_cache/simple/simple_index_file.h ('k') | net/http/http_cache.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stddef.h> 8 #include <stddef.h>
9 9
10 #include <map> 10 #include <map>
11 #include <string> 11 #include <string>
12 #include <unordered_map>
12 #include <utility> 13 #include <utility>
13 #include <vector> 14 #include <vector>
14 15
15 #include "base/containers/hash_tables.h"
16 #include "base/files/file_path.h" 16 #include "base/files/file_path.h"
17 #include "base/strings/string_piece.h"
17 #include "net/base/address_family.h" 18 #include "net/base/address_family.h"
18 #include "net/base/ip_address.h" 19 #include "net/base/ip_address.h"
19 #include "net/base/net_export.h" 20 #include "net/base/net_export.h"
20 21
21 namespace net { 22 namespace net {
22 typedef std::pair<std::string, AddressFamily> DnsHostsKey;
23 };
24 23
25 namespace BASE_HASH_NAMESPACE { 24 using DnsHostsKey = std::pair<std::string, AddressFamily>;
26 25
27 template<> 26 struct DnsHostsKeyHash {
28 struct hash<net::DnsHostsKey> { 27 std::size_t operator()(const DnsHostsKey& key) const {
29 std::size_t operator()(const net::DnsHostsKey& key) const {
30 return base::StringPieceHash()(key.first) + key.second; 28 return base::StringPieceHash()(key.first) + key.second;
31 } 29 }
32 }; 30 };
33 31
34 } // namespace BASE_HASH_NAMESPACE
35
36 namespace net {
37
38 // There are OS-specific variations in how commas in the hosts file behave. 32 // There are OS-specific variations in how commas in the hosts file behave.
39 enum ParseHostsCommaMode { 33 enum ParseHostsCommaMode {
40 // Comma is treated as part of a hostname: 34 // Comma is treated as part of a hostname:
41 // "127.0.0.1 foo,bar" parses as "foo,bar" mapping to "127.0.0.1". 35 // "127.0.0.1 foo,bar" parses as "foo,bar" mapping to "127.0.0.1".
42 PARSE_HOSTS_COMMA_IS_TOKEN, 36 PARSE_HOSTS_COMMA_IS_TOKEN,
43 37
44 // Comma is treated as a hostname separator: 38 // Comma is treated as a hostname separator:
45 // "127.0.0.1 foo,bar" parses as "foo" and "bar" both mapping to "127.0.0.1". 39 // "127.0.0.1 foo,bar" parses as "foo" and "bar" both mapping to "127.0.0.1".
46 PARSE_HOSTS_COMMA_IS_WHITESPACE, 40 PARSE_HOSTS_COMMA_IS_WHITESPACE,
47 }; 41 };
48 42
49 // Parsed results of a Hosts file. 43 // Parsed results of a Hosts file.
50 // 44 //
51 // Although Hosts files map IP address to a list of domain names, for name 45 // Although Hosts files map IP address to a list of domain names, for name
52 // resolution the desired mapping direction is: domain name to IP address. 46 // resolution the desired mapping direction is: domain name to IP address.
53 // When parsing Hosts, we apply the "first hit" rule as Windows and glibc do. 47 // When parsing Hosts, we apply the "first hit" rule as Windows and glibc do.
54 // With a Hosts file of: 48 // With a Hosts file of:
55 // 300.300.300.300 localhost # bad ip 49 // 300.300.300.300 localhost # bad ip
56 // 127.0.0.1 localhost 50 // 127.0.0.1 localhost
57 // 10.0.0.1 localhost 51 // 10.0.0.1 localhost
58 // The expected resolution of localhost is 127.0.0.1. 52 // The expected resolution of localhost is 127.0.0.1.
59 #if !defined(OS_ANDROID) 53 using DnsHosts = std::unordered_map<DnsHostsKey, IPAddress, DnsHostsKeyHash>;
60 typedef base::hash_map<DnsHostsKey, IPAddress> DnsHosts;
61 #else
62 // Android's hash_map doesn't support ==, so fall back to map. (Chromium on
63 // Android doesn't use the built-in DNS resolver anyway, so it's irrelevant.)
64 typedef std::map<DnsHostsKey, IPAddress> DnsHosts;
65 #endif
66 54
67 // Parses |contents| (as read from /etc/hosts or equivalent) and stores results 55 // Parses |contents| (as read from /etc/hosts or equivalent) and stores results
68 // in |dns_hosts|. Invalid lines are ignored (as in most implementations). 56 // in |dns_hosts|. Invalid lines are ignored (as in most implementations).
69 // Overrides the OS-specific default handling of commas, so unittests can test 57 // Overrides the OS-specific default handling of commas, so unittests can test
70 // both modes. 58 // both modes.
71 void NET_EXPORT_PRIVATE ParseHostsWithCommaModeForTesting( 59 void NET_EXPORT_PRIVATE ParseHostsWithCommaModeForTesting(
72 const std::string& contents, 60 const std::string& contents,
73 DnsHosts* dns_hosts, 61 DnsHosts* dns_hosts,
74 ParseHostsCommaMode comma_mode); 62 ParseHostsCommaMode comma_mode);
75 63
76 // Parses |contents| (as read from /etc/hosts or equivalent) and stores results 64 // Parses |contents| (as read from /etc/hosts or equivalent) and stores results
77 // in |dns_hosts|. Invalid lines are ignored (as in most implementations). 65 // in |dns_hosts|. Invalid lines are ignored (as in most implementations).
78 void NET_EXPORT_PRIVATE ParseHosts(const std::string& contents, 66 void NET_EXPORT_PRIVATE ParseHosts(const std::string& contents,
79 DnsHosts* dns_hosts); 67 DnsHosts* dns_hosts);
80 68
81 // As above but reads the file pointed to by |path|. 69 // As above but reads the file pointed to by |path|.
82 bool NET_EXPORT_PRIVATE ParseHostsFile(const base::FilePath& path, 70 bool NET_EXPORT_PRIVATE ParseHostsFile(const base::FilePath& path,
83 DnsHosts* dns_hosts); 71 DnsHosts* dns_hosts);
84 72
85 73
86 74
87 } // namespace net 75 } // namespace net
88 76
89 #endif // NET_DNS_DNS_HOSTS_H_ 77 #endif // NET_DNS_DNS_HOSTS_H_
90 78
OLDNEW
« no previous file with comments | « net/disk_cache/simple/simple_index_file.h ('k') | net/http/http_cache.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698