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
19namespace net {
20
21enum 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
30enum 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|.
48NET_EXPORT void HistogramAlternateProtocolUsage(
49    AlternateProtocolUsage usage,
50    AlternateProtocolExperiment alternate_protocol_experiment);
51
52enum 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|.
61NET_EXPORT void HistogramBrokenAlternateProtocolLocation(
62    BrokenAlternateProtocolLocation location);
63
64enum 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).
81NET_EXPORT bool IsAlternateProtocolValid(AlternateProtocol protocol);
82
83enum AlternateProtocolSize {
84  NUM_VALID_ALTERNATE_PROTOCOLS =
85    ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION -
86    ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION + 1,
87};
88
89NET_EXPORT const char* AlternateProtocolToString(AlternateProtocol protocol);
90NET_EXPORT AlternateProtocol AlternateProtocolFromString(
91    const std::string& str);
92NET_EXPORT_PRIVATE AlternateProtocol AlternateProtocolFromNextProto(
93    NextProto next_proto);
94
95struct NET_EXPORT PortAlternateProtocolPair {
96  bool Equals(const PortAlternateProtocolPair& other) const {
97    return port == other.port && protocol == other.protocol;
98  }
99
100  std::string ToString() const;
101
102  uint16 port;
103  AlternateProtocol protocol;
104};
105
106typedef base::MRUCache<
107    HostPortPair, PortAlternateProtocolPair> AlternateProtocolMap;
108typedef base::MRUCache<HostPortPair, SettingsMap> SpdySettingsMap;
109
110extern const char kAlternateProtocolHeader[];
111
112// The interface for setting/retrieving the HTTP server properties.
113// Currently, this class manages servers':
114// * SPDY support (based on NPN results)
115// * Alternate-Protocol support
116// * Spdy Settings (like CWND ID field)
117class NET_EXPORT HttpServerProperties {
118 public:
119  struct NetworkStats {
120    base::TimeDelta srtt;
121    uint64 bandwidth_estimate;
122  };
123
124  HttpServerProperties() {}
125  virtual ~HttpServerProperties() {}
126
127  // Gets a weak pointer for this object.
128  virtual base::WeakPtr<HttpServerProperties> GetWeakPtr() = 0;
129
130  // Deletes all data.
131  virtual void Clear() = 0;
132
133  // Returns true if |server| supports SPDY.
134  virtual bool SupportsSpdy(const HostPortPair& server) = 0;
135
136  // Add |server| into the persistent store. Should only be called from IO
137  // thread.
138  virtual void SetSupportsSpdy(const HostPortPair& server,
139                               bool support_spdy) = 0;
140
141  // Returns true if |server| has an Alternate-Protocol header.
142  virtual bool HasAlternateProtocol(const HostPortPair& server) = 0;
143
144  // Returns the Alternate-Protocol and port for |server|.
145  // HasAlternateProtocol(server) must be true.
146  virtual PortAlternateProtocolPair GetAlternateProtocol(
147      const HostPortPair& server) = 0;
148
149  // Sets the Alternate-Protocol for |server|.
150  virtual void SetAlternateProtocol(const HostPortPair& server,
151                                    uint16 alternate_port,
152                                    AlternateProtocol alternate_protocol) = 0;
153
154  // Sets the Alternate-Protocol for |server| to be BROKEN.
155  virtual void SetBrokenAlternateProtocol(const HostPortPair& server) = 0;
156
157  // Returns true if Alternate-Protocol for |server| was recently BROKEN.
158  virtual bool WasAlternateProtocolRecentlyBroken(
159      const HostPortPair& server) = 0;
160
161  // Confirms that Alternate-Protocol for |server| is working.
162  virtual void ConfirmAlternateProtocol(const HostPortPair& server) = 0;
163
164  // Clears the Alternate-Protocol for |server|.
165  virtual void ClearAlternateProtocol(const HostPortPair& server) = 0;
166
167  // Returns all Alternate-Protocol mappings.
168  virtual const AlternateProtocolMap& alternate_protocol_map() const = 0;
169
170  virtual void SetAlternateProtocolExperiment(
171      AlternateProtocolExperiment experiment) = 0;
172
173  virtual AlternateProtocolExperiment GetAlternateProtocolExperiment()
174      const = 0;
175
176  // Gets a reference to the SettingsMap stored for a host.
177  // If no settings are stored, returns an empty SettingsMap.
178  virtual const SettingsMap& GetSpdySettings(
179      const HostPortPair& host_port_pair) = 0;
180
181  // Saves an individual SPDY setting for a host. Returns true if SPDY setting
182  // is to be persisted.
183  virtual bool SetSpdySetting(const HostPortPair& host_port_pair,
184                              SpdySettingsIds id,
185                              SpdySettingsFlags flags,
186                              uint32 value) = 0;
187
188  // Clears all SPDY settings for a host.
189  virtual void ClearSpdySettings(const HostPortPair& host_port_pair) = 0;
190
191  // Clears all SPDY settings for all hosts.
192  virtual void ClearAllSpdySettings() = 0;
193
194  // Returns all persistent SPDY settings.
195  virtual const SpdySettingsMap& spdy_settings_map() const = 0;
196
197  virtual void SetServerNetworkStats(const HostPortPair& host_port_pair,
198                                     NetworkStats stats) = 0;
199
200  virtual const NetworkStats* GetServerNetworkStats(
201      const HostPortPair& host_port_pair) const = 0;
202
203 private:
204  DISALLOW_COPY_AND_ASSIGN(HttpServerProperties);
205};
206
207}  // namespace net
208
209#endif  // NET_HTTP_HTTP_SERVER_PROPERTIES_H_
210