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