blob: 3b2e5134c3b4dec24471e73e9179c92341aa59c0 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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"
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010015#include "net/tools/balsa/balsa_headers.h"
16#include "net/tools/balsa/balsa_visitor_interface.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000017#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
21namespace net {
22
23class FlipAcceptor;
24class MemoryCache;
25
26class 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();
Torne (Richard Coles)3551c9c2013-08-23 16:39:15 +010051 // virtual for tests
52 virtual SMInterface* FindOrMakeNewSMConnectionInterface(
53 const std::string& server_ip,
54 const std::string& server_port);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000055 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:
62 virtual void OnError(SpdyFramer::SpdyError error_code) OVERRIDE {}
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 is received.
85 // |stream_id| The stream receiving data.
86 // |data| A buffer containing the data received.
87 // |len| The length of the data buffer.
88 // When the other side has finished sending data on this stream,
89 // this method will be called with a zero-length buffer.
90 virtual void OnStreamFrameData(SpdyStreamId stream_id,
91 const char* data,
92 size_t len,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000093 bool fin) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000094
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010095 // Called when a SETTINGS frame is received.
96 // |clear_persisted| True if the respective flag is set on the SETTINGS frame.
97 virtual void OnSettings(bool clear_persisted) OVERRIDE {}
98
Torne (Richard Coles)58218062012-11-14 11:43:16 +000099 // Called when an individual setting within a SETTINGS frame has been parsed
100 // and validated.
101 virtual void OnSetting(SpdySettingsIds id,
102 uint8 flags,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000103 uint32 value) OVERRIDE {}
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000104
105 // Called when a PING frame has been parsed.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000106 virtual void OnPing(uint32 unique_id) OVERRIDE {}
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000107
108 // Called when a RST_STREAM frame has been parsed.
109 virtual void OnRstStream(SpdyStreamId stream_id,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000110 SpdyRstStreamStatus status) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000111
112 // Called when a GOAWAY frame has been parsed.
113 virtual void OnGoAway(SpdyStreamId last_accepted_stream_id,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000114 SpdyGoAwayStatus status) OVERRIDE {}
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000115
116 // Called when a WINDOW_UPDATE frame has been parsed.
117 virtual void OnWindowUpdate(SpdyStreamId stream_id,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000118 uint32 delta_window_size) OVERRIDE {}
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000119
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100120 // Called when a PUSH_PROMISE frame has been parsed.
121 virtual void OnPushPromise(SpdyStreamId stream_id,
122 SpdyStreamId promised_stream_id) OVERRIDE {}
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000123
124 public:
125 virtual size_t ProcessReadInput(const char* data, size_t len) OVERRIDE;
126 virtual size_t ProcessWriteInput(const char* data, size_t len) OVERRIDE;
127 virtual bool MessageFullyRead() const OVERRIDE;
128 virtual void SetStreamID(uint32 stream_id) OVERRIDE {}
129 virtual bool Error() const OVERRIDE;
130 virtual const char* ErrorAsString() const OVERRIDE;
131 virtual void Reset() OVERRIDE {}
132 virtual void ResetForNewInterface(int32 server_idx) OVERRIDE;
133 virtual void ResetForNewConnection() OVERRIDE;
134 // SMInterface's Cleanup is currently only called by SMConnection after a
135 // protocol message as been fully read. Spdy's SMInterface does not need
136 // to do any cleanup at this time.
137 // TODO(klindsay) This method is probably not being used properly and
138 // some logic review and method renaming is probably in order.
139 virtual void Cleanup() OVERRIDE {}
140 // Send a settings frame
141 virtual int PostAcceptHook() OVERRIDE;
142 virtual void NewStream(uint32 stream_id,
143 uint32 priority,
144 const std::string& filename) OVERRIDE;
145 void AddToOutputOrder(const MemCacheIter& mci);
146 virtual void SendEOF(uint32 stream_id) OVERRIDE;
147 virtual void SendErrorNotFound(uint32 stream_id) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000148 virtual size_t SendSynStream(uint32 stream_id,
149 const BalsaHeaders& headers) OVERRIDE;
150 virtual size_t SendSynReply(uint32 stream_id,
151 const BalsaHeaders& headers) OVERRIDE;
152 virtual void SendDataFrame(uint32 stream_id, const char* data, int64 len,
153 uint32 flags, bool compress) OVERRIDE;
Torne (Richard Coles)3551c9c2013-08-23 16:39:15 +0100154 BufferedSpdyFramer* spdy_framer() { return buffered_spdy_framer_; }
155
156 const OutputOrdering& output_ordering() const {
157 return client_output_ordering_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000158 }
159
160 static std::string forward_ip_header() { return forward_ip_header_; }
Torne (Richard Coles)3551c9c2013-08-23 16:39:15 +0100161 static void set_forward_ip_header(const std::string& value) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000162 forward_ip_header_ = value;
163 }
164
165 private:
166 void SendEOFImpl(uint32 stream_id);
167 void SendErrorNotFoundImpl(uint32 stream_id);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000168 void KillStream(uint32 stream_id);
169 void CopyHeaders(SpdyHeaderBlock& dest, const BalsaHeaders& headers);
170 size_t SendSynStreamImpl(uint32 stream_id, const BalsaHeaders& headers);
171 size_t SendSynReplyImpl(uint32 stream_id, const BalsaHeaders& headers);
172 void SendDataFrameImpl(uint32 stream_id, const char* data, int64 len,
173 SpdyDataFlags flags, bool compress);
174 void EnqueueDataFrame(DataFrame* df);
175 virtual void GetOutput() OVERRIDE;
Torne (Richard Coles)3551c9c2013-08-23 16:39:15 +0100176
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000177 private:
178 BufferedSpdyFramer* buffered_spdy_framer_;
179 bool valid_spdy_session_; // True if we have seen valid data on this session.
180 // Use this to fail fast when junk is sent to our
181 // port.
182
183 SMConnection* connection_;
184 OutputList* client_output_list_;
185 OutputOrdering client_output_ordering_;
186 uint32 next_outgoing_stream_id_;
187 EpollServer* epoll_server_;
188 FlipAcceptor* acceptor_;
189 MemoryCache* memory_cache_;
190 std::vector<SMInterface*> server_interface_list;
191 std::vector<int32> unused_server_interface_list;
192 typedef std::map<uint32, SMInterface*> StreamToSmif;
193 StreamToSmif stream_to_smif_;
194 bool close_on_error_;
195
196 static std::string forward_ip_header_;
197};
198
199} // namespace net
200
201#endif // NET_TOOLS_FLIP_SERVER_SPDY_INTERFACE_H_