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 if (protocol
!= other
.protocol
)
123 return protocol
< other
.protocol
;
124 if (host
!= other
.host
)
125 return host
< other
.host
;
126 return port
< other
.port
;
129 std::string
ToString() const;
131 AlternateProtocol protocol
;
136 struct NET_EXPORT AlternativeServiceInfo
{
137 AlternativeServiceInfo() : alternative_service(), probability(0.0) {}
139 AlternativeServiceInfo(const AlternativeService
& alternative_service
,
141 : alternative_service(alternative_service
), probability(probability
) {}
143 AlternativeServiceInfo(AlternateProtocol protocol
,
144 const std::string
& host
,
147 : alternative_service(protocol
, host
, port
), probability(probability
) {}
149 AlternativeServiceInfo(
150 const AlternativeServiceInfo
& alternative_service_info
) = default;
151 AlternativeServiceInfo
& operator=(
152 const AlternativeServiceInfo
& alternative_service_info
) = default;
154 bool operator==(const AlternativeServiceInfo
& other
) const {
155 return alternative_service
== other
.alternative_service
&&
156 probability
== other
.probability
;
159 bool operator!=(const AlternativeServiceInfo
& other
) const {
160 return !this->operator==(other
);
163 std::string
ToString() const;
165 AlternativeService alternative_service
;
169 struct NET_EXPORT SupportsQuic
{
170 SupportsQuic() : used_quic(false) {}
171 SupportsQuic(bool used_quic
, const std::string
& address
)
172 : used_quic(used_quic
),
175 bool Equals(const SupportsQuic
& other
) const {
176 return used_quic
== other
.used_quic
&& address
== other
.address
;
183 struct NET_EXPORT ServerNetworkStats
{
184 ServerNetworkStats() : bandwidth_estimate(QuicBandwidth::Zero()) {}
186 base::TimeDelta srtt
;
187 QuicBandwidth bandwidth_estimate
;
190 typedef base::MRUCache
<HostPortPair
, AlternativeServiceInfo
>
191 AlternativeServiceMap
;
192 typedef base::MRUCache
<HostPortPair
, SettingsMap
> SpdySettingsMap
;
193 typedef base::MRUCache
<HostPortPair
, ServerNetworkStats
> ServerNetworkStatsMap
;
195 extern const char kAlternateProtocolHeader
[];
197 // The interface for setting/retrieving the HTTP server properties.
198 // Currently, this class manages servers':
199 // * SPDY support (based on NPN results)
200 // * alternative service support
201 // * Spdy Settings (like CWND ID field)
202 class NET_EXPORT HttpServerProperties
{
204 HttpServerProperties() {}
205 virtual ~HttpServerProperties() {}
207 // Gets a weak pointer for this object.
208 virtual base::WeakPtr
<HttpServerProperties
> GetWeakPtr() = 0;
211 virtual void Clear() = 0;
213 // Returns true if |server| supports a network protocol which honors
214 // request prioritization.
215 virtual bool SupportsRequestPriority(const HostPortPair
& server
) = 0;
217 // Add |server| into the persistent store. Should only be called from IO
219 virtual void SetSupportsSpdy(const HostPortPair
& server
,
220 bool support_spdy
) = 0;
222 // Returns true if |server| has required HTTP/1.1 via HTTP/2 error code.
223 virtual bool RequiresHTTP11(const HostPortPair
& server
) = 0;
225 // Require HTTP/1.1 on subsequent connections. Not persisted.
226 virtual void SetHTTP11Required(const HostPortPair
& server
) = 0;
228 // Modify SSLConfig to force HTTP/1.1.
229 static void ForceHTTP11(SSLConfig
* ssl_config
);
231 // Modify SSLConfig to force HTTP/1.1 if necessary.
232 virtual void MaybeForceHTTP11(const HostPortPair
& server
,
233 SSLConfig
* ssl_config
) = 0;
235 // Returns the alternative service for |origin| if it has probability equal to
236 // or exceeding threshold, or else the forced AlternateProtocol if there is
237 // one, or else one with UNINITIALIZED_ALTERNATE_PROTOCOL.
238 virtual AlternativeService
GetAlternativeService(
239 const HostPortPair
& origin
) = 0;
241 // Sets the alternative service for |origin|.
242 // TODO(bnc): alternative_service.host is currently ignored, fix it.
243 virtual void SetAlternativeService(
244 const HostPortPair
& origin
,
245 const AlternativeService
& alternative_service
,
246 double alternative_probability
) = 0;
248 // Marks |alternative_service| as broken.
249 virtual void MarkAlternativeServiceBroken(
250 const AlternativeService
& alternative_service
) = 0;
252 // Marks |alternative_service| as recently broken.
253 virtual void MarkAlternativeServiceRecentlyBroken(
254 const AlternativeService
& alternative_service
) = 0;
256 // Returns true iff |alternative_service| is currently broken.
257 virtual bool IsAlternativeServiceBroken(
258 const AlternativeService
& alternative_service
) const = 0;
260 // Returns true iff |alternative_service| was recently broken.
261 virtual bool WasAlternativeServiceRecentlyBroken(
262 const AlternativeService
& alternative_service
) = 0;
264 // Confirms that |alternative_service| is working.
265 virtual void ConfirmAlternativeService(
266 const AlternativeService
& alternative_service
) = 0;
268 // Clears the alternative service for |origin|.
269 virtual void ClearAlternativeService(const HostPortPair
& origin
) = 0;
271 // Returns all alternative service mappings.
272 virtual const AlternativeServiceMap
& alternative_service_map() const = 0;
274 // Returns all alternative service mappings as human readable strings.
275 virtual base::Value
* GetAlternativeServiceInfoAsValue() const = 0;
277 // Sets the threshold to be used when evaluating alternative service
278 // advertisments. Only advertisements with a probability greater than or equal
279 // to |threshold| will be honored. |threshold| must be between 0.0 and 1.0
280 // inclusive. Hence, a threshold of 0.0 implies that all advertisements will
282 virtual void SetAlternateProtocolProbabilityThreshold(
283 double threshold
) = 0;
285 // Gets a reference to the SettingsMap stored for a host.
286 // If no settings are stored, returns an empty SettingsMap.
287 virtual const SettingsMap
& GetSpdySettings(
288 const HostPortPair
& host_port_pair
) = 0;
290 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
291 // is to be persisted.
292 virtual bool SetSpdySetting(const HostPortPair
& host_port_pair
,
294 SpdySettingsFlags flags
,
297 // Clears all SPDY settings for a host.
298 virtual void ClearSpdySettings(const HostPortPair
& host_port_pair
) = 0;
300 // Clears all SPDY settings for all hosts.
301 virtual void ClearAllSpdySettings() = 0;
303 // Returns all persistent SPDY settings.
304 virtual const SpdySettingsMap
& spdy_settings_map() const = 0;
306 virtual bool GetSupportsQuic(IPAddressNumber
* last_address
) const = 0;
308 virtual void SetSupportsQuic(bool used_quic
,
309 const IPAddressNumber
& last_address
) = 0;
311 virtual void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
312 ServerNetworkStats stats
) = 0;
314 virtual const ServerNetworkStats
* GetServerNetworkStats(
315 const HostPortPair
& host_port_pair
) = 0;
317 virtual const ServerNetworkStatsMap
& server_network_stats_map() const = 0;
320 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties
);
325 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_