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"
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|.
48 NET_EXPORT
void HistogramAlternateProtocolUsage(AlternateProtocolUsage usage
);
50 enum BrokenAlternateProtocolLocation
{
51 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB
= 0,
52 BROKEN_ALTERNATE_PROTOCOL_LOCATION_QUIC_STREAM_FACTORY
= 1,
53 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB_ALT
= 2,
54 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB_MAIN
= 3,
55 BROKEN_ALTERNATE_PROTOCOL_LOCATION_MAX
,
58 // Log a histogram to reflect |location|.
59 NET_EXPORT
void HistogramBrokenAlternateProtocolLocation(
60 BrokenAlternateProtocolLocation location
);
62 enum AlternateProtocol
{
63 DEPRECATED_NPN_SPDY_2
= 0,
64 ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
= DEPRECATED_NPN_SPDY_2
,
65 NPN_SPDY_MINIMUM_VERSION
= DEPRECATED_NPN_SPDY_2
,
68 NPN_SPDY_4_14
, // HTTP/2 draft-14
70 NPN_SPDY_MAXIMUM_VERSION
= NPN_SPDY_4
,
72 ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
= QUIC
,
73 UNINITIALIZED_ALTERNATE_PROTOCOL
,
76 // Simply returns whether |protocol| is between
77 // ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION and
78 // ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION (inclusive).
79 NET_EXPORT
bool IsAlternateProtocolValid(AlternateProtocol protocol
);
81 enum AlternateProtocolSize
{
82 NUM_VALID_ALTERNATE_PROTOCOLS
=
83 ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
-
84 ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
+ 1,
87 NET_EXPORT
const char* AlternateProtocolToString(AlternateProtocol protocol
);
88 NET_EXPORT AlternateProtocol
AlternateProtocolFromString(
89 const std::string
& str
);
90 NET_EXPORT_PRIVATE AlternateProtocol
AlternateProtocolFromNextProto(
91 NextProto next_proto
);
93 // (protocol, host, port) triple as defined in
94 // https://tools.ietf.org/id/draft-ietf-httpbis-alt-svc-06.html
95 struct NET_EXPORT AlternativeService
{
97 : protocol(UNINITIALIZED_ALTERNATE_PROTOCOL
), host(), port(0) {}
99 AlternativeService(AlternateProtocol protocol
,
100 const std::string
& host
,
102 : protocol(protocol
), host(host
), port(port
) {}
104 AlternativeService(AlternateProtocol protocol
,
105 const HostPortPair
& host_port_pair
)
106 : protocol(protocol
),
107 host(host_port_pair
.host()),
108 port(host_port_pair
.port()) {}
110 AlternativeService(const AlternativeService
& alternative_service
) = default;
111 AlternativeService
& operator=(const AlternativeService
& alternative_service
) =
114 HostPortPair
host_port_pair() { return HostPortPair(host
, port
); }
116 bool operator==(const AlternativeService
& other
) const {
117 return protocol
== other
.protocol
&& host
== other
.host
&&
121 bool operator!=(const AlternativeService
& other
) const {
122 return !this->operator==(other
);
125 bool operator<(const AlternativeService
& other
) const {
126 if (protocol
!= other
.protocol
)
127 return protocol
< other
.protocol
;
128 if (host
!= other
.host
)
129 return host
< other
.host
;
130 return port
< other
.port
;
133 std::string
ToString() const;
135 AlternateProtocol protocol
;
140 struct NET_EXPORT AlternativeServiceInfo
{
141 AlternativeServiceInfo() : alternative_service(), probability(0.0) {}
143 AlternativeServiceInfo(const AlternativeService
& alternative_service
,
145 : alternative_service(alternative_service
), probability(probability
) {}
147 AlternativeServiceInfo(AlternateProtocol protocol
,
148 const std::string
& host
,
151 : alternative_service(protocol
, host
, port
), probability(probability
) {}
153 AlternativeServiceInfo(
154 const AlternativeServiceInfo
& alternative_service_info
) = default;
155 AlternativeServiceInfo
& operator=(
156 const AlternativeServiceInfo
& alternative_service_info
) = default;
158 bool operator==(const AlternativeServiceInfo
& other
) const {
159 return alternative_service
== other
.alternative_service
&&
160 probability
== other
.probability
;
163 bool operator!=(const AlternativeServiceInfo
& other
) const {
164 return !this->operator==(other
);
167 std::string
ToString() const;
169 AlternativeService alternative_service
;
173 struct NET_EXPORT SupportsQuic
{
174 SupportsQuic() : used_quic(false) {}
175 SupportsQuic(bool used_quic
, const std::string
& address
)
176 : used_quic(used_quic
),
179 bool Equals(const SupportsQuic
& other
) const {
180 return used_quic
== other
.used_quic
&& address
== other
.address
;
187 struct NET_EXPORT ServerNetworkStats
{
188 ServerNetworkStats() : bandwidth_estimate(QuicBandwidth::Zero()) {}
190 base::TimeDelta srtt
;
191 QuicBandwidth bandwidth_estimate
;
194 typedef base::MRUCache
<HostPortPair
, AlternativeServiceInfo
>
195 AlternativeServiceMap
;
196 typedef base::MRUCache
<HostPortPair
, SettingsMap
> SpdySettingsMap
;
197 typedef base::MRUCache
<HostPortPair
, ServerNetworkStats
> ServerNetworkStatsMap
;
199 extern const char kAlternateProtocolHeader
[];
201 // The interface for setting/retrieving the HTTP server properties.
202 // Currently, this class manages servers':
203 // * SPDY support (based on NPN results)
204 // * alternative service support
205 // * Spdy Settings (like CWND ID field)
206 class NET_EXPORT HttpServerProperties
{
208 HttpServerProperties() {}
209 virtual ~HttpServerProperties() {}
211 // Gets a weak pointer for this object.
212 virtual base::WeakPtr
<HttpServerProperties
> GetWeakPtr() = 0;
215 virtual void Clear() = 0;
217 // Returns true if |server| supports a network protocol which honors
218 // request prioritization.
219 virtual bool SupportsRequestPriority(const HostPortPair
& server
) = 0;
221 // Add |server| into the persistent store. Should only be called from IO
223 virtual void SetSupportsSpdy(const HostPortPair
& server
,
224 bool support_spdy
) = 0;
226 // Returns true if |server| has required HTTP/1.1 via HTTP/2 error code.
227 virtual bool RequiresHTTP11(const HostPortPair
& server
) = 0;
229 // Require HTTP/1.1 on subsequent connections. Not persisted.
230 virtual void SetHTTP11Required(const HostPortPair
& server
) = 0;
232 // Modify SSLConfig to force HTTP/1.1.
233 static void ForceHTTP11(SSLConfig
* ssl_config
);
235 // Modify SSLConfig to force HTTP/1.1 if necessary.
236 virtual void MaybeForceHTTP11(const HostPortPair
& server
,
237 SSLConfig
* ssl_config
) = 0;
239 // Returns the alternative service for |origin| if it has probability equal to
240 // or exceeding threshold, or else the forced AlternateProtocol if there is
241 // one, or else one with UNINITIALIZED_ALTERNATE_PROTOCOL.
242 virtual AlternativeService
GetAlternativeService(
243 const HostPortPair
& origin
) = 0;
245 // Sets the alternative service for |origin|.
246 virtual void SetAlternativeService(
247 const HostPortPair
& origin
,
248 const AlternativeService
& alternative_service
,
249 double alternative_probability
) = 0;
251 // Marks |alternative_service| as broken.
252 virtual void MarkAlternativeServiceBroken(
253 const AlternativeService
& alternative_service
) = 0;
255 // Marks |alternative_service| as recently broken.
256 virtual void MarkAlternativeServiceRecentlyBroken(
257 const AlternativeService
& alternative_service
) = 0;
259 // Returns true iff |alternative_service| is currently broken.
260 virtual bool IsAlternativeServiceBroken(
261 const AlternativeService
& alternative_service
) const = 0;
263 // Returns true iff |alternative_service| was recently broken.
264 virtual bool WasAlternativeServiceRecentlyBroken(
265 const AlternativeService
& alternative_service
) = 0;
267 // Confirms that |alternative_service| is working.
268 virtual void ConfirmAlternativeService(
269 const AlternativeService
& alternative_service
) = 0;
271 // Clears the alternative service for |origin|.
272 virtual void ClearAlternativeService(const HostPortPair
& origin
) = 0;
274 // Returns all alternative service mappings.
275 virtual const AlternativeServiceMap
& alternative_service_map() const = 0;
277 // Returns all alternative service mappings as human readable strings.
278 virtual base::Value
* GetAlternativeServiceInfoAsValue() const = 0;
280 // Sets the threshold to be used when evaluating alternative service
281 // advertisments. Only advertisements with a probability greater than or equal
282 // to |threshold| will be honored. |threshold| must be between 0.0 and 1.0
283 // inclusive. Hence, a threshold of 0.0 implies that all advertisements will
285 virtual void SetAlternativeServiceProbabilityThreshold(double threshold
) = 0;
287 // Gets a reference to the SettingsMap stored for a host.
288 // If no settings are stored, returns an empty SettingsMap.
289 virtual const SettingsMap
& GetSpdySettings(
290 const HostPortPair
& host_port_pair
) = 0;
292 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
293 // is to be persisted.
294 virtual bool SetSpdySetting(const HostPortPair
& host_port_pair
,
296 SpdySettingsFlags flags
,
299 // Clears all SPDY settings for a host.
300 virtual void ClearSpdySettings(const HostPortPair
& host_port_pair
) = 0;
302 // Clears all SPDY settings for all hosts.
303 virtual void ClearAllSpdySettings() = 0;
305 // Returns all persistent SPDY settings.
306 virtual const SpdySettingsMap
& spdy_settings_map() const = 0;
308 virtual bool GetSupportsQuic(IPAddressNumber
* last_address
) const = 0;
310 virtual void SetSupportsQuic(bool used_quic
,
311 const IPAddressNumber
& last_address
) = 0;
313 virtual void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
314 ServerNetworkStats stats
) = 0;
316 virtual const ServerNetworkStats
* GetServerNetworkStats(
317 const HostPortPair
& host_port_pair
) = 0;
319 virtual const ServerNetworkStatsMap
& server_network_stats_map() const = 0;
322 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties
);
327 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_