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/socket/next_proto.h"
17 #include "net/spdy/spdy_framer.h" // TODO(willchan): Reconsider this.
21 enum AlternateProtocolExperiment
{
22 // 200 alternate_protocol servers are loaded (persisted 200 MRU servers).
23 ALTERNATE_PROTOCOL_NOT_PART_OF_EXPERIMENT
= 0,
24 // 200 alternate_protocol servers are loaded (persisted 1000 MRU servers).
25 ALTERNATE_PROTOCOL_TRUNCATED_200_SERVERS
,
26 // 1000 alternate_protocol servers are loaded (persisted 1000 MRU servers).
27 ALTERNATE_PROTOCOL_TRUNCATED_1000_SERVERS
,
30 enum AlternateProtocolUsage
{
31 // Alternate Protocol was used without racing a normal connection.
32 ALTERNATE_PROTOCOL_USAGE_NO_RACE
= 0,
33 // Alternate Protocol was used by winning a race with a normal connection.
34 ALTERNATE_PROTOCOL_USAGE_WON_RACE
= 1,
35 // Alternate Protocol was not used by losing a race with a normal connection.
36 ALTERNATE_PROTOCOL_USAGE_LOST_RACE
= 2,
37 // Alternate Protocol was not used because no Alternate-Protocol information
38 // was available when the request was issued, but an Alternate-Protocol header
39 // was present in the response.
40 ALTERNATE_PROTOCOL_USAGE_MAPPING_MISSING
= 3,
41 // Alternate Protocol was not used because it was marked broken.
42 ALTERNATE_PROTOCOL_USAGE_BROKEN
= 4,
43 // Maximum value for the enum.
44 ALTERNATE_PROTOCOL_USAGE_MAX
,
47 // Log a histogram to reflect |usage| and |alternate_protocol_experiment|.
48 NET_EXPORT
void HistogramAlternateProtocolUsage(
49 AlternateProtocolUsage usage
,
50 AlternateProtocolExperiment alternate_protocol_experiment
);
52 enum BrokenAlternateProtocolLocation
{
53 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB
= 0,
54 BROKEN_ALTERNATE_PROTOCOL_LOCATION_QUIC_STREAM_FACTORY
= 1,
55 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB_ALT
= 2,
56 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB_MAIN
= 3,
57 BROKEN_ALTERNATE_PROTOCOL_LOCATION_MAX
,
60 // Log a histogram to reflect |location|.
61 NET_EXPORT
void HistogramBrokenAlternateProtocolLocation(
62 BrokenAlternateProtocolLocation location
);
64 enum AlternateProtocol
{
65 DEPRECATED_NPN_SPDY_2
= 0,
66 ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
= DEPRECATED_NPN_SPDY_2
,
67 NPN_SPDY_MINIMUM_VERSION
= DEPRECATED_NPN_SPDY_2
,
70 NPN_SPDY_4
, // SPDY4 is HTTP/2.
71 NPN_SPDY_MAXIMUM_VERSION
= NPN_SPDY_4
,
73 ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
= QUIC
,
74 ALTERNATE_PROTOCOL_BROKEN
, // The alternate protocol is known to be broken.
75 UNINITIALIZED_ALTERNATE_PROTOCOL
,
78 // Simply returns whether |protocol| is between
79 // ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION and
80 // ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION (inclusive).
81 NET_EXPORT
bool IsAlternateProtocolValid(AlternateProtocol protocol
);
83 enum AlternateProtocolSize
{
84 NUM_VALID_ALTERNATE_PROTOCOLS
=
85 ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
-
86 ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
+ 1,
89 NET_EXPORT
const char* AlternateProtocolToString(AlternateProtocol protocol
);
90 NET_EXPORT AlternateProtocol
AlternateProtocolFromString(
91 const std::string
& str
);
92 NET_EXPORT_PRIVATE AlternateProtocol
AlternateProtocolFromNextProto(
93 NextProto next_proto
);
95 struct NET_EXPORT PortAlternateProtocolPair
{
96 bool Equals(const PortAlternateProtocolPair
& other
) const {
97 return port
== other
.port
&& protocol
== other
.protocol
;
100 std::string
ToString() const;
103 AlternateProtocol protocol
;
106 typedef base::MRUCache
<
107 HostPortPair
, PortAlternateProtocolPair
> AlternateProtocolMap
;
108 typedef base::MRUCache
<HostPortPair
, SettingsMap
> SpdySettingsMap
;
110 extern const char kAlternateProtocolHeader
[];
112 // The interface for setting/retrieving the HTTP server properties.
113 // Currently, this class manages servers':
114 // * SPDY support (based on NPN results)
115 // * Alternate-Protocol support
116 // * Spdy Settings (like CWND ID field)
117 class NET_EXPORT HttpServerProperties
{
119 struct NetworkStats
{
120 base::TimeDelta srtt
;
121 uint64 bandwidth_estimate
;
124 HttpServerProperties() {}
125 virtual ~HttpServerProperties() {}
127 // Gets a weak pointer for this object.
128 virtual base::WeakPtr
<HttpServerProperties
> GetWeakPtr() = 0;
131 virtual void Clear() = 0;
133 // Returns true if |server| supports SPDY.
134 virtual bool SupportsSpdy(const HostPortPair
& server
) = 0;
136 // Add |server| into the persistent store. Should only be called from IO
138 virtual void SetSupportsSpdy(const HostPortPair
& server
,
139 bool support_spdy
) = 0;
141 // Returns true if |server| has an Alternate-Protocol header.
142 virtual bool HasAlternateProtocol(const HostPortPair
& server
) = 0;
144 // Returns the Alternate-Protocol and port for |server|.
145 // HasAlternateProtocol(server) must be true.
146 virtual PortAlternateProtocolPair
GetAlternateProtocol(
147 const HostPortPair
& server
) = 0;
149 // Sets the Alternate-Protocol for |server|.
150 virtual void SetAlternateProtocol(const HostPortPair
& server
,
151 uint16 alternate_port
,
152 AlternateProtocol alternate_protocol
) = 0;
154 // Sets the Alternate-Protocol for |server| to be BROKEN.
155 virtual void SetBrokenAlternateProtocol(const HostPortPair
& server
) = 0;
157 // Returns true if Alternate-Protocol for |server| was recently BROKEN.
158 virtual bool WasAlternateProtocolRecentlyBroken(
159 const HostPortPair
& server
) = 0;
161 // Confirms that Alternate-Protocol for |server| is working.
162 virtual void ConfirmAlternateProtocol(const HostPortPair
& server
) = 0;
164 // Clears the Alternate-Protocol for |server|.
165 virtual void ClearAlternateProtocol(const HostPortPair
& server
) = 0;
167 // Returns all Alternate-Protocol mappings.
168 virtual const AlternateProtocolMap
& alternate_protocol_map() const = 0;
170 virtual void SetAlternateProtocolExperiment(
171 AlternateProtocolExperiment experiment
) = 0;
173 virtual AlternateProtocolExperiment
GetAlternateProtocolExperiment()
176 // Gets a reference to the SettingsMap stored for a host.
177 // If no settings are stored, returns an empty SettingsMap.
178 virtual const SettingsMap
& GetSpdySettings(
179 const HostPortPair
& host_port_pair
) = 0;
181 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
182 // is to be persisted.
183 virtual bool SetSpdySetting(const HostPortPair
& host_port_pair
,
185 SpdySettingsFlags flags
,
188 // Clears all SPDY settings for a host.
189 virtual void ClearSpdySettings(const HostPortPair
& host_port_pair
) = 0;
191 // Clears all SPDY settings for all hosts.
192 virtual void ClearAllSpdySettings() = 0;
194 // Returns all persistent SPDY settings.
195 virtual const SpdySettingsMap
& spdy_settings_map() const = 0;
197 virtual void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
198 NetworkStats stats
) = 0;
200 virtual const NetworkStats
* GetServerNetworkStats(
201 const HostPortPair
& host_port_pair
) const = 0;
204 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties
);
209 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_