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/strings/string_tokenizer.h"
10 #include "base/time/time.h"
11 #include "base/values.h"
12 #include "net/proxy/proxy_server.h"
13
14 using base::TimeDelta;
15 using base::TimeTicks;
16
17 namespace net {
18
ProxyList()19 ProxyList::ProxyList() {
20 }
21
~ProxyList()22 ProxyList::~ProxyList() {
23 }
24
Set(const std::string & proxy_uri_list)25 void ProxyList::Set(const std::string& proxy_uri_list) {
26 proxies_.clear();
27 base::StringTokenizer str_tok(proxy_uri_list, ";");
28 while (str_tok.GetNext()) {
29 ProxyServer uri = ProxyServer::FromURI(
30 str_tok.token_begin(), str_tok.token_end(), ProxyServer::SCHEME_HTTP);
31 // Silently discard malformed inputs.
32 if (uri.is_valid())
33 proxies_.push_back(uri);
34 }
35 }
36
SetSingleProxyServer(const ProxyServer & proxy_server)37 void ProxyList::SetSingleProxyServer(const ProxyServer& proxy_server) {
38 proxies_.clear();
39 AddProxyServer(proxy_server);
40 }
41
AddProxyServer(const ProxyServer & proxy_server)42 void ProxyList::AddProxyServer(const ProxyServer& proxy_server) {
43 if (proxy_server.is_valid())
44 proxies_.push_back(proxy_server);
45 }
46
DeprioritizeBadProxies(const ProxyRetryInfoMap & proxy_retry_info)47 void ProxyList::DeprioritizeBadProxies(
48 const ProxyRetryInfoMap& proxy_retry_info) {
49 // Partition the proxy list in two:
50 // (1) the known bad proxies
51 // (2) everything else
52 std::vector<ProxyServer> good_proxies;
53 std::vector<ProxyServer> bad_proxies_to_try;
54
55 std::vector<ProxyServer>::const_iterator iter = proxies_.begin();
56 for (; iter != proxies_.end(); ++iter) {
57 ProxyRetryInfoMap::const_iterator bad_proxy =
58 proxy_retry_info.find(iter->ToURI());
59 if (bad_proxy != proxy_retry_info.end()) {
60 // This proxy is bad. Check if it's time to retry.
61 if (bad_proxy->second.bad_until >= TimeTicks::Now()) {
62 // still invalid.
63 if (bad_proxy->second.try_while_bad)
64 bad_proxies_to_try.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_to_try.begin(),
74 bad_proxies_to_try.end());
75 }
76
RemoveProxiesWithoutScheme(int scheme_bit_field)77 void ProxyList::RemoveProxiesWithoutScheme(int scheme_bit_field) {
78 for (std::vector<ProxyServer>::iterator it = proxies_.begin();
79 it != proxies_.end(); ) {
80 if (!(scheme_bit_field & it->scheme())) {
81 it = proxies_.erase(it);
82 continue;
83 }
84 ++it;
85 }
86 }
87
Clear()88 void ProxyList::Clear() {
89 proxies_.clear();
90 }
91
IsEmpty() const92 bool ProxyList::IsEmpty() const {
93 return proxies_.empty();
94 }
95
size() const96 size_t ProxyList::size() const {
97 return proxies_.size();
98 }
99
100 // Returns true if |*this| lists the same proxies as |other|.
Equals(const ProxyList & other) const101 bool ProxyList::Equals(const ProxyList& other) const {
102 if (size() != other.size())
103 return false;
104 return proxies_ == other.proxies_;
105 }
106
Get() const107 const ProxyServer& ProxyList::Get() const {
108 DCHECK(!proxies_.empty());
109 return proxies_[0];
110 }
111
SetFromPacString(const std::string & pac_string)112 void ProxyList::SetFromPacString(const std::string& pac_string) {
113 base::StringTokenizer entry_tok(pac_string, ";");
114 proxies_.clear();
115 while (entry_tok.GetNext()) {
116 ProxyServer uri = ProxyServer::FromPacString(
117 entry_tok.token_begin(), entry_tok.token_end());
118 // Silently discard malformed inputs.
119 if (uri.is_valid())
120 proxies_.push_back(uri);
121 }
122
123 // If we failed to parse anything from the PAC results list, fallback to
124 // DIRECT (this basically means an error in the PAC script).
125 if (proxies_.empty()) {
126 proxies_.push_back(ProxyServer::Direct());
127 }
128 }
129
ToPacString() const130 std::string ProxyList::ToPacString() const {
131 std::string proxy_list;
132 std::vector<ProxyServer>::const_iterator iter = proxies_.begin();
133 for (; iter != proxies_.end(); ++iter) {
134 if (!proxy_list.empty())
135 proxy_list += ";";
136 proxy_list += iter->ToPacString();
137 }
138 return proxy_list.empty() ? std::string() : proxy_list;
139 }
140
ToValue() const141 base::ListValue* ProxyList::ToValue() const {
142 base::ListValue* list = new base::ListValue();
143 for (size_t i = 0; i < proxies_.size(); ++i)
144 list->AppendString(proxies_[i].ToURI());
145 return list;
146 }
147
Fallback(ProxyRetryInfoMap * proxy_retry_info,int net_error,const BoundNetLog & net_log)148 bool ProxyList::Fallback(ProxyRetryInfoMap* proxy_retry_info,
149 int net_error,
150 const BoundNetLog& net_log) {
151
152 // TODO(eroman): It would be good if instead of removing failed proxies
153 // from the list, we simply annotated them with the error code they failed
154 // with. Of course, ProxyService::ReconsiderProxyAfterError() would need to
155 // be given this information by the network transaction.
156 //
157 // The advantage of this approach is when the network transaction
158 // fails, we could output the full list of proxies that were attempted, and
159 // why each one of those failed (as opposed to just the last failure).
160 //
161 // And also, before failing the transaction wholesale, we could go back and
162 // retry the "bad proxies" which we never tried to begin with.
163 // (RemoveBadProxies would annotate them as 'expected bad' rather then delete
164 // them from the list, so we would know what they were).
165
166 if (proxies_.empty()) {
167 NOTREACHED();
168 return false;
169 }
170 // By default, proxies are not retried for 5 minutes.
171 UpdateRetryInfoOnFallback(proxy_retry_info,
172 TimeDelta::FromMinutes(5),
173 true,
174 ProxyServer(),
175 net_error,
176 net_log);
177
178 // Remove this proxy from our list.
179 proxies_.erase(proxies_.begin());
180 return !proxies_.empty();
181 }
182
AddProxyToRetryList(ProxyRetryInfoMap * proxy_retry_info,base::TimeDelta retry_delay,bool try_while_bad,const ProxyServer & proxy_to_retry,int net_error,const BoundNetLog & net_log) const183 void ProxyList::AddProxyToRetryList(ProxyRetryInfoMap* proxy_retry_info,
184 base::TimeDelta retry_delay,
185 bool try_while_bad,
186 const ProxyServer& proxy_to_retry,
187 int net_error,
188 const BoundNetLog& net_log) const {
189 // Mark this proxy as bad.
190 std::string proxy_key = proxy_to_retry.ToURI();
191 ProxyRetryInfoMap::iterator iter = proxy_retry_info->find(proxy_key);
192 if (iter != proxy_retry_info->end()) {
193 // TODO(nsylvain): This is not the first time we get this. We should
194 // double the retry time. Bug 997660.
195 iter->second.bad_until = TimeTicks::Now() + iter->second.current_delay;
196 } else {
197 ProxyRetryInfo retry_info;
198 retry_info.current_delay = retry_delay;
199 retry_info.bad_until = TimeTicks().Now() + retry_info.current_delay;
200 retry_info.try_while_bad = try_while_bad;
201 retry_info.net_error = net_error;
202 (*proxy_retry_info)[proxy_key] = retry_info;
203 }
204 net_log.AddEvent(NetLog::TYPE_PROXY_LIST_FALLBACK,
205 NetLog::StringCallback("bad_proxy", &proxy_key));
206 }
207
UpdateRetryInfoOnFallback(ProxyRetryInfoMap * proxy_retry_info,base::TimeDelta retry_delay,bool reconsider,const ProxyServer & another_proxy_to_bypass,int net_error,const BoundNetLog & net_log) const208 void ProxyList::UpdateRetryInfoOnFallback(
209 ProxyRetryInfoMap* proxy_retry_info,
210 base::TimeDelta retry_delay,
211 bool reconsider,
212 const ProxyServer& another_proxy_to_bypass,
213 int net_error,
214 const BoundNetLog& net_log) const {
215 DCHECK(retry_delay != base::TimeDelta());
216
217 if (proxies_.empty()) {
218 NOTREACHED();
219 return;
220 }
221
222 if (!proxies_[0].is_direct()) {
223 AddProxyToRetryList(proxy_retry_info,
224 retry_delay,
225 reconsider,
226 proxies_[0],
227 net_error,
228 net_log);
229
230 // If an additional proxy to bypass is specified, add it to the retry map
231 // as well.
232 if (another_proxy_to_bypass.is_valid()) {
233 AddProxyToRetryList(proxy_retry_info,
234 retry_delay,
235 reconsider,
236 another_proxy_to_bypass,
237 net_error,
238 net_log);
239 }
240 }
241 }
242
243 } // namespace net
244