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_
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/spdy/spdy_framer.h"
15 #include "net/spdy/spdy_header_block.h"
16 #include "net/spdy/spdy_protocol.h"
20 class NET_EXPORT_PRIVATE BufferedSpdyFramerVisitorInterface
{
22 BufferedSpdyFramerVisitorInterface() {}
24 // Called if an error is detected in the SpdyFrame protocol.
25 virtual void OnError(SpdyFramer::SpdyError error_code
) = 0;
27 // Called if an error is detected in a SPDY stream.
28 virtual void OnStreamError(SpdyStreamId stream_id
,
29 const std::string
& description
) = 0;
31 // Called after all the header data for SYN_STREAM control frame is received.
32 virtual void OnSynStream(SpdyStreamId stream_id
,
33 SpdyStreamId associated_stream_id
,
34 SpdyPriority priority
,
35 uint8 credential_slot
,
38 const SpdyHeaderBlock
& headers
) = 0;
40 // Called after all the header data for SYN_REPLY control frame is received.
41 virtual void OnSynReply(SpdyStreamId stream_id
,
43 const SpdyHeaderBlock
& headers
) = 0;
45 // Called after all the header data for HEADERS control frame is received.
46 virtual void OnHeaders(SpdyStreamId stream_id
,
48 const SpdyHeaderBlock
& headers
) = 0;
50 // Called when data is received.
51 // |stream_id| The stream receiving data.
52 // |data| A buffer containing the data received.
53 // |len| The length of the data buffer (at most 2^24 - 1 for SPDY/3,
54 // but 2^16 - 1 - 8 for SPDY/4).
55 // When the other side has finished sending data on this stream,
56 // this method will be called with a zero-length buffer.
57 virtual void OnStreamFrameData(SpdyStreamId stream_id
,
62 // Called when an individual setting within a SETTINGS frame has been parsed
64 virtual void OnSetting(SpdySettingsIds id
, uint8 flags
, uint32 value
) = 0;
66 // Called when a PING frame has been parsed.
67 virtual void OnPing(uint32 unique_id
) = 0;
69 // Called when a RST_STREAM frame has been parsed.
70 virtual void OnRstStream(SpdyStreamId stream_id
,
71 SpdyRstStreamStatus status
) = 0;
73 // Called when a GOAWAY frame has been parsed.
74 virtual void OnGoAway(SpdyStreamId last_accepted_stream_id
,
75 SpdyGoAwayStatus status
) = 0;
77 // Called when a WINDOW_UPDATE frame has been parsed.
78 virtual void OnWindowUpdate(SpdyStreamId stream_id
,
79 uint32 delta_window_size
) = 0;
81 // Called after a control frame has been compressed to allow the visitor
82 // to record compression statistics.
83 virtual void OnSynStreamCompressed(
84 size_t uncompressed_size
,
85 size_t compressed_size
) = 0;
88 virtual ~BufferedSpdyFramerVisitorInterface() {}
91 DISALLOW_COPY_AND_ASSIGN(BufferedSpdyFramerVisitorInterface
);
94 class NET_EXPORT_PRIVATE BufferedSpdyFramer
95 : public SpdyFramerVisitorInterface
{
97 BufferedSpdyFramer(int version
,
98 bool enable_compression
);
99 virtual ~BufferedSpdyFramer();
101 // Sets callbacks to be called from the buffered spdy framer. A visitor must
102 // be set, or else the framer will likely crash. It is acceptable for the
103 // visitor to do nothing. If this is called multiple times, only the last
104 // visitor will be used.
105 void set_visitor(BufferedSpdyFramerVisitorInterface
* visitor
);
107 // SpdyFramerVisitorInterface
108 virtual void OnError(SpdyFramer
* spdy_framer
) OVERRIDE
;
109 virtual void OnSynStream(SpdyStreamId stream_id
,
110 SpdyStreamId associated_stream_id
,
111 SpdyPriority priority
,
112 uint8 credential_slot
,
114 bool unidirectional
) OVERRIDE
;
115 virtual void OnSynReply(SpdyStreamId stream_id
, bool fin
) OVERRIDE
;
116 virtual void OnHeaders(SpdyStreamId stream_id
, bool fin
) OVERRIDE
;
117 virtual bool OnCredentialFrameData(const char* frame_data
,
118 size_t len
) OVERRIDE
;
119 virtual bool OnControlFrameHeaderData(SpdyStreamId stream_id
,
120 const char* header_data
,
121 size_t len
) OVERRIDE
;
122 virtual void OnStreamFrameData(SpdyStreamId stream_id
,
126 virtual void OnSetting(
127 SpdySettingsIds id
, uint8 flags
, uint32 value
) OVERRIDE
;
128 virtual void OnPing(uint32 unique_id
) OVERRIDE
;
129 virtual void OnRstStream(SpdyStreamId stream_id
,
130 SpdyRstStreamStatus status
) OVERRIDE
;
131 virtual void OnGoAway(SpdyStreamId last_accepted_stream_id
,
132 SpdyGoAwayStatus status
) OVERRIDE
;
133 virtual void OnWindowUpdate(SpdyStreamId stream_id
,
134 uint32 delta_window_size
) OVERRIDE
;
135 virtual void OnDataFrameHeader(SpdyStreamId stream_id
,
139 // Called after a syn stream control frame has been compressed to
140 // allow the visitor to record compression statistics.
141 virtual void OnSynStreamCompressed(
142 size_t uncompressed_size
,
143 size_t compressed_size
) OVERRIDE
;
145 // SpdyFramer methods.
146 size_t ProcessInput(const char* data
, size_t len
);
147 int protocol_version();
149 SpdyFramer::SpdyError
error_code() const;
150 SpdyFramer::SpdyState
state() const;
151 bool MessageFullyRead();
153 SpdyFrame
* CreateSynStream(SpdyStreamId stream_id
,
154 SpdyStreamId associated_stream_id
,
155 SpdyPriority priority
,
156 uint8 credential_slot
,
157 SpdyControlFlags flags
,
159 const SpdyHeaderBlock
* headers
);
160 SpdyFrame
* CreateSynReply(SpdyStreamId stream_id
,
161 SpdyControlFlags flags
,
163 const SpdyHeaderBlock
* headers
);
164 SpdyFrame
* CreateRstStream(SpdyStreamId stream_id
,
165 SpdyRstStreamStatus status
) const;
166 SpdyFrame
* CreateSettings(const SettingsMap
& values
) const;
167 SpdyFrame
* CreatePingFrame(uint32 unique_id
) const;
168 SpdyFrame
* CreateGoAway(
169 SpdyStreamId last_accepted_stream_id
,
170 SpdyGoAwayStatus status
) const;
171 SpdyFrame
* CreateHeaders(SpdyStreamId stream_id
,
172 SpdyControlFlags flags
,
174 const SpdyHeaderBlock
* headers
);
175 SpdyFrame
* CreateWindowUpdate(
176 SpdyStreamId stream_id
,
177 uint32 delta_window_size
) const;
178 SpdyFrame
* CreateCredentialFrame(
179 const SpdyCredential
& credential
) const;
180 SpdyFrame
* CreateDataFrame(SpdyStreamId stream_id
,
183 SpdyDataFlags flags
);
184 SpdyPriority
GetHighestPriority() const;
186 size_t GetDataFrameMinimumSize() const {
187 return spdy_framer_
.GetDataFrameMinimumSize();
190 size_t GetControlFrameHeaderSize() const {
191 return spdy_framer_
.GetControlFrameHeaderSize();
194 size_t GetFrameMinimumSize() const {
195 return spdy_framer_
.GetFrameMinimumSize();
198 size_t GetFrameMaximumSize() const {
199 return spdy_framer_
.GetFrameMaximumSize();
202 size_t GetDataFrameMaximumPayload() const {
203 return spdy_framer_
.GetDataFrameMaximumPayload();
206 int frames_received() const { return frames_received_
; }
209 // The size of the header_buffer_.
210 enum { kHeaderBufferSize
= 32 * 1024 };
212 void InitHeaderStreaming(SpdyStreamId stream_id
);
214 SpdyFramer spdy_framer_
;
215 BufferedSpdyFramerVisitorInterface
* visitor_
;
217 // Header block streaming state:
218 char header_buffer_
[kHeaderBufferSize
];
219 size_t header_buffer_used_
;
220 bool header_buffer_valid_
;
221 SpdyStreamId header_stream_id_
;
222 int frames_received_
;
224 // Collection of fields from control frames that we need to
225 // buffer up from the spdy framer.
226 struct ControlFrameFields
{
228 SpdyStreamId stream_id
;
229 SpdyStreamId associated_stream_id
;
230 SpdyPriority priority
;
231 uint8 credential_slot
;
235 scoped_ptr
<ControlFrameFields
> control_frame_fields_
;
237 DISALLOW_COPY_AND_ASSIGN(BufferedSpdyFramer
);
242 #endif // NET_SPDY_BUFFERED_SPDY_FRAMER_H_