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_(AlternateProtocolMap::NO_AUTO_EVICT), |
Ryan Hamilton
2014/03/05 17:51:22
With NO_AUTO_EVICT, this means the in-memory map w
ramant (doing other things)
2014/03/05 22:43:22
I went back and forth on the above. The current im
Ryan Hamilton
2014/03/05 23:10:20
SGTM. We can revisit this at any point.
| |
23 pipeline_capability_map_( | |
23 new CachedPipelineCapabilityMap(kDefaultNumHostsToRemember)), | 24 new CachedPipelineCapabilityMap(kDefaultNumHostsToRemember)), |
24 weak_ptr_factory_(this) { | 25 weak_ptr_factory_(this) { |
25 canoncial_suffixes_.push_back(".c.youtube.com"); | 26 canoncial_suffixes_.push_back(".c.youtube.com"); |
26 canoncial_suffixes_.push_back(".googlevideo.com"); | 27 canoncial_suffixes_.push_back(".googlevideo.com"); |
27 } | 28 } |
28 | 29 |
29 HttpServerPropertiesImpl::~HttpServerPropertiesImpl() { | 30 HttpServerPropertiesImpl::~HttpServerPropertiesImpl() { |
30 } | 31 } |
31 | 32 |
32 void HttpServerPropertiesImpl::InitializeSpdyServers( | 33 void HttpServerPropertiesImpl::InitializeSpdyServers( |
33 std::vector<std::string>* spdy_servers, | 34 std::vector<std::string>* spdy_servers, |
34 bool support_spdy) { | 35 bool support_spdy) { |
35 DCHECK(CalledOnValidThread()); | 36 DCHECK(CalledOnValidThread()); |
36 spdy_servers_table_.clear(); | 37 spdy_servers_table_.clear(); |
37 if (!spdy_servers) | 38 if (!spdy_servers) |
38 return; | 39 return; |
39 for (std::vector<std::string>::iterator it = spdy_servers->begin(); | 40 for (std::vector<std::string>::iterator it = spdy_servers->begin(); |
40 it != spdy_servers->end(); ++it) { | 41 it != spdy_servers->end(); ++it) { |
41 spdy_servers_table_[*it] = support_spdy; | 42 spdy_servers_table_[*it] = support_spdy; |
42 } | 43 } |
43 } | 44 } |
44 | 45 |
45 void HttpServerPropertiesImpl::InitializeAlternateProtocolServers( | 46 void HttpServerPropertiesImpl::InitializeAlternateProtocolServers( |
46 AlternateProtocolMap* alternate_protocol_map) { | 47 AlternateProtocolMap* alternate_protocol_map) { |
47 // First swap, and then add back all the ALTERNATE_PROTOCOL_BROKEN ones since | 48 // Keep all the ALTERNATE_PROTOCOL_BROKEN ones since those don't |
48 // those don't get persisted. | 49 // get persisted. |
49 alternate_protocol_map_.swap(*alternate_protocol_map); | 50 for (AlternateProtocolMap::iterator it = alternate_protocol_map_.begin(); |
51 it != alternate_protocol_map_.end();) { | |
52 if (it->second.protocol != ALTERNATE_PROTOCOL_BROKEN) { | |
53 AlternateProtocolMap::iterator old_it = it; | |
54 ++it; | |
55 alternate_protocol_map_.Erase(old_it); | |
56 } else { | |
57 ++it; | |
58 } | |
Ryan Hamilton
2014/03/05 17:51:22
alternatively:
AlternateProtocolMap::iterator old
ramant (doing other things)
2014/03/05 22:43:22
Done.
| |
59 } | |
Ryan Hamilton
2014/03/05 17:51:22
I take it there is no swap() method in MRUCache?
ramant (doing other things)
2014/03/05 22:43:22
Correct.
| |
60 | |
61 // Add the entries from persisted data. | |
50 for (AlternateProtocolMap::const_iterator it = | 62 for (AlternateProtocolMap::const_iterator it = |
51 alternate_protocol_map->begin(); | 63 alternate_protocol_map->begin(); |
52 it != alternate_protocol_map->end(); ++it) { | 64 it != alternate_protocol_map->end(); ++it) { |
53 if (it->second.protocol == ALTERNATE_PROTOCOL_BROKEN) | 65 alternate_protocol_map_.Put(it->first, it->second); |
54 alternate_protocol_map_[it->first] = it->second; | |
55 } | 66 } |
67 | |
56 // Attempt to find canonical servers. | 68 // Attempt to find canonical servers. |
57 int canonical_ports[] = { 80, 443 }; | 69 int canonical_ports[] = { 80, 443 }; |
58 for (size_t i = 0; i < canoncial_suffixes_.size(); ++i) { | 70 for (size_t i = 0; i < canoncial_suffixes_.size(); ++i) { |
59 std::string canonical_suffix = canoncial_suffixes_[i]; | 71 std::string canonical_suffix = canoncial_suffixes_[i]; |
60 for (size_t j = 0; j < arraysize(canonical_ports); ++j) { | 72 for (size_t j = 0; j < arraysize(canonical_ports); ++j) { |
61 HostPortPair canonical_host(canonical_suffix, canonical_ports[j]); | 73 HostPortPair canonical_host(canonical_suffix, canonical_ports[j]); |
62 // If we already have a valid canonical server, we're done. | 74 // If we already have a valid canonical server, we're done. |
63 if (ContainsKey(canonical_host_to_origin_map_, canonical_host) && | 75 if (ContainsKey(canonical_host_to_origin_map_, canonical_host) && |
64 ContainsKey(alternate_protocol_map_, | 76 (alternate_protocol_map_.Get(canonical_host_to_origin_map_[ |
65 canonical_host_to_origin_map_[canonical_host])) { | 77 canonical_host]) != alternate_protocol_map_.end())) { |
66 continue; | 78 continue; |
67 } | 79 } |
68 // Now attempt to find a server which matches this origin and set it as | 80 // Now attempt to find a server which matches this origin and set it as |
69 // canonical . | 81 // canonical . |
70 for (AlternateProtocolMap::const_iterator it = | 82 for (AlternateProtocolMap::const_iterator it = |
71 alternate_protocol_map_.begin(); | 83 alternate_protocol_map_.begin(); |
72 it != alternate_protocol_map_.end(); ++it) { | 84 it != alternate_protocol_map_.end(); ++it) { |
73 if (EndsWith(it->first.host(), canoncial_suffixes_[i], false)) { | 85 if (EndsWith(it->first.host(), canoncial_suffixes_[i], false)) { |
74 canonical_host_to_origin_map_[canonical_host] = it->first; | 86 canonical_host_to_origin_map_[canonical_host] = it->first; |
75 break; | 87 break; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 g_forced_alternate_protocol = NULL; | 152 g_forced_alternate_protocol = NULL; |
141 } | 153 } |
142 | 154 |
143 base::WeakPtr<HttpServerProperties> HttpServerPropertiesImpl::GetWeakPtr() { | 155 base::WeakPtr<HttpServerProperties> HttpServerPropertiesImpl::GetWeakPtr() { |
144 return weak_ptr_factory_.GetWeakPtr(); | 156 return weak_ptr_factory_.GetWeakPtr(); |
145 } | 157 } |
146 | 158 |
147 void HttpServerPropertiesImpl::Clear() { | 159 void HttpServerPropertiesImpl::Clear() { |
148 DCHECK(CalledOnValidThread()); | 160 DCHECK(CalledOnValidThread()); |
149 spdy_servers_table_.clear(); | 161 spdy_servers_table_.clear(); |
150 alternate_protocol_map_.clear(); | 162 alternate_protocol_map_.Clear(); |
151 spdy_settings_map_.clear(); | 163 spdy_settings_map_.clear(); |
152 pipeline_capability_map_->Clear(); | 164 pipeline_capability_map_->Clear(); |
153 } | 165 } |
154 | 166 |
155 bool HttpServerPropertiesImpl::SupportsSpdy( | 167 bool HttpServerPropertiesImpl::SupportsSpdy( |
156 const net::HostPortPair& host_port_pair) const { | 168 const net::HostPortPair& host_port_pair) const { |
157 DCHECK(CalledOnValidThread()); | 169 DCHECK(CalledOnValidThread()); |
158 if (host_port_pair.host().empty()) | 170 if (host_port_pair.host().empty()) |
159 return false; | 171 return false; |
160 std::string spdy_server = GetFlattenedSpdyServer(host_port_pair); | 172 std::string spdy_server = GetFlattenedSpdyServer(host_port_pair); |
(...skipping 17 matching lines...) Expand all Loading... | |
178 spdy_servers_table_.find(spdy_server); | 190 spdy_servers_table_.find(spdy_server); |
179 if ((spdy_host_port != spdy_servers_table_.end()) && | 191 if ((spdy_host_port != spdy_servers_table_.end()) && |
180 (spdy_host_port->second == support_spdy)) { | 192 (spdy_host_port->second == support_spdy)) { |
181 return; | 193 return; |
182 } | 194 } |
183 // Cache the data. | 195 // Cache the data. |
184 spdy_servers_table_[spdy_server] = support_spdy; | 196 spdy_servers_table_[spdy_server] = support_spdy; |
185 } | 197 } |
186 | 198 |
187 bool HttpServerPropertiesImpl::HasAlternateProtocol( | 199 bool HttpServerPropertiesImpl::HasAlternateProtocol( |
188 const HostPortPair& server) const { | 200 const HostPortPair& server) { |
189 if (ContainsKey(alternate_protocol_map_, server) || | 201 if (alternate_protocol_map_.Get(server) != alternate_protocol_map_.end() || |
190 g_forced_alternate_protocol) | 202 g_forced_alternate_protocol) |
191 return true; | 203 return true; |
192 | 204 |
193 return GetCanonicalHost(server) != canonical_host_to_origin_map_.end(); | 205 return GetCanonicalHost(server) != canonical_host_to_origin_map_.end(); |
194 } | 206 } |
195 | 207 |
196 PortAlternateProtocolPair | 208 PortAlternateProtocolPair |
197 HttpServerPropertiesImpl::GetAlternateProtocol( | 209 HttpServerPropertiesImpl::GetAlternateProtocol( |
198 const HostPortPair& server) const { | 210 const HostPortPair& server) { |
199 DCHECK(HasAlternateProtocol(server)); | 211 DCHECK(HasAlternateProtocol(server)); |
200 | 212 |
201 // First check the map. | 213 // First check the map. |
202 AlternateProtocolMap::const_iterator it = | 214 AlternateProtocolMap::iterator it = alternate_protocol_map_.Get(server); |
203 alternate_protocol_map_.find(server); | |
204 if (it != alternate_protocol_map_.end()) | 215 if (it != alternate_protocol_map_.end()) |
205 return it->second; | 216 return it->second; |
206 | 217 |
207 // Next check the canonical host. | 218 // Next check the canonical host. |
208 CanonicalHostMap::const_iterator canonical_host = GetCanonicalHost(server); | 219 CanonicalHostMap::const_iterator canonical_host = GetCanonicalHost(server); |
209 if (canonical_host != canonical_host_to_origin_map_.end()) | 220 if (canonical_host != canonical_host_to_origin_map_.end()) |
210 return alternate_protocol_map_.find(canonical_host->second)->second; | 221 return alternate_protocol_map_.Get(canonical_host->second)->second; |
211 | 222 |
212 // We must be forcing an alternate. | 223 // We must be forcing an alternate. |
213 DCHECK(g_forced_alternate_protocol); | 224 DCHECK(g_forced_alternate_protocol); |
214 return *g_forced_alternate_protocol; | 225 return *g_forced_alternate_protocol; |
215 } | 226 } |
216 | 227 |
217 void HttpServerPropertiesImpl::SetAlternateProtocol( | 228 void HttpServerPropertiesImpl::SetAlternateProtocol( |
218 const HostPortPair& server, | 229 const HostPortPair& server, |
219 uint16 alternate_port, | 230 uint16 alternate_port, |
220 AlternateProtocol alternate_protocol) { | 231 AlternateProtocol alternate_protocol) { |
(...skipping 19 matching lines...) Expand all Loading... | |
240 LOG(WARNING) << "Changing the alternate protocol for: " | 251 LOG(WARNING) << "Changing the alternate protocol for: " |
241 << server.ToString() | 252 << server.ToString() |
242 << " from [Port: " << existing_alternate.port | 253 << " from [Port: " << existing_alternate.port |
243 << ", Protocol: " << existing_alternate.protocol | 254 << ", Protocol: " << existing_alternate.protocol |
244 << "] to [Port: " << alternate_port | 255 << "] to [Port: " << alternate_port |
245 << ", Protocol: " << alternate_protocol | 256 << ", Protocol: " << alternate_protocol |
246 << "]."; | 257 << "]."; |
247 } | 258 } |
248 } | 259 } |
249 | 260 |
250 alternate_protocol_map_[server] = alternate; | 261 alternate_protocol_map_.Put(server, alternate); |
251 | 262 |
252 // If this host ends with a canonical suffix, then set it as the | 263 // If this host ends with a canonical suffix, then set it as the |
253 // canonical host. | 264 // canonical host. |
254 for (size_t i = 0; i < canoncial_suffixes_.size(); ++i) { | 265 for (size_t i = 0; i < canoncial_suffixes_.size(); ++i) { |
255 std::string canonical_suffix = canoncial_suffixes_[i]; | 266 std::string canonical_suffix = canoncial_suffixes_[i]; |
256 if (EndsWith(server.host(), canoncial_suffixes_[i], false)) { | 267 if (EndsWith(server.host(), canoncial_suffixes_[i], false)) { |
257 HostPortPair canonical_host(canonical_suffix, server.port()); | 268 HostPortPair canonical_host(canonical_suffix, server.port()); |
258 canonical_host_to_origin_map_[canonical_host] = server; | 269 canonical_host_to_origin_map_[canonical_host] = server; |
259 break; | 270 break; |
260 } | 271 } |
261 } | 272 } |
262 } | 273 } |
263 | 274 |
264 void HttpServerPropertiesImpl::SetBrokenAlternateProtocol( | 275 void HttpServerPropertiesImpl::SetBrokenAlternateProtocol( |
265 const HostPortPair& server) { | 276 const HostPortPair& server) { |
266 alternate_protocol_map_[server].protocol = ALTERNATE_PROTOCOL_BROKEN; | 277 AlternateProtocolMap::iterator it = alternate_protocol_map_.Get(server); |
278 if (it != alternate_protocol_map_.end()) { | |
279 it->second.protocol = ALTERNATE_PROTOCOL_BROKEN; | |
280 return; | |
281 } | |
282 PortAlternateProtocolPair alternate; | |
283 alternate.protocol = ALTERNATE_PROTOCOL_BROKEN; | |
284 alternate_protocol_map_.Put(server, alternate); | |
267 } | 285 } |
268 | 286 |
269 const AlternateProtocolMap& | 287 const AlternateProtocolMap& |
270 HttpServerPropertiesImpl::alternate_protocol_map() const { | 288 HttpServerPropertiesImpl::alternate_protocol_map() const { |
271 return alternate_protocol_map_; | 289 return alternate_protocol_map_; |
272 } | 290 } |
273 | 291 |
274 const SettingsMap& HttpServerPropertiesImpl::GetSpdySettings( | 292 const SettingsMap& HttpServerPropertiesImpl::GetSpdySettings( |
275 const HostPortPair& host_port_pair) const { | 293 const HostPortPair& host_port_pair) const { |
276 SpdySettingsMap::const_iterator it = spdy_settings_map_.find(host_port_pair); | 294 SpdySettingsMap::const_iterator it = spdy_settings_map_.find(host_port_pair); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
370 if (EndsWith(server.host(), canoncial_suffixes_[i], false)) { | 388 if (EndsWith(server.host(), canoncial_suffixes_[i], false)) { |
371 HostPortPair canonical_host(canonical_suffix, server.port()); | 389 HostPortPair canonical_host(canonical_suffix, server.port()); |
372 return canonical_host_to_origin_map_.find(canonical_host); | 390 return canonical_host_to_origin_map_.find(canonical_host); |
373 } | 391 } |
374 } | 392 } |
375 | 393 |
376 return canonical_host_to_origin_map_.end(); | 394 return canonical_host_to_origin_map_.end(); |
377 } | 395 } |
378 | 396 |
379 } // namespace net | 397 } // namespace net |
OLD | NEW |