http_proxy_connect_job.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright 2019 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_HTTP_HTTP_PROXY_CONNECT_JOB_H_
  5. #define NET_HTTP_HTTP_PROXY_CONNECT_JOB_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback_forward.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/time/time.h"
  11. #include "net/base/host_port_pair.h"
  12. #include "net/base/net_export.h"
  13. #include "net/base/network_isolation_key.h"
  14. #include "net/base/request_priority.h"
  15. #include "net/dns/public/resolve_error_info.h"
  16. #include "net/http/http_auth.h"
  17. #include "net/quic/quic_chromium_client_session.h"
  18. #include "net/socket/connect_job.h"
  19. #include "net/socket/ssl_client_socket.h"
  20. #include "net/spdy/spdy_session_key.h"
  21. #include "net/ssl/ssl_cert_request_info.h"
  22. #include "net/traffic_annotation/network_traffic_annotation.h"
  23. namespace net {
  24. class HttpAuthController;
  25. class HttpResponseInfo;
  26. class NetworkQualityEstimator;
  27. class SocketTag;
  28. class ProxyClientSocket;
  29. class SpdyStreamRequest;
  30. class SSLSocketParams;
  31. class TransportSocketParams;
  32. class QuicStreamRequest;
  33. // HttpProxySocketParams only needs the socket params for one of the proxy
  34. // types. The other param must be NULL. When using an HTTP proxy,
  35. // |transport_params| must be set. When using an HTTPS proxy or QUIC proxy,
  36. // |ssl_params| must be set.
  37. class NET_EXPORT_PRIVATE HttpProxySocketParams
  38. : public base::RefCounted<HttpProxySocketParams> {
  39. public:
  40. HttpProxySocketParams(scoped_refptr<TransportSocketParams> transport_params,
  41. scoped_refptr<SSLSocketParams> ssl_params,
  42. bool is_quic,
  43. const HostPortPair& endpoint,
  44. bool tunnel,
  45. const NetworkTrafficAnnotationTag traffic_annotation,
  46. const NetworkIsolationKey& network_isolation_key);
  47. HttpProxySocketParams(const HttpProxySocketParams&) = delete;
  48. HttpProxySocketParams& operator=(const HttpProxySocketParams&) = delete;
  49. const scoped_refptr<TransportSocketParams>& transport_params() const {
  50. return transport_params_;
  51. }
  52. const scoped_refptr<SSLSocketParams>& ssl_params() const {
  53. return ssl_params_;
  54. }
  55. bool is_quic() const { return is_quic_; }
  56. const HostPortPair& endpoint() const { return endpoint_; }
  57. bool tunnel() const { return tunnel_; }
  58. const NetworkIsolationKey& network_isolation_key() const {
  59. return network_isolation_key_;
  60. }
  61. const NetworkTrafficAnnotationTag traffic_annotation() const {
  62. return traffic_annotation_;
  63. }
  64. private:
  65. friend class base::RefCounted<HttpProxySocketParams>;
  66. ~HttpProxySocketParams();
  67. const scoped_refptr<TransportSocketParams> transport_params_;
  68. const scoped_refptr<SSLSocketParams> ssl_params_;
  69. bool is_quic_;
  70. const HostPortPair endpoint_;
  71. const bool tunnel_;
  72. const NetworkIsolationKey network_isolation_key_;
  73. const NetworkTrafficAnnotationTag traffic_annotation_;
  74. };
  75. // HttpProxyConnectJob optionally establishes a tunnel through the proxy
  76. // server after connecting the underlying transport socket.
  77. class NET_EXPORT_PRIVATE HttpProxyConnectJob : public ConnectJob,
  78. public ConnectJob::Delegate {
  79. public:
  80. class NET_EXPORT_PRIVATE Factory {
  81. public:
  82. Factory() = default;
  83. virtual ~Factory() = default;
  84. virtual std::unique_ptr<HttpProxyConnectJob> Create(
  85. RequestPriority priority,
  86. const SocketTag& socket_tag,
  87. const CommonConnectJobParams* common_connect_job_params,
  88. scoped_refptr<HttpProxySocketParams> params,
  89. ConnectJob::Delegate* delegate,
  90. const NetLogWithSource* net_log);
  91. };
  92. HttpProxyConnectJob(RequestPriority priority,
  93. const SocketTag& socket_tag,
  94. const CommonConnectJobParams* common_connect_job_params,
  95. scoped_refptr<HttpProxySocketParams> params,
  96. ConnectJob::Delegate* delegate,
  97. const NetLogWithSource* net_log);
  98. HttpProxyConnectJob(const HttpProxyConnectJob&) = delete;
  99. HttpProxyConnectJob& operator=(const HttpProxyConnectJob&) = delete;
  100. ~HttpProxyConnectJob() override;
  101. // A single priority is used for tunnels over H2 and QUIC, which can be shared
  102. // by multiple requests of different priorities either in series (tunnels for
  103. // HTTP/1.x requests) or simultaneously (tunnels for H2 requests). Changing
  104. // the priority of the tunnel based on the current request also potentially
  105. // leaks private data to the proxy.
  106. static const RequestPriority kH2QuicTunnelPriority;
  107. // ConnectJob methods.
  108. LoadState GetLoadState() const override;
  109. bool HasEstablishedConnection() const override;
  110. ResolveErrorInfo GetResolveErrorInfo() const override;
  111. bool IsSSLError() const override;
  112. scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override;
  113. // ConnectJob::Delegate implementation.
  114. void OnConnectJobComplete(int result, ConnectJob* job) override;
  115. void OnNeedsProxyAuth(const HttpResponseInfo& response,
  116. HttpAuthController* auth_controller,
  117. base::OnceClosure restart_with_auth_callback,
  118. ConnectJob* job) override;
  119. // In some cases, a timeout that's stricter than the TCP (+SSL, if applicable)
  120. // is used for HTTP proxies during connection establishment and SSL
  121. // negotiation for the connection to the proxy itself. In those cases, returns
  122. // the connection timeout that will be used by a HttpProxyConnectJob created
  123. // with the specified parameters, given current network conditions. Otherwise,
  124. // returns base::TimeDelta().
  125. static base::TimeDelta AlternateNestedConnectionTimeout(
  126. const HttpProxySocketParams& params,
  127. const NetworkQualityEstimator* network_quality_estimator);
  128. // Returns the timeout for establishing a tunnel after a connection has been
  129. // established.
  130. static base::TimeDelta TunnelTimeoutForTesting();
  131. // Updates the field trial parameters used in calculating timeouts.
  132. static void UpdateFieldTrialParametersForTesting();
  133. private:
  134. enum State {
  135. STATE_BEGIN_CONNECT,
  136. STATE_TRANSPORT_CONNECT,
  137. STATE_TRANSPORT_CONNECT_COMPLETE,
  138. STATE_HTTP_PROXY_CONNECT,
  139. STATE_HTTP_PROXY_CONNECT_COMPLETE,
  140. STATE_SPDY_PROXY_CREATE_STREAM,
  141. STATE_SPDY_PROXY_CREATE_STREAM_COMPLETE,
  142. STATE_QUIC_PROXY_CREATE_SESSION,
  143. STATE_QUIC_PROXY_CREATE_STREAM,
  144. STATE_QUIC_PROXY_CREATE_STREAM_COMPLETE,
  145. STATE_RESTART_WITH_AUTH,
  146. STATE_RESTART_WITH_AUTH_COMPLETE,
  147. STATE_NONE,
  148. };
  149. // Begins the tcp connection and the optional Http proxy tunnel. If the
  150. // request is not immediately serviceable (likely), the request will return
  151. // ERR_IO_PENDING. An OK return from this function or the callback means
  152. // that the connection is established; ERR_PROXY_AUTH_REQUESTED means
  153. // that the tunnel needs authentication credentials, the socket will be
  154. // returned in this case, and must be released back to the pool; or
  155. // a standard net error code will be returned.
  156. int ConnectInternal() override;
  157. ProxyServer::Scheme GetProxyServerScheme() const;
  158. void OnIOComplete(int result);
  159. void RestartWithAuthCredentials();
  160. // Runs the state transition loop.
  161. int DoLoop(int result);
  162. // Determine if need to go through TCP or SSL path.
  163. int DoBeginConnect();
  164. // Connecting to HTTP or HTTPS Proxy
  165. int DoTransportConnect();
  166. int DoTransportConnectComplete(int result);
  167. int DoHttpProxyConnect();
  168. int DoHttpProxyConnectComplete(int result);
  169. int DoSpdyProxyCreateStream();
  170. int DoSpdyProxyCreateStreamComplete(int result);
  171. int DoQuicProxyCreateSession();
  172. int DoQuicProxyCreateStream(int result);
  173. int DoQuicProxyCreateStreamComplete(int result);
  174. int DoRestartWithAuth();
  175. int DoRestartWithAuthComplete(int result);
  176. // ConnectJob implementation.
  177. void ChangePriorityInternal(RequestPriority priority) override;
  178. void OnTimedOutInternal() override;
  179. void OnAuthChallenge();
  180. const HostPortPair& GetDestination() const;
  181. std::string GetUserAgent() const;
  182. SpdySessionKey CreateSpdySessionKey() const;
  183. scoped_refptr<HttpProxySocketParams> params_;
  184. scoped_refptr<SSLCertRequestInfo> ssl_cert_request_info_;
  185. State next_state_ = STATE_NONE;
  186. bool has_restarted_ = false;
  187. // Set to true once a connection has been successfully established. Remains
  188. // true even if a new socket is being connected to retry with auth.
  189. bool has_established_connection_ = false;
  190. ResolveErrorInfo resolve_error_info_;
  191. std::unique_ptr<ConnectJob> nested_connect_job_;
  192. std::unique_ptr<ProxyClientSocket> transport_socket_;
  193. std::unique_ptr<SpdyStreamRequest> spdy_stream_request_;
  194. std::unique_ptr<QuicStreamRequest> quic_stream_request_;
  195. std::unique_ptr<QuicChromiumClientSession::Handle> quic_session_;
  196. scoped_refptr<HttpAuthController> http_auth_controller_;
  197. NetErrorDetails quic_net_error_details_;
  198. // Time when the connection to the proxy was started.
  199. base::TimeTicks connect_start_time_;
  200. base::WeakPtrFactory<HttpProxyConnectJob> weak_ptr_factory_{this};
  201. };
  202. } // namespace net
  203. #endif // NET_HTTP_HTTP_PROXY_CONNECT_JOB_H_