• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "net/proxy/proxy_list.h"
6 
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "base/rand_util.h"
10 #include "base/strings/string_tokenizer.h"
11 #include "base/time/time.h"
12 #include "base/values.h"
13 #include "net/proxy/proxy_server.h"
14 
15 using base::TimeDelta;
16 using base::TimeTicks;
17 
18 namespace net {
19 
ProxyList()20 ProxyList::ProxyList() {
21 }
22 
~ProxyList()23 ProxyList::~ProxyList() {
24 }
25 
Set(const std::string & proxy_uri_list)26 void ProxyList::Set(const std::string& proxy_uri_list) {
27   proxies_.clear();
28   base::StringTokenizer str_tok(proxy_uri_list, ";");
29   while (str_tok.GetNext()) {
30     ProxyServer uri = ProxyServer::FromURI(
31         str_tok.token_begin(), str_tok.token_end(), ProxyServer::SCHEME_HTTP);
32     // Silently discard malformed inputs.
33     if (uri.is_valid())
34       proxies_.push_back(uri);
35   }
36 }
37 
SetSingleProxyServer(const ProxyServer & proxy_server)38 void ProxyList::SetSingleProxyServer(const ProxyServer& proxy_server) {
39   proxies_.clear();
40   AddProxyServer(proxy_server);
41 }
42 
AddProxyServer(const ProxyServer & proxy_server)43 void ProxyList::AddProxyServer(const ProxyServer& proxy_server) {
44   if (proxy_server.is_valid())
45     proxies_.push_back(proxy_server);
46 }
47 
DeprioritizeBadProxies(const ProxyRetryInfoMap & proxy_retry_info)48 void ProxyList::DeprioritizeBadProxies(
49     const ProxyRetryInfoMap& proxy_retry_info) {
50   // Partition the proxy list in two:
51   //   (1) the known bad proxies
52   //   (2) everything else
53   std::vector<ProxyServer> good_proxies;
54   std::vector<ProxyServer> bad_proxies;
55 
56   std::vector<ProxyServer>::const_iterator iter = proxies_.begin();
57   for (; iter != proxies_.end(); ++iter) {
58     ProxyRetryInfoMap::const_iterator bad_proxy =
59         proxy_retry_info.find(iter->ToURI());
60     if (bad_proxy != proxy_retry_info.end()) {
61       // This proxy is bad. Check if it's time to retry.
62       if (bad_proxy->second.bad_until >= TimeTicks::Now()) {
63         // still invalid.
64         bad_proxies.push_back(*iter);
65         continue;
66       }
67     }
68     good_proxies.push_back(*iter);
69   }
70 
71   // "proxies_ = good_proxies + bad_proxies"
72   proxies_.swap(good_proxies);
73   proxies_.insert(proxies_.end(), bad_proxies.begin(), bad_proxies.end());
74 }
75 
HasUntriedProxies(const ProxyRetryInfoMap & proxy_retry_info) const76 bool ProxyList::HasUntriedProxies(
77     const ProxyRetryInfoMap& proxy_retry_info) const {
78   std::vector<ProxyServer>::const_iterator iter = proxies_.begin();
79   for (; iter != proxies_.end(); ++iter) {
80     ProxyRetryInfoMap::const_iterator bad_proxy =
81         proxy_retry_info.find(iter->ToURI());
82     if (bad_proxy != proxy_retry_info.end()) {
83       // This proxy is bad. Check if it's time to retry.
84       if (bad_proxy->second.bad_until >= TimeTicks::Now()) {
85         continue;
86       }
87     }
88     // Either we've found the entry in the retry map and it's expired or we
89     // didn't find a corresponding entry in the retry map. In either case, we
90     // have a proxy to try.
91     return true;
92   }
93   return false;
94 }
95 
RemoveProxiesWithoutScheme(int scheme_bit_field)96 void ProxyList::RemoveProxiesWithoutScheme(int scheme_bit_field) {
97   for (std::vector<ProxyServer>::iterator it = proxies_.begin();
98        it != proxies_.end(); ) {
99     if (!(scheme_bit_field & it->scheme())) {
100       it = proxies_.erase(it);
101       continue;
102     }
103     ++it;
104   }
105 }
106 
Clear()107 void ProxyList::Clear() {
108   proxies_.clear();
109 }
110 
IsEmpty() const111 bool ProxyList::IsEmpty() const {
112   return proxies_.empty();
113 }
114 
size() const115 size_t ProxyList::size() const {
116   return proxies_.size();
117 }
118 
119 // Returns true if |*this| lists the same proxies as |other|.
Equals(const ProxyList & other) const120 bool ProxyList::Equals(const ProxyList& other) const {
121   if (size() != other.size())
122     return false;
123   return proxies_ == other.proxies_;
124 }
125 
Get() const126 const ProxyServer& ProxyList::Get() const {
127   DCHECK(!proxies_.empty());
128   return proxies_[0];
129 }
130 
SetFromPacString(const std::string & pac_string)131 void ProxyList::SetFromPacString(const std::string& pac_string) {
132   base::StringTokenizer entry_tok(pac_string, ";");
133   proxies_.clear();
134   while (entry_tok.GetNext()) {
135     ProxyServer uri = ProxyServer::FromPacString(
136         entry_tok.token_begin(), entry_tok.token_end());
137     // Silently discard malformed inputs.
138     if (uri.is_valid())
139       proxies_.push_back(uri);
140   }
141 
142   // If we failed to parse anything from the PAC results list, fallback to
143   // DIRECT (this basically means an error in the PAC script).
144   if (proxies_.empty()) {
145     proxies_.push_back(ProxyServer::Direct());
146   }
147 }
148 
ToPacString() const149 std::string ProxyList::ToPacString() const {
150   std::string proxy_list;
151   std::vector<ProxyServer>::const_iterator iter = proxies_.begin();
152   for (; iter != proxies_.end(); ++iter) {
153     if (!proxy_list.empty())
154       proxy_list += ";";
155     proxy_list += iter->ToPacString();
156   }
157   return proxy_list.empty() ? std::string() : proxy_list;
158 }
159 
ToValue() const160 base::ListValue* ProxyList::ToValue() const {
161   base::ListValue* list = new base::ListValue();
162   for (size_t i = 0; i < proxies_.size(); ++i)
163     list->AppendString(proxies_[i].ToURI());
164   return list;
165 }
166 
Fallback(ProxyRetryInfoMap * proxy_retry_info,const BoundNetLog & net_log)167 bool ProxyList::Fallback(ProxyRetryInfoMap* proxy_retry_info,
168                          const BoundNetLog& net_log) {
169 
170   // TODO(eroman): It would be good if instead of removing failed proxies
171   // from the list, we simply annotated them with the error code they failed
172   // with. Of course, ProxyService::ReconsiderProxyAfterError() would need to
173   // be given this information by the network transaction.
174   //
175   // The advantage of this approach is when the network transaction
176   // fails, we could output the full list of proxies that were attempted, and
177   // why each one of those failed (as opposed to just the last failure).
178   //
179   // And also, before failing the transaction wholesale, we could go back and
180   // retry the "bad proxies" which we never tried to begin with.
181   // (RemoveBadProxies would annotate them as 'expected bad' rather then delete
182   // them from the list, so we would know what they were).
183 
184   if (proxies_.empty()) {
185     NOTREACHED();
186     return false;
187   }
188   UpdateRetryInfoOnFallback(proxy_retry_info, base::TimeDelta(), ProxyServer(),
189                             net_log);
190 
191   // Remove this proxy from our list.
192   proxies_.erase(proxies_.begin());
193   return !proxies_.empty();
194 }
195 
AddProxyToRetryList(ProxyRetryInfoMap * proxy_retry_info,base::TimeDelta retry_delay,const std::string & proxy_key,const BoundNetLog & net_log) const196 void ProxyList::AddProxyToRetryList(ProxyRetryInfoMap* proxy_retry_info,
197                                     base::TimeDelta retry_delay,
198                                     const std::string& proxy_key,
199                                     const BoundNetLog& net_log) const {
200   // Mark this proxy as bad.
201   ProxyRetryInfoMap::iterator iter = proxy_retry_info->find(proxy_key);
202   if (iter != proxy_retry_info->end()) {
203     // TODO(nsylvain): This is not the first time we get this. We should
204     // double the retry time. Bug 997660.
205     iter->second.bad_until = TimeTicks::Now() + iter->second.current_delay;
206   } else {
207     ProxyRetryInfo retry_info;
208     retry_info.current_delay = retry_delay;
209     retry_info.bad_until = TimeTicks().Now() + retry_info.current_delay;
210     (*proxy_retry_info)[proxy_key] = retry_info;
211   }
212   net_log.AddEvent(NetLog::TYPE_PROXY_LIST_FALLBACK,
213                    NetLog::StringCallback("bad_proxy", &proxy_key));
214 }
215 
UpdateRetryInfoOnFallback(ProxyRetryInfoMap * proxy_retry_info,base::TimeDelta retry_delay,const ProxyServer & another_proxy_to_bypass,const BoundNetLog & net_log) const216 void ProxyList::UpdateRetryInfoOnFallback(
217     ProxyRetryInfoMap* proxy_retry_info,
218     base::TimeDelta retry_delay,
219     const ProxyServer& another_proxy_to_bypass,
220     const BoundNetLog& net_log) const {
221   // Time to wait before retrying a bad proxy server.
222   if (retry_delay == base::TimeDelta()) {
223 #if defined(SPDY_PROXY_AUTH_ORIGIN)
224     // Randomize the timeout over a range from one to five minutes.
225     retry_delay =
226         TimeDelta::FromMilliseconds(
227             base::RandInt(1 * 60 * 1000, 5 * 60 * 1000));
228 #else
229     retry_delay = TimeDelta::FromMinutes(5);
230 #endif
231   }
232 
233   if (proxies_.empty()) {
234     NOTREACHED();
235     return;
236   }
237 
238   if (!proxies_[0].is_direct()) {
239     std::string key = proxies_[0].ToURI();
240     AddProxyToRetryList(proxy_retry_info, retry_delay, key, net_log);
241 
242     // If additional proxies to bypass are specified, add these to the retry
243     // map as well.
244     if (another_proxy_to_bypass.is_valid()) {
245       // Start at index 1 because index 0 is already handled above.
246       for (size_t j = 1; j < proxies_.size(); ++j) {
247         if (proxies_[j].is_direct())
248           break;
249         if (another_proxy_to_bypass == proxies_[j]) {
250           key = proxies_[j].ToURI();
251           AddProxyToRetryList(proxy_retry_info, retry_delay, key, net_log);
252         }
253       }
254     }
255   }
256 }
257 
258 }  // namespace net
259