Using pre-existing constants instead of hard-coding
[chromium-blink-merge.git] / net / spdy / buffered_spdy_framer.cc
blob5b95be2af2921c21c1a7d9b1d27a7525275791df
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 #include "net/spdy/buffered_spdy_framer.h"
7 #include "base/logging.h"
9 namespace net {
11 SpdyMajorVersion NextProtoToSpdyMajorVersion(NextProto next_proto) {
12 switch (next_proto) {
13 case kProtoDeprecatedSPDY2:
14 return SPDY2;
15 case kProtoSPDY3:
16 case kProtoSPDY31:
17 return SPDY3;
18 case kProtoSPDY4:
19 return SPDY4;
20 case kProtoUnknown:
21 case kProtoHTTP11:
22 case kProtoQUIC1SPDY3:
23 break;
25 NOTREACHED();
26 return SPDY2;
29 BufferedSpdyFramer::BufferedSpdyFramer(SpdyMajorVersion version,
30 bool enable_compression)
31 : spdy_framer_(version),
32 visitor_(NULL),
33 header_buffer_used_(0),
34 header_buffer_valid_(false),
35 header_stream_id_(SpdyFramer::kInvalidStream),
36 frames_received_(0) {
37 spdy_framer_.set_enable_compression(enable_compression);
38 memset(header_buffer_, 0, sizeof(header_buffer_));
41 BufferedSpdyFramer::~BufferedSpdyFramer() {
44 void BufferedSpdyFramer::set_visitor(
45 BufferedSpdyFramerVisitorInterface* visitor) {
46 visitor_ = visitor;
47 spdy_framer_.set_visitor(this);
50 void BufferedSpdyFramer::set_debug_visitor(
51 SpdyFramerDebugVisitorInterface* debug_visitor) {
52 spdy_framer_.set_debug_visitor(debug_visitor);
55 void BufferedSpdyFramer::OnError(SpdyFramer* spdy_framer) {
56 DCHECK(spdy_framer);
57 visitor_->OnError(spdy_framer->error_code());
60 void BufferedSpdyFramer::OnSynStream(SpdyStreamId stream_id,
61 SpdyStreamId associated_stream_id,
62 SpdyPriority priority,
63 bool fin,
64 bool unidirectional) {
65 frames_received_++;
66 DCHECK(!control_frame_fields_.get());
67 control_frame_fields_.reset(new ControlFrameFields());
68 control_frame_fields_->type = SYN_STREAM;
69 control_frame_fields_->stream_id = stream_id;
70 control_frame_fields_->associated_stream_id = associated_stream_id;
71 control_frame_fields_->priority = priority;
72 control_frame_fields_->fin = fin;
73 control_frame_fields_->unidirectional = unidirectional;
75 InitHeaderStreaming(stream_id);
78 void BufferedSpdyFramer::OnHeaders(SpdyStreamId stream_id,
79 bool fin,
80 bool end) {
81 frames_received_++;
82 DCHECK(!control_frame_fields_.get());
83 control_frame_fields_.reset(new ControlFrameFields());
84 control_frame_fields_->type = HEADERS;
85 control_frame_fields_->stream_id = stream_id;
86 control_frame_fields_->fin = fin;
88 InitHeaderStreaming(stream_id);
91 void BufferedSpdyFramer::OnSynReply(SpdyStreamId stream_id,
92 bool fin) {
93 frames_received_++;
94 DCHECK(!control_frame_fields_.get());
95 control_frame_fields_.reset(new ControlFrameFields());
96 control_frame_fields_->type = SYN_REPLY;
97 control_frame_fields_->stream_id = stream_id;
98 control_frame_fields_->fin = fin;
100 InitHeaderStreaming(stream_id);
103 bool BufferedSpdyFramer::OnControlFrameHeaderData(SpdyStreamId stream_id,
104 const char* header_data,
105 size_t len) {
106 CHECK_EQ(header_stream_id_, stream_id);
108 if (len == 0) {
109 // Indicates end-of-header-block.
110 CHECK(header_buffer_valid_);
112 SpdyHeaderBlock headers;
113 size_t parsed_len = spdy_framer_.ParseHeaderBlockInBuffer(
114 header_buffer_, header_buffer_used_, &headers);
115 // TODO(rch): this really should be checking parsed_len != len,
116 // but a bunch of tests fail. Need to figure out why.
117 if (parsed_len == 0) {
118 visitor_->OnStreamError(
119 stream_id, "Could not parse Spdy Control Frame Header.");
120 return false;
122 DCHECK(control_frame_fields_.get());
123 switch (control_frame_fields_->type) {
124 case SYN_STREAM:
125 visitor_->OnSynStream(control_frame_fields_->stream_id,
126 control_frame_fields_->associated_stream_id,
127 control_frame_fields_->priority,
128 control_frame_fields_->fin,
129 control_frame_fields_->unidirectional,
130 headers);
131 break;
132 case SYN_REPLY:
133 visitor_->OnSynReply(control_frame_fields_->stream_id,
134 control_frame_fields_->fin,
135 headers);
136 break;
137 case HEADERS:
138 visitor_->OnHeaders(control_frame_fields_->stream_id,
139 control_frame_fields_->fin,
140 headers);
141 break;
142 case PUSH_PROMISE:
143 DCHECK_LT(SPDY3, protocol_version());
144 visitor_->OnPushPromise(control_frame_fields_->stream_id,
145 control_frame_fields_->promised_stream_id,
146 headers);
147 break;
148 default:
149 DCHECK(false) << "Unexpect control frame type: "
150 << control_frame_fields_->type;
151 break;
153 control_frame_fields_.reset(NULL);
154 return true;
157 const size_t available = kHeaderBufferSize - header_buffer_used_;
158 if (len > available) {
159 header_buffer_valid_ = false;
160 visitor_->OnStreamError(
161 stream_id, "Received more data than the allocated size.");
162 return false;
164 memcpy(header_buffer_ + header_buffer_used_, header_data, len);
165 header_buffer_used_ += len;
166 return true;
169 void BufferedSpdyFramer::OnDataFrameHeader(SpdyStreamId stream_id,
170 size_t length,
171 bool fin) {
172 frames_received_++;
173 header_stream_id_ = stream_id;
174 visitor_->OnDataFrameHeader(stream_id, length, fin);
177 void BufferedSpdyFramer::OnStreamFrameData(SpdyStreamId stream_id,
178 const char* data,
179 size_t len,
180 bool fin) {
181 visitor_->OnStreamFrameData(stream_id, data, len, fin);
184 void BufferedSpdyFramer::OnSettings(bool clear_persisted) {
185 visitor_->OnSettings(clear_persisted);
188 void BufferedSpdyFramer::OnSetting(SpdySettingsIds id,
189 uint8 flags,
190 uint32 value) {
191 visitor_->OnSetting(id, flags, value);
194 void BufferedSpdyFramer::OnSettingsAck() {
195 visitor_->OnSettingsAck();
198 void BufferedSpdyFramer::OnSettingsEnd() {
199 visitor_->OnSettingsEnd();
202 void BufferedSpdyFramer::OnPing(SpdyPingId unique_id, bool is_ack) {
203 visitor_->OnPing(unique_id, is_ack);
206 void BufferedSpdyFramer::OnRstStream(SpdyStreamId stream_id,
207 SpdyRstStreamStatus status) {
208 visitor_->OnRstStream(stream_id, status);
210 void BufferedSpdyFramer::OnGoAway(SpdyStreamId last_accepted_stream_id,
211 SpdyGoAwayStatus status) {
212 visitor_->OnGoAway(last_accepted_stream_id, status);
215 void BufferedSpdyFramer::OnWindowUpdate(SpdyStreamId stream_id,
216 uint32 delta_window_size) {
217 visitor_->OnWindowUpdate(stream_id, delta_window_size);
220 void BufferedSpdyFramer::OnPushPromise(SpdyStreamId stream_id,
221 SpdyStreamId promised_stream_id,
222 bool end) {
223 DCHECK_LT(SPDY3, protocol_version());
224 frames_received_++;
225 DCHECK(!control_frame_fields_.get());
226 control_frame_fields_.reset(new ControlFrameFields());
227 control_frame_fields_->type = PUSH_PROMISE;
228 control_frame_fields_->stream_id = stream_id;
229 control_frame_fields_->promised_stream_id = promised_stream_id;
231 InitHeaderStreaming(stream_id);
234 void BufferedSpdyFramer::OnContinuation(SpdyStreamId stream_id, bool end) {
237 SpdyMajorVersion BufferedSpdyFramer::protocol_version() {
238 return spdy_framer_.protocol_version();
241 size_t BufferedSpdyFramer::ProcessInput(const char* data, size_t len) {
242 return spdy_framer_.ProcessInput(data, len);
245 void BufferedSpdyFramer::Reset() {
246 spdy_framer_.Reset();
249 SpdyFramer::SpdyError BufferedSpdyFramer::error_code() const {
250 return spdy_framer_.error_code();
253 SpdyFramer::SpdyState BufferedSpdyFramer::state() const {
254 return spdy_framer_.state();
257 bool BufferedSpdyFramer::MessageFullyRead() {
258 return state() == SpdyFramer::SPDY_AUTO_RESET;
261 bool BufferedSpdyFramer::HasError() {
262 return spdy_framer_.HasError();
265 // TODO(jgraettinger): Eliminate uses of this method (prefer
266 // SpdySynStreamIR).
267 SpdyFrame* BufferedSpdyFramer::CreateSynStream(
268 SpdyStreamId stream_id,
269 SpdyStreamId associated_stream_id,
270 SpdyPriority priority,
271 SpdyControlFlags flags,
272 const SpdyHeaderBlock* headers) {
273 SpdySynStreamIR syn_stream(stream_id);
274 syn_stream.set_associated_to_stream_id(associated_stream_id);
275 syn_stream.set_priority(priority);
276 syn_stream.set_fin((flags & CONTROL_FLAG_FIN) != 0);
277 syn_stream.set_unidirectional((flags & CONTROL_FLAG_UNIDIRECTIONAL) != 0);
278 // TODO(hkhalil): Avoid copy here.
279 syn_stream.set_name_value_block(*headers);
280 return spdy_framer_.SerializeSynStream(syn_stream);
283 // TODO(jgraettinger): Eliminate uses of this method (prefer
284 // SpdySynReplyIR).
285 SpdyFrame* BufferedSpdyFramer::CreateSynReply(
286 SpdyStreamId stream_id,
287 SpdyControlFlags flags,
288 const SpdyHeaderBlock* headers) {
289 SpdySynReplyIR syn_reply(stream_id);
290 syn_reply.set_fin(flags & CONTROL_FLAG_FIN);
291 // TODO(hkhalil): Avoid copy here.
292 syn_reply.set_name_value_block(*headers);
293 return spdy_framer_.SerializeSynReply(syn_reply);
296 // TODO(jgraettinger): Eliminate uses of this method (prefer
297 // SpdyRstStreamIR).
298 SpdyFrame* BufferedSpdyFramer::CreateRstStream(
299 SpdyStreamId stream_id,
300 SpdyRstStreamStatus status) const {
301 SpdyRstStreamIR rst_ir(stream_id, status, "RST");
302 return spdy_framer_.SerializeRstStream(rst_ir);
305 // TODO(jgraettinger): Eliminate uses of this method (prefer
306 // SpdySettingsIR).
307 SpdyFrame* BufferedSpdyFramer::CreateSettings(
308 const SettingsMap& values) const {
309 SpdySettingsIR settings_ir;
310 for (SettingsMap::const_iterator it = values.begin();
311 it != values.end();
312 ++it) {
313 settings_ir.AddSetting(
314 it->first,
315 (it->second.first & SETTINGS_FLAG_PLEASE_PERSIST) != 0,
316 (it->second.first & SETTINGS_FLAG_PERSISTED) != 0,
317 it->second.second);
319 return spdy_framer_.SerializeSettings(settings_ir);
322 // TODO(jgraettinger): Eliminate uses of this method (prefer SpdyPingIR).
323 SpdyFrame* BufferedSpdyFramer::CreatePingFrame(uint32 unique_id,
324 bool is_ack) const {
325 SpdyPingIR ping_ir(unique_id);
326 ping_ir.set_is_ack(is_ack);
327 return spdy_framer_.SerializePing(ping_ir);
330 // TODO(jgraettinger): Eliminate uses of this method (prefer SpdyGoAwayIR).
331 SpdyFrame* BufferedSpdyFramer::CreateGoAway(
332 SpdyStreamId last_accepted_stream_id,
333 SpdyGoAwayStatus status) const {
334 SpdyGoAwayIR go_ir(last_accepted_stream_id, status, "");
335 return spdy_framer_.SerializeGoAway(go_ir);
338 // TODO(jgraettinger): Eliminate uses of this method (prefer SpdyHeadersIR).
339 SpdyFrame* BufferedSpdyFramer::CreateHeaders(
340 SpdyStreamId stream_id,
341 SpdyControlFlags flags,
342 const SpdyHeaderBlock* headers) {
343 SpdyHeadersIR headers_ir(stream_id);
344 headers_ir.set_fin((flags & CONTROL_FLAG_FIN) != 0);
345 headers_ir.set_name_value_block(*headers);
346 return spdy_framer_.SerializeHeaders(headers_ir);
349 // TODO(jgraettinger): Eliminate uses of this method (prefer
350 // SpdyWindowUpdateIR).
351 SpdyFrame* BufferedSpdyFramer::CreateWindowUpdate(
352 SpdyStreamId stream_id,
353 uint32 delta_window_size) const {
354 SpdyWindowUpdateIR update_ir(stream_id, delta_window_size);
355 return spdy_framer_.SerializeWindowUpdate(update_ir);
358 // TODO(jgraettinger): Eliminate uses of this method (prefer SpdyDataIR).
359 SpdyFrame* BufferedSpdyFramer::CreateDataFrame(SpdyStreamId stream_id,
360 const char* data,
361 uint32 len,
362 SpdyDataFlags flags) {
363 SpdyDataIR data_ir(stream_id,
364 base::StringPiece(data, len));
365 data_ir.set_fin((flags & DATA_FLAG_FIN) != 0);
366 return spdy_framer_.SerializeData(data_ir);
369 // TODO(jgraettinger): Eliminate uses of this method (prefer SpdyPushPromiseIR).
370 SpdyFrame* BufferedSpdyFramer::CreatePushPromise(
371 SpdyStreamId stream_id,
372 SpdyStreamId promised_stream_id,
373 const SpdyHeaderBlock* headers) {
374 SpdyPushPromiseIR push_promise_ir(stream_id, promised_stream_id);
375 push_promise_ir.set_name_value_block(*headers);
376 return spdy_framer_.SerializePushPromise(push_promise_ir);
379 SpdyPriority BufferedSpdyFramer::GetHighestPriority() const {
380 return spdy_framer_.GetHighestPriority();
383 void BufferedSpdyFramer::InitHeaderStreaming(SpdyStreamId stream_id) {
384 memset(header_buffer_, 0, kHeaderBufferSize);
385 header_buffer_used_ = 0;
386 header_buffer_valid_ = true;
387 header_stream_id_ = stream_id;
388 DCHECK_NE(header_stream_id_, SpdyFramer::kInvalidStream);
391 } // namespace net