xref: /aosp_15_r20/external/cronet/net/proxy_resolution/proxy_list.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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 <algorithm>
6 
7 #include "net/proxy_resolution/proxy_list.h"
8 
9 #include "base/check.h"
10 #include "base/functional/callback.h"
11 #include "base/notreached.h"
12 #include "base/strings/string_tokenizer.h"
13 #include "base/time/time.h"
14 #include "base/values.h"
15 #include "net/base/proxy_chain.h"
16 #include "net/base/proxy_server.h"
17 #include "net/base/proxy_string_util.h"
18 #include "net/log/net_log.h"
19 #include "net/log/net_log_event_type.h"
20 #include "net/log/net_log_with_source.h"
21 
22 using base::TimeTicks;
23 
24 namespace net {
25 
26 ProxyList::ProxyList() = default;
27 
28 ProxyList::ProxyList(const ProxyList& other) = default;
29 
30 ProxyList::ProxyList(ProxyList&& other) = default;
31 
32 ProxyList& ProxyList::operator=(const ProxyList& other) = default;
33 
34 ProxyList& ProxyList::operator=(ProxyList&& other) = default;
35 
36 ProxyList::~ProxyList() = default;
37 
Set(const std::string & proxy_uri_list)38 void ProxyList::Set(const std::string& proxy_uri_list) {
39   Clear();
40   base::StringTokenizer str_tok(proxy_uri_list, ";");
41   while (str_tok.GetNext()) {
42     ProxyChain chain =
43         ProxyUriToProxyChain(str_tok.token_piece(), ProxyServer::SCHEME_HTTP);
44     AddProxyChain(chain);
45   }
46 }
47 
SetSingleProxyChain(const ProxyChain & proxy_chain)48 void ProxyList::SetSingleProxyChain(const ProxyChain& proxy_chain) {
49   Clear();
50   AddProxyChain(proxy_chain);
51 }
52 
SetSingleProxyServer(const ProxyServer & proxy_server)53 void ProxyList::SetSingleProxyServer(const ProxyServer& proxy_server) {
54   Clear();
55   AddProxyServer(proxy_server);
56 }
57 
AddProxyChain(const ProxyChain & proxy_chain)58 void ProxyList::AddProxyChain(const ProxyChain& proxy_chain) {
59   // Silently discard malformed inputs.
60   if (proxy_chain.IsValid()) {
61     proxy_chains_.push_back(proxy_chain);
62   }
63 }
64 
AddProxyServer(const ProxyServer & proxy_server)65 void ProxyList::AddProxyServer(const ProxyServer& proxy_server) {
66   AddProxyChain(ProxyChain(proxy_server));
67 }
68 
DeprioritizeBadProxyChains(const ProxyRetryInfoMap & proxy_retry_info)69 void ProxyList::DeprioritizeBadProxyChains(
70     const ProxyRetryInfoMap& proxy_retry_info) {
71   // Partition the proxy list in two:
72   //   (1) the known bad proxy chains
73   //   (2) everything else
74   std::vector<ProxyChain> good_chains;
75   std::vector<ProxyChain> bad_chains_to_try;
76 
77   std::vector<ProxyChain>::const_iterator iter = proxy_chains_.begin();
78   for (; iter != proxy_chains_.end(); ++iter) {
79     auto bad_info = proxy_retry_info.find(*iter);
80     if (bad_info != proxy_retry_info.end()) {
81       // This proxy is bad. Check if it's time to retry.
82       if (bad_info->second.bad_until >= TimeTicks::Now()) {
83         // still invalid.
84         if (bad_info->second.try_while_bad) {
85           bad_chains_to_try.push_back(*iter);
86         }
87         continue;
88       }
89     }
90     good_chains.push_back(*iter);
91   }
92 
93   // "proxy_chains_ = good_chains + bad_proxies"
94   proxy_chains_.swap(good_chains);
95   proxy_chains_.insert(proxy_chains_.end(), bad_chains_to_try.begin(),
96                        bad_chains_to_try.end());
97 }
98 
RemoveProxiesWithoutScheme(int scheme_bit_field)99 void ProxyList::RemoveProxiesWithoutScheme(int scheme_bit_field) {
100   std::erase_if(proxy_chains_, [&](const ProxyChain& chain) {
101     auto& proxy_servers = chain.proxy_servers();
102     // Remove the chain if any of the component servers does not match
103     // at least one scheme in `scheme_bit_field`.
104     return std::any_of(proxy_servers.begin(), proxy_servers.end(),
105                        [&](const ProxyServer& server) {
106                          return !(scheme_bit_field & server.scheme());
107                        });
108   });
109 }
110 
Clear()111 void ProxyList::Clear() {
112   proxy_chains_.clear();
113 }
114 
IsEmpty() const115 bool ProxyList::IsEmpty() const {
116   return proxy_chains_.empty();
117 }
118 
size() const119 size_t ProxyList::size() const {
120   return proxy_chains_.size();
121 }
122 
123 // Returns true if |*this| lists the same proxy chains as |other|.
Equals(const ProxyList & other) const124 bool ProxyList::Equals(const ProxyList& other) const {
125   if (size() != other.size())
126     return false;
127   return proxy_chains_ == other.proxy_chains_;
128 }
129 
First() const130 const ProxyChain& ProxyList::First() const {
131   CHECK(!proxy_chains_.empty());
132   return proxy_chains_[0];
133 }
134 
AllChains() const135 const std::vector<ProxyChain>& ProxyList::AllChains() const {
136   return proxy_chains_;
137 }
138 
SetFromPacString(const std::string & pac_string)139 void ProxyList::SetFromPacString(const std::string& pac_string) {
140   Clear();
141   base::StringTokenizer entry_tok(pac_string, ";");
142   while (entry_tok.GetNext()) {
143     ProxyChain proxy_chain =
144         PacResultElementToProxyChain(entry_tok.token_piece());
145     if (proxy_chain.IsValid()) {
146       proxy_chains_.emplace_back(proxy_chain);
147     }
148   }
149 
150   // If we failed to parse anything from the PAC results list, fallback to
151   // DIRECT (this basically means an error in the PAC script).
152   if (proxy_chains_.empty()) {
153     proxy_chains_.push_back(ProxyChain::Direct());
154   }
155 }
156 
ToPacString() const157 std::string ProxyList::ToPacString() const {
158   std::string proxy_list;
159   for (const ProxyChain& proxy_chain : proxy_chains_) {
160     if (!proxy_list.empty()) {
161       proxy_list += ";";
162     }
163     CHECK(!proxy_chain.is_multi_proxy());
164     proxy_list += proxy_chain.is_direct()
165                       ? "DIRECT"
166                       : ProxyServerToPacResultElement(proxy_chain.First());
167   }
168   return proxy_list.empty() ? std::string() : proxy_list;
169 }
170 
ToDebugString() const171 std::string ProxyList::ToDebugString() const {
172   std::string proxy_list;
173 
174   for (const ProxyChain& proxy_chain : proxy_chains_) {
175     if (!proxy_list.empty()) {
176       proxy_list += ";";
177     }
178     if (proxy_chain.is_multi_proxy()) {
179       proxy_list += proxy_chain.ToDebugString();
180     } else {
181       proxy_list += proxy_chain.is_direct()
182                         ? "DIRECT"
183                         : ProxyServerToPacResultElement(proxy_chain.First());
184     }
185   }
186   return proxy_list;
187 }
188 
ToValue() const189 base::Value ProxyList::ToValue() const {
190   base::Value::List list;
191   for (const auto& proxy_chain : proxy_chains_) {
192     if (proxy_chain.is_direct()) {
193       list.Append("direct://");
194     } else {
195       list.Append(proxy_chain.ToDebugString());
196     }
197   }
198   return base::Value(std::move(list));
199 }
200 
Fallback(ProxyRetryInfoMap * proxy_retry_info,int net_error,const NetLogWithSource & net_log)201 bool ProxyList::Fallback(ProxyRetryInfoMap* proxy_retry_info,
202                          int net_error,
203                          const NetLogWithSource& net_log) {
204   if (proxy_chains_.empty()) {
205     NOTREACHED();
206     return false;
207   }
208   // By default, proxy chains are not retried for 5 minutes.
209   UpdateRetryInfoOnFallback(proxy_retry_info, base::Minutes(5), true,
210                             std::vector<ProxyChain>(), net_error, net_log);
211 
212   // Remove this proxy from our list.
213   proxy_chains_.erase(proxy_chains_.begin());
214   return !proxy_chains_.empty();
215 }
216 
AddProxyChainToRetryList(ProxyRetryInfoMap * proxy_retry_info,base::TimeDelta retry_delay,bool try_while_bad,const ProxyChain & proxy_chain_to_retry,int net_error,const NetLogWithSource & net_log) const217 void ProxyList::AddProxyChainToRetryList(
218     ProxyRetryInfoMap* proxy_retry_info,
219     base::TimeDelta retry_delay,
220     bool try_while_bad,
221     const ProxyChain& proxy_chain_to_retry,
222     int net_error,
223     const NetLogWithSource& net_log) const {
224   // Mark this proxy chain as bad.
225   TimeTicks bad_until = TimeTicks::Now() + retry_delay;
226   auto iter = proxy_retry_info->find(proxy_chain_to_retry);
227   if (iter == proxy_retry_info->end() || bad_until > iter->second.bad_until) {
228     ProxyRetryInfo retry_info;
229     retry_info.current_delay = retry_delay;
230     retry_info.bad_until = bad_until;
231     retry_info.try_while_bad = try_while_bad;
232     retry_info.net_error = net_error;
233     (*proxy_retry_info)[proxy_chain_to_retry] = retry_info;
234   }
235   net_log.AddEventWithStringParams(NetLogEventType::PROXY_LIST_FALLBACK,
236                                    "bad_proxy_chain",
237                                    proxy_chain_to_retry.ToDebugString());
238 }
239 
UpdateRetryInfoOnFallback(ProxyRetryInfoMap * proxy_retry_info,base::TimeDelta retry_delay,bool reconsider,const std::vector<ProxyChain> & additional_proxies_to_bypass,int net_error,const NetLogWithSource & net_log) const240 void ProxyList::UpdateRetryInfoOnFallback(
241     ProxyRetryInfoMap* proxy_retry_info,
242     base::TimeDelta retry_delay,
243     bool reconsider,
244     const std::vector<ProxyChain>& additional_proxies_to_bypass,
245     int net_error,
246     const NetLogWithSource& net_log) const {
247   DCHECK(!retry_delay.is_zero());
248 
249   if (proxy_chains_.empty()) {
250     NOTREACHED();
251     return;
252   }
253 
254   auto& first_chain = proxy_chains_[0];
255   if (!first_chain.is_direct()) {
256     AddProxyChainToRetryList(proxy_retry_info, retry_delay, reconsider,
257                              first_chain, net_error, net_log);
258     // If any additional proxies to bypass are specified, add to the retry map
259     // as well.
260     for (const ProxyChain& additional_proxy_chain :
261          additional_proxies_to_bypass) {
262       AddProxyChainToRetryList(
263           proxy_retry_info, retry_delay, reconsider,
264           ProxyChain(additional_proxy_chain.proxy_servers()), net_error,
265           net_log);
266     }
267   }
268 }
269 
270 }  // namespace net
271