spdy_proxy_client_socket.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_
  5. #define NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <string>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "net/base/completion_once_callback.h"
  14. #include "net/base/host_port_pair.h"
  15. #include "net/base/net_export.h"
  16. #include "net/base/proxy_server.h"
  17. #include "net/http/http_auth_controller.h"
  18. #include "net/http/http_request_headers.h"
  19. #include "net/http/http_request_info.h"
  20. #include "net/http/http_response_info.h"
  21. #include "net/http/proxy_client_socket.h"
  22. #include "net/log/net_log_source.h"
  23. #include "net/log/net_log_with_source.h"
  24. #include "net/spdy/spdy_http_stream.h"
  25. #include "net/spdy/spdy_read_queue.h"
  26. #include "net/spdy/spdy_session.h"
  27. #include "net/spdy/spdy_stream.h"
  28. #include "net/third_party/quiche/src/quiche/spdy/core/spdy_protocol.h"
  29. #include "net/traffic_annotation/network_traffic_annotation.h"
  30. namespace net {
  31. class IOBuffer;
  32. class ProxyDelegate;
  33. class SpdyStream;
  34. // Tunnels a stream socket over an HTTP/2 connection.
  35. class NET_EXPORT_PRIVATE SpdyProxyClientSocket : public ProxyClientSocket,
  36. public SpdyStream::Delegate {
  37. public:
  38. // Create a socket on top of the |spdy_stream| by sending a HEADERS CONNECT
  39. // frame for |endpoint|. After the response HEADERS frame is received, any
  40. // data read/written to the socket will be transferred in data frames. This
  41. // object will set itself as |spdy_stream|'s delegate.
  42. SpdyProxyClientSocket(const base::WeakPtr<SpdyStream>& spdy_stream,
  43. const ProxyServer& proxy_server,
  44. const std::string& user_agent,
  45. const HostPortPair& endpoint,
  46. const NetLogWithSource& source_net_log,
  47. scoped_refptr<HttpAuthController> auth_controller,
  48. ProxyDelegate* proxy_delegate);
  49. SpdyProxyClientSocket(const SpdyProxyClientSocket&) = delete;
  50. SpdyProxyClientSocket& operator=(const SpdyProxyClientSocket&) = delete;
  51. // On destruction Disconnect() is called.
  52. ~SpdyProxyClientSocket() override;
  53. // ProxyClientSocket methods:
  54. const HttpResponseInfo* GetConnectResponseInfo() const override;
  55. const scoped_refptr<HttpAuthController>& GetAuthController() const override;
  56. int RestartWithAuth(CompletionOnceCallback callback) override;
  57. void SetStreamPriority(RequestPriority priority) override;
  58. // StreamSocket implementation.
  59. int Connect(CompletionOnceCallback callback) override;
  60. void Disconnect() override;
  61. bool IsConnected() const override;
  62. bool IsConnectedAndIdle() const override;
  63. const NetLogWithSource& NetLog() const override;
  64. bool WasEverUsed() const override;
  65. bool WasAlpnNegotiated() const override;
  66. NextProto GetNegotiatedProtocol() const override;
  67. bool GetSSLInfo(SSLInfo* ssl_info) override;
  68. int64_t GetTotalReceivedBytes() const override;
  69. void ApplySocketTag(const SocketTag& tag) override;
  70. // Socket implementation.
  71. int Read(IOBuffer* buf,
  72. int buf_len,
  73. CompletionOnceCallback callback) override;
  74. int ReadIfReady(IOBuffer* buf,
  75. int buf_len,
  76. CompletionOnceCallback callback) override;
  77. int CancelReadIfReady() override;
  78. int Write(IOBuffer* buf,
  79. int buf_len,
  80. CompletionOnceCallback callback,
  81. const NetworkTrafficAnnotationTag& traffic_annotation) override;
  82. int SetReceiveBufferSize(int32_t size) override;
  83. int SetSendBufferSize(int32_t size) override;
  84. int GetPeerAddress(IPEndPoint* address) const override;
  85. int GetLocalAddress(IPEndPoint* address) const override;
  86. // SpdyStream::Delegate implementation.
  87. void OnHeadersSent() override;
  88. void OnEarlyHintsReceived(const spdy::Http2HeaderBlock& headers) override;
  89. void OnHeadersReceived(
  90. const spdy::Http2HeaderBlock& response_headers,
  91. const spdy::Http2HeaderBlock* pushed_request_headers) override;
  92. void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override;
  93. void OnDataSent() override;
  94. void OnTrailers(const spdy::Http2HeaderBlock& trailers) override;
  95. void OnClose(int status) override;
  96. bool CanGreaseFrameType() const override;
  97. NetLogSource source_dependency() const override;
  98. private:
  99. enum State {
  100. STATE_DISCONNECTED,
  101. STATE_GENERATE_AUTH_TOKEN,
  102. STATE_GENERATE_AUTH_TOKEN_COMPLETE,
  103. STATE_SEND_REQUEST,
  104. STATE_SEND_REQUEST_COMPLETE,
  105. STATE_READ_REPLY_COMPLETE,
  106. STATE_OPEN,
  107. STATE_CLOSED
  108. };
  109. // Calls |callback.Run(result)|. Used to run a callback posted to the
  110. // message loop.
  111. void RunWriteCallback(CompletionOnceCallback callback, int result) const;
  112. void OnIOComplete(int result);
  113. int DoLoop(int last_io_result);
  114. int DoGenerateAuthToken();
  115. int DoGenerateAuthTokenComplete(int result);
  116. int DoSendRequest();
  117. int DoSendRequestComplete(int result);
  118. int DoReadReplyComplete(int result);
  119. // Populates |user_buffer_| with as much read data as possible
  120. // and returns the number of bytes read.
  121. size_t PopulateUserReadBuffer(char* out, size_t len);
  122. // Called when the peer sent END_STREAM.
  123. void MaybeSendEndStream();
  124. State next_state_ = STATE_DISCONNECTED;
  125. // Pointer to the SPDY Stream that this sits on top of.
  126. base::WeakPtr<SpdyStream> spdy_stream_;
  127. // Stores the callback to the layer above, called on completing Read() or
  128. // Connect().
  129. CompletionOnceCallback read_callback_;
  130. // Stores the callback to the layer above, called on completing Write().
  131. CompletionOnceCallback write_callback_;
  132. // CONNECT request and response.
  133. HttpRequestInfo request_;
  134. HttpResponseInfo response_;
  135. // The hostname and port of the endpoint. This is not necessarily the one
  136. // specified by the URL, due to Alternate-Protocol or fixed testing ports.
  137. const HostPortPair endpoint_;
  138. scoped_refptr<HttpAuthController> auth_;
  139. const ProxyServer proxy_server_;
  140. // This delegate must outlive this proxy client socket.
  141. const raw_ptr<ProxyDelegate> proxy_delegate_;
  142. std::string user_agent_;
  143. // We buffer the response body as it arrives asynchronously from the stream.
  144. SpdyReadQueue read_buffer_queue_;
  145. // User provided buffer for the Read() response.
  146. scoped_refptr<IOBuffer> user_buffer_;
  147. size_t user_buffer_len_ = 0;
  148. // User specified number of bytes to be written.
  149. int write_buffer_len_ = 0;
  150. // True if the transport socket has ever sent data.
  151. bool was_ever_used_ = false;
  152. const NetLogWithSource net_log_;
  153. const NetLogSource source_dependency_;
  154. // State for handling END_STREAM. When the peer sends a DATA frame with
  155. // END_STREAM, it should be treated as being equivalent to the TCP FIN bit.
  156. // We should send a DATA frame with END_STREAM after receiving END_STREAM
  157. // as the spec requires.
  158. enum class EndStreamState {
  159. kNone,
  160. kEndStreamReceived,
  161. kEndStreamSent,
  162. };
  163. EndStreamState end_stream_state_ = EndStreamState::kNone;
  164. // The default weak pointer factory.
  165. base::WeakPtrFactory<SpdyProxyClientSocket> weak_factory_{this};
  166. // Only used for posting write callbacks. Weak pointers created by this
  167. // factory are invalidated in Disconnect().
  168. base::WeakPtrFactory<SpdyProxyClientSocket> write_callback_weak_factory_{
  169. this};
  170. };
  171. } // namespace net
  172. #endif // NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_