• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/third_party/quiche/src/quiche/common/http/http_header_block.h"
20 #include "net/traffic_annotation/network_traffic_annotation.h"
21 
22 namespace net {
23 
24 class HttpAuthController;
25 class ProxyDelegate;
26 
27 // QuicProxyClientSocket tunnels a stream socket over an underlying
28 // QuicChromiumClientStream. Bytes written to/read from a QuicProxyClientSocket
29 // are sent/received via STREAM frames in the underlying QUIC stream.
30 class NET_EXPORT_PRIVATE QuicProxyClientSocket : public ProxyClientSocket {
31  public:
32   // Create a socket on top of the |stream| by sending a HEADERS CONNECT
33   // frame for |endpoint|.  After the response HEADERS frame is received, any
34   // data read/written to the socket will be transferred in STREAM frames.
35   QuicProxyClientSocket(
36       std::unique_ptr<QuicChromiumClientStream::Handle> stream,
37       std::unique_ptr<QuicChromiumClientSession::Handle> session,
38       const ProxyChain& proxy_chain,
39       size_t proxy_chain_index,
40       const std::string& user_agent,
41       const HostPortPair& endpoint,
42       const NetLogWithSource& net_log,
43       scoped_refptr<HttpAuthController> auth_controller,
44       ProxyDelegate* proxy_delegate);
45 
46   QuicProxyClientSocket(const QuicProxyClientSocket&) = delete;
47   QuicProxyClientSocket& operator=(const QuicProxyClientSocket&) = delete;
48 
49   // On destruction Disconnect() is called.
50   ~QuicProxyClientSocket() override;
51 
52   // ProxyClientSocket methods:
53   const HttpResponseInfo* GetConnectResponseInfo() const override;
54   const scoped_refptr<HttpAuthController>& GetAuthController() const override;
55   int RestartWithAuth(CompletionOnceCallback callback) override;
56   void SetStreamPriority(RequestPriority priority) override;
57 
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   NextProto GetNegotiatedProtocol() const override;
66   bool GetSSLInfo(SSLInfo* ssl_info) override;
67   int64_t GetTotalReceivedBytes() const override;
68   void ApplySocketTag(const SocketTag& tag) override;
69 
70   // Socket implementation.
71   int Read(IOBuffer* buf,
72            int buf_len,
73            CompletionOnceCallback callback) override;
74   int Write(IOBuffer* buf,
75             int buf_len,
76             CompletionOnceCallback callback,
77             const NetworkTrafficAnnotationTag& traffic_annotation) override;
78   int SetReceiveBufferSize(int32_t size) override;
79   int SetSendBufferSize(int32_t size) override;
80   int GetPeerAddress(IPEndPoint* address) const override;
81   int GetLocalAddress(IPEndPoint* address) const override;
82 
83  private:
84   enum State {
85     STATE_DISCONNECTED,
86     STATE_GENERATE_AUTH_TOKEN,
87     STATE_GENERATE_AUTH_TOKEN_COMPLETE,
88     STATE_SEND_REQUEST,
89     STATE_SEND_REQUEST_COMPLETE,
90     STATE_READ_REPLY,
91     STATE_READ_REPLY_COMPLETE,
92     STATE_CONNECT_COMPLETE
93   };
94 
95   void OnIOComplete(int result);  // Callback used during connecting
96   void OnReadComplete(int rv);
97   void OnWriteComplete(int rv);
98 
99   // Callback for stream_->ReadInitialHeaders()
100   void OnReadResponseHeadersComplete(int result);
101   int ProcessResponseHeaders(const quiche::HttpHeaderBlock& headers);
102 
103   int DoLoop(int last_io_result);
104   int DoGenerateAuthToken();
105   int DoGenerateAuthTokenComplete(int result);
106   int DoSendRequest();
107   int DoSendRequestComplete(int result);
108   int DoReadReply();
109   int DoReadReplyComplete(int result);
110 
111   State next_state_ = STATE_DISCONNECTED;
112 
113   // Handle to the QUIC Stream that this sits on top of.
114   std::unique_ptr<QuicChromiumClientStream::Handle> stream_;
115 
116   // Handle to the session that |stream_| belongs to.
117   std::unique_ptr<QuicChromiumClientSession::Handle> session_;
118 
119   // Stores the callback for Connect().
120   CompletionOnceCallback connect_callback_;
121   // Stores the callback for Read().
122   CompletionOnceCallback read_callback_;
123   // Stores the read buffer pointer for Read().
124   raw_ptr<IOBuffer> read_buf_ = nullptr;
125   // Stores the callback for Write().
126   CompletionOnceCallback write_callback_;
127   // Stores the write buffer length for Write().
128   int write_buf_len_ = 0;
129 
130   // CONNECT request and response.
131   HttpRequestInfo request_;
132   HttpResponseInfo response_;
133 
134   quiche::HttpHeaderBlock response_header_block_;
135 
136   // The hostname and port of the endpoint.  This is not necessarily the one
137   // specified by the URL, due to Alternate-Protocol or fixed testing ports.
138   const HostPortPair endpoint_;
139   scoped_refptr<HttpAuthController> auth_;
140 
141   const ProxyChain proxy_chain_;
142   const size_t proxy_chain_index_;
143 
144   // This delegate must outlive this proxy client socket.
145   const raw_ptr<ProxyDelegate> proxy_delegate_;
146 
147   std::string user_agent_;
148 
149   const NetLogWithSource net_log_;
150 
151   // The default weak pointer factory.
152   base::WeakPtrFactory<QuicProxyClientSocket> weak_factory_{this};
153 };
154 
155 }  // namespace net
156 
157 #endif  // NET_QUIC_QUIC_PROXY_CLIENT_SOCKET_H_
158