xref: /aosp_15_r20/external/cronet/net/http/http_proxy_client_socket.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 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_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_
6 #define NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <string>
12 
13 #include "base/memory/raw_ptr.h"
14 #include "base/memory/scoped_refptr.h"
15 #include "net/base/completion_once_callback.h"
16 #include "net/base/completion_repeating_callback.h"
17 #include "net/base/host_port_pair.h"
18 #include "net/base/net_export.h"
19 #include "net/base/proxy_chain.h"
20 #include "net/http/http_auth_controller.h"
21 #include "net/http/http_request_headers.h"
22 #include "net/http/http_request_info.h"
23 #include "net/http/http_response_info.h"
24 #include "net/http/proxy_client_socket.h"
25 #include "net/log/net_log_with_source.h"
26 #include "net/socket/ssl_client_socket.h"
27 #include "net/traffic_annotation/network_traffic_annotation.h"
28 
29 namespace net {
30 
31 class GrowableIOBuffer;
32 class HttpStreamParser;
33 class IOBuffer;
34 class ProxyDelegate;
35 class StreamSocket;
36 
37 // Tunnels a stream socket over an HTTP/1.1 connection.
38 //
39 // This class handles only _tunneled_ proxy connections, using `CONNECT`.
40 // Un-tunneled proxy requests are handled in the HTTP stream layer,
41 // specifically in `HttpBasicStream`.
42 class NET_EXPORT_PRIVATE HttpProxyClientSocket : public ProxyClientSocket {
43  public:
44   // Takes ownership of |socket|, which should already be connected by the time
45   // Connect() is called. |socket| is assumed to be a fresh socket.
46   HttpProxyClientSocket(std::unique_ptr<StreamSocket> socket,
47                         const std::string& user_agent,
48                         const HostPortPair& endpoint,
49                         const ProxyChain& proxy_chain,
50                         size_t proxy_chain_index,
51                         scoped_refptr<HttpAuthController> http_auth_controller,
52                         ProxyDelegate* proxy_delegate,
53                         const NetworkTrafficAnnotationTag& traffic_annotation);
54 
55   HttpProxyClientSocket(const HttpProxyClientSocket&) = delete;
56   HttpProxyClientSocket& operator=(const HttpProxyClientSocket&) = delete;
57 
58   // On destruction Disconnect() is called.
59   ~HttpProxyClientSocket() override;
60 
61   // ProxyClientSocket implementation.
62   const HttpResponseInfo* GetConnectResponseInfo() const override;
63   int RestartWithAuth(CompletionOnceCallback callback) override;
64   const scoped_refptr<HttpAuthController>& GetAuthController() const override;
65 
66   // StreamSocket implementation.
67   int Connect(CompletionOnceCallback callback) override;
68   void Disconnect() override;
69   bool IsConnected() const override;
70   bool IsConnectedAndIdle() const override;
71   const NetLogWithSource& NetLog() const override;
72   bool WasEverUsed() const override;
73   NextProto GetNegotiatedProtocol() const override;
74   bool GetSSLInfo(SSLInfo* ssl_info) override;
75   int64_t GetTotalReceivedBytes() const override;
76   void ApplySocketTag(const SocketTag& tag) override;
77 
78   // Socket implementation.
79   int Read(IOBuffer* buf,
80            int buf_len,
81            CompletionOnceCallback callback) override;
82   int ReadIfReady(IOBuffer* buf,
83                   int buf_len,
84                   CompletionOnceCallback callback) override;
85   int CancelReadIfReady() override;
86   int Write(IOBuffer* buf,
87             int buf_len,
88             CompletionOnceCallback callback,
89             const NetworkTrafficAnnotationTag& traffic_annotation) override;
90   int SetReceiveBufferSize(int32_t size) override;
91   int SetSendBufferSize(int32_t size) override;
92   int GetPeerAddress(IPEndPoint* address) const override;
93   int GetLocalAddress(IPEndPoint* address) const override;
94 
95  private:
96   enum State {
97     STATE_NONE,
98     STATE_GENERATE_AUTH_TOKEN,
99     STATE_GENERATE_AUTH_TOKEN_COMPLETE,
100     STATE_SEND_REQUEST,
101     STATE_SEND_REQUEST_COMPLETE,
102     STATE_READ_HEADERS,
103     STATE_READ_HEADERS_COMPLETE,
104     STATE_DRAIN_BODY,
105     STATE_DRAIN_BODY_COMPLETE,
106     STATE_DONE,
107   };
108 
109   // The size in bytes of the buffer we use to drain the response body that
110   // we want to throw away.  The response body is typically a small error
111   // page just a few hundred bytes long.
112   static const int kDrainBodyBufferSize = 1024;
113 
114   int PrepareForAuthRestart();
115   int DidDrainBodyForAuthRestart();
116 
117   void DoCallback(int result);
118   void OnIOComplete(int result);
119 
120   int DoLoop(int last_io_result);
121   int DoGenerateAuthToken();
122   int DoGenerateAuthTokenComplete(int result);
123   int DoSendRequest();
124   int DoSendRequestComplete(int result);
125   int DoReadHeaders();
126   int DoReadHeadersComplete(int result);
127   int DoDrainBody();
128   int DoDrainBodyComplete(int result);
129 
130   // Returns whether |next_state_| is STATE_DONE.
131   bool CheckDone();
132 
133   CompletionRepeatingCallback io_callback_;
134   State next_state_ = STATE_NONE;
135 
136   // Stores the callback provided by the caller of async operations.
137   CompletionOnceCallback user_callback_;
138 
139   HttpRequestInfo request_;
140   HttpResponseInfo response_;
141 
142   scoped_refptr<GrowableIOBuffer> parser_buf_;
143   scoped_refptr<IOBuffer> drain_buf_;
144   std::unique_ptr<StreamSocket> socket_;
145   std::unique_ptr<HttpStreamParser> http_stream_parser_;
146 
147   // Whether or not |socket_| has been previously used. Once auth credentials
148   // are sent, set to true.
149   bool is_reused_ = false;
150 
151   // The hostname and port of the endpoint.  This is not necessarily the one
152   // specified by the URL, due to Alternate-Protocol or fixed testing ports.
153   const HostPortPair endpoint_;
154   scoped_refptr<HttpAuthController> auth_;
155 
156   std::string request_line_;
157   HttpRequestHeaders request_headers_;
158 
159   const ProxyChain proxy_chain_;
160   const size_t proxy_chain_index_;
161 
162   // This delegate must outlive this proxy client socket.
163   raw_ptr<ProxyDelegate> proxy_delegate_;
164 
165   // Network traffic annotation for handshaking and setup.
166   const NetworkTrafficAnnotationTag traffic_annotation_;
167 
168   const NetLogWithSource net_log_;
169 };
170 
171 }  // namespace net
172 
173 #endif  // NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_
174