Home | History | Annotate | Download | only in http
      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/memory/weak_ptr.h"
     12 #include "net/base/host_port_pair.h"
     13 #include "net/base/net_export.h"
     14 #include "net/http/http_pipelined_host_capability.h"
     15 #include "net/socket/next_proto.h"
     16 #include "net/spdy/spdy_framer.h"  // TODO(willchan): Reconsider this.
     17 
     18 namespace net {
     19 
     20 enum AlternateProtocol {
     21   DEPRECATED_NPN_SPDY_2 = 0,
     22   ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION = DEPRECATED_NPN_SPDY_2,
     23   NPN_SPDY_MINIMUM_VERSION = DEPRECATED_NPN_SPDY_2,
     24   NPN_SPDY_3,
     25   NPN_SPDY_3_1,
     26   NPN_SPDY_4A2,
     27   // We lump in HTTP/2 with the SPDY protocols for now.
     28   NPN_HTTP2_DRAFT_04,
     29   NPN_SPDY_MAXIMUM_VERSION = NPN_HTTP2_DRAFT_04,
     30   QUIC,
     31   ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION = QUIC,
     32   ALTERNATE_PROTOCOL_BROKEN,  // The alternate protocol is known to be broken.
     33   UNINITIALIZED_ALTERNATE_PROTOCOL,
     34 };
     35 
     36 // Simply returns whether |protocol| is between
     37 // ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION and
     38 // ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION (inclusive).
     39 NET_EXPORT bool IsAlternateProtocolValid(AlternateProtocol protocol);
     40 
     41 enum AlternateProtocolSize {
     42   NUM_VALID_ALTERNATE_PROTOCOLS =
     43     ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION -
     44     ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION + 1,
     45 };
     46 
     47 NET_EXPORT const char* AlternateProtocolToString(AlternateProtocol protocol);
     48 NET_EXPORT AlternateProtocol AlternateProtocolFromString(
     49     const std::string& str);
     50 NET_EXPORT_PRIVATE AlternateProtocol AlternateProtocolFromNextProto(
     51     NextProto next_proto);
     52 
     53 struct NET_EXPORT PortAlternateProtocolPair {
     54   bool Equals(const PortAlternateProtocolPair& other) const {
     55     return port == other.port && protocol == other.protocol;
     56   }
     57 
     58   std::string ToString() const;
     59 
     60   uint16 port;
     61   AlternateProtocol protocol;
     62 };
     63 
     64 typedef std::map<HostPortPair, PortAlternateProtocolPair> AlternateProtocolMap;
     65 typedef std::map<HostPortPair, SettingsMap> SpdySettingsMap;
     66 typedef std::map<HostPortPair,
     67         HttpPipelinedHostCapability> PipelineCapabilityMap;
     68 
     69 extern const char kAlternateProtocolHeader[];
     70 
     71 // The interface for setting/retrieving the HTTP server properties.
     72 // Currently, this class manages servers':
     73 // * SPDY support (based on NPN results)
     74 // * Alternate-Protocol support
     75 // * Spdy Settings (like CWND ID field)
     76 class NET_EXPORT HttpServerProperties {
     77  public:
     78   HttpServerProperties() {}
     79   virtual ~HttpServerProperties() {}
     80 
     81   // Gets a weak pointer for this object.
     82   virtual base::WeakPtr<HttpServerProperties> GetWeakPtr() = 0;
     83 
     84   // Deletes all data.
     85   virtual void Clear() = 0;
     86 
     87   // Returns true if |server| supports SPDY.
     88   virtual bool SupportsSpdy(const HostPortPair& server) const = 0;
     89 
     90   // Add |server| into the persistent store. Should only be called from IO
     91   // thread.
     92   virtual void SetSupportsSpdy(const HostPortPair& server,
     93                                bool support_spdy) = 0;
     94 
     95   // Returns true if |server| has an Alternate-Protocol header.
     96   virtual bool HasAlternateProtocol(const HostPortPair& server) const = 0;
     97 
     98   // Returns the Alternate-Protocol and port for |server|.
     99   // HasAlternateProtocol(server) must be true.
    100   virtual PortAlternateProtocolPair GetAlternateProtocol(
    101       const HostPortPair& server) const = 0;
    102 
    103   // Sets the Alternate-Protocol for |server|.
    104   virtual void SetAlternateProtocol(const HostPortPair& server,
    105                                     uint16 alternate_port,
    106                                     AlternateProtocol alternate_protocol) = 0;
    107 
    108   // Sets the Alternate-Protocol for |server| to be BROKEN.
    109   virtual void SetBrokenAlternateProtocol(const HostPortPair& server) = 0;
    110 
    111   // Returns all Alternate-Protocol mappings.
    112   virtual const AlternateProtocolMap& alternate_protocol_map() const = 0;
    113 
    114   // Gets a reference to the SettingsMap stored for a host.
    115   // If no settings are stored, returns an empty SettingsMap.
    116   virtual const SettingsMap& GetSpdySettings(
    117       const HostPortPair& host_port_pair) const = 0;
    118 
    119   // Saves an individual SPDY setting for a host. Returns true if SPDY setting
    120   // is to be persisted.
    121   virtual bool SetSpdySetting(const HostPortPair& host_port_pair,
    122                               SpdySettingsIds id,
    123                               SpdySettingsFlags flags,
    124                               uint32 value) = 0;
    125 
    126   // Clears all SPDY settings for a host.
    127   virtual void ClearSpdySettings(const HostPortPair& host_port_pair) = 0;
    128 
    129   // Clears all SPDY settings for all hosts.
    130   virtual void ClearAllSpdySettings() = 0;
    131 
    132   // Returns all persistent SPDY settings.
    133   virtual const SpdySettingsMap& spdy_settings_map() const = 0;
    134 
    135   virtual HttpPipelinedHostCapability GetPipelineCapability(
    136       const HostPortPair& origin) = 0;
    137 
    138   virtual void SetPipelineCapability(
    139       const HostPortPair& origin,
    140       HttpPipelinedHostCapability capability) = 0;
    141 
    142   virtual void ClearPipelineCapabilities() = 0;
    143 
    144   virtual PipelineCapabilityMap GetPipelineCapabilityMap() const = 0;
    145 
    146  private:
    147   DISALLOW_COPY_AND_ASSIGN(HttpServerProperties);
    148 };
    149 
    150 }  // namespace net
    151 
    152 #endif  // NET_HTTP_HTTP_SERVER_PROPERTIES_H_
    153