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 
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 "net/base/completion_once_callback.h"
13 #include "net/base/load_timing_info.h"
14 #include "net/base/proxy_server.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 provides a socket interface to 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 ProxyServer& proxy_server,
38       const std::string& user_agent,
39       const HostPortPair& endpoint,
40       const NetLogWithSource& net_log,
41       HttpAuthController* auth_controller,
42       ProxyDelegate* proxy_delegate);
43 
44   // On destruction Disconnect() is called.
45   ~QuicProxyClientSocket() override;
46 
47   // ProxyClientSocket methods:
48   const HttpResponseInfo* GetConnectResponseInfo() const override;
49   const scoped_refptr<HttpAuthController>& GetAuthController() const override;
50   int RestartWithAuth(CompletionOnceCallback callback) override;
51   bool IsUsingSpdy() const override;
52   NextProto GetProxyNegotiatedProtocol() const override;
53   void SetStreamPriority(RequestPriority priority) override;
54 
55   // StreamSocket implementation.
56   int Connect(CompletionOnceCallback callback) override;
57   void Disconnect() override;
58   bool IsConnected() const override;
59   bool IsConnectedAndIdle() const override;
60   const NetLogWithSource& NetLog() const override;
61   bool WasEverUsed() const override;
62   bool WasAlpnNegotiated() const override;
63   NextProto GetNegotiatedProtocol() const override;
64   bool GetSSLInfo(SSLInfo* ssl_info) override;
65   void GetConnectionAttempts(ConnectionAttempts* out) const override;
ClearConnectionAttempts()66   void ClearConnectionAttempts() override {}
AddConnectionAttempts(const ConnectionAttempts & attempts)67   void AddConnectionAttempts(const ConnectionAttempts& attempts) override {}
68   int64_t GetTotalReceivedBytes() const override;
69   void ApplySocketTag(const SocketTag& tag) override;
70 
71   // Socket implementation.
72   int Read(IOBuffer* buf,
73            int buf_len,
74            CompletionOnceCallback callback) override;
75   int Write(IOBuffer* buf,
76             int buf_len,
77             CompletionOnceCallback callback,
78             const NetworkTrafficAnnotationTag& traffic_annotation) override;
79   int SetReceiveBufferSize(int32_t size) override;
80   int SetSendBufferSize(int32_t size) override;
81   int GetPeerAddress(IPEndPoint* address) const override;
82   int GetLocalAddress(IPEndPoint* address) const override;
83 
84  private:
85   enum State {
86     STATE_DISCONNECTED,
87     STATE_GENERATE_AUTH_TOKEN,
88     STATE_GENERATE_AUTH_TOKEN_COMPLETE,
89     STATE_SEND_REQUEST,
90     STATE_SEND_REQUEST_COMPLETE,
91     STATE_READ_REPLY,
92     STATE_READ_REPLY_COMPLETE,
93     STATE_CONNECT_COMPLETE
94   };
95 
96   void OnIOComplete(int result);  // Callback used during connecting
97   void OnReadComplete(int rv);
98   void OnWriteComplete(int rv);
99 
100   // Callback for stream_->ReadInitialHeaders()
101   void OnReadResponseHeadersComplete(int result);
102   int ProcessResponseHeaders(const spdy::SpdyHeaderBlock& headers);
103 
104   int DoLoop(int last_io_result);
105   int DoGenerateAuthToken();
106   int DoGenerateAuthTokenComplete(int result);
107   int DoSendRequest();
108   int DoSendRequestComplete(int result);
109   int DoReadReply();
110   int DoReadReplyComplete(int result);
111 
112   bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const;
113 
114   State next_state_;
115 
116   // Handle to the QUIC Stream that this sits on top of.
117   std::unique_ptr<QuicChromiumClientStream::Handle> stream_;
118 
119   // Handle to the session that |stream_| belongs to.
120   std::unique_ptr<QuicChromiumClientSession::Handle> session_;
121 
122   // Stores the callback for Connect().
123   CompletionOnceCallback connect_callback_;
124   // Stores the callback for Read().
125   CompletionOnceCallback read_callback_;
126   // Stores the read buffer pointer for Read().
127   IOBuffer* read_buf_;
128   // Stores the callback for Write().
129   CompletionOnceCallback write_callback_;
130   // Stores the write buffer length for Write().
131   int write_buf_len_;
132 
133   // CONNECT request and response.
134   HttpRequestInfo request_;
135   HttpResponseInfo response_;
136 
137   spdy::SpdyHeaderBlock response_header_block_;
138 
139   // The hostname and port of the endpoint.  This is not necessarily the one
140   // specified by the URL, due to Alternate-Protocol or fixed testing ports.
141   const HostPortPair endpoint_;
142   scoped_refptr<HttpAuthController> auth_;
143 
144   const ProxyServer proxy_server_;
145 
146   // This delegate must outlive this proxy client socket.
147   ProxyDelegate* const proxy_delegate_;
148 
149   std::string user_agent_;
150 
151   // Session connect timing info.
152   LoadTimingInfo::ConnectTiming connect_timing_;
153 
154   const NetLogWithSource net_log_;
155 
156   // The default weak pointer factory.
157   base::WeakPtrFactory<QuicProxyClientSocket> weak_factory_{this};
158 
159   DISALLOW_COPY_AND_ASSIGN(QuicProxyClientSocket);
160 };
161 
162 }  // namespace net
163 
164 #endif  // NET_QUIC_QUIC_PROXY_CLIENT_SOCKET_H_
165