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 #ifndef NET_HTTP_HTTP_SERVER_PROPERTIES_IMPL_H_
6 #define NET_HTTP_HTTP_SERVER_PROPERTIES_IMPL_H_
12 #include "base/basictypes.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "base/values.h"
17 #include "net/base/host_port_pair.h"
18 #include "net/base/net_export.h"
19 #include "net/http/http_server_properties.h"
27 // The implementation for setting/retrieving the HTTP server properties.
28 class NET_EXPORT HttpServerPropertiesImpl
29 : public HttpServerProperties
,
30 NON_EXPORTED_BASE(public base::NonThreadSafe
) {
32 HttpServerPropertiesImpl();
33 ~HttpServerPropertiesImpl() override
;
35 // Initializes |spdy_servers_map_| with the servers (host/port) from
36 // |spdy_servers| that either support SPDY or not.
37 void InitializeSpdyServers(std::vector
<std::string
>* spdy_servers
,
40 void InitializeAlternateProtocolServers(
41 AlternateProtocolMap
* alternate_protocol_servers
);
43 void InitializeSpdySettingsServers(SpdySettingsMap
* spdy_settings_map
);
45 void InitializeSupportsQuic(SupportsQuicMap
* supports_quic_map
);
47 // Get the list of servers (host/port) that support SPDY. The max_size is the
48 // number of MRU servers that support SPDY that are to be returned.
49 void GetSpdyServerList(base::ListValue
* spdy_server_list
,
50 size_t max_size
) const;
52 // Returns flattened string representation of the |host_port_pair|. Used by
54 static std::string
GetFlattenedSpdyServer(
55 const net::HostPortPair
& host_port_pair
);
57 // Debugging to simulate presence of an AlternateProtocol.
58 // If we don't have an alternate protocol in the map for any given host/port
59 // pair, force this ProtocolPortPair.
60 static void ForceAlternateProtocol(const AlternateProtocolInfo
& pair
);
61 static void DisableForcedAlternateProtocol();
63 // Returns the canonical host suffix for |server|, or std::string() if none
65 std::string
GetCanonicalSuffix(const net::HostPortPair
& server
);
67 // -----------------------------
68 // HttpServerProperties methods:
69 // -----------------------------
71 // Gets a weak pointer for this object.
72 base::WeakPtr
<HttpServerProperties
> GetWeakPtr() override
;
75 void Clear() override
;
77 // Returns true if |server| supports SPDY.
78 bool SupportsSpdy(const HostPortPair
& server
) override
;
80 // Add |server| into the persistent store.
81 void SetSupportsSpdy(const HostPortPair
& server
, bool support_spdy
) override
;
83 // Returns true if |server| has an Alternate-Protocol header.
84 bool HasAlternateProtocol(const HostPortPair
& server
) override
;
86 // Returns the Alternate-Protocol and port for |server|.
87 // HasAlternateProtocol(server) must be true.
88 AlternateProtocolInfo
GetAlternateProtocol(
89 const HostPortPair
& server
) override
;
91 // Sets the Alternate-Protocol for |server|.
92 void SetAlternateProtocol(const HostPortPair
& server
,
93 uint16 alternate_port
,
94 AlternateProtocol alternate_protocol
,
95 double probability
) override
;
97 // Sets the Alternate-Protocol for |server| to be BROKEN.
98 void SetBrokenAlternateProtocol(const HostPortPair
& server
) override
;
100 // Returns true if Alternate-Protocol for |server| was recently BROKEN.
101 bool WasAlternateProtocolRecentlyBroken(const HostPortPair
& server
) override
;
103 // Confirms that Alternate-Protocol for |server| is working.
104 void ConfirmAlternateProtocol(const HostPortPair
& server
) override
;
106 // Clears the Alternate-Protocol for |server|.
107 void ClearAlternateProtocol(const HostPortPair
& server
) override
;
109 // Returns all Alternate-Protocol mappings.
110 const AlternateProtocolMap
& alternate_protocol_map() const override
;
112 void SetAlternateProtocolProbabilityThreshold(double threshold
) override
;
114 // Gets a reference to the SettingsMap stored for a host.
115 // If no settings are stored, returns an empty SettingsMap.
116 const SettingsMap
& GetSpdySettings(
117 const HostPortPair
& host_port_pair
) override
;
119 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
120 // is to be persisted.
121 bool SetSpdySetting(const HostPortPair
& host_port_pair
,
123 SpdySettingsFlags flags
,
124 uint32 value
) override
;
126 // Clears all entries in |spdy_settings_map_| for a host.
127 void ClearSpdySettings(const HostPortPair
& host_port_pair
) override
;
129 // Clears all entries in |spdy_settings_map_|.
130 void ClearAllSpdySettings() override
;
132 // Returns all persistent SPDY settings.
133 const SpdySettingsMap
& spdy_settings_map() const override
;
135 // Methods for SupportsQuic.
136 SupportsQuic
GetSupportsQuic(
137 const HostPortPair
& host_port_pair
) const override
;
139 void SetSupportsQuic(const HostPortPair
& host_port_pair
,
141 const std::string
& address
) override
;
143 const SupportsQuicMap
& supports_quic_map() const override
;
145 // Methods for NetworkStats.
146 void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
147 NetworkStats stats
) override
;
149 const NetworkStats
* GetServerNetworkStats(
150 const HostPortPair
& host_port_pair
) const override
;
153 // |spdy_servers_map_| has flattened representation of servers (host, port)
154 // that either support or not support SPDY protocol.
155 typedef base::MRUCache
<std::string
, bool> SpdyServerHostPortMap
;
156 typedef std::map
<HostPortPair
, NetworkStats
> ServerNetworkStatsMap
;
157 typedef std::map
<HostPortPair
, HostPortPair
> CanonicalHostMap
;
158 typedef std::vector
<std::string
> CanonicalSufficList
;
159 // List of broken host:ports and the times when they can be expired.
160 struct BrokenAlternateProtocolEntry
{
162 base::TimeTicks when
;
164 typedef std::list
<BrokenAlternateProtocolEntry
>
165 BrokenAlternateProtocolList
;
166 // Map from host:port to the number of times alternate protocol has
167 // been marked broken.
168 typedef std::map
<HostPortPair
, int> BrokenAlternateProtocolMap
;
170 // Return the canonical host for |server|, or end if none exists.
171 CanonicalHostMap::const_iterator
GetCanonicalHost(HostPortPair server
) const;
173 void RemoveCanonicalHost(const HostPortPair
& server
);
174 void ExpireBrokenAlternateProtocolMappings();
175 void ScheduleBrokenAlternateProtocolMappingsExpiration();
177 SpdyServerHostPortMap spdy_servers_map_
;
179 AlternateProtocolMap alternate_protocol_map_
;
180 BrokenAlternateProtocolList broken_alternate_protocol_list_
;
181 BrokenAlternateProtocolMap broken_alternate_protocol_map_
;
183 SpdySettingsMap spdy_settings_map_
;
184 SupportsQuicMap supports_quic_map_
;
185 ServerNetworkStatsMap server_network_stats_map_
;
186 // Contains a map of servers which could share the same alternate protocol.
187 // Map from a Canonical host/port (host is some postfix of host names) to an
188 // actual origin, which has a plausible alternate protocol mapping.
189 CanonicalHostMap canonical_host_to_origin_map_
;
190 // Contains list of suffixes (for exmaple ".c.youtube.com",
191 // ".googlevideo.com", ".googleusercontent.com") of canonical hostnames.
192 CanonicalSufficList canonical_suffixes_
;
194 double alternate_protocol_probability_threshold_
;
196 base::WeakPtrFactory
<HttpServerPropertiesImpl
> weak_ptr_factory_
;
198 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesImpl
);
203 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_IMPL_H_