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.
5 #ifndef NET_HTTP_HTTP_SERVER_PROPERTIES_H_
6 #define NET_HTTP_HTTP_SERVER_PROPERTIES_H_
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 #include "net/spdy/spdy_protocol.h"
22 enum AlternateProtocolUsage {
23 // Alternate Protocol was used without racing a normal connection.
24 ALTERNATE_PROTOCOL_USAGE_NO_RACE = 0,
25 // Alternate Protocol was used by winning a race with a normal connection.
26 ALTERNATE_PROTOCOL_USAGE_WON_RACE = 1,
27 // Alternate Protocol was not used by losing a race with a normal connection.
28 ALTERNATE_PROTOCOL_USAGE_LOST_RACE = 2,
29 // Alternate Protocol was not used because no Alternate-Protocol information
30 // was available when the request was issued, but an Alternate-Protocol header
31 // was present in the response.
32 ALTERNATE_PROTOCOL_USAGE_MAPPING_MISSING = 3,
33 // Alternate Protocol was not used because it was marked broken.
34 ALTERNATE_PROTOCOL_USAGE_BROKEN = 4,
35 // Maximum value for the enum.
36 ALTERNATE_PROTOCOL_USAGE_MAX,
39 // Log a histogram to reflect |usage|.
40 NET_EXPORT void HistogramAlternateProtocolUsage(AlternateProtocolUsage usage);
42 enum BrokenAlternateProtocolLocation {
43 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB = 0,
44 BROKEN_ALTERNATE_PROTOCOL_LOCATION_QUIC_STREAM_FACTORY = 1,
45 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB_ALT = 2,
46 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_IMPL_JOB_MAIN = 3,
47 BROKEN_ALTERNATE_PROTOCOL_LOCATION_MAX,
50 // Log a histogram to reflect |location|.
51 NET_EXPORT void HistogramBrokenAlternateProtocolLocation(
52 BrokenAlternateProtocolLocation location);
54 enum AlternateProtocol {
55 DEPRECATED_NPN_SPDY_2 = 0,
56 ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION = DEPRECATED_NPN_SPDY_2,
57 NPN_SPDY_MINIMUM_VERSION = DEPRECATED_NPN_SPDY_2,
60 NPN_SPDY_4, // SPDY4 is HTTP/2.
61 NPN_SPDY_MAXIMUM_VERSION = NPN_SPDY_4,
63 ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION = QUIC,
64 UNINITIALIZED_ALTERNATE_PROTOCOL,
67 // Simply returns whether |protocol| is between
68 // ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION and
69 // ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION (inclusive).
70 NET_EXPORT bool IsAlternateProtocolValid(AlternateProtocol protocol);
72 enum AlternateProtocolSize {
73 NUM_VALID_ALTERNATE_PROTOCOLS =
74 ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION -
75 ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION + 1,
78 NET_EXPORT const char* AlternateProtocolToString(AlternateProtocol protocol);
79 NET_EXPORT AlternateProtocol AlternateProtocolFromString(
80 const std::string& str);
81 NET_EXPORT_PRIVATE AlternateProtocol AlternateProtocolFromNextProto(
82 NextProto next_proto);
84 struct NET_EXPORT AlternateProtocolInfo {
85 AlternateProtocolInfo(uint16 port,
86 AlternateProtocol protocol,
90 probability(probability),
93 AlternateProtocolInfo(uint16 port,
94 AlternateProtocol protocol,
99 probability(probability),
100 is_broken(is_broken) {}
102 bool Equals(const AlternateProtocolInfo& other) const {
103 return port == other.port &&
104 protocol == other.protocol &&
105 probability == other.probability;
108 std::string ToString() const;
111 AlternateProtocol protocol;
116 struct NET_EXPORT SupportsQuic {
117 SupportsQuic() : used_quic(false) {}
118 SupportsQuic(bool used_quic, const std::string& address)
119 : used_quic(used_quic),
122 bool Equals(const SupportsQuic& other) const {
123 return used_quic == other.used_quic && address == other.address;
130 typedef base::MRUCache<
131 HostPortPair, AlternateProtocolInfo> AlternateProtocolMap;
132 typedef base::MRUCache<HostPortPair, SettingsMap> SpdySettingsMap;
133 typedef std::map<HostPortPair, SupportsQuic> SupportsQuicMap;
135 extern const char kAlternateProtocolHeader[];
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 {
144 struct NetworkStats {
145 base::TimeDelta srtt;
146 uint64 bandwidth_estimate;
149 HttpServerProperties() {}
150 virtual ~HttpServerProperties() {}
152 // Gets a weak pointer for this object.
153 virtual base::WeakPtr<HttpServerProperties> GetWeakPtr() = 0;
156 virtual void Clear() = 0;
158 // Returns true if |server| supports SPDY.
159 virtual bool SupportsSpdy(const HostPortPair& server) = 0;
161 // Add |server| into the persistent store. Should only be called from IO
163 virtual void SetSupportsSpdy(const HostPortPair& server,
164 bool support_spdy) = 0;
166 // Returns true if |server| has an Alternate-Protocol header.
167 virtual bool HasAlternateProtocol(const HostPortPair& server) = 0;
169 // Returns the Alternate-Protocol and port for |server|.
170 // HasAlternateProtocol(server) must be true.
171 virtual AlternateProtocolInfo GetAlternateProtocol(
172 const HostPortPair& server) = 0;
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;
180 // Sets the Alternate-Protocol for |server| to be BROKEN.
181 virtual void SetBrokenAlternateProtocol(const HostPortPair& server) = 0;
183 // Returns true if Alternate-Protocol for |server| was recently BROKEN.
184 virtual bool WasAlternateProtocolRecentlyBroken(
185 const HostPortPair& server) = 0;
187 // Confirms that Alternate-Protocol for |server| is working.
188 virtual void ConfirmAlternateProtocol(const HostPortPair& server) = 0;
190 // Clears the Alternate-Protocol for |server|.
191 virtual void ClearAlternateProtocol(const HostPortPair& server) = 0;
193 // Returns all Alternate-Protocol mappings.
194 virtual const AlternateProtocolMap& alternate_protocol_map() const = 0;
196 // Sets the threshold to be used when evaluating Alternate-Protocol
197 // advertisments. Only advertisements with a with a probability
198 // greater than |threshold| will be honored. |threshold| must be
199 // between 0 and 1 inclusive. Hence, a threshold of 0 implies that
200 // all advertisements will be honored.
201 virtual void SetAlternateProtocolProbabilityThreshold(
202 double threshold) = 0;
204 // Gets a reference to the SettingsMap stored for a host.
205 // If no settings are stored, returns an empty SettingsMap.
206 virtual const SettingsMap& GetSpdySettings(
207 const HostPortPair& host_port_pair) = 0;
209 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
210 // is to be persisted.
211 virtual bool SetSpdySetting(const HostPortPair& host_port_pair,
213 SpdySettingsFlags flags,
216 // Clears all SPDY settings for a host.
217 virtual void ClearSpdySettings(const HostPortPair& host_port_pair) = 0;
219 // Clears all SPDY settings for all hosts.
220 virtual void ClearAllSpdySettings() = 0;
222 // Returns all persistent SPDY settings.
223 virtual const SpdySettingsMap& spdy_settings_map() const = 0;
225 // TODO(rtenneti): Make SupportsQuic a global (instead of per host_port_pair).
226 virtual SupportsQuic GetSupportsQuic(
227 const HostPortPair& host_port_pair) const = 0;
229 virtual void SetSupportsQuic(const HostPortPair& host_port_pair,
231 const std::string& address) = 0;
233 virtual const SupportsQuicMap& supports_quic_map() const = 0;
235 virtual void SetServerNetworkStats(const HostPortPair& host_port_pair,
236 NetworkStats stats) = 0;
238 virtual const NetworkStats* GetServerNetworkStats(
239 const HostPortPair& host_port_pair) const = 0;
242 DISALLOW_COPY_AND_ASSIGN(HttpServerProperties);
247 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_H_