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 #include <limits> | |
8 | 9 |
9 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
14 #include "net/base/big_endian.h" | 15 #include "net/base/big_endian.h" |
16 #include "net/base/io_buffer.h" | |
15 #include "net/websockets/websocket_frame.h" | 17 #include "net/websockets/websocket_frame.h" |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
19 const uint8 kFinalBit = 0x80; | 21 const uint8 kFinalBit = 0x80; |
20 const uint8 kReserved1Bit = 0x40; | 22 const uint8 kReserved1Bit = 0x40; |
21 const uint8 kReserved2Bit = 0x20; | 23 const uint8 kReserved2Bit = 0x20; |
22 const uint8 kReserved3Bit = 0x10; | 24 const uint8 kReserved3Bit = 0x10; |
23 const uint8 kOpCodeMask = 0xF; | 25 const uint8 kOpCodeMask = 0xF; |
24 const uint8 kMaskBit = 0x80; | 26 const uint8 kMaskBit = 0x80; |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 | 173 |
172 scoped_ptr<WebSocketFrameChunk> WebSocketFrameParser::DecodeFramePayload( | 174 scoped_ptr<WebSocketFrameChunk> WebSocketFrameParser::DecodeFramePayload( |
173 bool first_chunk) { | 175 bool first_chunk) { |
174 static const int kMaskingKeyLength = WebSocketFrameHeader::kMaskingKeyLength; | 176 static const int kMaskingKeyLength = WebSocketFrameHeader::kMaskingKeyLength; |
175 | 177 |
176 const char* current = &buffer_.front() + current_read_pos_; | 178 const char* current = &buffer_.front() + current_read_pos_; |
177 const char* end = &buffer_.front() + buffer_.size(); | 179 const char* end = &buffer_.front() + buffer_.size(); |
178 uint64 next_size = std::min<uint64>( | 180 uint64 next_size = std::min<uint64>( |
179 end - current, | 181 end - current, |
180 current_frame_header_->payload_length - frame_offset_); | 182 current_frame_header_->payload_length - frame_offset_); |
183 DCHECK_LE(next_size, static_cast<uint64>(std::numeric_limits<int>::max())); | |
Ryan Sleevi
2012/07/30 07:21:13
nit: Is this more appropriate as a CHECK, rather t
Takashi Toyoshima
2012/07/30 08:01:21
Agreed.
RFC defines status code 1009 meaning "Mes
mmenke
2012/07/30 13:26:57
Just thought I'd point out that these don't corres
| |
181 | 184 |
182 scoped_ptr<WebSocketFrameChunk> frame_chunk(new WebSocketFrameChunk); | 185 scoped_ptr<WebSocketFrameChunk> frame_chunk(new WebSocketFrameChunk); |
183 if (first_chunk) { | 186 if (first_chunk) { |
184 frame_chunk->header.reset(new WebSocketFrameHeader(*current_frame_header_)); | 187 frame_chunk->header.reset(new WebSocketFrameHeader(*current_frame_header_)); |
185 } | 188 } |
186 frame_chunk->final_chunk = false; | 189 frame_chunk->final_chunk = false; |
187 frame_chunk->data.assign(current, current + next_size); | 190 if (next_size) { |
188 if (current_frame_header_->masked) { | 191 frame_chunk->data = new IOBufferWithSize(static_cast<int>(next_size)); |
189 // Unmask the payload. | 192 char* io_data = frame_chunk->data->data(); |
190 // TODO(yutak): This could be faster by doing unmasking for each | 193 memcpy(io_data, current, next_size); |
191 // machine word (instead of each byte). | 194 if (current_frame_header_->masked) { |
192 size_t key_offset = frame_offset_ % kMaskingKeyLength; | 195 // Unmask the payload. |
193 for (uint64 i = 0; i < next_size; ++i) { | 196 // TODO(yutak): This could be faster by doing unmasking for each |
194 frame_chunk->data[i] ^= masking_key_[key_offset]; | 197 // machine word (instead of each byte). |
Ryan Sleevi
2012/07/30 07:21:13
FWIW, when you do go and begin to (micro?)-optimiz
Takashi Toyoshima
2012/07/30 08:01:21
Wow, he made many interesting optimizations.
Thank
| |
195 key_offset = (key_offset + 1) % kMaskingKeyLength; | 198 size_t key_offset = frame_offset_ % kMaskingKeyLength; |
199 for (uint64 i = 0; i < next_size; ++i) { | |
200 io_data[i] ^= masking_key_[key_offset]; | |
201 key_offset = (key_offset + 1) % kMaskingKeyLength; | |
202 } | |
196 } | 203 } |
204 | |
205 current_read_pos_ += next_size; | |
206 frame_offset_ += next_size; | |
197 } | 207 } |
198 | 208 |
199 current_read_pos_ += next_size; | |
200 frame_offset_ += next_size; | |
201 | |
202 DCHECK_LE(frame_offset_, current_frame_header_->payload_length); | 209 DCHECK_LE(frame_offset_, current_frame_header_->payload_length); |
203 if (frame_offset_ == current_frame_header_->payload_length) { | 210 if (frame_offset_ == current_frame_header_->payload_length) { |
204 frame_chunk->final_chunk = true; | 211 frame_chunk->final_chunk = true; |
205 current_frame_header_.reset(); | 212 current_frame_header_.reset(); |
206 frame_offset_ = 0; | 213 frame_offset_ = 0; |
207 } | 214 } |
208 | 215 |
209 return frame_chunk.Pass(); | 216 return frame_chunk.Pass(); |
210 } | 217 } |
211 | 218 |
212 } // namespace net | 219 } // namespace net |
OLD | NEW |