[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 | |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 256 | // Return all alternative services for |origin|, learned in the context of |
| 257 | // |network_isolation_key|, including broken ones. Returned alternative |
| 258 | // services never have empty hostnames. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 259 | AlternativeServiceInfoVector GetAlternativeServiceInfos( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 260 | const url::SchemeHostPort& origin, |
| 261 | const net::NetworkIsolationKey& network_isolation_key); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 262 | |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 263 | // Set a single HTTP/2 alternative service for |origin|. Previous |
| 264 | // alternative services for |origin| are discarded. |
| 265 | // |alternative_service.host| may be empty. |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame^] | 266 | void SetHttp2AlternativeService( |
| 267 | const url::SchemeHostPort& origin, |
| 268 | const NetworkIsolationKey& network_isolation_key, |
| 269 | const AlternativeService& alternative_service, |
| 270 | base::Time expiration); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 271 | |
| 272 | // Set a single QUIC alternative service for |origin|. Previous alternative |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 273 | // services for |origin| are discarded. |
| 274 | // |alternative_service.host| may be empty. |
Matt Menke | 2f63ef69 | 2019-08-02 22:48:03 | [diff] [blame] | 275 | void SetQuicAlternativeService( |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 276 | const url::SchemeHostPort& origin, |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame^] | 277 | const NetworkIsolationKey& network_isolation_key, |
bnc | cacc099 | 2015-03-20 20:22:22 | [diff] [blame] | 278 | const AlternativeService& alternative_service, |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 279 | base::Time expiration, |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 280 | const quic::ParsedQuicVersionVector& advertised_versions); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 281 | |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 282 | // Set alternative services for |origin|, learned in the context of |
| 283 | // |network_isolation_key|. Previous alternative services for |origin| are |
| 284 | // discarded. Hostnames in |alternative_service_info_vector| may be empty. |
bnc | 4b91d83 | 2016-07-27 23:36:12 | [diff] [blame] | 285 | // |alternative_service_info_vector| may be empty. |
Matt Menke | 2f63ef69 | 2019-08-02 22:48:03 | [diff] [blame] | 286 | void SetAlternativeServices( |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 287 | const url::SchemeHostPort& origin, |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 288 | const net::NetworkIsolationKey& network_isolation_key, |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 289 | const AlternativeServiceInfoVector& alternative_service_info_vector); |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 290 | |
bnc | cacc099 | 2015-03-20 20:22:22 | [diff] [blame] | 291 | // Marks |alternative_service| as broken. |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 292 | // |alternative_service.host| must not be empty. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 293 | void MarkAlternativeServiceBroken( |
| 294 | const AlternativeService& alternative_service); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 295 | |
Zhongyi Shi | 826b1d2 | 2018-08-28 21:45:15 | [diff] [blame] | 296 | // Marks |alternative_service| as broken until the default network changes. |
| 297 | // |alternative_service.host| must not be empty. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 298 | void MarkAlternativeServiceBrokenUntilDefaultNetworkChanges( |
| 299 | const AlternativeService& alternative_service); |
Zhongyi Shi | 826b1d2 | 2018-08-28 21:45:15 | [diff] [blame] | 300 | |
bnc | d1e0aa2 | 2015-03-13 10:14:31 | [diff] [blame] | 301 | // Marks |alternative_service| as recently broken. |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 302 | // |alternative_service.host| must not be empty. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 303 | void MarkAlternativeServiceRecentlyBroken( |
| 304 | const AlternativeService& alternative_service); |
bnc | d1e0aa2 | 2015-03-13 10:14:31 | [diff] [blame] | 305 | |
bnc | 8445b300 | 2015-03-13 01:57:09 | [diff] [blame] | 306 | // Returns true iff |alternative_service| is currently broken. |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 307 | // |alternative_service.host| must not be empty. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 308 | bool IsAlternativeServiceBroken( |
| 309 | const AlternativeService& alternative_service) const; |
bnc | 8445b300 | 2015-03-13 01:57:09 | [diff] [blame] | 310 | |
bnc | cacc099 | 2015-03-20 20:22:22 | [diff] [blame] | 311 | // Returns true iff |alternative_service| was recently broken. |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 312 | // |alternative_service.host| must not be empty. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 313 | bool WasAlternativeServiceRecentlyBroken( |
| 314 | const AlternativeService& alternative_service); |
[email protected] | f5716e3 | 2014-04-18 00:44:16 | [diff] [blame] | 315 | |
bnc | cacc099 | 2015-03-20 20:22:22 | [diff] [blame] | 316 | // Confirms that |alternative_service| is working. |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 317 | // |alternative_service.host| must not be empty. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 318 | void ConfirmAlternativeService(const AlternativeService& alternative_service); |
[email protected] | f5716e3 | 2014-04-18 00:44:16 | [diff] [blame] | 319 | |
Zhongyi Shi | 826b1d2 | 2018-08-28 21:45:15 | [diff] [blame] | 320 | // Called when the default network changes. |
| 321 | // Clears all the alternative services that were marked broken until the |
| 322 | // default network changed. |
Matt Menke | 2f63ef69 | 2019-08-02 22:48:03 | [diff] [blame] | 323 | void OnDefaultNetworkChanged(); |
Zhongyi Shi | 826b1d2 | 2018-08-28 21:45:15 | [diff] [blame] | 324 | |
bnc | 8ba74a178 | 2015-04-14 17:42:08 | [diff] [blame] | 325 | // Returns all alternative service mappings as human readable strings. |
bnc | d9b132e | 2015-07-08 05:16:10 | [diff] [blame] | 326 | // Empty alternative service hostnames will be printed as such. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 327 | std::unique_ptr<base::Value> GetAlternativeServiceInfoAsValue() const; |
bnc | 8ba74a178 | 2015-04-14 17:42:08 | [diff] [blame] | 328 | |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 329 | bool GetSupportsQuic(IPAddress* last_address) const; |
rtenneti | 1c863aa | 2014-09-25 18:39:33 | [diff] [blame] | 330 | |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 331 | void SetSupportsQuic(bool used_quic, const IPAddress& last_address); |
rtenneti | 1c863aa | 2014-09-25 18:39:33 | [diff] [blame] | 332 | |
rch | ac7f35e | 2017-03-15 20:42:30 | [diff] [blame] | 333 | // Sets |stats| for |server|. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 334 | void SetServerNetworkStats(const url::SchemeHostPort& server, |
| 335 | ServerNetworkStats stats); |
[email protected] | 3b8cf7f | 2014-01-27 22:08:51 | [diff] [blame] | 336 | |
rch | ac7f35e | 2017-03-15 20:42:30 | [diff] [blame] | 337 | // Clears any stats for |server|. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 338 | void ClearServerNetworkStats(const url::SchemeHostPort& server); |
rch | ac7f35e | 2017-03-15 20:42:30 | [diff] [blame] | 339 | |
| 340 | // Returns any stats for |server| or nullptr if there are none. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 341 | const ServerNetworkStats* GetServerNetworkStats( |
| 342 | const url::SchemeHostPort& server); |
rtenneti | 338cd36a | 2015-01-06 00:20:07 | [diff] [blame] | 343 | |
rtenneti | 8b673f7 | 2015-10-08 23:45:37 | [diff] [blame] | 344 | // Save QuicServerInfo (in std::string form) for the given |server_id|. |
Matt Menke | 2f63ef69 | 2019-08-02 22:48:03 | [diff] [blame] | 345 | void SetQuicServerInfo(const quic::QuicServerId& server_id, |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 346 | const std::string& server_info); |
rtenneti | 8b673f7 | 2015-10-08 23:45:37 | [diff] [blame] | 347 | |
| 348 | // Get QuicServerInfo (in std::string form) for the given |server_id|. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 349 | const std::string* GetQuicServerInfo(const quic::QuicServerId& server_id); |
rtenneti | 8b673f7 | 2015-10-08 23:45:37 | [diff] [blame] | 350 | |
| 351 | // Returns all persistent QuicServerInfo objects. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 352 | const QuicServerInfoMap& quic_server_info_map() const; |
rtenneti | 8b673f7 | 2015-10-08 23:45:37 | [diff] [blame] | 353 | |
rtenneti | 6971c17 | 2016-01-15 20:12:10 | [diff] [blame] | 354 | // Returns the number of server configs (QuicServerInfo objects) persisted. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 355 | size_t max_server_configs_stored_in_properties() const; |
rtenneti | 6971c17 | 2016-01-15 20:12:10 | [diff] [blame] | 356 | |
| 357 | // Sets the number of server configs (QuicServerInfo objects) to be persisted. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 358 | void SetMaxServerConfigsStoredInProperties( |
| 359 | size_t max_server_configs_stored_in_properties); |
rtenneti | 6971c17 | 2016-01-15 20:12:10 | [diff] [blame] | 360 | |
xunjieli | 1df4de1 | 2017-02-09 17:21:19 | [diff] [blame] | 361 | // Returns whether HttpServerProperties is initialized. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 362 | bool IsInitialized() const; |
| 363 | |
| 364 | // BrokenAlternativeServices::Delegate method. |
| 365 | void OnExpireBrokenAlternativeService( |
| 366 | const AlternativeService& expired_alternative_service) override; |
| 367 | |
| 368 | static base::TimeDelta GetUpdatePrefsDelayForTesting(); |
| 369 | |
| 370 | // Test-only routines that call the methods used to load the specified |
| 371 | // field(s) from a prefs file. Unlike OnPrefsLoaded(), these may be invoked |
| 372 | // multiple times. |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 373 | void OnServerInfoLoadedForTesting( |
| 374 | std::unique_ptr<ServerInfoMap> server_info_map) { |
| 375 | OnServerInfoLoaded(std::move(server_info_map)); |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 376 | } |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 377 | void OnSupportsQuicLoadedForTesting(const IPAddress& last_address) { |
| 378 | OnSupportsQuicLoaded(last_address); |
| 379 | } |
| 380 | void OnQuicServerInfoMapLoadedForTesting( |
| 381 | std::unique_ptr<QuicServerInfoMap> quic_server_info_map) { |
| 382 | OnQuicServerInfoMapLoaded(std::move(quic_server_info_map)); |
| 383 | } |
| 384 | void OnBrokenAndRecentlyBrokenAlternativeServicesLoadedForTesting( |
| 385 | std::unique_ptr<BrokenAlternativeServiceList> |
| 386 | broken_alternative_service_list, |
| 387 | std::unique_ptr<RecentlyBrokenAlternativeServices> |
| 388 | recently_broken_alternative_services) { |
| 389 | OnBrokenAndRecentlyBrokenAlternativeServicesLoaded( |
| 390 | std::move(broken_alternative_service_list), |
| 391 | std::move(recently_broken_alternative_services)); |
| 392 | } |
| 393 | |
| 394 | const std::string* GetCanonicalSuffixForTesting( |
| 395 | const std::string& host) const { |
| 396 | return GetCanonicalSuffix(host); |
| 397 | } |
| 398 | |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 399 | const ServerInfoMap& server_info_map_for_testing() const { |
| 400 | return server_info_map_; |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | // TODO(mmenke): Look into removing this. |
| 404 | HttpServerPropertiesManager* properties_manager_for_testing() { |
| 405 | return properties_manager_.get(); |
| 406 | } |
xunjieli | 1df4de1 | 2017-02-09 17:21:19 | [diff] [blame] | 407 | |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 408 | private: |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 409 | // TODO (wangyix): modify HttpServerProperties unit tests so this |
| 410 | // friendness is no longer required. |
| 411 | friend class HttpServerPropertiesPeer; |
| 412 | |
Matt Menke | 04a5a08 | 2019-08-21 15:07:07 | [diff] [blame] | 413 | typedef base::flat_map<ServerInfoMapKey, url::SchemeHostPort> |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 414 | CanonicalAltSvcMap; |
| 415 | typedef base::flat_map<HostPortPair, quic::QuicServerId> |
| 416 | CanonicalServerInfoMap; |
| 417 | typedef std::vector<std::string> CanonicalSuffixList; |
| 418 | typedef std::set<HostPortPair> Http11ServerHostPortSet; |
| 419 | |
Matt Menke | 1be93d2 | 2019-08-20 16:57:58 | [diff] [blame] | 420 | // Helper function to use the passed in parameters and |
| 421 | // |use_network_isolation_key_| to create a ServerInfoMapKey. |
| 422 | ServerInfoMapKey CreateServerInfoKey( |
| 423 | const url::SchemeHostPort& server, |
Matt Menke | 04a5a08 | 2019-08-21 15:07:07 | [diff] [blame] | 424 | const NetworkIsolationKey& network_isolation_key) const; |
Matt Menke | 1be93d2 | 2019-08-20 16:57:58 | [diff] [blame] | 425 | |
Matt Menke | 04a5a08 | 2019-08-21 15:07:07 | [diff] [blame] | 426 | // Return the iterator for |server| in the context of |network_isolation_key|, |
| 427 | // or for its canonical host, or end. Skips over ServerInfos without |
| 428 | // |alternative_service_info| populated. |
Matt Menke | fe9b596 | 2019-08-14 20:56:14 | [diff] [blame] | 429 | ServerInfoMap::const_iterator GetIteratorWithAlternativeServiceInfo( |
Matt Menke | 04a5a08 | 2019-08-21 15:07:07 | [diff] [blame] | 430 | const url::SchemeHostPort& server, |
| 431 | const net::NetworkIsolationKey& network_isolation_key); |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 432 | |
Matt Menke | 04a5a08 | 2019-08-21 15:07:07 | [diff] [blame] | 433 | // Return the canonical host for |server| in the context of |
| 434 | // |network_isolation_key|, or end if none exists. |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 435 | CanonicalAltSvcMap::const_iterator GetCanonicalAltSvcHost( |
Matt Menke | 04a5a08 | 2019-08-21 15:07:07 | [diff] [blame] | 436 | const url::SchemeHostPort& server, |
| 437 | const net::NetworkIsolationKey& network_isolation_key) const; |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 438 | |
| 439 | // Return the canonical host with the same canonical suffix as |server|. |
| 440 | // The returned canonical host can be used to search for server info in |
| 441 | // |quic_server_info_map_|. Return 'end' the host doesn't exist. |
| 442 | CanonicalServerInfoMap::const_iterator GetCanonicalServerInfoHost( |
| 443 | const quic::QuicServerId& server) const; |
| 444 | |
Matt Menke | 04a5a08 | 2019-08-21 15:07:07 | [diff] [blame] | 445 | // Remove the canonical alt-svc host for |server| with |
| 446 | // |network_isolation_key|. |
| 447 | void RemoveAltSvcCanonicalHost( |
| 448 | const url::SchemeHostPort& server, |
| 449 | const NetworkIsolationKey& network_isolation_key); |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 450 | |
| 451 | // Update |canonical_server_info_map_| with the new canonical host. |
| 452 | // The |server| should have the corresponding server info associated with it |
| 453 | // in |quic_server_info_map_|. If |canonical_server_info_map_| doesn't |
| 454 | // have an entry associated with |server|, the method will add one. |
| 455 | void UpdateCanonicalServerInfoMap(const quic::QuicServerId& server); |
| 456 | |
| 457 | // Returns the canonical host suffix for |host|, or nullptr if none |
| 458 | // exists. |
| 459 | const std::string* GetCanonicalSuffix(const std::string& host) const; |
| 460 | |
| 461 | void OnPrefsLoaded( |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 462 | std::unique_ptr<ServerInfoMap> server_info_map, |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 463 | const IPAddress& last_quic_address, |
| 464 | std::unique_ptr<QuicServerInfoMap> quic_server_info_map, |
| 465 | std::unique_ptr<BrokenAlternativeServiceList> |
| 466 | broken_alternative_service_list, |
| 467 | std::unique_ptr<RecentlyBrokenAlternativeServices> |
Matt Menke | 7932d81 | 2019-08-02 21:26:08 | [diff] [blame] | 468 | recently_broken_alternative_services); |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 469 | |
| 470 | // These methods are called by OnPrefsLoaded to handle merging properties |
| 471 | // loaded from prefs with what has been learned while waiting for prefs to |
| 472 | // load. |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 473 | void OnServerInfoLoaded(std::unique_ptr<ServerInfoMap> server_info_map); |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 474 | void OnSupportsQuicLoaded(const IPAddress& last_address); |
| 475 | void OnQuicServerInfoMapLoaded( |
| 476 | std::unique_ptr<QuicServerInfoMap> quic_server_info_map); |
| 477 | void OnBrokenAndRecentlyBrokenAlternativeServicesLoaded( |
| 478 | std::unique_ptr<BrokenAlternativeServiceList> |
| 479 | broken_alternative_service_list, |
| 480 | std::unique_ptr<RecentlyBrokenAlternativeServices> |
| 481 | recently_broken_alternative_services); |
| 482 | |
| 483 | // Queue a delayed call to WriteProperties(). If |is_initialized_| is false, |
| 484 | // or |properties_manager_| is nullptr, or there's already a queued call to |
| 485 | // WriteProperties(), does nothing. |
| 486 | void MaybeQueueWriteProperties(); |
| 487 | |
| 488 | // Writes cached state to |properties_manager_|, which must not be null. |
| 489 | // Invokes |callback| on completion, if non-null. |
| 490 | void WriteProperties(base::OnceClosure callback) const; |
| 491 | |
| 492 | const base::TickClock* tick_clock_; // Unowned |
| 493 | base::Clock* clock_; // Unowned |
| 494 | |
Matt Menke | 1be93d2 | 2019-08-20 16:57:58 | [diff] [blame] | 495 | // Cached value of kPartitionHttpServerPropertiesByNetworkIsolationKey |
| 496 | // feature. Cached to improve performance. |
| 497 | const bool use_network_isolation_key_; |
| 498 | |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 499 | // Set to true once initial properties have been retrieved from disk by |
| 500 | // |properties_manager_|. Always true if |properties_manager_| is nullptr. |
| 501 | bool is_initialized_; |
| 502 | |
Matt Menke | 723f1029 | 2019-08-02 21:13:10 | [diff] [blame] | 503 | // Queue a write when resources finish loading. Set to true when |
| 504 | // MaybeQueueWriteProperties() is invoked while still waiting on |
| 505 | // initialization to complete. |
| 506 | bool queue_write_on_load_; |
| 507 | |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 508 | // Used to load/save properties from/to preferences. May be nullptr. |
| 509 | std::unique_ptr<HttpServerPropertiesManager> properties_manager_; |
| 510 | |
Matt Menke | 5e7dcd3 | 2019-08-09 22:25:21 | [diff] [blame] | 511 | ServerInfoMap server_info_map_; |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 512 | |
Matt Menke | fe9b596 | 2019-08-14 20:56:14 | [diff] [blame] | 513 | Http11ServerHostPortSet http11_servers_; |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 514 | |
| 515 | BrokenAlternativeServices broken_alternative_services_; |
| 516 | |
| 517 | IPAddress last_quic_address_; |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 518 | // Contains a map of servers which could share the same alternate protocol. |
| 519 | // Map from a Canonical scheme/host/port (host is some postfix of host names) |
| 520 | // to an actual origin, which has a plausible alternate protocol mapping. |
| 521 | CanonicalAltSvcMap canonical_alt_svc_map_; |
| 522 | |
| 523 | // Contains list of suffixes (for example ".c.youtube.com", |
| 524 | // ".googlevideo.com", ".googleusercontent.com") of canonical hostnames. |
| 525 | const CanonicalSuffixList canonical_suffixes_; |
| 526 | |
| 527 | QuicServerInfoMap quic_server_info_map_; |
| 528 | |
| 529 | // Maps canonical suffixes to host names that have the same canonical suffix |
| 530 | // and have a corresponding entry in |quic_server_info_map_|. The map can be |
| 531 | // used to quickly look for server info for hosts that share the same |
| 532 | // canonical suffix but don't have exact match in |quic_server_info_map_|. The |
| 533 | // map exists solely to improve the search performance. It only contains |
| 534 | // derived data that can be recalculated by traversing |
| 535 | // |quic_server_info_map_|. |
| 536 | CanonicalServerInfoMap canonical_server_info_map_; |
| 537 | |
| 538 | size_t max_server_configs_stored_in_properties_; |
| 539 | |
| 540 | // Used to post calls to WriteProperties(). |
| 541 | base::OneShotTimer prefs_update_timer_; |
| 542 | |
| 543 | THREAD_CHECKER(thread_checker_); |
| 544 | |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 545 | DISALLOW_COPY_AND_ASSIGN(HttpServerProperties); |
| 546 | }; |
| 547 | |
| 548 | } // namespace net |
| 549 | |
| 550 | #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_ |