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/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 } | 62 } |
63 } | 63 } |
64 | 64 |
65 text_ = StringPiece(); | 65 text_ = StringPiece(); |
66 return false; | 66 return false; |
67 } | 67 } |
68 | 68 |
69 // Fast-forwards the parser to the next line. Should be called if an IP | 69 // Fast-forwards the parser to the next line. Should be called if an IP |
70 // address doesn't parse, to avoid wasting time tokenizing hostnames that | 70 // address doesn't parse, to avoid wasting time tokenizing hostnames that |
71 // will be ignored. | 71 // will be ignored. |
72 void SkipRestOfLine() { | 72 void SkipRestOfLine() { pos_ = text_.find("\n", pos_); } |
73 pos_ = text_.find("\n", pos_); | |
74 } | |
75 | 73 |
76 // Returns whether the last-parsed token is an IP address (true) or a | 74 // Returns whether the last-parsed token is an IP address (true) or a |
77 // hostname (false). | 75 // hostname (false). |
78 bool token_is_ip() { return token_is_ip_; } | 76 bool token_is_ip() { return token_is_ip_; } |
79 | 77 |
80 // Returns the text of the last-parsed token as a StringPiece referencing | 78 // Returns the text of the last-parsed token as a StringPiece referencing |
81 // the same underlying memory as the StringPiece passed to the constructor. | 79 // the same underlying memory as the StringPiece passed to the constructor. |
82 // Returns an empty StringPiece if no token has been parsed or the end of | 80 // Returns an empty StringPiece if no token has been parsed or the end of |
83 // the input string has been reached. | 81 // the input string has been reached. |
84 const StringPiece& token() { return token_; } | 82 const StringPiece& token() { return token_; } |
85 | 83 |
86 private: | 84 private: |
87 void SkipToken() { | 85 void SkipToken() { pos_ = text_.find_first_of(" \t\n\r#", pos_); } |
88 pos_ = text_.find_first_of(" \t\n\r#", pos_); | |
89 } | |
90 | 86 |
91 void SkipWhitespace() { | 87 void SkipWhitespace() { pos_ = text_.find_first_not_of(" \t", pos_); } |
92 pos_ = text_.find_first_not_of(" \t", pos_); | |
93 } | |
94 | 88 |
95 StringPiece text_; | 89 StringPiece text_; |
96 const char* data_; | 90 const char* data_; |
97 const size_t end_; | 91 const size_t end_; |
98 | 92 |
99 size_t pos_; | 93 size_t pos_; |
100 StringPiece token_; | 94 StringPiece token_; |
101 bool token_is_ip_; | 95 bool token_is_ip_; |
102 | 96 |
103 DISALLOW_COPY_AND_ASSIGN(HostsParser); | 97 DISALLOW_COPY_AND_ASSIGN(HostsParser); |
104 }; | 98 }; |
105 | 99 |
106 | |
107 | |
108 void ParseHosts(const std::string& contents, DnsHosts* dns_hosts) { | 100 void ParseHosts(const std::string& contents, DnsHosts* dns_hosts) { |
109 CHECK(dns_hosts); | 101 CHECK(dns_hosts); |
110 DnsHosts& hosts = *dns_hosts; | 102 DnsHosts& hosts = *dns_hosts; |
111 | 103 |
112 StringPiece ip_text; | 104 StringPiece ip_text; |
113 IPAddressNumber ip; | 105 IPAddressNumber ip; |
114 AddressFamily family = ADDRESS_FAMILY_IPV4; | 106 AddressFamily family = ADDRESS_FAMILY_IPV4; |
115 HostsParser parser(contents); | 107 HostsParser parser(contents); |
116 while (parser.Advance()) { | 108 while (parser.Advance()) { |
117 if (parser.token_is_ip()) { | 109 if (parser.token_is_ip()) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 | 151 |
160 std::string contents; | 152 std::string contents; |
161 if (!base::ReadFileToString(path, &contents)) | 153 if (!base::ReadFileToString(path, &contents)) |
162 return false; | 154 return false; |
163 | 155 |
164 ParseHosts(contents, dns_hosts); | 156 ParseHosts(contents, dns_hosts); |
165 return true; | 157 return true; |
166 } | 158 } |
167 | 159 |
168 } // namespace net | 160 } // namespace net |
169 | |
OLD | NEW |