| 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/file_util.h" | 7 #include "base/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/string_tokenizer.h" | |
| 11 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/strings/string_tokenizer.h" |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 void ParseHosts(const std::string& contents, DnsHosts* dns_hosts) { | 15 void ParseHosts(const std::string& contents, DnsHosts* dns_hosts) { |
| 16 CHECK(dns_hosts); | 16 CHECK(dns_hosts); |
| 17 DnsHosts& hosts = *dns_hosts; | 17 DnsHosts& hosts = *dns_hosts; |
| 18 // Split into lines. Accept CR for Windows. | 18 // Split into lines. Accept CR for Windows. |
| 19 StringTokenizer contents_lines(contents, "\n\r"); | 19 base::StringTokenizer contents_lines(contents, "\n\r"); |
| 20 while (contents_lines.GetNext()) { | 20 while (contents_lines.GetNext()) { |
| 21 // Ignore comments after '#'. | 21 // Ignore comments after '#'. |
| 22 std::string line = contents_lines.token(); | 22 std::string line = contents_lines.token(); |
| 23 StringTokenizer line_parts(line, "#"); | 23 base::StringTokenizer line_parts(line, "#"); |
| 24 line_parts.set_options(StringTokenizer::RETURN_DELIMS); | 24 line_parts.set_options(base::StringTokenizer::RETURN_DELIMS); |
| 25 | 25 |
| 26 if (line_parts.GetNext() && !line_parts.token_is_delim()) { | 26 if (line_parts.GetNext() && !line_parts.token_is_delim()) { |
| 27 // Split and trim whitespace. | 27 // Split and trim whitespace. |
| 28 std::string part = line_parts.token(); | 28 std::string part = line_parts.token(); |
| 29 StringTokenizer tokens(part, " \t"); | 29 base::StringTokenizer tokens(part, " \t"); |
| 30 | 30 |
| 31 if (tokens.GetNext()) { | 31 if (tokens.GetNext()) { |
| 32 IPAddressNumber ip; | 32 IPAddressNumber ip; |
| 33 // TODO(szym): handle %iface notation on mac | 33 // TODO(szym): handle %iface notation on mac |
| 34 if (!ParseIPLiteralToNumber(tokens.token(), &ip)) | 34 if (!ParseIPLiteralToNumber(tokens.token(), &ip)) |
| 35 continue; // Ignore malformed lines. | 35 continue; // Ignore malformed lines. |
| 36 AddressFamily fam = (ip.size() == 4) ? ADDRESS_FAMILY_IPV4 : | 36 AddressFamily fam = (ip.size() == 4) ? ADDRESS_FAMILY_IPV4 : |
| 37 ADDRESS_FAMILY_IPV6; | 37 ADDRESS_FAMILY_IPV6; |
| 38 while (tokens.GetNext()) { | 38 while (tokens.GetNext()) { |
| 39 DnsHostsKey key(tokens.token(), fam); | 39 DnsHostsKey key(tokens.token(), fam); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 68 std::string contents; | 68 std::string contents; |
| 69 if (!file_util::ReadFileToString(path, &contents)) | 69 if (!file_util::ReadFileToString(path, &contents)) |
| 70 return false; | 70 return false; |
| 71 | 71 |
| 72 ParseHosts(contents, dns_hosts); | 72 ParseHosts(contents, dns_hosts); |
| 73 return true; | 73 return true; |
| 74 } | 74 } |
| 75 | 75 |
| 76 } // namespace net | 76 } // namespace net |
| 77 | 77 |
| OLD | NEW |