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_H_
6 #define NET_HTTP_HTTP_SERVER_PROPERTIES_H_
10 #include "base/basictypes.h"
11 #include "base/containers/mru_cache.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/time/time.h"
14 #include "net/base/host_port_pair.h"
15 #include "net/base/net_export.h"
16 #include "net/quic/quic_bandwidth.h"
17 #include "net/socket/next_proto.h"
18 #include "net/spdy/spdy_framer.h" // TODO(willchan): Reconsider this.
19 #include "net/spdy/spdy_protocol.h"
23 enum AlternateProtocolUsage
{
24 // Alternate Protocol was used without racing a normal connection.
25 ALTERNATE_PROTOCOL_USAGE_NO_RACE
= 0,
26 // Alternate Protocol was used by winning a race with a normal connection.
27 ALTERNATE_PROTOCOL_USAGE_WON_RACE
= 1,
28 // Alternate Protocol was not used by losing a race with a normal connection.
29 ALTERNATE_PROTOCOL_USAGE_LOST_RACE
= 2,
30 // Alternate Protocol was not used because no Alternate-Protocol information
31 // was available when the request was issued, but an Alternate-Protocol header
32 // was present in the response.
33 ALTERNATE_PROTOCOL_USAGE_MAPPING_MISSING
= 3,
34 // Alternate Protocol was not used because it was marked broken.
35 ALTERNATE_PROTOCOL_USAGE_BROKEN
= 4,
36 // Maximum value for the enum.
37 ALTERNATE_PROTOCOL_USAGE_MAX
,
40 // Log a histogram to reflect |usage|.
41 NET_EXPORT
void HistogramAlternateProtocolUsage(AlternateProtocolUsage usage
);
43 enum BrokenAlternateProtocolLocation
{
44 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB
= 0,
45 BROKEN_ALTERNATE_PROTOCOL_LOCATION_QUIC_STREAM_FACTORY
= 1,
46 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB_ALT
= 2,
47 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB_MAIN
= 3,
48 BROKEN_ALTERNATE_PROTOCOL_LOCATION_MAX
,
51 // Log a histogram to reflect |location|.
52 NET_EXPORT
void HistogramBrokenAlternateProtocolLocation(
53 BrokenAlternateProtocolLocation location
);
55 enum AlternateProtocol
{
56 DEPRECATED_NPN_SPDY_2
= 0,
57 ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
= DEPRECATED_NPN_SPDY_2
,
58 NPN_SPDY_MINIMUM_VERSION
= DEPRECATED_NPN_SPDY_2
,
61 NPN_SPDY_4_14
, // HTTP/2 draft-14
62 NPN_SPDY_4_15
, // HTTP/2 draft-15
63 NPN_SPDY_MAXIMUM_VERSION
= NPN_SPDY_4_15
,
65 ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
= QUIC
,
66 UNINITIALIZED_ALTERNATE_PROTOCOL
,
69 // Simply returns whether |protocol| is between
70 // ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION and
71 // ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION (inclusive).
72 NET_EXPORT
bool IsAlternateProtocolValid(AlternateProtocol protocol
);
74 enum AlternateProtocolSize
{
75 NUM_VALID_ALTERNATE_PROTOCOLS
=
76 ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
-
77 ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
+ 1,
80 NET_EXPORT
const char* AlternateProtocolToString(AlternateProtocol protocol
);
81 NET_EXPORT AlternateProtocol
AlternateProtocolFromString(
82 const std::string
& str
);
83 NET_EXPORT_PRIVATE AlternateProtocol
AlternateProtocolFromNextProto(
84 NextProto next_proto
);
86 struct NET_EXPORT AlternateProtocolInfo
{
87 AlternateProtocolInfo(uint16 port
,
88 AlternateProtocol protocol
,
92 probability(probability
),
95 AlternateProtocolInfo(uint16 port
,
96 AlternateProtocol protocol
,
101 probability(probability
),
102 is_broken(is_broken
) {}
104 bool Equals(const AlternateProtocolInfo
& other
) const {
105 return port
== other
.port
&&
106 protocol
== other
.protocol
&&
107 probability
== other
.probability
;
110 std::string
ToString() const;
113 AlternateProtocol protocol
;
118 struct NET_EXPORT SupportsQuic
{
119 SupportsQuic() : used_quic(false) {}
120 SupportsQuic(bool used_quic
, const std::string
& address
)
121 : used_quic(used_quic
),
124 bool Equals(const SupportsQuic
& other
) const {
125 return used_quic
== other
.used_quic
&& address
== other
.address
;
132 typedef base::MRUCache
<
133 HostPortPair
, AlternateProtocolInfo
> AlternateProtocolMap
;
134 typedef base::MRUCache
<HostPortPair
, SettingsMap
> SpdySettingsMap
;
135 typedef std::map
<HostPortPair
, SupportsQuic
> SupportsQuicMap
;
137 extern const char kAlternateProtocolHeader
[];
139 // The interface for setting/retrieving the HTTP server properties.
140 // Currently, this class manages servers':
141 // * SPDY support (based on NPN results)
142 // * Alternate-Protocol support
143 // * Spdy Settings (like CWND ID field)
144 class NET_EXPORT HttpServerProperties
{
146 struct NetworkStats
{
147 NetworkStats() : bandwidth_estimate(QuicBandwidth::Zero()) {}
149 base::TimeDelta srtt
;
150 QuicBandwidth bandwidth_estimate
;
153 HttpServerProperties() {}
154 virtual ~HttpServerProperties() {}
156 // Gets a weak pointer for this object.
157 virtual base::WeakPtr
<HttpServerProperties
> GetWeakPtr() = 0;
160 virtual void Clear() = 0;
162 // Returns true if |server| supports SPDY.
163 virtual bool SupportsSpdy(const HostPortPair
& server
) = 0;
165 // Add |server| into the persistent store. Should only be called from IO
167 virtual void SetSupportsSpdy(const HostPortPair
& server
,
168 bool support_spdy
) = 0;
170 // Returns true if |server| has an Alternate-Protocol header.
171 virtual bool HasAlternateProtocol(const HostPortPair
& server
) = 0;
173 // Returns the Alternate-Protocol and port for |server|.
174 // HasAlternateProtocol(server) must be true.
175 virtual AlternateProtocolInfo
GetAlternateProtocol(
176 const HostPortPair
& server
) = 0;
178 // Sets the Alternate-Protocol for |server|.
179 virtual void SetAlternateProtocol(const HostPortPair
& server
,
180 uint16 alternate_port
,
181 AlternateProtocol alternate_protocol
,
182 double probability
) = 0;
184 // Sets the Alternate-Protocol for |server| to be BROKEN.
185 virtual void SetBrokenAlternateProtocol(const HostPortPair
& server
) = 0;
187 // Returns true if Alternate-Protocol for |server| was recently BROKEN.
188 virtual bool WasAlternateProtocolRecentlyBroken(
189 const HostPortPair
& server
) = 0;
191 // Confirms that Alternate-Protocol for |server| is working.
192 virtual void ConfirmAlternateProtocol(const HostPortPair
& server
) = 0;
194 // Clears the Alternate-Protocol for |server|.
195 virtual void ClearAlternateProtocol(const HostPortPair
& server
) = 0;
197 // Returns all Alternate-Protocol mappings.
198 virtual const AlternateProtocolMap
& alternate_protocol_map() const = 0;
200 // Sets the threshold to be used when evaluating Alternate-Protocol
201 // advertisments. Only advertisements with a with a probability
202 // greater than |threshold| will be honored. |threshold| must be
203 // between 0 and 1 inclusive. Hence, a threshold of 0 implies that
204 // all advertisements will be honored.
205 virtual void SetAlternateProtocolProbabilityThreshold(
206 double threshold
) = 0;
208 // Gets a reference to the SettingsMap stored for a host.
209 // If no settings are stored, returns an empty SettingsMap.
210 virtual const SettingsMap
& GetSpdySettings(
211 const HostPortPair
& host_port_pair
) = 0;
213 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
214 // is to be persisted.
215 virtual bool SetSpdySetting(const HostPortPair
& host_port_pair
,
217 SpdySettingsFlags flags
,
220 // Clears all SPDY settings for a host.
221 virtual void ClearSpdySettings(const HostPortPair
& host_port_pair
) = 0;
223 // Clears all SPDY settings for all hosts.
224 virtual void ClearAllSpdySettings() = 0;
226 // Returns all persistent SPDY settings.
227 virtual const SpdySettingsMap
& spdy_settings_map() const = 0;
229 // TODO(rtenneti): Make SupportsQuic a global (instead of per host_port_pair).
230 virtual SupportsQuic
GetSupportsQuic(
231 const HostPortPair
& host_port_pair
) const = 0;
233 virtual void SetSupportsQuic(const HostPortPair
& host_port_pair
,
235 const std::string
& address
) = 0;
237 virtual const SupportsQuicMap
& supports_quic_map() const = 0;
239 virtual void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
240 NetworkStats stats
) = 0;
242 virtual const NetworkStats
* GetServerNetworkStats(
243 const HostPortPair
& host_port_pair
) const = 0;
246 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties
);
251 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_