| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/http/http_network_transaction.h" | 5 #include "net/http/http_network_transaction.h" |
| 6 | 6 |
| 7 #include <math.h> // ceil | 7 #include <math.h> // ceil |
| 8 #include <stdarg.h> | 8 #include <stdarg.h> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 // failure should cause the network transaction to resend the request, and the | 282 // failure should cause the network transaction to resend the request, and the |
| 283 // other argument should be NULL. | 283 // other argument should be NULL. |
| 284 void KeepAliveConnectionResendRequestTest(const MockWrite* write_failure, | 284 void KeepAliveConnectionResendRequestTest(const MockWrite* write_failure, |
| 285 const MockRead* read_failure); | 285 const MockRead* read_failure); |
| 286 | 286 |
| 287 // Either |write_failure| specifies a write failure or |read_failure| | 287 // Either |write_failure| specifies a write failure or |read_failure| |
| 288 // specifies a read failure when using a reused socket. In either case, the | 288 // specifies a read failure when using a reused socket. In either case, the |
| 289 // failure should cause the network transaction to resend the request, and the | 289 // failure should cause the network transaction to resend the request, and the |
| 290 // other argument should be NULL. | 290 // other argument should be NULL. |
| 291 void PreconnectErrorResendRequestTest(const MockWrite* write_failure, | 291 void PreconnectErrorResendRequestTest(const MockWrite* write_failure, |
| 292 const MockRead* read_failure); | 292 const MockRead* read_failure, |
| 293 bool use_spdy); |
| 293 | 294 |
| 294 SimpleGetHelperResult SimpleGetHelperForData(StaticSocketDataProvider* data[], | 295 SimpleGetHelperResult SimpleGetHelperForData(StaticSocketDataProvider* data[], |
| 295 size_t data_count) { | 296 size_t data_count) { |
| 296 SimpleGetHelperResult out; | 297 SimpleGetHelperResult out; |
| 297 | 298 |
| 298 HttpRequestInfo request; | 299 HttpRequestInfo request; |
| 299 request.method = "GET"; | 300 request.method = "GET"; |
| 300 request.url = GURL("http://www.google.com/"); | 301 request.url = GURL("http://www.google.com/"); |
| 301 request.load_flags = 0; | 302 request.load_flags = 0; |
| 302 | 303 |
| (...skipping 998 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1301 | 1302 |
| 1302 std::string response_data; | 1303 std::string response_data; |
| 1303 rv = ReadTransaction(trans.get(), &response_data); | 1304 rv = ReadTransaction(trans.get(), &response_data); |
| 1304 EXPECT_EQ(OK, rv); | 1305 EXPECT_EQ(OK, rv); |
| 1305 EXPECT_EQ(kExpectedResponseData[i], response_data); | 1306 EXPECT_EQ(kExpectedResponseData[i], response_data); |
| 1306 } | 1307 } |
| 1307 } | 1308 } |
| 1308 | 1309 |
| 1309 void HttpNetworkTransactionTest::PreconnectErrorResendRequestTest( | 1310 void HttpNetworkTransactionTest::PreconnectErrorResendRequestTest( |
| 1310 const MockWrite* write_failure, | 1311 const MockWrite* write_failure, |
| 1311 const MockRead* read_failure) { | 1312 const MockRead* read_failure, |
| 1313 bool use_spdy) { |
| 1312 HttpRequestInfo request; | 1314 HttpRequestInfo request; |
| 1313 request.method = "GET"; | 1315 request.method = "GET"; |
| 1314 request.url = GURL("http://www.foo.com/"); | 1316 request.url = GURL("https://www.foo.com/"); |
| 1315 request.load_flags = 0; | 1317 request.load_flags = 0; |
| 1316 | 1318 |
| 1317 CapturingNetLog net_log; | 1319 CapturingNetLog net_log; |
| 1318 session_deps_.net_log = &net_log; | 1320 session_deps_.net_log = &net_log; |
| 1319 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | 1321 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 1320 | 1322 |
| 1321 // Written data for successfully sending a request. | 1323 SSLSocketDataProvider ssl1(ASYNC, OK); |
| 1322 MockWrite data1_writes[] = { | 1324 SSLSocketDataProvider ssl2(ASYNC, OK); |
| 1323 MockWrite("GET / HTTP/1.1\r\n" | 1325 if (use_spdy) { |
| 1324 "Host: www.foo.com\r\n" | 1326 ssl1.SetNextProto(GetParam()); |
| 1325 "Connection: keep-alive\r\n\r\n"), | 1327 ssl2.SetNextProto(GetParam()); |
| 1326 }; | 1328 } |
| 1329 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 1330 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 1327 | 1331 |
| 1328 // Read results for the first request. | 1332 // SPDY versions of the request and response. |
| 1329 MockRead data1_reads[] = { | 1333 scoped_ptr<SpdyFrame> spdy_request(spdy_util_.ConstructSpdyGet( |
| 1330 MockRead(ASYNC, OK), | 1334 request.url.spec().c_str(), false, 1, DEFAULT_PRIORITY)); |
| 1331 }; | 1335 scoped_ptr<SpdyFrame> spdy_response( |
| 1336 spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 1337 scoped_ptr<SpdyFrame> spdy_data( |
| 1338 spdy_util_.ConstructSpdyBodyFrame(1, "hello", 5, true)); |
| 1332 | 1339 |
| 1340 // HTTP/1.1 versions of the request and response. |
| 1341 const char kHttpRequest[] = "GET / HTTP/1.1\r\n" |
| 1342 "Host: www.foo.com\r\n" |
| 1343 "Connection: keep-alive\r\n\r\n"; |
| 1344 const char kHttpResponse[] = "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"; |
| 1345 const char kHttpData[] = "hello"; |
| 1346 |
| 1347 std::vector<MockRead> data1_reads; |
| 1348 std::vector<MockWrite> data1_writes; |
| 1333 if (write_failure) { | 1349 if (write_failure) { |
| 1334 ASSERT_FALSE(read_failure); | 1350 ASSERT_FALSE(read_failure); |
| 1335 data1_writes[0] = *write_failure; | 1351 data1_writes.push_back(*write_failure); |
| 1352 data1_reads.push_back(MockRead(ASYNC, OK)); |
| 1336 } else { | 1353 } else { |
| 1337 ASSERT_TRUE(read_failure); | 1354 ASSERT_TRUE(read_failure); |
| 1338 data1_reads[0] = *read_failure; | 1355 if (use_spdy) { |
| 1356 data1_writes.push_back(CreateMockWrite(*spdy_request)); |
| 1357 } else { |
| 1358 data1_writes.push_back(MockWrite(kHttpRequest)); |
| 1359 } |
| 1360 data1_reads.push_back(*read_failure); |
| 1339 } | 1361 } |
| 1340 | 1362 |
| 1341 StaticSocketDataProvider data1(data1_reads, arraysize(data1_reads), | 1363 StaticSocketDataProvider data1(&data1_reads[0], data1_reads.size(), |
| 1342 data1_writes, arraysize(data1_writes)); | 1364 &data1_writes[0], data1_writes.size()); |
| 1343 session_deps_.socket_factory->AddSocketDataProvider(&data1); | 1365 session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 1344 | 1366 |
| 1345 MockRead data2_reads[] = { | 1367 std::vector<MockRead> data2_reads; |
| 1346 MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), | 1368 std::vector<MockWrite> data2_writes; |
| 1347 MockRead("hello"), | 1369 |
| 1348 MockRead(ASYNC, OK), | 1370 if (use_spdy) { |
| 1349 }; | 1371 data2_writes.push_back(CreateMockWrite(*spdy_request, 0, ASYNC)); |
| 1350 StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); | 1372 |
| 1373 data2_reads.push_back(CreateMockRead(*spdy_response, 1, ASYNC)); |
| 1374 data2_reads.push_back(CreateMockRead(*spdy_data, 2, ASYNC)); |
| 1375 data2_reads.push_back(MockRead(ASYNC, OK, 3)); |
| 1376 } else { |
| 1377 data2_writes.push_back( |
| 1378 MockWrite(ASYNC, kHttpRequest, strlen(kHttpRequest), 0)); |
| 1379 |
| 1380 data2_reads.push_back( |
| 1381 MockRead(ASYNC, kHttpResponse, strlen(kHttpResponse), 1)); |
| 1382 data2_reads.push_back(MockRead(ASYNC, kHttpData, strlen(kHttpData), 2)); |
| 1383 data2_reads.push_back(MockRead(ASYNC, OK, 3)); |
| 1384 } |
| 1385 OrderedSocketData data2(&data2_reads[0], data2_reads.size(), |
| 1386 &data2_writes[0], data2_writes.size()); |
| 1351 session_deps_.socket_factory->AddSocketDataProvider(&data2); | 1387 session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 1352 | 1388 |
| 1353 // Preconnect a socket. | 1389 // Preconnect a socket. |
| 1354 net::SSLConfig ssl_config; | 1390 net::SSLConfig ssl_config; |
| 1355 session->ssl_config_service()->GetSSLConfig(&ssl_config); | 1391 session->ssl_config_service()->GetSSLConfig(&ssl_config); |
| 1356 if (session->http_stream_factory()->has_next_protos()) | 1392 if (session->http_stream_factory()->has_next_protos()) |
| 1357 ssl_config.next_protos = session->http_stream_factory()->next_protos(); | 1393 ssl_config.next_protos = session->http_stream_factory()->next_protos(); |
| 1358 session->http_stream_factory()->PreconnectStreams( | 1394 session->http_stream_factory()->PreconnectStreams( |
| 1359 1, request, DEFAULT_PRIORITY, ssl_config, ssl_config); | 1395 1, request, DEFAULT_PRIORITY, ssl_config, ssl_config); |
| 1360 // Wait for the preconnect to complete. | 1396 // Wait for the preconnect to complete. |
| 1361 // TODO(davidben): Some way to wait for an idle socket count might be handy. | 1397 // TODO(davidben): Some way to wait for an idle socket count might be handy. |
| 1362 base::RunLoop().RunUntilIdle(); | 1398 base::RunLoop().RunUntilIdle(); |
| 1363 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); | 1399 EXPECT_EQ(1, GetIdleSocketCountInSSLSocketPool(session.get())); |
| 1364 | 1400 |
| 1365 // Make the request. | 1401 // Make the request. |
| 1366 TestCompletionCallback callback; | 1402 TestCompletionCallback callback; |
| 1367 | 1403 |
| 1368 scoped_ptr<HttpTransaction> trans( | 1404 scoped_ptr<HttpTransaction> trans( |
| 1369 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | 1405 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); |
| 1370 | 1406 |
| 1371 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | 1407 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 1372 EXPECT_EQ(ERR_IO_PENDING, rv); | 1408 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1373 | 1409 |
| 1374 rv = callback.WaitForResult(); | 1410 rv = callback.WaitForResult(); |
| 1375 EXPECT_EQ(OK, rv); | 1411 EXPECT_EQ(OK, rv); |
| 1376 | 1412 |
| 1377 LoadTimingInfo load_timing_info; | 1413 LoadTimingInfo load_timing_info; |
| 1378 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | 1414 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 1379 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); | 1415 TestLoadTimingNotReused( |
| 1416 load_timing_info, |
| 1417 CONNECT_TIMING_HAS_DNS_TIMES|CONNECT_TIMING_HAS_SSL_TIMES); |
| 1380 | 1418 |
| 1381 const HttpResponseInfo* response = trans->GetResponseInfo(); | 1419 const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1382 ASSERT_TRUE(response != NULL); | 1420 ASSERT_TRUE(response != NULL); |
| 1383 | 1421 |
| 1384 EXPECT_TRUE(response->headers.get() != NULL); | 1422 EXPECT_TRUE(response->headers.get() != NULL); |
| 1385 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | 1423 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 1386 | 1424 |
| 1387 std::string response_data; | 1425 std::string response_data; |
| 1388 rv = ReadTransaction(trans.get(), &response_data); | 1426 rv = ReadTransaction(trans.get(), &response_data); |
| 1389 EXPECT_EQ(OK, rv); | 1427 EXPECT_EQ(OK, rv); |
| 1390 EXPECT_EQ("hello", response_data); | 1428 EXPECT_EQ(kHttpData, response_data); |
| 1391 } | 1429 } |
| 1392 | 1430 |
| 1393 TEST_P(HttpNetworkTransactionTest, | 1431 TEST_P(HttpNetworkTransactionTest, |
| 1394 KeepAliveConnectionNotConnectedOnWrite) { | 1432 KeepAliveConnectionNotConnectedOnWrite) { |
| 1395 MockWrite write_failure(ASYNC, ERR_SOCKET_NOT_CONNECTED); | 1433 MockWrite write_failure(ASYNC, ERR_SOCKET_NOT_CONNECTED); |
| 1396 KeepAliveConnectionResendRequestTest(&write_failure, NULL); | 1434 KeepAliveConnectionResendRequestTest(&write_failure, NULL); |
| 1397 } | 1435 } |
| 1398 | 1436 |
| 1399 TEST_P(HttpNetworkTransactionTest, KeepAliveConnectionReset) { | 1437 TEST_P(HttpNetworkTransactionTest, KeepAliveConnectionReset) { |
| 1400 MockRead read_failure(ASYNC, ERR_CONNECTION_RESET); | 1438 MockRead read_failure(ASYNC, ERR_CONNECTION_RESET); |
| 1401 KeepAliveConnectionResendRequestTest(NULL, &read_failure); | 1439 KeepAliveConnectionResendRequestTest(NULL, &read_failure); |
| 1402 } | 1440 } |
| 1403 | 1441 |
| 1404 TEST_P(HttpNetworkTransactionTest, KeepAliveConnectionEOF) { | 1442 TEST_P(HttpNetworkTransactionTest, KeepAliveConnectionEOF) { |
| 1405 MockRead read_failure(SYNCHRONOUS, OK); // EOF | 1443 MockRead read_failure(SYNCHRONOUS, OK); // EOF |
| 1406 KeepAliveConnectionResendRequestTest(NULL, &read_failure); | 1444 KeepAliveConnectionResendRequestTest(NULL, &read_failure); |
| 1407 } | 1445 } |
| 1408 | 1446 |
| 1409 TEST_P(HttpNetworkTransactionTest, | 1447 TEST_P(HttpNetworkTransactionTest, |
| 1410 PreconnectErrorNotConnectedOnWrite) { | 1448 PreconnectErrorNotConnectedOnWrite) { |
| 1411 MockWrite write_failure(ASYNC, ERR_SOCKET_NOT_CONNECTED); | 1449 MockWrite write_failure(ASYNC, ERR_SOCKET_NOT_CONNECTED); |
| 1412 PreconnectErrorResendRequestTest(&write_failure, NULL); | 1450 PreconnectErrorResendRequestTest(&write_failure, NULL, false); |
| 1413 } | 1451 } |
| 1414 | 1452 |
| 1415 TEST_P(HttpNetworkTransactionTest, PreconnectErrorReset) { | 1453 TEST_P(HttpNetworkTransactionTest, PreconnectErrorReset) { |
| 1416 MockRead read_failure(ASYNC, ERR_CONNECTION_RESET); | 1454 MockRead read_failure(ASYNC, ERR_CONNECTION_RESET); |
| 1417 PreconnectErrorResendRequestTest(NULL, &read_failure); | 1455 PreconnectErrorResendRequestTest(NULL, &read_failure, false); |
| 1418 } | 1456 } |
| 1419 | 1457 |
| 1420 TEST_P(HttpNetworkTransactionTest, PreconnectErrorEOF) { | 1458 TEST_P(HttpNetworkTransactionTest, PreconnectErrorEOF) { |
| 1421 MockRead read_failure(SYNCHRONOUS, OK); // EOF | 1459 MockRead read_failure(SYNCHRONOUS, OK); // EOF |
| 1422 PreconnectErrorResendRequestTest(NULL, &read_failure); | 1460 PreconnectErrorResendRequestTest(NULL, &read_failure, false); |
| 1461 } |
| 1462 |
| 1463 TEST_P(HttpNetworkTransactionTest, PreconnectErrorAsyncEOF) { |
| 1464 MockRead read_failure(ASYNC, OK); // EOF |
| 1465 PreconnectErrorResendRequestTest(NULL, &read_failure, false); |
| 1466 } |
| 1467 |
| 1468 TEST_P(HttpNetworkTransactionTest, |
| 1469 SpdyPreconnectErrorNotConnectedOnWrite) { |
| 1470 MockWrite write_failure(ASYNC, ERR_SOCKET_NOT_CONNECTED); |
| 1471 PreconnectErrorResendRequestTest(&write_failure, NULL, true); |
| 1472 } |
| 1473 |
| 1474 TEST_P(HttpNetworkTransactionTest, SpdyPreconnectErrorReset) { |
| 1475 MockRead read_failure(ASYNC, ERR_CONNECTION_RESET); |
| 1476 PreconnectErrorResendRequestTest(NULL, &read_failure, true); |
| 1477 } |
| 1478 |
| 1479 TEST_P(HttpNetworkTransactionTest, SpdyPreconnectErrorEOF) { |
| 1480 MockRead read_failure(SYNCHRONOUS, OK); // EOF |
| 1481 PreconnectErrorResendRequestTest(NULL, &read_failure, true); |
| 1482 } |
| 1483 |
| 1484 TEST_P(HttpNetworkTransactionTest, SpdyPreconnectErrorAsyncEOF) { |
| 1485 MockRead read_failure(ASYNC, OK); // EOF |
| 1486 PreconnectErrorResendRequestTest(NULL, &read_failure, true); |
| 1423 } | 1487 } |
| 1424 | 1488 |
| 1425 TEST_P(HttpNetworkTransactionTest, NonKeepAliveConnectionReset) { | 1489 TEST_P(HttpNetworkTransactionTest, NonKeepAliveConnectionReset) { |
| 1426 HttpRequestInfo request; | 1490 HttpRequestInfo request; |
| 1427 request.method = "GET"; | 1491 request.method = "GET"; |
| 1428 request.url = GURL("http://www.google.com/"); | 1492 request.url = GURL("http://www.google.com/"); |
| 1429 request.load_flags = 0; | 1493 request.load_flags = 0; |
| 1430 | 1494 |
| 1431 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | 1495 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 1432 scoped_ptr<HttpTransaction> trans( | 1496 scoped_ptr<HttpTransaction> trans( |
| (...skipping 11110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 12543 // established, to let the HTTP request start. | 12607 // established, to let the HTTP request start. |
| 12544 ASSERT_EQ(OK, http_callback.WaitForResult()); | 12608 ASSERT_EQ(OK, http_callback.WaitForResult()); |
| 12545 std::string response_data; | 12609 std::string response_data; |
| 12546 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data)); | 12610 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data)); |
| 12547 EXPECT_EQ("falafel", response_data); | 12611 EXPECT_EQ("falafel", response_data); |
| 12548 | 12612 |
| 12549 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session)); | 12613 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session)); |
| 12550 } | 12614 } |
| 12551 | 12615 |
| 12552 } // namespace net | 12616 } // namespace net |
| OLD | NEW |