blob: 983828aa8092fc8a4188f202d343f91cadf82579 [file] [log] [blame]
[email protected]39c48fc2012-03-12 18:42:121// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]db96a882011-10-09 02:01:542// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_HTTP_HTTP_SERVER_PROPERTIES_H_
6#define NET_HTTP_HTTP_SERVER_PROPERTIES_H_
7
[email protected]17291a022011-10-10 07:32:538#include <map>
[email protected]db96a882011-10-09 02:01:549#include <string>
jsbellcea42a52015-11-30 23:50:2510#include <tuple>
bncd9b132e2015-07-08 05:16:1011#include <vector>
12
[email protected]db96a882011-10-09 02:01:5413#include "base/basictypes.h"
[email protected]9801e3702014-03-07 09:33:5514#include "base/containers/mru_cache.h"
[email protected]30d4c022013-07-18 22:58:1615#include "base/memory/weak_ptr.h"
[email protected]3b8cf7f2014-01-27 22:08:5116#include "base/time/time.h"
[email protected]db96a882011-10-09 02:01:5417#include "net/base/host_port_pair.h"
tfarina73144d12015-10-13 00:29:5818#include "net/base/ip_address_number.h"
[email protected]db96a882011-10-09 02:01:5419#include "net/base/net_export.h"
pkastinga9f22d4e2014-12-01 22:43:4620#include "net/quic/quic_bandwidth.h"
rtenneti8b673f72015-10-08 23:45:3721#include "net/quic/quic_server_id.h"
[email protected]0ce3af82013-07-22 16:17:1622#include "net/socket/next_proto.h"
[email protected]53bfa31c2011-11-15 19:20:3123#include "net/spdy/spdy_framer.h" // TODO(willchan): Reconsider this.
bnc1fd91332014-10-28 20:30:0024#include "net/spdy/spdy_protocol.h"
[email protected]db96a882011-10-09 02:01:5425
bnc8ba74a1782015-04-14 17:42:0826namespace base {
27class Value;
28}
29
[email protected]db96a882011-10-09 02:01:5430namespace net {
31
bncfacdd852015-01-09 19:22:5432struct SSLConfig;
33
[email protected]db9bfea2014-04-11 18:51:3934enum AlternateProtocolUsage {
35 // Alternate Protocol was used without racing a normal connection.
36 ALTERNATE_PROTOCOL_USAGE_NO_RACE = 0,
37 // Alternate Protocol was used by winning a race with a normal connection.
38 ALTERNATE_PROTOCOL_USAGE_WON_RACE = 1,
39 // Alternate Protocol was not used by losing a race with a normal connection.
40 ALTERNATE_PROTOCOL_USAGE_LOST_RACE = 2,
41 // Alternate Protocol was not used because no Alternate-Protocol information
42 // was available when the request was issued, but an Alternate-Protocol header
43 // was present in the response.
44 ALTERNATE_PROTOCOL_USAGE_MAPPING_MISSING = 3,
45 // Alternate Protocol was not used because it was marked broken.
46 ALTERNATE_PROTOCOL_USAGE_BROKEN = 4,
47 // Maximum value for the enum.
48 ALTERNATE_PROTOCOL_USAGE_MAX,
49};
50
rtenneti7a6c3e82014-10-24 07:26:1851// Log a histogram to reflect |usage|.
52NET_EXPORT void HistogramAlternateProtocolUsage(AlternateProtocolUsage usage);
[email protected]db9bfea2014-04-11 18:51:3953
[email protected]7b33037f2014-04-11 23:00:2854enum BrokenAlternateProtocolLocation {
55 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB = 0,
56 BROKEN_ALTERNATE_PROTOCOL_LOCATION_QUIC_STREAM_FACTORY = 1,
[email protected]00c159f2014-05-21 22:38:1657 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB_ALT = 2,
58 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB_MAIN = 3,
[email protected]7b33037f2014-04-11 23:00:2859 BROKEN_ALTERNATE_PROTOCOL_LOCATION_MAX,
60};
61
62// Log a histogram to reflect |location|.
63NET_EXPORT void HistogramBrokenAlternateProtocolLocation(
64 BrokenAlternateProtocolLocation location);
65
[email protected]17291a022011-10-10 07:32:5366enum AlternateProtocol {
[email protected]b05bcaa32013-10-06 05:26:0267 DEPRECATED_NPN_SPDY_2 = 0,
68 ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION = DEPRECATED_NPN_SPDY_2,
69 NPN_SPDY_MINIMUM_VERSION = DEPRECATED_NPN_SPDY_2,
[email protected]39c48fc2012-03-12 18:42:1270 NPN_SPDY_3,
[email protected]63bf9662013-03-05 20:46:0171 NPN_SPDY_3_1,
bnc3f946d62015-07-08 21:22:0372 NPN_HTTP_2, // HTTP/2
73 NPN_SPDY_MAXIMUM_VERSION = NPN_HTTP_2,
[email protected]7db54b82013-04-01 21:53:4574 QUIC,
[email protected]13621d62013-10-04 18:39:2175 ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION = QUIC,
[email protected]17291a022011-10-10 07:32:5376 UNINITIALIZED_ALTERNATE_PROTOCOL,
77};
78
[email protected]13621d62013-10-04 18:39:2179// Simply returns whether |protocol| is between
80// ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION and
81// ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION (inclusive).
82NET_EXPORT bool IsAlternateProtocolValid(AlternateProtocol protocol);
83
84enum AlternateProtocolSize {
85 NUM_VALID_ALTERNATE_PROTOCOLS =
86 ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION -
87 ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION + 1,
88};
89
[email protected]04af25f62012-04-10 19:11:3990NET_EXPORT const char* AlternateProtocolToString(AlternateProtocol protocol);
91NET_EXPORT AlternateProtocol AlternateProtocolFromString(
[email protected]13621d62013-10-04 18:39:2192 const std::string& str);
[email protected]0ce3af82013-07-22 16:17:1693NET_EXPORT_PRIVATE AlternateProtocol AlternateProtocolFromNextProto(
94 NextProto next_proto);
[email protected]04af25f62012-04-10 19:11:3995
bnc4988e432015-03-31 03:06:2596// (protocol, host, port) triple as defined in
97// https://tools.ietf.org/id/draft-ietf-httpbis-alt-svc-06.html
bnc44387762015-03-09 18:00:2098struct NET_EXPORT AlternativeService {
99 AlternativeService()
100 : protocol(UNINITIALIZED_ALTERNATE_PROTOCOL), host(), port(0) {}
101
102 AlternativeService(AlternateProtocol protocol,
103 const std::string& host,
104 uint16 port)
105 : protocol(protocol), host(host), port(port) {}
106
bnccacc0992015-03-20 20:22:22107 AlternativeService(AlternateProtocol protocol,
108 const HostPortPair& host_port_pair)
109 : protocol(protocol),
110 host(host_port_pair.host()),
111 port(host_port_pair.port()) {}
112
bnc44387762015-03-09 18:00:20113 AlternativeService(const AlternativeService& alternative_service) = default;
114 AlternativeService& operator=(const AlternativeService& alternative_service) =
115 default;
116
bnc6298e992015-06-04 12:17:40117 HostPortPair host_port_pair() const { return HostPortPair(host, port); }
bnc62a44f022015-04-02 15:59:41118
bnc127681f2015-03-09 23:23:49119 bool operator==(const AlternativeService& other) const {
120 return protocol == other.protocol && host == other.host &&
121 port == other.port;
122 }
123
rtenneti56dea0b2015-06-04 01:01:08124 bool operator!=(const AlternativeService& other) const {
125 return !this->operator==(other);
126 }
127
bnc44387762015-03-09 18:00:20128 bool operator<(const AlternativeService& other) const {
jsbellcea42a52015-11-30 23:50:25129 return std::tie(protocol, host, port) <
130 std::tie(other.protocol, other.host, other.port);
bnc44387762015-03-09 18:00:20131 }
132
bnc4988e432015-03-31 03:06:25133 std::string ToString() const;
134
bnc44387762015-03-09 18:00:20135 AlternateProtocol protocol;
136 std::string host;
137 uint16 port;
138};
139
bnc4988e432015-03-31 03:06:25140struct NET_EXPORT AlternativeServiceInfo {
141 AlternativeServiceInfo() : alternative_service(), probability(0.0) {}
bnc97aff32a2015-02-02 20:17:03142
bnc4988e432015-03-31 03:06:25143 AlternativeServiceInfo(const AlternativeService& alternative_service,
bnc7dc7e1b42015-07-28 14:43:12144 double probability,
145 base::Time expiration)
146 : alternative_service(alternative_service),
147 probability(probability),
148 expiration(expiration) {}
[email protected]287d9412014-07-08 23:01:00149
bnc4988e432015-03-31 03:06:25150 AlternativeServiceInfo(AlternateProtocol protocol,
151 const std::string& host,
152 uint16 port,
bnc7dc7e1b42015-07-28 14:43:12153 double probability,
154 base::Time expiration)
155 : alternative_service(protocol, host, port),
156 probability(probability),
157 expiration(expiration) {}
bnc4988e432015-03-31 03:06:25158
159 AlternativeServiceInfo(
160 const AlternativeServiceInfo& alternative_service_info) = default;
161 AlternativeServiceInfo& operator=(
162 const AlternativeServiceInfo& alternative_service_info) = default;
163
164 bool operator==(const AlternativeServiceInfo& other) const {
165 return alternative_service == other.alternative_service &&
bnc7dc7e1b42015-07-28 14:43:12166 probability == other.probability && expiration == other.expiration;
bnc4988e432015-03-31 03:06:25167 }
168
169 bool operator!=(const AlternativeServiceInfo& other) const {
170 return !this->operator==(other);
[email protected]17291a022011-10-10 07:32:53171 }
172
173 std::string ToString() const;
174
bnc4988e432015-03-31 03:06:25175 AlternativeService alternative_service;
[email protected]287d9412014-07-08 23:01:00176 double probability;
bnc7dc7e1b42015-07-28 14:43:12177 base::Time expiration;
[email protected]17291a022011-10-10 07:32:53178};
179
rtenneti1c863aa2014-09-25 18:39:33180struct NET_EXPORT SupportsQuic {
181 SupportsQuic() : used_quic(false) {}
182 SupportsQuic(bool used_quic, const std::string& address)
183 : used_quic(used_quic),
184 address(address) {}
185
186 bool Equals(const SupportsQuic& other) const {
187 return used_quic == other.used_quic && address == other.address;
188 }
189
190 bool used_quic;
191 std::string address;
192};
193
rtenneti338cd36a2015-01-06 00:20:07194struct NET_EXPORT ServerNetworkStats {
195 ServerNetworkStats() : bandwidth_estimate(QuicBandwidth::Zero()) {}
196
rtenneticce34d52015-06-05 23:36:29197 bool operator==(const ServerNetworkStats& other) const {
198 return srtt == other.srtt && bandwidth_estimate == other.bandwidth_estimate;
199 }
200
201 bool operator!=(const ServerNetworkStats& other) const {
202 return !this->operator==(other);
203 }
204
rtenneti338cd36a2015-01-06 00:20:07205 base::TimeDelta srtt;
206 QuicBandwidth bandwidth_estimate;
207};
208
bncd9b132e2015-07-08 05:16:10209typedef std::vector<AlternativeService> AlternativeServiceVector;
210typedef std::vector<AlternativeServiceInfo> AlternativeServiceInfoVector;
211typedef base::MRUCache<HostPortPair, AlternativeServiceInfoVector>
bnc4988e432015-03-31 03:06:25212 AlternativeServiceMap;
[email protected]1741c942014-03-18 07:33:39213typedef base::MRUCache<HostPortPair, SettingsMap> SpdySettingsMap;
rtenneti338cd36a2015-01-06 00:20:07214typedef base::MRUCache<HostPortPair, ServerNetworkStats> ServerNetworkStatsMap;
rtenneti8b673f72015-10-08 23:45:37215typedef base::MRUCache<QuicServerId, std::string> QuicServerInfoMap;
216
217// Persist 5 QUIC Servers. This is mainly used by cronet.
218const int kMaxQuicServersToPersist = 5;
[email protected]17291a022011-10-10 07:32:53219
220extern const char kAlternateProtocolHeader[];
bncc958faa2015-07-31 18:14:52221extern const char kAlternativeServiceHeader[];
[email protected]17291a022011-10-10 07:32:53222
[email protected]db96a882011-10-09 02:01:54223// The interface for setting/retrieving the HTTP server properties.
[email protected]17291a022011-10-10 07:32:53224// Currently, this class manages servers':
rtenneticd2aaa15b2015-10-10 20:29:33225// * SPDY support (based on NPN results).
226// * alternative service support.
227// * SPDY Settings (like CWND ID field).
228// * QUIC data (like ServerNetworkStats and QuicServerInfo).
229//
230// Embedders must ensure that HttpServerProperites is completely initialized
231// before the first request is issued.
[email protected]db96a882011-10-09 02:01:54232class NET_EXPORT HttpServerProperties {
233 public:
[email protected]41c3696d2012-04-08 15:22:31234 HttpServerProperties() {}
235 virtual ~HttpServerProperties() {}
[email protected]db96a882011-10-09 02:01:54236
[email protected]30d4c022013-07-18 22:58:16237 // Gets a weak pointer for this object.
238 virtual base::WeakPtr<HttpServerProperties> GetWeakPtr() = 0;
239
[email protected]17291a022011-10-10 07:32:53240 // Deletes all data.
[email protected]de95f922011-10-12 07:05:18241 virtual void Clear() = 0;
[email protected]17291a022011-10-10 07:32:53242
rch9c3e3382015-01-30 23:57:32243 // Returns true if |server| supports a network protocol which honors
244 // request prioritization.
245 virtual bool SupportsRequestPriority(const HostPortPair& server) = 0;
[email protected]db96a882011-10-09 02:01:54246
rtennetie267d6a2015-06-05 21:55:23247 // Returns the value set by SetSupportsSpdy(). If not set, returns false.
248 virtual bool GetSupportsSpdy(const HostPortPair& server) = 0;
249
[email protected]db96a882011-10-09 02:01:54250 // Add |server| into the persistent store. Should only be called from IO
251 // thread.
252 virtual void SetSupportsSpdy(const HostPortPair& server,
253 bool support_spdy) = 0;
254
bncfacdd852015-01-09 19:22:54255 // Returns true if |server| has required HTTP/1.1 via HTTP/2 error code.
256 virtual bool RequiresHTTP11(const HostPortPair& server) = 0;
257
258 // Require HTTP/1.1 on subsequent connections. Not persisted.
259 virtual void SetHTTP11Required(const HostPortPair& server) = 0;
260
261 // Modify SSLConfig to force HTTP/1.1.
262 static void ForceHTTP11(SSLConfig* ssl_config);
263
264 // Modify SSLConfig to force HTTP/1.1 if necessary.
265 virtual void MaybeForceHTTP11(const HostPortPair& server,
266 SSLConfig* ssl_config) = 0;
267
bncd9b132e2015-07-08 05:16:10268 // Return all alternative services for |origin| with probability greater than
269 // or equal to the threshold, including broken ones.
270 // Returned alternative services never have empty hostnames.
271 virtual AlternativeServiceVector GetAlternativeServices(
bnc181b39a2015-03-17 21:36:47272 const HostPortPair& origin) = 0;
[email protected]17291a022011-10-10 07:32:53273
bncd9b132e2015-07-08 05:16:10274 // Set a single alternative service for |origin|. Previous alternative
275 // services for |origin| are discarded.
276 // |alternative_service.host| may be empty.
277 // Return true if |alternative_service_map_| is changed.
278 virtual bool SetAlternativeService(
bnccacc0992015-03-20 20:22:22279 const HostPortPair& origin,
280 const AlternativeService& alternative_service,
bnc7dc7e1b42015-07-28 14:43:12281 double alternative_probability,
282 base::Time expiration) = 0;
[email protected]17291a022011-10-10 07:32:53283
bncd9b132e2015-07-08 05:16:10284 // Set alternative services for |origin|. Previous alternative services for
285 // |origin| are discarded.
286 // Hostnames in |alternative_service_info_vector| may be empty.
287 // Return true if |alternative_service_map_| is changed.
288 virtual bool SetAlternativeServices(
289 const HostPortPair& origin,
290 const AlternativeServiceInfoVector& alternative_service_info_vector) = 0;
291
bnccacc0992015-03-20 20:22:22292 // Marks |alternative_service| as broken.
bncd9b132e2015-07-08 05:16:10293 // |alternative_service.host| must not be empty.
bnccacc0992015-03-20 20:22:22294 virtual void MarkAlternativeServiceBroken(
295 const AlternativeService& alternative_service) = 0;
[email protected]17291a022011-10-10 07:32:53296
bncd1e0aa22015-03-13 10:14:31297 // Marks |alternative_service| as recently broken.
bncd9b132e2015-07-08 05:16:10298 // |alternative_service.host| must not be empty.
bncd1e0aa22015-03-13 10:14:31299 virtual void MarkAlternativeServiceRecentlyBroken(
300 const AlternativeService& alternative_service) = 0;
301
bnc8445b3002015-03-13 01:57:09302 // Returns true iff |alternative_service| is currently broken.
bncd9b132e2015-07-08 05:16:10303 // |alternative_service.host| must not be empty.
bnc8445b3002015-03-13 01:57:09304 virtual bool IsAlternativeServiceBroken(
bnc8ba74a1782015-04-14 17:42:08305 const AlternativeService& alternative_service) const = 0;
bnc8445b3002015-03-13 01:57:09306
bnccacc0992015-03-20 20:22:22307 // Returns true iff |alternative_service| was recently broken.
bncd9b132e2015-07-08 05:16:10308 // |alternative_service.host| must not be empty.
bnccacc0992015-03-20 20:22:22309 virtual bool WasAlternativeServiceRecentlyBroken(
310 const AlternativeService& alternative_service) = 0;
[email protected]f5716e32014-04-18 00:44:16311
bnccacc0992015-03-20 20:22:22312 // Confirms that |alternative_service| is working.
bncd9b132e2015-07-08 05:16:10313 // |alternative_service.host| must not be empty.
bnccacc0992015-03-20 20:22:22314 virtual void ConfirmAlternativeService(
315 const AlternativeService& alternative_service) = 0;
[email protected]f5716e32014-04-18 00:44:16316
bncd9b132e2015-07-08 05:16:10317 // Clear all alternative services for |origin|.
318 virtual void ClearAlternativeServices(const HostPortPair& origin) = 0;
[email protected]90276a152014-03-06 10:20:41319
bnc4988e432015-03-31 03:06:25320 // Returns all alternative service mappings.
bncd9b132e2015-07-08 05:16:10321 // Returned alternative services may have empty hostnames.
bnc4988e432015-03-31 03:06:25322 virtual const AlternativeServiceMap& alternative_service_map() const = 0;
[email protected]db96a882011-10-09 02:01:54323
bnc8ba74a1782015-04-14 17:42:08324 // Returns all alternative service mappings as human readable strings.
bncd9b132e2015-07-08 05:16:10325 // Empty alternative service hostnames will be printed as such.
ketan.goyald822f5ad2015-06-04 04:32:08326 virtual scoped_ptr<base::Value> GetAlternativeServiceInfoAsValue() const = 0;
bnc8ba74a1782015-04-14 17:42:08327
bnccacc0992015-03-20 20:22:22328 // Sets the threshold to be used when evaluating alternative service
329 // advertisments. Only advertisements with a probability greater than or equal
330 // to |threshold| will be honored. |threshold| must be between 0.0 and 1.0
331 // inclusive. Hence, a threshold of 0.0 implies that all advertisements will
332 // be honored.
bnc62891a52015-04-27 14:14:12333 virtual void SetAlternativeServiceProbabilityThreshold(double threshold) = 0;
[email protected]287d9412014-07-08 23:01:00334
[email protected]e0935cc2012-03-24 14:12:48335 // Gets a reference to the SettingsMap stored for a host.
336 // If no settings are stored, returns an empty SettingsMap.
337 virtual const SettingsMap& GetSpdySettings(
[email protected]1741c942014-03-18 07:33:39338 const HostPortPair& host_port_pair) = 0;
[email protected]53bfa31c2011-11-15 19:20:31339
[email protected]e0935cc2012-03-24 14:12:48340 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
341 // is to be persisted.
[email protected]39c48fc2012-03-12 18:42:12342 virtual bool SetSpdySetting(const HostPortPair& host_port_pair,
[email protected]e0935cc2012-03-24 14:12:48343 SpdySettingsIds id,
344 SpdySettingsFlags flags,
345 uint32 value) = 0;
[email protected]39c48fc2012-03-12 18:42:12346
[email protected]6dd1134e2013-04-30 21:13:09347 // Clears all SPDY settings for a host.
348 virtual void ClearSpdySettings(const HostPortPair& host_port_pair) = 0;
349
350 // Clears all SPDY settings for all hosts.
351 virtual void ClearAllSpdySettings() = 0;
[email protected]53bfa31c2011-11-15 19:20:31352
[email protected]e0935cc2012-03-24 14:12:48353 // Returns all persistent SPDY settings.
[email protected]53bfa31c2011-11-15 19:20:31354 virtual const SpdySettingsMap& spdy_settings_map() const = 0;
355
rcha5a10a82015-02-04 23:04:41356 virtual bool GetSupportsQuic(IPAddressNumber* last_address) const = 0;
rtenneti1c863aa2014-09-25 18:39:33357
rcha5a10a82015-02-04 23:04:41358 virtual void SetSupportsQuic(bool used_quic,
359 const IPAddressNumber& last_address) = 0;
rtenneti1c863aa2014-09-25 18:39:33360
rtenneticce34d52015-06-05 23:36:29361 // Sets |stats| for |host_port_pair|.
[email protected]3b8cf7f2014-01-27 22:08:51362 virtual void SetServerNetworkStats(const HostPortPair& host_port_pair,
rtenneti338cd36a2015-01-06 00:20:07363 ServerNetworkStats stats) = 0;
[email protected]3b8cf7f2014-01-27 22:08:51364
rtenneti338cd36a2015-01-06 00:20:07365 virtual const ServerNetworkStats* GetServerNetworkStats(
366 const HostPortPair& host_port_pair) = 0;
367
368 virtual const ServerNetworkStatsMap& server_network_stats_map() const = 0;
[email protected]3b8cf7f2014-01-27 22:08:51369
rtenneti8b673f72015-10-08 23:45:37370 // Save QuicServerInfo (in std::string form) for the given |server_id|.
371 // Returns true if the value has changed otherwise it returns false.
372 virtual bool SetQuicServerInfo(const QuicServerId& server_id,
373 const std::string& server_info) = 0;
374
375 // Get QuicServerInfo (in std::string form) for the given |server_id|.
376 virtual const std::string* GetQuicServerInfo(
377 const QuicServerId& server_id) = 0;
378
379 // Returns all persistent QuicServerInfo objects.
380 virtual const QuicServerInfoMap& quic_server_info_map() const = 0;
381
[email protected]db96a882011-10-09 02:01:54382 private:
[email protected]db96a882011-10-09 02:01:54383 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties);
384};
385
386} // namespace net
387
388#endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_