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