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
,
71 NPN_SPDY_MAXIMUM_VERSION
= NPN_HTTP_2
,
73 ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
= QUIC
,
74 UNINITIALIZED_ALTERNATE_PROTOCOL
,
77 // Simply returns whether |protocol| is between
78 // ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION and
79 // ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION (inclusive).
80 NET_EXPORT
bool IsAlternateProtocolValid(AlternateProtocol protocol
);
82 enum AlternateProtocolSize
{
83 NUM_VALID_ALTERNATE_PROTOCOLS
=
84 ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
-
85 ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
+ 1,
88 NET_EXPORT
const char* AlternateProtocolToString(AlternateProtocol protocol
);
89 NET_EXPORT AlternateProtocol
AlternateProtocolFromString(
90 const std::string
& str
);
91 NET_EXPORT_PRIVATE AlternateProtocol
AlternateProtocolFromNextProto(
92 NextProto next_proto
);
94 // (protocol, host, port) triple as defined in
95 // https://tools.ietf.org/id/draft-ietf-httpbis-alt-svc-06.html
96 struct NET_EXPORT AlternativeService
{
98 : protocol(UNINITIALIZED_ALTERNATE_PROTOCOL
), host(), port(0) {}
100 AlternativeService(AlternateProtocol protocol
,
101 const std::string
& host
,
103 : protocol(protocol
), host(host
), port(port
) {}
105 AlternativeService(AlternateProtocol protocol
,
106 const HostPortPair
& host_port_pair
)
107 : protocol(protocol
),
108 host(host_port_pair
.host()),
109 port(host_port_pair
.port()) {}
111 AlternativeService(const AlternativeService
& alternative_service
) = default;
112 AlternativeService
& operator=(const AlternativeService
& alternative_service
) =
115 HostPortPair
host_port_pair() const { return HostPortPair(host
, port
); }
117 bool operator==(const AlternativeService
& other
) const {
118 return protocol
== other
.protocol
&& host
== other
.host
&&
122 bool operator!=(const AlternativeService
& other
) const {
123 return !this->operator==(other
);
126 bool operator<(const AlternativeService
& other
) const {
127 if (protocol
!= other
.protocol
)
128 return protocol
< other
.protocol
;
129 if (host
!= other
.host
)
130 return host
< other
.host
;
131 return port
< other
.port
;
134 std::string
ToString() const;
136 AlternateProtocol protocol
;
141 struct NET_EXPORT AlternativeServiceInfo
{
142 AlternativeServiceInfo() : alternative_service(), probability(0.0) {}
144 AlternativeServiceInfo(const AlternativeService
& alternative_service
,
146 base::Time expiration
)
147 : alternative_service(alternative_service
),
148 probability(probability
),
149 expiration(expiration
) {}
151 AlternativeServiceInfo(AlternateProtocol protocol
,
152 const std::string
& host
,
155 base::Time expiration
)
156 : alternative_service(protocol
, host
, port
),
157 probability(probability
),
158 expiration(expiration
) {}
160 AlternativeServiceInfo(
161 const AlternativeServiceInfo
& alternative_service_info
) = default;
162 AlternativeServiceInfo
& operator=(
163 const AlternativeServiceInfo
& alternative_service_info
) = default;
165 bool operator==(const AlternativeServiceInfo
& other
) const {
166 return alternative_service
== other
.alternative_service
&&
167 probability
== other
.probability
&& expiration
== other
.expiration
;
170 bool operator!=(const AlternativeServiceInfo
& other
) const {
171 return !this->operator==(other
);
174 std::string
ToString() const;
176 AlternativeService alternative_service
;
178 base::Time expiration
;
181 struct NET_EXPORT SupportsQuic
{
182 SupportsQuic() : used_quic(false) {}
183 SupportsQuic(bool used_quic
, const std::string
& address
)
184 : used_quic(used_quic
),
187 bool Equals(const SupportsQuic
& other
) const {
188 return used_quic
== other
.used_quic
&& address
== other
.address
;
195 struct NET_EXPORT ServerNetworkStats
{
196 ServerNetworkStats() : bandwidth_estimate(QuicBandwidth::Zero()) {}
198 bool operator==(const ServerNetworkStats
& other
) const {
199 return srtt
== other
.srtt
&& bandwidth_estimate
== other
.bandwidth_estimate
;
202 bool operator!=(const ServerNetworkStats
& other
) const {
203 return !this->operator==(other
);
206 base::TimeDelta srtt
;
207 QuicBandwidth bandwidth_estimate
;
210 typedef std::vector
<AlternativeService
> AlternativeServiceVector
;
211 typedef std::vector
<AlternativeServiceInfo
> AlternativeServiceInfoVector
;
212 typedef base::MRUCache
<HostPortPair
, AlternativeServiceInfoVector
>
213 AlternativeServiceMap
;
214 typedef base::MRUCache
<HostPortPair
, SettingsMap
> SpdySettingsMap
;
215 typedef base::MRUCache
<HostPortPair
, ServerNetworkStats
> ServerNetworkStatsMap
;
217 extern const char kAlternateProtocolHeader
[];
218 extern const char kAlternativeServiceHeader
[];
220 // The interface for setting/retrieving the HTTP server properties.
221 // Currently, this class manages servers':
222 // * SPDY support (based on NPN results)
223 // * alternative service support
224 // * Spdy Settings (like CWND ID field)
225 class NET_EXPORT HttpServerProperties
{
227 HttpServerProperties() {}
228 virtual ~HttpServerProperties() {}
230 // Gets a weak pointer for this object.
231 virtual base::WeakPtr
<HttpServerProperties
> GetWeakPtr() = 0;
234 virtual void Clear() = 0;
236 // Returns true if |server| supports a network protocol which honors
237 // request prioritization.
238 virtual bool SupportsRequestPriority(const HostPortPair
& server
) = 0;
240 // Returns the value set by SetSupportsSpdy(). If not set, returns false.
241 virtual bool GetSupportsSpdy(const HostPortPair
& server
) = 0;
243 // Add |server| into the persistent store. Should only be called from IO
245 virtual void SetSupportsSpdy(const HostPortPair
& server
,
246 bool support_spdy
) = 0;
248 // Returns true if |server| has required HTTP/1.1 via HTTP/2 error code.
249 virtual bool RequiresHTTP11(const HostPortPair
& server
) = 0;
251 // Require HTTP/1.1 on subsequent connections. Not persisted.
252 virtual void SetHTTP11Required(const HostPortPair
& server
) = 0;
254 // Modify SSLConfig to force HTTP/1.1.
255 static void ForceHTTP11(SSLConfig
* ssl_config
);
257 // Modify SSLConfig to force HTTP/1.1 if necessary.
258 virtual void MaybeForceHTTP11(const HostPortPair
& server
,
259 SSLConfig
* ssl_config
) = 0;
261 // Return all alternative services for |origin| with probability greater than
262 // or equal to the threshold, including broken ones.
263 // Returned alternative services never have empty hostnames.
264 virtual AlternativeServiceVector
GetAlternativeServices(
265 const HostPortPair
& origin
) = 0;
267 // Set a single alternative service for |origin|. Previous alternative
268 // services for |origin| are discarded.
269 // |alternative_service.host| may be empty.
270 // Return true if |alternative_service_map_| is changed.
271 virtual bool SetAlternativeService(
272 const HostPortPair
& origin
,
273 const AlternativeService
& alternative_service
,
274 double alternative_probability
,
275 base::Time expiration
) = 0;
277 // Set alternative services for |origin|. Previous alternative services for
278 // |origin| are discarded.
279 // Hostnames in |alternative_service_info_vector| may be empty.
280 // Return true if |alternative_service_map_| is changed.
281 virtual bool SetAlternativeServices(
282 const HostPortPair
& origin
,
283 const AlternativeServiceInfoVector
& alternative_service_info_vector
) = 0;
285 // Marks |alternative_service| as broken.
286 // |alternative_service.host| must not be empty.
287 virtual void MarkAlternativeServiceBroken(
288 const AlternativeService
& alternative_service
) = 0;
290 // Marks |alternative_service| as recently broken.
291 // |alternative_service.host| must not be empty.
292 virtual void MarkAlternativeServiceRecentlyBroken(
293 const AlternativeService
& alternative_service
) = 0;
295 // Returns true iff |alternative_service| is currently broken.
296 // |alternative_service.host| must not be empty.
297 virtual bool IsAlternativeServiceBroken(
298 const AlternativeService
& alternative_service
) const = 0;
300 // Returns true iff |alternative_service| was recently broken.
301 // |alternative_service.host| must not be empty.
302 virtual bool WasAlternativeServiceRecentlyBroken(
303 const AlternativeService
& alternative_service
) = 0;
305 // Confirms that |alternative_service| is working.
306 // |alternative_service.host| must not be empty.
307 virtual void ConfirmAlternativeService(
308 const AlternativeService
& alternative_service
) = 0;
310 // Clear all alternative services for |origin|.
311 virtual void ClearAlternativeServices(const HostPortPair
& origin
) = 0;
313 // Returns all alternative service mappings.
314 // Returned alternative services may have empty hostnames.
315 virtual const AlternativeServiceMap
& alternative_service_map() const = 0;
317 // Returns all alternative service mappings as human readable strings.
318 // Empty alternative service hostnames will be printed as such.
319 virtual scoped_ptr
<base::Value
> GetAlternativeServiceInfoAsValue() const = 0;
321 // Sets the threshold to be used when evaluating alternative service
322 // advertisments. Only advertisements with a probability greater than or equal
323 // to |threshold| will be honored. |threshold| must be between 0.0 and 1.0
324 // inclusive. Hence, a threshold of 0.0 implies that all advertisements will
326 virtual void SetAlternativeServiceProbabilityThreshold(double threshold
) = 0;
328 // Gets a reference to the SettingsMap stored for a host.
329 // If no settings are stored, returns an empty SettingsMap.
330 virtual const SettingsMap
& GetSpdySettings(
331 const HostPortPair
& host_port_pair
) = 0;
333 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
334 // is to be persisted.
335 virtual bool SetSpdySetting(const HostPortPair
& host_port_pair
,
337 SpdySettingsFlags flags
,
340 // Clears all SPDY settings for a host.
341 virtual void ClearSpdySettings(const HostPortPair
& host_port_pair
) = 0;
343 // Clears all SPDY settings for all hosts.
344 virtual void ClearAllSpdySettings() = 0;
346 // Returns all persistent SPDY settings.
347 virtual const SpdySettingsMap
& spdy_settings_map() const = 0;
349 virtual bool GetSupportsQuic(IPAddressNumber
* last_address
) const = 0;
351 virtual void SetSupportsQuic(bool used_quic
,
352 const IPAddressNumber
& last_address
) = 0;
354 // Sets |stats| for |host_port_pair|.
355 virtual void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
356 ServerNetworkStats stats
) = 0;
358 virtual const ServerNetworkStats
* GetServerNetworkStats(
359 const HostPortPair
& host_port_pair
) = 0;
361 virtual const ServerNetworkStatsMap
& server_network_stats_map() const = 0;
364 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties
);
369 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_