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_
14 #include "base/basictypes.h"
15 #include "base/containers/hash_tables.h"
16 #include "base/gtest_prod_util.h"
17 #include "base/threading/non_thread_safe.h"
18 #include "base/values.h"
19 #include "net/base/host_port_pair.h"
20 #include "net/base/net_export.h"
21 #include "net/http/http_server_properties.h"
29 // The implementation for setting/retrieving the HTTP server properties.
30 class NET_EXPORT HttpServerPropertiesImpl
31 : public HttpServerProperties
,
32 NON_EXPORTED_BASE(public base::NonThreadSafe
) {
34 HttpServerPropertiesImpl();
35 ~HttpServerPropertiesImpl() override
;
37 // Initializes |spdy_servers_map_| with the servers (host/port) from
38 // |spdy_servers| that either support SPDY or not.
39 void InitializeSpdyServers(std::vector
<std::string
>* spdy_servers
,
42 void InitializeAlternateProtocolServers(
43 AlternateProtocolMap
* alternate_protocol_servers
);
45 void InitializeSpdySettingsServers(SpdySettingsMap
* spdy_settings_map
);
47 void InitializeSupportsQuic(IPAddressNumber
* last_address
);
49 void InitializeServerNetworkStats(
50 ServerNetworkStatsMap
* server_network_stats_map
);
52 // Get the list of servers (host/port) that support SPDY. The max_size is the
53 // number of MRU servers that support SPDY that are to be returned.
54 void GetSpdyServerList(base::ListValue
* spdy_server_list
,
55 size_t max_size
) const;
57 // Returns flattened string representation of the |host_port_pair|. Used by
59 static std::string
GetFlattenedSpdyServer(const HostPortPair
& host_port_pair
);
61 // Debugging to simulate presence of an AlternateProtocol.
62 // If we don't have an alternate protocol in the map for any given host/port
63 // pair, force this ProtocolPortPair.
64 static void ForceAlternateProtocol(const AlternateProtocolInfo
& pair
);
65 static void DisableForcedAlternateProtocol();
67 // Returns the canonical host suffix for |host|, or std::string() if none
69 std::string
GetCanonicalSuffix(const std::string
& host
);
71 // -----------------------------
72 // HttpServerProperties methods:
73 // -----------------------------
75 base::WeakPtr
<HttpServerProperties
> GetWeakPtr() override
;
76 void Clear() override
;
77 bool SupportsRequestPriority(const HostPortPair
& server
) override
;
78 void SetSupportsSpdy(const HostPortPair
& server
, bool support_spdy
) override
;
79 bool RequiresHTTP11(const HostPortPair
& server
) override
;
80 void SetHTTP11Required(const HostPortPair
& server
) override
;
81 void MaybeForceHTTP11(const HostPortPair
& server
,
82 SSLConfig
* ssl_config
) override
;
83 AlternateProtocolInfo
GetAlternateProtocol(
84 const HostPortPair
& server
) override
;
85 void SetAlternateProtocol(const HostPortPair
& server
,
86 uint16 alternate_port
,
87 AlternateProtocol alternate_protocol
,
88 double probability
) override
;
89 void SetBrokenAlternateProtocol(const HostPortPair
& server
) override
;
90 bool WasAlternateProtocolRecentlyBroken(const HostPortPair
& server
) override
;
91 void ConfirmAlternateProtocol(const HostPortPair
& server
) override
;
92 void ClearAlternateProtocol(const HostPortPair
& server
) override
;
93 const AlternateProtocolMap
& alternate_protocol_map() const override
;
94 void SetAlternateProtocolProbabilityThreshold(double threshold
) override
;
95 const SettingsMap
& GetSpdySettings(
96 const HostPortPair
& host_port_pair
) override
;
97 bool SetSpdySetting(const HostPortPair
& host_port_pair
,
99 SpdySettingsFlags flags
,
100 uint32 value
) override
;
101 void ClearSpdySettings(const HostPortPair
& host_port_pair
) override
;
102 void ClearAllSpdySettings() override
;
103 const SpdySettingsMap
& spdy_settings_map() const override
;
104 bool GetSupportsQuic(IPAddressNumber
* last_address
) const override
;
105 void SetSupportsQuic(bool used_quic
, const IPAddressNumber
& address
) override
;
106 void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
107 ServerNetworkStats stats
) override
;
108 const ServerNetworkStats
* GetServerNetworkStats(
109 const HostPortPair
& host_port_pair
) override
;
110 const ServerNetworkStatsMap
& server_network_stats_map() const override
;
113 // |spdy_servers_map_| has flattened representation of servers (host, port)
114 // that either support or not support SPDY protocol.
115 typedef base::MRUCache
<std::string
, bool> SpdyServerHostPortMap
;
116 typedef std::map
<HostPortPair
, HostPortPair
> CanonicalHostMap
;
117 typedef std::vector
<std::string
> CanonicalSufficList
;
118 typedef std::set
<HostPortPair
> Http11ServerHostPortSet
;
120 // Server, port, and AlternateProtocol: an entity that can be broken. (Once
121 // we use AlternativeService, the same AltSvc can be broken for one server but
122 // not for another depending on what certificate it can offer.)
123 struct BrokenAlternateProtocolEntry
{
124 BrokenAlternateProtocolEntry(const BrokenAlternateProtocolEntry
&) = default;
125 BrokenAlternateProtocolEntry(const HostPortPair
& server
,
127 AlternateProtocol protocol
)
128 : server(server
), port(port
), protocol(protocol
) {}
130 bool operator<(const BrokenAlternateProtocolEntry
& other
) const {
131 if (!server
.Equals(other
.server
))
132 return server
< other
.server
;
133 if (port
!= other
.port
)
134 return port
< other
.port
;
135 return protocol
< other
.protocol
;
140 AlternateProtocol protocol
;
142 // BrokenAlternateProtocolEntry with expiration time.
143 struct BrokenAlternateProtocolEntryWithTime
{
144 BrokenAlternateProtocolEntryWithTime(
145 const BrokenAlternateProtocolEntry
& broken_alternate_protocol_entry
,
146 base::TimeTicks when
)
147 : broken_alternate_protocol_entry(broken_alternate_protocol_entry
),
150 BrokenAlternateProtocolEntry broken_alternate_protocol_entry
;
151 base::TimeTicks when
;
153 // Deque of BrokenAlternateProtocolEntryWithTime items, ordered by expiration
155 typedef std::deque
<BrokenAlternateProtocolEntryWithTime
>
156 BrokenAlternateProtocolList
;
157 // Map from (server, alternate protocol and port) to the number of
158 // times that alternate protocol has been marked broken for that server.
159 typedef std::map
<BrokenAlternateProtocolEntry
, int>
160 BrokenAlternateProtocolMap
;
162 // Return the iterator for |server|, or for its canonical host, or end.
163 AlternateProtocolMap::const_iterator
GetAlternateProtocolIterator(
164 const HostPortPair
& server
);
166 // Return the canonical host for |server|, or end if none exists.
167 CanonicalHostMap::const_iterator
GetCanonicalHost(HostPortPair server
) const;
169 void RemoveCanonicalHost(const HostPortPair
& server
);
170 void ExpireBrokenAlternateProtocolMappings();
171 void ScheduleBrokenAlternateProtocolMappingsExpiration();
173 SpdyServerHostPortMap spdy_servers_map_
;
174 Http11ServerHostPortSet http11_servers_
;
176 AlternateProtocolMap alternate_protocol_map_
;
177 BrokenAlternateProtocolList broken_alternate_protocol_list_
;
178 BrokenAlternateProtocolMap broken_alternate_protocol_map_
;
180 IPAddressNumber last_quic_address_
;
181 SpdySettingsMap spdy_settings_map_
;
182 ServerNetworkStatsMap server_network_stats_map_
;
183 // Contains a map of servers which could share the same alternate protocol.
184 // Map from a Canonical host/port (host is some postfix of host names) to an
185 // actual origin, which has a plausible alternate protocol mapping.
186 CanonicalHostMap canonical_host_to_origin_map_
;
187 // Contains list of suffixes (for exmaple ".c.youtube.com",
188 // ".googlevideo.com", ".googleusercontent.com") of canonical hostnames.
189 CanonicalSufficList canonical_suffixes_
;
191 double alternate_protocol_probability_threshold_
;
193 base::WeakPtrFactory
<HttpServerPropertiesImpl
> weak_ptr_factory_
;
195 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesImpl
);
200 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_IMPL_H_