Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(422)

Side by Side Diff: net/websockets/websocket_frame_parser.cc

Issue 10796107: WebSocketFrameChunk should use IOBuffer to hold data (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: revised Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/websockets/websocket_frame_parser.h" 5 #include "net/websockets/websocket_frame_parser.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h" 13 #include "base/memory/scoped_vector.h"
14 #include "net/base/big_endian.h" 14 #include "net/base/big_endian.h"
15 #include "net/base/io_buffer.h"
15 #include "net/websockets/websocket_frame.h" 16 #include "net/websockets/websocket_frame.h"
16 17
17 namespace { 18 namespace {
18 19
19 const uint8 kFinalBit = 0x80; 20 const uint8 kFinalBit = 0x80;
20 const uint8 kReserved1Bit = 0x40; 21 const uint8 kReserved1Bit = 0x40;
21 const uint8 kReserved2Bit = 0x20; 22 const uint8 kReserved2Bit = 0x20;
22 const uint8 kReserved3Bit = 0x10; 23 const uint8 kReserved3Bit = 0x10;
23 const uint8 kOpCodeMask = 0xF; 24 const uint8 kOpCodeMask = 0xF;
24 const uint8 kMaskBit = 0x80; 25 const uint8 kMaskBit = 0x80;
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 } 171 }
171 172
172 scoped_ptr<WebSocketFrameChunk> WebSocketFrameParser::DecodeFramePayload( 173 scoped_ptr<WebSocketFrameChunk> WebSocketFrameParser::DecodeFramePayload(
173 bool first_chunk) { 174 bool first_chunk) {
174 static const int kMaskingKeyLength = WebSocketFrameHeader::kMaskingKeyLength; 175 static const int kMaskingKeyLength = WebSocketFrameHeader::kMaskingKeyLength;
175 176
176 const char* current = &buffer_.front() + current_read_pos_; 177 const char* current = &buffer_.front() + current_read_pos_;
177 const char* end = &buffer_.front() + buffer_.size(); 178 const char* end = &buffer_.front() + buffer_.size();
178 uint64 next_size = std::min<uint64>( 179 uint64 next_size = std::min<uint64>(
179 end - current, 180 end - current,
180 current_frame_header_->payload_length - frame_offset_); 181 current_frame_header_->payload_length - frame_offset_);
mmenke 2012/07/27 14:25:55 Might want to DCHECK_LE(next_size, std::numeric_li
Takashi Toyoshima 2012/07/27 16:08:30 Done.
181 182
182 scoped_ptr<WebSocketFrameChunk> frame_chunk(new WebSocketFrameChunk); 183 scoped_ptr<WebSocketFrameChunk> frame_chunk(new WebSocketFrameChunk);
183 if (first_chunk) { 184 if (first_chunk) {
184 frame_chunk->header.reset(new WebSocketFrameHeader(*current_frame_header_)); 185 frame_chunk->header.reset(new WebSocketFrameHeader(*current_frame_header_));
185 } 186 }
186 frame_chunk->final_chunk = false; 187 frame_chunk->final_chunk = false;
187 frame_chunk->data.assign(current, current + next_size); 188 frame_chunk->data = new IOBufferWithSize(next_size);
mmenke 2012/07/27 14:25:55 This doesn't give a warning without casting next_s
Takashi Toyoshima 2012/07/27 16:08:30 Humm... my local linux build passes it without any
189 DCHECK(frame_chunk->data);
mmenke 2012/07/27 14:25:55 Not needed. We crash on any allocation failure.
Takashi Toyoshima 2012/07/27 16:08:30 Done.
190 char* io_data = frame_chunk->data->data();
191 memcpy(io_data, current, next_size);
188 if (current_frame_header_->masked) { 192 if (current_frame_header_->masked) {
189 // Unmask the payload. 193 // Unmask the payload.
190 // TODO(yutak): This could be faster by doing unmasking for each 194 // TODO(yutak): This could be faster by doing unmasking for each
191 // machine word (instead of each byte). 195 // machine word (instead of each byte).
192 size_t key_offset = frame_offset_ % kMaskingKeyLength; 196 size_t key_offset = frame_offset_ % kMaskingKeyLength;
193 for (uint64 i = 0; i < next_size; ++i) { 197 for (uint64 i = 0; i < next_size; ++i) {
194 frame_chunk->data[i] ^= masking_key_[key_offset]; 198 io_data[i] ^= masking_key_[key_offset];
195 key_offset = (key_offset + 1) % kMaskingKeyLength; 199 key_offset = (key_offset + 1) % kMaskingKeyLength;
196 } 200 }
197 } 201 }
198 202
199 current_read_pos_ += next_size; 203 current_read_pos_ += next_size;
200 frame_offset_ += next_size; 204 frame_offset_ += next_size;
201 205
202 DCHECK_LE(frame_offset_, current_frame_header_->payload_length); 206 DCHECK_LE(frame_offset_, current_frame_header_->payload_length);
203 if (frame_offset_ == current_frame_header_->payload_length) { 207 if (frame_offset_ == current_frame_header_->payload_length) {
204 frame_chunk->final_chunk = true; 208 frame_chunk->final_chunk = true;
205 current_frame_header_.reset(); 209 current_frame_header_.reset();
206 frame_offset_ = 0; 210 frame_offset_ = 0;
207 } 211 }
208 212
209 return frame_chunk.Pass(); 213 return frame_chunk.Pass();
210 } 214 }
211 215
212 } // namespace net 216 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698