1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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_IMPL_H_ 6 #define NET_HTTP_HTTP_SERVER_PROPERTIES_IMPL_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/containers/hash_tables.h" 14 #include "base/gtest_prod_util.h" 15 #include "base/threading/non_thread_safe.h" 16 #include "base/values.h" 17 #include "net/base/host_port_pair.h" 18 #include "net/base/net_export.h" 19 #include "net/http/http_server_properties.h" 20 21 namespace base { 22 class ListValue; 23 } 24 25 namespace net { 26 27 // The implementation for setting/retrieving the HTTP server properties. 28 class NET_EXPORT HttpServerPropertiesImpl 29 : public HttpServerProperties, 30 NON_EXPORTED_BASE(public base::NonThreadSafe) { 31 public: 32 HttpServerPropertiesImpl(); 33 virtual ~HttpServerPropertiesImpl(); 34 35 // Initializes |spdy_servers_map_| with the servers (host/port) from 36 // |spdy_servers| that either support SPDY or not. 37 void InitializeSpdyServers(std::vector<std::string>* spdy_servers, 38 bool support_spdy); 39 40 void InitializeAlternateProtocolServers( 41 AlternateProtocolMap* alternate_protocol_servers); 42 43 void InitializeSpdySettingsServers(SpdySettingsMap* spdy_settings_map); 44 45 void InitializeSupportsQuic(SupportsQuicMap* supports_quic_map); 46 47 // Get the list of servers (host/port) that support SPDY. The max_size is the 48 // number of MRU servers that support SPDY that are to be returned. 49 void GetSpdyServerList(base::ListValue* spdy_server_list, 50 size_t max_size) const; 51 52 // Returns flattened string representation of the |host_port_pair|. Used by 53 // unittests. 54 static std::string GetFlattenedSpdyServer( 55 const net::HostPortPair& host_port_pair); 56 57 // Debugging to simulate presence of an AlternateProtocol. 58 // If we don't have an alternate protocol in the map for any given host/port 59 // pair, force this ProtocolPortPair. 60 static void ForceAlternateProtocol(const AlternateProtocolInfo& pair); 61 static void DisableForcedAlternateProtocol(); 62 63 // Returns the canonical host suffix for |server|, or std::string() if none 64 // exists. 65 std::string GetCanonicalSuffix(const net::HostPortPair& server); 66 67 // ----------------------------- 68 // HttpServerProperties methods: 69 // ----------------------------- 70 71 // Gets a weak pointer for this object. 72 virtual base::WeakPtr<HttpServerProperties> GetWeakPtr() OVERRIDE; 73 74 // Deletes all data. 75 virtual void Clear() OVERRIDE; 76 77 // Returns true if |server| supports SPDY. 78 virtual bool SupportsSpdy(const HostPortPair& server) OVERRIDE; 79 80 // Add |server| into the persistent store. 81 virtual void SetSupportsSpdy(const HostPortPair& server, 82 bool support_spdy) OVERRIDE; 83 84 // Returns true if |server| has an Alternate-Protocol header. 85 virtual bool HasAlternateProtocol(const HostPortPair& server) OVERRIDE; 86 87 // Returns the Alternate-Protocol and port for |server|. 88 // HasAlternateProtocol(server) must be true. 89 virtual AlternateProtocolInfo GetAlternateProtocol( 90 const HostPortPair& server) OVERRIDE; 91 92 // Sets the Alternate-Protocol for |server|. 93 virtual void SetAlternateProtocol( 94 const HostPortPair& server, 95 uint16 alternate_port, 96 AlternateProtocol alternate_protocol, 97 double probability) OVERRIDE; 98 99 // Sets the Alternate-Protocol for |server| to be BROKEN. 100 virtual void SetBrokenAlternateProtocol(const HostPortPair& server) OVERRIDE; 101 102 // Returns true if Alternate-Protocol for |server| was recently BROKEN. 103 virtual bool WasAlternateProtocolRecentlyBroken( 104 const HostPortPair& server) OVERRIDE; 105 106 // Confirms that Alternate-Protocol for |server| is working. 107 virtual void ConfirmAlternateProtocol(const HostPortPair& server) OVERRIDE; 108 109 // Clears the Alternate-Protocol for |server|. 110 virtual void ClearAlternateProtocol(const HostPortPair& server) OVERRIDE; 111 112 // Returns all Alternate-Protocol mappings. 113 virtual const AlternateProtocolMap& alternate_protocol_map() const OVERRIDE; 114 115 virtual void SetAlternateProtocolExperiment( 116 AlternateProtocolExperiment experiment) OVERRIDE; 117 118 virtual void SetAlternateProtocolProbabilityThreshold( 119 double threshold) OVERRIDE; 120 121 virtual AlternateProtocolExperiment GetAlternateProtocolExperiment() 122 const OVERRIDE; 123 124 // Gets a reference to the SettingsMap stored for a host. 125 // If no settings are stored, returns an empty SettingsMap. 126 virtual const SettingsMap& GetSpdySettings( 127 const HostPortPair& host_port_pair) OVERRIDE; 128 129 // Saves an individual SPDY setting for a host. Returns true if SPDY setting 130 // is to be persisted. 131 virtual bool SetSpdySetting(const HostPortPair& host_port_pair, 132 SpdySettingsIds id, 133 SpdySettingsFlags flags, 134 uint32 value) OVERRIDE; 135 136 // Clears all entries in |spdy_settings_map_| for a host. 137 virtual void ClearSpdySettings(const HostPortPair& host_port_pair) OVERRIDE; 138 139 // Clears all entries in |spdy_settings_map_|. 140 virtual void ClearAllSpdySettings() OVERRIDE; 141 142 // Returns all persistent SPDY settings. 143 virtual const SpdySettingsMap& spdy_settings_map() const OVERRIDE; 144 145 // Methods for SupportsQuic. 146 virtual SupportsQuic GetSupportsQuic( 147 const HostPortPair& host_port_pair) const OVERRIDE; 148 149 virtual void SetSupportsQuic(const HostPortPair& host_port_pair, 150 bool used_quic, 151 const std::string& address) OVERRIDE; 152 153 virtual const SupportsQuicMap& supports_quic_map() const OVERRIDE; 154 155 // Methods for NetworkStats. 156 virtual void SetServerNetworkStats(const HostPortPair& host_port_pair, 157 NetworkStats stats) OVERRIDE; 158 159 virtual const NetworkStats* GetServerNetworkStats( 160 const HostPortPair& host_port_pair) const OVERRIDE; 161 162 private: 163 // |spdy_servers_map_| has flattened representation of servers (host, port) 164 // that either support or not support SPDY protocol. 165 typedef base::MRUCache<std::string, bool> SpdyServerHostPortMap; 166 typedef std::map<HostPortPair, NetworkStats> ServerNetworkStatsMap; 167 typedef std::map<HostPortPair, HostPortPair> CanonicalHostMap; 168 typedef std::vector<std::string> CanonicalSufficList; 169 // List of broken host:ports and the times when they can be expired. 170 struct BrokenAlternateProtocolEntry { 171 HostPortPair server; 172 base::TimeTicks when; 173 }; 174 typedef std::list<BrokenAlternateProtocolEntry> 175 BrokenAlternateProtocolList; 176 // Map from host:port to the number of times alternate protocol has 177 // been marked broken. 178 typedef std::map<HostPortPair, int> BrokenAlternateProtocolMap; 179 180 // Return the canonical host for |server|, or end if none exists. 181 CanonicalHostMap::const_iterator GetCanonicalHost(HostPortPair server) const; 182 183 void RemoveCanonicalHost(const HostPortPair& server); 184 void ExpireBrokenAlternateProtocolMappings(); 185 void ScheduleBrokenAlternateProtocolMappingsExpiration(); 186 187 SpdyServerHostPortMap spdy_servers_map_; 188 189 AlternateProtocolMap alternate_protocol_map_; 190 BrokenAlternateProtocolList broken_alternate_protocol_list_; 191 BrokenAlternateProtocolMap broken_alternate_protocol_map_; 192 AlternateProtocolExperiment alternate_protocol_experiment_; 193 194 SpdySettingsMap spdy_settings_map_; 195 SupportsQuicMap supports_quic_map_; 196 ServerNetworkStatsMap server_network_stats_map_; 197 // Contains a map of servers which could share the same alternate protocol. 198 // Map from a Canonical host/port (host is some postfix of host names) to an 199 // actual origin, which has a plausible alternate protocol mapping. 200 CanonicalHostMap canonical_host_to_origin_map_; 201 // Contains list of suffixes (for exmaple ".c.youtube.com", 202 // ".googlevideo.com", ".googleusercontent.com") of canoncial hostnames. 203 CanonicalSufficList canoncial_suffixes_; 204 205 double alternate_protocol_probability_threshold_; 206 207 base::WeakPtrFactory<HttpServerPropertiesImpl> weak_ptr_factory_; 208 209 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesImpl); 210 }; 211 212 } // namespace net 213 214 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_IMPL_H_ 215