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