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();
193 friend class net::tools::test::QuicClientPeer
;
195 // A packet writer factory that always returns the same writer
196 class DummyPacketWriterFactory
: public QuicConnection::PacketWriterFactory
{
198 DummyPacketWriterFactory(QuicPacketWriter
* writer
);
199 ~DummyPacketWriterFactory() override
;
201 QuicPacketWriter
* Create(QuicConnection
* connection
) const override
;
204 QuicPacketWriter
* writer_
;
207 // Used during initialization: creates the UDP socket FD, sets socket options,
208 // and binds the socket to our address.
209 bool CreateUDPSocket();
211 // Read a UDP packet and hand it to the framer.
212 bool ReadAndProcessPacket();
214 // Used by |helper_| to time alarms.
217 // Address of the server.
218 const IPEndPoint server_address_
;
220 // |server_id_| is a tuple (hostname, port, is_https) of the server.
221 QuicServerId server_id_
;
223 // config_ and crypto_config_ contain configuration and cached state about
226 QuicCryptoClientConfig crypto_config_
;
228 // Address of the client if the client is connected to the server.
229 IPEndPoint client_address_
;
231 // If initialized, the address to bind to.
232 IPAddressNumber bind_to_address_
;
233 // Local port to bind to. Initialize to 0.
236 // Writer used to actually send packets to the wire. Needs to outlive
238 scoped_ptr
<QuicPacketWriter
> writer_
;
240 // Session which manages streams.
241 scoped_ptr
<QuicClientSession
> session_
;
243 // UDP socket connected to the server.
244 scoped_ptr
<UDPClientSocket
> socket_
;
246 // Connection on the socket. Owned by |session_|.
247 QuicConnection
* connection_
;
249 // Helper to be used by created connections.
250 scoped_ptr
<QuicConnectionHelper
> helper_
;
252 // Listens for full responses.
253 scoped_ptr
<ResponseListener
> response_listener_
;
255 // Tracks if the client is initialized to connect.
258 // If overflow_supported_ is true, this will be the number of packets dropped
259 // during the lifetime of the server.
260 QuicPacketCount packets_dropped_
;
262 // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped
263 // because the socket would otherwise overflow.
264 bool overflow_supported_
;
266 // This vector contains QUIC versions which we currently support.
267 // This should be ordered such that the highest supported version is the first
268 // element, with subsequent elements in descending order (versions can be
269 // skipped as necessary). We will always pick supported_versions_[0] as the
270 // initial version to use.
271 QuicVersionVector supported_versions_
;
273 // If true, store the latest response code, headers, and body.
274 bool store_response_
;
275 // HTTP response code from most recent response.
276 size_t latest_response_code_
;
277 // HTTP headers from most recent response.
278 std::string latest_response_headers_
;
279 // Body of most recent response.
280 std::string latest_response_body_
;
282 // The log used for the sockets.
285 scoped_ptr
<QuicPacketReader
> packet_reader_
;
287 base::WeakPtrFactory
<QuicSimpleClient
> weak_factory_
;
289 DISALLOW_COPY_AND_ASSIGN(QuicSimpleClient
);
295 #endif // NET_TOOLS_QUIC_QUIC_SIMPLE_CLIENT_H_