blob: dda83011e07091c5f323857a80e4491714941083 [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
Matt Menke609160742019-08-02 18:47:268#include <stddef.h>
9#include <stdint.h>
10
[email protected]17291a022011-10-10 07:32:5311#include <map>
Matt Menke609160742019-08-02 18:47:2612#include <memory>
13#include <set>
[email protected]db96a882011-10-09 02:01:5414#include <string>
jsbellcea42a52015-11-30 23:50:2515#include <tuple>
bncd9b132e2015-07-08 05:16:1016#include <vector>
17
Matt Menkedce50562017-12-19 22:12:2018#include "base/callback.h"
[email protected]9801e3702014-03-07 09:33:5519#include "base/containers/mru_cache.h"
Avi Drissman13fc8932015-12-20 04:40:4620#include "base/macros.h"
Matt Menke609160742019-08-02 18:47:2621#include "base/memory/weak_ptr.h"
Matt Menke5e7dcd32019-08-09 22:25:2122#include "base/optional.h"
Matt Menke609160742019-08-02 18:47:2623#include "base/threading/thread_checker.h"
[email protected]3b8cf7f2014-01-27 22:08:5124#include "base/time/time.h"
Matt Menke609160742019-08-02 18:47:2625#include "base/timer/timer.h"
26#include "base/values.h"
27#include "net/base/host_port_pair.h"
28#include "net/base/ip_address.h"
[email protected]db96a882011-10-09 02:01:5429#include "net/base/net_export.h"
Matt Menke1be93d22019-08-20 16:57:5830#include "net/base/network_isolation_key.h"
Matt Menke2890796e2019-08-02 16:55:2331#include "net/http/alternative_service.h"
Matt Menke609160742019-08-02 18:47:2632#include "net/http/broken_alternative_services.h"
33#include "net/http/http_server_properties.h"
Victor Vasiliev6bb59d22019-03-08 21:34:5134#include "net/third_party/quiche/src/quic/core/quic_bandwidth.h"
35#include "net/third_party/quiche/src/quic/core/quic_server_id.h"
36#include "net/third_party/quiche/src/quic/core/quic_versions.h"
Victor Vasiliev27cc7712019-01-24 11:50:1437#include "net/third_party/quiche/src/spdy/core/spdy_framer.h" // TODO(willchan): Reconsider this.
38#include "net/third_party/quiche/src/spdy/core/spdy_protocol.h"
zhongyi3d4a55e72016-04-22 20:36:4639#include "url/scheme_host_port.h"
[email protected]db96a882011-10-09 02:01:5440
bnc8ba74a1782015-04-14 17:42:0841namespace base {
Matt Menke609160742019-08-02 18:47:2642class Clock;
43class TickClock;
bnc8ba74a1782015-04-14 17:42:0844class Value;
45}
46
[email protected]db96a882011-10-09 02:01:5447namespace net {
48
Matt Menke609160742019-08-02 18:47:2649class HttpServerPropertiesManager;
martijnfe9636e2016-02-06 14:33:3250class IPAddress;
Matt Menke609160742019-08-02 18:47:2651class NetLog;
bncfacdd852015-01-09 19:22:5452struct SSLConfig;
53
rtenneti1c863aa2014-09-25 18:39:3354struct NET_EXPORT SupportsQuic {
55 SupportsQuic() : used_quic(false) {}
56 SupportsQuic(bool used_quic, const std::string& address)
Matt Menke609160742019-08-02 18:47:2657 : used_quic(used_quic), address(address) {}
rtenneti1c863aa2014-09-25 18:39:3358
59 bool Equals(const SupportsQuic& other) const {
60 return used_quic == other.used_quic && address == other.address;
61 }
62
63 bool used_quic;
64 std::string address;
65};
66
rtenneti338cd36a2015-01-06 00:20:0767struct NET_EXPORT ServerNetworkStats {
Ryan Hamilton8d9ee76e2018-05-29 23:52:5268 ServerNetworkStats() : bandwidth_estimate(quic::QuicBandwidth::Zero()) {}
rtenneti338cd36a2015-01-06 00:20:0769
rtenneticce34d52015-06-05 23:36:2970 bool operator==(const ServerNetworkStats& other) const {
71 return srtt == other.srtt && bandwidth_estimate == other.bandwidth_estimate;
72 }
73
74 bool operator!=(const ServerNetworkStats& other) const {
75 return !this->operator==(other);
76 }
77
rtenneti338cd36a2015-01-06 00:20:0778 base::TimeDelta srtt;
Ryan Hamilton8d9ee76e2018-05-29 23:52:5279 quic::QuicBandwidth bandwidth_estimate;
rtenneti338cd36a2015-01-06 00:20:0780};
81
bncd9b132e2015-07-08 05:16:1082typedef std::vector<AlternativeService> AlternativeServiceVector;
Yixin Wang4a227aa22017-11-30 21:33:0183
Yixin Wang4a227aa22017-11-30 21:33:0184// Store at most 200 MRU RecentlyBrokenAlternativeServices in memory and disk.
Matt Menke2890796e2019-08-02 16:55:2385// This ideally would be with the other constants in HttpServerProperties, but
86// has to go here instead of prevent a circular dependency.
Yixin Wang4a227aa22017-11-30 21:33:0187const int kMaxRecentlyBrokenAlternativeServiceEntries = 200;
88
89// Store at most 5 MRU QUIC servers by default. This is mainly used by cronet.
90const int kDefaultMaxQuicServerEntries = 5;
91
[email protected]db96a882011-10-09 02:01:5492// The interface for setting/retrieving the HTTP server properties.
[email protected]17291a022011-10-10 07:32:5393// Currently, this class manages servers':
Bence Békyffb237f2017-06-29 12:17:3994// * HTTP/2 support;
95// * Alternative Service support;
rtenneticd2aaa15b2015-10-10 20:29:3396// * QUIC data (like ServerNetworkStats and QuicServerInfo).
97//
Matt Menkefe1f1c82019-08-20 17:49:1198// Optionally retrieves and saves properties from/to disk. This class is not
99// threadsafe.
Matt Menke609160742019-08-02 18:47:26100class NET_EXPORT HttpServerProperties
101 : public BrokenAlternativeServices::Delegate {
[email protected]db96a882011-10-09 02:01:54102 public:
Matt Menke5e7dcd32019-08-09 22:25:21103 // Store at most 500 MRU ServerInfos in memory and disk.
104 static const int kMaxServerInfoEntries = 500;
105
Matt Menke609160742019-08-02 18:47:26106 // Provides an interface to interact with persistent preferences storage
107 // implemented by the embedder. The prefs are assumed not to have been loaded
108 // before HttpServerPropertiesManager construction.
109 class NET_EXPORT PrefDelegate {
110 public:
111 virtual ~PrefDelegate();
112
113 // Returns the branch of the preferences system for the server properties.
114 // Returns nullptr if the pref system has no data for the server properties.
115 virtual const base::DictionaryValue* GetServerProperties() const = 0;
116
117 // Sets the server properties to the given value. If |callback| is
118 // non-empty, flushes data to persistent storage and invokes |callback|
119 // asynchronously when complete.
120 virtual void SetServerProperties(const base::DictionaryValue& value,
121 base::OnceClosure callback) = 0;
122
123 // Starts listening for prefs to be loaded. If prefs are already loaded,
124 // |pref_loaded_callback| will be invoked asynchronously. Callback will be
125 // invoked even if prefs fail to load. Will only be called once by the
126 // HttpServerPropertiesManager.
127 virtual void WaitForPrefLoad(base::OnceClosure pref_loaded_callback) = 0;
128 };
129
Matt Menkef2ee07c2019-08-29 02:10:36130 // Contains metadata about a particular server. Note that all methods that
131 // take a "SchemeHostPort" expect schemes of ws and wss to be mapped to http
132 // and https, respectively. See GetNormalizedSchemeHostPort().
Matt Menke5e7dcd32019-08-09 22:25:21133 struct NET_EXPORT ServerInfo {
134 ServerInfo();
135 ServerInfo(const ServerInfo& server_info);
136 ServerInfo(ServerInfo&& server_info);
137 ~ServerInfo();
138
Matt Menkefe9b5962019-08-14 20:56:14139 // Returns true if no fields are populated.
140 bool empty() const;
141
Matt Menke1be93d22019-08-20 16:57:58142 // Used in tests.
143 bool operator==(const ServerInfo& other) const;
144
Matt Menke5e7dcd32019-08-09 22:25:21145 // IMPORTANT: When adding a field here, be sure to update
146 // HttpServerProperties::OnServerInfoLoaded() as well as
147 // HttpServerPropertiesManager to correctly load/save the from/to the pref
148 // store.
149
150 // Whether or not a server is known to support H2/SPDY. False indicates
151 // known lack of support, true indicates known support, and not set
152 // indicates unknown. The difference between false and not set only matters
153 // when loading from disk, when an initialized false value will take
154 // priority over a not set value.
155 base::Optional<bool> supports_spdy;
156
Matt Menkef2ee07c2019-08-29 02:10:36157 // True if the server has previously indicated it required HTTP/1.1. Unlike
158 // other fields, not persisted to disk.
159 base::Optional<bool> requires_http11;
160
Matt Menkefe9b5962019-08-14 20:56:14161 base::Optional<AlternativeServiceInfoVector> alternative_services;
Matt Menke86878a62019-08-14 21:01:11162 base::Optional<ServerNetworkStats> server_network_stats;
Matt Menke5e7dcd32019-08-09 22:25:21163 };
164
Matt Menke1be93d22019-08-20 16:57:58165 struct NET_EXPORT ServerInfoMapKey {
166 // If |use_network_isolation_key| is false, an empty NetworkIsolationKey is
Matt Menked9b24f02019-09-26 17:07:17167 // used instead of |network_isolation_key|. Note that |server| can be passed
168 // in via std::move(), since most callsites can pass a recently created
169 // SchemeHostPort.
170 ServerInfoMapKey(url::SchemeHostPort server,
Matt Menke1be93d22019-08-20 16:57:58171 const NetworkIsolationKey& network_isolation_key,
172 bool use_network_isolation_key);
173 ~ServerInfoMapKey();
174
175 bool operator<(const ServerInfoMapKey& other) const;
176
Matt Menked9b24f02019-09-26 17:07:17177 // IMPORTANT: The constructor normalizes the scheme so that "ws" is replaced
178 // by "http" and "wss" by "https", so this should never be compared directly
179 // with values passed into to HttpServerProperties methods.
Matt Menke1be93d22019-08-20 16:57:58180 url::SchemeHostPort server;
Matt Menked9b24f02019-09-26 17:07:17181
Matt Menke1be93d22019-08-20 16:57:58182 NetworkIsolationKey network_isolation_key;
183 };
184
Matt Menke5e7dcd32019-08-09 22:25:21185 class NET_EXPORT ServerInfoMap
Matt Menke1be93d22019-08-20 16:57:58186 : public base::MRUCache<ServerInfoMapKey, ServerInfo> {
Matt Menke5e7dcd32019-08-09 22:25:21187 public:
188 ServerInfoMap();
189
190 // If there's an entry corresponding to |key|, brings that entry to the
191 // front and returns an iterator to it. Otherwise, inserts an empty
192 // ServerInfo using |key|, and returns an iterator to it.
Matt Menke1be93d22019-08-20 16:57:58193 iterator GetOrPut(const ServerInfoMapKey& key);
Matt Menke5e7dcd32019-08-09 22:25:21194
Matt Menkefe9b5962019-08-14 20:56:14195 // Erases the ServerInfo identified by |server_info_it| if no fields have
196 // data. The iterator must point to an entry in the map. Regardless of
197 // whether the entry is removed or not, returns iterator for the next entry.
198 iterator EraseIfEmpty(iterator server_info_it);
199
Matt Menke5e7dcd32019-08-09 22:25:21200 private:
201 DISALLOW_COPY_AND_ASSIGN(ServerInfoMap);
202 };
203
Matt Menke0142bc02019-09-13 20:17:45204 struct NET_EXPORT QuicServerInfoMapKey {
205 // If |use_network_isolation_key| is false, an empty NetworkIsolationKey is
206 // used instead of |network_isolation_key|.
207 QuicServerInfoMapKey(const quic::QuicServerId& server_id,
208 const NetworkIsolationKey& network_isolation_key,
209 bool use_network_isolation_key);
210 ~QuicServerInfoMapKey();
211
212 bool operator<(const QuicServerInfoMapKey& other) const;
213
214 // Used in tests.
215 bool operator==(const QuicServerInfoMapKey& other) const;
216
217 quic::QuicServerId server_id;
218 NetworkIsolationKey network_isolation_key;
219 };
220
221 // Max number of quic servers to store is not hardcoded and can be set.
222 // Because of this, QuicServerInfoMap will not be a subclass of MRUCache.
223 // Separate from ServerInfoMap because the key includes privacy mode (Since
224 // this is analogous to the SSL session cache, which has separate caches for
225 // privacy mode), and each entry can be quite large, so it has its own size
226 // limit, which is much smaller than the ServerInfoMap's limit.
227 typedef base::MRUCache<QuicServerInfoMapKey, std::string> QuicServerInfoMap;
228
Matt Menke609160742019-08-02 18:47:26229 // If a |pref_delegate| is specified, it will be used to read/write the
230 // properties to a pref file. Writes are rate limited to improve performance.
231 //
232 // |tick_clock| is used for setting expiration times and scheduling the
233 // expiration of broken alternative services. If null, default clock will be
234 // used.
235 //
236 // |clock| is used for converting base::TimeTicks to base::Time for
237 // wherever base::Time is preferable.
238 HttpServerProperties(std::unique_ptr<PrefDelegate> pref_delegate = nullptr,
239 NetLog* net_log = nullptr,
240 const base::TickClock* tick_clock = nullptr,
241 base::Clock* clock = nullptr);
242
243 ~HttpServerProperties() override;
[email protected]db96a882011-10-09 02:01:54244
Matt Menkedce50562017-12-19 22:12:20245 // Deletes all data. If |callback| is non-null, flushes data to disk
246 // and invokes the callback asynchronously once changes have been written to
247 // disk.
Matt Menke609160742019-08-02 18:47:26248 void Clear(base::OnceClosure callback);
[email protected]17291a022011-10-10 07:32:53249
Matt Menkefe1f1c82019-08-20 17:49:11250 // Returns true if |server|, in the context of |network_isolation_key|, has
251 // previously supported a network protocol which honors request
252 // prioritization.
253 //
rdsmithc31e0602016-08-30 06:27:23254 // Note that this also implies that the server supports request
255 // multiplexing, since priorities imply a relationship between
256 // multiple requests.
Matt Menkefe1f1c82019-08-20 17:49:11257 bool SupportsRequestPriority(
258 const url::SchemeHostPort& server,
259 const net::NetworkIsolationKey& network_isolation_key);
[email protected]db96a882011-10-09 02:01:54260
rtennetie267d6a2015-06-05 21:55:23261 // Returns the value set by SetSupportsSpdy(). If not set, returns false.
Matt Menkefe1f1c82019-08-20 17:49:11262 bool GetSupportsSpdy(const url::SchemeHostPort& server,
263 const net::NetworkIsolationKey& network_isolation_key);
rtennetie267d6a2015-06-05 21:55:23264
Matt Menkefe1f1c82019-08-20 17:49:11265 // Records whether |server| supports H2 or not. Information is restricted to
266 // the context of |network_isolation_key|, to prevent cross-site information
267 // leakage.
268 void SetSupportsSpdy(const url::SchemeHostPort& server,
269 const net::NetworkIsolationKey& network_isolation_key,
270 bool supports_spdy);
[email protected]db96a882011-10-09 02:01:54271
Matt Menkef2ee07c2019-08-29 02:10:36272 // Returns true if |server| has required HTTP/1.1 via HTTP/2 error code, in
273 // the context of |network_isolation_key|.
274 bool RequiresHTTP11(const url::SchemeHostPort& server,
275 const net::NetworkIsolationKey& network_isolation_key);
bncfacdd852015-01-09 19:22:54276
Matt Menkef2ee07c2019-08-29 02:10:36277 // Require HTTP/1.1 on subsequent connections, in the context of
278 // |network_isolation_key|. Not persisted.
279 void SetHTTP11Required(const url::SchemeHostPort& server,
280 const net::NetworkIsolationKey& network_isolation_key);
bncfacdd852015-01-09 19:22:54281
bncfacdd852015-01-09 19:22:54282 // Modify SSLConfig to force HTTP/1.1 if necessary.
Matt Menkef2ee07c2019-08-29 02:10:36283 void MaybeForceHTTP11(const url::SchemeHostPort& server,
284 const net::NetworkIsolationKey& network_isolation_key,
285 SSLConfig* ssl_config);
bncfacdd852015-01-09 19:22:54286
Matt Menke3233d8f22019-08-20 21:01:49287 // Return all alternative services for |origin|, learned in the context of
288 // |network_isolation_key|, including broken ones. Returned alternative
289 // services never have empty hostnames.
Matt Menke609160742019-08-02 18:47:26290 AlternativeServiceInfoVector GetAlternativeServiceInfos(
Matt Menke3233d8f22019-08-20 21:01:49291 const url::SchemeHostPort& origin,
292 const net::NetworkIsolationKey& network_isolation_key);
[email protected]17291a022011-10-10 07:32:53293
zhongyie537a002017-06-27 16:48:21294 // Set a single HTTP/2 alternative service for |origin|. Previous
295 // alternative services for |origin| are discarded.
296 // |alternative_service.host| may be empty.
Matt Menke9aa86262019-08-21 15:52:07297 void SetHttp2AlternativeService(
298 const url::SchemeHostPort& origin,
299 const NetworkIsolationKey& network_isolation_key,
300 const AlternativeService& alternative_service,
301 base::Time expiration);
zhongyie537a002017-06-27 16:48:21302
303 // Set a single QUIC alternative service for |origin|. Previous alternative
bncd9b132e2015-07-08 05:16:10304 // services for |origin| are discarded.
305 // |alternative_service.host| may be empty.
Matt Menke2f63ef692019-08-02 22:48:03306 void SetQuicAlternativeService(
zhongyi3d4a55e72016-04-22 20:36:46307 const url::SchemeHostPort& origin,
Matt Menke9aa86262019-08-21 15:52:07308 const NetworkIsolationKey& network_isolation_key,
bnccacc0992015-03-20 20:22:22309 const AlternativeService& alternative_service,
zhongyie537a002017-06-27 16:48:21310 base::Time expiration,
Matt Menke609160742019-08-02 18:47:26311 const quic::ParsedQuicVersionVector& advertised_versions);
[email protected]17291a022011-10-10 07:32:53312
Matt Menke3233d8f22019-08-20 21:01:49313 // Set alternative services for |origin|, learned in the context of
314 // |network_isolation_key|. Previous alternative services for |origin| are
315 // discarded. Hostnames in |alternative_service_info_vector| may be empty.
bnc4b91d832016-07-27 23:36:12316 // |alternative_service_info_vector| may be empty.
Matt Menke2f63ef692019-08-02 22:48:03317 void SetAlternativeServices(
zhongyi3d4a55e72016-04-22 20:36:46318 const url::SchemeHostPort& origin,
Matt Menke3233d8f22019-08-20 21:01:49319 const net::NetworkIsolationKey& network_isolation_key,
Matt Menke609160742019-08-02 18:47:26320 const AlternativeServiceInfoVector& alternative_service_info_vector);
bncd9b132e2015-07-08 05:16:10321
Matt Menke977f02792019-09-10 16:23:09322 // Marks |alternative_service| as broken in the context of
323 // |network_isolation_key|. |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26324 void MarkAlternativeServiceBroken(
Matt Menke977f02792019-09-10 16:23:09325 const AlternativeService& alternative_service,
Matt Menkeb32ba5122019-09-10 19:17:05326 const net::NetworkIsolationKey& network_isolation_key);
[email protected]17291a022011-10-10 07:32:53327
Matt Menke977f02792019-09-10 16:23:09328 // Marks |alternative_service| as broken in the context of
329 // |network_isolation_key| until the default network changes.
Zhongyi Shi826b1d22018-08-28 21:45:15330 // |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26331 void MarkAlternativeServiceBrokenUntilDefaultNetworkChanges(
Matt Menke977f02792019-09-10 16:23:09332 const AlternativeService& alternative_service,
Matt Menkeb32ba5122019-09-10 19:17:05333 const net::NetworkIsolationKey& network_isolation_key);
Zhongyi Shi826b1d22018-08-28 21:45:15334
Matt Menke977f02792019-09-10 16:23:09335 // Marks |alternative_service| as recently broken in the context of
336 // |network_isolation_key|. |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26337 void MarkAlternativeServiceRecentlyBroken(
Matt Menke977f02792019-09-10 16:23:09338 const AlternativeService& alternative_service,
Matt Menkeb32ba5122019-09-10 19:17:05339 const net::NetworkIsolationKey& network_isolation_key);
bncd1e0aa22015-03-13 10:14:31340
Matt Menke977f02792019-09-10 16:23:09341 // Returns true iff |alternative_service| is currently broken in the context
342 // of |network_isolation_key|. |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26343 bool IsAlternativeServiceBroken(
Matt Menke977f02792019-09-10 16:23:09344 const AlternativeService& alternative_service,
Matt Menkeb32ba5122019-09-10 19:17:05345 const net::NetworkIsolationKey& network_isolation_key) const;
bnc8445b3002015-03-13 01:57:09346
Matt Menke977f02792019-09-10 16:23:09347 // Returns true iff |alternative_service| was recently broken in the context
348 // of |network_isolation_key|. |alternative_service.host| must not be empty.
Matt Menke609160742019-08-02 18:47:26349 bool WasAlternativeServiceRecentlyBroken(
Matt Menke977f02792019-09-10 16:23:09350 const AlternativeService& alternative_service,
Matt Menkeb32ba5122019-09-10 19:17:05351 const net::NetworkIsolationKey& network_isolation_key);
[email protected]f5716e32014-04-18 00:44:16352
Matt Menke977f02792019-09-10 16:23:09353 // Confirms that |alternative_service| is working in the context of
354 // |network_isolation_key|. |alternative_service.host| must not be empty.
355 void ConfirmAlternativeService(
356 const AlternativeService& alternative_service,
Matt Menkeb32ba5122019-09-10 19:17:05357 const net::NetworkIsolationKey& network_isolation_key);
[email protected]f5716e32014-04-18 00:44:16358
Zhongyi Shi826b1d22018-08-28 21:45:15359 // Called when the default network changes.
360 // Clears all the alternative services that were marked broken until the
361 // default network changed.
Matt Menke2f63ef692019-08-02 22:48:03362 void OnDefaultNetworkChanged();
Zhongyi Shi826b1d22018-08-28 21:45:15363
bnc8ba74a1782015-04-14 17:42:08364 // Returns all alternative service mappings as human readable strings.
bncd9b132e2015-07-08 05:16:10365 // Empty alternative service hostnames will be printed as such.
Matt Menke609160742019-08-02 18:47:26366 std::unique_ptr<base::Value> GetAlternativeServiceInfoAsValue() const;
bnc8ba74a1782015-04-14 17:42:08367
Matt Menkeb566c392019-09-11 23:22:43368 // Tracks the last local address when QUIC was known to work. The address
369 // cannot be set to an empty address - use
370 // ClearLastLocalAddressWhenQuicWorked() if it needs to be cleared.
371 bool WasLastLocalAddressWhenQuicWorked(const IPAddress& local_address) const;
372 bool HasLastLocalAddressWhenQuicWorked() const;
373 void SetLastLocalAddressWhenQuicWorked(
374 IPAddress last_local_address_when_quic_worked);
375 void ClearLastLocalAddressWhenQuicWorked();
rtenneti1c863aa2014-09-25 18:39:33376
rchac7f35e2017-03-15 20:42:30377 // Sets |stats| for |server|.
Matt Menke609160742019-08-02 18:47:26378 void SetServerNetworkStats(const url::SchemeHostPort& server,
Matt Menke19475f72019-08-21 18:57:44379 const NetworkIsolationKey& network_isolation_key,
Matt Menke609160742019-08-02 18:47:26380 ServerNetworkStats stats);
[email protected]3b8cf7f2014-01-27 22:08:51381
rchac7f35e2017-03-15 20:42:30382 // Clears any stats for |server|.
Matt Menke19475f72019-08-21 18:57:44383 void ClearServerNetworkStats(
384 const url::SchemeHostPort& server,
385 const NetworkIsolationKey& network_isolation_key);
rchac7f35e2017-03-15 20:42:30386
387 // Returns any stats for |server| or nullptr if there are none.
Matt Menke609160742019-08-02 18:47:26388 const ServerNetworkStats* GetServerNetworkStats(
Matt Menke19475f72019-08-21 18:57:44389 const url::SchemeHostPort& server,
390 const NetworkIsolationKey& network_isolation_key);
rtenneti338cd36a2015-01-06 00:20:07391
Matt Menke0142bc02019-09-13 20:17:45392 // Save QuicServerInfo (in std::string form) for the given |server_id|, in the
393 // context of |network_isolation_key|.
Matt Menke2f63ef692019-08-02 22:48:03394 void SetQuicServerInfo(const quic::QuicServerId& server_id,
Matt Menke0142bc02019-09-13 20:17:45395 const NetworkIsolationKey& network_isolation_key,
Matt Menke609160742019-08-02 18:47:26396 const std::string& server_info);
rtenneti8b673f72015-10-08 23:45:37397
Matt Menke0142bc02019-09-13 20:17:45398 // Get QuicServerInfo (in std::string form) for the given |server_id|, in the
399 // context of |network_isolation_key|.
400 const std::string* GetQuicServerInfo(
401 const quic::QuicServerId& server_id,
402 const NetworkIsolationKey& network_isolation_key);
rtenneti8b673f72015-10-08 23:45:37403
404 // Returns all persistent QuicServerInfo objects.
Matt Menke609160742019-08-02 18:47:26405 const QuicServerInfoMap& quic_server_info_map() const;
rtenneti8b673f72015-10-08 23:45:37406
rtenneti6971c172016-01-15 20:12:10407 // Returns the number of server configs (QuicServerInfo objects) persisted.
Matt Menke609160742019-08-02 18:47:26408 size_t max_server_configs_stored_in_properties() const;
rtenneti6971c172016-01-15 20:12:10409
410 // Sets the number of server configs (QuicServerInfo objects) to be persisted.
Matt Menke609160742019-08-02 18:47:26411 void SetMaxServerConfigsStoredInProperties(
412 size_t max_server_configs_stored_in_properties);
rtenneti6971c172016-01-15 20:12:10413
xunjieli1df4de12017-02-09 17:21:19414 // Returns whether HttpServerProperties is initialized.
Matt Menke609160742019-08-02 18:47:26415 bool IsInitialized() const;
416
417 // BrokenAlternativeServices::Delegate method.
418 void OnExpireBrokenAlternativeService(
Matt Menke9d0e9952019-09-10 16:09:38419 const AlternativeService& expired_alternative_service,
420 const NetworkIsolationKey& network_isolation_key) override;
Matt Menke609160742019-08-02 18:47:26421
422 static base::TimeDelta GetUpdatePrefsDelayForTesting();
423
424 // Test-only routines that call the methods used to load the specified
425 // field(s) from a prefs file. Unlike OnPrefsLoaded(), these may be invoked
426 // multiple times.
Matt Menke5e7dcd32019-08-09 22:25:21427 void OnServerInfoLoadedForTesting(
428 std::unique_ptr<ServerInfoMap> server_info_map) {
429 OnServerInfoLoaded(std::move(server_info_map));
Matt Menke609160742019-08-02 18:47:26430 }
Matt Menkeb566c392019-09-11 23:22:43431 void OnLastLocalAddressWhenQuicWorkedForTesting(
432 const IPAddress& last_local_address_when_quic_worked) {
433 OnLastLocalAddressWhenQuicWorkedLoaded(last_local_address_when_quic_worked);
Matt Menke609160742019-08-02 18:47:26434 }
435 void OnQuicServerInfoMapLoadedForTesting(
436 std::unique_ptr<QuicServerInfoMap> quic_server_info_map) {
437 OnQuicServerInfoMapLoaded(std::move(quic_server_info_map));
438 }
439 void OnBrokenAndRecentlyBrokenAlternativeServicesLoadedForTesting(
440 std::unique_ptr<BrokenAlternativeServiceList>
441 broken_alternative_service_list,
442 std::unique_ptr<RecentlyBrokenAlternativeServices>
443 recently_broken_alternative_services) {
444 OnBrokenAndRecentlyBrokenAlternativeServicesLoaded(
445 std::move(broken_alternative_service_list),
446 std::move(recently_broken_alternative_services));
447 }
448
449 const std::string* GetCanonicalSuffixForTesting(
450 const std::string& host) const {
451 return GetCanonicalSuffix(host);
452 }
453
Matt Menke5e7dcd32019-08-09 22:25:21454 const ServerInfoMap& server_info_map_for_testing() const {
455 return server_info_map_;
Matt Menke609160742019-08-02 18:47:26456 }
457
458 // TODO(mmenke): Look into removing this.
459 HttpServerPropertiesManager* properties_manager_for_testing() {
460 return properties_manager_.get();
461 }
xunjieli1df4de12017-02-09 17:21:19462
[email protected]db96a882011-10-09 02:01:54463 private:
Matt Menke609160742019-08-02 18:47:26464 // TODO (wangyix): modify HttpServerProperties unit tests so this
465 // friendness is no longer required.
466 friend class HttpServerPropertiesPeer;
467
Matt Menke0142bc02019-09-13 20:17:45468 typedef base::flat_map<ServerInfoMapKey, url::SchemeHostPort> CanonicalMap;
469 typedef base::flat_map<QuicServerInfoMapKey, quic::QuicServerId>
470 QuicCanonicalMap;
Matt Menke609160742019-08-02 18:47:26471 typedef std::vector<std::string> CanonicalSuffixList;
Matt Menke609160742019-08-02 18:47:26472
Matt Menked9b24f02019-09-26 17:07:17473 // Internal implementations of public methods. SchemeHostPort argument must be
474 // normalized before calling (ws/wss replaced with http/https). Use wrapped
475 // functions instead of putting the normalization in the public functions to
476 // reduce chance of regression - normalization in ServerInfoMapKey's
477 // constructor would leave |server.scheme| as wrong if not access through the
478 // key, and explicit normalization to create |normalized_server| means the one
479 // with the incorrect scheme would still be available.
480 bool GetSupportsSpdyInternal(
481 url::SchemeHostPort server,
482 const net::NetworkIsolationKey& network_isolation_key);
483 void SetSupportsSpdyInternal(
484 url::SchemeHostPort server,
485 const net::NetworkIsolationKey& network_isolation_key,
486 bool supports_spdy);
487 bool RequiresHTTP11Internal(
488 url::SchemeHostPort server,
489 const net::NetworkIsolationKey& network_isolation_key);
490 void SetHTTP11RequiredInternal(
491 url::SchemeHostPort server,
492 const net::NetworkIsolationKey& network_isolation_key);
493 void MaybeForceHTTP11Internal(
494 url::SchemeHostPort server,
495 const net::NetworkIsolationKey& network_isolation_key,
496 SSLConfig* ssl_config);
497 AlternativeServiceInfoVector GetAlternativeServiceInfosInternal(
498 const url::SchemeHostPort& origin,
499 const net::NetworkIsolationKey& network_isolation_key);
500 void SetAlternativeServicesInternal(
501 const url::SchemeHostPort& origin,
502 const net::NetworkIsolationKey& network_isolation_key,
503 const AlternativeServiceInfoVector& alternative_service_info_vector);
504 void SetServerNetworkStatsInternal(
505 url::SchemeHostPort server,
506 const NetworkIsolationKey& network_isolation_key,
507 ServerNetworkStats stats);
508 void ClearServerNetworkStatsInternal(
509 url::SchemeHostPort server,
510 const NetworkIsolationKey& network_isolation_key);
511 const ServerNetworkStats* GetServerNetworkStatsInternal(
512 url::SchemeHostPort server,
513 const NetworkIsolationKey& network_isolation_key);
514
Matt Menke0142bc02019-09-13 20:17:45515 // Helper functions to use the passed in parameters and
516 // |use_network_isolation_key_| to create a [Quic]ServerInfoMapKey.
Matt Menke1be93d22019-08-20 16:57:58517 ServerInfoMapKey CreateServerInfoKey(
518 const url::SchemeHostPort& server,
Matt Menke04a5a082019-08-21 15:07:07519 const NetworkIsolationKey& network_isolation_key) const;
Matt Menke0142bc02019-09-13 20:17:45520 QuicServerInfoMapKey CreateQuicServerInfoKey(
521 const quic::QuicServerId& server_id,
522 const NetworkIsolationKey& network_isolation_key) const;
Matt Menke1be93d22019-08-20 16:57:58523
Matt Menke04a5a082019-08-21 15:07:07524 // Return the iterator for |server| in the context of |network_isolation_key|,
525 // or for its canonical host, or end. Skips over ServerInfos without
526 // |alternative_service_info| populated.
Matt Menkefe9b5962019-08-14 20:56:14527 ServerInfoMap::const_iterator GetIteratorWithAlternativeServiceInfo(
Matt Menke04a5a082019-08-21 15:07:07528 const url::SchemeHostPort& server,
529 const net::NetworkIsolationKey& network_isolation_key);
Matt Menke609160742019-08-02 18:47:26530
Matt Menke04a5a082019-08-21 15:07:07531 // Return the canonical host for |server| in the context of
532 // |network_isolation_key|, or end if none exists.
Matt Menke0142bc02019-09-13 20:17:45533 CanonicalMap::const_iterator GetCanonicalAltSvcHost(
Matt Menke04a5a082019-08-21 15:07:07534 const url::SchemeHostPort& server,
535 const net::NetworkIsolationKey& network_isolation_key) const;
Matt Menke609160742019-08-02 18:47:26536
537 // Return the canonical host with the same canonical suffix as |server|.
538 // The returned canonical host can be used to search for server info in
539 // |quic_server_info_map_|. Return 'end' the host doesn't exist.
Matt Menke0142bc02019-09-13 20:17:45540 QuicCanonicalMap::const_iterator GetCanonicalServerInfoHost(
541 const QuicServerInfoMapKey& key) const;
Matt Menke609160742019-08-02 18:47:26542
Matt Menke04a5a082019-08-21 15:07:07543 // Remove the canonical alt-svc host for |server| with
544 // |network_isolation_key|.
545 void RemoveAltSvcCanonicalHost(
546 const url::SchemeHostPort& server,
547 const NetworkIsolationKey& network_isolation_key);
Matt Menke609160742019-08-02 18:47:26548
549 // Update |canonical_server_info_map_| with the new canonical host.
Matt Menke0142bc02019-09-13 20:17:45550 // The |key| should have the corresponding server info associated with it
Matt Menke609160742019-08-02 18:47:26551 // in |quic_server_info_map_|. If |canonical_server_info_map_| doesn't
Matt Menke0142bc02019-09-13 20:17:45552 // have an entry associated with |key|, the method will add one.
553 void UpdateCanonicalServerInfoMap(const QuicServerInfoMapKey& key);
Matt Menke609160742019-08-02 18:47:26554
555 // Returns the canonical host suffix for |host|, or nullptr if none
556 // exists.
557 const std::string* GetCanonicalSuffix(const std::string& host) const;
558
Matt Menkeb566c392019-09-11 23:22:43559 void OnPrefsLoaded(std::unique_ptr<ServerInfoMap> server_info_map,
560 const IPAddress& last_local_address_when_quic_worked,
561 std::unique_ptr<QuicServerInfoMap> quic_server_info_map,
562 std::unique_ptr<BrokenAlternativeServiceList>
563 broken_alternative_service_list,
564 std::unique_ptr<RecentlyBrokenAlternativeServices>
565 recently_broken_alternative_services);
Matt Menke609160742019-08-02 18:47:26566
567 // These methods are called by OnPrefsLoaded to handle merging properties
568 // loaded from prefs with what has been learned while waiting for prefs to
569 // load.
Matt Menke5e7dcd32019-08-09 22:25:21570 void OnServerInfoLoaded(std::unique_ptr<ServerInfoMap> server_info_map);
Matt Menkeb566c392019-09-11 23:22:43571 void OnLastLocalAddressWhenQuicWorkedLoaded(
572 const IPAddress& last_local_address_when_quic_worked);
Matt Menke609160742019-08-02 18:47:26573 void OnQuicServerInfoMapLoaded(
574 std::unique_ptr<QuicServerInfoMap> quic_server_info_map);
575 void OnBrokenAndRecentlyBrokenAlternativeServicesLoaded(
576 std::unique_ptr<BrokenAlternativeServiceList>
577 broken_alternative_service_list,
578 std::unique_ptr<RecentlyBrokenAlternativeServices>
579 recently_broken_alternative_services);
580
581 // Queue a delayed call to WriteProperties(). If |is_initialized_| is false,
582 // or |properties_manager_| is nullptr, or there's already a queued call to
583 // WriteProperties(), does nothing.
584 void MaybeQueueWriteProperties();
585
586 // Writes cached state to |properties_manager_|, which must not be null.
587 // Invokes |callback| on completion, if non-null.
588 void WriteProperties(base::OnceClosure callback) const;
589
590 const base::TickClock* tick_clock_; // Unowned
591 base::Clock* clock_; // Unowned
592
Matt Menke1be93d22019-08-20 16:57:58593 // Cached value of kPartitionHttpServerPropertiesByNetworkIsolationKey
594 // feature. Cached to improve performance.
595 const bool use_network_isolation_key_;
596
Matt Menke609160742019-08-02 18:47:26597 // Set to true once initial properties have been retrieved from disk by
598 // |properties_manager_|. Always true if |properties_manager_| is nullptr.
599 bool is_initialized_;
600
Matt Menke723f10292019-08-02 21:13:10601 // Queue a write when resources finish loading. Set to true when
602 // MaybeQueueWriteProperties() is invoked while still waiting on
603 // initialization to complete.
604 bool queue_write_on_load_;
605
Matt Menke609160742019-08-02 18:47:26606 // Used to load/save properties from/to preferences. May be nullptr.
607 std::unique_ptr<HttpServerPropertiesManager> properties_manager_;
608
Matt Menke5e7dcd32019-08-09 22:25:21609 ServerInfoMap server_info_map_;
Matt Menke609160742019-08-02 18:47:26610
Matt Menke609160742019-08-02 18:47:26611 BrokenAlternativeServices broken_alternative_services_;
612
Matt Menkeb566c392019-09-11 23:22:43613 IPAddress last_local_address_when_quic_worked_;
Matt Menke609160742019-08-02 18:47:26614 // Contains a map of servers which could share the same alternate protocol.
Matt Menke0142bc02019-09-13 20:17:45615 // Map from a Canonical scheme/host/port/NIK (host is some postfix of host
616 // names) to an actual origin, which has a plausible alternate protocol
617 // mapping.
618 CanonicalMap canonical_alt_svc_map_;
Matt Menke609160742019-08-02 18:47:26619
620 // Contains list of suffixes (for example ".c.youtube.com",
621 // ".googlevideo.com", ".googleusercontent.com") of canonical hostnames.
622 const CanonicalSuffixList canonical_suffixes_;
623
624 QuicServerInfoMap quic_server_info_map_;
625
626 // Maps canonical suffixes to host names that have the same canonical suffix
627 // and have a corresponding entry in |quic_server_info_map_|. The map can be
628 // used to quickly look for server info for hosts that share the same
629 // canonical suffix but don't have exact match in |quic_server_info_map_|. The
630 // map exists solely to improve the search performance. It only contains
631 // derived data that can be recalculated by traversing
632 // |quic_server_info_map_|.
Matt Menke0142bc02019-09-13 20:17:45633 QuicCanonicalMap canonical_server_info_map_;
Matt Menke609160742019-08-02 18:47:26634
635 size_t max_server_configs_stored_in_properties_;
636
637 // Used to post calls to WriteProperties().
638 base::OneShotTimer prefs_update_timer_;
639
640 THREAD_CHECKER(thread_checker_);
641
[email protected]db96a882011-10-09 02:01:54642 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties);
643};
644
645} // namespace net
646
647#endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_