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
66 NPN_SPDY_MAXIMUM_VERSION
= NPN_SPDY_4
,
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 // (protocol, host, port) triple as defined in
90 // https://tools.ietf.org/id/draft-ietf-httpbis-alt-svc-06.html
91 struct NET_EXPORT AlternativeService
{
93 : protocol(UNINITIALIZED_ALTERNATE_PROTOCOL
), host(), port(0) {}
95 AlternativeService(AlternateProtocol protocol
,
96 const std::string
& host
,
98 : protocol(protocol
), host(host
), port(port
) {}
100 AlternativeService(AlternateProtocol protocol
,
101 const HostPortPair
& host_port_pair
)
102 : protocol(protocol
),
103 host(host_port_pair
.host()),
104 port(host_port_pair
.port()) {}
106 AlternativeService(const AlternativeService
& alternative_service
) = default;
107 AlternativeService
& operator=(const AlternativeService
& alternative_service
) =
110 HostPortPair
host_port_pair() { return HostPortPair(host
, port
); }
112 bool operator==(const AlternativeService
& other
) const {
113 return protocol
== other
.protocol
&& host
== other
.host
&&
117 bool operator<(const AlternativeService
& other
) const {
118 if (protocol
!= other
.protocol
)
119 return protocol
< other
.protocol
;
120 if (host
!= other
.host
)
121 return host
< other
.host
;
122 return port
< other
.port
;
125 std::string
ToString() const;
127 AlternateProtocol protocol
;
132 struct NET_EXPORT AlternativeServiceInfo
{
133 AlternativeServiceInfo() : alternative_service(), probability(0.0) {}
135 AlternativeServiceInfo(const AlternativeService
& alternative_service
,
137 : alternative_service(alternative_service
), probability(probability
) {}
139 AlternativeServiceInfo(AlternateProtocol protocol
,
140 const std::string
& host
,
143 : alternative_service(protocol
, host
, port
), probability(probability
) {}
145 AlternativeServiceInfo(
146 const AlternativeServiceInfo
& alternative_service_info
) = default;
147 AlternativeServiceInfo
& operator=(
148 const AlternativeServiceInfo
& alternative_service_info
) = default;
150 bool operator==(const AlternativeServiceInfo
& other
) const {
151 return alternative_service
== other
.alternative_service
&&
152 probability
== other
.probability
;
155 bool operator!=(const AlternativeServiceInfo
& other
) const {
156 return !this->operator==(other
);
159 std::string
ToString() const;
161 AlternativeService alternative_service
;
165 struct NET_EXPORT SupportsQuic
{
166 SupportsQuic() : used_quic(false) {}
167 SupportsQuic(bool used_quic
, const std::string
& address
)
168 : used_quic(used_quic
),
171 bool Equals(const SupportsQuic
& other
) const {
172 return used_quic
== other
.used_quic
&& address
== other
.address
;
179 struct NET_EXPORT ServerNetworkStats
{
180 ServerNetworkStats() : bandwidth_estimate(QuicBandwidth::Zero()) {}
182 base::TimeDelta srtt
;
183 QuicBandwidth bandwidth_estimate
;
186 typedef base::MRUCache
<HostPortPair
, AlternativeServiceInfo
>
187 AlternativeServiceMap
;
188 typedef base::MRUCache
<HostPortPair
, SettingsMap
> SpdySettingsMap
;
189 typedef base::MRUCache
<HostPortPair
, ServerNetworkStats
> ServerNetworkStatsMap
;
191 extern const char kAlternateProtocolHeader
[];
193 // The interface for setting/retrieving the HTTP server properties.
194 // Currently, this class manages servers':
195 // * SPDY support (based on NPN results)
196 // * alternative service support
197 // * Spdy Settings (like CWND ID field)
198 class NET_EXPORT HttpServerProperties
{
200 HttpServerProperties() {}
201 virtual ~HttpServerProperties() {}
203 // Gets a weak pointer for this object.
204 virtual base::WeakPtr
<HttpServerProperties
> GetWeakPtr() = 0;
207 virtual void Clear() = 0;
209 // Returns true if |server| supports a network protocol which honors
210 // request prioritization.
211 virtual bool SupportsRequestPriority(const HostPortPair
& server
) = 0;
213 // Add |server| into the persistent store. Should only be called from IO
215 virtual void SetSupportsSpdy(const HostPortPair
& server
,
216 bool support_spdy
) = 0;
218 // Returns true if |server| has required HTTP/1.1 via HTTP/2 error code.
219 virtual bool RequiresHTTP11(const HostPortPair
& server
) = 0;
221 // Require HTTP/1.1 on subsequent connections. Not persisted.
222 virtual void SetHTTP11Required(const HostPortPair
& server
) = 0;
224 // Modify SSLConfig to force HTTP/1.1.
225 static void ForceHTTP11(SSLConfig
* ssl_config
);
227 // Modify SSLConfig to force HTTP/1.1 if necessary.
228 virtual void MaybeForceHTTP11(const HostPortPair
& server
,
229 SSLConfig
* ssl_config
) = 0;
231 // Returns the alternative service for |origin| if it has probability equal to
232 // or exceeding threshold, or else the forced AlternateProtocol if there is
233 // one, or else one with UNINITIALIZED_ALTERNATE_PROTOCOL.
234 virtual AlternativeService
GetAlternativeService(
235 const HostPortPair
& origin
) = 0;
237 // Sets the alternative service for |origin|.
238 // TODO(bnc): alternative_service.host is currently ignored, fix it.
239 virtual void SetAlternativeService(
240 const HostPortPair
& origin
,
241 const AlternativeService
& alternative_service
,
242 double alternative_probability
) = 0;
244 // Marks |alternative_service| as broken.
245 virtual void MarkAlternativeServiceBroken(
246 const AlternativeService
& alternative_service
) = 0;
248 // Marks |alternative_service| as recently broken.
249 virtual void MarkAlternativeServiceRecentlyBroken(
250 const AlternativeService
& alternative_service
) = 0;
252 // Returns true iff |alternative_service| is currently broken.
253 virtual bool IsAlternativeServiceBroken(
254 const AlternativeService
& alternative_service
) = 0;
256 // Returns true iff |alternative_service| was recently broken.
257 virtual bool WasAlternativeServiceRecentlyBroken(
258 const AlternativeService
& alternative_service
) = 0;
260 // Confirms that |alternative_service| is working.
261 virtual void ConfirmAlternativeService(
262 const AlternativeService
& alternative_service
) = 0;
264 // Clears the alternative service for |origin|.
265 virtual void ClearAlternativeService(const HostPortPair
& origin
) = 0;
267 // Returns all alternative service mappings.
268 virtual const AlternativeServiceMap
& alternative_service_map() const = 0;
270 // Sets the threshold to be used when evaluating alternative service
271 // advertisments. Only advertisements with a probability greater than or equal
272 // to |threshold| will be honored. |threshold| must be between 0.0 and 1.0
273 // inclusive. Hence, a threshold of 0.0 implies that all advertisements will
275 virtual void SetAlternateProtocolProbabilityThreshold(
276 double threshold
) = 0;
278 // Gets a reference to the SettingsMap stored for a host.
279 // If no settings are stored, returns an empty SettingsMap.
280 virtual const SettingsMap
& GetSpdySettings(
281 const HostPortPair
& host_port_pair
) = 0;
283 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
284 // is to be persisted.
285 virtual bool SetSpdySetting(const HostPortPair
& host_port_pair
,
287 SpdySettingsFlags flags
,
290 // Clears all SPDY settings for a host.
291 virtual void ClearSpdySettings(const HostPortPair
& host_port_pair
) = 0;
293 // Clears all SPDY settings for all hosts.
294 virtual void ClearAllSpdySettings() = 0;
296 // Returns all persistent SPDY settings.
297 virtual const SpdySettingsMap
& spdy_settings_map() const = 0;
299 virtual bool GetSupportsQuic(IPAddressNumber
* last_address
) const = 0;
301 virtual void SetSupportsQuic(bool used_quic
,
302 const IPAddressNumber
& last_address
) = 0;
304 virtual void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
305 ServerNetworkStats stats
) = 0;
307 virtual const ServerNetworkStats
* GetServerNetworkStats(
308 const HostPortPair
& host_port_pair
) = 0;
310 virtual const ServerNetworkStatsMap
& server_network_stats_map() const = 0;
313 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties
);
318 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_