OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "net/tools/quic/quic_simple_client.h" | 5 #include "net/tools/quic/quic_simple_client.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "net/quic/core/quic_flags.h" | 21 #include "net/quic/core/quic_flags.h" |
22 #include "net/quic/core/quic_protocol.h" | 22 #include "net/quic/core/quic_protocol.h" |
23 #include "net/quic/core/quic_server_id.h" | 23 #include "net/quic/core/quic_server_id.h" |
24 #include "net/quic/core/spdy_utils.h" | 24 #include "net/quic/core/spdy_utils.h" |
25 #include "net/spdy/spdy_header_block.h" | 25 #include "net/spdy/spdy_header_block.h" |
26 #include "net/spdy/spdy_http_utils.h" | 26 #include "net/spdy/spdy_http_utils.h" |
27 #include "net/udp/udp_client_socket.h" | 27 #include "net/udp/udp_client_socket.h" |
28 | 28 |
29 using std::string; | 29 using std::string; |
30 using std::vector; | 30 using std::vector; |
| 31 using base::StringPiece; |
31 | 32 |
32 namespace net { | 33 namespace net { |
33 | 34 |
34 void QuicSimpleClient::ClientQuicDataToResend::Resend() { | 35 void QuicSimpleClient::ClientQuicDataToResend::Resend() { |
35 client_->SendRequest(*headers_, body_, fin_); | 36 client_->SendRequest(*headers_, body_, fin_); |
36 delete headers_; | |
37 headers_ = nullptr; | 37 headers_ = nullptr; |
38 } | 38 } |
39 | 39 |
40 QuicSimpleClient::QuicSimpleClient( | 40 QuicSimpleClient::QuicSimpleClient( |
41 IPEndPoint server_address, | 41 IPEndPoint server_address, |
42 const QuicServerId& server_id, | 42 const QuicServerId& server_id, |
43 const QuicVersionVector& supported_versions, | 43 const QuicVersionVector& supported_versions, |
44 std::unique_ptr<ProofVerifier> proof_verifier) | 44 std::unique_ptr<ProofVerifier> proof_verifier) |
45 : QuicClientBase(server_id, | 45 : QuicClientBase(server_id, |
46 supported_versions, | 46 supported_versions, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 QuicClientBase::Initialize(); | 86 QuicClientBase::Initialize(); |
87 | 87 |
88 if (!CreateUDPSocket()) { | 88 if (!CreateUDPSocket()) { |
89 return false; | 89 return false; |
90 } | 90 } |
91 | 91 |
92 initialized_ = true; | 92 initialized_ = true; |
93 return true; | 93 return true; |
94 } | 94 } |
95 | 95 |
96 QuicSimpleClient::QuicDataToResend::QuicDataToResend(HttpRequestInfo* headers, | |
97 base::StringPiece body, | |
98 bool fin) | |
99 : headers_(headers), body_(body), fin_(fin) {} | |
100 | |
101 QuicSimpleClient::QuicDataToResend::~QuicDataToResend() { | |
102 if (headers_) { | |
103 delete headers_; | |
104 } | |
105 } | |
106 | |
107 bool QuicSimpleClient::CreateUDPSocket() { | 96 bool QuicSimpleClient::CreateUDPSocket() { |
108 std::unique_ptr<UDPClientSocket> socket( | 97 std::unique_ptr<UDPClientSocket> socket( |
109 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), | 98 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), |
110 &net_log_, NetLog::Source())); | 99 &net_log_, NetLog::Source())); |
111 | 100 |
112 int address_family = server_address_.GetSockAddrFamily(); | 101 int address_family = server_address_.GetSockAddrFamily(); |
113 if (bind_to_address_.size() != 0) { | 102 if (bind_to_address_.size() != 0) { |
114 client_address_ = IPEndPoint(bind_to_address_, local_port_); | 103 client_address_ = IPEndPoint(bind_to_address_, local_port_); |
115 } else if (address_family == AF_INET) { | 104 } else if (address_family == AF_INET) { |
116 client_address_ = IPEndPoint(IPAddress::IPv4AllZeros(), local_port_); | 105 client_address_ = IPEndPoint(IPAddress::IPv4AllZeros(), local_port_); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 } | 224 } |
236 data_to_resend_on_connect_.clear(); | 225 data_to_resend_on_connect_.clear(); |
237 | 226 |
238 reset_writer(); | 227 reset_writer(); |
239 packet_reader_.reset(); | 228 packet_reader_.reset(); |
240 packet_reader_started_ = false; | 229 packet_reader_started_ = false; |
241 | 230 |
242 initialized_ = false; | 231 initialized_ = false; |
243 } | 232 } |
244 | 233 |
245 void QuicSimpleClient::SendRequest(const HttpRequestInfo& headers, | 234 void QuicSimpleClient::SendRequest(const SpdyHeaderBlock& headers, |
246 base::StringPiece body, | 235 StringPiece body, |
247 bool fin) { | 236 bool fin) { |
248 QuicSpdyClientStream* stream = CreateReliableClientStream(); | 237 QuicSpdyClientStream* stream = CreateReliableClientStream(); |
249 if (stream == nullptr) { | 238 if (stream == nullptr) { |
250 LOG(DFATAL) << "stream creation failed!"; | 239 LOG(DFATAL) << "stream creation failed!"; |
251 return; | 240 return; |
252 } | 241 } |
253 SpdyHeaderBlock header_block; | |
254 CreateSpdyHeadersFromHttpRequest(headers, headers.extra_headers, true, | |
255 &header_block); | |
256 stream->set_visitor(this); | 242 stream->set_visitor(this); |
257 stream->SendRequest(std::move(header_block), body, fin); | 243 stream->SendRequest(headers.Clone(), body, fin); |
258 if (FLAGS_enable_quic_stateless_reject_support) { | 244 if (FLAGS_enable_quic_stateless_reject_support) { |
259 // Record this in case we need to resend. | 245 // Record this in case we need to resend. |
260 auto* new_headers = new HttpRequestInfo; | 246 std::unique_ptr<SpdyHeaderBlock> new_headers( |
261 *new_headers = headers; | 247 new SpdyHeaderBlock(headers.Clone())); |
262 auto* data_to_resend = | 248 auto data_to_resend = |
263 new ClientQuicDataToResend(new_headers, body, fin, this); | 249 new ClientQuicDataToResend(std::move(new_headers), body, fin, this); |
264 MaybeAddQuicDataToResend(std::unique_ptr<QuicDataToResend>(data_to_resend)); | 250 MaybeAddQuicDataToResend(std::unique_ptr<QuicDataToResend>(data_to_resend)); |
265 } | 251 } |
266 } | 252 } |
267 | 253 |
268 void QuicSimpleClient::MaybeAddQuicDataToResend( | 254 void QuicSimpleClient::MaybeAddQuicDataToResend( |
269 std::unique_ptr<QuicDataToResend> data_to_resend) { | 255 std::unique_ptr<QuicDataToResend> data_to_resend) { |
270 DCHECK(FLAGS_enable_quic_stateless_reject_support); | 256 DCHECK(FLAGS_enable_quic_stateless_reject_support); |
271 if (session()->IsCryptoHandshakeConfirmed()) { | 257 if (session()->IsCryptoHandshakeConfirmed()) { |
272 // The handshake is confirmed. No need to continue saving requests to | 258 // The handshake is confirmed. No need to continue saving requests to |
273 // resend. | 259 // resend. |
274 data_to_resend_on_connect_.clear(); | 260 data_to_resend_on_connect_.clear(); |
275 return; | 261 return; |
276 } | 262 } |
277 | 263 |
278 // The handshake is not confirmed. Push the data onto the queue of data to | 264 // The handshake is not confirmed. Push the data onto the queue of data to |
279 // resend if statelessly rejected. | 265 // resend if statelessly rejected. |
280 data_to_resend_on_connect_.push_back(std::move(data_to_resend)); | 266 data_to_resend_on_connect_.push_back(std::move(data_to_resend)); |
281 } | 267 } |
282 | 268 |
283 void QuicSimpleClient::SendRequestAndWaitForResponse( | 269 void QuicSimpleClient::SendRequestAndWaitForResponse( |
284 const HttpRequestInfo& request, | 270 const SpdyHeaderBlock& headers, |
285 base::StringPiece body, | 271 base::StringPiece body, |
286 bool fin) { | 272 bool fin) { |
287 SendRequest(request, body, fin); | 273 SendRequest(headers, body, fin); |
288 while (WaitForEvents()) { | 274 while (WaitForEvents()) { |
289 } | 275 } |
290 } | 276 } |
291 | 277 |
292 void QuicSimpleClient::SendRequestsAndWaitForResponse( | 278 void QuicSimpleClient::SendRequestsAndWaitForResponse( |
293 const base::CommandLine::StringVector& url_list) { | 279 const base::CommandLine::StringVector& url_list) { |
294 for (size_t i = 0; i < url_list.size(); ++i) { | 280 for (size_t i = 0; i < url_list.size(); ++i) { |
295 HttpRequestInfo request; | 281 SpdyHeaderBlock headers; |
296 request.method = "GET"; | 282 if (!SpdyUtils::PopulateHeaderBlockFromUrl(url_list[i], &headers)) { |
297 request.url = GURL(url_list[i]); | 283 QUIC_BUG << "Unable to create request"; |
298 SendRequest(request, "", true); | 284 continue; |
| 285 } |
| 286 SendRequest(headers, "", true); |
299 } | 287 } |
300 | 288 |
301 while (WaitForEvents()) { | 289 while (WaitForEvents()) { |
302 } | 290 } |
303 } | 291 } |
304 | 292 |
305 bool QuicSimpleClient::WaitForEvents() { | 293 bool QuicSimpleClient::WaitForEvents() { |
306 DCHECK(connected()); | 294 DCHECK(connected()); |
307 | 295 |
308 base::RunLoop().RunUntilIdle(); | 296 base::RunLoop().RunUntilIdle(); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 session()->connection()->ProcessUdpPacket(local_address, peer_address, | 389 session()->connection()->ProcessUdpPacket(local_address, peer_address, |
402 packet); | 390 packet); |
403 if (!session()->connection()->connected()) { | 391 if (!session()->connection()->connected()) { |
404 return false; | 392 return false; |
405 } | 393 } |
406 | 394 |
407 return true; | 395 return true; |
408 } | 396 } |
409 | 397 |
410 } // namespace net | 398 } // namespace net |
OLD | NEW |