[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 5 | #include "net/spdy/spdy_session_pool.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 6 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 7 | #include "base/logging.h" |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 8 | #include "base/metrics/histogram.h" |
[email protected] | 1ce7b66b | 2010-10-12 20:32:44 | [diff] [blame] | 9 | #include "base/values.h" |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 10 | #include "net/base/address_list.h" |
[email protected] | f458033 | 2010-09-25 21:20:27 | [diff] [blame] | 11 | #include "net/http/http_network_session.h" |
[email protected] | 53bfa31c | 2011-11-15 19:20:31 | [diff] [blame] | 12 | #include "net/http/http_server_properties.h" |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 13 | #include "net/spdy/spdy_session.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 14 | |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 15 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 16 | namespace net { |
| 17 | |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | enum SpdySessionGetTypes { |
| 21 | CREATED_NEW = 0, |
| 22 | FOUND_EXISTING = 1, |
| 23 | FOUND_EXISTING_FROM_IP_POOL = 2, |
| 24 | IMPORTED_FROM_SOCKET = 3, |
| 25 | SPDY_SESSION_GET_MAX = 4 |
| 26 | }; |
| 27 | |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 28 | } // namespace |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 29 | |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 30 | SpdySessionPool::SpdySessionPool( |
| 31 | HostResolver* resolver, |
| 32 | SSLConfigService* ssl_config_service, |
| 33 | HttpServerProperties* http_server_properties, |
[email protected] | f9cf557 | 2012-12-04 15:52:09 | [diff] [blame] | 34 | bool force_single_domain, |
| 35 | bool enable_ip_pooling, |
| 36 | bool enable_credential_frames, |
| 37 | bool enable_compression, |
| 38 | bool enable_ping_based_connection_checking, |
| 39 | NextProto default_protocol, |
[email protected] | a3827a0 | 2013-02-22 00:04:32 | [diff] [blame] | 40 | size_t stream_initial_recv_window_size, |
[email protected] | f9cf557 | 2012-12-04 15:52:09 | [diff] [blame] | 41 | size_t initial_max_concurrent_streams, |
| 42 | size_t max_concurrent_streams_limit, |
| 43 | SpdySessionPool::TimeFunc time_func, |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 44 | const std::string& trusted_spdy_proxy) |
[email protected] | 53bfa31c | 2011-11-15 19:20:31 | [diff] [blame] | 45 | : http_server_properties_(http_server_properties), |
| 46 | ssl_config_service_(ssl_config_service), |
[email protected] | b9ec688 | 2011-07-01 07:40:26 | [diff] [blame] | 47 | resolver_(resolver), |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 48 | verify_domain_authentication_(true), |
[email protected] | e8bde62 | 2012-06-14 23:20:24 | [diff] [blame] | 49 | enable_sending_initial_settings_(true), |
[email protected] | f9cf557 | 2012-12-04 15:52:09 | [diff] [blame] | 50 | force_single_domain_(force_single_domain), |
| 51 | enable_ip_pooling_(enable_ip_pooling), |
| 52 | enable_credential_frames_(enable_credential_frames), |
| 53 | enable_compression_(enable_compression), |
| 54 | enable_ping_based_connection_checking_( |
| 55 | enable_ping_based_connection_checking), |
| 56 | default_protocol_(default_protocol), |
[email protected] | a3827a0 | 2013-02-22 00:04:32 | [diff] [blame] | 57 | stream_initial_recv_window_size_(stream_initial_recv_window_size), |
[email protected] | f9cf557 | 2012-12-04 15:52:09 | [diff] [blame] | 58 | initial_max_concurrent_streams_(initial_max_concurrent_streams), |
| 59 | max_concurrent_streams_limit_(max_concurrent_streams_limit), |
| 60 | time_func_(time_func), |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 61 | trusted_spdy_proxy_( |
| 62 | HostPortPair::FromString(trusted_spdy_proxy)) { |
[email protected] | 232a581 | 2011-03-04 22:42:08 | [diff] [blame] | 63 | NetworkChangeNotifier::AddIPAddressObserver(this); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 64 | if (ssl_config_service_.get()) |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 65 | ssl_config_service_->AddObserver(this); |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 66 | CertDatabase::GetInstance()->AddObserver(this); |
[email protected] | b846acd | 2010-06-07 18:13:10 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 69 | SpdySessionPool::~SpdySessionPool() { |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 70 | CloseAllSessions(); |
[email protected] | b846acd | 2010-06-07 18:13:10 | [diff] [blame] | 71 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 72 | if (ssl_config_service_.get()) |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 73 | ssl_config_service_->RemoveObserver(this); |
[email protected] | 232a581 | 2011-03-04 22:42:08 | [diff] [blame] | 74 | NetworkChangeNotifier::RemoveIPAddressObserver(this); |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 75 | CertDatabase::GetInstance()->RemoveObserver(this); |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 76 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 77 | |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 78 | scoped_refptr<SpdySession> SpdySessionPool::GetIfExists( |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 79 | const SpdySessionKey& spdy_session_key, |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 80 | const BoundNetLog& net_log) { |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 81 | SpdySessionsMap::iterator it = FindSessionByKey(spdy_session_key); |
| 82 | if (it != sessions_.end()) { |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 83 | UMA_HISTOGRAM_ENUMERATION("Net.SpdySessionGet", |
| 84 | FOUND_EXISTING, |
| 85 | SPDY_SESSION_GET_MAX); |
[email protected] | 00cd9c4 | 2010-11-02 20:15:57 | [diff] [blame] | 86 | net_log.AddEvent( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 87 | NetLog::TYPE_SPDY_SESSION_POOL_FOUND_EXISTING_SESSION, |
| 88 | it->second->net_log().source().ToEventParametersCallback()); |
| 89 | return it->second; |
| 90 | } |
| 91 | |
| 92 | // Check if we have a Session through a domain alias. |
| 93 | scoped_refptr<SpdySession> spdy_session = |
| 94 | GetFromAlias(spdy_session_key, net_log, true); |
| 95 | if (spdy_session) { |
| 96 | UMA_HISTOGRAM_ENUMERATION("Net.SpdySessionGet", |
| 97 | FOUND_EXISTING_FROM_IP_POOL, |
| 98 | SPDY_SESSION_GET_MAX); |
| 99 | net_log.AddEvent( |
| 100 | NetLog::TYPE_SPDY_SESSION_POOL_FOUND_EXISTING_SESSION_FROM_IP_POOL, |
| 101 | spdy_session->net_log().source().ToEventParametersCallback()); |
| 102 | // Add this session to the map so that we can find it next time. |
| 103 | AddSession(spdy_session_key, spdy_session); |
| 104 | spdy_session->AddPooledAlias(spdy_session_key); |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 105 | return spdy_session; |
[email protected] | 6cd3bd20 | 2010-08-30 05:23:06 | [diff] [blame] | 106 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 107 | |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 108 | return scoped_refptr<SpdySession>(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 111 | net::Error SpdySessionPool::GetSpdySessionFromSocket( |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 112 | const SpdySessionKey& spdy_session_key, |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 113 | scoped_ptr<ClientSocketHandle> connection, |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 114 | const BoundNetLog& net_log, |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 115 | int certificate_error_code, |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 116 | scoped_refptr<SpdySession>* spdy_session, |
| 117 | bool is_secure) { |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 118 | UMA_HISTOGRAM_ENUMERATION("Net.SpdySessionGet", |
| 119 | IMPORTED_FROM_SOCKET, |
| 120 | SPDY_SESSION_GET_MAX); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 121 | // Create the SPDY session and add it to the pool. |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 122 | *spdy_session = new SpdySession(spdy_session_key, this, |
[email protected] | 53bfa31c | 2011-11-15 19:20:31 | [diff] [blame] | 123 | http_server_properties_, |
[email protected] | b9ec688 | 2011-07-01 07:40:26 | [diff] [blame] | 124 | verify_domain_authentication_, |
[email protected] | e8bde62 | 2012-06-14 23:20:24 | [diff] [blame] | 125 | enable_sending_initial_settings_, |
[email protected] | f9cf557 | 2012-12-04 15:52:09 | [diff] [blame] | 126 | enable_credential_frames_, |
| 127 | enable_compression_, |
| 128 | enable_ping_based_connection_checking_, |
| 129 | default_protocol_, |
[email protected] | a3827a0 | 2013-02-22 00:04:32 | [diff] [blame] | 130 | stream_initial_recv_window_size_, |
[email protected] | f9cf557 | 2012-12-04 15:52:09 | [diff] [blame] | 131 | initial_max_concurrent_streams_, |
| 132 | max_concurrent_streams_limit_, |
| 133 | time_func_, |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 134 | trusted_spdy_proxy_, |
[email protected] | f458033 | 2010-09-25 21:20:27 | [diff] [blame] | 135 | net_log.net_log()); |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 136 | AddSession(spdy_session_key, *spdy_session); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 137 | |
[email protected] | 00cd9c4 | 2010-11-02 20:15:57 | [diff] [blame] | 138 | net_log.AddEvent( |
| 139 | NetLog::TYPE_SPDY_SESSION_POOL_IMPORTED_SESSION_FROM_SOCKET, |
[email protected] | b618d1e | 2012-06-13 20:45:43 | [diff] [blame] | 140 | (*spdy_session)->net_log().source().ToEventParametersCallback()); |
[email protected] | 6cd3bd20 | 2010-08-30 05:23:06 | [diff] [blame] | 141 | |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 142 | // We have a new session. Lookup the IP address for this session so that we |
| 143 | // can match future Sessions (potentially to different domains) which can |
| 144 | // potentially be pooled with this one. Because GetPeerAddress() reports the |
| 145 | // proxy's address instead of the origin server, check to see if this is a |
| 146 | // direct connection. |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 147 | if (enable_ip_pooling_ && |
| 148 | spdy_session_key.proxy_server().is_direct()) { |
[email protected] | a352869 | 2012-06-08 00:11:42 | [diff] [blame] | 149 | IPEndPoint address; |
| 150 | if (connection->socket()->GetPeerAddress(&address) == OK) |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 151 | aliases_[address] = spdy_session_key; |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 154 | // Now we can initialize the session with the SSL socket. |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 155 | return (*spdy_session)->InitializeWithSocket(connection.Pass(), is_secure, |
[email protected] | 6cd3bd20 | 2010-08-30 05:23:06 | [diff] [blame] | 156 | certificate_error_code); |
[email protected] | 9f7c4fd | 2009-11-24 18:50:15 | [diff] [blame] | 157 | } |
| 158 | |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 159 | // Make a copy of |sessions_| in the Close* functions below to avoid |
| 160 | // reentrancy problems. Due to aliases, it doesn't suffice to simply |
| 161 | // increment the iterator before closing. |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 162 | |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 163 | void SpdySessionPool::CloseCurrentSessions(net::Error error) { |
| 164 | SpdySessionsMap sessions_copy = sessions_; |
| 165 | for (SpdySessionsMap::const_iterator it = sessions_copy.begin(); |
| 166 | it != sessions_copy.end(); ++it) { |
| 167 | TryCloseSession(it->first, error, "Closing current sessions."); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void SpdySessionPool::CloseCurrentIdleSessions() { |
| 172 | SpdySessionsMap sessions_copy = sessions_; |
| 173 | for (SpdySessionsMap::const_iterator it = sessions_copy.begin(); |
| 174 | it != sessions_copy.end(); ++it) { |
| 175 | if (!it->second->is_active()) |
| 176 | TryCloseSession(it->first, ERR_ABORTED, "Closing idle sessions."); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void SpdySessionPool::CloseAllSessions() { |
| 181 | while (!sessions_.empty()) { |
| 182 | SpdySessionsMap sessions_copy = sessions_; |
| 183 | for (SpdySessionsMap::const_iterator it = sessions_copy.begin(); |
| 184 | it != sessions_copy.end(); ++it) { |
| 185 | TryCloseSession(it->first, ERR_ABORTED, "Closing all sessions."); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void SpdySessionPool::TryCloseSession(const SpdySessionKey& key, |
| 191 | net::Error error, |
| 192 | const std::string& description) { |
| 193 | SpdySessionsMap::const_iterator it = sessions_.find(key); |
| 194 | if (it == sessions_.end()) |
| 195 | return; |
| 196 | scoped_refptr<SpdySession> session = it->second; |
| 197 | session->CloseSessionOnError(error, description); |
| 198 | if (DCHECK_IS_ON()) { |
| 199 | it = sessions_.find(key); |
| 200 | // A new session with the same key may have been added, but it |
| 201 | // must not be the one we just closed. |
| 202 | if (it != sessions_.end()) |
| 203 | DCHECK_NE(it->second, session); |
| 204 | } |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 205 | } |
| 206 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 207 | void SpdySessionPool::Remove(const scoped_refptr<SpdySession>& session) { |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 208 | RemoveSession(session->spdy_session_key()); |
[email protected] | 00cd9c4 | 2010-11-02 20:15:57 | [diff] [blame] | 209 | session->net_log().AddEvent( |
| 210 | NetLog::TYPE_SPDY_SESSION_POOL_REMOVE_SESSION, |
[email protected] | b618d1e | 2012-06-13 20:45:43 | [diff] [blame] | 211 | session->net_log().source().ToEventParametersCallback()); |
[email protected] | 6cbfa85 | 2012-03-14 06:35:54 | [diff] [blame] | 212 | |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 213 | const std::set<SpdySessionKey>& aliases = session->pooled_aliases(); |
| 214 | for (std::set<SpdySessionKey>::const_iterator it = aliases.begin(); |
[email protected] | 6cbfa85 | 2012-03-14 06:35:54 | [diff] [blame] | 215 | it != aliases.end(); ++it) { |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 216 | RemoveSession(*it); |
[email protected] | 6cbfa85 | 2012-03-14 06:35:54 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 220 | base::Value* SpdySessionPool::SpdySessionPoolInfoToValue() const { |
| 221 | base::ListValue* list = new base::ListValue(); |
[email protected] | 1ce7b66b | 2010-10-12 20:32:44 | [diff] [blame] | 222 | |
[email protected] | 1ce7b66b | 2010-10-12 20:32:44 | [diff] [blame] | 223 | for (SpdySessionsMap::const_iterator it = sessions_.begin(); |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 224 | it != sessions_.end(); ++it) { |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 225 | // Only add the session if the key in the map matches the main |
| 226 | // host_port_proxy_pair (not an alias). |
| 227 | const SpdySessionKey& key = it->first; |
| 228 | const SpdySessionKey& session_key = it->second->spdy_session_key(); |
| 229 | if (key.Equals(session_key)) |
| 230 | list->Append(it->second->GetInfoAsValue()); |
[email protected] | 1ce7b66b | 2010-10-12 20:32:44 | [diff] [blame] | 231 | } |
| 232 | return list; |
| 233 | } |
| 234 | |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 235 | void SpdySessionPool::OnIPAddressChanged() { |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 236 | CloseCurrentSessions(ERR_NETWORK_CHANGED); |
[email protected] | 6dd1134e | 2013-04-30 21:13:09 | [diff] [blame] | 237 | http_server_properties_->ClearAllSpdySettings(); |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 238 | } |
| 239 | |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 240 | void SpdySessionPool::OnSSLConfigChanged() { |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 241 | CloseCurrentSessions(ERR_NETWORK_CHANGED); |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 242 | } |
| 243 | |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 244 | scoped_refptr<SpdySession> SpdySessionPool::GetFromAlias( |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 245 | const SpdySessionKey& spdy_session_key, |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 246 | const BoundNetLog& net_log, |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 247 | bool record_histograms) { |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 248 | // We should only be checking aliases when there is no direct session. |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 249 | DCHECK(FindSessionByKey(spdy_session_key) == sessions_.end()); |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 250 | |
[email protected] | f9cf557 | 2012-12-04 15:52:09 | [diff] [blame] | 251 | if (!enable_ip_pooling_) |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 252 | return NULL; |
| 253 | |
| 254 | AddressList addresses; |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 255 | if (!LookupAddresses(spdy_session_key, net_log, &addresses)) |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 256 | return NULL; |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 257 | for (AddressList::const_iterator iter = addresses.begin(); |
| 258 | iter != addresses.end(); |
| 259 | ++iter) { |
| 260 | SpdyAliasMap::const_iterator alias_iter = aliases_.find(*iter); |
| 261 | if (alias_iter == aliases_.end()) |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 262 | continue; |
| 263 | |
| 264 | // We found an alias. |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 265 | const SpdySessionKey& alias_key = alias_iter->second; |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 266 | |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 267 | // If the proxy and privacy settings match, we can reuse this session. |
| 268 | if (!(alias_key.proxy_server() == spdy_session_key.proxy_server()) || |
| 269 | !(alias_key.privacy_mode() == |
| 270 | spdy_session_key.privacy_mode())) |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 271 | continue; |
| 272 | |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 273 | SpdySessionsMap::iterator it = FindSessionByKey(alias_key); |
| 274 | if (it == sessions_.end()) { |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 275 | NOTREACHED(); // It shouldn't be in the aliases table if we can't get it! |
| 276 | continue; |
| 277 | } |
| 278 | |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 279 | scoped_refptr<SpdySession> spdy_session = it->second; |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 280 | // If the SPDY session is a secure one, we need to verify that the server |
| 281 | // is authenticated to serve traffic for |host_port_proxy_pair| too. |
| 282 | if (!spdy_session->VerifyDomainAuthentication( |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 283 | spdy_session_key.host_port_pair().host())) { |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 284 | if (record_histograms) |
| 285 | UMA_HISTOGRAM_ENUMERATION("Net.SpdyIPPoolDomainMatch", 0, 2); |
| 286 | continue; |
| 287 | } |
| 288 | if (record_histograms) |
| 289 | UMA_HISTOGRAM_ENUMERATION("Net.SpdyIPPoolDomainMatch", 1, 2); |
| 290 | return spdy_session; |
| 291 | } |
| 292 | return NULL; |
| 293 | } |
| 294 | |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 295 | void SpdySessionPool::OnCertAdded(const X509Certificate* cert) { |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 296 | CloseCurrentSessions(ERR_NETWORK_CHANGED); |
[email protected] | c940d37 | 2011-04-13 17:20:18 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void SpdySessionPool::OnCertTrustChanged(const X509Certificate* cert) { |
| 300 | // Per wtc, we actually only need to CloseCurrentSessions when trust is |
| 301 | // reduced. CloseCurrentSessions now because OnCertTrustChanged does not |
| 302 | // tell us this. |
| 303 | // See comments in ClientSocketPoolManager::OnCertTrustChanged. |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 304 | CloseCurrentSessions(ERR_NETWORK_CHANGED); |
[email protected] | 62635c7 | 2011-03-10 04:16:25 | [diff] [blame] | 305 | } |
| 306 | |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 307 | const SpdySessionKey& SpdySessionPool::NormalizeListKey( |
| 308 | const SpdySessionKey& spdy_session_key) const { |
[email protected] | f9cf557 | 2012-12-04 15:52:09 | [diff] [blame] | 309 | if (!force_single_domain_) |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 310 | return spdy_session_key; |
[email protected] | ea1cc2cd | 2011-02-22 16:47:38 | [diff] [blame] | 311 | |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 312 | static SpdySessionKey* single_domain_key = NULL; |
| 313 | if (!single_domain_key) { |
[email protected] | ea1cc2cd | 2011-02-22 16:47:38 | [diff] [blame] | 314 | HostPortPair single_domain = HostPortPair("singledomain.com", 80); |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 315 | single_domain_key = new SpdySessionKey(single_domain, |
| 316 | ProxyServer::Direct(), |
| 317 | kPrivacyModeDisabled); |
[email protected] | ea1cc2cd | 2011-02-22 16:47:38 | [diff] [blame] | 318 | } |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 319 | return *single_domain_key; |
[email protected] | ea1cc2cd | 2011-02-22 16:47:38 | [diff] [blame] | 320 | } |
| 321 | |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 322 | void SpdySessionPool::AddSession( |
| 323 | const SpdySessionKey& spdy_session_key, |
| 324 | const scoped_refptr<SpdySession>& spdy_session) { |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 325 | const SpdySessionKey& key = NormalizeListKey(spdy_session_key); |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 326 | std::pair<SpdySessionsMap::iterator, bool> result = |
| 327 | sessions_.insert(std::make_pair(key, spdy_session)); |
| 328 | CHECK(result.second); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 329 | } |
| 330 | |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 331 | SpdySessionPool::SpdySessionsMap::iterator |
| 332 | SpdySessionPool::FindSessionByKey(const SpdySessionKey& spdy_session_key) { |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 333 | const SpdySessionKey& key = NormalizeListKey(spdy_session_key); |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 334 | return sessions_.find(key); |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 335 | } |
| 336 | |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame^] | 337 | void SpdySessionPool::RemoveSession(const SpdySessionKey& spdy_session_key) { |
| 338 | SpdySessionsMap::iterator it = FindSessionByKey(spdy_session_key); |
| 339 | CHECK(it != sessions_.end()); |
| 340 | sessions_.erase(it); |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 341 | RemoveAliases(spdy_session_key); |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 342 | } |
| 343 | |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 344 | bool SpdySessionPool::LookupAddresses(const SpdySessionKey& spdy_session_key, |
[email protected] | ae94021 | 2012-03-06 15:44:13 | [diff] [blame] | 345 | const BoundNetLog& net_log, |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 346 | AddressList* addresses) const { |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 347 | net::HostResolver::RequestInfo resolve_info( |
| 348 | spdy_session_key.host_port_pair()); |
[email protected] | ae94021 | 2012-03-06 15:44:13 | [diff] [blame] | 349 | int rv = resolver_->ResolveFromCache(resolve_info, addresses, net_log); |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 350 | DCHECK_NE(ERR_IO_PENDING, rv); |
| 351 | return rv == OK; |
| 352 | } |
| 353 | |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 354 | void SpdySessionPool::RemoveAliases(const SpdySessionKey& spdy_session_key) { |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 355 | // Walk the aliases map, find references to this pair. |
| 356 | // TODO(mbelshe): Figure out if this is too expensive. |
| 357 | SpdyAliasMap::iterator alias_it = aliases_.begin(); |
| 358 | while (alias_it != aliases_.end()) { |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 359 | if (alias_it->second.Equals(spdy_session_key)) { |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 360 | aliases_.erase(alias_it); |
| 361 | alias_it = aliases_.begin(); // Iterator was invalidated. |
| 362 | continue; |
| 363 | } |
| 364 | ++alias_it; |
| 365 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 366 | } |
| 367 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 368 | } // namespace net |