• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_INFO_H_
6 #define NET_PROXY_RESOLUTION_PROXY_INFO_H_
7 
8 #include <string>
9 
10 #include "base/gtest_prod_util.h"
11 #include "base/time/time.h"
12 #include "net/base/net_export.h"
13 #include "net/base/proxy_server.h"
14 #include "net/proxy_resolution/proxy_config.h"
15 #include "net/proxy_resolution/proxy_list.h"
16 #include "net/proxy_resolution/proxy_retry_info.h"
17 #include "net/traffic_annotation/network_traffic_annotation.h"
18 
19 namespace net {
20 
21 class NetLogWithSource;
22 
23 // This object holds proxy information returned by ResolveProxy.
24 class NET_EXPORT ProxyInfo {
25  public:
26   ProxyInfo();
27   ProxyInfo(const ProxyInfo& other);
28   ~ProxyInfo();
29   // Default copy-constructor and assignment operator are OK!
30 
31   // Uses the same proxy server as the given |proxy_info|.
32   void Use(const ProxyInfo& proxy_info);
33 
34   // Uses a direct connection.
35   void UseDirect();
36 
37   // Uses a direct connection. did_bypass_proxy() will return true to indicate
38   // that the direct connection is the result of configured proxy bypass rules.
39   void UseDirectWithBypassedProxy();
40 
41   // Uses a specific proxy server, of the form:
42   //   proxy-uri = [<scheme> "://"] <hostname> [":" <port>]
43   // This may optionally be a semi-colon delimited list of <proxy-uri>.
44   // It is OK to have LWS between entries.
45   void UseNamedProxy(const std::string& proxy_uri_list);
46 
47   // Sets the proxy list to a single entry, |proxy_server|.
48   void UseProxyServer(const ProxyServer& proxy_server);
49 
50   // Parses from the given PAC result.
51   void UsePacString(const std::string& pac_string);
52 
53   // Uses the proxies from the given list.
54   void UseProxyList(const ProxyList& proxy_list);
55 
56   // Uses the proxies from the given list, but does not otherwise reset the
57   // proxy configuration.
58   void OverrideProxyList(const ProxyList& proxy_list);
59 
60   // Returns true if this proxy info specifies a direct connection.
is_direct()61   bool is_direct() const {
62     // We don't implicitly fallback to DIRECT unless it was added to the list.
63     if (is_empty())
64       return false;
65     return proxy_list_.Get().is_direct();
66   }
67 
is_direct_only()68   bool is_direct_only() const {
69     return is_direct() && proxy_list_.size() == 1 && proxy_retry_info_.empty();
70   }
71 
72   // Returns true if the first valid proxy server is an https proxy.
is_https()73   bool is_https() const {
74     if (is_empty())
75       return false;
76     return proxy_server().is_https();
77   }
78 
79   // Returns true if the first proxy server is an HTTP compatible proxy.
is_http_like()80   bool is_http_like() const {
81     if (is_empty())
82       return false;
83     return proxy_server().is_http_like();
84   }
85 
86   // Returns true if the first proxy server is an HTTP compatible proxy over a
87   // secure connection.
is_secure_http_like()88   bool is_secure_http_like() const {
89     if (is_empty())
90       return false;
91     return proxy_server().is_secure_http_like();
92   }
93 
94   // Returns true if the first valid proxy server is an http proxy.
is_http()95   bool is_http() const {
96     if (is_empty())
97       return false;
98     return proxy_server().is_http();
99   }
100 
101   // Returns true if the first valid proxy server is a quic proxy.
is_quic()102   bool is_quic() const {
103     if (is_empty())
104       return false;
105     return proxy_server().is_quic();
106   }
107 
108   // Returns true if the first valid proxy server is a socks server.
is_socks()109   bool is_socks() const {
110     if (is_empty())
111       return false;
112     return proxy_server().is_socks();
113   }
114 
115   // Returns true if this proxy info has no proxies left to try.
is_empty()116   bool is_empty() const {
117     return proxy_list_.IsEmpty();
118   }
119 
120   // Returns true if this proxy resolution is using a direct connection due to
121   // proxy bypass rules.
did_bypass_proxy()122   bool did_bypass_proxy() const {
123     return did_bypass_proxy_;
124   }
125 
126   // Returns the first valid proxy server. is_empty() must be false to be able
127   // to call this function.
proxy_server()128   const ProxyServer& proxy_server() const { return proxy_list_.Get(); }
129 
130   // Returns the full list of proxies to use.
proxy_list()131   const ProxyList& proxy_list() const { return proxy_list_; }
132 
133   // See description in ProxyList::ToPacString().
134   std::string ToPacString() const;
135 
136   // Marks the current proxy as bad. |net_error| should contain the network
137   // error encountered when this proxy was tried, if any. If this fallback
138   // is not because of a network error, then |OK| should be passed in (eg. for
139   // reasons such as local policy). Returns true if there is another proxy
140   // available to try in |proxy_list_|.
141   bool Fallback(int net_error, const NetLogWithSource& net_log);
142 
143   // De-prioritizes the proxies that we have cached as not working, by moving
144   // them to the end of the proxy list.
145   void DeprioritizeBadProxies(const ProxyRetryInfoMap& proxy_retry_info);
146 
147   // Deletes any entry which doesn't have one of the specified proxy schemes.
148   void RemoveProxiesWithoutScheme(int scheme_bit_field);
149 
set_proxy_resolve_start_time(const base::TimeTicks & proxy_resolve_start_time)150   void set_proxy_resolve_start_time(
151       const base::TimeTicks& proxy_resolve_start_time) {
152     proxy_resolve_start_time_ = proxy_resolve_start_time;
153   }
154 
proxy_resolve_start_time()155   base::TimeTicks proxy_resolve_start_time() const {
156     return proxy_resolve_start_time_;
157   }
158 
set_proxy_resolve_end_time(const base::TimeTicks & proxy_resolve_end_time)159   void set_proxy_resolve_end_time(
160       const base::TimeTicks& proxy_resolve_end_time) {
161     proxy_resolve_end_time_ = proxy_resolve_end_time;
162   }
163 
proxy_resolve_end_time()164   base::TimeTicks proxy_resolve_end_time() const {
165     return proxy_resolve_end_time_;
166   }
167 
set_traffic_annotation(const MutableNetworkTrafficAnnotationTag & traffic_annotation)168   void set_traffic_annotation(
169       const MutableNetworkTrafficAnnotationTag& traffic_annotation) {
170     traffic_annotation_ = traffic_annotation;
171   }
172 
traffic_annotation()173   MutableNetworkTrafficAnnotationTag traffic_annotation() const {
174     return traffic_annotation_;
175   }
176 
proxy_retry_info()177   const ProxyRetryInfoMap& proxy_retry_info() const {
178     return proxy_retry_info_;
179   }
180 
181  private:
182   // Reset proxy and config settings.
183   void Reset();
184 
185   // The ordered list of proxy servers (including DIRECT attempts) remaining to
186   // try. If proxy_list_ is empty, then there is nothing left to fall back to.
187   ProxyList proxy_list_;
188 
189   // List of proxies that have been tried already.
190   ProxyRetryInfoMap proxy_retry_info_;
191 
192   // The traffic annotation of the used proxy config.
193   MutableNetworkTrafficAnnotationTag traffic_annotation_;
194 
195   // Whether the proxy result represent a proxy bypass.
196   bool did_bypass_proxy_ = false;
197 
198   // How long it took to resolve the proxy.  Times are both null if proxy was
199   // determined synchronously without running a PAC.
200   base::TimeTicks proxy_resolve_start_time_;
201   base::TimeTicks proxy_resolve_end_time_;
202 };
203 
204 }  // namespace net
205 
206 #endif  // NET_PROXY_RESOLUTION_PROXY_INFO_H_
207