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() const { 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 bool operator==(const ServerNetworkStats
& other
) const {
191 return srtt
== other
.srtt
&& bandwidth_estimate
== other
.bandwidth_estimate
;
194 bool operator!=(const ServerNetworkStats
& other
) const {
195 return !this->operator==(other
);
198 base::TimeDelta srtt
;
199 QuicBandwidth bandwidth_estimate
;
202 typedef base::MRUCache
<HostPortPair
, AlternativeServiceInfo
>
203 AlternativeServiceMap
;
204 typedef base::MRUCache
<HostPortPair
, SettingsMap
> SpdySettingsMap
;
205 typedef base::MRUCache
<HostPortPair
, ServerNetworkStats
> ServerNetworkStatsMap
;
207 extern const char kAlternateProtocolHeader
[];
209 // The interface for setting/retrieving the HTTP server properties.
210 // Currently, this class manages servers':
211 // * SPDY support (based on NPN results)
212 // * alternative service support
213 // * Spdy Settings (like CWND ID field)
214 class NET_EXPORT HttpServerProperties
{
216 HttpServerProperties() {}
217 virtual ~HttpServerProperties() {}
219 // Gets a weak pointer for this object.
220 virtual base::WeakPtr
<HttpServerProperties
> GetWeakPtr() = 0;
223 virtual void Clear() = 0;
225 // Returns true if |server| supports a network protocol which honors
226 // request prioritization.
227 virtual bool SupportsRequestPriority(const HostPortPair
& server
) = 0;
229 // Returns the value set by SetSupportsSpdy(). If not set, returns false.
230 virtual bool GetSupportsSpdy(const HostPortPair
& server
) = 0;
232 // Add |server| into the persistent store. Should only be called from IO
234 virtual void SetSupportsSpdy(const HostPortPair
& server
,
235 bool support_spdy
) = 0;
237 // Returns true if |server| has required HTTP/1.1 via HTTP/2 error code.
238 virtual bool RequiresHTTP11(const HostPortPair
& server
) = 0;
240 // Require HTTP/1.1 on subsequent connections. Not persisted.
241 virtual void SetHTTP11Required(const HostPortPair
& server
) = 0;
243 // Modify SSLConfig to force HTTP/1.1.
244 static void ForceHTTP11(SSLConfig
* ssl_config
);
246 // Modify SSLConfig to force HTTP/1.1 if necessary.
247 virtual void MaybeForceHTTP11(const HostPortPair
& server
,
248 SSLConfig
* ssl_config
) = 0;
250 // Returns the alternative service for |origin| if it has probability equal to
251 // or exceeding threshold, or else the forced AlternateProtocol if there is
252 // one, or else one with UNINITIALIZED_ALTERNATE_PROTOCOL.
253 virtual AlternativeService
GetAlternativeService(
254 const HostPortPair
& origin
) = 0;
256 // Sets the alternative service for |origin|.
257 virtual void SetAlternativeService(
258 const HostPortPair
& origin
,
259 const AlternativeService
& alternative_service
,
260 double alternative_probability
) = 0;
262 // Marks |alternative_service| as broken.
263 virtual void MarkAlternativeServiceBroken(
264 const AlternativeService
& alternative_service
) = 0;
266 // Marks |alternative_service| as recently broken.
267 virtual void MarkAlternativeServiceRecentlyBroken(
268 const AlternativeService
& alternative_service
) = 0;
270 // Returns true iff |alternative_service| is currently broken.
271 virtual bool IsAlternativeServiceBroken(
272 const AlternativeService
& alternative_service
) const = 0;
274 // Returns true iff |alternative_service| was recently broken.
275 virtual bool WasAlternativeServiceRecentlyBroken(
276 const AlternativeService
& alternative_service
) = 0;
278 // Confirms that |alternative_service| is working.
279 virtual void ConfirmAlternativeService(
280 const AlternativeService
& alternative_service
) = 0;
282 // Clears the alternative service for |origin|.
283 virtual void ClearAlternativeService(const HostPortPair
& origin
) = 0;
285 // Returns all alternative service mappings.
286 virtual const AlternativeServiceMap
& alternative_service_map() const = 0;
288 // Returns all alternative service mappings as human readable strings.
289 virtual scoped_ptr
<base::Value
> GetAlternativeServiceInfoAsValue() const = 0;
291 // Sets the threshold to be used when evaluating alternative service
292 // advertisments. Only advertisements with a probability greater than or equal
293 // to |threshold| will be honored. |threshold| must be between 0.0 and 1.0
294 // inclusive. Hence, a threshold of 0.0 implies that all advertisements will
296 virtual void SetAlternativeServiceProbabilityThreshold(double threshold
) = 0;
298 // Gets a reference to the SettingsMap stored for a host.
299 // If no settings are stored, returns an empty SettingsMap.
300 virtual const SettingsMap
& GetSpdySettings(
301 const HostPortPair
& host_port_pair
) = 0;
303 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
304 // is to be persisted.
305 virtual bool SetSpdySetting(const HostPortPair
& host_port_pair
,
307 SpdySettingsFlags flags
,
310 // Clears all SPDY settings for a host.
311 virtual void ClearSpdySettings(const HostPortPair
& host_port_pair
) = 0;
313 // Clears all SPDY settings for all hosts.
314 virtual void ClearAllSpdySettings() = 0;
316 // Returns all persistent SPDY settings.
317 virtual const SpdySettingsMap
& spdy_settings_map() const = 0;
319 virtual bool GetSupportsQuic(IPAddressNumber
* last_address
) const = 0;
321 virtual void SetSupportsQuic(bool used_quic
,
322 const IPAddressNumber
& last_address
) = 0;
324 // Sets |stats| for |host_port_pair|.
325 virtual void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
326 ServerNetworkStats stats
) = 0;
328 virtual const ServerNetworkStats
* GetServerNetworkStats(
329 const HostPortPair
& host_port_pair
) = 0;
331 virtual const ServerNetworkStatsMap
& server_network_stats_map() const = 0;
334 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties
);
339 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_