1 // Copyright (c) 2012 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.
5 // A toy client, which connects to a specified port and sends QUIC
6 // request to that endpoint.
8 #ifndef NET_TOOLS_QUIC_QUIC_SIMPLE_CLIENT_H_
9 #define NET_TOOLS_QUIC_QUIC_SIMPLE_CLIENT_H_
13 #include "base/basictypes.h"
14 #include "base/command_line.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/strings/string_piece.h"
17 #include "net/base/io_buffer.h"
18 #include "net/base/ip_endpoint.h"
19 #include "net/http/http_response_headers.h"
20 #include "net/log/net_log.h"
21 #include "net/quic/crypto/crypto_handshake.h"
22 #include "net/quic/quic_config.h"
23 #include "net/quic/quic_framer.h"
24 #include "net/quic/quic_packet_creator.h"
25 #include "net/quic/quic_packet_reader.h"
26 #include "net/tools/quic/quic_client_session.h"
27 #include "net/tools/quic/quic_spdy_client_stream.h"
31 struct HttpRequestInfo
;
34 class QuicConnectionHelper
;
35 class UDPClientSocket
;
43 class QuicSimpleClient
: public QuicDataStream::Visitor
,
44 public QuicPacketReader::Visitor
{
46 class ResponseListener
{
49 virtual ~ResponseListener() {}
50 virtual void OnCompleteResponse(QuicStreamId id
,
51 const HttpResponseHeaders
& response_headers
,
52 const std::string
& response_body
) = 0;
55 // Create a quic client, which will have events managed by an externally owned
57 QuicSimpleClient(IPEndPoint server_address
,
58 const QuicServerId
& server_id
,
59 const QuicVersionVector
& supported_versions
);
60 QuicSimpleClient(IPEndPoint server_address
,
61 const QuicServerId
& server_id
,
62 const QuicVersionVector
& supported_versions
,
63 const QuicConfig
& config
);
65 ~QuicSimpleClient() override
;
67 // Initializes the client to create a connection. Should be called exactly
68 // once before calling StartConnect or Connect. Returns true if the
69 // initialization succeeds, false otherwise.
72 // "Connect" to the QUIC server, including performing synchronous crypto
76 // Start the crypto handshake. This can be done in place of the synchronous
77 // Connect(), but callers are responsible for making sure the crypto handshake
81 // Returns true if the crypto handshake has yet to establish encryption.
82 // Returns false if encryption is active (even if the server hasn't confirmed
83 // the handshake) or if the connection has been closed.
84 bool EncryptionBeingEstablished();
86 // Disconnects from the QUIC server.
89 // Sends an HTTP request and does not wait for response before returning.
90 void SendRequest(const HttpRequestInfo
& headers
,
91 base::StringPiece body
,
94 // Sends an HTTP request and waits for response before returning.
95 void SendRequestAndWaitForResponse(const HttpRequestInfo
& headers
,
96 base::StringPiece body
,
99 // Sends a request simple GET for each URL in |args|, and then waits for
101 void SendRequestsAndWaitForResponse(
102 const base::CommandLine::StringVector
& url_list
);
104 // Returns a newly created QuicSpdyClientStream, owned by the
106 QuicSpdyClientStream
* CreateReliableClientStream();
108 // Wait for events until the stream with the given ID is closed.
109 void WaitForStreamToClose(QuicStreamId id
);
111 // Wait for events until the handshake is confirmed.
112 void WaitForCryptoHandshakeConfirmed();
114 // Wait up to 50ms, and handle any events which occur.
115 // Returns true if there are any outstanding requests.
116 bool WaitForEvents();
118 // QuicPacketReader::Visitor
119 void OnReadError(int result
) override
;
120 bool OnPacket(const QuicEncryptedPacket
& packet
,
121 IPEndPoint local_address
,
122 IPEndPoint peer_address
) override
;
124 // QuicDataStream::Visitor
125 void OnClose(QuicDataStream
* stream
) override
;
127 QuicClientSession
* session() { return session_
.get(); }
129 bool connected() const;
130 bool goaway_received() const;
132 void set_bind_to_address(IPAddressNumber address
) {
133 bind_to_address_
= address
;
136 IPAddressNumber
bind_to_address() const { return bind_to_address_
; }
138 void set_local_port(int local_port
) { local_port_
= local_port
; }
140 const IPEndPoint
& server_address() const { return server_address_
; }
142 const IPEndPoint
& client_address() const { return client_address_
; }
144 const QuicServerId
& server_id() const { return server_id_
; }
146 // This should only be set before the initial Connect()
147 void set_server_id(const QuicServerId
& server_id
) {
148 server_id_
= server_id
;
151 void SetUserAgentID(const std::string
& user_agent_id
) {
152 crypto_config_
.set_user_agent_id(user_agent_id
);
155 // SetProofVerifier sets the ProofVerifier that will be used to verify the
156 // server's certificate and takes ownership of |verifier|.
157 void SetProofVerifier(ProofVerifier
* verifier
) {
158 // TODO(rtenneti): We should set ProofVerifier in QuicClientSession.
159 crypto_config_
.SetProofVerifier(verifier
);
162 // SetChannelIDSource sets a ChannelIDSource that will be called, when the
163 // server supports channel IDs, to obtain a channel ID for signing a message
164 // proving possession of the channel ID. This object takes ownership of
166 void SetChannelIDSource(ChannelIDSource
* source
) {
167 crypto_config_
.SetChannelIDSource(source
);
170 void SetSupportedVersions(const QuicVersionVector
& versions
) {
171 supported_versions_
= versions
;
174 // Takes ownership of the listener.
175 void set_response_listener(ResponseListener
* listener
) {
176 response_listener_
.reset(listener
);
179 QuicConfig
* config() { return &config_
; }
181 void set_store_response(bool val
) { store_response_
= val
; }
183 size_t latest_response_code() const;
184 const std::string
& latest_response_headers() const;
185 const std::string
& latest_response_body() const;
188 virtual QuicConnectionId
GenerateConnectionId();
189 virtual QuicConnectionHelper
* CreateQuicConnectionHelper();
190 virtual QuicPacketWriter
* CreateQuicPacketWriter();
192 virtual QuicClientSession
* CreateQuicClientSession(
193 const QuicConfig
& config
,
194 QuicConnection
* connection
,
195 const QuicServerId
& server_id
,
196 QuicCryptoClientConfig
* crypto_config
);
199 friend class net::tools::test::QuicClientPeer
;
201 // A packet writer factory that always returns the same writer
202 class DummyPacketWriterFactory
: public QuicConnection::PacketWriterFactory
{
204 DummyPacketWriterFactory(QuicPacketWriter
* writer
);
205 ~DummyPacketWriterFactory() override
;
207 QuicPacketWriter
* Create(QuicConnection
* connection
) const override
;
210 QuicPacketWriter
* writer_
;
213 // Used during initialization: creates the UDP socket FD, sets socket options,
214 // and binds the socket to our address.
215 bool CreateUDPSocket();
217 // Read a UDP packet and hand it to the framer.
218 bool ReadAndProcessPacket();
220 // Used by |helper_| to time alarms.
223 // Address of the server.
224 const IPEndPoint server_address_
;
226 // |server_id_| is a tuple (hostname, port, is_https) of the server.
227 QuicServerId server_id_
;
229 // config_ and crypto_config_ contain configuration and cached state about
232 QuicCryptoClientConfig crypto_config_
;
234 // Address of the client if the client is connected to the server.
235 IPEndPoint client_address_
;
237 // If initialized, the address to bind to.
238 IPAddressNumber bind_to_address_
;
239 // Local port to bind to. Initialize to 0.
242 // Writer used to actually send packets to the wire. Needs to outlive
244 scoped_ptr
<QuicPacketWriter
> writer_
;
246 // Session which manages streams.
247 scoped_ptr
<QuicClientSession
> session_
;
249 // UDP socket connected to the server.
250 scoped_ptr
<UDPClientSocket
> socket_
;
252 // Connection on the socket. Owned by |session_|.
253 QuicConnection
* connection_
;
255 // Helper to be used by created connections.
256 scoped_ptr
<QuicConnectionHelper
> helper_
;
258 // Listens for full responses.
259 scoped_ptr
<ResponseListener
> response_listener_
;
261 // Tracks if the client is initialized to connect.
264 // If overflow_supported_ is true, this will be the number of packets dropped
265 // during the lifetime of the server.
266 QuicPacketCount packets_dropped_
;
268 // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped
269 // because the socket would otherwise overflow.
270 bool overflow_supported_
;
272 // This vector contains QUIC versions which we currently support.
273 // This should be ordered such that the highest supported version is the first
274 // element, with subsequent elements in descending order (versions can be
275 // skipped as necessary). We will always pick supported_versions_[0] as the
276 // initial version to use.
277 QuicVersionVector supported_versions_
;
279 // If true, store the latest response code, headers, and body.
280 bool store_response_
;
281 // HTTP response code from most recent response.
282 size_t latest_response_code_
;
283 // HTTP headers from most recent response.
284 std::string latest_response_headers_
;
285 // Body of most recent response.
286 std::string latest_response_body_
;
288 // The log used for the sockets.
291 scoped_ptr
<QuicPacketReader
> packet_reader_
;
293 base::WeakPtrFactory
<QuicSimpleClient
> weak_factory_
;
295 DISALLOW_COPY_AND_ASSIGN(QuicSimpleClient
);
301 #endif // NET_TOOLS_QUIC_QUIC_SIMPLE_CLIENT_H_