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 struct NET_EXPORT AlternativeService
{
91 : protocol(UNINITIALIZED_ALTERNATE_PROTOCOL
), host(), port(0) {}
93 AlternativeService(AlternateProtocol protocol
,
94 const std::string
& host
,
96 : protocol(protocol
), host(host
), port(port
) {}
98 AlternativeService(AlternateProtocol protocol
,
99 const HostPortPair
& host_port_pair
)
100 : protocol(protocol
),
101 host(host_port_pair
.host()),
102 port(host_port_pair
.port()) {}
104 AlternativeService(const AlternativeService
& alternative_service
) = default;
105 AlternativeService
& operator=(const AlternativeService
& alternative_service
) =
108 bool operator==(const AlternativeService
& other
) const {
109 return protocol
== other
.protocol
&& host
== other
.host
&&
113 bool operator<(const AlternativeService
& other
) const {
114 if (protocol
!= other
.protocol
)
115 return protocol
< other
.protocol
;
116 if (host
!= other
.host
)
117 return host
< other
.host
;
118 return port
< other
.port
;
121 AlternateProtocol protocol
;
126 struct NET_EXPORT AlternateProtocolInfo
{
127 AlternateProtocolInfo()
128 : port(0), protocol(UNINITIALIZED_ALTERNATE_PROTOCOL
), probability(0) {}
130 AlternateProtocolInfo(uint16 port
,
131 AlternateProtocol protocol
,
133 : port(port
), protocol(protocol
), probability(probability
) {}
135 bool Equals(const AlternateProtocolInfo
& other
) const {
136 return port
== other
.port
&&
137 protocol
== other
.protocol
&&
138 probability
== other
.probability
;
141 std::string
ToString() const;
144 AlternateProtocol protocol
;
148 struct NET_EXPORT SupportsQuic
{
149 SupportsQuic() : used_quic(false) {}
150 SupportsQuic(bool used_quic
, const std::string
& address
)
151 : used_quic(used_quic
),
154 bool Equals(const SupportsQuic
& other
) const {
155 return used_quic
== other
.used_quic
&& address
== other
.address
;
162 struct NET_EXPORT ServerNetworkStats
{
163 ServerNetworkStats() : bandwidth_estimate(QuicBandwidth::Zero()) {}
165 base::TimeDelta srtt
;
166 QuicBandwidth bandwidth_estimate
;
169 typedef base::MRUCache
<
170 HostPortPair
, AlternateProtocolInfo
> AlternateProtocolMap
;
171 typedef base::MRUCache
<HostPortPair
, SettingsMap
> SpdySettingsMap
;
172 typedef base::MRUCache
<HostPortPair
, ServerNetworkStats
> ServerNetworkStatsMap
;
174 extern const char kAlternateProtocolHeader
[];
176 // The interface for setting/retrieving the HTTP server properties.
177 // Currently, this class manages servers':
178 // * SPDY support (based on NPN results)
179 // * Alternate-Protocol support
180 // * Spdy Settings (like CWND ID field)
181 class NET_EXPORT HttpServerProperties
{
183 HttpServerProperties() {}
184 virtual ~HttpServerProperties() {}
186 // Gets a weak pointer for this object.
187 virtual base::WeakPtr
<HttpServerProperties
> GetWeakPtr() = 0;
190 virtual void Clear() = 0;
192 // Returns true if |server| supports a network protocol which honors
193 // request prioritization.
194 virtual bool SupportsRequestPriority(const HostPortPair
& server
) = 0;
196 // Add |server| into the persistent store. Should only be called from IO
198 virtual void SetSupportsSpdy(const HostPortPair
& server
,
199 bool support_spdy
) = 0;
201 // Returns true if |server| has required HTTP/1.1 via HTTP/2 error code.
202 virtual bool RequiresHTTP11(const HostPortPair
& server
) = 0;
204 // Require HTTP/1.1 on subsequent connections. Not persisted.
205 virtual void SetHTTP11Required(const HostPortPair
& server
) = 0;
207 // Modify SSLConfig to force HTTP/1.1.
208 static void ForceHTTP11(SSLConfig
* ssl_config
);
210 // Modify SSLConfig to force HTTP/1.1 if necessary.
211 virtual void MaybeForceHTTP11(const HostPortPair
& server
,
212 SSLConfig
* ssl_config
) = 0;
214 // Returns the alternative service for |origin| if it has probability equal to
215 // or exceeding threshold, or else the forced AlternateProtocol if there is
216 // one, or else one with UNINITIALIZED_ALTERNATE_PROTOCOL.
217 virtual AlternativeService
GetAlternativeService(
218 const HostPortPair
& origin
) = 0;
220 // Sets the alternative service for |origin|.
221 // TODO(bnc): alternative_service.host is currently ignored, fix it.
222 virtual void SetAlternativeService(
223 const HostPortPair
& origin
,
224 const AlternativeService
& alternative_service
,
225 double alternative_probability
) = 0;
227 // Marks |alternative_service| as broken.
228 virtual void MarkAlternativeServiceBroken(
229 const AlternativeService
& alternative_service
) = 0;
231 // Marks |alternative_service| as recently broken.
232 virtual void MarkAlternativeServiceRecentlyBroken(
233 const AlternativeService
& alternative_service
) = 0;
235 // Returns true iff |alternative_service| is currently broken.
236 virtual bool IsAlternativeServiceBroken(
237 const AlternativeService
& alternative_service
) = 0;
239 // Returns true iff |alternative_service| was recently broken.
240 virtual bool WasAlternativeServiceRecentlyBroken(
241 const AlternativeService
& alternative_service
) = 0;
243 // Confirms that |alternative_service| is working.
244 virtual void ConfirmAlternativeService(
245 const AlternativeService
& alternative_service
) = 0;
247 // Clears the alternative service for |origin|.
248 virtual void ClearAlternativeService(const HostPortPair
& origin
) = 0;
250 // Returns all Alternate-Protocol mappings.
251 virtual const AlternateProtocolMap
& alternate_protocol_map() const = 0;
253 // Sets the threshold to be used when evaluating alternative service
254 // advertisments. Only advertisements with a probability greater than or equal
255 // to |threshold| will be honored. |threshold| must be between 0.0 and 1.0
256 // inclusive. Hence, a threshold of 0.0 implies that all advertisements will
258 virtual void SetAlternateProtocolProbabilityThreshold(
259 double threshold
) = 0;
261 // Gets a reference to the SettingsMap stored for a host.
262 // If no settings are stored, returns an empty SettingsMap.
263 virtual const SettingsMap
& GetSpdySettings(
264 const HostPortPair
& host_port_pair
) = 0;
266 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
267 // is to be persisted.
268 virtual bool SetSpdySetting(const HostPortPair
& host_port_pair
,
270 SpdySettingsFlags flags
,
273 // Clears all SPDY settings for a host.
274 virtual void ClearSpdySettings(const HostPortPair
& host_port_pair
) = 0;
276 // Clears all SPDY settings for all hosts.
277 virtual void ClearAllSpdySettings() = 0;
279 // Returns all persistent SPDY settings.
280 virtual const SpdySettingsMap
& spdy_settings_map() const = 0;
282 virtual bool GetSupportsQuic(IPAddressNumber
* last_address
) const = 0;
284 virtual void SetSupportsQuic(bool used_quic
,
285 const IPAddressNumber
& last_address
) = 0;
287 virtual void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
288 ServerNetworkStats stats
) = 0;
290 virtual const ServerNetworkStats
* GetServerNetworkStats(
291 const HostPortPair
& host_port_pair
) = 0;
293 virtual const ServerNetworkStatsMap
& server_network_stats_map() const = 0;
296 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties
);
301 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_