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 scoped_ptr
<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());
152 bool ProxyList::Fallback(ProxyRetryInfoMap
* proxy_retry_info
,
154 const BoundNetLog
& net_log
) {
155 // TODO(eroman): It would be good if instead of removing failed proxies
156 // from the list, we simply annotated them with the error code they failed
157 // with. Of course, ProxyService::ReconsiderProxyAfterError() would need to
158 // be given this information by the network transaction.
160 // The advantage of this approach is when the network transaction
161 // fails, we could output the full list of proxies that were attempted, and
162 // why each one of those failed (as opposed to just the last failure).
164 // And also, before failing the transaction wholesale, we could go back and
165 // retry the "bad proxies" which we never tried to begin with.
166 // (RemoveBadProxies would annotate them as 'expected bad' rather then delete
167 // them from the list, so we would know what they were).
169 if (proxies_
.empty()) {
173 // By default, proxies are not retried for 5 minutes.
174 UpdateRetryInfoOnFallback(proxy_retry_info
, TimeDelta::FromMinutes(5), true,
175 std::vector
<ProxyServer
>(), net_error
, net_log
);
177 // Remove this proxy from our list.
178 proxies_
.erase(proxies_
.begin());
179 return !proxies_
.empty();
182 void ProxyList::AddProxyToRetryList(ProxyRetryInfoMap
* proxy_retry_info
,
183 base::TimeDelta retry_delay
,
185 const ProxyServer
& proxy_to_retry
,
187 const BoundNetLog
& net_log
) const {
188 // Mark this proxy as bad.
189 TimeTicks bad_until
= TimeTicks::Now() + retry_delay
;
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() || bad_until
> iter
->second
.bad_until
) {
193 ProxyRetryInfo retry_info
;
194 retry_info
.current_delay
= retry_delay
;
195 retry_info
.bad_until
= bad_until
;
196 retry_info
.try_while_bad
= try_while_bad
;
197 retry_info
.net_error
= net_error
;
198 (*proxy_retry_info
)[proxy_key
] = retry_info
;
200 net_log
.AddEvent(NetLog::TYPE_PROXY_LIST_FALLBACK
,
201 NetLog::StringCallback("bad_proxy", &proxy_key
));
204 void ProxyList::UpdateRetryInfoOnFallback(
205 ProxyRetryInfoMap
* proxy_retry_info
,
206 base::TimeDelta retry_delay
,
208 const std::vector
<ProxyServer
>& additional_proxies_to_bypass
,
210 const BoundNetLog
& net_log
) const {
211 DCHECK(retry_delay
!= base::TimeDelta());
213 if (proxies_
.empty()) {
218 if (!proxies_
[0].is_direct()) {
219 AddProxyToRetryList(proxy_retry_info
,
225 // If any additional proxies to bypass are specified, add to the retry map
227 for (const ProxyServer
& additional_proxy
: additional_proxies_to_bypass
) {
228 AddProxyToRetryList(proxy_retry_info
, retry_delay
, reconsider
,
229 additional_proxy
, net_error
, net_log
);