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_
12 #include "base/basictypes.h"
13 #include "base/containers/mru_cache.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/time/time.h"
16 #include "net/base/host_port_pair.h"
17 #include "net/base/net_export.h"
18 #include "net/base/net_util.h"
19 #include "net/quic/quic_bandwidth.h"
20 #include "net/socket/next_proto.h"
21 #include "net/spdy/spdy_framer.h" // TODO(willchan): Reconsider this.
22 #include "net/spdy/spdy_protocol.h"
32 enum AlternateProtocolUsage
{
33 // Alternate Protocol was used without racing a normal connection.
34 ALTERNATE_PROTOCOL_USAGE_NO_RACE
= 0,
35 // Alternate Protocol was used by winning a race with a normal connection.
36 ALTERNATE_PROTOCOL_USAGE_WON_RACE
= 1,
37 // Alternate Protocol was not used by losing a race with a normal connection.
38 ALTERNATE_PROTOCOL_USAGE_LOST_RACE
= 2,
39 // Alternate Protocol was not used because no Alternate-Protocol information
40 // was available when the request was issued, but an Alternate-Protocol header
41 // was present in the response.
42 ALTERNATE_PROTOCOL_USAGE_MAPPING_MISSING
= 3,
43 // Alternate Protocol was not used because it was marked broken.
44 ALTERNATE_PROTOCOL_USAGE_BROKEN
= 4,
45 // Maximum value for the enum.
46 ALTERNATE_PROTOCOL_USAGE_MAX
,
49 // Log a histogram to reflect |usage|.
50 NET_EXPORT
void HistogramAlternateProtocolUsage(AlternateProtocolUsage usage
);
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_HTTP_2_14
, // HTTP/2 draft-14
72 NPN_SPDY_MAXIMUM_VERSION
= NPN_HTTP_2
,
74 ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
= QUIC
,
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 // (protocol, host, port) triple as defined in
96 // https://tools.ietf.org/id/draft-ietf-httpbis-alt-svc-06.html
97 struct NET_EXPORT AlternativeService
{
99 : protocol(UNINITIALIZED_ALTERNATE_PROTOCOL
), host(), port(0) {}
101 AlternativeService(AlternateProtocol protocol
,
102 const std::string
& host
,
104 : protocol(protocol
), host(host
), port(port
) {}
106 AlternativeService(AlternateProtocol protocol
,
107 const HostPortPair
& host_port_pair
)
108 : protocol(protocol
),
109 host(host_port_pair
.host()),
110 port(host_port_pair
.port()) {}
112 AlternativeService(const AlternativeService
& alternative_service
) = default;
113 AlternativeService
& operator=(const AlternativeService
& alternative_service
) =
116 HostPortPair
host_port_pair() const { return HostPortPair(host
, port
); }
118 bool operator==(const AlternativeService
& other
) const {
119 return protocol
== other
.protocol
&& host
== other
.host
&&
123 bool operator!=(const AlternativeService
& other
) const {
124 return !this->operator==(other
);
127 bool operator<(const AlternativeService
& other
) const {
128 if (protocol
!= other
.protocol
)
129 return protocol
< other
.protocol
;
130 if (host
!= other
.host
)
131 return host
< other
.host
;
132 return port
< other
.port
;
135 std::string
ToString() const;
137 AlternateProtocol protocol
;
142 struct NET_EXPORT AlternativeServiceInfo
{
143 AlternativeServiceInfo() : alternative_service(), probability(0.0) {}
145 AlternativeServiceInfo(const AlternativeService
& alternative_service
,
147 : alternative_service(alternative_service
), probability(probability
) {}
149 AlternativeServiceInfo(AlternateProtocol protocol
,
150 const std::string
& host
,
153 : alternative_service(protocol
, host
, port
), probability(probability
) {}
155 AlternativeServiceInfo(
156 const AlternativeServiceInfo
& alternative_service_info
) = default;
157 AlternativeServiceInfo
& operator=(
158 const AlternativeServiceInfo
& alternative_service_info
) = default;
160 bool operator==(const AlternativeServiceInfo
& other
) const {
161 return alternative_service
== other
.alternative_service
&&
162 probability
== other
.probability
;
165 bool operator!=(const AlternativeServiceInfo
& other
) const {
166 return !this->operator==(other
);
169 std::string
ToString() const;
171 AlternativeService alternative_service
;
175 struct NET_EXPORT SupportsQuic
{
176 SupportsQuic() : used_quic(false) {}
177 SupportsQuic(bool used_quic
, const std::string
& address
)
178 : used_quic(used_quic
),
181 bool Equals(const SupportsQuic
& other
) const {
182 return used_quic
== other
.used_quic
&& address
== other
.address
;
189 struct NET_EXPORT ServerNetworkStats
{
190 ServerNetworkStats() : bandwidth_estimate(QuicBandwidth::Zero()) {}
192 bool operator==(const ServerNetworkStats
& other
) const {
193 return srtt
== other
.srtt
&& bandwidth_estimate
== other
.bandwidth_estimate
;
196 bool operator!=(const ServerNetworkStats
& other
) const {
197 return !this->operator==(other
);
200 base::TimeDelta srtt
;
201 QuicBandwidth bandwidth_estimate
;
204 typedef std::vector
<AlternativeService
> AlternativeServiceVector
;
205 typedef std::vector
<AlternativeServiceInfo
> AlternativeServiceInfoVector
;
206 typedef base::MRUCache
<HostPortPair
, AlternativeServiceInfoVector
>
207 AlternativeServiceMap
;
208 typedef base::MRUCache
<HostPortPair
, SettingsMap
> SpdySettingsMap
;
209 typedef base::MRUCache
<HostPortPair
, ServerNetworkStats
> ServerNetworkStatsMap
;
211 extern const char kAlternateProtocolHeader
[];
213 // The interface for setting/retrieving the HTTP server properties.
214 // Currently, this class manages servers':
215 // * SPDY support (based on NPN results)
216 // * alternative service support
217 // * Spdy Settings (like CWND ID field)
218 class NET_EXPORT HttpServerProperties
{
220 HttpServerProperties() {}
221 virtual ~HttpServerProperties() {}
223 // Gets a weak pointer for this object.
224 virtual base::WeakPtr
<HttpServerProperties
> GetWeakPtr() = 0;
227 virtual void Clear() = 0;
229 // Returns true if |server| supports a network protocol which honors
230 // request prioritization.
231 virtual bool SupportsRequestPriority(const HostPortPair
& server
) = 0;
233 // Returns the value set by SetSupportsSpdy(). If not set, returns false.
234 virtual bool GetSupportsSpdy(const HostPortPair
& server
) = 0;
236 // Add |server| into the persistent store. Should only be called from IO
238 virtual void SetSupportsSpdy(const HostPortPair
& server
,
239 bool support_spdy
) = 0;
241 // Returns true if |server| has required HTTP/1.1 via HTTP/2 error code.
242 virtual bool RequiresHTTP11(const HostPortPair
& server
) = 0;
244 // Require HTTP/1.1 on subsequent connections. Not persisted.
245 virtual void SetHTTP11Required(const HostPortPair
& server
) = 0;
247 // Modify SSLConfig to force HTTP/1.1.
248 static void ForceHTTP11(SSLConfig
* ssl_config
);
250 // Modify SSLConfig to force HTTP/1.1 if necessary.
251 virtual void MaybeForceHTTP11(const HostPortPair
& server
,
252 SSLConfig
* ssl_config
) = 0;
254 // Return all alternative services for |origin| with probability greater than
255 // or equal to the threshold, including broken ones.
256 // Returned alternative services never have empty hostnames.
257 virtual AlternativeServiceVector
GetAlternativeServices(
258 const HostPortPair
& origin
) = 0;
260 // Set a single alternative service for |origin|. Previous alternative
261 // services for |origin| are discarded.
262 // |alternative_service.host| may be empty.
263 // Return true if |alternative_service_map_| is changed.
264 virtual bool SetAlternativeService(
265 const HostPortPair
& origin
,
266 const AlternativeService
& alternative_service
,
267 double alternative_probability
) = 0;
269 // Set alternative services for |origin|. Previous alternative services for
270 // |origin| are discarded.
271 // Hostnames in |alternative_service_info_vector| may be empty.
272 // Return true if |alternative_service_map_| is changed.
273 virtual bool SetAlternativeServices(
274 const HostPortPair
& origin
,
275 const AlternativeServiceInfoVector
& alternative_service_info_vector
) = 0;
277 // Marks |alternative_service| as broken.
278 // |alternative_service.host| must not be empty.
279 virtual void MarkAlternativeServiceBroken(
280 const AlternativeService
& alternative_service
) = 0;
282 // Marks |alternative_service| as recently broken.
283 // |alternative_service.host| must not be empty.
284 virtual void MarkAlternativeServiceRecentlyBroken(
285 const AlternativeService
& alternative_service
) = 0;
287 // Returns true iff |alternative_service| is currently broken.
288 // |alternative_service.host| must not be empty.
289 virtual bool IsAlternativeServiceBroken(
290 const AlternativeService
& alternative_service
) const = 0;
292 // Returns true iff |alternative_service| was recently broken.
293 // |alternative_service.host| must not be empty.
294 virtual bool WasAlternativeServiceRecentlyBroken(
295 const AlternativeService
& alternative_service
) = 0;
297 // Confirms that |alternative_service| is working.
298 // |alternative_service.host| must not be empty.
299 virtual void ConfirmAlternativeService(
300 const AlternativeService
& alternative_service
) = 0;
302 // Clear all alternative services for |origin|.
303 virtual void ClearAlternativeServices(const HostPortPair
& origin
) = 0;
305 // Returns all alternative service mappings.
306 // Returned alternative services may have empty hostnames.
307 virtual const AlternativeServiceMap
& alternative_service_map() const = 0;
309 // Returns all alternative service mappings as human readable strings.
310 // Empty alternative service hostnames will be printed as such.
311 virtual scoped_ptr
<base::Value
> GetAlternativeServiceInfoAsValue() const = 0;
313 // Sets the threshold to be used when evaluating alternative service
314 // advertisments. Only advertisements with a probability greater than or equal
315 // to |threshold| will be honored. |threshold| must be between 0.0 and 1.0
316 // inclusive. Hence, a threshold of 0.0 implies that all advertisements will
318 virtual void SetAlternativeServiceProbabilityThreshold(double threshold
) = 0;
320 // Gets a reference to the SettingsMap stored for a host.
321 // If no settings are stored, returns an empty SettingsMap.
322 virtual const SettingsMap
& GetSpdySettings(
323 const HostPortPair
& host_port_pair
) = 0;
325 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
326 // is to be persisted.
327 virtual bool SetSpdySetting(const HostPortPair
& host_port_pair
,
329 SpdySettingsFlags flags
,
332 // Clears all SPDY settings for a host.
333 virtual void ClearSpdySettings(const HostPortPair
& host_port_pair
) = 0;
335 // Clears all SPDY settings for all hosts.
336 virtual void ClearAllSpdySettings() = 0;
338 // Returns all persistent SPDY settings.
339 virtual const SpdySettingsMap
& spdy_settings_map() const = 0;
341 virtual bool GetSupportsQuic(IPAddressNumber
* last_address
) const = 0;
343 virtual void SetSupportsQuic(bool used_quic
,
344 const IPAddressNumber
& last_address
) = 0;
346 // Sets |stats| for |host_port_pair|.
347 virtual void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
348 ServerNetworkStats stats
) = 0;
350 virtual const ServerNetworkStats
* GetServerNetworkStats(
351 const HostPortPair
& host_port_pair
) = 0;
353 virtual const ServerNetworkStatsMap
& server_network_stats_map() const = 0;
356 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties
);
361 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_