OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 | 103 |
104 struct RuleBasedHostResolverProc::Rule { | 104 struct RuleBasedHostResolverProc::Rule { |
105 enum ResolverType { | 105 enum ResolverType { |
106 kResolverTypeFail, | 106 kResolverTypeFail, |
107 kResolverTypeSystem, | 107 kResolverTypeSystem, |
108 kResolverTypeIPV6Literal, | 108 kResolverTypeIPV6Literal, |
109 }; | 109 }; |
110 | 110 |
111 ResolverType resolver_type; | 111 ResolverType resolver_type; |
112 std::string host_pattern; | 112 std::string host_pattern; |
113 AddressFamily address_family; | |
113 std::string replacement; | 114 std::string replacement; |
114 int latency_ms; // In milliseconds. | 115 int latency_ms; // In milliseconds. |
115 | 116 |
116 Rule(ResolverType resolver_type, | 117 Rule(ResolverType resolver_type, |
117 const std::string& host_pattern, | 118 const std::string& host_pattern, |
119 AddressFamily address_family, | |
118 const std::string& replacement, | 120 const std::string& replacement, |
119 int latency_ms) | 121 int latency_ms) |
120 : resolver_type(resolver_type), | 122 : resolver_type(resolver_type), |
121 host_pattern(host_pattern), | 123 host_pattern(host_pattern), |
124 address_family(address_family), | |
122 replacement(replacement), | 125 replacement(replacement), |
123 latency_ms(latency_ms) {} | 126 latency_ms(latency_ms) {} |
124 }; | 127 }; |
125 | 128 |
126 RuleBasedHostResolverProc::RuleBasedHostResolverProc(HostResolverProc* previous) | 129 RuleBasedHostResolverProc::RuleBasedHostResolverProc(HostResolverProc* previous) |
127 : HostResolverProc(previous) { | 130 : HostResolverProc(previous) { |
128 } | 131 } |
129 | 132 |
130 RuleBasedHostResolverProc::~RuleBasedHostResolverProc() { | 133 RuleBasedHostResolverProc::~RuleBasedHostResolverProc() { |
131 } | 134 } |
132 | 135 |
133 void RuleBasedHostResolverProc::AddRule(const std::string& host_pattern, | 136 void RuleBasedHostResolverProc::AddRule(const std::string& host_pattern, |
134 const std::string& replacement) { | 137 const std::string& replacement) { |
138 AddRuleForFamily(host_pattern, ADDRESS_FAMILY_UNSPECIFIED, replacement); | |
139 } | |
140 | |
141 void RuleBasedHostResolverProc::AddRuleForFamily( | |
142 const std::string& host_pattern, | |
143 AddressFamily address_family, | |
144 const std::string& replacement) { | |
135 DCHECK(!replacement.empty()); | 145 DCHECK(!replacement.empty()); |
136 Rule rule(Rule::kResolverTypeSystem, host_pattern, replacement, 0); | 146 Rule rule(Rule::kResolverTypeSystem, host_pattern, |
147 address_family, replacement, 0); | |
137 rules_.push_back(rule); | 148 rules_.push_back(rule); |
138 } | 149 } |
139 | 150 |
151 | |
wtc
2009/10/22 21:53:54
Nit: delete one blank line here.
| |
140 void RuleBasedHostResolverProc::AddIPv6Rule(const std::string& host_pattern, | 152 void RuleBasedHostResolverProc::AddIPv6Rule(const std::string& host_pattern, |
141 const std::string& ipv6_literal) { | 153 const std::string& ipv6_literal) { |
142 Rule rule(Rule::kResolverTypeIPV6Literal, host_pattern, ipv6_literal, 0); | 154 Rule rule(Rule::kResolverTypeIPV6Literal, host_pattern, |
155 ADDRESS_FAMILY_UNSPECIFIED, ipv6_literal, 0); | |
wtc
2009/10/22 21:53:54
Why do you pass ADDRESS_FAMILY_UNSPECIFIED here?
I
| |
143 rules_.push_back(rule); | 156 rules_.push_back(rule); |
144 } | 157 } |
145 | 158 |
146 void RuleBasedHostResolverProc::AddRuleWithLatency( | 159 void RuleBasedHostResolverProc::AddRuleWithLatency( |
147 const std::string& host_pattern, | 160 const std::string& host_pattern, |
148 const std::string& replacement, | 161 const std::string& replacement, |
149 int latency_ms) { | 162 int latency_ms) { |
150 DCHECK(!replacement.empty()); | 163 DCHECK(!replacement.empty()); |
151 Rule rule(Rule::kResolverTypeSystem, host_pattern, replacement, latency_ms); | 164 Rule rule(Rule::kResolverTypeSystem, host_pattern, |
165 ADDRESS_FAMILY_UNSPECIFIED, replacement, latency_ms); | |
152 rules_.push_back(rule); | 166 rules_.push_back(rule); |
153 } | 167 } |
154 | 168 |
155 void RuleBasedHostResolverProc::AllowDirectLookup( | 169 void RuleBasedHostResolverProc::AllowDirectLookup( |
156 const std::string& host_pattern) { | 170 const std::string& host_pattern) { |
157 Rule rule(Rule::kResolverTypeSystem, host_pattern, "", 0); | 171 Rule rule(Rule::kResolverTypeSystem, host_pattern, |
172 ADDRESS_FAMILY_UNSPECIFIED, "", 0); | |
158 rules_.push_back(rule); | 173 rules_.push_back(rule); |
159 } | 174 } |
160 | 175 |
161 void RuleBasedHostResolverProc::AddSimulatedFailure( | 176 void RuleBasedHostResolverProc::AddSimulatedFailure( |
162 const std::string& host_pattern) { | 177 const std::string& host_pattern) { |
163 Rule rule(Rule::kResolverTypeFail, host_pattern, "", 0); | 178 Rule rule(Rule::kResolverTypeFail, host_pattern, |
179 ADDRESS_FAMILY_UNSPECIFIED, "", 0); | |
164 rules_.push_back(rule); | 180 rules_.push_back(rule); |
165 } | 181 } |
166 | 182 |
167 int RuleBasedHostResolverProc::Resolve(const std::string& host, | 183 int RuleBasedHostResolverProc::Resolve(const std::string& host, |
168 AddressFamily address_family, | 184 AddressFamily address_family, |
169 AddressList* addrlist) { | 185 AddressList* addrlist) { |
170 RuleList::iterator r; | 186 RuleList::iterator r; |
171 for (r = rules_.begin(); r != rules_.end(); ++r) { | 187 for (r = rules_.begin(); r != rules_.end(); ++r) { |
172 if (MatchPattern(host, r->host_pattern)) { | 188 bool matches_address_family = |
189 r->address_family == ADDRESS_FAMILY_UNSPECIFIED || | |
190 r->address_family == address_family; | |
191 | |
192 if (matches_address_family && MatchPattern(host, r->host_pattern)) { | |
173 if (r->latency_ms != 0) | 193 if (r->latency_ms != 0) |
174 PlatformThread::Sleep(r->latency_ms); | 194 PlatformThread::Sleep(r->latency_ms); |
175 | 195 |
176 // Remap to a new host. | 196 // Remap to a new host. |
177 const std::string& effective_host = | 197 const std::string& effective_host = |
178 r->replacement.empty() ? host : r->replacement; | 198 r->replacement.empty() ? host : r->replacement; |
179 | 199 |
180 // Apply the resolving function to the remapped hostname. | 200 // Apply the resolving function to the remapped hostname. |
181 switch (r->resolver_type) { | 201 switch (r->resolver_type) { |
182 case Rule::kResolverTypeFail: | 202 case Rule::kResolverTypeFail: |
(...skipping 27 matching lines...) Expand all Loading... | |
210 CHECK(old_proc == current_proc_); | 230 CHECK(old_proc == current_proc_); |
211 } | 231 } |
212 | 232 |
213 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { | 233 void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { |
214 current_proc_ = proc; | 234 current_proc_ = proc; |
215 previous_proc_ = HostResolverProc::SetDefault(current_proc_); | 235 previous_proc_ = HostResolverProc::SetDefault(current_proc_); |
216 current_proc_->set_previous_proc(previous_proc_); | 236 current_proc_->set_previous_proc(previous_proc_); |
217 } | 237 } |
218 | 238 |
219 } // namespace net | 239 } // namespace net |
OLD | NEW |