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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 int GetIdleSocketCountInTransportSocketPool(net::HttpNetworkSession* session) { | 104 int GetIdleSocketCountInTransportSocketPool(net::HttpNetworkSession* session) { |
105 return session->GetTransportSocketPool( | 105 return session->GetTransportSocketPool( |
106 net::HttpNetworkSession::NORMAL_SOCKET_POOL)->IdleSocketCount(); | 106 net::HttpNetworkSession::NORMAL_SOCKET_POOL)->IdleSocketCount(); |
107 } | 107 } |
108 | 108 |
109 int GetIdleSocketCountInSSLSocketPool(net::HttpNetworkSession* session) { | 109 int GetIdleSocketCountInSSLSocketPool(net::HttpNetworkSession* session) { |
110 return session->GetSSLSocketPool( | 110 return session->GetSSLSocketPool( |
111 net::HttpNetworkSession::NORMAL_SOCKET_POOL)->IdleSocketCount(); | 111 net::HttpNetworkSession::NORMAL_SOCKET_POOL)->IdleSocketCount(); |
112 } | 112 } |
113 | 113 |
| 114 bool IsTransportSocketPoolStalled(net::HttpNetworkSession* session) { |
| 115 return session->GetTransportSocketPool( |
| 116 net::HttpNetworkSession::NORMAL_SOCKET_POOL)->IsStalled(); |
| 117 } |
| 118 |
114 // Takes in a Value created from a NetLogHttpResponseParameter, and returns | 119 // Takes in a Value created from a NetLogHttpResponseParameter, and returns |
115 // a JSONified list of headers as a single string. Uses single quotes instead | 120 // a JSONified list of headers as a single string. Uses single quotes instead |
116 // of double quotes for easier comparison. Returns false on failure. | 121 // of double quotes for easier comparison. Returns false on failure. |
117 bool GetHeaders(base::DictionaryValue* params, std::string* headers) { | 122 bool GetHeaders(base::DictionaryValue* params, std::string* headers) { |
118 if (!params) | 123 if (!params) |
119 return false; | 124 return false; |
120 base::ListValue* header_list; | 125 base::ListValue* header_list; |
121 if (!params->GetList("headers", &header_list)) | 126 if (!params->GetList("headers", &header_list)) |
122 return false; | 127 return false; |
123 std::string double_quote_headers; | 128 std::string double_quote_headers; |
(...skipping 11544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
11668 rv = callback.WaitForResult(); | 11673 rv = callback.WaitForResult(); |
11669 EXPECT_EQ(OK, rv); | 11674 EXPECT_EQ(OK, rv); |
11670 | 11675 |
11671 HttpRequestHeaders request_headers; | 11676 HttpRequestHeaders request_headers; |
11672 EXPECT_TRUE(trans->GetFullRequestHeaders(&request_headers)); | 11677 EXPECT_TRUE(trans->GetFullRequestHeaders(&request_headers)); |
11673 std::string foo; | 11678 std::string foo; |
11674 EXPECT_TRUE(request_headers.GetHeader("X-Foo", &foo)); | 11679 EXPECT_TRUE(request_headers.GetHeader("X-Foo", &foo)); |
11675 EXPECT_EQ("bar", foo); | 11680 EXPECT_EQ("bar", foo); |
11676 } | 11681 } |
11677 | 11682 |
| 11683 // Tests that when a used socket is returned to the SSL socket pool, it's closed |
| 11684 // if the transport socket pool is stalled on the global socket limit. |
| 11685 TEST_P(HttpNetworkTransactionTest, CloseSSLSocketOnIdleForHttpRequest) { |
| 11686 ClientSocketPoolManager::set_max_sockets_per_group( |
| 11687 HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 11688 ClientSocketPoolManager::set_max_sockets_per_pool( |
| 11689 HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 11690 |
| 11691 // Set up SSL request. |
| 11692 |
| 11693 HttpRequestInfo ssl_request; |
| 11694 ssl_request.method = "GET"; |
| 11695 ssl_request.url = GURL("https://www.google.com/"); |
| 11696 |
| 11697 MockWrite ssl_writes[] = { |
| 11698 MockWrite("GET / HTTP/1.1\r\n" |
| 11699 "Host: www.google.com\r\n" |
| 11700 "Connection: keep-alive\r\n\r\n"), |
| 11701 }; |
| 11702 MockRead ssl_reads[] = { |
| 11703 MockRead("HTTP/1.1 200 OK\r\n"), |
| 11704 MockRead("Content-Length: 11\r\n\r\n"), |
| 11705 MockRead("hello world"), |
| 11706 MockRead(SYNCHRONOUS, OK), |
| 11707 }; |
| 11708 StaticSocketDataProvider ssl_data(ssl_reads, arraysize(ssl_reads), |
| 11709 ssl_writes, arraysize(ssl_writes)); |
| 11710 session_deps_.socket_factory->AddSocketDataProvider(&ssl_data); |
| 11711 |
| 11712 SSLSocketDataProvider ssl(ASYNC, OK); |
| 11713 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 11714 |
| 11715 // Set up HTTP request. |
| 11716 |
| 11717 HttpRequestInfo http_request; |
| 11718 http_request.method = "GET"; |
| 11719 http_request.url = GURL("http://www.google.com/"); |
| 11720 |
| 11721 MockWrite http_writes[] = { |
| 11722 MockWrite("GET / HTTP/1.1\r\n" |
| 11723 "Host: www.google.com\r\n" |
| 11724 "Connection: keep-alive\r\n\r\n"), |
| 11725 }; |
| 11726 MockRead http_reads[] = { |
| 11727 MockRead("HTTP/1.1 200 OK\r\n"), |
| 11728 MockRead("Content-Length: 7\r\n\r\n"), |
| 11729 MockRead("falafel"), |
| 11730 MockRead(SYNCHRONOUS, OK), |
| 11731 }; |
| 11732 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), |
| 11733 http_writes, arraysize(http_writes)); |
| 11734 session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 11735 |
| 11736 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 11737 |
| 11738 // Start the SSL request. |
| 11739 TestCompletionCallback ssl_callback; |
| 11740 scoped_ptr<HttpTransaction> ssl_trans( |
| 11741 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); |
| 11742 ASSERT_EQ(ERR_IO_PENDING, |
| 11743 ssl_trans->Start(&ssl_request, ssl_callback.callback(), |
| 11744 BoundNetLog())); |
| 11745 |
| 11746 // Start the HTTP request. Pool should stall. |
| 11747 TestCompletionCallback http_callback; |
| 11748 scoped_ptr<HttpTransaction> http_trans( |
| 11749 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); |
| 11750 ASSERT_EQ(ERR_IO_PENDING, |
| 11751 http_trans->Start(&http_request, http_callback.callback(), |
| 11752 BoundNetLog())); |
| 11753 EXPECT_TRUE(IsTransportSocketPoolStalled(session)); |
| 11754 |
| 11755 // Wait for response from SSL request. |
| 11756 ASSERT_EQ(OK, ssl_callback.WaitForResult()); |
| 11757 std::string response_data; |
| 11758 ASSERT_EQ(OK, ReadTransaction(ssl_trans.get(), &response_data)); |
| 11759 EXPECT_EQ("hello world", response_data); |
| 11760 |
| 11761 // The SSL socket should automatically be closed, so the HTTP request can |
| 11762 // start. |
| 11763 EXPECT_EQ(0, GetIdleSocketCountInSSLSocketPool(session)); |
| 11764 ASSERT_FALSE(IsTransportSocketPoolStalled(session)); |
| 11765 |
| 11766 // The HTTP request can now complete. |
| 11767 ASSERT_EQ(OK, http_callback.WaitForResult()); |
| 11768 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data)); |
| 11769 EXPECT_EQ("falafel", response_data); |
| 11770 |
| 11771 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session)); |
| 11772 } |
| 11773 |
| 11774 // Tests that when a SSL connection is established but there's no corresponding |
| 11775 // request that needs it, the new socket is closed if the transport socket pool |
| 11776 // is stalled on the global socket limit. |
| 11777 TEST_P(HttpNetworkTransactionTest, CloseSSLSocketOnIdleForHttpRequest2) { |
| 11778 ClientSocketPoolManager::set_max_sockets_per_group( |
| 11779 HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 11780 ClientSocketPoolManager::set_max_sockets_per_pool( |
| 11781 HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 11782 |
| 11783 // Set up an ssl request. |
| 11784 |
| 11785 HttpRequestInfo ssl_request; |
| 11786 ssl_request.method = "GET"; |
| 11787 ssl_request.url = GURL("https://www.foopy.com/"); |
| 11788 |
| 11789 // No data will be sent on the SSL socket. |
| 11790 StaticSocketDataProvider ssl_data; |
| 11791 session_deps_.socket_factory->AddSocketDataProvider(&ssl_data); |
| 11792 |
| 11793 SSLSocketDataProvider ssl(ASYNC, OK); |
| 11794 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 11795 |
| 11796 // Set up HTTP request. |
| 11797 |
| 11798 HttpRequestInfo http_request; |
| 11799 http_request.method = "GET"; |
| 11800 http_request.url = GURL("http://www.google.com/"); |
| 11801 |
| 11802 MockWrite http_writes[] = { |
| 11803 MockWrite("GET / HTTP/1.1\r\n" |
| 11804 "Host: www.google.com\r\n" |
| 11805 "Connection: keep-alive\r\n\r\n"), |
| 11806 }; |
| 11807 MockRead http_reads[] = { |
| 11808 MockRead("HTTP/1.1 200 OK\r\n"), |
| 11809 MockRead("Content-Length: 7\r\n\r\n"), |
| 11810 MockRead("falafel"), |
| 11811 MockRead(SYNCHRONOUS, OK), |
| 11812 }; |
| 11813 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), |
| 11814 http_writes, arraysize(http_writes)); |
| 11815 session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 11816 |
| 11817 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 11818 |
| 11819 // Preconnect an SSL socket. A preconnect is needed because connect jobs are |
| 11820 // cancelled when a normal transaction is cancelled. |
| 11821 net::HttpStreamFactory* http_stream_factory = session->http_stream_factory(); |
| 11822 net::SSLConfig ssl_config; |
| 11823 session->ssl_config_service()->GetSSLConfig(&ssl_config); |
| 11824 http_stream_factory->PreconnectStreams(1, ssl_request, DEFAULT_PRIORITY, |
| 11825 ssl_config, ssl_config); |
| 11826 EXPECT_EQ(0, GetIdleSocketCountInSSLSocketPool(session)); |
| 11827 |
| 11828 // Start the HTTP request. Pool should stall. |
| 11829 TestCompletionCallback http_callback; |
| 11830 scoped_ptr<HttpTransaction> http_trans( |
| 11831 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); |
| 11832 ASSERT_EQ(ERR_IO_PENDING, |
| 11833 http_trans->Start(&http_request, http_callback.callback(), |
| 11834 BoundNetLog())); |
| 11835 EXPECT_TRUE(IsTransportSocketPoolStalled(session)); |
| 11836 |
| 11837 // The SSL connection will automatically be closed once the connection is |
| 11838 // established, to let the HTTP request start. |
| 11839 ASSERT_EQ(OK, http_callback.WaitForResult()); |
| 11840 std::string response_data; |
| 11841 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data)); |
| 11842 EXPECT_EQ("falafel", response_data); |
| 11843 |
| 11844 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session)); |
| 11845 } |
| 11846 |
11678 } // namespace net | 11847 } // namespace net |
OLD | NEW |