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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 int GetIdleSocketCountInTransportSocketPool(net::HttpNetworkSession* session) { | 88 int GetIdleSocketCountInTransportSocketPool(net::HttpNetworkSession* session) { |
89 return session->GetTransportSocketPool( | 89 return session->GetTransportSocketPool( |
90 net::HttpNetworkSession::NORMAL_SOCKET_POOL)->IdleSocketCount(); | 90 net::HttpNetworkSession::NORMAL_SOCKET_POOL)->IdleSocketCount(); |
91 } | 91 } |
92 | 92 |
93 int GetIdleSocketCountInSSLSocketPool(net::HttpNetworkSession* session) { | 93 int GetIdleSocketCountInSSLSocketPool(net::HttpNetworkSession* session) { |
94 return session->GetSSLSocketPool( | 94 return session->GetSSLSocketPool( |
95 net::HttpNetworkSession::NORMAL_SOCKET_POOL)->IdleSocketCount(); | 95 net::HttpNetworkSession::NORMAL_SOCKET_POOL)->IdleSocketCount(); |
96 } | 96 } |
97 | 97 |
| 98 bool IsTransportSocketPoolStalled(net::HttpNetworkSession* session) { |
| 99 return session->GetTransportSocketPool( |
| 100 net::HttpNetworkSession::NORMAL_SOCKET_POOL)->IsStalled(); |
| 101 } |
| 102 |
98 // Takes in a Value created from a NetLogHttpResponseParameter, and returns | 103 // Takes in a Value created from a NetLogHttpResponseParameter, and returns |
99 // a JSONified list of headers as a single string. Uses single quotes instead | 104 // a JSONified list of headers as a single string. Uses single quotes instead |
100 // of double quotes for easier comparison. Returns false on failure. | 105 // of double quotes for easier comparison. Returns false on failure. |
101 bool GetHeaders(base::DictionaryValue* params, std::string* headers) { | 106 bool GetHeaders(base::DictionaryValue* params, std::string* headers) { |
102 if (!params) | 107 if (!params) |
103 return false; | 108 return false; |
104 base::ListValue* header_list; | 109 base::ListValue* header_list; |
105 if (!params->GetList("headers", &header_list)) | 110 if (!params->GetList("headers", &header_list)) |
106 return false; | 111 return false; |
107 std::string double_quote_headers; | 112 std::string double_quote_headers; |
(...skipping 11881 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
11989 fake_factory->last_stream_request(); | 11994 fake_factory->last_stream_request(); |
11990 ASSERT_TRUE(fake_request != NULL); | 11995 ASSERT_TRUE(fake_request != NULL); |
11991 base::WeakPtr<FakeStream> fake_stream = fake_request->FinishStreamRequest(); | 11996 base::WeakPtr<FakeStream> fake_stream = fake_request->FinishStreamRequest(); |
11992 ASSERT_TRUE(fake_stream != NULL); | 11997 ASSERT_TRUE(fake_stream != NULL); |
11993 EXPECT_EQ(LOW, fake_stream->priority()); | 11998 EXPECT_EQ(LOW, fake_stream->priority()); |
11994 | 11999 |
11995 trans.SetPriority(LOWEST); | 12000 trans.SetPriority(LOWEST); |
11996 EXPECT_EQ(LOWEST, fake_stream->priority()); | 12001 EXPECT_EQ(LOWEST, fake_stream->priority()); |
11997 } | 12002 } |
11998 | 12003 |
| 12004 // Tests that when a used socket is returned to the SSL socket pool, it's closed |
| 12005 // if the transport socket pool is stalled on the global socket limit. |
| 12006 TEST_P(HttpNetworkTransactionTest, CloseSSLSocketOnIdleForHttpRequest) { |
| 12007 ClientSocketPoolManager::set_max_sockets_per_group( |
| 12008 HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 12009 ClientSocketPoolManager::set_max_sockets_per_pool( |
| 12010 HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 12011 |
| 12012 // Set up SSL request. |
| 12013 |
| 12014 HttpRequestInfo ssl_request; |
| 12015 ssl_request.method = "GET"; |
| 12016 ssl_request.url = GURL("https://www.google.com/"); |
| 12017 |
| 12018 MockWrite ssl_writes[] = { |
| 12019 MockWrite("GET / HTTP/1.1\r\n" |
| 12020 "Host: www.google.com\r\n" |
| 12021 "Connection: keep-alive\r\n\r\n"), |
| 12022 }; |
| 12023 MockRead ssl_reads[] = { |
| 12024 MockRead("HTTP/1.1 200 OK\r\n"), |
| 12025 MockRead("Content-Length: 11\r\n\r\n"), |
| 12026 MockRead("hello world"), |
| 12027 MockRead(SYNCHRONOUS, OK), |
| 12028 }; |
| 12029 StaticSocketDataProvider ssl_data(ssl_reads, arraysize(ssl_reads), |
| 12030 ssl_writes, arraysize(ssl_writes)); |
| 12031 session_deps_.socket_factory->AddSocketDataProvider(&ssl_data); |
| 12032 |
| 12033 SSLSocketDataProvider ssl(ASYNC, OK); |
| 12034 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 12035 |
| 12036 // Set up HTTP request. |
| 12037 |
| 12038 HttpRequestInfo http_request; |
| 12039 http_request.method = "GET"; |
| 12040 http_request.url = GURL("http://www.google.com/"); |
| 12041 |
| 12042 MockWrite http_writes[] = { |
| 12043 MockWrite("GET / HTTP/1.1\r\n" |
| 12044 "Host: www.google.com\r\n" |
| 12045 "Connection: keep-alive\r\n\r\n"), |
| 12046 }; |
| 12047 MockRead http_reads[] = { |
| 12048 MockRead("HTTP/1.1 200 OK\r\n"), |
| 12049 MockRead("Content-Length: 7\r\n\r\n"), |
| 12050 MockRead("falafel"), |
| 12051 MockRead(SYNCHRONOUS, OK), |
| 12052 }; |
| 12053 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), |
| 12054 http_writes, arraysize(http_writes)); |
| 12055 session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 12056 |
| 12057 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 12058 |
| 12059 // Start the SSL request. |
| 12060 TestCompletionCallback ssl_callback; |
| 12061 scoped_ptr<HttpTransaction> ssl_trans( |
| 12062 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); |
| 12063 ASSERT_EQ(ERR_IO_PENDING, |
| 12064 ssl_trans->Start(&ssl_request, ssl_callback.callback(), |
| 12065 BoundNetLog())); |
| 12066 |
| 12067 // Start the HTTP request. Pool should stall. |
| 12068 TestCompletionCallback http_callback; |
| 12069 scoped_ptr<HttpTransaction> http_trans( |
| 12070 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); |
| 12071 ASSERT_EQ(ERR_IO_PENDING, |
| 12072 http_trans->Start(&http_request, http_callback.callback(), |
| 12073 BoundNetLog())); |
| 12074 EXPECT_TRUE(IsTransportSocketPoolStalled(session)); |
| 12075 |
| 12076 // Wait for response from SSL request. |
| 12077 ASSERT_EQ(OK, ssl_callback.WaitForResult()); |
| 12078 std::string response_data; |
| 12079 ASSERT_EQ(OK, ReadTransaction(ssl_trans.get(), &response_data)); |
| 12080 EXPECT_EQ("hello world", response_data); |
| 12081 |
| 12082 // The SSL socket should automatically be closed, so the HTTP request can |
| 12083 // start. |
| 12084 EXPECT_EQ(0, GetIdleSocketCountInSSLSocketPool(session)); |
| 12085 ASSERT_FALSE(IsTransportSocketPoolStalled(session)); |
| 12086 |
| 12087 // The HTTP request can now complete. |
| 12088 ASSERT_EQ(OK, http_callback.WaitForResult()); |
| 12089 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data)); |
| 12090 EXPECT_EQ("falafel", response_data); |
| 12091 |
| 12092 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session)); |
| 12093 } |
| 12094 |
| 12095 // Tests that when a SSL connection is established but there's no corresponding |
| 12096 // request that needs it, the new socket is closed if the transport socket pool |
| 12097 // is stalled on the global socket limit. |
| 12098 TEST_P(HttpNetworkTransactionTest, CloseSSLSocketOnIdleForHttpRequest2) { |
| 12099 ClientSocketPoolManager::set_max_sockets_per_group( |
| 12100 HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 12101 ClientSocketPoolManager::set_max_sockets_per_pool( |
| 12102 HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 12103 |
| 12104 // Set up an ssl request. |
| 12105 |
| 12106 HttpRequestInfo ssl_request; |
| 12107 ssl_request.method = "GET"; |
| 12108 ssl_request.url = GURL("https://www.foopy.com/"); |
| 12109 |
| 12110 // No data will be sent on the SSL socket. |
| 12111 StaticSocketDataProvider ssl_data; |
| 12112 session_deps_.socket_factory->AddSocketDataProvider(&ssl_data); |
| 12113 |
| 12114 SSLSocketDataProvider ssl(ASYNC, OK); |
| 12115 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 12116 |
| 12117 // Set up HTTP request. |
| 12118 |
| 12119 HttpRequestInfo http_request; |
| 12120 http_request.method = "GET"; |
| 12121 http_request.url = GURL("http://www.google.com/"); |
| 12122 |
| 12123 MockWrite http_writes[] = { |
| 12124 MockWrite("GET / HTTP/1.1\r\n" |
| 12125 "Host: www.google.com\r\n" |
| 12126 "Connection: keep-alive\r\n\r\n"), |
| 12127 }; |
| 12128 MockRead http_reads[] = { |
| 12129 MockRead("HTTP/1.1 200 OK\r\n"), |
| 12130 MockRead("Content-Length: 7\r\n\r\n"), |
| 12131 MockRead("falafel"), |
| 12132 MockRead(SYNCHRONOUS, OK), |
| 12133 }; |
| 12134 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), |
| 12135 http_writes, arraysize(http_writes)); |
| 12136 session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 12137 |
| 12138 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 12139 |
| 12140 // Preconnect an SSL socket. A preconnect is needed because connect jobs are |
| 12141 // cancelled when a normal transaction is cancelled. |
| 12142 net::HttpStreamFactory* http_stream_factory = session->http_stream_factory(); |
| 12143 net::SSLConfig ssl_config; |
| 12144 session->ssl_config_service()->GetSSLConfig(&ssl_config); |
| 12145 http_stream_factory->PreconnectStreams(1, ssl_request, DEFAULT_PRIORITY, |
| 12146 ssl_config, ssl_config); |
| 12147 EXPECT_EQ(0, GetIdleSocketCountInSSLSocketPool(session)); |
| 12148 |
| 12149 // Start the HTTP request. Pool should stall. |
| 12150 TestCompletionCallback http_callback; |
| 12151 scoped_ptr<HttpTransaction> http_trans( |
| 12152 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); |
| 12153 ASSERT_EQ(ERR_IO_PENDING, |
| 12154 http_trans->Start(&http_request, http_callback.callback(), |
| 12155 BoundNetLog())); |
| 12156 EXPECT_TRUE(IsTransportSocketPoolStalled(session)); |
| 12157 |
| 12158 // The SSL connection will automatically be closed once the connection is |
| 12159 // established, to let the HTTP request start. |
| 12160 ASSERT_EQ(OK, http_callback.WaitForResult()); |
| 12161 std::string response_data; |
| 12162 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data)); |
| 12163 EXPECT_EQ("falafel", response_data); |
| 12164 |
| 12165 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session)); |
| 12166 } |
| 12167 |
11999 } // namespace net | 12168 } // namespace net |
OLD | NEW |