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 #include "net/dns/dns_hosts.h" | 5 #include "net/dns/dns_hosts.h" |
6 | 6 |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 | 124 |
125 const ParseHostsCommaMode comma_mode_; | 125 const ParseHostsCommaMode comma_mode_; |
126 | 126 |
127 DISALLOW_COPY_AND_ASSIGN(HostsParser); | 127 DISALLOW_COPY_AND_ASSIGN(HostsParser); |
128 }; | 128 }; |
129 | 129 |
130 void ParseHostsWithCommaMode(const std::string& contents, | 130 void ParseHostsWithCommaMode(const std::string& contents, |
131 DnsHosts* dns_hosts, | 131 DnsHosts* dns_hosts, |
132 ParseHostsCommaMode comma_mode) { | 132 ParseHostsCommaMode comma_mode) { |
133 CHECK(dns_hosts); | 133 CHECK(dns_hosts); |
134 DnsHosts& hosts = *dns_hosts; | |
135 | 134 |
136 StringPiece ip_text; | 135 StringPiece ip_text; |
137 IPAddressNumber ip; | 136 IPAddressNumber ip; |
138 AddressFamily family = ADDRESS_FAMILY_IPV4; | 137 AddressFamily family = ADDRESS_FAMILY_IPV4; |
139 HostsParser parser(contents, comma_mode); | 138 HostsParser parser(contents, comma_mode); |
140 while (parser.Advance()) { | 139 while (parser.Advance()) { |
141 if (parser.token_is_ip()) { | 140 if (parser.token_is_ip()) { |
142 StringPiece new_ip_text = parser.token(); | 141 StringPiece new_ip_text = parser.token(); |
143 // Some ad-blocking hosts files contain thousands of entries pointing to | 142 // Some ad-blocking hosts files contain thousands of entries pointing to |
144 // the same IP address (usually 127.0.0.1). Don't bother parsing the IP | 143 // the same IP address (usually 127.0.0.1). Don't bother parsing the IP |
145 // again if it's the same as the one above it. | 144 // again if it's the same as the one above it. |
146 if (new_ip_text != ip_text) { | 145 if (new_ip_text != ip_text) { |
147 IPAddressNumber new_ip; | 146 IPAddressNumber new_ip; |
148 if (ParseIPLiteralToNumber(parser.token().as_string(), &new_ip)) { | 147 if (ParseIPLiteralToNumber(parser.token().as_string(), &new_ip)) { |
149 ip_text = new_ip_text; | 148 ip_text = new_ip_text; |
150 ip.swap(new_ip); | 149 ip.swap(new_ip); |
151 family = (ip.size() == 4) ? ADDRESS_FAMILY_IPV4 : ADDRESS_FAMILY_IPV6; | 150 family = (ip.size() == 4) ? ADDRESS_FAMILY_IPV4 : ADDRESS_FAMILY_IPV6; |
152 } else { | 151 } else { |
153 parser.SkipRestOfLine(); | 152 parser.SkipRestOfLine(); |
154 } | 153 } |
155 } | 154 } |
156 } else { | 155 } else { |
157 DnsHostsKey key(parser.token().as_string(), family); | 156 DnsHostsKey key(parser.token().as_string(), family); |
158 base::StringToLowerASCII(&key.first); | 157 base::StringToLowerASCII(&key.first); |
159 IPAddressNumber& mapped_ip = hosts[key]; | 158 IPAddressNumber* mapped_ip = &(*dns_hosts)[key]; |
160 if (mapped_ip.empty()) | 159 if (mapped_ip->empty()) |
161 mapped_ip = ip; | 160 *mapped_ip = ip; |
162 // else ignore this entry (first hit counts) | 161 // else ignore this entry (first hit counts) |
163 } | 162 } |
164 } | 163 } |
165 } | 164 } |
166 | 165 |
167 } // namespace | 166 } // namespace |
168 | 167 |
169 void ParseHostsWithCommaModeForTesting(const std::string& contents, | 168 void ParseHostsWithCommaModeForTesting(const std::string& contents, |
170 DnsHosts* dns_hosts, | 169 DnsHosts* dns_hosts, |
171 ParseHostsCommaMode comma_mode) { | 170 ParseHostsCommaMode comma_mode) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 std::string contents; | 205 std::string contents; |
207 if (!base::ReadFileToString(path, &contents)) | 206 if (!base::ReadFileToString(path, &contents)) |
208 return false; | 207 return false; |
209 | 208 |
210 ParseHosts(contents, dns_hosts); | 209 ParseHosts(contents, dns_hosts); |
211 return true; | 210 return true; |
212 } | 211 } |
213 | 212 |
214 } // namespace net | 213 } // namespace net |
215 | 214 |
OLD | NEW |