proxy_info.h 6.6 KB

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