proxy_resolving_client_socket.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 SERVICES_NETWORK_PROXY_RESOLVING_CLIENT_SOCKET_H_
  5. #define SERVICES_NETWORK_PROXY_RESOLVING_CLIENT_SOCKET_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/compiler_specific.h"
  9. #include "base/component_export.h"
  10. #include "base/gtest_prod_util.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "net/base/host_port_pair.h"
  15. #include "net/base/net_errors.h"
  16. #include "net/base/network_isolation_key.h"
  17. #include "net/log/net_log_with_source.h"
  18. #include "net/proxy_resolution/proxy_info.h"
  19. #include "net/proxy_resolution/proxy_resolution_service.h"
  20. #include "net/socket/connect_job.h"
  21. #include "net/socket/next_proto.h"
  22. #include "net/socket/stream_socket.h"
  23. #include "net/traffic_annotation/network_traffic_annotation.h"
  24. #include "url/gurl.h"
  25. namespace net {
  26. struct CommonConnectJobParams;
  27. class ConnectJobFactory;
  28. class HttpAuthController;
  29. class HttpResponseInfo;
  30. class HttpNetworkSession;
  31. class NetworkIsolationKey;
  32. class ProxyResolutionRequest;
  33. } // namespace net
  34. namespace network {
  35. // This class represents a net::StreamSocket implementation that does proxy
  36. // resolution for the provided url before establishing a connection. If there is
  37. // a proxy configured, a connection will be established to the proxy.
  38. class COMPONENT_EXPORT(NETWORK_SERVICE) ProxyResolvingClientSocket
  39. : public net::StreamSocket,
  40. public net::ConnectJob::Delegate {
  41. public:
  42. // Constructs a new ProxyResolvingClientSocket. `url`'s host and port specify
  43. // where a connection will be established to. The full URL will be only used
  44. // for proxy resolution. Caller doesn't need to explicitly sanitize the url,
  45. // any sensitive data (like embedded usernames and passwords), and local data
  46. // (i.e. reference fragment) will be sanitized by net::ProxyResolutionService
  47. // before the url is disclosed to the PAC script. If `use_tls`, this will try
  48. // to do a tls connect instead of a regular tcp connect. `network_session`,
  49. // `common_connect_job_params`, and `connect_job_factory` must outlive `this`.
  50. ProxyResolvingClientSocket(
  51. net::HttpNetworkSession* network_session,
  52. const net::CommonConnectJobParams* common_connect_job_params,
  53. const GURL& url,
  54. const net::NetworkIsolationKey& network_isolation_key,
  55. bool use_tls,
  56. const net::ConnectJobFactory* connect_job_factory);
  57. ProxyResolvingClientSocket(const ProxyResolvingClientSocket&) = delete;
  58. ProxyResolvingClientSocket& operator=(const ProxyResolvingClientSocket&) =
  59. delete;
  60. ~ProxyResolvingClientSocket() override;
  61. // net::StreamSocket implementation.
  62. int Read(net::IOBuffer* buf,
  63. int buf_len,
  64. net::CompletionOnceCallback callback) override;
  65. int ReadIfReady(net::IOBuffer* buf,
  66. int buf_len,
  67. net::CompletionOnceCallback callback) override;
  68. int CancelReadIfReady() override;
  69. int Write(
  70. net::IOBuffer* buf,
  71. int buf_len,
  72. net::CompletionOnceCallback callback,
  73. const net::NetworkTrafficAnnotationTag& traffic_annotation) override;
  74. int SetReceiveBufferSize(int32_t size) override;
  75. int SetSendBufferSize(int32_t size) override;
  76. int Connect(net::CompletionOnceCallback callback) override;
  77. void Disconnect() override;
  78. bool IsConnected() const override;
  79. bool IsConnectedAndIdle() const override;
  80. int GetPeerAddress(net::IPEndPoint* address) const override;
  81. int GetLocalAddress(net::IPEndPoint* address) const override;
  82. const net::NetLogWithSource& NetLog() const override;
  83. bool WasEverUsed() const override;
  84. bool WasAlpnNegotiated() const override;
  85. net::NextProto GetNegotiatedProtocol() const override;
  86. bool GetSSLInfo(net::SSLInfo* ssl_info) override;
  87. int64_t GetTotalReceivedBytes() const override;
  88. void ApplySocketTag(const net::SocketTag& tag) override;
  89. private:
  90. enum State {
  91. STATE_PROXY_RESOLVE,
  92. STATE_PROXY_RESOLVE_COMPLETE,
  93. STATE_INIT_CONNECTION,
  94. STATE_INIT_CONNECTION_COMPLETE,
  95. STATE_DONE,
  96. STATE_NONE,
  97. };
  98. FRIEND_TEST_ALL_PREFIXES(ProxyResolvingClientSocketTest, ConnectToProxy);
  99. FRIEND_TEST_ALL_PREFIXES(ProxyResolvingClientSocketTest, ReadWriteErrors);
  100. FRIEND_TEST_ALL_PREFIXES(ProxyResolvingClientSocketTest,
  101. ResetSocketAfterTunnelAuth);
  102. void OnIOComplete(int result);
  103. int DoLoop(int result);
  104. int DoProxyResolve();
  105. int DoProxyResolveComplete(int result);
  106. int DoInitConnection();
  107. int DoInitConnectionComplete(int result);
  108. // net::ConnectJob::Delegate implementation:
  109. void OnConnectJobComplete(int result, net::ConnectJob* job) override;
  110. void OnNeedsProxyAuth(const net::HttpResponseInfo& response,
  111. net::HttpAuthController* auth_controller,
  112. base::OnceClosure restart_with_auth_callback,
  113. net::ConnectJob* job) override;
  114. int ReconsiderProxyAfterError(int error);
  115. raw_ptr<net::HttpNetworkSession> network_session_;
  116. raw_ptr<const net::CommonConnectJobParams> common_connect_job_params_;
  117. raw_ptr<const net::ConnectJobFactory> connect_job_factory_;
  118. std::unique_ptr<net::ConnectJob> connect_job_;
  119. std::unique_ptr<net::StreamSocket> socket_;
  120. std::unique_ptr<net::ProxyResolutionRequest> proxy_resolve_request_;
  121. net::ProxyInfo proxy_info_;
  122. const GURL url_;
  123. const net::NetworkIsolationKey network_isolation_key_;
  124. const bool use_tls_;
  125. net::NetLogWithSource net_log_;
  126. // The callback passed to Connect().
  127. net::CompletionOnceCallback user_connect_callback_;
  128. State next_state_;
  129. base::WeakPtrFactory<ProxyResolvingClientSocket> weak_factory_{this};
  130. };
  131. } // namespace network
  132. #endif // SERVICES_NETWORK_PROXY_RESOLVING_CLIENT_SOCKET_H_