Home | History | Annotate | Download | only in flip_server
      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_TOOLS_FLIP_SERVER_SPDY_INTERFACE_H_
      6 #define NET_TOOLS_FLIP_SERVER_SPDY_INTERFACE_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/compiler_specific.h"
     13 #include "net/spdy/buffered_spdy_framer.h"
     14 #include "net/spdy/spdy_protocol.h"
     15 #include "net/tools/flip_server/balsa_headers.h"
     16 #include "net/tools/flip_server/balsa_visitor_interface.h"
     17 #include "net/tools/flip_server/output_ordering.h"
     18 #include "net/tools/flip_server/sm_connection.h"
     19 #include "net/tools/flip_server/sm_interface.h"
     20 
     21 namespace net {
     22 
     23 class FlipAcceptor;
     24 class MemoryCache;
     25 
     26 class SpdySM : public BufferedSpdyFramerVisitorInterface,
     27                public SMInterface {
     28  public:
     29   SpdySM(SMConnection* connection,
     30          SMInterface* sm_http_interface,
     31          EpollServer* epoll_server,
     32          MemoryCache* memory_cache,
     33          FlipAcceptor* acceptor);
     34   virtual ~SpdySM();
     35 
     36   virtual void InitSMInterface(SMInterface* sm_http_interface,
     37                                int32 server_idx) OVERRIDE {}
     38 
     39   virtual void InitSMConnection(SMConnectionPoolInterface* connection_pool,
     40                                 SMInterface* sm_interface,
     41                                 EpollServer* epoll_server,
     42                                 int fd,
     43                                 std::string server_ip,
     44                                 std::string server_port,
     45                                 std::string remote_ip,
     46                                 bool use_ssl) OVERRIDE;
     47 
     48  private:
     49   virtual void set_is_request() OVERRIDE {}
     50   SMInterface* NewConnectionInterface();
     51   SMInterface* FindOrMakeNewSMConnectionInterface(std::string server_ip,
     52                                                   std::string server_port);
     53   int SpdyHandleNewStream(SpdyStreamId stream_id,
     54                           SpdyPriority priority,
     55                           const SpdyHeaderBlock& headers,
     56                           std::string& http_data,
     57                           bool* is_https_scheme);
     58 
     59   // BufferedSpdyFramerVisitorInterface:
     60   virtual void OnError(SpdyFramer::SpdyError error_code) OVERRIDE {}
     61   virtual void OnStreamError(SpdyStreamId stream_id,
     62                              const std::string& description) OVERRIDE {}
     63   // Called after all the header data for SYN_STREAM control frame is received.
     64   virtual void OnSynStream(SpdyStreamId stream_id,
     65                            SpdyStreamId associated_stream_id,
     66                            SpdyPriority priority,
     67                            uint8 credential_slot,
     68                            bool fin,
     69                            bool unidirectional,
     70                            const SpdyHeaderBlock& headers) OVERRIDE;
     71 
     72   // Called after all the header data for SYN_REPLY control frame is received.
     73   virtual void OnSynReply(SpdyStreamId stream_id,
     74                           bool fin,
     75                           const SpdyHeaderBlock& headers) OVERRIDE;
     76 
     77   // Called after all the header data for HEADERS control frame is received.
     78   virtual void OnHeaders(SpdyStreamId stream_id,
     79                          bool fin,
     80                          const SpdyHeaderBlock& headers) OVERRIDE;
     81 
     82   // Called when data is received.
     83   // |stream_id| The stream receiving data.
     84   // |data| A buffer containing the data received.
     85   // |len| The length of the data buffer.
     86   // When the other side has finished sending data on this stream,
     87   // this method will be called with a zero-length buffer.
     88   virtual void OnStreamFrameData(SpdyStreamId stream_id,
     89                                  const char* data,
     90                                  size_t len,
     91                                  bool fin) OVERRIDE;
     92 
     93   // Called when a SETTINGS frame is received.
     94   // |clear_persisted| True if the respective flag is set on the SETTINGS frame.
     95   virtual void OnSettings(bool clear_persisted) OVERRIDE {}
     96 
     97   // Called when an individual setting within a SETTINGS frame has been parsed
     98   // and validated.
     99   virtual void OnSetting(SpdySettingsIds id,
    100                          uint8 flags,
    101                          uint32 value) OVERRIDE {}
    102 
    103   // Called when a PING frame has been parsed.
    104   virtual void OnPing(uint32 unique_id) OVERRIDE {}
    105 
    106   // Called when a RST_STREAM frame has been parsed.
    107   virtual void OnRstStream(SpdyStreamId stream_id,
    108                            SpdyRstStreamStatus status) OVERRIDE;
    109 
    110   // Called when a GOAWAY frame has been parsed.
    111   virtual void OnGoAway(SpdyStreamId last_accepted_stream_id,
    112                         SpdyGoAwayStatus status) OVERRIDE {}
    113 
    114   // Called when a WINDOW_UPDATE frame has been parsed.
    115   virtual void OnWindowUpdate(SpdyStreamId stream_id,
    116                               uint32 delta_window_size) OVERRIDE {}
    117 
    118   // Called when a PUSH_PROMISE frame has been parsed.
    119   virtual void OnPushPromise(SpdyStreamId stream_id,
    120                              SpdyStreamId promised_stream_id) OVERRIDE {}
    121 
    122  public:
    123   virtual size_t ProcessReadInput(const char* data, size_t len) OVERRIDE;
    124   virtual size_t ProcessWriteInput(const char* data, size_t len) OVERRIDE;
    125   virtual bool MessageFullyRead() const OVERRIDE;
    126   virtual void SetStreamID(uint32 stream_id) OVERRIDE {}
    127   virtual bool Error() const OVERRIDE;
    128   virtual const char* ErrorAsString() const OVERRIDE;
    129   virtual void Reset() OVERRIDE {}
    130   virtual void ResetForNewInterface(int32 server_idx) OVERRIDE;
    131   virtual void ResetForNewConnection() OVERRIDE;
    132   // SMInterface's Cleanup is currently only called by SMConnection after a
    133   // protocol message as been fully read. Spdy's SMInterface does not need
    134   // to do any cleanup at this time.
    135   // TODO(klindsay) This method is probably not being used properly and
    136   // some logic review and method renaming is probably in order.
    137   virtual void Cleanup() OVERRIDE {}
    138   // Send a settings frame
    139   virtual int PostAcceptHook() OVERRIDE;
    140   virtual void NewStream(uint32 stream_id,
    141                          uint32 priority,
    142                          const std::string& filename) OVERRIDE;
    143   void AddToOutputOrder(const MemCacheIter& mci);
    144   virtual void SendEOF(uint32 stream_id) OVERRIDE;
    145   virtual void SendErrorNotFound(uint32 stream_id) OVERRIDE;
    146   void SendOKResponse(uint32 stream_id, std::string* output);
    147   virtual size_t SendSynStream(uint32 stream_id,
    148                                const BalsaHeaders& headers) OVERRIDE;
    149   virtual size_t SendSynReply(uint32 stream_id,
    150                               const BalsaHeaders& headers) OVERRIDE;
    151   virtual void SendDataFrame(uint32 stream_id, const char* data, int64 len,
    152                              uint32 flags, bool compress) OVERRIDE;
    153   BufferedSpdyFramer* spdy_framer() {
    154       return buffered_spdy_framer_;
    155   }
    156 
    157   static std::string forward_ip_header() { return forward_ip_header_; }
    158   static void set_forward_ip_header(std::string value) {
    159     forward_ip_header_ = value;
    160   }
    161 
    162  private:
    163   void SendEOFImpl(uint32 stream_id);
    164   void SendErrorNotFoundImpl(uint32 stream_id);
    165   void SendOKResponseImpl(uint32 stream_id, std::string* output);
    166   void KillStream(uint32 stream_id);
    167   void CopyHeaders(SpdyHeaderBlock& dest, const BalsaHeaders& headers);
    168   size_t SendSynStreamImpl(uint32 stream_id, const BalsaHeaders& headers);
    169   size_t SendSynReplyImpl(uint32 stream_id, const BalsaHeaders& headers);
    170   void SendDataFrameImpl(uint32 stream_id, const char* data, int64 len,
    171                          SpdyDataFlags flags, bool compress);
    172   void EnqueueDataFrame(DataFrame* df);
    173   virtual void GetOutput() OVERRIDE;
    174  private:
    175   BufferedSpdyFramer* buffered_spdy_framer_;
    176   bool valid_spdy_session_;  // True if we have seen valid data on this session.
    177                              // Use this to fail fast when junk is sent to our
    178                              // port.
    179 
    180   SMConnection* connection_;
    181   OutputList* client_output_list_;
    182   OutputOrdering client_output_ordering_;
    183   uint32 next_outgoing_stream_id_;
    184   EpollServer* epoll_server_;
    185   FlipAcceptor* acceptor_;
    186   MemoryCache* memory_cache_;
    187   std::vector<SMInterface*> server_interface_list;
    188   std::vector<int32> unused_server_interface_list;
    189   typedef std::map<uint32, SMInterface*> StreamToSmif;
    190   StreamToSmif stream_to_smif_;
    191   bool close_on_error_;
    192 
    193   static std::string forward_ip_header_;
    194 };
    195 
    196 }  // namespace net
    197 
    198 #endif  // NET_TOOLS_FLIP_SERVER_SPDY_INTERFACE_H_
    199