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_TOOLS_FLIP_SERVER_SPDY_INTERFACE_H_
6 #define NET_TOOLS_FLIP_SERVER_SPDY_INTERFACE_H_
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "net/spdy/buffered_spdy_framer.h"
15 #include "net/spdy/spdy_protocol.h"
16 #include "net/tools/balsa/balsa_headers.h"
17 #include "net/tools/balsa/balsa_visitor_interface.h"
18 #include "net/tools/flip_server/output_ordering.h"
19 #include "net/tools/flip_server/sm_connection.h"
20 #include "net/tools/flip_server/sm_interface.h"
27 class SpdySM
: public BufferedSpdyFramerVisitorInterface
, public SMInterface
{
29 SpdySM(SMConnection
* connection
,
30 SMInterface
* sm_http_interface
,
31 EpollServer
* epoll_server
,
32 MemoryCache
* memory_cache
,
33 FlipAcceptor
* acceptor
,
34 SpdyMajorVersion spdy_version
);
37 void InitSMInterface(SMInterface
* sm_http_interface
,
38 int32 server_idx
) override
{}
40 void InitSMConnection(SMConnectionPoolInterface
* connection_pool
,
41 SMInterface
* sm_interface
,
42 EpollServer
* epoll_server
,
44 std::string server_ip
,
45 std::string server_port
,
46 std::string remote_ip
,
47 bool use_ssl
) override
;
49 // Create new SPDY framer after reusing SpdySM and negotiating new version
50 void CreateFramer(SpdyMajorVersion spdy_version
);
53 void set_is_request() override
{}
54 SMInterface
* NewConnectionInterface();
56 virtual SMInterface
* FindOrMakeNewSMConnectionInterface(
57 const std::string
& server_ip
,
58 const std::string
& server_port
);
59 int SpdyHandleNewStream(SpdyStreamId stream_id
,
60 SpdyPriority priority
,
61 const SpdyHeaderBlock
& headers
,
62 std::string
& http_data
,
63 bool* is_https_scheme
);
65 // BufferedSpdyFramerVisitorInterface:
66 void OnError(SpdyFramer::SpdyError error_code
) override
{}
67 void OnStreamError(SpdyStreamId stream_id
,
68 const std::string
& description
) override
{}
69 // Called after all the header data for SYN_STREAM control frame is received.
70 void OnSynStream(SpdyStreamId stream_id
,
71 SpdyStreamId associated_stream_id
,
72 SpdyPriority priority
,
75 const SpdyHeaderBlock
& headers
) override
;
77 // Called after all the header data for SYN_REPLY control frame is received.
78 void OnSynReply(SpdyStreamId stream_id
,
80 const SpdyHeaderBlock
& headers
) override
;
82 // Called after all the header data for HEADERS control frame is received.
83 void OnHeaders(SpdyStreamId stream_id
,
85 SpdyPriority priority
,
86 SpdyStreamId parent_stream_id
,
89 const SpdyHeaderBlock
& headers
) override
;
91 // Called when data frame header is received.
92 void OnDataFrameHeader(SpdyStreamId stream_id
,
96 // Called when data is received.
97 // |stream_id| The stream receiving data.
98 // |data| A buffer containing the data received.
99 // |len| The length of the data buffer.
100 // When the other side has finished sending data on this stream,
101 // this method will be called with a zero-length buffer.
102 void OnStreamFrameData(SpdyStreamId stream_id
,
107 // Called when padding is received (padding length field or padding octets).
108 // |stream_id| The stream receiving data.
109 // |len| The number of padding octets.
110 void OnStreamPadding(SpdyStreamId stream_id
, size_t len
) override
;
112 // Called when a SETTINGS frame is received.
113 // |clear_persisted| True if the respective flag is set on the SETTINGS frame.
114 void OnSettings(bool clear_persisted
) override
{}
116 // Called when an individual setting within a SETTINGS frame has been parsed
118 void OnSetting(SpdySettingsIds id
, uint8 flags
, uint32 value
) override
{}
120 // Called when a PING frame has been parsed.
121 void OnPing(SpdyPingId unique_id
, bool is_ack
) override
{}
123 // Called when a RST_STREAM frame has been parsed.
124 void OnRstStream(SpdyStreamId stream_id
, SpdyRstStreamStatus status
) override
;
126 // Called when a GOAWAY frame has been parsed.
127 void OnGoAway(SpdyStreamId last_accepted_stream_id
,
128 SpdyGoAwayStatus status
) override
{}
130 // Called when a WINDOW_UPDATE frame has been parsed.
131 void OnWindowUpdate(SpdyStreamId stream_id
, int delta_window_size
) override
{}
133 // Called when a PUSH_PROMISE frame has been parsed.
134 void OnPushPromise(SpdyStreamId stream_id
,
135 SpdyStreamId promised_stream_id
,
136 const SpdyHeaderBlock
& headers
) override
{}
138 bool OnUnknownFrame(SpdyStreamId stream_id
, int frame_type
) override
;
141 size_t ProcessReadInput(const char* data
, size_t len
) override
;
142 size_t ProcessWriteInput(const char* data
, size_t len
) override
;
143 bool MessageFullyRead() const override
;
144 void SetStreamID(uint32 stream_id
) override
{}
145 bool Error() const override
;
146 const char* ErrorAsString() const override
;
147 void Reset() override
{}
148 void ResetForNewInterface(int32 server_idx
) override
;
149 void ResetForNewConnection() override
;
150 // SMInterface's Cleanup is currently only called by SMConnection after a
151 // protocol message as been fully read. Spdy's SMInterface does not need
152 // to do any cleanup at this time.
153 // TODO(klindsay) This method is probably not being used properly and
154 // some logic review and method renaming is probably in order.
155 void Cleanup() override
{}
156 // Send a settings frame
157 int PostAcceptHook() override
;
158 void NewStream(uint32 stream_id
,
160 const std::string
& filename
) override
;
161 void AddToOutputOrder(const MemCacheIter
& mci
);
162 void SendEOF(uint32 stream_id
) override
;
163 void SendErrorNotFound(uint32 stream_id
) override
;
164 size_t SendSynStream(uint32 stream_id
, const BalsaHeaders
& headers
) override
;
165 size_t SendSynReply(uint32 stream_id
, const BalsaHeaders
& headers
) override
;
166 void SendDataFrame(uint32 stream_id
,
170 bool compress
) override
;
171 BufferedSpdyFramer
* spdy_framer() { return buffered_spdy_framer_
.get(); }
173 const OutputOrdering
& output_ordering() const {
174 return client_output_ordering_
;
177 static std::string
forward_ip_header() { return forward_ip_header_
; }
178 static void set_forward_ip_header(const std::string
& value
) {
179 forward_ip_header_
= value
;
181 SpdyMajorVersion
spdy_version() const {
182 DCHECK(buffered_spdy_framer_
);
183 return buffered_spdy_framer_
->protocol_version();
187 void SendEOFImpl(uint32 stream_id
);
188 void SendErrorNotFoundImpl(uint32 stream_id
);
189 void KillStream(uint32 stream_id
);
190 void CopyHeaders(SpdyHeaderBlock
& dest
, const BalsaHeaders
& headers
);
191 size_t SendSynStreamImpl(uint32 stream_id
, const BalsaHeaders
& headers
);
192 size_t SendSynReplyImpl(uint32 stream_id
, const BalsaHeaders
& headers
);
193 void SendDataFrameImpl(uint32 stream_id
,
198 void EnqueueDataFrame(DataFrame
* df
);
199 void GetOutput() override
;
202 scoped_ptr
<BufferedSpdyFramer
> buffered_spdy_framer_
;
203 bool valid_spdy_session_
; // True if we have seen valid data on this session.
204 // Use this to fail fast when junk is sent to our
207 SMConnection
* connection_
;
208 OutputList
* client_output_list_
;
209 OutputOrdering client_output_ordering_
;
210 uint32 next_outgoing_stream_id_
;
211 EpollServer
* epoll_server_
;
212 FlipAcceptor
* acceptor_
;
213 MemoryCache
* memory_cache_
;
214 std::vector
<SMInterface
*> server_interface_list
;
215 std::vector
<int32
> unused_server_interface_list
;
216 typedef std::map
<uint32
, SMInterface
*> StreamToSmif
;
217 StreamToSmif stream_to_smif_
;
218 bool close_on_error_
;
220 static std::string forward_ip_header_
;
225 #endif // NET_TOOLS_FLIP_SERVER_SPDY_INTERFACE_H_