OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/base/mock_host_resolver.h" | 5 #include "net/base/mock_host_resolver.h" |
6 | 6 |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/platform_thread.h" | 8 #include "base/platform_thread.h" |
9 #include "base/ref_counted.h" | 9 #include "base/ref_counted.h" |
10 #include "googleurl/src/url_canon_ip.h" | 10 #include "googleurl/src/url_canon_ip.h" |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 AddressFamily address_family, | 142 AddressFamily address_family, |
143 const std::string& replacement) { | 143 const std::string& replacement) { |
144 DCHECK(!replacement.empty()); | 144 DCHECK(!replacement.empty()); |
145 Rule rule(Rule::kResolverTypeSystem, host_pattern, | 145 Rule rule(Rule::kResolverTypeSystem, host_pattern, |
146 address_family, replacement, 0); | 146 address_family, replacement, 0); |
147 rules_.push_back(rule); | 147 rules_.push_back(rule); |
148 } | 148 } |
149 | 149 |
150 void RuleBasedHostResolverProc::AddIPv6Rule(const std::string& host_pattern, | 150 void RuleBasedHostResolverProc::AddIPv6Rule(const std::string& host_pattern, |
151 const std::string& ipv6_literal) { | 151 const std::string& ipv6_literal) { |
152 Rule rule(Rule::kResolverTypeIPV6Literal, | 152 Rule rule(Rule::kResolverTypeIPV6Literal, host_pattern, |
153 host_pattern, | 153 ADDRESS_FAMILY_UNSPECIFIED, ipv6_literal, 0); |
154 ADDRESS_FAMILY_UNSPECIFIED, | |
155 ipv6_literal, | |
156 0); | |
157 rules_.push_back(rule); | 154 rules_.push_back(rule); |
158 } | 155 } |
159 | 156 |
160 void RuleBasedHostResolverProc::AddRuleWithLatency( | 157 void RuleBasedHostResolverProc::AddRuleWithLatency( |
161 const std::string& host_pattern, | 158 const std::string& host_pattern, |
162 const std::string& replacement, | 159 const std::string& replacement, |
163 int latency_ms) { | 160 int latency_ms) { |
164 DCHECK(!replacement.empty()); | 161 DCHECK(!replacement.empty()); |
165 Rule rule(Rule::kResolverTypeSystem, host_pattern, | 162 Rule rule(Rule::kResolverTypeSystem, host_pattern, |
166 ADDRESS_FAMILY_UNSPECIFIED, replacement, latency_ms); | 163 ADDRESS_FAMILY_UNSPECIFIED, replacement, latency_ms); |
167 rules_.push_back(rule); | 164 rules_.push_back(rule); |
168 } | 165 } |
169 | 166 |
170 void RuleBasedHostResolverProc::AllowDirectLookup( | 167 void RuleBasedHostResolverProc::AllowDirectLookup( |
171 const std::string& host_pattern) { | 168 const std::string& host_pattern) { |
172 Rule rule(Rule::kResolverTypeSystem, host_pattern, | 169 Rule rule(Rule::kResolverTypeSystem, host_pattern, |
173 ADDRESS_FAMILY_UNSPECIFIED, "", 0); | 170 ADDRESS_FAMILY_UNSPECIFIED, "", 0); |
174 rules_.push_back(rule); | 171 rules_.push_back(rule); |
175 } | 172 } |
176 | 173 |
177 void RuleBasedHostResolverProc::AddSimulatedFailure( | 174 void RuleBasedHostResolverProc::AddSimulatedFailure( |
178 const std::string& host_pattern) { | 175 const std::string& host_pattern) { |
179 Rule rule(Rule::kResolverTypeFail, host_pattern, | 176 Rule rule(Rule::kResolverTypeFail, host_pattern, |
180 ADDRESS_FAMILY_UNSPECIFIED, "", 0); | 177 ADDRESS_FAMILY_UNSPECIFIED, "", 0); |
181 rules_.push_back(rule); | 178 rules_.push_back(rule); |
182 } | 179 } |
183 | 180 |
184 int RuleBasedHostResolverProc::Resolve(const std::string& host, | 181 int RuleBasedHostResolverProc::Resolve(const std::string& host, |
185 AddressFamily address_family, | 182 AddressFamily address_family, |
186 HostResolverFlags host_resolver_flags, | |
187 AddressList* addrlist) { | 183 AddressList* addrlist) { |
188 RuleList::iterator r; | 184 RuleList::iterator r; |
189 for (r = rules_.begin(); r != rules_.end(); ++r) { | 185 for (r = rules_.begin(); r != rules_.end(); ++r) { |
190 bool matches_address_family = | 186 bool matches_address_family = |
191 r->address_family == ADDRESS_FAMILY_UNSPECIFIED || | 187 r->address_family == ADDRESS_FAMILY_UNSPECIFIED || |
192 r->address_family == address_family; | 188 r->address_family == address_family; |
193 | 189 |
194 if (matches_address_family && MatchPatternASCII(host, r->host_pattern)) { | 190 if (matches_address_family && MatchPatternASCII(host, r->host_pattern)) { |
195 if (r->latency_ms != 0) | 191 if (r->latency_ms != 0) |
196 PlatformThread::Sleep(r->latency_ms); | 192 PlatformThread::Sleep(r->latency_ms); |
197 | 193 |
198 // Remap to a new host. | 194 // Remap to a new host. |
199 const std::string& effective_host = | 195 const std::string& effective_host = |
200 r->replacement.empty() ? host : r->replacement; | 196 r->replacement.empty() ? host : r->replacement; |
201 | 197 |
202 // Apply the resolving function to the remapped hostname. | 198 // Apply the resolving function to the remapped hostname. |
203 switch (r->resolver_type) { | 199 switch (r->resolver_type) { |
204 case Rule::kResolverTypeFail: | 200 case Rule::kResolverTypeFail: |
205 return ERR_NAME_NOT_RESOLVED; | 201 return ERR_NAME_NOT_RESOLVED; |
206 case Rule::kResolverTypeSystem: | 202 case Rule::kResolverTypeSystem: |
207 return SystemHostResolverProc(effective_host, | 203 return SystemHostResolverProc(effective_host, |
208 address_family, | 204 address_family, |
209 host_resolver_flags, | |
210 addrlist); | 205 addrlist); |
211 case Rule::kResolverTypeIPV6Literal: | 206 case Rule::kResolverTypeIPV6Literal: |
212 return ResolveIPV6LiteralUsingGURL(effective_host, addrlist); | 207 return ResolveIPV6LiteralUsingGURL(effective_host, addrlist); |
213 default: | 208 default: |
214 NOTREACHED(); | 209 NOTREACHED(); |
215 return ERR_UNEXPECTED; | 210 return ERR_UNEXPECTED; |
216 } | 211 } |
217 } | 212 } |
218 } | 213 } |
219 return ResolveUsingPrevious(host, address_family, | 214 return ResolveUsingPrevious(host, address_family, addrlist); |
220 host_resolver_flags, addrlist); | |
221 } | 215 } |
222 | 216 |
223 //----------------------------------------------------------------------------- | 217 //----------------------------------------------------------------------------- |
224 | 218 |
225 ScopedDefaultHostResolverProc::ScopedDefaultHostResolverProc( | 219 ScopedDefaultHostResolverProc::ScopedDefaultHostResolverProc( |
226 HostResolverProc* proc) { | 220 HostResolverProc* proc) { |
227 Init(proc); | 221 Init(proc); |
228 } | 222 } |
229 | 223 |
230 ScopedDefaultHostResolverProc::~ScopedDefaultHostResolverProc() { | 224 ScopedDefaultHostResolverProc::~ScopedDefaultHostResolverProc() { |
231 HostResolverProc* old_proc = HostResolverProc::SetDefault(previous_proc_); | 225 HostResolverProc* old_proc = HostResolverProc::SetDefault(previous_proc_); |
232 // The lifetimes of multiple instances must be nested. | 226 // The lifetimes of multiple instances must be nested. |
233 CHECK_EQ(old_proc, current_proc_); | 227 CHECK_EQ(old_proc, current_proc_); |
234 } | 228 } |
235 | 229 |
236 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { | 230 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { |
237 current_proc_ = proc; | 231 current_proc_ = proc; |
238 previous_proc_ = HostResolverProc::SetDefault(current_proc_); | 232 previous_proc_ = HostResolverProc::SetDefault(current_proc_); |
239 current_proc_->SetLastProc(previous_proc_); | 233 current_proc_->SetLastProc(previous_proc_); |
240 } | 234 } |
241 | 235 |
242 } // namespace net | 236 } // namespace net |
OLD | NEW |