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.
5 #include "net/proxy/proxy_list.h"
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"
14 using base::TimeDelta
;
15 using base::TimeTicks
;
19 ProxyList::ProxyList() {
22 ProxyList::~ProxyList() {
25 void ProxyList::Set(const std::string
& proxy_uri_list
) {
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.
33 proxies_
.push_back(uri
);
37 void ProxyList::SetSingleProxyServer(const ProxyServer
& proxy_server
) {
39 AddProxyServer(proxy_server
);
42 void ProxyList::AddProxyServer(const ProxyServer
& proxy_server
) {
43 if (proxy_server
.is_valid())
44 proxies_
.push_back(proxy_server
);
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
;
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()) {
63 if (bad_proxy
->second
.try_while_bad
)
64 bad_proxies_to_try
.push_back(*iter
);
68 good_proxies
.push_back(*iter
);
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());
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
);
88 void ProxyList::Clear() {
92 bool ProxyList::IsEmpty() const {
93 return proxies_
.empty();
96 size_t ProxyList::size() const {
97 return proxies_
.size();
100 // Returns true if |*this| lists the same proxies as |other|.
101 bool ProxyList::Equals(const ProxyList
& other
) const {
102 if (size() != other
.size())
104 return proxies_
== other
.proxies_
;
107 const ProxyServer
& ProxyList::Get() const {
108 DCHECK(!proxies_
.empty());
112 const std::vector
<ProxyServer
>& ProxyList::GetAll() const {
116 void ProxyList::SetFromPacString(const std::string
& pac_string
) {
117 base::StringTokenizer
entry_tok(pac_string
, ";");
119 while (entry_tok
.GetNext()) {
120 ProxyServer uri
= ProxyServer::FromPacString(
121 entry_tok
.token_begin(), entry_tok
.token_end());
122 // Silently discard malformed inputs.
124 proxies_
.push_back(uri
);
127 // If we failed to parse anything from the PAC results list, fallback to
128 // DIRECT (this basically means an error in the PAC script).
129 if (proxies_
.empty()) {
130 proxies_
.push_back(ProxyServer::Direct());
134 std::string
ProxyList::ToPacString() const {
135 std::string proxy_list
;
136 std::vector
<ProxyServer
>::const_iterator iter
= proxies_
.begin();
137 for (; iter
!= proxies_
.end(); ++iter
) {
138 if (!proxy_list
.empty())
140 proxy_list
+= iter
->ToPacString();
142 return proxy_list
.empty() ? std::string() : proxy_list
;
145 base::ListValue
* ProxyList::ToValue() const {
146 scoped_ptr
<base::ListValue
> list(new base::ListValue());
147 for (size_t i
= 0; i
< proxies_
.size(); ++i
)
148 list
->AppendString(proxies_
[i
].ToURI());
149 return list
.release();
152 bool ProxyList::Fallback(ProxyRetryInfoMap
* proxy_retry_info
,
154 const BoundNetLog
& net_log
) {
156 // TODO(eroman): It would be good if instead of removing failed proxies
157 // from the list, we simply annotated them with the error code they failed
158 // with. Of course, ProxyService::ReconsiderProxyAfterError() would need to
159 // be given this information by the network transaction.
161 // The advantage of this approach is when the network transaction
162 // fails, we could output the full list of proxies that were attempted, and
163 // why each one of those failed (as opposed to just the last failure).
165 // And also, before failing the transaction wholesale, we could go back and
166 // retry the "bad proxies" which we never tried to begin with.
167 // (RemoveBadProxies would annotate them as 'expected bad' rather then delete
168 // them from the list, so we would know what they were).
170 if (proxies_
.empty()) {
174 // By default, proxies are not retried for 5 minutes.
175 UpdateRetryInfoOnFallback(proxy_retry_info
, TimeDelta::FromMinutes(5), true,
176 std::vector
<ProxyServer
>(), net_error
, net_log
);
178 // Remove this proxy from our list.
179 proxies_
.erase(proxies_
.begin());
180 return !proxies_
.empty();
183 void ProxyList::AddProxyToRetryList(ProxyRetryInfoMap
* proxy_retry_info
,
184 base::TimeDelta retry_delay
,
186 const ProxyServer
& proxy_to_retry
,
188 const BoundNetLog
& net_log
) const {
189 // Mark this proxy as bad.
190 TimeTicks bad_until
= TimeTicks::Now() + retry_delay
;
191 std::string proxy_key
= proxy_to_retry
.ToURI();
192 ProxyRetryInfoMap::iterator iter
= proxy_retry_info
->find(proxy_key
);
193 if (iter
== proxy_retry_info
->end() || bad_until
> iter
->second
.bad_until
) {
194 ProxyRetryInfo retry_info
;
195 retry_info
.current_delay
= retry_delay
;
196 retry_info
.bad_until
= bad_until
;
197 retry_info
.try_while_bad
= try_while_bad
;
198 retry_info
.net_error
= net_error
;
199 (*proxy_retry_info
)[proxy_key
] = retry_info
;
201 net_log
.AddEvent(NetLog::TYPE_PROXY_LIST_FALLBACK
,
202 NetLog::StringCallback("bad_proxy", &proxy_key
));
205 void ProxyList::UpdateRetryInfoOnFallback(
206 ProxyRetryInfoMap
* proxy_retry_info
,
207 base::TimeDelta retry_delay
,
209 const std::vector
<ProxyServer
>& additional_proxies_to_bypass
,
211 const BoundNetLog
& net_log
) const {
212 DCHECK(retry_delay
!= base::TimeDelta());
214 if (proxies_
.empty()) {
219 if (!proxies_
[0].is_direct()) {
220 AddProxyToRetryList(proxy_retry_info
,
226 // If any additional proxies to bypass are specified, add to the retry map
228 for (const ProxyServer
& additional_proxy
: additional_proxies_to_bypass
) {
229 AddProxyToRetryList(proxy_retry_info
, retry_delay
, reconsider
,
230 additional_proxy
, net_error
, net_log
);