Chromium Code Reviews| 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/string_tokenizer.h" | 10 #include "base/string_tokenizer.h" |
| 10 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 11 | 12 |
| 12 namespace net { | 13 namespace net { |
| 13 | 14 |
| 14 void ParseHosts(const std::string& contents, DnsHosts* dns_hosts) { | 15 void ParseHosts(const std::string& contents, DnsHosts* dns_hosts) { |
| 15 CHECK(dns_hosts); | 16 CHECK(dns_hosts); |
| 16 DnsHosts& hosts = *dns_hosts; | 17 DnsHosts& hosts = *dns_hosts; |
| 17 // Split into lines. Accept CR for Windows. | 18 // Split into lines. Accept CR for Windows. |
| 18 StringTokenizer contents_lines(contents, "\n\r"); | 19 StringTokenizer contents_lines(contents, "\n\r"); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 40 IPAddressNumber& mapped_ip = hosts[key]; | 41 IPAddressNumber& mapped_ip = hosts[key]; |
| 41 if (mapped_ip.empty()) | 42 if (mapped_ip.empty()) |
| 42 mapped_ip = ip; | 43 mapped_ip = ip; |
| 43 // else ignore this entry (first hit counts) | 44 // else ignore this entry (first hit counts) |
| 44 } | 45 } |
| 45 } | 46 } |
| 46 } | 47 } |
| 47 } | 48 } |
| 48 } | 49 } |
| 49 | 50 |
| 50 // Reads the contents of the file at |path| into |str| if the total length is | |
| 51 // less than |max_size|. | |
| 52 static bool ReadFile(const FilePath& path, int64 max_size, std::string* str) { | |
|
szym
2012/09/12 16:42:35
I removed this function because it doesn't seem to
| |
| 53 int64 size; | |
| 54 if (!file_util::GetFileSize(path, &size) || size > max_size) | |
| 55 return false; | |
| 56 return file_util::ReadFileToString(path, str); | |
| 57 } | |
| 58 | |
| 59 bool ParseHostsFile(const FilePath& path, DnsHosts* dns_hosts) { | 51 bool ParseHostsFile(const FilePath& path, DnsHosts* dns_hosts) { |
| 60 dns_hosts->clear(); | 52 dns_hosts->clear(); |
| 61 // Missing file indicates empty HOSTS. | 53 // Missing file indicates empty HOSTS. |
| 62 if (!file_util::PathExists(path)) | 54 if (!file_util::PathExists(path)) |
| 63 return true; | 55 return true; |
| 64 | 56 |
| 57 int64 size; | |
| 58 if (!file_util::GetFileSize(path, &size)) | |
| 59 return false; | |
| 60 | |
| 61 UMA_HISTOGRAM_COUNTS("AsyncDNS.HostsSize", size); | |
| 62 | |
| 63 // Reject HOSTS files larger than |kMaxHostsSize|. | |
| 64 const int64 kMaxHostsSize = 1 << 16; | |
| 65 if (size > kMaxHostsSize) | |
| 66 return false; | |
| 67 | |
| 65 std::string contents; | 68 std::string contents; |
| 66 const int64 kMaxHostsSize = 1 << 16; | 69 if (!file_util::ReadFileToString(path, &contents)) |
| 67 if (!ReadFile(path, kMaxHostsSize, &contents)) | |
| 68 return false; | 70 return false; |
| 69 | 71 |
| 70 ParseHosts(contents, dns_hosts); | 72 ParseHosts(contents, dns_hosts); |
| 71 return true; | 73 return true; |
| 72 } | 74 } |
| 73 | 75 |
| 74 } // namespace net | 76 } // namespace net |
| 75 | 77 |
| OLD | NEW |