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), |
| 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(); |
50 for (AlternateProtocolMap::const_iterator it = | 51 it != alternate_protocol_map_.end();) { |
51 alternate_protocol_map->begin(); | 52 AlternateProtocolMap::iterator old_it = it; |
52 it != alternate_protocol_map->end(); ++it) { | 53 ++it; |
53 if (it->second.protocol == ALTERNATE_PROTOCOL_BROKEN) | 54 if (old_it->second.protocol != ALTERNATE_PROTOCOL_BROKEN) { |
54 alternate_protocol_map_[it->first] = it->second; | 55 alternate_protocol_map_.Erase(old_it); |
| 56 } |
55 } | 57 } |
| 58 |
| 59 // Add the entries from persisted data. |
| 60 for (AlternateProtocolMap::reverse_iterator it = |
| 61 alternate_protocol_map->rbegin(); |
| 62 it != alternate_protocol_map->rend(); ++it) { |
| 63 alternate_protocol_map_.Put(it->first, it->second); |
| 64 } |
| 65 |
56 // Attempt to find canonical servers. | 66 // Attempt to find canonical servers. |
57 int canonical_ports[] = { 80, 443 }; | 67 int canonical_ports[] = { 80, 443 }; |
58 for (size_t i = 0; i < canoncial_suffixes_.size(); ++i) { | 68 for (size_t i = 0; i < canoncial_suffixes_.size(); ++i) { |
59 std::string canonical_suffix = canoncial_suffixes_[i]; | 69 std::string canonical_suffix = canoncial_suffixes_[i]; |
60 for (size_t j = 0; j < arraysize(canonical_ports); ++j) { | 70 for (size_t j = 0; j < arraysize(canonical_ports); ++j) { |
61 HostPortPair canonical_host(canonical_suffix, canonical_ports[j]); | 71 HostPortPair canonical_host(canonical_suffix, canonical_ports[j]); |
62 // If we already have a valid canonical server, we're done. | 72 // If we already have a valid canonical server, we're done. |
63 if (ContainsKey(canonical_host_to_origin_map_, canonical_host) && | 73 if (ContainsKey(canonical_host_to_origin_map_, canonical_host) && |
64 ContainsKey(alternate_protocol_map_, | 74 (alternate_protocol_map_.Peek(canonical_host_to_origin_map_[ |
65 canonical_host_to_origin_map_[canonical_host])) { | 75 canonical_host]) != alternate_protocol_map_.end())) { |
66 continue; | 76 continue; |
67 } | 77 } |
68 // Now attempt to find a server which matches this origin and set it as | 78 // Now attempt to find a server which matches this origin and set it as |
69 // canonical . | 79 // canonical . |
70 for (AlternateProtocolMap::const_iterator it = | 80 for (AlternateProtocolMap::const_iterator it = |
71 alternate_protocol_map_.begin(); | 81 alternate_protocol_map_.begin(); |
72 it != alternate_protocol_map_.end(); ++it) { | 82 it != alternate_protocol_map_.end(); ++it) { |
73 if (EndsWith(it->first.host(), canoncial_suffixes_[i], false)) { | 83 if (EndsWith(it->first.host(), canoncial_suffixes_[i], false)) { |
74 canonical_host_to_origin_map_[canonical_host] = it->first; | 84 canonical_host_to_origin_map_[canonical_host] = it->first; |
75 break; | 85 break; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 g_forced_alternate_protocol = NULL; | 150 g_forced_alternate_protocol = NULL; |
141 } | 151 } |
142 | 152 |
143 base::WeakPtr<HttpServerProperties> HttpServerPropertiesImpl::GetWeakPtr() { | 153 base::WeakPtr<HttpServerProperties> HttpServerPropertiesImpl::GetWeakPtr() { |
144 return weak_ptr_factory_.GetWeakPtr(); | 154 return weak_ptr_factory_.GetWeakPtr(); |
145 } | 155 } |
146 | 156 |
147 void HttpServerPropertiesImpl::Clear() { | 157 void HttpServerPropertiesImpl::Clear() { |
148 DCHECK(CalledOnValidThread()); | 158 DCHECK(CalledOnValidThread()); |
149 spdy_servers_table_.clear(); | 159 spdy_servers_table_.clear(); |
150 alternate_protocol_map_.clear(); | 160 alternate_protocol_map_.Clear(); |
151 spdy_settings_map_.clear(); | 161 spdy_settings_map_.clear(); |
152 pipeline_capability_map_->Clear(); | 162 pipeline_capability_map_->Clear(); |
153 } | 163 } |
154 | 164 |
155 bool HttpServerPropertiesImpl::SupportsSpdy( | 165 bool HttpServerPropertiesImpl::SupportsSpdy( |
156 const net::HostPortPair& host_port_pair) const { | 166 const net::HostPortPair& host_port_pair) const { |
157 DCHECK(CalledOnValidThread()); | 167 DCHECK(CalledOnValidThread()); |
158 if (host_port_pair.host().empty()) | 168 if (host_port_pair.host().empty()) |
159 return false; | 169 return false; |
160 std::string spdy_server = GetFlattenedSpdyServer(host_port_pair); | 170 std::string spdy_server = GetFlattenedSpdyServer(host_port_pair); |
(...skipping 17 matching lines...) Expand all Loading... |
178 spdy_servers_table_.find(spdy_server); | 188 spdy_servers_table_.find(spdy_server); |
179 if ((spdy_host_port != spdy_servers_table_.end()) && | 189 if ((spdy_host_port != spdy_servers_table_.end()) && |
180 (spdy_host_port->second == support_spdy)) { | 190 (spdy_host_port->second == support_spdy)) { |
181 return; | 191 return; |
182 } | 192 } |
183 // Cache the data. | 193 // Cache the data. |
184 spdy_servers_table_[spdy_server] = support_spdy; | 194 spdy_servers_table_[spdy_server] = support_spdy; |
185 } | 195 } |
186 | 196 |
187 bool HttpServerPropertiesImpl::HasAlternateProtocol( | 197 bool HttpServerPropertiesImpl::HasAlternateProtocol( |
188 const HostPortPair& server) const { | 198 const HostPortPair& server) { |
189 if (ContainsKey(alternate_protocol_map_, server) || | 199 if (alternate_protocol_map_.Get(server) != alternate_protocol_map_.end() || |
190 g_forced_alternate_protocol) | 200 g_forced_alternate_protocol) |
191 return true; | 201 return true; |
192 | 202 |
193 return GetCanonicalHost(server) != canonical_host_to_origin_map_.end(); | 203 return GetCanonicalHost(server) != canonical_host_to_origin_map_.end(); |
194 } | 204 } |
195 | 205 |
196 PortAlternateProtocolPair | 206 PortAlternateProtocolPair |
197 HttpServerPropertiesImpl::GetAlternateProtocol( | 207 HttpServerPropertiesImpl::GetAlternateProtocol( |
198 const HostPortPair& server) const { | 208 const HostPortPair& server) { |
199 DCHECK(HasAlternateProtocol(server)); | 209 DCHECK(HasAlternateProtocol(server)); |
200 | 210 |
201 // First check the map. | 211 // First check the map. |
202 AlternateProtocolMap::const_iterator it = | 212 AlternateProtocolMap::iterator it = alternate_protocol_map_.Get(server); |
203 alternate_protocol_map_.find(server); | |
204 if (it != alternate_protocol_map_.end()) | 213 if (it != alternate_protocol_map_.end()) |
205 return it->second; | 214 return it->second; |
206 | 215 |
207 // Next check the canonical host. | 216 // Next check the canonical host. |
208 CanonicalHostMap::const_iterator canonical_host = GetCanonicalHost(server); | 217 CanonicalHostMap::const_iterator canonical_host = GetCanonicalHost(server); |
209 if (canonical_host != canonical_host_to_origin_map_.end()) | 218 if (canonical_host != canonical_host_to_origin_map_.end()) |
210 return alternate_protocol_map_.find(canonical_host->second)->second; | 219 return alternate_protocol_map_.Get(canonical_host->second)->second; |
211 | 220 |
212 // We must be forcing an alternate. | 221 // We must be forcing an alternate. |
213 DCHECK(g_forced_alternate_protocol); | 222 DCHECK(g_forced_alternate_protocol); |
214 return *g_forced_alternate_protocol; | 223 return *g_forced_alternate_protocol; |
215 } | 224 } |
216 | 225 |
217 void HttpServerPropertiesImpl::SetAlternateProtocol( | 226 void HttpServerPropertiesImpl::SetAlternateProtocol( |
218 const HostPortPair& server, | 227 const HostPortPair& server, |
219 uint16 alternate_port, | 228 uint16 alternate_port, |
220 AlternateProtocol alternate_protocol) { | 229 AlternateProtocol alternate_protocol) { |
(...skipping 19 matching lines...) Expand all Loading... |
240 LOG(WARNING) << "Changing the alternate protocol for: " | 249 LOG(WARNING) << "Changing the alternate protocol for: " |
241 << server.ToString() | 250 << server.ToString() |
242 << " from [Port: " << existing_alternate.port | 251 << " from [Port: " << existing_alternate.port |
243 << ", Protocol: " << existing_alternate.protocol | 252 << ", Protocol: " << existing_alternate.protocol |
244 << "] to [Port: " << alternate_port | 253 << "] to [Port: " << alternate_port |
245 << ", Protocol: " << alternate_protocol | 254 << ", Protocol: " << alternate_protocol |
246 << "]."; | 255 << "]."; |
247 } | 256 } |
248 } | 257 } |
249 | 258 |
250 alternate_protocol_map_[server] = alternate; | 259 alternate_protocol_map_.Put(server, alternate); |
251 | 260 |
252 // If this host ends with a canonical suffix, then set it as the | 261 // If this host ends with a canonical suffix, then set it as the |
253 // canonical host. | 262 // canonical host. |
254 for (size_t i = 0; i < canoncial_suffixes_.size(); ++i) { | 263 for (size_t i = 0; i < canoncial_suffixes_.size(); ++i) { |
255 std::string canonical_suffix = canoncial_suffixes_[i]; | 264 std::string canonical_suffix = canoncial_suffixes_[i]; |
256 if (EndsWith(server.host(), canoncial_suffixes_[i], false)) { | 265 if (EndsWith(server.host(), canoncial_suffixes_[i], false)) { |
257 HostPortPair canonical_host(canonical_suffix, server.port()); | 266 HostPortPair canonical_host(canonical_suffix, server.port()); |
258 canonical_host_to_origin_map_[canonical_host] = server; | 267 canonical_host_to_origin_map_[canonical_host] = server; |
259 break; | 268 break; |
260 } | 269 } |
261 } | 270 } |
262 } | 271 } |
263 | 272 |
264 void HttpServerPropertiesImpl::SetBrokenAlternateProtocol( | 273 void HttpServerPropertiesImpl::SetBrokenAlternateProtocol( |
265 const HostPortPair& server) { | 274 const HostPortPair& server) { |
266 alternate_protocol_map_[server].protocol = ALTERNATE_PROTOCOL_BROKEN; | 275 AlternateProtocolMap::iterator it = alternate_protocol_map_.Get(server); |
| 276 if (it != alternate_protocol_map_.end()) { |
| 277 it->second.protocol = ALTERNATE_PROTOCOL_BROKEN; |
| 278 return; |
| 279 } |
| 280 PortAlternateProtocolPair alternate; |
| 281 alternate.protocol = ALTERNATE_PROTOCOL_BROKEN; |
| 282 alternate_protocol_map_.Put(server, alternate); |
267 } | 283 } |
268 | 284 |
269 void HttpServerPropertiesImpl::ClearAlternateProtocol( | 285 void HttpServerPropertiesImpl::ClearAlternateProtocol( |
270 const HostPortPair& server) { | 286 const HostPortPair& server) { |
271 alternate_protocol_map_.erase(server); | 287 alternate_protocol_map_.erase(server); |
272 } | 288 } |
273 | 289 |
274 const AlternateProtocolMap& | 290 const AlternateProtocolMap& |
275 HttpServerPropertiesImpl::alternate_protocol_map() const { | 291 HttpServerPropertiesImpl::alternate_protocol_map() const { |
276 return alternate_protocol_map_; | 292 return alternate_protocol_map_; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 if (EndsWith(server.host(), canoncial_suffixes_[i], false)) { | 391 if (EndsWith(server.host(), canoncial_suffixes_[i], false)) { |
376 HostPortPair canonical_host(canonical_suffix, server.port()); | 392 HostPortPair canonical_host(canonical_suffix, server.port()); |
377 return canonical_host_to_origin_map_.find(canonical_host); | 393 return canonical_host_to_origin_map_.find(canonical_host); |
378 } | 394 } |
379 } | 395 } |
380 | 396 |
381 return canonical_host_to_origin_map_.end(); | 397 return canonical_host_to_origin_map_.end(); |
382 } | 398 } |
383 | 399 |
384 } // namespace net | 400 } // namespace net |
OLD | NEW |