OLD | NEW |
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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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_); |
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 IOBuffer(next_size); |
| 189 frame_chunk->size = next_size; |
| 190 DCHECK(frame_chunk->data); |
| 191 char* io_data = frame_chunk->data->data(); |
| 192 memcpy(io_data, current, next_size); |
188 if (current_frame_header_->masked) { | 193 if (current_frame_header_->masked) { |
189 // Unmask the payload. | 194 // Unmask the payload. |
190 // TODO(yutak): This could be faster by doing unmasking for each | 195 // TODO(yutak): This could be faster by doing unmasking for each |
191 // machine word (instead of each byte). | 196 // machine word (instead of each byte). |
192 size_t key_offset = frame_offset_ % kMaskingKeyLength; | 197 size_t key_offset = frame_offset_ % kMaskingKeyLength; |
193 for (uint64 i = 0; i < next_size; ++i) { | 198 for (uint64 i = 0; i < next_size; ++i) { |
194 frame_chunk->data[i] ^= masking_key_[key_offset]; | 199 io_data[i] ^= masking_key_[key_offset]; |
195 key_offset = (key_offset + 1) % kMaskingKeyLength; | 200 key_offset = (key_offset + 1) % kMaskingKeyLength; |
196 } | 201 } |
197 } | 202 } |
198 | 203 |
199 current_read_pos_ += next_size; | 204 current_read_pos_ += next_size; |
200 frame_offset_ += next_size; | 205 frame_offset_ += next_size; |
201 | 206 |
202 DCHECK_LE(frame_offset_, current_frame_header_->payload_length); | 207 DCHECK_LE(frame_offset_, current_frame_header_->payload_length); |
203 if (frame_offset_ == current_frame_header_->payload_length) { | 208 if (frame_offset_ == current_frame_header_->payload_length) { |
204 frame_chunk->final_chunk = true; | 209 frame_chunk->final_chunk = true; |
205 current_frame_header_.reset(); | 210 current_frame_header_.reset(); |
206 frame_offset_ = 0; | 211 frame_offset_ = 0; |
207 } | 212 } |
208 | 213 |
209 return frame_chunk.Pass(); | 214 return frame_chunk.Pass(); |
210 } | 215 } |
211 | 216 |
212 } // namespace net | 217 } // namespace net |
OLD | NEW |