• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_H_
6 #define NET_HTTP_HTTP_SERVER_PROPERTIES_H_
7 
8 #include <map>
9 #include <string>
10 #include "base/basictypes.h"
11 #include "base/containers/mru_cache.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/time/time.h"
14 #include "net/base/host_port_pair.h"
15 #include "net/base/net_export.h"
16 #include "net/socket/next_proto.h"
17 #include "net/spdy/spdy_framer.h"  // TODO(willchan): Reconsider this.
18 
19 namespace net {
20 
21 enum AlternateProtocolExperiment {
22   // 200 alternate_protocol servers are loaded (persisted 200 MRU servers).
23   ALTERNATE_PROTOCOL_NOT_PART_OF_EXPERIMENT = 0,
24   // 200 alternate_protocol servers are loaded (persisted 1000 MRU servers).
25   ALTERNATE_PROTOCOL_TRUNCATED_200_SERVERS,
26   // 1000 alternate_protocol servers are loaded (persisted 1000 MRU servers).
27   ALTERNATE_PROTOCOL_TRUNCATED_1000_SERVERS,
28 };
29 
30 enum AlternateProtocolUsage {
31   // Alternate Protocol was used without racing a normal connection.
32   ALTERNATE_PROTOCOL_USAGE_NO_RACE = 0,
33   // Alternate Protocol was used by winning a race with a normal connection.
34   ALTERNATE_PROTOCOL_USAGE_WON_RACE = 1,
35   // Alternate Protocol was not used by losing a race with a normal connection.
36   ALTERNATE_PROTOCOL_USAGE_LOST_RACE = 2,
37   // Alternate Protocol was not used because no Alternate-Protocol information
38   // was available when the request was issued, but an Alternate-Protocol header
39   // was present in the response.
40   ALTERNATE_PROTOCOL_USAGE_MAPPING_MISSING = 3,
41   // Alternate Protocol was not used because it was marked broken.
42   ALTERNATE_PROTOCOL_USAGE_BROKEN = 4,
43   // Maximum value for the enum.
44   ALTERNATE_PROTOCOL_USAGE_MAX,
45 };
46 
47 // Log a histogram to reflect |usage| and |alternate_protocol_experiment|.
48 NET_EXPORT void HistogramAlternateProtocolUsage(
49     AlternateProtocolUsage usage,
50     AlternateProtocolExperiment alternate_protocol_experiment);
51 
52 enum BrokenAlternateProtocolLocation {
53   BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB = 0,
54   BROKEN_ALTERNATE_PROTOCOL_LOCATION_QUIC_STREAM_FACTORY = 1,
55   BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB_ALT = 2,
56   BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB_MAIN = 3,
57   BROKEN_ALTERNATE_PROTOCOL_LOCATION_MAX,
58 };
59 
60 // Log a histogram to reflect |location|.
61 NET_EXPORT void HistogramBrokenAlternateProtocolLocation(
62     BrokenAlternateProtocolLocation location);
63 
64 enum AlternateProtocol {
65   DEPRECATED_NPN_SPDY_2 = 0,
66   ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION = DEPRECATED_NPN_SPDY_2,
67   NPN_SPDY_MINIMUM_VERSION = DEPRECATED_NPN_SPDY_2,
68   NPN_SPDY_3,
69   NPN_SPDY_3_1,
70   NPN_SPDY_4,  // SPDY4 is HTTP/2.
71   NPN_SPDY_MAXIMUM_VERSION = NPN_SPDY_4,
72   QUIC,
73   ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION = QUIC,
74   ALTERNATE_PROTOCOL_BROKEN,  // The alternate protocol is known to be broken.
75   UNINITIALIZED_ALTERNATE_PROTOCOL,
76 };
77 
78 // Simply returns whether |protocol| is between
79 // ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION and
80 // ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION (inclusive).
81 NET_EXPORT bool IsAlternateProtocolValid(AlternateProtocol protocol);
82 
83 enum AlternateProtocolSize {
84   NUM_VALID_ALTERNATE_PROTOCOLS =
85     ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION -
86     ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION + 1,
87 };
88 
89 NET_EXPORT const char* AlternateProtocolToString(AlternateProtocol protocol);
90 NET_EXPORT AlternateProtocol AlternateProtocolFromString(
91     const std::string& str);
92 NET_EXPORT_PRIVATE AlternateProtocol AlternateProtocolFromNextProto(
93     NextProto next_proto);
94 
95 struct NET_EXPORT AlternateProtocolInfo {
AlternateProtocolInfoAlternateProtocolInfo96   AlternateProtocolInfo(uint16 port,
97                         AlternateProtocol protocol,
98                         double probability)
99       : port(port),
100         protocol(protocol),
101         probability(probability) {}
102 
EqualsAlternateProtocolInfo103   bool Equals(const AlternateProtocolInfo& other) const {
104     return port == other.port &&
105         protocol == other.protocol &&
106         probability == other.probability;
107   }
108 
109   std::string ToString() const;
110 
111   uint16 port;
112   AlternateProtocol protocol;
113   double probability;
114 };
115 
116 struct NET_EXPORT SupportsQuic {
SupportsQuicSupportsQuic117   SupportsQuic() : used_quic(false) {}
SupportsQuicSupportsQuic118   SupportsQuic(bool used_quic, const std::string& address)
119       : used_quic(used_quic),
120         address(address) {}
121 
EqualsSupportsQuic122   bool Equals(const SupportsQuic& other) const {
123     return used_quic == other.used_quic && address == other.address;
124   }
125 
126   bool used_quic;
127   std::string address;
128 };
129 
130 typedef base::MRUCache<
131     HostPortPair, AlternateProtocolInfo> AlternateProtocolMap;
132 typedef base::MRUCache<HostPortPair, SettingsMap> SpdySettingsMap;
133 typedef std::map<HostPortPair, SupportsQuic> SupportsQuicMap;
134 
135 extern const char kAlternateProtocolHeader[];
136 
137 // The interface for setting/retrieving the HTTP server properties.
138 // Currently, this class manages servers':
139 // * SPDY support (based on NPN results)
140 // * Alternate-Protocol support
141 // * Spdy Settings (like CWND ID field)
142 class NET_EXPORT HttpServerProperties {
143  public:
144   struct NetworkStats {
145     base::TimeDelta srtt;
146     uint64 bandwidth_estimate;
147   };
148 
HttpServerProperties()149   HttpServerProperties() {}
~HttpServerProperties()150   virtual ~HttpServerProperties() {}
151 
152   // Gets a weak pointer for this object.
153   virtual base::WeakPtr<HttpServerProperties> GetWeakPtr() = 0;
154 
155   // Deletes all data.
156   virtual void Clear() = 0;
157 
158   // Returns true if |server| supports SPDY.
159   virtual bool SupportsSpdy(const HostPortPair& server) = 0;
160 
161   // Add |server| into the persistent store. Should only be called from IO
162   // thread.
163   virtual void SetSupportsSpdy(const HostPortPair& server,
164                                bool support_spdy) = 0;
165 
166   // Returns true if |server| has an Alternate-Protocol header.
167   virtual bool HasAlternateProtocol(const HostPortPair& server) = 0;
168 
169   // Returns the Alternate-Protocol and port for |server|.
170   // HasAlternateProtocol(server) must be true.
171   virtual AlternateProtocolInfo GetAlternateProtocol(
172       const HostPortPair& server) = 0;
173 
174   // Sets the Alternate-Protocol for |server|.
175   virtual void SetAlternateProtocol(const HostPortPair& server,
176                                     uint16 alternate_port,
177                                     AlternateProtocol alternate_protocol,
178                                     double probability) = 0;
179 
180   // Sets the Alternate-Protocol for |server| to be BROKEN.
181   virtual void SetBrokenAlternateProtocol(const HostPortPair& server) = 0;
182 
183   // Returns true if Alternate-Protocol for |server| was recently BROKEN.
184   virtual bool WasAlternateProtocolRecentlyBroken(
185       const HostPortPair& server) = 0;
186 
187   // Confirms that Alternate-Protocol for |server| is working.
188   virtual void ConfirmAlternateProtocol(const HostPortPair& server) = 0;
189 
190   // Clears the Alternate-Protocol for |server|.
191   virtual void ClearAlternateProtocol(const HostPortPair& server) = 0;
192 
193   // Returns all Alternate-Protocol mappings.
194   virtual const AlternateProtocolMap& alternate_protocol_map() const = 0;
195 
196   virtual void SetAlternateProtocolExperiment(
197       AlternateProtocolExperiment experiment) = 0;
198 
199   // Sets the threshold to be used when evaluating Alternate-Protocol
200   // advertisments. Only advertisements with a with a probability
201   // greater than |threshold| will be honored. |threshold| must be
202   // between 0 and 1 inclusive. Hence, a threshold of 0 implies that
203   // all advertisements will be honored.
204   virtual void SetAlternateProtocolProbabilityThreshold(
205       double threshold) = 0;
206 
207   virtual AlternateProtocolExperiment GetAlternateProtocolExperiment()
208       const = 0;
209 
210   // Gets a reference to the SettingsMap stored for a host.
211   // If no settings are stored, returns an empty SettingsMap.
212   virtual const SettingsMap& GetSpdySettings(
213       const HostPortPair& host_port_pair) = 0;
214 
215   // Saves an individual SPDY setting for a host. Returns true if SPDY setting
216   // is to be persisted.
217   virtual bool SetSpdySetting(const HostPortPair& host_port_pair,
218                               SpdySettingsIds id,
219                               SpdySettingsFlags flags,
220                               uint32 value) = 0;
221 
222   // Clears all SPDY settings for a host.
223   virtual void ClearSpdySettings(const HostPortPair& host_port_pair) = 0;
224 
225   // Clears all SPDY settings for all hosts.
226   virtual void ClearAllSpdySettings() = 0;
227 
228   // Returns all persistent SPDY settings.
229   virtual const SpdySettingsMap& spdy_settings_map() const = 0;
230 
231   // TODO(rtenneti): Make SupportsQuic a global (instead of per host_port_pair).
232   virtual SupportsQuic GetSupportsQuic(
233       const HostPortPair& host_port_pair) const = 0;
234 
235   virtual void SetSupportsQuic(const HostPortPair& host_port_pair,
236                                bool used_quic,
237                                const std::string& address) = 0;
238 
239   virtual const SupportsQuicMap& supports_quic_map() const = 0;
240 
241   virtual void SetServerNetworkStats(const HostPortPair& host_port_pair,
242                                      NetworkStats stats) = 0;
243 
244   virtual const NetworkStats* GetServerNetworkStats(
245       const HostPortPair& host_port_pair) const = 0;
246 
247  private:
248   DISALLOW_COPY_AND_ASSIGN(HttpServerProperties);
249 };
250 
251 }  // namespace net
252 
253 #endif  // NET_HTTP_HTTP_SERVER_PROPERTIES_H_
254