xref: /aosp_15_r20/external/cronet/net/proxy_resolution/proxy_list.h (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 #ifndef NET_PROXY_RESOLUTION_PROXY_LIST_H_
6 #define NET_PROXY_RESOLUTION_PROXY_LIST_H_
7 
8 #include <stddef.h>
9 
10 #include <memory>
11 #include <optional>
12 #include <string>
13 #include <vector>
14 
15 #include "net/base/net_export.h"
16 #include "net/base/proxy_server.h"
17 #include "net/proxy_resolution/proxy_retry_info.h"
18 
19 namespace base {
20 class TimeDelta;
21 class Value;
22 }
23 
24 namespace net {
25 
26 class ProxyChain;
27 class NetLogWithSource;
28 
29 // This class is used to hold a prioritized list of proxy chains. It handles
30 // fallback to lower-priority chains if multiple chains are specified.
31 class NET_EXPORT_PRIVATE ProxyList {
32  public:
33   ProxyList();
34   ProxyList(const ProxyList& other);
35   ProxyList(ProxyList&& other);
36   ProxyList& operator=(const ProxyList& other);
37   ProxyList& operator=(ProxyList&& other);
38   ~ProxyList();
39 
40   // Initializes the ProxyList to contain one or more ProxyChains.
41   // `proxy_uri_list` is a semicolon-delimited list of proxy URIs. Note that
42   // multi-proxy chains cannot be represented in this format.
43   void Set(const std::string& proxy_uri_list);
44 
45   // Set the proxy list to a single entry, |proxy_chain|.
46   void SetSingleProxyChain(const ProxyChain& proxy_chain);
47 
48   // Set the proxy list to a single entry, a chain containing |proxy_server|.
49   void SetSingleProxyServer(const ProxyServer& proxy_server);
50 
51   // Append a single proxy chain to the end of the proxy list.
52   void AddProxyChain(const ProxyChain& proxy_chain);
53 
54   // Append a single proxy chain containing the given server to the end of the
55   // proxy list.
56   void AddProxyServer(const ProxyServer& proxy_server);
57 
58   // De-prioritizes the proxy chains that are cached as not working but are
59   // allowed to be reconsidered, by moving them to the end of the fallback list.
60   void DeprioritizeBadProxyChains(const ProxyRetryInfoMap& proxy_retry_info);
61 
62   // Deletes all chains which don't exclusively consist of proxy servers with
63   // the specified schemes. `scheme_bit_field` is a bunch of
64   // `ProxyServer::Scheme` bitwise ORed together. This is used to remove proxies
65   // that do not support specific functionality such as websockets.
66   void RemoveProxiesWithoutScheme(int scheme_bit_field);
67 
68   // Clear the proxy list.
69   void Clear();
70 
71   // Returns true if there is nothing left in the ProxyList.
72   bool IsEmpty() const;
73 
74   // Returns the number of proxy servers in this list.
75   size_t size() const;
76 
77   // Returns true if |*this| lists the same proxies as |other|.
78   bool Equals(const ProxyList& other) const;
79 
80   // Returns the first proxy chain in the list.
81   const ProxyChain& First() const;
82 
83   // Returns all proxy chains in the list.
84   const std::vector<ProxyChain>& AllChains() const;
85 
86   // Sets the list by parsing the PAC result |pac_string|.
87   // Some examples for |pac_string|:
88   //   "DIRECT"
89   //   "PROXY foopy1"
90   //   "PROXY foopy1; SOCKS4 foopy2:1188"
91   // Does a best-effort parse, and silently discards any errors.
92   void SetFromPacString(const std::string& pac_string);
93 
94   // Returns a PAC-style semicolon-separated list of valid proxy servers.
95   // For example: "PROXY xxx.xxx.xxx.xxx:xx; SOCKS yyy.yyy.yyy:yy". This is
96   // only valid if the list contains no multi-proxy chains, as those cannot
97   // be represented in PAC syntax.
98   std::string ToPacString() const;
99 
100   // Returns a semicolon-separated list of proxy chain debug representations.
101   // For single-proxy chains, this is just the PAC representation of the proxy;
102   // otherwise the chain is displayed in "[..]".
103   // TODO(https://crbug.com/1491092): Once a PAC string format for multi-proxy
104   // chains is implemented, this can be removed in favor of `ToPacString()`.
105   std::string ToDebugString() const;
106 
107   // Returns a serialized value for the list.
108   base::Value ToValue() const;
109 
110   // Marks the current proxy chain as bad and deletes it from the list. The
111   // list of known bad proxies is given by |proxy_retry_info|. |net_error|
112   // should contain the network error encountered when this proxy chain was
113   // tried, if any. If this fallback is not because of a network error, then
114   // |OK| should be passed in (eg. for reasons such as local policy). Returns
115   // true if there is another chain available in the list.
116   bool Fallback(ProxyRetryInfoMap* proxy_retry_info,
117                 int net_error,
118                 const NetLogWithSource& net_log);
119 
120   // Updates |proxy_retry_info| to indicate that the first proxy chain in the
121   // list is bad. This is distinct from Fallback(), above, to allow updating
122   // proxy retry information without modifying a given transction's proxy list.
123   // Will retry after |retry_delay| if positive, and will use the default proxy
124   // retry duration otherwise. It may reconsider the proxy beforehand if
125   // |reconsider| is true. Additionally updates |proxy_retry_info| with
126   // |additional_proxies_to_bypass|. |net_error| should contain the network
127   // error countered when this proxy chain was tried, or OK if the proxy retry
128   // info is being updated for a non-network related reason (e.g. local policy).
129   void UpdateRetryInfoOnFallback(
130       ProxyRetryInfoMap* proxy_retry_info,
131       base::TimeDelta retry_delay,
132       bool reconsider,
133       const std::vector<ProxyChain>& additional_proxies_to_bypass,
134       int net_error,
135       const NetLogWithSource& net_log) const;
136 
137  private:
138   // Updates |proxy_retry_info| to indicate that the |proxy_to_retry| in
139   // |proxies_| is bad for |retry_delay|, but may be reconsidered earlier if
140   // |try_while_bad| is true. |net_error| should contain the network error
141   // countered when this proxy was tried, or OK if the proxy retry info is
142   // being updated for a non-network related reason (e.g. local policy).
143   void AddProxyChainToRetryList(ProxyRetryInfoMap* proxy_retry_info,
144                                 base::TimeDelta retry_delay,
145                                 bool try_while_bad,
146                                 const ProxyChain& proxy_chain_to_retry,
147                                 int net_error,
148                                 const NetLogWithSource& net_log) const;
149 
150   // List of proxy chains.
151   std::vector<ProxyChain> proxy_chains_;
152 };
153 
154 }  // namespace net
155 
156 #endif  // NET_PROXY_RESOLUTION_PROXY_LIST_H_
157