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/base/net_util.h"
17 #include "net/quic/quic_bandwidth.h"
18 #include "net/socket/next_proto.h"
19 #include "net/spdy/spdy_framer.h" // TODO(willchan): Reconsider this.
20 #include "net/spdy/spdy_protocol.h"
26 enum AlternateProtocolUsage
{
27 // Alternate Protocol was used without racing a normal connection.
28 ALTERNATE_PROTOCOL_USAGE_NO_RACE
= 0,
29 // Alternate Protocol was used by winning a race with a normal connection.
30 ALTERNATE_PROTOCOL_USAGE_WON_RACE
= 1,
31 // Alternate Protocol was not used by losing a race with a normal connection.
32 ALTERNATE_PROTOCOL_USAGE_LOST_RACE
= 2,
33 // Alternate Protocol was not used because no Alternate-Protocol information
34 // was available when the request was issued, but an Alternate-Protocol header
35 // was present in the response.
36 ALTERNATE_PROTOCOL_USAGE_MAPPING_MISSING
= 3,
37 // Alternate Protocol was not used because it was marked broken.
38 ALTERNATE_PROTOCOL_USAGE_BROKEN
= 4,
39 // Maximum value for the enum.
40 ALTERNATE_PROTOCOL_USAGE_MAX
,
43 // Log a histogram to reflect |usage|.
44 NET_EXPORT
void HistogramAlternateProtocolUsage(AlternateProtocolUsage usage
);
46 enum BrokenAlternateProtocolLocation
{
47 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB
= 0,
48 BROKEN_ALTERNATE_PROTOCOL_LOCATION_QUIC_STREAM_FACTORY
= 1,
49 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB_ALT
= 2,
50 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB_MAIN
= 3,
51 BROKEN_ALTERNATE_PROTOCOL_LOCATION_MAX
,
54 // Log a histogram to reflect |location|.
55 NET_EXPORT
void HistogramBrokenAlternateProtocolLocation(
56 BrokenAlternateProtocolLocation location
);
58 enum AlternateProtocol
{
59 DEPRECATED_NPN_SPDY_2
= 0,
60 ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
= DEPRECATED_NPN_SPDY_2
,
61 NPN_SPDY_MINIMUM_VERSION
= DEPRECATED_NPN_SPDY_2
,
64 NPN_SPDY_4_14
, // HTTP/2 draft-14
65 NPN_SPDY_4_15
, // HTTP/2 draft-15
66 NPN_SPDY_MAXIMUM_VERSION
= NPN_SPDY_4_15
,
68 ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
= QUIC
,
69 UNINITIALIZED_ALTERNATE_PROTOCOL
,
72 // Simply returns whether |protocol| is between
73 // ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION and
74 // ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION (inclusive).
75 NET_EXPORT
bool IsAlternateProtocolValid(AlternateProtocol protocol
);
77 enum AlternateProtocolSize
{
78 NUM_VALID_ALTERNATE_PROTOCOLS
=
79 ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
-
80 ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
+ 1,
83 NET_EXPORT
const char* AlternateProtocolToString(AlternateProtocol protocol
);
84 NET_EXPORT AlternateProtocol
AlternateProtocolFromString(
85 const std::string
& str
);
86 NET_EXPORT_PRIVATE AlternateProtocol
AlternateProtocolFromNextProto(
87 NextProto next_proto
);
89 struct NET_EXPORT AlternateProtocolInfo
{
90 AlternateProtocolInfo()
92 protocol(UNINITIALIZED_ALTERNATE_PROTOCOL
),
96 AlternateProtocolInfo(uint16 port
,
97 AlternateProtocol protocol
,
101 probability(probability
),
104 AlternateProtocolInfo(uint16 port
,
105 AlternateProtocol protocol
,
110 probability(probability
),
111 is_broken(is_broken
) {}
113 bool Equals(const AlternateProtocolInfo
& other
) const {
114 return port
== other
.port
&&
115 protocol
== other
.protocol
&&
116 probability
== other
.probability
;
119 std::string
ToString() const;
122 AlternateProtocol protocol
;
127 struct NET_EXPORT SupportsQuic
{
128 SupportsQuic() : used_quic(false) {}
129 SupportsQuic(bool used_quic
, const std::string
& address
)
130 : used_quic(used_quic
),
133 bool Equals(const SupportsQuic
& other
) const {
134 return used_quic
== other
.used_quic
&& address
== other
.address
;
141 struct NET_EXPORT ServerNetworkStats
{
142 ServerNetworkStats() : bandwidth_estimate(QuicBandwidth::Zero()) {}
144 base::TimeDelta srtt
;
145 QuicBandwidth bandwidth_estimate
;
148 typedef base::MRUCache
<
149 HostPortPair
, AlternateProtocolInfo
> AlternateProtocolMap
;
150 typedef base::MRUCache
<HostPortPair
, SettingsMap
> SpdySettingsMap
;
151 typedef base::MRUCache
<HostPortPair
, ServerNetworkStats
> ServerNetworkStatsMap
;
153 extern const char kAlternateProtocolHeader
[];
155 // The interface for setting/retrieving the HTTP server properties.
156 // Currently, this class manages servers':
157 // * SPDY support (based on NPN results)
158 // * Alternate-Protocol support
159 // * Spdy Settings (like CWND ID field)
160 class NET_EXPORT HttpServerProperties
{
162 HttpServerProperties() {}
163 virtual ~HttpServerProperties() {}
165 // Gets a weak pointer for this object.
166 virtual base::WeakPtr
<HttpServerProperties
> GetWeakPtr() = 0;
169 virtual void Clear() = 0;
171 // Returns true if |server| supports a network protocol which honors
172 // request prioritization.
173 virtual bool SupportsRequestPriority(const HostPortPair
& server
) = 0;
175 // Add |server| into the persistent store. Should only be called from IO
177 virtual void SetSupportsSpdy(const HostPortPair
& server
,
178 bool support_spdy
) = 0;
180 // Returns true if |server| has required HTTP/1.1 via HTTP/2 error code.
181 virtual bool RequiresHTTP11(const HostPortPair
& server
) = 0;
183 // Require HTTP/1.1 on subsequent connections. Not persisted.
184 virtual void SetHTTP11Required(const HostPortPair
& server
) = 0;
186 // Modify SSLConfig to force HTTP/1.1.
187 static void ForceHTTP11(SSLConfig
* ssl_config
);
189 // Modify SSLConfig to force HTTP/1.1 if necessary.
190 virtual void MaybeForceHTTP11(const HostPortPair
& server
,
191 SSLConfig
* ssl_config
) = 0;
193 // Returns the AlternateProtocol for |server| if it has probability equal to
194 // or exceeding threshold, or else the forced AlternateProtocol if there is
195 // one, or else one with UNINITIALIZED_ALTERNATE_PROTOCOL.
196 virtual AlternateProtocolInfo
GetAlternateProtocol(
197 const HostPortPair
& server
) = 0;
199 // Sets the Alternate-Protocol for |server|.
200 virtual void SetAlternateProtocol(const HostPortPair
& server
,
201 uint16 alternate_port
,
202 AlternateProtocol alternate_protocol
,
203 double probability
) = 0;
205 // Sets the Alternate-Protocol for |server| to be BROKEN.
206 virtual void SetBrokenAlternateProtocol(const HostPortPair
& server
) = 0;
208 // Returns true if Alternate-Protocol for |server| was recently BROKEN.
209 virtual bool WasAlternateProtocolRecentlyBroken(
210 const HostPortPair
& server
) = 0;
212 // Confirms that Alternate-Protocol for |server| is working.
213 virtual void ConfirmAlternateProtocol(const HostPortPair
& server
) = 0;
215 // Clears the Alternate-Protocol for |server|.
216 virtual void ClearAlternateProtocol(const HostPortPair
& server
) = 0;
218 // Returns all Alternate-Protocol mappings.
219 virtual const AlternateProtocolMap
& alternate_protocol_map() const = 0;
221 // Sets the threshold to be used when evaluating Alternate-Protocol
222 // advertisments. Only advertisements with a with a probability
223 // greater than |threshold| will be honored. |threshold| must be
224 // between 0 and 1 inclusive. Hence, a threshold of 0 implies that
225 // all advertisements will be honored.
226 virtual void SetAlternateProtocolProbabilityThreshold(
227 double threshold
) = 0;
229 // Gets a reference to the SettingsMap stored for a host.
230 // If no settings are stored, returns an empty SettingsMap.
231 virtual const SettingsMap
& GetSpdySettings(
232 const HostPortPair
& host_port_pair
) = 0;
234 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
235 // is to be persisted.
236 virtual bool SetSpdySetting(const HostPortPair
& host_port_pair
,
238 SpdySettingsFlags flags
,
241 // Clears all SPDY settings for a host.
242 virtual void ClearSpdySettings(const HostPortPair
& host_port_pair
) = 0;
244 // Clears all SPDY settings for all hosts.
245 virtual void ClearAllSpdySettings() = 0;
247 // Returns all persistent SPDY settings.
248 virtual const SpdySettingsMap
& spdy_settings_map() const = 0;
250 virtual bool GetSupportsQuic(IPAddressNumber
* last_address
) const = 0;
252 virtual void SetSupportsQuic(bool used_quic
,
253 const IPAddressNumber
& last_address
) = 0;
255 virtual void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
256 ServerNetworkStats stats
) = 0;
258 virtual const ServerNetworkStats
* GetServerNetworkStats(
259 const HostPortPair
& host_port_pair
) = 0;
261 virtual const ServerNetworkStatsMap
& server_network_stats_map() const = 0;
264 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties
);
269 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_