OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // A base class for the toy client, which connects to a specified port and sends | 5 // A base class for the toy client, which connects to a specified port and sends |
6 // QUIC request to that endpoint. | 6 // QUIC request to that endpoint. |
7 | 7 |
8 #ifndef NET_TOOLS_QUIC_QUIC_CLIENT_BASE_H_ | 8 #ifndef NET_TOOLS_QUIC_QUIC_CLIENT_BASE_H_ |
9 #define NET_TOOLS_QUIC_QUIC_CLIENT_BASE_H_ | 9 #define NET_TOOLS_QUIC_QUIC_CLIENT_BASE_H_ |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "net/tools/quic/quic_spdy_client_stream.h" | 27 #include "net/tools/quic/quic_spdy_client_stream.h" |
28 | 28 |
29 namespace net { | 29 namespace net { |
30 | 30 |
31 class ProofVerifier; | 31 class ProofVerifier; |
32 class QuicServerId; | 32 class QuicServerId; |
33 | 33 |
34 class QuicClientBase : public QuicClientPushPromiseIndex::Delegate, | 34 class QuicClientBase : public QuicClientPushPromiseIndex::Delegate, |
35 public QuicSpdyStream::Visitor { | 35 public QuicSpdyStream::Visitor { |
36 public: | 36 public: |
| 37 // A ResponseListener is notified when a complete response is received. |
| 38 class ResponseListener { |
| 39 public: |
| 40 ResponseListener() {} |
| 41 virtual ~ResponseListener() {} |
| 42 virtual void OnCompleteResponse(QuicStreamId id, |
| 43 const SpdyHeaderBlock& response_headers, |
| 44 const std::string& response_body) = 0; |
| 45 }; |
| 46 |
37 // The client uses these objects to keep track of any data to resend upon | 47 // The client uses these objects to keep track of any data to resend upon |
38 // receipt of a stateless reject. Recall that the client API allows callers | 48 // receipt of a stateless reject. Recall that the client API allows callers |
39 // to optimistically send data to the server prior to handshake-confirmation. | 49 // to optimistically send data to the server prior to handshake-confirmation. |
40 // If the client subsequently receives a stateless reject, it must tear down | 50 // If the client subsequently receives a stateless reject, it must tear down |
41 // its existing session, create a new session, and resend all previously sent | 51 // its existing session, create a new session, and resend all previously sent |
42 // data. It uses these objects to keep track of all the sent data, and to | 52 // data. It uses these objects to keep track of all the sent data, and to |
43 // resend the data upon a subsequent connection. | 53 // resend the data upon a subsequent connection. |
44 class QuicDataToResend { | 54 class QuicDataToResend { |
45 public: | 55 public: |
46 // |headers| may be null, since it's possible to send data without headers. | 56 // |headers| may be null, since it's possible to send data without headers. |
(...skipping 18 matching lines...) Expand all Loading... |
65 | 75 |
66 QuicClientBase(const QuicServerId& server_id, | 76 QuicClientBase(const QuicServerId& server_id, |
67 const QuicVersionVector& supported_versions, | 77 const QuicVersionVector& supported_versions, |
68 const QuicConfig& config, | 78 const QuicConfig& config, |
69 QuicConnectionHelperInterface* helper, | 79 QuicConnectionHelperInterface* helper, |
70 QuicAlarmFactory* alarm_factory, | 80 QuicAlarmFactory* alarm_factory, |
71 std::unique_ptr<ProofVerifier> proof_verifier); | 81 std::unique_ptr<ProofVerifier> proof_verifier); |
72 | 82 |
73 ~QuicClientBase() override; | 83 ~QuicClientBase() override; |
74 | 84 |
| 85 // QuicSpdyStream::Visitor |
| 86 void OnClose(QuicSpdyStream* stream) override; |
| 87 |
75 // Initializes the client to create a connection. Should be called exactly | 88 // Initializes the client to create a connection. Should be called exactly |
76 // once before calling StartConnect or Connect. Returns true if the | 89 // once before calling StartConnect or Connect. Returns true if the |
77 // initialization succeeds, false otherwise. | 90 // initialization succeeds, false otherwise. |
78 virtual bool Initialize(); | 91 virtual bool Initialize(); |
79 | 92 |
80 // Returns true if the crypto handshake has yet to establish encryption. | 93 // Returns true if the crypto handshake has yet to establish encryption. |
81 // Returns false if encryption is active (even if the server hasn't confirmed | 94 // Returns false if encryption is active (even if the server hasn't confirmed |
82 // the handshake) or if the connection has been closed. | 95 // the handshake) or if the connection has been closed. |
83 bool EncryptionBeingEstablished(); | 96 bool EncryptionBeingEstablished(); |
84 | 97 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 const SpdyHeaderBlock& promise_request, | 227 const SpdyHeaderBlock& promise_request, |
215 const SpdyHeaderBlock& promise_response) override; | 228 const SpdyHeaderBlock& promise_response) override; |
216 void OnRendezvousResult(QuicSpdyStream*) override; | 229 void OnRendezvousResult(QuicSpdyStream*) override; |
217 | 230 |
218 // If the crypto handshake has not yet been confirmed, adds the data to the | 231 // If the crypto handshake has not yet been confirmed, adds the data to the |
219 // queue of data to resend if the client receives a stateless reject. | 232 // queue of data to resend if the client receives a stateless reject. |
220 // Otherwise, deletes the data. | 233 // Otherwise, deletes the data. |
221 void MaybeAddQuicDataToResend( | 234 void MaybeAddQuicDataToResend( |
222 std::unique_ptr<QuicDataToResend> data_to_resend); | 235 std::unique_ptr<QuicDataToResend> data_to_resend); |
223 | 236 |
| 237 void set_store_response(bool val) { store_response_ = val; } |
| 238 |
| 239 size_t latest_response_code() const; |
| 240 const std::string& latest_response_headers() const; |
| 241 const SpdyHeaderBlock& latest_response_header_block() const; |
| 242 const std::string& latest_response_body() const; |
| 243 const std::string& latest_response_trailers() const; |
| 244 |
| 245 void set_response_listener(std::unique_ptr<ResponseListener> listener) { |
| 246 response_listener_ = std::move(listener); |
| 247 } |
| 248 |
| 249 void set_bind_to_address(IPAddress address) { bind_to_address_ = address; } |
| 250 |
| 251 IPAddress bind_to_address() const { return bind_to_address_; } |
| 252 |
| 253 void set_local_port(int local_port) { local_port_ = local_port; } |
| 254 |
| 255 int local_port() const { return local_port_; } |
| 256 |
| 257 const IPEndPoint& server_address() const { return server_address_; } |
| 258 |
| 259 void set_server_address(const IPEndPoint& server_address) { |
| 260 server_address_ = server_address; |
| 261 } |
| 262 |
224 protected: | 263 protected: |
225 // Takes ownership of |connection|. | 264 // Takes ownership of |connection|. |
226 virtual QuicClientSession* CreateQuicClientSession( | 265 virtual QuicClientSession* CreateQuicClientSession( |
227 QuicConnection* connection); | 266 QuicConnection* connection); |
228 | 267 |
229 // Generates the next ConnectionId for |server_id_|. By default, if the | 268 // Generates the next ConnectionId for |server_id_|. By default, if the |
230 // cached server config contains a server-designated ID, that ID will be | 269 // cached server config contains a server-designated ID, that ID will be |
231 // returned. Otherwise, the next random ID will be returned. | 270 // returned. Otherwise, the next random ID will be returned. |
232 QuicConnectionId GetNextConnectionId(); | 271 QuicConnectionId GetNextConnectionId(); |
233 | 272 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 | 324 |
286 private: | 325 private: |
287 QuicClientBase* client_; | 326 QuicClientBase* client_; |
288 | 327 |
289 DISALLOW_COPY_AND_ASSIGN(ClientQuicDataToResend); | 328 DISALLOW_COPY_AND_ASSIGN(ClientQuicDataToResend); |
290 }; | 329 }; |
291 | 330 |
292 // |server_id_| is a tuple (hostname, port, is_https) of the server. | 331 // |server_id_| is a tuple (hostname, port, is_https) of the server. |
293 QuicServerId server_id_; | 332 QuicServerId server_id_; |
294 | 333 |
| 334 // Address of the server. |
| 335 IPEndPoint server_address_; |
| 336 |
| 337 // If initialized, the address to bind to. |
| 338 IPAddress bind_to_address_; |
| 339 |
| 340 // Local port to bind to. Initialize to 0. |
| 341 int local_port_; |
| 342 |
295 // config_ and crypto_config_ contain configuration and cached state about | 343 // config_ and crypto_config_ contain configuration and cached state about |
296 // servers. | 344 // servers. |
297 QuicConfig config_; | 345 QuicConfig config_; |
298 QuicCryptoClientConfig crypto_config_; | 346 QuicCryptoClientConfig crypto_config_; |
299 | 347 |
300 // Helper to be used by created connections. Needs to outlive |session_|. | 348 // Helper to be used by created connections. Needs to outlive |session_|. |
301 std::unique_ptr<QuicConnectionHelperInterface> helper_; | 349 std::unique_ptr<QuicConnectionHelperInterface> helper_; |
302 | 350 |
303 // Helper to be used by created connections. Needs to outlive |session_|. | 351 // Helper to be used by created connections. Needs to outlive |session_|. |
304 std::unique_ptr<QuicAlarmFactory> alarm_factory_; | 352 std::unique_ptr<QuicAlarmFactory> alarm_factory_; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 | 385 |
338 // True when the client is attempting to connect or re-connect the session (in | 386 // True when the client is attempting to connect or re-connect the session (in |
339 // the case of a stateless reject). Set to false between a call to | 387 // the case of a stateless reject). Set to false between a call to |
340 // Disconnect() and the subsequent call to StartConnect(). When | 388 // Disconnect() and the subsequent call to StartConnect(). When |
341 // connected_or_attempting_connect_ is false, the session object corresponds | 389 // connected_or_attempting_connect_ is false, the session object corresponds |
342 // to the previous client-level connection. | 390 // to the previous client-level connection. |
343 bool connected_or_attempting_connect_; | 391 bool connected_or_attempting_connect_; |
344 | 392 |
345 QuicClientPushPromiseIndex push_promise_index_; | 393 QuicClientPushPromiseIndex push_promise_index_; |
346 | 394 |
| 395 // If true, store the latest response code, headers, and body. |
| 396 bool store_response_; |
| 397 // HTTP response code from most recent response. |
| 398 int latest_response_code_; |
| 399 // HTTP/2 headers from most recent response. |
| 400 std::string latest_response_headers_; |
| 401 // HTTP/2 headers from most recent response. |
| 402 SpdyHeaderBlock latest_response_header_block_; |
| 403 // Body of most recent response. |
| 404 std::string latest_response_body_; |
| 405 // HTTP/2 trailers from most recent response. |
| 406 std::string latest_response_trailers_; |
| 407 |
| 408 // Listens for full responses. |
| 409 std::unique_ptr<ResponseListener> response_listener_; |
| 410 |
347 // Keeps track of any data that must be resent upon a subsequent successful | 411 // Keeps track of any data that must be resent upon a subsequent successful |
348 // connection, in case the client receives a stateless reject. | 412 // connection, in case the client receives a stateless reject. |
349 std::vector<std::unique_ptr<QuicDataToResend>> data_to_resend_on_connect_; | 413 std::vector<std::unique_ptr<QuicDataToResend>> data_to_resend_on_connect_; |
350 | 414 |
351 std::unique_ptr<ClientQuicDataToResend> push_promise_data_to_resend_; | 415 std::unique_ptr<ClientQuicDataToResend> push_promise_data_to_resend_; |
352 | 416 |
353 DISALLOW_COPY_AND_ASSIGN(QuicClientBase); | 417 DISALLOW_COPY_AND_ASSIGN(QuicClientBase); |
354 }; | 418 }; |
355 | 419 |
356 } // namespace net | 420 } // namespace net |
357 | 421 |
358 #endif // NET_TOOLS_QUIC_QUIC_CLIENT_BASE_H_ | 422 #endif // NET_TOOLS_QUIC_QUIC_CLIENT_BASE_H_ |
OLD | NEW |