quic_proxy_client_socket.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (c) 2017 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_QUIC_QUIC_PROXY_CLIENT_SOCKET_H_
  5. #define NET_QUIC_QUIC_PROXY_CLIENT_SOCKET_H_
  6. #include <cstdio>
  7. #include <memory>
  8. #include <string>
  9. #include "base/memory/raw_ptr.h"
  10. #include "net/base/completion_once_callback.h"
  11. #include "net/base/proxy_server.h"
  12. #include "net/http/proxy_client_socket.h"
  13. #include "net/quic/quic_chromium_client_session.h"
  14. #include "net/quic/quic_chromium_client_stream.h"
  15. #include "net/spdy/spdy_read_queue.h"
  16. #include "net/traffic_annotation/network_traffic_annotation.h"
  17. namespace net {
  18. class HttpAuthController;
  19. class ProxyDelegate;
  20. // QuicProxyClientSocket tunnels a stream socket over an underlying
  21. // QuicChromiumClientStream. Bytes written to/read from a QuicProxyClientSocket
  22. // are sent/received via STREAM frames in the underlying QUIC stream.
  23. class NET_EXPORT_PRIVATE QuicProxyClientSocket : public ProxyClientSocket {
  24. public:
  25. // Create a socket on top of the |stream| by sending a HEADERS CONNECT
  26. // frame for |endpoint|. After the response HEADERS frame is received, any
  27. // data read/written to the socket will be transferred in STREAM frames.
  28. QuicProxyClientSocket(
  29. std::unique_ptr<QuicChromiumClientStream::Handle> stream,
  30. std::unique_ptr<QuicChromiumClientSession::Handle> session,
  31. const ProxyServer& proxy_server,
  32. const std::string& user_agent,
  33. const HostPortPair& endpoint,
  34. const NetLogWithSource& net_log,
  35. scoped_refptr<HttpAuthController> auth_controller,
  36. ProxyDelegate* proxy_delegate);
  37. QuicProxyClientSocket(const QuicProxyClientSocket&) = delete;
  38. QuicProxyClientSocket& operator=(const QuicProxyClientSocket&) = delete;
  39. // On destruction Disconnect() is called.
  40. ~QuicProxyClientSocket() override;
  41. // ProxyClientSocket methods:
  42. const HttpResponseInfo* GetConnectResponseInfo() const override;
  43. const scoped_refptr<HttpAuthController>& GetAuthController() const override;
  44. int RestartWithAuth(CompletionOnceCallback callback) override;
  45. void SetStreamPriority(RequestPriority priority) override;
  46. // StreamSocket implementation.
  47. int Connect(CompletionOnceCallback callback) override;
  48. void Disconnect() override;
  49. bool IsConnected() const override;
  50. bool IsConnectedAndIdle() const override;
  51. const NetLogWithSource& NetLog() const override;
  52. bool WasEverUsed() const override;
  53. bool WasAlpnNegotiated() const override;
  54. NextProto GetNegotiatedProtocol() const override;
  55. bool GetSSLInfo(SSLInfo* ssl_info) override;
  56. int64_t GetTotalReceivedBytes() const override;
  57. void ApplySocketTag(const SocketTag& tag) override;
  58. // Socket implementation.
  59. int Read(IOBuffer* buf,
  60. int buf_len,
  61. CompletionOnceCallback callback) override;
  62. int Write(IOBuffer* buf,
  63. int buf_len,
  64. CompletionOnceCallback callback,
  65. const NetworkTrafficAnnotationTag& traffic_annotation) override;
  66. int SetReceiveBufferSize(int32_t size) override;
  67. int SetSendBufferSize(int32_t size) override;
  68. int GetPeerAddress(IPEndPoint* address) const override;
  69. int GetLocalAddress(IPEndPoint* address) const override;
  70. private:
  71. enum State {
  72. STATE_DISCONNECTED,
  73. STATE_GENERATE_AUTH_TOKEN,
  74. STATE_GENERATE_AUTH_TOKEN_COMPLETE,
  75. STATE_SEND_REQUEST,
  76. STATE_SEND_REQUEST_COMPLETE,
  77. STATE_READ_REPLY,
  78. STATE_READ_REPLY_COMPLETE,
  79. STATE_CONNECT_COMPLETE
  80. };
  81. void OnIOComplete(int result); // Callback used during connecting
  82. void OnReadComplete(int rv);
  83. void OnWriteComplete(int rv);
  84. // Callback for stream_->ReadInitialHeaders()
  85. void OnReadResponseHeadersComplete(int result);
  86. int ProcessResponseHeaders(const spdy::Http2HeaderBlock& headers);
  87. int DoLoop(int last_io_result);
  88. int DoGenerateAuthToken();
  89. int DoGenerateAuthTokenComplete(int result);
  90. int DoSendRequest();
  91. int DoSendRequestComplete(int result);
  92. int DoReadReply();
  93. int DoReadReplyComplete(int result);
  94. State next_state_ = STATE_DISCONNECTED;
  95. // Handle to the QUIC Stream that this sits on top of.
  96. std::unique_ptr<QuicChromiumClientStream::Handle> stream_;
  97. // Handle to the session that |stream_| belongs to.
  98. std::unique_ptr<QuicChromiumClientSession::Handle> session_;
  99. // Stores the callback for Connect().
  100. CompletionOnceCallback connect_callback_;
  101. // Stores the callback for Read().
  102. CompletionOnceCallback read_callback_;
  103. // Stores the read buffer pointer for Read().
  104. raw_ptr<IOBuffer> read_buf_ = nullptr;
  105. // Stores the callback for Write().
  106. CompletionOnceCallback write_callback_;
  107. // Stores the write buffer length for Write().
  108. int write_buf_len_ = 0;
  109. // CONNECT request and response.
  110. HttpRequestInfo request_;
  111. HttpResponseInfo response_;
  112. spdy::Http2HeaderBlock response_header_block_;
  113. // The hostname and port of the endpoint. This is not necessarily the one
  114. // specified by the URL, due to Alternate-Protocol or fixed testing ports.
  115. const HostPortPair endpoint_;
  116. scoped_refptr<HttpAuthController> auth_;
  117. const ProxyServer proxy_server_;
  118. // This delegate must outlive this proxy client socket.
  119. const raw_ptr<ProxyDelegate> proxy_delegate_;
  120. std::string user_agent_;
  121. const NetLogWithSource net_log_;
  122. // The default weak pointer factory.
  123. base::WeakPtrFactory<QuicProxyClientSocket> weak_factory_{this};
  124. };
  125. } // namespace net
  126. #endif // NET_QUIC_QUIC_PROXY_CLIENT_SOCKET_H_