Port Android relocation packer to chromium build
[chromium-blink-merge.git] / net / spdy / buffered_spdy_framer.h
blob7025aad7c74d69abc7047d040afe304c2d513c2f
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_SPDY_BUFFERED_SPDY_FRAMER_H_
6 #define NET_SPDY_BUFFERED_SPDY_FRAMER_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "net/base/net_export.h"
14 #include "net/socket/next_proto.h"
15 #include "net/spdy/spdy_framer.h"
16 #include "net/spdy/spdy_header_block.h"
17 #include "net/spdy/spdy_protocol.h"
19 namespace net {
21 // Returns the SPDY major version corresponding to the given NextProto
22 // value, which must represent a SPDY-like protocol.
23 NET_EXPORT_PRIVATE SpdyMajorVersion NextProtoToSpdyMajorVersion(
24 NextProto next_proto);
26 class NET_EXPORT_PRIVATE BufferedSpdyFramerVisitorInterface {
27 public:
28 BufferedSpdyFramerVisitorInterface() {}
30 // Called if an error is detected in the SpdyFrame protocol.
31 virtual void OnError(SpdyFramer::SpdyError error_code) = 0;
33 // Called if an error is detected in a SPDY stream.
34 virtual void OnStreamError(SpdyStreamId stream_id,
35 const std::string& description) = 0;
37 // Called after all the header data for SYN_STREAM control frame is received.
38 virtual void OnSynStream(SpdyStreamId stream_id,
39 SpdyStreamId associated_stream_id,
40 SpdyPriority priority,
41 bool fin,
42 bool unidirectional,
43 const SpdyHeaderBlock& headers) = 0;
45 // Called after all the header data for SYN_REPLY control frame is received.
46 virtual void OnSynReply(SpdyStreamId stream_id,
47 bool fin,
48 const SpdyHeaderBlock& headers) = 0;
50 // Called after all the header data for HEADERS control frame is received.
51 virtual void OnHeaders(SpdyStreamId stream_id,
52 bool has_priority,
53 SpdyPriority priority,
54 bool fin,
55 const SpdyHeaderBlock& headers) = 0;
57 // Called when a data frame header is received.
58 virtual void OnDataFrameHeader(SpdyStreamId stream_id,
59 size_t length,
60 bool fin) = 0;
62 // Called when data is received.
63 // |stream_id| The stream receiving data.
64 // |data| A buffer containing the data received.
65 // |len| The length of the data buffer (at most 2^24 - 1 for SPDY/3,
66 // but 2^16 - 1 - 8 for SPDY/4).
67 // When the other side has finished sending data on this stream,
68 // this method will be called with a zero-length buffer.
69 virtual void OnStreamFrameData(SpdyStreamId stream_id,
70 const char* data,
71 size_t len,
72 bool fin) = 0;
74 // Called when padding is received (padding length field or padding octets).
75 // |stream_id| The stream receiving data.
76 // |len| The number of padding octets.
77 virtual void OnStreamPadding(SpdyStreamId stream_id, size_t len) = 0;
79 // Called when a SETTINGS frame is received.
80 // |clear_persisted| True if the respective flag is set on the SETTINGS frame.
81 virtual void OnSettings(bool clear_persisted) = 0;
83 // Called when an individual setting within a SETTINGS frame has been parsed
84 // and validated.
85 virtual void OnSetting(SpdySettingsIds id, uint8 flags, uint32 value) = 0;
87 // Called when a SETTINGS frame is received with the ACK flag set.
88 virtual void OnSettingsAck() {}
90 // Called at the completion of parsing SETTINGS id and value tuples.
91 virtual void OnSettingsEnd() {}
93 // Called when a PING frame has been parsed.
94 virtual void OnPing(SpdyPingId unique_id, bool is_ack) = 0;
96 // Called when a RST_STREAM frame has been parsed.
97 virtual void OnRstStream(SpdyStreamId stream_id,
98 SpdyRstStreamStatus status) = 0;
100 // Called when a GOAWAY frame has been parsed.
101 virtual void OnGoAway(SpdyStreamId last_accepted_stream_id,
102 SpdyGoAwayStatus status) = 0;
104 // Called when a WINDOW_UPDATE frame has been parsed.
105 virtual void OnWindowUpdate(SpdyStreamId stream_id,
106 uint32 delta_window_size) = 0;
108 // Called when a PUSH_PROMISE frame has been parsed.
109 virtual void OnPushPromise(SpdyStreamId stream_id,
110 SpdyStreamId promised_stream_id,
111 const SpdyHeaderBlock& headers) = 0;
113 // Called when a frame type we don't recognize is received.
114 // Return true if this appears to be a valid extension frame, false otherwise.
115 // We distinguish between extension frames and nonsense by checking
116 // whether the stream id is valid.
117 virtual bool OnUnknownFrame(SpdyStreamId stream_id, int frame_type) = 0;
119 protected:
120 virtual ~BufferedSpdyFramerVisitorInterface() {}
122 private:
123 DISALLOW_COPY_AND_ASSIGN(BufferedSpdyFramerVisitorInterface);
126 class NET_EXPORT_PRIVATE BufferedSpdyFramer
127 : public SpdyFramerVisitorInterface {
128 public:
129 BufferedSpdyFramer(SpdyMajorVersion version,
130 bool enable_compression);
131 ~BufferedSpdyFramer() override;
133 // Sets callbacks to be called from the buffered spdy framer. A visitor must
134 // be set, or else the framer will likely crash. It is acceptable for the
135 // visitor to do nothing. If this is called multiple times, only the last
136 // visitor will be used.
137 void set_visitor(BufferedSpdyFramerVisitorInterface* visitor);
139 // Set debug callbacks to be called from the framer. The debug visitor is
140 // completely optional and need not be set in order for normal operation.
141 // If this is called multiple times, only the last visitor will be used.
142 void set_debug_visitor(SpdyFramerDebugVisitorInterface* debug_visitor);
144 // SpdyFramerVisitorInterface
145 void OnError(SpdyFramer* spdy_framer) override;
146 void OnSynStream(SpdyStreamId stream_id,
147 SpdyStreamId associated_stream_id,
148 SpdyPriority priority,
149 bool fin,
150 bool unidirectional) override;
151 void OnSynReply(SpdyStreamId stream_id, bool fin) override;
152 void OnHeaders(SpdyStreamId stream_id,
153 bool has_priority,
154 SpdyPriority priority,
155 bool fin,
156 bool end) override;
157 bool OnControlFrameHeaderData(SpdyStreamId stream_id,
158 const char* header_data,
159 size_t len) override;
160 void OnStreamFrameData(SpdyStreamId stream_id,
161 const char* data,
162 size_t len,
163 bool fin) override;
164 void OnStreamPadding(SpdyStreamId stream_id, size_t len) override;
165 void OnSettings(bool clear_persisted) override;
166 void OnSetting(SpdySettingsIds id, uint8 flags, uint32 value) override;
167 void OnSettingsAck() override;
168 void OnSettingsEnd() override;
169 void OnPing(SpdyPingId unique_id, bool is_ack) override;
170 void OnRstStream(SpdyStreamId stream_id, SpdyRstStreamStatus status) override;
171 void OnGoAway(SpdyStreamId last_accepted_stream_id,
172 SpdyGoAwayStatus status) override;
173 void OnWindowUpdate(SpdyStreamId stream_id,
174 uint32 delta_window_size) override;
175 void OnPushPromise(SpdyStreamId stream_id,
176 SpdyStreamId promised_stream_id,
177 bool end) override;
178 void OnDataFrameHeader(SpdyStreamId stream_id,
179 size_t length,
180 bool fin) override;
181 void OnContinuation(SpdyStreamId stream_id, bool end) override;
182 bool OnUnknownFrame(SpdyStreamId stream_id, int frame_type) override;
184 // SpdyFramer methods.
185 size_t ProcessInput(const char* data, size_t len);
186 SpdyMajorVersion protocol_version();
187 void Reset();
188 SpdyFramer::SpdyError error_code() const;
189 SpdyFramer::SpdyState state() const;
190 bool MessageFullyRead();
191 bool HasError();
192 SpdyFrame* CreateSynStream(SpdyStreamId stream_id,
193 SpdyStreamId associated_stream_id,
194 SpdyPriority priority,
195 SpdyControlFlags flags,
196 const SpdyHeaderBlock* headers);
197 SpdyFrame* CreateSynReply(SpdyStreamId stream_id,
198 SpdyControlFlags flags,
199 const SpdyHeaderBlock* headers);
200 SpdyFrame* CreateRstStream(SpdyStreamId stream_id,
201 SpdyRstStreamStatus status) const;
202 SpdyFrame* CreateSettings(const SettingsMap& values) const;
203 SpdyFrame* CreatePingFrame(SpdyPingId unique_id, bool is_ack) const;
204 SpdyFrame* CreateGoAway(
205 SpdyStreamId last_accepted_stream_id,
206 SpdyGoAwayStatus status) const;
207 SpdyFrame* CreateHeaders(SpdyStreamId stream_id,
208 SpdyControlFlags flags,
209 SpdyPriority priority,
210 const SpdyHeaderBlock* headers);
211 SpdyFrame* CreateWindowUpdate(
212 SpdyStreamId stream_id,
213 uint32 delta_window_size) const;
214 SpdyFrame* CreateDataFrame(SpdyStreamId stream_id,
215 const char* data,
216 uint32 len,
217 SpdyDataFlags flags);
218 SpdyFrame* CreatePushPromise(SpdyStreamId stream_id,
219 SpdyStreamId promised_stream_id,
220 const SpdyHeaderBlock* headers);
222 // Serialize a frame of unknown type.
223 SpdySerializedFrame* SerializeFrame(const SpdyFrameIR& frame) {
224 return spdy_framer_.SerializeFrame(frame);
227 SpdyPriority GetHighestPriority() const;
229 size_t GetDataFrameMinimumSize() const {
230 return spdy_framer_.GetDataFrameMinimumSize();
233 size_t GetControlFrameHeaderSize() const {
234 return spdy_framer_.GetControlFrameHeaderSize();
237 size_t GetSynStreamMinimumSize() const {
238 return spdy_framer_.GetSynStreamMinimumSize();
241 size_t GetFrameMinimumSize() const {
242 return spdy_framer_.GetFrameMinimumSize();
245 size_t GetFrameMaximumSize() const {
246 return spdy_framer_.GetFrameMaximumSize();
249 size_t GetDataFrameMaximumPayload() const {
250 return spdy_framer_.GetDataFrameMaximumPayload();
253 int frames_received() const { return frames_received_; }
255 private:
256 // The size of the header_buffer_.
257 enum { kHeaderBufferSize = 32 * 1024 };
259 void InitHeaderStreaming(SpdyStreamId stream_id);
261 SpdyFramer spdy_framer_;
262 BufferedSpdyFramerVisitorInterface* visitor_;
264 // Header block streaming state:
265 char header_buffer_[kHeaderBufferSize];
266 size_t header_buffer_used_;
267 bool header_buffer_valid_;
268 SpdyStreamId header_stream_id_;
269 int frames_received_;
271 // Collection of fields from control frames that we need to
272 // buffer up from the spdy framer.
273 struct ControlFrameFields {
274 SpdyFrameType type;
275 SpdyStreamId stream_id;
276 SpdyStreamId associated_stream_id;
277 SpdyStreamId promised_stream_id;
278 bool has_priority;
279 SpdyPriority priority;
280 uint8 credential_slot;
281 bool fin;
282 bool unidirectional;
284 scoped_ptr<ControlFrameFields> control_frame_fields_;
286 DISALLOW_COPY_AND_ASSIGN(BufferedSpdyFramer);
289 } // namespace net
291 #endif // NET_SPDY_BUFFERED_SPDY_FRAMER_H_