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 "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
10 #include "net/http/http_request_info.h" | 10 #include "net/http/http_request_info.h" |
| 11 #include "net/http/http_response_info.h" |
11 #include "net/quic/crypto/quic_random.h" | 12 #include "net/quic/crypto/quic_random.h" |
12 #include "net/quic/quic_connection.h" | 13 #include "net/quic/quic_connection.h" |
13 #include "net/quic/quic_connection_helper.h" | 14 #include "net/quic/quic_connection_helper.h" |
14 #include "net/quic/quic_default_packet_writer.h" | 15 #include "net/quic/quic_default_packet_writer.h" |
15 #include "net/quic/quic_protocol.h" | 16 #include "net/quic/quic_protocol.h" |
16 #include "net/quic/quic_server_id.h" | 17 #include "net/quic/quic_server_id.h" |
| 18 #include "net/spdy/spdy_http_utils.h" |
17 #include "net/udp/udp_client_socket.h" | 19 #include "net/udp/udp_client_socket.h" |
18 | 20 |
19 using std::string; | 21 using std::string; |
20 using std::vector; | 22 using std::vector; |
21 | 23 |
22 namespace net { | 24 namespace net { |
23 namespace tools { | 25 namespace tools { |
24 | 26 |
25 QuicSimpleClient::QuicSimpleClient(IPEndPoint server_address, | 27 QuicSimpleClient::QuicSimpleClient(IPEndPoint server_address, |
26 const QuicServerId& server_id, | 28 const QuicServerId& server_id, |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 | 149 |
148 writer_.reset(CreateQuicPacketWriter()); | 150 writer_.reset(CreateQuicPacketWriter()); |
149 connection_ = new QuicConnection(GenerateConnectionId(), | 151 connection_ = new QuicConnection(GenerateConnectionId(), |
150 server_address_, | 152 server_address_, |
151 helper_.get(), | 153 helper_.get(), |
152 DummyPacketWriterFactory(writer_.get()), | 154 DummyPacketWriterFactory(writer_.get()), |
153 /* owns_writer= */ false, | 155 /* owns_writer= */ false, |
154 Perspective::IS_CLIENT, | 156 Perspective::IS_CLIENT, |
155 server_id_.is_https(), | 157 server_id_.is_https(), |
156 supported_versions_); | 158 supported_versions_); |
157 session_.reset(new QuicSimpleClientSession(config_, connection_)); | 159 session_.reset(new QuicClientSession(config_, connection_)); |
158 session_->InitializeSession(server_id_, &crypto_config_); | 160 session_->InitializeSession(server_id_, &crypto_config_); |
159 session_->CryptoConnect(); | 161 session_->CryptoConnect(); |
160 } | 162 } |
161 | 163 |
162 bool QuicSimpleClient::EncryptionBeingEstablished() { | 164 bool QuicSimpleClient::EncryptionBeingEstablished() { |
163 return !session_->IsEncryptionEstablished() && | 165 return !session_->IsEncryptionEstablished() && |
164 session_->connection()->connected(); | 166 session_->connection()->connected(); |
165 } | 167 } |
166 | 168 |
167 void QuicSimpleClient::Disconnect() { | 169 void QuicSimpleClient::Disconnect() { |
168 DCHECK(initialized_); | 170 DCHECK(initialized_); |
169 | 171 |
170 if (connected()) { | 172 if (connected()) { |
171 session()->connection()->SendConnectionClose(QUIC_PEER_GOING_AWAY); | 173 session()->connection()->SendConnectionClose(QUIC_PEER_GOING_AWAY); |
172 } | 174 } |
173 | 175 |
174 writer_.reset(); | 176 writer_.reset(); |
175 packet_reader_.reset(); | 177 packet_reader_.reset(); |
176 | 178 |
177 initialized_ = false; | 179 initialized_ = false; |
178 } | 180 } |
179 | 181 |
180 void QuicSimpleClient::SendRequest(const HttpRequestInfo& headers, | 182 void QuicSimpleClient::SendRequest(const HttpRequestInfo& headers, |
181 base::StringPiece body, | 183 base::StringPiece body, |
182 bool fin) { | 184 bool fin) { |
183 QuicSimpleClientStream* stream = CreateReliableClientStream(); | 185 QuicSpdyClientStream* stream = CreateReliableClientStream(); |
184 if (stream == nullptr) { | 186 if (stream == nullptr) { |
185 LOG(DFATAL) << "stream creation failed!"; | 187 LOG(DFATAL) << "stream creation failed!"; |
186 return; | 188 return; |
187 } | 189 } |
188 stream->SendRequest(headers, body, fin); | 190 SpdyHeaderBlock header_block; |
| 191 CreateSpdyHeadersFromHttpRequest(headers, headers.extra_headers, SPDY3, true, |
| 192 &header_block); |
| 193 stream->SendRequest(header_block, body, fin); |
189 stream->set_visitor(this); | 194 stream->set_visitor(this); |
190 } | 195 } |
191 | 196 |
192 void QuicSimpleClient::SendRequestAndWaitForResponse( | 197 void QuicSimpleClient::SendRequestAndWaitForResponse( |
193 const HttpRequestInfo& request, | 198 const HttpRequestInfo& request, |
194 base::StringPiece body, | 199 base::StringPiece body, |
195 bool fin) { | 200 bool fin) { |
196 SendRequest(request, body, fin); | 201 SendRequest(request, body, fin); |
197 while (WaitForEvents()) {} | 202 while (WaitForEvents()) {} |
198 } | 203 } |
199 | 204 |
200 void QuicSimpleClient::SendRequestsAndWaitForResponse( | 205 void QuicSimpleClient::SendRequestsAndWaitForResponse( |
201 const base::CommandLine::StringVector& url_list) { | 206 const base::CommandLine::StringVector& url_list) { |
202 for (size_t i = 0; i < url_list.size(); ++i) { | 207 for (size_t i = 0; i < url_list.size(); ++i) { |
203 HttpRequestInfo request; | 208 HttpRequestInfo request; |
204 request.method = "GET"; | 209 request.method = "GET"; |
205 request.url = GURL(url_list[i]); | 210 request.url = GURL(url_list[i]); |
206 SendRequest(request, "", true); | 211 SendRequest(request, "", true); |
207 } | 212 } |
208 | 213 |
209 while (WaitForEvents()) {} | 214 while (WaitForEvents()) {} |
210 } | 215 } |
211 | 216 |
212 QuicSimpleClientStream* QuicSimpleClient::CreateReliableClientStream() { | 217 QuicSpdyClientStream* QuicSimpleClient::CreateReliableClientStream() { |
213 if (!connected()) { | 218 if (!connected()) { |
214 return nullptr; | 219 return nullptr; |
215 } | 220 } |
216 | 221 |
217 return session_->CreateOutgoingDataStream(); | 222 return session_->CreateOutgoingDataStream(); |
218 } | 223 } |
219 | 224 |
220 void QuicSimpleClient::WaitForStreamToClose(QuicStreamId id) { | 225 void QuicSimpleClient::WaitForStreamToClose(QuicStreamId id) { |
221 DCHECK(connected()); | 226 DCHECK(connected()); |
222 | 227 |
(...skipping 11 matching lines...) Expand all Loading... |
234 } | 239 } |
235 | 240 |
236 bool QuicSimpleClient::WaitForEvents() { | 241 bool QuicSimpleClient::WaitForEvents() { |
237 DCHECK(connected()); | 242 DCHECK(connected()); |
238 | 243 |
239 base::RunLoop().RunUntilIdle(); | 244 base::RunLoop().RunUntilIdle(); |
240 return session_->num_active_requests() != 0; | 245 return session_->num_active_requests() != 0; |
241 } | 246 } |
242 | 247 |
243 void QuicSimpleClient::OnClose(QuicDataStream* stream) { | 248 void QuicSimpleClient::OnClose(QuicDataStream* stream) { |
244 QuicSimpleClientStream* client_stream = | 249 QuicSpdyClientStream* client_stream = |
245 static_cast<QuicSimpleClientStream*>(stream); | 250 static_cast<QuicSpdyClientStream*>(stream); |
| 251 HttpResponseInfo response; |
| 252 SpdyHeadersToHttpResponse(client_stream->headers(), SPDY3, &response); |
246 if (response_listener_.get() != nullptr) { | 253 if (response_listener_.get() != nullptr) { |
247 response_listener_->OnCompleteResponse( | 254 response_listener_->OnCompleteResponse( |
248 stream->id(), *client_stream->headers(), client_stream->data()); | 255 stream->id(), *response.headers, client_stream->data()); |
249 } | 256 } |
250 | 257 |
251 // Store response headers and body. | 258 // Store response headers and body. |
252 if (store_response_) { | 259 if (store_response_) { |
253 latest_response_code_ = client_stream->headers()->response_code(); | 260 latest_response_code_ = client_stream->response_code(); |
254 client_stream->headers()->GetNormalizedHeaders(&latest_response_headers_); | 261 response.headers->GetNormalizedHeaders(&latest_response_headers_); |
255 latest_response_body_ = client_stream->data(); | 262 latest_response_body_ = client_stream->data(); |
256 } | 263 } |
257 } | 264 } |
258 | 265 |
259 bool QuicSimpleClient::connected() const { | 266 bool QuicSimpleClient::connected() const { |
260 return session_.get() && session_->connection() && | 267 return session_.get() && session_->connection() && |
261 session_->connection()->connected(); | 268 session_->connection()->connected(); |
262 } | 269 } |
263 | 270 |
264 bool QuicSimpleClient::goaway_received() const { | 271 bool QuicSimpleClient::goaway_received() const { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 session_->connection()->ProcessUdpPacket(local_address, peer_address, packet); | 313 session_->connection()->ProcessUdpPacket(local_address, peer_address, packet); |
307 if (!session_->connection()->connected()) { | 314 if (!session_->connection()->connected()) { |
308 return false; | 315 return false; |
309 } | 316 } |
310 | 317 |
311 return true; | 318 return true; |
312 } | 319 } |
313 | 320 |
314 } // namespace tools | 321 } // namespace tools |
315 } // namespace net | 322 } // namespace net |
OLD | NEW |