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/string_tokenizer.h"
10 #include "base/time.h"
11 #include "net/proxy/proxy_server.h"
13 using base::TimeDelta
;
14 using base::TimeTicks
;
18 ProxyList::ProxyList() {
21 ProxyList::~ProxyList() {
24 void ProxyList::Set(const std::string
& proxy_uri_list
) {
26 StringTokenizer
str_tok(proxy_uri_list
, ";");
27 while (str_tok
.GetNext()) {
28 ProxyServer uri
= ProxyServer::FromURI(
29 str_tok
.token_begin(), str_tok
.token_end(), ProxyServer::SCHEME_HTTP
);
30 // Silently discard malformed inputs.
32 proxies_
.push_back(uri
);
36 void ProxyList::SetSingleProxyServer(const ProxyServer
& proxy_server
) {
38 if (proxy_server
.is_valid())
39 proxies_
.push_back(proxy_server
);
42 void ProxyList::DeprioritizeBadProxies(
43 const ProxyRetryInfoMap
& proxy_retry_info
) {
44 // Partition the proxy list in two:
45 // (1) the known bad proxies
46 // (2) everything else
47 std::vector
<ProxyServer
> good_proxies
;
48 std::vector
<ProxyServer
> bad_proxies
;
50 std::vector
<ProxyServer
>::const_iterator iter
= proxies_
.begin();
51 for (; iter
!= proxies_
.end(); ++iter
) {
52 ProxyRetryInfoMap::const_iterator bad_proxy
=
53 proxy_retry_info
.find(iter
->ToURI());
54 if (bad_proxy
!= proxy_retry_info
.end()) {
55 // This proxy is bad. Check if it's time to retry.
56 if (bad_proxy
->second
.bad_until
>= TimeTicks::Now()) {
58 bad_proxies
.push_back(*iter
);
62 good_proxies
.push_back(*iter
);
65 // "proxies_ = good_proxies + bad_proxies"
66 proxies_
.swap(good_proxies
);
67 proxies_
.insert(proxies_
.end(), bad_proxies
.begin(), bad_proxies
.end());
70 bool ProxyList::HasUntriedProxies(
71 const ProxyRetryInfoMap
& proxy_retry_info
) const {
72 std::vector
<ProxyServer
>::const_iterator iter
= proxies_
.begin();
73 for (; iter
!= proxies_
.end(); ++iter
) {
74 ProxyRetryInfoMap::const_iterator bad_proxy
=
75 proxy_retry_info
.find(iter
->ToURI());
76 if (bad_proxy
!= proxy_retry_info
.end()) {
77 // This proxy is bad. Check if it's time to retry.
78 if (bad_proxy
->second
.bad_until
>= TimeTicks::Now()) {
82 // Either we've found the entry in the retry map and it's expired or we
83 // didn't find a corresponding entry in the retry map. In either case, we
84 // have a proxy to try.
90 void ProxyList::RemoveProxiesWithoutScheme(int scheme_bit_field
) {
91 for (std::vector
<ProxyServer
>::iterator it
= proxies_
.begin();
92 it
!= proxies_
.end(); ) {
93 if (!(scheme_bit_field
& it
->scheme())) {
94 it
= proxies_
.erase(it
);
101 void ProxyList::Clear() {
105 bool ProxyList::IsEmpty() const {
106 return proxies_
.empty();
109 size_t ProxyList::size() const {
110 return proxies_
.size();
113 const ProxyServer
& ProxyList::Get() const {
114 DCHECK(!proxies_
.empty());
118 void ProxyList::SetFromPacString(const std::string
& pac_string
) {
119 StringTokenizer
entry_tok(pac_string
, ";");
121 while (entry_tok
.GetNext()) {
122 ProxyServer uri
= ProxyServer::FromPacString(
123 entry_tok
.token_begin(), entry_tok
.token_end());
124 // Silently discard malformed inputs.
126 proxies_
.push_back(uri
);
129 // If we failed to parse anything from the PAC results list, fallback to
130 // DIRECT (this basically means an error in the PAC script).
131 if (proxies_
.empty()) {
132 proxies_
.push_back(ProxyServer::Direct());
136 std::string
ProxyList::ToPacString() const {
137 std::string proxy_list
;
138 std::vector
<ProxyServer
>::const_iterator iter
= proxies_
.begin();
139 for (; iter
!= proxies_
.end(); ++iter
) {
140 if (!proxy_list
.empty())
142 proxy_list
+= iter
->ToPacString();
144 return proxy_list
.empty() ? std::string() : proxy_list
;
147 bool ProxyList::Fallback(ProxyRetryInfoMap
* proxy_retry_info
,
148 const BoundNetLog
& net_log
) {
150 // TODO(eroman): It would be good if instead of removing failed proxies
151 // from the list, we simply annotated them with the error code they failed
152 // with. Of course, ProxyService::ReconsiderProxyAfterError() would need to
153 // be given this information by the network transaction.
155 // The advantage of this approach is when the network transaction
156 // fails, we could output the full list of proxies that were attempted, and
157 // why each one of those failed (as opposed to just the last failure).
159 // And also, before failing the transaction wholesale, we could go back and
160 // retry the "bad proxies" which we never tried to begin with.
161 // (RemoveBadProxies would annotate them as 'expected bad' rather then delete
162 // them from the list, so we would know what they were).
164 if (proxies_
.empty()) {
168 UpdateRetryInfoOnFallback(proxy_retry_info
, net_log
);
170 // Remove this proxy from our list.
171 proxies_
.erase(proxies_
.begin());
172 return !proxies_
.empty();
175 void ProxyList::UpdateRetryInfoOnFallback(
176 ProxyRetryInfoMap
* proxy_retry_info
, const BoundNetLog
& net_log
) const {
177 // Number of minutes to wait before retrying a bad proxy server.
178 const TimeDelta kProxyRetryDelay
= TimeDelta::FromMinutes(5);
180 if (proxies_
.empty()) {
185 if (!proxies_
[0].is_direct()) {
186 std::string key
= proxies_
[0].ToURI();
187 // Mark this proxy as bad.
188 ProxyRetryInfoMap::iterator iter
= proxy_retry_info
->find(key
);
189 if (iter
!= proxy_retry_info
->end()) {
190 // TODO(nsylvain): This is not the first time we get this. We should
191 // double the retry time. Bug 997660.
192 iter
->second
.bad_until
= TimeTicks::Now() + iter
->second
.current_delay
;
194 ProxyRetryInfo retry_info
;
195 retry_info
.current_delay
= kProxyRetryDelay
;
196 retry_info
.bad_until
= TimeTicks().Now() + retry_info
.current_delay
;
197 (*proxy_retry_info
)[key
] = retry_info
;
199 net_log
.AddEvent(NetLog::TYPE_PROXY_LIST_FALLBACK
,
200 NetLog::StringCallback("bad_proxy", &key
));