Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_server_properties_impl.h" | 5 #include "net/http/http_server_properties_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "net/http/http_pipelined_host_capability.h" | 12 #include "net/http/http_pipelined_host_capability.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 // TODO(simonjam): Run experiments with different values of this to see what | 16 // TODO(simonjam): Run experiments with different values of this to see what |
| 17 // value is good at avoiding evictions without eating too much memory. Until | 17 // value is good at avoiding evictions without eating too much memory. Until |
| 18 // then, this is just a bad guess. | 18 // then, this is just a bad guess. |
| 19 static const int kDefaultNumHostsToRemember = 200; | 19 static const int kDefaultNumHostsToRemember = 200; |
| 20 | 20 |
| 21 HttpServerPropertiesImpl::HttpServerPropertiesImpl() | 21 HttpServerPropertiesImpl::HttpServerPropertiesImpl() |
| 22 : pipeline_capability_map_( | 22 : alternate_protocol_map_(new CachedAlternateProtocolMap( |
| 23 CachedAlternateProtocolMap::NO_AUTO_EVICT)), | |
|
ramant (doing other things)
2014/03/05 00:01:30
rch: Should we keep fewer entries in the memory? A
| |
| 24 pipeline_capability_map_( | |
| 23 new CachedPipelineCapabilityMap(kDefaultNumHostsToRemember)), | 25 new CachedPipelineCapabilityMap(kDefaultNumHostsToRemember)), |
| 24 weak_ptr_factory_(this) { | 26 weak_ptr_factory_(this) { |
| 25 canoncial_suffixes_.push_back(".c.youtube.com"); | 27 canoncial_suffixes_.push_back(".c.youtube.com"); |
| 26 canoncial_suffixes_.push_back(".googlevideo.com"); | 28 canoncial_suffixes_.push_back(".googlevideo.com"); |
| 27 } | 29 } |
| 28 | 30 |
| 29 HttpServerPropertiesImpl::~HttpServerPropertiesImpl() { | 31 HttpServerPropertiesImpl::~HttpServerPropertiesImpl() { |
| 30 } | 32 } |
| 31 | 33 |
| 32 void HttpServerPropertiesImpl::InitializeSpdyServers( | 34 void HttpServerPropertiesImpl::InitializeSpdyServers( |
| 33 std::vector<std::string>* spdy_servers, | 35 std::vector<std::string>* spdy_servers, |
| 34 bool support_spdy) { | 36 bool support_spdy) { |
| 35 DCHECK(CalledOnValidThread()); | 37 DCHECK(CalledOnValidThread()); |
| 36 spdy_servers_table_.clear(); | 38 spdy_servers_table_.clear(); |
| 37 if (!spdy_servers) | 39 if (!spdy_servers) |
| 38 return; | 40 return; |
| 39 for (std::vector<std::string>::iterator it = spdy_servers->begin(); | 41 for (std::vector<std::string>::iterator it = spdy_servers->begin(); |
| 40 it != spdy_servers->end(); ++it) { | 42 it != spdy_servers->end(); ++it) { |
| 41 spdy_servers_table_[*it] = support_spdy; | 43 spdy_servers_table_[*it] = support_spdy; |
| 42 } | 44 } |
| 43 } | 45 } |
| 44 | 46 |
| 45 void HttpServerPropertiesImpl::InitializeAlternateProtocolServers( | 47 void HttpServerPropertiesImpl::InitializeAlternateProtocolServers( |
| 46 AlternateProtocolMap* alternate_protocol_map) { | 48 AlternateProtocolMap* alternate_protocol_map) { |
| 47 // First swap, and then add back all the ALTERNATE_PROTOCOL_BROKEN ones since | 49 // Keep all the ALTERNATE_PROTOCOL_BROKEN ones since those don't |
| 48 // those don't get persisted. | 50 // get persisted. |
| 49 alternate_protocol_map_.swap(*alternate_protocol_map); | 51 for (CachedAlternateProtocolMap::iterator it = |
| 52 alternate_protocol_map_->begin(); | |
| 53 it != alternate_protocol_map_->end();) { | |
| 54 if (it->second.protocol != ALTERNATE_PROTOCOL_BROKEN) { | |
| 55 CachedAlternateProtocolMap::iterator old_it = it; | |
| 56 ++it; | |
| 57 alternate_protocol_map_->Erase(old_it); | |
| 58 } else { | |
| 59 ++it; | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 // Add the entries from persisted data. | |
| 50 for (AlternateProtocolMap::const_iterator it = | 64 for (AlternateProtocolMap::const_iterator it = |
| 51 alternate_protocol_map->begin(); | 65 alternate_protocol_map->begin(); |
| 52 it != alternate_protocol_map->end(); ++it) { | 66 it != alternate_protocol_map->end(); ++it) { |
| 53 if (it->second.protocol == ALTERNATE_PROTOCOL_BROKEN) | 67 alternate_protocol_map_->Put(it->first, it->second); |
| 54 alternate_protocol_map_[it->first] = it->second; | |
| 55 } | 68 } |
| 69 | |
| 56 // Attempt to find canonical servers. | 70 // Attempt to find canonical servers. |
| 57 int canonical_ports[] = { 80, 443 }; | 71 int canonical_ports[] = { 80, 443 }; |
| 58 for (size_t i = 0; i < canoncial_suffixes_.size(); ++i) { | 72 for (size_t i = 0; i < canoncial_suffixes_.size(); ++i) { |
| 59 std::string canonical_suffix = canoncial_suffixes_[i]; | 73 std::string canonical_suffix = canoncial_suffixes_[i]; |
| 60 for (size_t j = 0; j < arraysize(canonical_ports); ++j) { | 74 for (size_t j = 0; j < arraysize(canonical_ports); ++j) { |
| 61 HostPortPair canonical_host(canonical_suffix, canonical_ports[j]); | 75 HostPortPair canonical_host(canonical_suffix, canonical_ports[j]); |
| 62 // If we already have a valid canonical server, we're done. | 76 // If we already have a valid canonical server, we're done. |
| 63 if (ContainsKey(canonical_host_to_origin_map_, canonical_host) && | 77 if (ContainsKey(canonical_host_to_origin_map_, canonical_host) && |
| 64 ContainsKey(alternate_protocol_map_, | 78 (alternate_protocol_map_->Get( |
| 65 canonical_host_to_origin_map_[canonical_host])) { | 79 canonical_host_to_origin_map_[canonical_host]) != |
| 80 alternate_protocol_map_->end())) { | |
| 66 continue; | 81 continue; |
| 67 } | 82 } |
| 68 // Now attempt to find a server which matches this origin and set it as | 83 // Now attempt to find a server which matches this origin and set it as |
| 69 // canonical . | 84 // canonical . |
| 70 for (AlternateProtocolMap::const_iterator it = | 85 for (CachedAlternateProtocolMap::const_iterator it = |
| 71 alternate_protocol_map_.begin(); | 86 alternate_protocol_map_->begin(); |
| 72 it != alternate_protocol_map_.end(); ++it) { | 87 it != alternate_protocol_map_->end(); ++it) { |
| 73 if (EndsWith(it->first.host(), canoncial_suffixes_[i], false)) { | 88 if (EndsWith(it->first.host(), canoncial_suffixes_[i], false)) { |
| 74 canonical_host_to_origin_map_[canonical_host] = it->first; | 89 canonical_host_to_origin_map_[canonical_host] = it->first; |
| 75 break; | 90 break; |
| 76 } | 91 } |
| 77 } | 92 } |
| 78 } | 93 } |
| 79 } | 94 } |
| 80 } | 95 } |
| 81 | 96 |
| 82 void HttpServerPropertiesImpl::InitializeSpdySettingsServers( | 97 void HttpServerPropertiesImpl::InitializeSpdySettingsServers( |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 g_forced_alternate_protocol = NULL; | 155 g_forced_alternate_protocol = NULL; |
| 141 } | 156 } |
| 142 | 157 |
| 143 base::WeakPtr<HttpServerProperties> HttpServerPropertiesImpl::GetWeakPtr() { | 158 base::WeakPtr<HttpServerProperties> HttpServerPropertiesImpl::GetWeakPtr() { |
| 144 return weak_ptr_factory_.GetWeakPtr(); | 159 return weak_ptr_factory_.GetWeakPtr(); |
| 145 } | 160 } |
| 146 | 161 |
| 147 void HttpServerPropertiesImpl::Clear() { | 162 void HttpServerPropertiesImpl::Clear() { |
| 148 DCHECK(CalledOnValidThread()); | 163 DCHECK(CalledOnValidThread()); |
| 149 spdy_servers_table_.clear(); | 164 spdy_servers_table_.clear(); |
| 150 alternate_protocol_map_.clear(); | 165 alternate_protocol_map_->Clear(); |
| 151 spdy_settings_map_.clear(); | 166 spdy_settings_map_.clear(); |
| 152 pipeline_capability_map_->Clear(); | 167 pipeline_capability_map_->Clear(); |
| 153 } | 168 } |
| 154 | 169 |
| 155 bool HttpServerPropertiesImpl::SupportsSpdy( | 170 bool HttpServerPropertiesImpl::SupportsSpdy( |
| 156 const net::HostPortPair& host_port_pair) const { | 171 const net::HostPortPair& host_port_pair) const { |
| 157 DCHECK(CalledOnValidThread()); | 172 DCHECK(CalledOnValidThread()); |
| 158 if (host_port_pair.host().empty()) | 173 if (host_port_pair.host().empty()) |
| 159 return false; | 174 return false; |
| 160 std::string spdy_server = GetFlattenedSpdyServer(host_port_pair); | 175 std::string spdy_server = GetFlattenedSpdyServer(host_port_pair); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 179 if ((spdy_host_port != spdy_servers_table_.end()) && | 194 if ((spdy_host_port != spdy_servers_table_.end()) && |
| 180 (spdy_host_port->second == support_spdy)) { | 195 (spdy_host_port->second == support_spdy)) { |
| 181 return; | 196 return; |
| 182 } | 197 } |
| 183 // Cache the data. | 198 // Cache the data. |
| 184 spdy_servers_table_[spdy_server] = support_spdy; | 199 spdy_servers_table_[spdy_server] = support_spdy; |
| 185 } | 200 } |
| 186 | 201 |
| 187 bool HttpServerPropertiesImpl::HasAlternateProtocol( | 202 bool HttpServerPropertiesImpl::HasAlternateProtocol( |
| 188 const HostPortPair& server) const { | 203 const HostPortPair& server) const { |
| 189 if (ContainsKey(alternate_protocol_map_, server) || | 204 if (alternate_protocol_map_->Get(server) != alternate_protocol_map_->end() || |
| 190 g_forced_alternate_protocol) | 205 g_forced_alternate_protocol) |
| 191 return true; | 206 return true; |
| 192 | 207 |
| 193 return GetCanonicalHost(server) != canonical_host_to_origin_map_.end(); | 208 return GetCanonicalHost(server) != canonical_host_to_origin_map_.end(); |
| 194 } | 209 } |
| 195 | 210 |
| 196 PortAlternateProtocolPair | 211 PortAlternateProtocolPair |
| 197 HttpServerPropertiesImpl::GetAlternateProtocol( | 212 HttpServerPropertiesImpl::GetAlternateProtocol( |
| 198 const HostPortPair& server) const { | 213 const HostPortPair& server) const { |
| 199 DCHECK(HasAlternateProtocol(server)); | 214 DCHECK(HasAlternateProtocol(server)); |
| 200 | 215 |
| 201 // First check the map. | 216 // First check the map. |
| 202 AlternateProtocolMap::const_iterator it = | 217 CachedAlternateProtocolMap::const_iterator it = |
| 203 alternate_protocol_map_.find(server); | 218 alternate_protocol_map_->Get(server); |
| 204 if (it != alternate_protocol_map_.end()) | 219 if (it != alternate_protocol_map_->end()) |
| 205 return it->second; | 220 return it->second; |
| 206 | 221 |
| 207 // Next check the canonical host. | 222 // Next check the canonical host. |
| 208 CanonicalHostMap::const_iterator canonical_host = GetCanonicalHost(server); | 223 CanonicalHostMap::const_iterator canonical_host = GetCanonicalHost(server); |
| 209 if (canonical_host != canonical_host_to_origin_map_.end()) | 224 if (canonical_host != canonical_host_to_origin_map_.end()) |
| 210 return alternate_protocol_map_.find(canonical_host->second)->second; | 225 return alternate_protocol_map_->Get(canonical_host->second)->second; |
| 211 | 226 |
| 212 // We must be forcing an alternate. | 227 // We must be forcing an alternate. |
| 213 DCHECK(g_forced_alternate_protocol); | 228 DCHECK(g_forced_alternate_protocol); |
| 214 return *g_forced_alternate_protocol; | 229 return *g_forced_alternate_protocol; |
| 215 } | 230 } |
| 216 | 231 |
| 217 void HttpServerPropertiesImpl::SetAlternateProtocol( | 232 void HttpServerPropertiesImpl::SetAlternateProtocol( |
| 218 const HostPortPair& server, | 233 const HostPortPair& server, |
| 219 uint16 alternate_port, | 234 uint16 alternate_port, |
| 220 AlternateProtocol alternate_protocol) { | 235 AlternateProtocol alternate_protocol) { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 240 LOG(WARNING) << "Changing the alternate protocol for: " | 255 LOG(WARNING) << "Changing the alternate protocol for: " |
| 241 << server.ToString() | 256 << server.ToString() |
| 242 << " from [Port: " << existing_alternate.port | 257 << " from [Port: " << existing_alternate.port |
| 243 << ", Protocol: " << existing_alternate.protocol | 258 << ", Protocol: " << existing_alternate.protocol |
| 244 << "] to [Port: " << alternate_port | 259 << "] to [Port: " << alternate_port |
| 245 << ", Protocol: " << alternate_protocol | 260 << ", Protocol: " << alternate_protocol |
| 246 << "]."; | 261 << "]."; |
| 247 } | 262 } |
| 248 } | 263 } |
| 249 | 264 |
| 250 alternate_protocol_map_[server] = alternate; | 265 alternate_protocol_map_->Put(server, alternate); |
| 251 | 266 |
| 252 // If this host ends with a canonical suffix, then set it as the | 267 // If this host ends with a canonical suffix, then set it as the |
| 253 // canonical host. | 268 // canonical host. |
| 254 for (size_t i = 0; i < canoncial_suffixes_.size(); ++i) { | 269 for (size_t i = 0; i < canoncial_suffixes_.size(); ++i) { |
| 255 std::string canonical_suffix = canoncial_suffixes_[i]; | 270 std::string canonical_suffix = canoncial_suffixes_[i]; |
| 256 if (EndsWith(server.host(), canoncial_suffixes_[i], false)) { | 271 if (EndsWith(server.host(), canoncial_suffixes_[i], false)) { |
| 257 HostPortPair canonical_host(canonical_suffix, server.port()); | 272 HostPortPair canonical_host(canonical_suffix, server.port()); |
| 258 canonical_host_to_origin_map_[canonical_host] = server; | 273 canonical_host_to_origin_map_[canonical_host] = server; |
| 259 break; | 274 break; |
| 260 } | 275 } |
| 261 } | 276 } |
| 262 } | 277 } |
| 263 | 278 |
| 264 void HttpServerPropertiesImpl::SetBrokenAlternateProtocol( | 279 void HttpServerPropertiesImpl::SetBrokenAlternateProtocol( |
| 265 const HostPortPair& server) { | 280 const HostPortPair& server) { |
| 266 alternate_protocol_map_[server].protocol = ALTERNATE_PROTOCOL_BROKEN; | 281 CachedAlternateProtocolMap::iterator it = |
| 282 alternate_protocol_map_->Get(server); | |
| 283 if (it != alternate_protocol_map_->end()) { | |
| 284 it->second.protocol = ALTERNATE_PROTOCOL_BROKEN; | |
| 285 return; | |
| 286 } | |
| 287 PortAlternateProtocolPair alternate; | |
| 288 alternate.protocol = ALTERNATE_PROTOCOL_BROKEN; | |
| 289 alternate_protocol_map_->Put(server, alternate); | |
| 267 } | 290 } |
| 268 | 291 |
| 269 const AlternateProtocolMap& | 292 AlternateProtocolMap |
| 270 HttpServerPropertiesImpl::alternate_protocol_map() const { | 293 HttpServerPropertiesImpl::GetAlternateProtocolMap() const { |
| 271 return alternate_protocol_map_; | 294 AlternateProtocolMap result; |
| 295 CachedAlternateProtocolMap::const_iterator it; | |
| 296 for (it = alternate_protocol_map_->begin(); | |
| 297 it != alternate_protocol_map_->end(); ++it) { | |
| 298 result[it->first] = it->second; | |
| 299 } | |
| 300 return result; | |
| 272 } | 301 } |
| 273 | 302 |
| 274 const SettingsMap& HttpServerPropertiesImpl::GetSpdySettings( | 303 const SettingsMap& HttpServerPropertiesImpl::GetSpdySettings( |
| 275 const HostPortPair& host_port_pair) const { | 304 const HostPortPair& host_port_pair) const { |
| 276 SpdySettingsMap::const_iterator it = spdy_settings_map_.find(host_port_pair); | 305 SpdySettingsMap::const_iterator it = spdy_settings_map_.find(host_port_pair); |
| 277 if (it == spdy_settings_map_.end()) { | 306 if (it == spdy_settings_map_.end()) { |
| 278 CR_DEFINE_STATIC_LOCAL(SettingsMap, kEmptySettingsMap, ()); | 307 CR_DEFINE_STATIC_LOCAL(SettingsMap, kEmptySettingsMap, ()); |
| 279 return kEmptySettingsMap; | 308 return kEmptySettingsMap; |
| 280 } | 309 } |
| 281 return it->second; | 310 return it->second; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 if (EndsWith(server.host(), canoncial_suffixes_[i], false)) { | 399 if (EndsWith(server.host(), canoncial_suffixes_[i], false)) { |
| 371 HostPortPair canonical_host(canonical_suffix, server.port()); | 400 HostPortPair canonical_host(canonical_suffix, server.port()); |
| 372 return canonical_host_to_origin_map_.find(canonical_host); | 401 return canonical_host_to_origin_map_.find(canonical_host); |
| 373 } | 402 } |
| 374 } | 403 } |
| 375 | 404 |
| 376 return canonical_host_to_origin_map_.end(); | 405 return canonical_host_to_origin_map_.end(); |
| 377 } | 406 } |
| 378 | 407 |
| 379 } // namespace net | 408 } // namespace net |
| OLD | NEW |