[email protected] | 39c48fc | 2012-03-12 18:42:12 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 2 | // 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 Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 11 | #include <map> |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 12 | #include <memory> |
| 13 | #include <set> |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 14 | #include <string> |
jsbell | cea42a5 | 2015-11-30 23:50:25 | [diff] [blame] | 15 | #include <tuple> |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
Matt Menke | dce5056 | 2017-12-19 22:12:20 | [diff] [blame] | 18 | #include "base/callback.h" |
[email protected] | 9801e370 | 2014-03-07 09:33:55 | [diff] [blame] | 19 | #include "base/containers/mru_cache.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 20 | #include "base/macros.h" |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 21 | #include "base/memory/weak_ptr.h" |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 22 | #include "base/optional.h" |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 23 | #include "base/threading/thread_checker.h" |
[email protected] | 3b8cf7f | 2014-01-27 22:08:51 | [diff] [blame] | 24 | #include "base/time/time.h" |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 25 | #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] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 29 | #include "net/base/net_export.h" |
Matt Menke | 1be93d2 | 2019-08-20 16:57:58 | [diff] [blame] | 30 | #include "net/base/network_isolation_key.h" |
Matt Menke | 2890796e | 2019-08-02 16:55:23 | [diff] [blame] | 31 | #include "net/http/alternative_service.h" |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 32 | #include "net/http/broken_alternative_services.h" |
| 33 | #include "net/http/http_server_properties.h" |
Victor Vasiliev | 6bb59d2 | 2019-03-08 21:34:51 | [diff] [blame] | 34 | #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 Vasiliev | 27cc771 | 2019-01-24 11:50:14 | [diff] [blame] | 37 | #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" |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 39 | #include "url/scheme_host_port.h" |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 40 | |
bnc | 8ba74a178 | 2015-04-14 17:42:08 | [diff] [blame] | 41 | namespace base { |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 42 | class Clock; |
| 43 | class TickClock; |
bnc | 8ba74a178 | 2015-04-14 17:42:08 | [diff] [blame] | 44 | class Value; |
| 45 | } |
| 46 | |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 47 | namespace net { |
| 48 | |
Matt Menke | 2890796e | 2019-08-02 16:55:23 | [diff] [blame] | 49 | class HostPortPair; |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 50 | class HttpServerPropertiesManager; |
martijn | fe9636e | 2016-02-06 14:33:32 | [diff] [blame] | 51 | class IPAddress; |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 52 | class NetLog; |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 53 | struct SSLConfig; |
| 54 | |
rtenneti | 1c863aa | 2014-09-25 18:39:33 | [diff] [blame] | 55 | struct NET_EXPORT SupportsQuic { |
| 56 | SupportsQuic() : used_quic(false) {} |
| 57 | SupportsQuic(bool used_quic, const std::string& address) |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 58 | : used_quic(used_quic), address(address) {} |
rtenneti | 1c863aa | 2014-09-25 18:39:33 | [diff] [blame] | 59 | |
| 60 | bool Equals(const SupportsQuic& other) const { |
| 61 | return used_quic == other.used_quic && address == other.address; |
| 62 | } |
| 63 | |
| 64 | bool used_quic; |
| 65 | std::string address; |
| 66 | }; |
| 67 | |
rtenneti | 338cd36a | 2015-01-06 00:20:07 | [diff] [blame] | 68 | struct NET_EXPORT ServerNetworkStats { |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 69 | ServerNetworkStats() : bandwidth_estimate(quic::QuicBandwidth::Zero()) {} |
rtenneti | 338cd36a | 2015-01-06 00:20:07 | [diff] [blame] | 70 | |
rtenneti | cce34d5 | 2015-06-05 23:36:29 | [diff] [blame] | 71 | bool operator==(const ServerNetworkStats& other) const { |
| 72 | return srtt == other.srtt && bandwidth_estimate == other.bandwidth_estimate; |
| 73 | } |
| 74 | |
| 75 | bool operator!=(const ServerNetworkStats& other) const { |
| 76 | return !this->operator==(other); |
| 77 | } |
| 78 | |
rtenneti | 338cd36a | 2015-01-06 00:20:07 | [diff] [blame] | 79 | base::TimeDelta srtt; |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 80 | quic::QuicBandwidth bandwidth_estimate; |
rtenneti | 338cd36a | 2015-01-06 00:20:07 | [diff] [blame] | 81 | }; |
| 82 | |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 83 | typedef std::vector<AlternativeService> AlternativeServiceVector; |
| 84 | typedef std::vector<AlternativeServiceInfo> AlternativeServiceInfoVector; |
Yixin Wang | 4a227aa2 | 2017-11-30 21:33:01 | [diff] [blame] | 85 | |
Yixin Wang | 4a227aa2 | 2017-11-30 21:33:01 | [diff] [blame] | 86 | // Store at most 200 MRU RecentlyBrokenAlternativeServices in memory and disk. |
Matt Menke | 2890796e | 2019-08-02 16:55:23 | [diff] [blame] | 87 | // This ideally would be with the other constants in HttpServerProperties, but |
| 88 | // has to go here instead of prevent a circular dependency. |
Yixin Wang | 4a227aa2 | 2017-11-30 21:33:01 | [diff] [blame] | 89 | const int kMaxRecentlyBrokenAlternativeServiceEntries = 200; |
| 90 | |
| 91 | // Store at most 5 MRU QUIC servers by default. This is mainly used by cronet. |
| 92 | const int kDefaultMaxQuicServerEntries = 5; |
| 93 | |
Yixin Wang | 4a227aa2 | 2017-11-30 21:33:01 | [diff] [blame] | 94 | // Max number of quic servers to store is not hardcoded and can be set. |
| 95 | // Because of this, QuicServerInfoMap will not be a subclass of MRUCache. |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 96 | typedef base::MRUCache<quic::QuicServerId, std::string> QuicServerInfoMap; |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 97 | |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 98 | // The interface for setting/retrieving the HTTP server properties. |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 99 | // Currently, this class manages servers': |
Bence Béky | ffb237f | 2017-06-29 12:17:39 | [diff] [blame] | 100 | // * HTTP/2 support; |
| 101 | // * Alternative Service support; |
rtenneti | cd2aaa15b | 2015-10-10 20:29:33 | [diff] [blame] | 102 | // * QUIC data (like ServerNetworkStats and QuicServerInfo). |
| 103 | // |
Matt Menke | fe1f1c8 | 2019-08-20 17:49:11 | [diff] [blame^] | 104 | // Optionally retrieves and saves properties from/to disk. This class is not |
| 105 | // threadsafe. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 106 | class NET_EXPORT HttpServerProperties |
| 107 | : public BrokenAlternativeServices::Delegate { |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 108 | public: |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 109 | // Store at most 500 MRU ServerInfos in memory and disk. |
| 110 | static const int kMaxServerInfoEntries = 500; |
| 111 | |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 112 | // Provides an interface to interact with persistent preferences storage |
| 113 | // implemented by the embedder. The prefs are assumed not to have been loaded |
| 114 | // before HttpServerPropertiesManager construction. |
| 115 | class NET_EXPORT PrefDelegate { |
| 116 | public: |
| 117 | virtual ~PrefDelegate(); |
| 118 | |
| 119 | // Returns the branch of the preferences system for the server properties. |
| 120 | // Returns nullptr if the pref system has no data for the server properties. |
| 121 | virtual const base::DictionaryValue* GetServerProperties() const = 0; |
| 122 | |
| 123 | // Sets the server properties to the given value. If |callback| is |
| 124 | // non-empty, flushes data to persistent storage and invokes |callback| |
| 125 | // asynchronously when complete. |
| 126 | virtual void SetServerProperties(const base::DictionaryValue& value, |
| 127 | base::OnceClosure callback) = 0; |
| 128 | |
| 129 | // Starts listening for prefs to be loaded. If prefs are already loaded, |
| 130 | // |pref_loaded_callback| will be invoked asynchronously. Callback will be |
| 131 | // invoked even if prefs fail to load. Will only be called once by the |
| 132 | // HttpServerPropertiesManager. |
| 133 | virtual void WaitForPrefLoad(base::OnceClosure pref_loaded_callback) = 0; |
| 134 | }; |
| 135 | |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 136 | // Contains metadata about a particular server. |
| 137 | struct NET_EXPORT ServerInfo { |
| 138 | ServerInfo(); |
| 139 | ServerInfo(const ServerInfo& server_info); |
| 140 | ServerInfo(ServerInfo&& server_info); |
| 141 | ~ServerInfo(); |
| 142 | |
Matt Menke | fe9b596 | 2019-08-14 20:56:14 | [diff] [blame] | 143 | // Returns true if no fields are populated. |
| 144 | bool empty() const; |
| 145 | |
Matt Menke | 1be93d2 | 2019-08-20 16:57:58 | [diff] [blame] | 146 | // Used in tests. |
| 147 | bool operator==(const ServerInfo& other) const; |
| 148 | |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 149 | // IMPORTANT: When adding a field here, be sure to update |
| 150 | // HttpServerProperties::OnServerInfoLoaded() as well as |
| 151 | // HttpServerPropertiesManager to correctly load/save the from/to the pref |
| 152 | // store. |
| 153 | |
| 154 | // Whether or not a server is known to support H2/SPDY. False indicates |
| 155 | // known lack of support, true indicates known support, and not set |
| 156 | // indicates unknown. The difference between false and not set only matters |
| 157 | // when loading from disk, when an initialized false value will take |
| 158 | // priority over a not set value. |
| 159 | base::Optional<bool> supports_spdy; |
| 160 | |
Matt Menke | fe9b596 | 2019-08-14 20:56:14 | [diff] [blame] | 161 | base::Optional<AlternativeServiceInfoVector> alternative_services; |
Matt Menke | 86878a6 | 2019-08-14 21:01:11 | [diff] [blame] | 162 | base::Optional<ServerNetworkStats> server_network_stats; |
| 163 | |
| 164 | // TODO(mmenke): Add other per-server data as well |
| 165 | // (Http11ServerHostPortSet, QUIC server info). |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 166 | }; |
| 167 | |
Matt Menke | 1be93d2 | 2019-08-20 16:57:58 | [diff] [blame] | 168 | struct NET_EXPORT ServerInfoMapKey { |
| 169 | // If |use_network_isolation_key| is false, an empty NetworkIsolationKey is |
| 170 | // used instead of |network_isolation_key|. |
| 171 | ServerInfoMapKey(const url::SchemeHostPort& server, |
| 172 | const NetworkIsolationKey& network_isolation_key, |
| 173 | bool use_network_isolation_key); |
| 174 | ~ServerInfoMapKey(); |
| 175 | |
| 176 | bool operator<(const ServerInfoMapKey& other) const; |
| 177 | |
| 178 | url::SchemeHostPort server; |
| 179 | NetworkIsolationKey network_isolation_key; |
| 180 | }; |
| 181 | |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 182 | class NET_EXPORT ServerInfoMap |
Matt Menke | 1be93d2 | 2019-08-20 16:57:58 | [diff] [blame] | 183 | : public base::MRUCache<ServerInfoMapKey, ServerInfo> { |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 184 | public: |
| 185 | ServerInfoMap(); |
| 186 | |
| 187 | // If there's an entry corresponding to |key|, brings that entry to the |
| 188 | // front and returns an iterator to it. Otherwise, inserts an empty |
| 189 | // ServerInfo using |key|, and returns an iterator to it. |
Matt Menke | 1be93d2 | 2019-08-20 16:57:58 | [diff] [blame] | 190 | iterator GetOrPut(const ServerInfoMapKey& key); |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 191 | |
Matt Menke | fe9b596 | 2019-08-14 20:56:14 | [diff] [blame] | 192 | // Erases the ServerInfo identified by |server_info_it| if no fields have |
| 193 | // data. The iterator must point to an entry in the map. Regardless of |
| 194 | // whether the entry is removed or not, returns iterator for the next entry. |
| 195 | iterator EraseIfEmpty(iterator server_info_it); |
| 196 | |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 197 | private: |
| 198 | DISALLOW_COPY_AND_ASSIGN(ServerInfoMap); |
| 199 | }; |
| 200 | |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 201 | // If a |pref_delegate| is specified, it will be used to read/write the |
| 202 | // properties to a pref file. Writes are rate limited to improve performance. |
| 203 | // |
| 204 | // |tick_clock| is used for setting expiration times and scheduling the |
| 205 | // expiration of broken alternative services. If null, default clock will be |
| 206 | // used. |
| 207 | // |
| 208 | // |clock| is used for converting base::TimeTicks to base::Time for |
| 209 | // wherever base::Time is preferable. |
| 210 | HttpServerProperties(std::unique_ptr<PrefDelegate> pref_delegate = nullptr, |
| 211 | NetLog* net_log = nullptr, |
| 212 | const base::TickClock* tick_clock = nullptr, |
| 213 | base::Clock* clock = nullptr); |
| 214 | |
| 215 | ~HttpServerProperties() override; |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 216 | |
Matt Menke | dce5056 | 2017-12-19 22:12:20 | [diff] [blame] | 217 | // Deletes all data. If |callback| is non-null, flushes data to disk |
| 218 | // and invokes the callback asynchronously once changes have been written to |
| 219 | // disk. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 220 | void Clear(base::OnceClosure callback); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 221 | |
Matt Menke | fe1f1c8 | 2019-08-20 17:49:11 | [diff] [blame^] | 222 | // Returns true if |server|, in the context of |network_isolation_key|, has |
| 223 | // previously supported a network protocol which honors request |
| 224 | // prioritization. |
| 225 | // |
rdsmith | c31e060 | 2016-08-30 06:27:23 | [diff] [blame] | 226 | // Note that this also implies that the server supports request |
| 227 | // multiplexing, since priorities imply a relationship between |
| 228 | // multiple requests. |
Matt Menke | fe1f1c8 | 2019-08-20 17:49:11 | [diff] [blame^] | 229 | bool SupportsRequestPriority( |
| 230 | const url::SchemeHostPort& server, |
| 231 | const net::NetworkIsolationKey& network_isolation_key); |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 232 | |
rtenneti | e267d6a | 2015-06-05 21:55:23 | [diff] [blame] | 233 | // Returns the value set by SetSupportsSpdy(). If not set, returns false. |
Matt Menke | fe1f1c8 | 2019-08-20 17:49:11 | [diff] [blame^] | 234 | bool GetSupportsSpdy(const url::SchemeHostPort& server, |
| 235 | const net::NetworkIsolationKey& network_isolation_key); |
rtenneti | e267d6a | 2015-06-05 21:55:23 | [diff] [blame] | 236 | |
Matt Menke | fe1f1c8 | 2019-08-20 17:49:11 | [diff] [blame^] | 237 | // Records whether |server| supports H2 or not. Information is restricted to |
| 238 | // the context of |network_isolation_key|, to prevent cross-site information |
| 239 | // leakage. |
| 240 | void SetSupportsSpdy(const url::SchemeHostPort& server, |
| 241 | const net::NetworkIsolationKey& network_isolation_key, |
| 242 | bool supports_spdy); |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 243 | |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 244 | // Returns true if |server| has required HTTP/1.1 via HTTP/2 error code. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 245 | bool RequiresHTTP11(const HostPortPair& server); |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 246 | |
| 247 | // Require HTTP/1.1 on subsequent connections. Not persisted. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 248 | void SetHTTP11Required(const HostPortPair& server); |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 249 | |
| 250 | // Modify SSLConfig to force HTTP/1.1. |
| 251 | static void ForceHTTP11(SSLConfig* ssl_config); |
| 252 | |
| 253 | // Modify SSLConfig to force HTTP/1.1 if necessary. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 254 | void MaybeForceHTTP11(const HostPortPair& server, SSLConfig* ssl_config); |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 255 | |
rch | dc7b905 | 2016-03-17 20:51:50 | [diff] [blame] | 256 | // Return all alternative services for |origin|, including broken ones. |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 257 | // Returned alternative services never have empty hostnames. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 258 | AlternativeServiceInfoVector GetAlternativeServiceInfos( |
| 259 | const url::SchemeHostPort& origin); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 260 | |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 261 | // Set a single HTTP/2 alternative service for |origin|. Previous |
| 262 | // alternative services for |origin| are discarded. |
| 263 | // |alternative_service.host| may be empty. |
Matt Menke | 2f63ef69 | 2019-08-02 22:48:03 | [diff] [blame] | 264 | void SetHttp2AlternativeService(const url::SchemeHostPort& origin, |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 265 | const AlternativeService& alternative_service, |
| 266 | base::Time expiration); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 267 | |
| 268 | // Set a single QUIC alternative service for |origin|. Previous alternative |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 269 | // services for |origin| are discarded. |
| 270 | // |alternative_service.host| may be empty. |
Matt Menke | 2f63ef69 | 2019-08-02 22:48:03 | [diff] [blame] | 271 | void SetQuicAlternativeService( |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 272 | const url::SchemeHostPort& origin, |
bnc | cacc099 | 2015-03-20 20:22:22 | [diff] [blame] | 273 | const AlternativeService& alternative_service, |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 274 | base::Time expiration, |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 275 | const quic::ParsedQuicVersionVector& advertised_versions); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 276 | |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 277 | // Set alternative services for |origin|. Previous alternative services for |
| 278 | // |origin| are discarded. |
| 279 | // Hostnames in |alternative_service_info_vector| may be empty. |
bnc | 4b91d83 | 2016-07-27 23:36:12 | [diff] [blame] | 280 | // |alternative_service_info_vector| may be empty. |
Matt Menke | 2f63ef69 | 2019-08-02 22:48:03 | [diff] [blame] | 281 | void SetAlternativeServices( |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 282 | const url::SchemeHostPort& origin, |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 283 | const AlternativeServiceInfoVector& alternative_service_info_vector); |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 284 | |
bnc | cacc099 | 2015-03-20 20:22:22 | [diff] [blame] | 285 | // Marks |alternative_service| as broken. |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 286 | // |alternative_service.host| must not be empty. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 287 | void MarkAlternativeServiceBroken( |
| 288 | const AlternativeService& alternative_service); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 289 | |
Zhongyi Shi | 826b1d2 | 2018-08-28 21:45:15 | [diff] [blame] | 290 | // Marks |alternative_service| as broken until the default network changes. |
| 291 | // |alternative_service.host| must not be empty. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 292 | void MarkAlternativeServiceBrokenUntilDefaultNetworkChanges( |
| 293 | const AlternativeService& alternative_service); |
Zhongyi Shi | 826b1d2 | 2018-08-28 21:45:15 | [diff] [blame] | 294 | |
bnc | d1e0aa2 | 2015-03-13 10:14:31 | [diff] [blame] | 295 | // Marks |alternative_service| as recently broken. |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 296 | // |alternative_service.host| must not be empty. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 297 | void MarkAlternativeServiceRecentlyBroken( |
| 298 | const AlternativeService& alternative_service); |
bnc | d1e0aa2 | 2015-03-13 10:14:31 | [diff] [blame] | 299 | |
bnc | 8445b300 | 2015-03-13 01:57:09 | [diff] [blame] | 300 | // Returns true iff |alternative_service| is currently broken. |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 301 | // |alternative_service.host| must not be empty. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 302 | bool IsAlternativeServiceBroken( |
| 303 | const AlternativeService& alternative_service) const; |
bnc | 8445b300 | 2015-03-13 01:57:09 | [diff] [blame] | 304 | |
bnc | cacc099 | 2015-03-20 20:22:22 | [diff] [blame] | 305 | // Returns true iff |alternative_service| was recently broken. |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 306 | // |alternative_service.host| must not be empty. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 307 | bool WasAlternativeServiceRecentlyBroken( |
| 308 | const AlternativeService& alternative_service); |
[email protected] | f5716e3 | 2014-04-18 00:44:16 | [diff] [blame] | 309 | |
bnc | cacc099 | 2015-03-20 20:22:22 | [diff] [blame] | 310 | // Confirms that |alternative_service| is working. |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 311 | // |alternative_service.host| must not be empty. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 312 | void ConfirmAlternativeService(const AlternativeService& alternative_service); |
[email protected] | f5716e3 | 2014-04-18 00:44:16 | [diff] [blame] | 313 | |
Zhongyi Shi | 826b1d2 | 2018-08-28 21:45:15 | [diff] [blame] | 314 | // Called when the default network changes. |
| 315 | // Clears all the alternative services that were marked broken until the |
| 316 | // default network changed. |
Matt Menke | 2f63ef69 | 2019-08-02 22:48:03 | [diff] [blame] | 317 | void OnDefaultNetworkChanged(); |
Zhongyi Shi | 826b1d2 | 2018-08-28 21:45:15 | [diff] [blame] | 318 | |
bnc | 8ba74a178 | 2015-04-14 17:42:08 | [diff] [blame] | 319 | // Returns all alternative service mappings as human readable strings. |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 320 | // Empty alternative service hostnames will be printed as such. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 321 | std::unique_ptr<base::Value> GetAlternativeServiceInfoAsValue() const; |
bnc | 8ba74a178 | 2015-04-14 17:42:08 | [diff] [blame] | 322 | |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 323 | bool GetSupportsQuic(IPAddress* last_address) const; |
rtenneti | 1c863aa | 2014-09-25 18:39:33 | [diff] [blame] | 324 | |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 325 | void SetSupportsQuic(bool used_quic, const IPAddress& last_address); |
rtenneti | 1c863aa | 2014-09-25 18:39:33 | [diff] [blame] | 326 | |
rch | ac7f35e | 2017-03-15 20:42:30 | [diff] [blame] | 327 | // Sets |stats| for |server|. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 328 | void SetServerNetworkStats(const url::SchemeHostPort& server, |
| 329 | ServerNetworkStats stats); |
[email protected] | 3b8cf7f | 2014-01-27 22:08:51 | [diff] [blame] | 330 | |
rch | ac7f35e | 2017-03-15 20:42:30 | [diff] [blame] | 331 | // Clears any stats for |server|. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 332 | void ClearServerNetworkStats(const url::SchemeHostPort& server); |
rch | ac7f35e | 2017-03-15 20:42:30 | [diff] [blame] | 333 | |
| 334 | // Returns any stats for |server| or nullptr if there are none. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 335 | const ServerNetworkStats* GetServerNetworkStats( |
| 336 | const url::SchemeHostPort& server); |
rtenneti | 338cd36a | 2015-01-06 00:20:07 | [diff] [blame] | 337 | |
rtenneti | 8b673f7 | 2015-10-08 23:45:37 | [diff] [blame] | 338 | // Save QuicServerInfo (in std::string form) for the given |server_id|. |
Matt Menke | 2f63ef69 | 2019-08-02 22:48:03 | [diff] [blame] | 339 | void SetQuicServerInfo(const quic::QuicServerId& server_id, |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 340 | const std::string& server_info); |
rtenneti | 8b673f7 | 2015-10-08 23:45:37 | [diff] [blame] | 341 | |
| 342 | // Get QuicServerInfo (in std::string form) for the given |server_id|. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 343 | const std::string* GetQuicServerInfo(const quic::QuicServerId& server_id); |
rtenneti | 8b673f7 | 2015-10-08 23:45:37 | [diff] [blame] | 344 | |
| 345 | // Returns all persistent QuicServerInfo objects. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 346 | const QuicServerInfoMap& quic_server_info_map() const; |
rtenneti | 8b673f7 | 2015-10-08 23:45:37 | [diff] [blame] | 347 | |
rtenneti | 6971c17 | 2016-01-15 20:12:10 | [diff] [blame] | 348 | // Returns the number of server configs (QuicServerInfo objects) persisted. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 349 | size_t max_server_configs_stored_in_properties() const; |
rtenneti | 6971c17 | 2016-01-15 20:12:10 | [diff] [blame] | 350 | |
| 351 | // Sets the number of server configs (QuicServerInfo objects) to be persisted. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 352 | void SetMaxServerConfigsStoredInProperties( |
| 353 | size_t max_server_configs_stored_in_properties); |
rtenneti | 6971c17 | 2016-01-15 20:12:10 | [diff] [blame] | 354 | |
xunjieli | 1df4de1 | 2017-02-09 17:21:19 | [diff] [blame] | 355 | // Returns whether HttpServerProperties is initialized. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 356 | bool IsInitialized() const; |
| 357 | |
| 358 | // BrokenAlternativeServices::Delegate method. |
| 359 | void OnExpireBrokenAlternativeService( |
| 360 | const AlternativeService& expired_alternative_service) override; |
| 361 | |
| 362 | static base::TimeDelta GetUpdatePrefsDelayForTesting(); |
| 363 | |
| 364 | // Test-only routines that call the methods used to load the specified |
| 365 | // field(s) from a prefs file. Unlike OnPrefsLoaded(), these may be invoked |
| 366 | // multiple times. |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 367 | void OnServerInfoLoadedForTesting( |
| 368 | std::unique_ptr<ServerInfoMap> server_info_map) { |
| 369 | OnServerInfoLoaded(std::move(server_info_map)); |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 370 | } |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 371 | void OnSupportsQuicLoadedForTesting(const IPAddress& last_address) { |
| 372 | OnSupportsQuicLoaded(last_address); |
| 373 | } |
| 374 | void OnQuicServerInfoMapLoadedForTesting( |
| 375 | std::unique_ptr<QuicServerInfoMap> quic_server_info_map) { |
| 376 | OnQuicServerInfoMapLoaded(std::move(quic_server_info_map)); |
| 377 | } |
| 378 | void OnBrokenAndRecentlyBrokenAlternativeServicesLoadedForTesting( |
| 379 | std::unique_ptr<BrokenAlternativeServiceList> |
| 380 | broken_alternative_service_list, |
| 381 | std::unique_ptr<RecentlyBrokenAlternativeServices> |
| 382 | recently_broken_alternative_services) { |
| 383 | OnBrokenAndRecentlyBrokenAlternativeServicesLoaded( |
| 384 | std::move(broken_alternative_service_list), |
| 385 | std::move(recently_broken_alternative_services)); |
| 386 | } |
| 387 | |
| 388 | const std::string* GetCanonicalSuffixForTesting( |
| 389 | const std::string& host) const { |
| 390 | return GetCanonicalSuffix(host); |
| 391 | } |
| 392 | |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 393 | const ServerInfoMap& server_info_map_for_testing() const { |
| 394 | return server_info_map_; |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // TODO(mmenke): Look into removing this. |
| 398 | HttpServerPropertiesManager* properties_manager_for_testing() { |
| 399 | return properties_manager_.get(); |
| 400 | } |
xunjieli | 1df4de1 | 2017-02-09 17:21:19 | [diff] [blame] | 401 | |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 402 | private: |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 403 | // TODO (wangyix): modify HttpServerProperties unit tests so this |
| 404 | // friendness is no longer required. |
| 405 | friend class HttpServerPropertiesPeer; |
| 406 | |
| 407 | typedef base::flat_map<url::SchemeHostPort, url::SchemeHostPort> |
| 408 | CanonicalAltSvcMap; |
| 409 | typedef base::flat_map<HostPortPair, quic::QuicServerId> |
| 410 | CanonicalServerInfoMap; |
| 411 | typedef std::vector<std::string> CanonicalSuffixList; |
| 412 | typedef std::set<HostPortPair> Http11ServerHostPortSet; |
| 413 | |
Matt Menke | 1be93d2 | 2019-08-20 16:57:58 | [diff] [blame] | 414 | // Helper function to use the passed in parameters and |
| 415 | // |use_network_isolation_key_| to create a ServerInfoMapKey. |
| 416 | ServerInfoMapKey CreateServerInfoKey( |
| 417 | const url::SchemeHostPort& server, |
| 418 | const NetworkIsolationKey& network_isolation_key); |
| 419 | |
Matt Menke | fe9b596 | 2019-08-14 20:56:14 | [diff] [blame] | 420 | // Return the iterator for |server|, or for its canonical host, or end. Skips |
| 421 | // over ServerInfos without |alternative_service_info| populated. |
| 422 | ServerInfoMap::const_iterator GetIteratorWithAlternativeServiceInfo( |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 423 | const url::SchemeHostPort& server); |
| 424 | |
| 425 | // Return the canonical host for |server|, or end if none exists. |
| 426 | CanonicalAltSvcMap::const_iterator GetCanonicalAltSvcHost( |
| 427 | const url::SchemeHostPort& server) const; |
| 428 | |
| 429 | // Return the canonical host with the same canonical suffix as |server|. |
| 430 | // The returned canonical host can be used to search for server info in |
| 431 | // |quic_server_info_map_|. Return 'end' the host doesn't exist. |
| 432 | CanonicalServerInfoMap::const_iterator GetCanonicalServerInfoHost( |
| 433 | const quic::QuicServerId& server) const; |
| 434 | |
| 435 | // Remove the canonical alt-svc host for |server|. |
| 436 | void RemoveAltSvcCanonicalHost(const url::SchemeHostPort& server); |
| 437 | |
| 438 | // Update |canonical_server_info_map_| with the new canonical host. |
| 439 | // The |server| should have the corresponding server info associated with it |
| 440 | // in |quic_server_info_map_|. If |canonical_server_info_map_| doesn't |
| 441 | // have an entry associated with |server|, the method will add one. |
| 442 | void UpdateCanonicalServerInfoMap(const quic::QuicServerId& server); |
| 443 | |
| 444 | // Returns the canonical host suffix for |host|, or nullptr if none |
| 445 | // exists. |
| 446 | const std::string* GetCanonicalSuffix(const std::string& host) const; |
| 447 | |
| 448 | void OnPrefsLoaded( |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 449 | std::unique_ptr<ServerInfoMap> server_info_map, |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 450 | const IPAddress& last_quic_address, |
| 451 | std::unique_ptr<QuicServerInfoMap> quic_server_info_map, |
| 452 | std::unique_ptr<BrokenAlternativeServiceList> |
| 453 | broken_alternative_service_list, |
| 454 | std::unique_ptr<RecentlyBrokenAlternativeServices> |
Matt Menke | 7932d81 | 2019-08-02 21:26:08 | [diff] [blame] | 455 | recently_broken_alternative_services); |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 456 | |
| 457 | // These methods are called by OnPrefsLoaded to handle merging properties |
| 458 | // loaded from prefs with what has been learned while waiting for prefs to |
| 459 | // load. |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 460 | void OnServerInfoLoaded(std::unique_ptr<ServerInfoMap> server_info_map); |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 461 | void OnSupportsQuicLoaded(const IPAddress& last_address); |
| 462 | void OnQuicServerInfoMapLoaded( |
| 463 | std::unique_ptr<QuicServerInfoMap> quic_server_info_map); |
| 464 | void OnBrokenAndRecentlyBrokenAlternativeServicesLoaded( |
| 465 | std::unique_ptr<BrokenAlternativeServiceList> |
| 466 | broken_alternative_service_list, |
| 467 | std::unique_ptr<RecentlyBrokenAlternativeServices> |
| 468 | recently_broken_alternative_services); |
| 469 | |
| 470 | // Queue a delayed call to WriteProperties(). If |is_initialized_| is false, |
| 471 | // or |properties_manager_| is nullptr, or there's already a queued call to |
| 472 | // WriteProperties(), does nothing. |
| 473 | void MaybeQueueWriteProperties(); |
| 474 | |
| 475 | // Writes cached state to |properties_manager_|, which must not be null. |
| 476 | // Invokes |callback| on completion, if non-null. |
| 477 | void WriteProperties(base::OnceClosure callback) const; |
| 478 | |
| 479 | const base::TickClock* tick_clock_; // Unowned |
| 480 | base::Clock* clock_; // Unowned |
| 481 | |
Matt Menke | 1be93d2 | 2019-08-20 16:57:58 | [diff] [blame] | 482 | // Cached value of kPartitionHttpServerPropertiesByNetworkIsolationKey |
| 483 | // feature. Cached to improve performance. |
| 484 | const bool use_network_isolation_key_; |
| 485 | |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 486 | // Set to true once initial properties have been retrieved from disk by |
| 487 | // |properties_manager_|. Always true if |properties_manager_| is nullptr. |
| 488 | bool is_initialized_; |
| 489 | |
Matt Menke | 723f1029 | 2019-08-02 21:13:10 | [diff] [blame] | 490 | // Queue a write when resources finish loading. Set to true when |
| 491 | // MaybeQueueWriteProperties() is invoked while still waiting on |
| 492 | // initialization to complete. |
| 493 | bool queue_write_on_load_; |
| 494 | |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 495 | // Used to load/save properties from/to preferences. May be nullptr. |
| 496 | std::unique_ptr<HttpServerPropertiesManager> properties_manager_; |
| 497 | |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 498 | ServerInfoMap server_info_map_; |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 499 | |
Matt Menke | fe9b596 | 2019-08-14 20:56:14 | [diff] [blame] | 500 | Http11ServerHostPortSet http11_servers_; |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 501 | |
| 502 | BrokenAlternativeServices broken_alternative_services_; |
| 503 | |
| 504 | IPAddress last_quic_address_; |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 505 | // Contains a map of servers which could share the same alternate protocol. |
| 506 | // Map from a Canonical scheme/host/port (host is some postfix of host names) |
| 507 | // to an actual origin, which has a plausible alternate protocol mapping. |
| 508 | CanonicalAltSvcMap canonical_alt_svc_map_; |
| 509 | |
| 510 | // Contains list of suffixes (for example ".c.youtube.com", |
| 511 | // ".googlevideo.com", ".googleusercontent.com") of canonical hostnames. |
| 512 | const CanonicalSuffixList canonical_suffixes_; |
| 513 | |
| 514 | QuicServerInfoMap quic_server_info_map_; |
| 515 | |
| 516 | // Maps canonical suffixes to host names that have the same canonical suffix |
| 517 | // and have a corresponding entry in |quic_server_info_map_|. The map can be |
| 518 | // used to quickly look for server info for hosts that share the same |
| 519 | // canonical suffix but don't have exact match in |quic_server_info_map_|. The |
| 520 | // map exists solely to improve the search performance. It only contains |
| 521 | // derived data that can be recalculated by traversing |
| 522 | // |quic_server_info_map_|. |
| 523 | CanonicalServerInfoMap canonical_server_info_map_; |
| 524 | |
| 525 | size_t max_server_configs_stored_in_properties_; |
| 526 | |
| 527 | // Used to post calls to WriteProperties(). |
| 528 | base::OneShotTimer prefs_update_timer_; |
| 529 | |
| 530 | THREAD_CHECKER(thread_checker_); |
| 531 | |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 532 | DISALLOW_COPY_AND_ASSIGN(HttpServerProperties); |
| 533 | }; |
| 534 | |
| 535 | } // namespace net |
| 536 | |
| 537 | #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_ |