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