| 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/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "net/dns/dns_util.h" |
| 12 | 13 |
| 13 using base::StringPiece; | 14 using base::StringPiece; |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 // Parses the contents of a hosts file. Returns one token (IP or hostname) at | 20 // Parses the contents of a hosts file. Returns one token (IP or hostname) at |
| 20 // a time. Doesn't copy anything; accepts the file as a StringPiece and | 21 // a time. Doesn't copy anything; accepts the file as a StringPiece and |
| 21 // returns tokens as StringPieces. | 22 // returns tokens as StringPieces. |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 if (new_ip.AssignFromIPLiteral(parser.token())) { | 149 if (new_ip.AssignFromIPLiteral(parser.token())) { |
| 149 ip_text = new_ip_text; | 150 ip_text = new_ip_text; |
| 150 ip = new_ip; | 151 ip = new_ip; |
| 151 family = (ip.IsIPv4()) ? ADDRESS_FAMILY_IPV4 : ADDRESS_FAMILY_IPV6; | 152 family = (ip.IsIPv4()) ? ADDRESS_FAMILY_IPV4 : ADDRESS_FAMILY_IPV6; |
| 152 } else { | 153 } else { |
| 153 parser.SkipRestOfLine(); | 154 parser.SkipRestOfLine(); |
| 154 } | 155 } |
| 155 } | 156 } |
| 156 } else { | 157 } else { |
| 157 DnsHostsKey key(parser.token().as_string(), family); | 158 DnsHostsKey key(parser.token().as_string(), family); |
| 159 if (!IsValidDNSDomain(key.first)) |
| 160 continue; |
| 158 key.first = base::ToLowerASCII(key.first); | 161 key.first = base::ToLowerASCII(key.first); |
| 159 IPAddress* mapped_ip = &(*dns_hosts)[key]; | 162 IPAddress* mapped_ip = &(*dns_hosts)[key]; |
| 160 if (mapped_ip->empty()) | 163 if (mapped_ip->empty()) |
| 161 *mapped_ip = ip; | 164 *mapped_ip = ip; |
| 162 // else ignore this entry (first hit counts) | 165 // else ignore this entry (first hit counts) |
| 163 } | 166 } |
| 164 } | 167 } |
| 165 } | 168 } |
| 166 | 169 |
| 167 } // namespace | 170 } // namespace |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 std::string contents; | 209 std::string contents; |
| 207 if (!base::ReadFileToString(path, &contents)) | 210 if (!base::ReadFileToString(path, &contents)) |
| 208 return false; | 211 return false; |
| 209 | 212 |
| 210 ParseHosts(contents, dns_hosts); | 213 ParseHosts(contents, dns_hosts); |
| 211 return true; | 214 return true; |
| 212 } | 215 } |
| 213 | 216 |
| 214 } // namespace net | 217 } // namespace net |
| 215 | 218 |
| OLD | NEW |