blob: d5301f7eba766dd8d1b68eb137d2fd7a422b6224 [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
Matt Menkedce50562017-12-19 22:12:2013#include "base/callback.h"
[email protected]9801e3702014-03-07 09:33:5514#include "base/containers/mru_cache.h"
Avi Drissman13fc8932015-12-20 04:40:4615#include "base/macros.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/net_export.h"
Matt Menke2890796e2019-08-02 16:55:2318#include "net/http/alternative_service.h"
Victor Vasiliev6bb59d22019-03-08 21:34:5119#include "net/third_party/quiche/src/quic/core/quic_bandwidth.h"
20#include "net/third_party/quiche/src/quic/core/quic_server_id.h"
21#include "net/third_party/quiche/src/quic/core/quic_versions.h"
Victor Vasiliev27cc7712019-01-24 11:50:1422#include "net/third_party/quiche/src/spdy/core/spdy_framer.h" // TODO(willchan): Reconsider this.
23#include "net/third_party/quiche/src/spdy/core/spdy_protocol.h"
zhongyi3d4a55e72016-04-22 20:36:4624#include "url/scheme_host_port.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
Matt Menke2890796e2019-08-02 16:55:2332class HostPortPair;
martijnfe9636e2016-02-06 14:33:3233class IPAddress;
bncfacdd852015-01-09 19:22:5434struct SSLConfig;
35
rtenneti1c863aa2014-09-25 18:39:3336struct NET_EXPORT SupportsQuic {
37 SupportsQuic() : used_quic(false) {}
38 SupportsQuic(bool used_quic, const std::string& address)
39 : used_quic(used_quic),
40 address(address) {}
41
42 bool Equals(const SupportsQuic& other) const {
43 return used_quic == other.used_quic && address == other.address;
44 }
45
46 bool used_quic;
47 std::string address;
48};
49
rtenneti338cd36a2015-01-06 00:20:0750struct NET_EXPORT ServerNetworkStats {
Ryan Hamilton8d9ee76e2018-05-29 23:52:5251 ServerNetworkStats() : bandwidth_estimate(quic::QuicBandwidth::Zero()) {}
rtenneti338cd36a2015-01-06 00:20:0752
rtenneticce34d52015-06-05 23:36:2953 bool operator==(const ServerNetworkStats& other) const {
54 return srtt == other.srtt && bandwidth_estimate == other.bandwidth_estimate;
55 }
56
57 bool operator!=(const ServerNetworkStats& other) const {
58 return !this->operator==(other);
59 }
60
rtenneti338cd36a2015-01-06 00:20:0761 base::TimeDelta srtt;
Ryan Hamilton8d9ee76e2018-05-29 23:52:5262 quic::QuicBandwidth bandwidth_estimate;
rtenneti338cd36a2015-01-06 00:20:0763};
64
bncd9b132e2015-07-08 05:16:1065typedef std::vector<AlternativeService> AlternativeServiceVector;
66typedef std::vector<AlternativeServiceInfo> AlternativeServiceInfoVector;
Yixin Wang4a227aa22017-11-30 21:33:0167
Yixin Wang4a227aa22017-11-30 21:33:0168// Store at most 300 MRU SupportsSpdyServerHostPortPairs in memory and disk.
69const int kMaxSupportsSpdyServerEntries = 300;
70
71// Store at most 200 MRU AlternateProtocolHostPortPairs in memory and disk.
72const int kMaxAlternateProtocolEntries = 200;
73
74// Store at most 200 MRU ServerNetworkStats in memory and disk.
75const int kMaxServerNetworkStatsEntries = 200;
76
77// Store at most 200 MRU RecentlyBrokenAlternativeServices in memory and disk.
Matt Menke2890796e2019-08-02 16:55:2378// This ideally would be with the other constants in HttpServerProperties, but
79// has to go here instead of prevent a circular dependency.
Yixin Wang4a227aa22017-11-30 21:33:0180const int kMaxRecentlyBrokenAlternativeServiceEntries = 200;
81
82// Store at most 5 MRU QUIC servers by default. This is mainly used by cronet.
83const int kDefaultMaxQuicServerEntries = 5;
84
85// Stores flattened representation of servers (scheme, host, port) and whether
86// or not they support SPDY.
87class SpdyServersMap : public base::MRUCache<std::string, bool> {
88 public:
89 SpdyServersMap()
90 : base::MRUCache<std::string, bool>(kMaxSupportsSpdyServerEntries) {}
91};
92
93class AlternativeServiceMap
94 : public base::MRUCache<url::SchemeHostPort, AlternativeServiceInfoVector> {
95 public:
96 AlternativeServiceMap()
97 : base::MRUCache<url::SchemeHostPort, AlternativeServiceInfoVector>(
98 kMaxAlternateProtocolEntries) {}
99};
100
101class ServerNetworkStatsMap
102 : public base::MRUCache<url::SchemeHostPort, ServerNetworkStats> {
103 public:
104 ServerNetworkStatsMap()
105 : base::MRUCache<url::SchemeHostPort, ServerNetworkStats>(
106 kMaxServerNetworkStatsEntries) {}
107};
108
Yixin Wang4a227aa22017-11-30 21:33:01109// Max number of quic servers to store is not hardcoded and can be set.
110// Because of this, QuicServerInfoMap will not be a subclass of MRUCache.
Ryan Hamilton8d9ee76e2018-05-29 23:52:52111typedef base::MRUCache<quic::QuicServerId, std::string> QuicServerInfoMap;
[email protected]17291a022011-10-10 07:32:53112
[email protected]db96a882011-10-09 02:01:54113// The interface for setting/retrieving the HTTP server properties.
[email protected]17291a022011-10-10 07:32:53114// Currently, this class manages servers':
Bence Békyffb237f2017-06-29 12:17:39115// * HTTP/2 support;
116// * Alternative Service support;
rtenneticd2aaa15b2015-10-10 20:29:33117// * QUIC data (like ServerNetworkStats and QuicServerInfo).
118//
119// Embedders must ensure that HttpServerProperites is completely initialized
120// before the first request is issued.
[email protected]db96a882011-10-09 02:01:54121class NET_EXPORT HttpServerProperties {
122 public:
[email protected]41c3696d2012-04-08 15:22:31123 HttpServerProperties() {}
124 virtual ~HttpServerProperties() {}
[email protected]db96a882011-10-09 02:01:54125
Matt Menkedce50562017-12-19 22:12:20126 // Deletes all data. If |callback| is non-null, flushes data to disk
127 // and invokes the callback asynchronously once changes have been written to
128 // disk.
129 virtual void Clear(base::OnceClosure callback) = 0;
[email protected]17291a022011-10-10 07:32:53130
rch9c3e3382015-01-30 23:57:32131 // Returns true if |server| supports a network protocol which honors
132 // request prioritization.
rdsmithc31e0602016-08-30 06:27:23133 // Note that this also implies that the server supports request
134 // multiplexing, since priorities imply a relationship between
135 // multiple requests.
zhongyi3d4a55e72016-04-22 20:36:46136 virtual bool SupportsRequestPriority(const url::SchemeHostPort& server) = 0;
[email protected]db96a882011-10-09 02:01:54137
rtennetie267d6a2015-06-05 21:55:23138 // Returns the value set by SetSupportsSpdy(). If not set, returns false.
zhongyi3d4a55e72016-04-22 20:36:46139 virtual bool GetSupportsSpdy(const url::SchemeHostPort& server) = 0;
rtennetie267d6a2015-06-05 21:55:23140
[email protected]db96a882011-10-09 02:01:54141 // Add |server| into the persistent store. Should only be called from IO
142 // thread.
zhongyi3d4a55e72016-04-22 20:36:46143 virtual void SetSupportsSpdy(const url::SchemeHostPort& server,
[email protected]db96a882011-10-09 02:01:54144 bool support_spdy) = 0;
145
bncfacdd852015-01-09 19:22:54146 // Returns true if |server| has required HTTP/1.1 via HTTP/2 error code.
147 virtual bool RequiresHTTP11(const HostPortPair& server) = 0;
148
149 // Require HTTP/1.1 on subsequent connections. Not persisted.
150 virtual void SetHTTP11Required(const HostPortPair& server) = 0;
151
152 // Modify SSLConfig to force HTTP/1.1.
153 static void ForceHTTP11(SSLConfig* ssl_config);
154
155 // Modify SSLConfig to force HTTP/1.1 if necessary.
156 virtual void MaybeForceHTTP11(const HostPortPair& server,
157 SSLConfig* ssl_config) = 0;
158
rchdc7b9052016-03-17 20:51:50159 // Return all alternative services for |origin|, including broken ones.
bncd9b132e2015-07-08 05:16:10160 // Returned alternative services never have empty hostnames.
zhongyic4de03032017-05-19 04:07:34161 virtual AlternativeServiceInfoVector GetAlternativeServiceInfos(
zhongyi3d4a55e72016-04-22 20:36:46162 const url::SchemeHostPort& origin) = 0;
[email protected]17291a022011-10-10 07:32:53163
zhongyie537a002017-06-27 16:48:21164 // Set a single HTTP/2 alternative service for |origin|. Previous
165 // alternative services for |origin| are discarded.
166 // |alternative_service.host| may be empty.
167 // Return true if |alternative_service_map_| has changed significantly enough
168 // that it should be persisted to disk.
169 virtual bool SetHttp2AlternativeService(
170 const url::SchemeHostPort& origin,
171 const AlternativeService& alternative_service,
172 base::Time expiration) = 0;
173
174 // Set a single QUIC alternative service for |origin|. Previous alternative
bncd9b132e2015-07-08 05:16:10175 // services for |origin| are discarded.
176 // |alternative_service.host| may be empty.
bnc4b91d832016-07-27 23:36:12177 // Return true if |alternative_service_map_| has changed significantly enough
178 // that it should be persisted to disk.
zhongyie537a002017-06-27 16:48:21179 virtual bool SetQuicAlternativeService(
zhongyi3d4a55e72016-04-22 20:36:46180 const url::SchemeHostPort& origin,
bnccacc0992015-03-20 20:22:22181 const AlternativeService& alternative_service,
zhongyie537a002017-06-27 16:48:21182 base::Time expiration,
Nick Harper23290b82019-05-02 00:02:56183 const quic::ParsedQuicVersionVector& advertised_versions) = 0;
[email protected]17291a022011-10-10 07:32:53184
bncd9b132e2015-07-08 05:16:10185 // Set alternative services for |origin|. Previous alternative services for
186 // |origin| are discarded.
187 // Hostnames in |alternative_service_info_vector| may be empty.
bnc4b91d832016-07-27 23:36:12188 // |alternative_service_info_vector| may be empty.
189 // Return true if |alternative_service_map_| has changed significantly enough
190 // that it should be persisted to disk.
bncd9b132e2015-07-08 05:16:10191 virtual bool SetAlternativeServices(
zhongyi3d4a55e72016-04-22 20:36:46192 const url::SchemeHostPort& origin,
bncd9b132e2015-07-08 05:16:10193 const AlternativeServiceInfoVector& alternative_service_info_vector) = 0;
194
bnccacc0992015-03-20 20:22:22195 // Marks |alternative_service| as broken.
bncd9b132e2015-07-08 05:16:10196 // |alternative_service.host| must not be empty.
bnccacc0992015-03-20 20:22:22197 virtual void MarkAlternativeServiceBroken(
198 const AlternativeService& alternative_service) = 0;
[email protected]17291a022011-10-10 07:32:53199
Zhongyi Shi826b1d22018-08-28 21:45:15200 // Marks |alternative_service| as broken until the default network changes.
201 // |alternative_service.host| must not be empty.
202 virtual void MarkAlternativeServiceBrokenUntilDefaultNetworkChanges(
203 const AlternativeService& alternative_service) = 0;
204
bncd1e0aa22015-03-13 10:14:31205 // Marks |alternative_service| as recently broken.
bncd9b132e2015-07-08 05:16:10206 // |alternative_service.host| must not be empty.
bncd1e0aa22015-03-13 10:14:31207 virtual void MarkAlternativeServiceRecentlyBroken(
208 const AlternativeService& alternative_service) = 0;
209
bnc8445b3002015-03-13 01:57:09210 // Returns true iff |alternative_service| is currently broken.
bncd9b132e2015-07-08 05:16:10211 // |alternative_service.host| must not be empty.
bnc8445b3002015-03-13 01:57:09212 virtual bool IsAlternativeServiceBroken(
bnc8ba74a1782015-04-14 17:42:08213 const AlternativeService& alternative_service) const = 0;
bnc8445b3002015-03-13 01:57:09214
bnccacc0992015-03-20 20:22:22215 // Returns true iff |alternative_service| was recently broken.
bncd9b132e2015-07-08 05:16:10216 // |alternative_service.host| must not be empty.
bnccacc0992015-03-20 20:22:22217 virtual bool WasAlternativeServiceRecentlyBroken(
218 const AlternativeService& alternative_service) = 0;
[email protected]f5716e32014-04-18 00:44:16219
bnccacc0992015-03-20 20:22:22220 // Confirms that |alternative_service| is working.
bncd9b132e2015-07-08 05:16:10221 // |alternative_service.host| must not be empty.
bnccacc0992015-03-20 20:22:22222 virtual void ConfirmAlternativeService(
223 const AlternativeService& alternative_service) = 0;
[email protected]f5716e32014-04-18 00:44:16224
Zhongyi Shi826b1d22018-08-28 21:45:15225 // Called when the default network changes.
226 // Clears all the alternative services that were marked broken until the
227 // default network changed.
228 // Returns true if there is any broken alternative service affected by the
229 // default network change.
230 virtual bool OnDefaultNetworkChanged() = 0;
231
bnc4988e432015-03-31 03:06:25232 // Returns all alternative service mappings.
bncd9b132e2015-07-08 05:16:10233 // Returned alternative services may have empty hostnames.
bnc4988e432015-03-31 03:06:25234 virtual const AlternativeServiceMap& alternative_service_map() const = 0;
[email protected]db96a882011-10-09 02:01:54235
bnc8ba74a1782015-04-14 17:42:08236 // Returns all alternative service mappings as human readable strings.
bncd9b132e2015-07-08 05:16:10237 // Empty alternative service hostnames will be printed as such.
danakj1fd259a02016-04-16 03:17:09238 virtual std::unique_ptr<base::Value> GetAlternativeServiceInfoAsValue()
239 const = 0;
bnc8ba74a1782015-04-14 17:42:08240
martijnfe9636e2016-02-06 14:33:32241 virtual bool GetSupportsQuic(IPAddress* last_address) const = 0;
rtenneti1c863aa2014-09-25 18:39:33242
rcha5a10a82015-02-04 23:04:41243 virtual void SetSupportsQuic(bool used_quic,
martijnfe9636e2016-02-06 14:33:32244 const IPAddress& last_address) = 0;
rtenneti1c863aa2014-09-25 18:39:33245
rchac7f35e2017-03-15 20:42:30246 // Sets |stats| for |server|.
zhongyi3d4a55e72016-04-22 20:36:46247 virtual void SetServerNetworkStats(const url::SchemeHostPort& server,
rtenneti338cd36a2015-01-06 00:20:07248 ServerNetworkStats stats) = 0;
[email protected]3b8cf7f2014-01-27 22:08:51249
rchac7f35e2017-03-15 20:42:30250 // Clears any stats for |server|.
251 virtual void ClearServerNetworkStats(const url::SchemeHostPort& server) = 0;
252
253 // Returns any stats for |server| or nullptr if there are none.
rtenneti338cd36a2015-01-06 00:20:07254 virtual const ServerNetworkStats* GetServerNetworkStats(
zhongyi3d4a55e72016-04-22 20:36:46255 const url::SchemeHostPort& server) = 0;
rtenneti338cd36a2015-01-06 00:20:07256
257 virtual const ServerNetworkStatsMap& server_network_stats_map() const = 0;
[email protected]3b8cf7f2014-01-27 22:08:51258
rtenneti8b673f72015-10-08 23:45:37259 // Save QuicServerInfo (in std::string form) for the given |server_id|.
260 // Returns true if the value has changed otherwise it returns false.
Ryan Hamilton8d9ee76e2018-05-29 23:52:52261 virtual bool SetQuicServerInfo(const quic::QuicServerId& server_id,
rtenneti8b673f72015-10-08 23:45:37262 const std::string& server_info) = 0;
263
264 // Get QuicServerInfo (in std::string form) for the given |server_id|.
265 virtual const std::string* GetQuicServerInfo(
Ryan Hamilton8d9ee76e2018-05-29 23:52:52266 const quic::QuicServerId& server_id) = 0;
rtenneti8b673f72015-10-08 23:45:37267
268 // Returns all persistent QuicServerInfo objects.
269 virtual const QuicServerInfoMap& quic_server_info_map() const = 0;
270
rtenneti6971c172016-01-15 20:12:10271 // Returns the number of server configs (QuicServerInfo objects) persisted.
272 virtual size_t max_server_configs_stored_in_properties() const = 0;
273
274 // Sets the number of server configs (QuicServerInfo objects) to be persisted.
275 virtual void SetMaxServerConfigsStoredInProperties(
276 size_t max_server_configs_stored_in_properties) = 0;
277
xunjieli1df4de12017-02-09 17:21:19278 // Returns whether HttpServerProperties is initialized.
279 virtual bool IsInitialized() const = 0;
280
[email protected]db96a882011-10-09 02:01:54281 private:
[email protected]db96a882011-10-09 02:01:54282 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties);
283};
284
285} // namespace net
286
287#endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_