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

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

Issue 9721002: [net/dns] Removes locking from DnsConfigServiceWin and adds local computer name. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Move lowercasing to ServeFromHosts, add test. Reorganize OnHostsRead. Created 8 years, 9 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/dns/dns_hosts.h ('k') | net/dns/dns_hosts_unittest.cc » ('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 #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/string_tokenizer.h" 9 #include "base/string_tokenizer.h"
10 #include "base/string_util.h"
10 11
11 namespace net { 12 namespace net {
12 13
13 void ParseHosts(const std::string& contents, DnsHosts* dns_hosts) { 14 void ParseHosts(const std::string& contents, DnsHosts* dns_hosts) {
14 CHECK(dns_hosts); 15 CHECK(dns_hosts);
15 DnsHosts& hosts = *dns_hosts; 16 DnsHosts& hosts = *dns_hosts;
16 // Split into lines. Accept CR for Windows. 17 // Split into lines. Accept CR for Windows.
17 StringTokenizer contents_lines(contents, "\n\r"); 18 StringTokenizer contents_lines(contents, "\n\r");
18 while (contents_lines.GetNext()) { 19 while (contents_lines.GetNext()) {
19 // Ignore comments after '#'. 20 // Ignore comments after '#'.
20 std::string line = contents_lines.token(); 21 std::string line = contents_lines.token();
21 StringTokenizer line_parts(line, "#"); 22 StringTokenizer line_parts(line, "#");
22 line_parts.set_options(StringTokenizer::RETURN_DELIMS); 23 line_parts.set_options(StringTokenizer::RETURN_DELIMS);
23 24
24 if (line_parts.GetNext() && !line_parts.token_is_delim()) { 25 if (line_parts.GetNext() && !line_parts.token_is_delim()) {
25 // Split and trim whitespace. 26 // Split and trim whitespace.
26 std::string part = line_parts.token(); 27 std::string part = line_parts.token();
27 StringTokenizer tokens(part, " \t"); 28 StringTokenizer tokens(part, " \t");
28 29
29 if (tokens.GetNext()) { 30 if (tokens.GetNext()) {
30 IPAddressNumber ip; 31 IPAddressNumber ip;
31 // TODO(szym): handle %iface notation on mac 32 // TODO(szym): handle %iface notation on mac
32 if (!ParseIPLiteralToNumber(tokens.token(), &ip)) 33 if (!ParseIPLiteralToNumber(tokens.token(), &ip))
33 continue; // Ignore malformed lines. 34 continue; // Ignore malformed lines.
34 AddressFamily fam = (ip.size() == 4) ? ADDRESS_FAMILY_IPV4 : 35 AddressFamily fam = (ip.size() == 4) ? ADDRESS_FAMILY_IPV4 :
35 ADDRESS_FAMILY_IPV6; 36 ADDRESS_FAMILY_IPV6;
36 while (tokens.GetNext()) { 37 while (tokens.GetNext()) {
37 IPAddressNumber& mapped_ip = hosts[DnsHostsKey(tokens.token(), fam)]; 38 DnsHostsKey key(tokens.token(), fam);
39 StringToLowerASCII(&(key.first));
40 IPAddressNumber& mapped_ip = hosts[key];
38 if (mapped_ip.empty()) 41 if (mapped_ip.empty())
39 mapped_ip = ip; 42 mapped_ip = ip;
40 // else ignore this entry (first hit counts) 43 // else ignore this entry (first hit counts)
41 } 44 }
42 } 45 }
43 } 46 }
44 } 47 }
45 } 48 }
46 49
47 DnsHostsReader::DnsHostsReader(const FilePath& path, 50 DnsHostsReader::DnsHostsReader(const FilePath& path,
48 const CallbackType& callback) 51 const CallbackType& callback)
49 : path_(path), 52 : path_(path),
50 callback_(callback), 53 callback_(callback),
51 success_(false) { 54 success_(false) {
52 DCHECK(!callback.is_null()); 55 DCHECK(!callback.is_null());
53 } 56 }
54 57
58 DnsHostsReader::DnsHostsReader(const FilePath& path)
59 : path_(path),
60 success_(false) {
61 }
62
55 // Reads the contents of the file at |path| into |str| if the total length is 63 // Reads the contents of the file at |path| into |str| if the total length is
56 // less than |max_size|. 64 // less than |max_size|.
57 static bool ReadFile(const FilePath& path, int64 max_size, std::string* str) { 65 static bool ReadFile(const FilePath& path, int64 max_size, std::string* str) {
58 int64 size; 66 int64 size;
59 if (!file_util::GetFileSize(path, &size) || size > max_size) 67 if (!file_util::GetFileSize(path, &size) || size > max_size)
60 return false; 68 return false;
61 return file_util::ReadFileToString(path, str); 69 return file_util::ReadFileToString(path, str);
62 } 70 }
63 71
64 void DnsHostsReader::DoWork() { 72 void DnsHostsReader::DoWork() {
(...skipping 17 matching lines...) Expand all
82 void DnsHostsReader::OnWorkFinished() { 90 void DnsHostsReader::OnWorkFinished() {
83 DCHECK(!IsCancelled()); 91 DCHECK(!IsCancelled());
84 if (success_) 92 if (success_)
85 callback_.Run(dns_hosts_); 93 callback_.Run(dns_hosts_);
86 } 94 }
87 95
88 DnsHostsReader::~DnsHostsReader() {} 96 DnsHostsReader::~DnsHostsReader() {}
89 97
90 } // namespace net 98 } // namespace net
91 99
OLDNEW
« no previous file with comments | « net/dns/dns_hosts.h ('k') | net/dns/dns_hosts_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698