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 base::Time expiration
)
148 : alternative_service(alternative_service
),
149 probability(probability
),
150 expiration(expiration
) {}
152 AlternativeServiceInfo(AlternateProtocol protocol
,
153 const std::string
& host
,
156 base::Time expiration
)
157 : alternative_service(protocol
, host
, port
),
158 probability(probability
),
159 expiration(expiration
) {}
161 AlternativeServiceInfo(
162 const AlternativeServiceInfo
& alternative_service_info
) = default;
163 AlternativeServiceInfo
& operator=(
164 const AlternativeServiceInfo
& alternative_service_info
) = default;
166 bool operator==(const AlternativeServiceInfo
& other
) const {
167 return alternative_service
== other
.alternative_service
&&
168 probability
== other
.probability
&& expiration
== other
.expiration
;
171 bool operator!=(const AlternativeServiceInfo
& other
) const {
172 return !this->operator==(other
);
175 std::string
ToString() const;
177 AlternativeService alternative_service
;
179 base::Time expiration
;
182 struct NET_EXPORT SupportsQuic
{
183 SupportsQuic() : used_quic(false) {}
184 SupportsQuic(bool used_quic
, const std::string
& address
)
185 : used_quic(used_quic
),
188 bool Equals(const SupportsQuic
& other
) const {
189 return used_quic
== other
.used_quic
&& address
== other
.address
;
196 struct NET_EXPORT ServerNetworkStats
{
197 ServerNetworkStats() : bandwidth_estimate(QuicBandwidth::Zero()) {}
199 bool operator==(const ServerNetworkStats
& other
) const {
200 return srtt
== other
.srtt
&& bandwidth_estimate
== other
.bandwidth_estimate
;
203 bool operator!=(const ServerNetworkStats
& other
) const {
204 return !this->operator==(other
);
207 base::TimeDelta srtt
;
208 QuicBandwidth bandwidth_estimate
;
211 typedef std::vector
<AlternativeService
> AlternativeServiceVector
;
212 typedef std::vector
<AlternativeServiceInfo
> AlternativeServiceInfoVector
;
213 typedef base::MRUCache
<HostPortPair
, AlternativeServiceInfoVector
>
214 AlternativeServiceMap
;
215 typedef base::MRUCache
<HostPortPair
, SettingsMap
> SpdySettingsMap
;
216 typedef base::MRUCache
<HostPortPair
, ServerNetworkStats
> ServerNetworkStatsMap
;
218 extern const char kAlternateProtocolHeader
[];
219 extern const char kAlternativeServiceHeader
[];
221 // The interface for setting/retrieving the HTTP server properties.
222 // Currently, this class manages servers':
223 // * SPDY support (based on NPN results)
224 // * alternative service support
225 // * Spdy Settings (like CWND ID field)
226 class NET_EXPORT HttpServerProperties
{
228 HttpServerProperties() {}
229 virtual ~HttpServerProperties() {}
231 // Gets a weak pointer for this object.
232 virtual base::WeakPtr
<HttpServerProperties
> GetWeakPtr() = 0;
235 virtual void Clear() = 0;
237 // Returns true if |server| supports a network protocol which honors
238 // request prioritization.
239 virtual bool SupportsRequestPriority(const HostPortPair
& server
) = 0;
241 // Returns the value set by SetSupportsSpdy(). If not set, returns false.
242 virtual bool GetSupportsSpdy(const HostPortPair
& server
) = 0;
244 // Add |server| into the persistent store. Should only be called from IO
246 virtual void SetSupportsSpdy(const HostPortPair
& server
,
247 bool support_spdy
) = 0;
249 // Returns true if |server| has required HTTP/1.1 via HTTP/2 error code.
250 virtual bool RequiresHTTP11(const HostPortPair
& server
) = 0;
252 // Require HTTP/1.1 on subsequent connections. Not persisted.
253 virtual void SetHTTP11Required(const HostPortPair
& server
) = 0;
255 // Modify SSLConfig to force HTTP/1.1.
256 static void ForceHTTP11(SSLConfig
* ssl_config
);
258 // Modify SSLConfig to force HTTP/1.1 if necessary.
259 virtual void MaybeForceHTTP11(const HostPortPair
& server
,
260 SSLConfig
* ssl_config
) = 0;
262 // Return all alternative services for |origin| with probability greater than
263 // or equal to the threshold, including broken ones.
264 // Returned alternative services never have empty hostnames.
265 virtual AlternativeServiceVector
GetAlternativeServices(
266 const HostPortPair
& origin
) = 0;
268 // Set a single alternative service for |origin|. Previous alternative
269 // services for |origin| are discarded.
270 // |alternative_service.host| may be empty.
271 // Return true if |alternative_service_map_| is changed.
272 virtual bool SetAlternativeService(
273 const HostPortPair
& origin
,
274 const AlternativeService
& alternative_service
,
275 double alternative_probability
,
276 base::Time expiration
) = 0;
278 // Set alternative services for |origin|. Previous alternative services for
279 // |origin| are discarded.
280 // Hostnames in |alternative_service_info_vector| may be empty.
281 // Return true if |alternative_service_map_| is changed.
282 virtual bool SetAlternativeServices(
283 const HostPortPair
& origin
,
284 const AlternativeServiceInfoVector
& alternative_service_info_vector
) = 0;
286 // Marks |alternative_service| as broken.
287 // |alternative_service.host| must not be empty.
288 virtual void MarkAlternativeServiceBroken(
289 const AlternativeService
& alternative_service
) = 0;
291 // Marks |alternative_service| as recently broken.
292 // |alternative_service.host| must not be empty.
293 virtual void MarkAlternativeServiceRecentlyBroken(
294 const AlternativeService
& alternative_service
) = 0;
296 // Returns true iff |alternative_service| is currently broken.
297 // |alternative_service.host| must not be empty.
298 virtual bool IsAlternativeServiceBroken(
299 const AlternativeService
& alternative_service
) const = 0;
301 // Returns true iff |alternative_service| was recently broken.
302 // |alternative_service.host| must not be empty.
303 virtual bool WasAlternativeServiceRecentlyBroken(
304 const AlternativeService
& alternative_service
) = 0;
306 // Confirms that |alternative_service| is working.
307 // |alternative_service.host| must not be empty.
308 virtual void ConfirmAlternativeService(
309 const AlternativeService
& alternative_service
) = 0;
311 // Clear all alternative services for |origin|.
312 virtual void ClearAlternativeServices(const HostPortPair
& origin
) = 0;
314 // Returns all alternative service mappings.
315 // Returned alternative services may have empty hostnames.
316 virtual const AlternativeServiceMap
& alternative_service_map() const = 0;
318 // Returns all alternative service mappings as human readable strings.
319 // Empty alternative service hostnames will be printed as such.
320 virtual scoped_ptr
<base::Value
> GetAlternativeServiceInfoAsValue() const = 0;
322 // Sets the threshold to be used when evaluating alternative service
323 // advertisments. Only advertisements with a probability greater than or equal
324 // to |threshold| will be honored. |threshold| must be between 0.0 and 1.0
325 // inclusive. Hence, a threshold of 0.0 implies that all advertisements will
327 virtual void SetAlternativeServiceProbabilityThreshold(double threshold
) = 0;
329 // Gets a reference to the SettingsMap stored for a host.
330 // If no settings are stored, returns an empty SettingsMap.
331 virtual const SettingsMap
& GetSpdySettings(
332 const HostPortPair
& host_port_pair
) = 0;
334 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
335 // is to be persisted.
336 virtual bool SetSpdySetting(const HostPortPair
& host_port_pair
,
338 SpdySettingsFlags flags
,
341 // Clears all SPDY settings for a host.
342 virtual void ClearSpdySettings(const HostPortPair
& host_port_pair
) = 0;
344 // Clears all SPDY settings for all hosts.
345 virtual void ClearAllSpdySettings() = 0;
347 // Returns all persistent SPDY settings.
348 virtual const SpdySettingsMap
& spdy_settings_map() const = 0;
350 virtual bool GetSupportsQuic(IPAddressNumber
* last_address
) const = 0;
352 virtual void SetSupportsQuic(bool used_quic
,
353 const IPAddressNumber
& last_address
) = 0;
355 // Sets |stats| for |host_port_pair|.
356 virtual void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
357 ServerNetworkStats stats
) = 0;
359 virtual const ServerNetworkStats
* GetServerNetworkStats(
360 const HostPortPair
& host_port_pair
) = 0;
362 virtual const ServerNetworkStatsMap
& server_network_stats_map() const = 0;
365 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties
);
370 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_