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