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
3 // found in the LICENSE file.
5 #include "net/http/http_server_properties_impl.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/stl_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "net/http/http_pipelined_host_capability.h"
15 // TODO(simonjam): Run experiments with different values of this to see what
16 // value is good at avoiding evictions without eating too much memory. Until
17 // then, this is just a bad guess.
18 static const int kDefaultNumHostsToRemember
= 200;
20 HttpServerPropertiesImpl::HttpServerPropertiesImpl()
21 : pipeline_capability_map_(
22 new CachedPipelineCapabilityMap(kDefaultNumHostsToRemember
)) {
25 HttpServerPropertiesImpl::~HttpServerPropertiesImpl() {
28 void HttpServerPropertiesImpl::InitializeSpdyServers(
29 std::vector
<std::string
>* spdy_servers
,
31 DCHECK(CalledOnValidThread());
32 spdy_servers_table_
.clear();
35 for (std::vector
<std::string
>::iterator it
= spdy_servers
->begin();
36 it
!= spdy_servers
->end(); ++it
) {
37 spdy_servers_table_
[*it
] = support_spdy
;
41 void HttpServerPropertiesImpl::InitializeAlternateProtocolServers(
42 AlternateProtocolMap
* alternate_protocol_map
) {
43 // First swap, and then add back all the ALTERNATE_PROTOCOL_BROKEN ones since
44 // those don't get persisted.
45 alternate_protocol_map_
.swap(*alternate_protocol_map
);
46 for (AlternateProtocolMap::const_iterator it
=
47 alternate_protocol_map
->begin();
48 it
!= alternate_protocol_map
->end(); ++it
) {
49 if (it
->second
.protocol
== ALTERNATE_PROTOCOL_BROKEN
)
50 alternate_protocol_map_
[it
->first
] = it
->second
;
54 void HttpServerPropertiesImpl::InitializeSpdySettingsServers(
55 SpdySettingsMap
* spdy_settings_map
) {
56 spdy_settings_map_
.swap(*spdy_settings_map
);
59 void HttpServerPropertiesImpl::InitializePipelineCapabilities(
60 const PipelineCapabilityMap
* pipeline_capability_map
) {
61 PipelineCapabilityMap::const_iterator it
;
62 pipeline_capability_map_
->Clear();
63 for (it
= pipeline_capability_map
->begin();
64 it
!= pipeline_capability_map
->end(); ++it
) {
65 pipeline_capability_map_
->Put(it
->first
, it
->second
);
69 void HttpServerPropertiesImpl::SetNumPipelinedHostsToRemember(int max_size
) {
70 DCHECK(pipeline_capability_map_
->empty());
71 pipeline_capability_map_
.reset(new CachedPipelineCapabilityMap(max_size
));
74 void HttpServerPropertiesImpl::GetSpdyServerList(
75 base::ListValue
* spdy_server_list
) const {
76 DCHECK(CalledOnValidThread());
77 DCHECK(spdy_server_list
);
78 spdy_server_list
->Clear();
79 // Get the list of servers (host/port) that support SPDY.
80 for (SpdyServerHostPortTable::const_iterator it
= spdy_servers_table_
.begin();
81 it
!= spdy_servers_table_
.end(); ++it
) {
82 const std::string spdy_server_host_port
= it
->first
;
84 spdy_server_list
->Append(new base::StringValue(spdy_server_host_port
));
89 std::string
HttpServerPropertiesImpl::GetFlattenedSpdyServer(
90 const net::HostPortPair
& host_port_pair
) {
91 std::string spdy_server
;
92 spdy_server
.append(host_port_pair
.host());
93 spdy_server
.append(":");
94 base::StringAppendF(&spdy_server
, "%d", host_port_pair
.port());
98 static const PortAlternateProtocolPair
* g_forced_alternate_protocol
= NULL
;
101 void HttpServerPropertiesImpl::ForceAlternateProtocol(
102 const PortAlternateProtocolPair
& pair
) {
103 // Note: we're going to leak this.
104 if (g_forced_alternate_protocol
)
105 delete g_forced_alternate_protocol
;
106 g_forced_alternate_protocol
= new PortAlternateProtocolPair(pair
);
110 void HttpServerPropertiesImpl::DisableForcedAlternateProtocol() {
111 delete g_forced_alternate_protocol
;
112 g_forced_alternate_protocol
= NULL
;
115 void HttpServerPropertiesImpl::Clear() {
116 DCHECK(CalledOnValidThread());
117 spdy_servers_table_
.clear();
118 alternate_protocol_map_
.clear();
119 spdy_settings_map_
.clear();
120 pipeline_capability_map_
->Clear();
123 bool HttpServerPropertiesImpl::SupportsSpdy(
124 const net::HostPortPair
& host_port_pair
) const {
125 DCHECK(CalledOnValidThread());
126 if (host_port_pair
.host().empty())
128 std::string spdy_server
= GetFlattenedSpdyServer(host_port_pair
);
130 SpdyServerHostPortTable::const_iterator spdy_host_port
=
131 spdy_servers_table_
.find(spdy_server
);
132 if (spdy_host_port
!= spdy_servers_table_
.end())
133 return spdy_host_port
->second
;
137 void HttpServerPropertiesImpl::SetSupportsSpdy(
138 const net::HostPortPair
& host_port_pair
,
140 DCHECK(CalledOnValidThread());
141 if (host_port_pair
.host().empty())
143 std::string spdy_server
= GetFlattenedSpdyServer(host_port_pair
);
145 SpdyServerHostPortTable::iterator spdy_host_port
=
146 spdy_servers_table_
.find(spdy_server
);
147 if ((spdy_host_port
!= spdy_servers_table_
.end()) &&
148 (spdy_host_port
->second
== support_spdy
)) {
152 spdy_servers_table_
[spdy_server
] = support_spdy
;
155 bool HttpServerPropertiesImpl::HasAlternateProtocol(
156 const HostPortPair
& server
) const {
157 return ContainsKey(alternate_protocol_map_
, server
) ||
158 g_forced_alternate_protocol
;
161 PortAlternateProtocolPair
162 HttpServerPropertiesImpl::GetAlternateProtocol(
163 const HostPortPair
& server
) const {
164 DCHECK(HasAlternateProtocol(server
));
166 // First check the map.
167 AlternateProtocolMap::const_iterator it
=
168 alternate_protocol_map_
.find(server
);
169 if (it
!= alternate_protocol_map_
.end())
172 // We must be forcing an alternate.
173 DCHECK(g_forced_alternate_protocol
);
174 return *g_forced_alternate_protocol
;
177 void HttpServerPropertiesImpl::SetAlternateProtocol(
178 const HostPortPair
& server
,
179 uint16 alternate_port
,
180 AlternateProtocol alternate_protocol
) {
181 if (alternate_protocol
== ALTERNATE_PROTOCOL_BROKEN
) {
182 LOG(DFATAL
) << "Call SetBrokenAlternateProtocol() instead.";
186 PortAlternateProtocolPair alternate
;
187 alternate
.port
= alternate_port
;
188 alternate
.protocol
= alternate_protocol
;
189 if (HasAlternateProtocol(server
)) {
190 const PortAlternateProtocolPair existing_alternate
=
191 GetAlternateProtocol(server
);
193 if (existing_alternate
.protocol
== ALTERNATE_PROTOCOL_BROKEN
) {
194 DVLOG(1) << "Ignore alternate protocol since it's known to be broken.";
198 if (alternate_protocol
!= ALTERNATE_PROTOCOL_BROKEN
&&
199 !existing_alternate
.Equals(alternate
)) {
200 LOG(WARNING
) << "Changing the alternate protocol for: "
202 << " from [Port: " << existing_alternate
.port
203 << ", Protocol: " << existing_alternate
.protocol
204 << "] to [Port: " << alternate_port
205 << ", Protocol: " << alternate_protocol
210 alternate_protocol_map_
[server
] = alternate
;
213 void HttpServerPropertiesImpl::SetBrokenAlternateProtocol(
214 const HostPortPair
& server
) {
215 alternate_protocol_map_
[server
].protocol
= ALTERNATE_PROTOCOL_BROKEN
;
218 const AlternateProtocolMap
&
219 HttpServerPropertiesImpl::alternate_protocol_map() const {
220 return alternate_protocol_map_
;
223 const SettingsMap
& HttpServerPropertiesImpl::GetSpdySettings(
224 const HostPortPair
& host_port_pair
) const {
225 SpdySettingsMap::const_iterator it
= spdy_settings_map_
.find(host_port_pair
);
226 if (it
== spdy_settings_map_
.end()) {
227 CR_DEFINE_STATIC_LOCAL(SettingsMap
, kEmptySettingsMap
, ());
228 return kEmptySettingsMap
;
233 bool HttpServerPropertiesImpl::SetSpdySetting(
234 const HostPortPair
& host_port_pair
,
236 SpdySettingsFlags flags
,
238 if (!(flags
& SETTINGS_FLAG_PLEASE_PERSIST
))
241 SettingsMap
& settings_map
= spdy_settings_map_
[host_port_pair
];
242 SettingsFlagsAndValue
flags_and_value(SETTINGS_FLAG_PERSISTED
, value
);
243 settings_map
[id
] = flags_and_value
;
247 void HttpServerPropertiesImpl::ClearSpdySettings(
248 const HostPortPair
& host_port_pair
) {
249 spdy_settings_map_
.erase(host_port_pair
);
252 void HttpServerPropertiesImpl::ClearAllSpdySettings() {
253 spdy_settings_map_
.clear();
256 const SpdySettingsMap
&
257 HttpServerPropertiesImpl::spdy_settings_map() const {
258 return spdy_settings_map_
;
261 HttpPipelinedHostCapability
HttpServerPropertiesImpl::GetPipelineCapability(
262 const HostPortPair
& origin
) {
263 HttpPipelinedHostCapability capability
= PIPELINE_UNKNOWN
;
264 CachedPipelineCapabilityMap::const_iterator it
=
265 pipeline_capability_map_
->Get(origin
);
266 if (it
!= pipeline_capability_map_
->end()) {
267 capability
= it
->second
;
272 void HttpServerPropertiesImpl::SetPipelineCapability(
273 const HostPortPair
& origin
,
274 HttpPipelinedHostCapability capability
) {
275 CachedPipelineCapabilityMap::iterator it
=
276 pipeline_capability_map_
->Peek(origin
);
277 if (it
== pipeline_capability_map_
->end() ||
278 it
->second
!= PIPELINE_INCAPABLE
) {
279 pipeline_capability_map_
->Put(origin
, capability
);
283 void HttpServerPropertiesImpl::ClearPipelineCapabilities() {
284 pipeline_capability_map_
->Clear();
287 PipelineCapabilityMap
288 HttpServerPropertiesImpl::GetPipelineCapabilityMap() const {
289 PipelineCapabilityMap result
;
290 CachedPipelineCapabilityMap::const_iterator it
;
291 for (it
= pipeline_capability_map_
->begin();
292 it
!= pipeline_capability_map_
->end(); ++it
) {
293 result
[it
->first
] = it
->second
;