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 #include <limits> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 18 matching lines...) Expand all Loading... | |
29 const uint64 kPayloadLengthWithTwoByteExtendedLengthField = 126; | 29 const uint64 kPayloadLengthWithTwoByteExtendedLengthField = 126; |
30 const uint64 kPayloadLengthWithEightByteExtendedLengthField = 127; | 30 const uint64 kPayloadLengthWithEightByteExtendedLengthField = 127; |
31 | 31 |
32 } // Unnamed namespace. | 32 } // Unnamed namespace. |
33 | 33 |
34 namespace net { | 34 namespace net { |
35 | 35 |
36 WebSocketFrameParser::WebSocketFrameParser() | 36 WebSocketFrameParser::WebSocketFrameParser() |
37 : current_read_pos_(0), | 37 : current_read_pos_(0), |
38 frame_offset_(0), | 38 frame_offset_(0), |
39 failed_(false) { | 39 failed_(false), |
40 error_code_(WS_OK) { | |
40 std::fill(masking_key_, | 41 std::fill(masking_key_, |
41 masking_key_ + WebSocketFrameHeader::kMaskingKeyLength, | 42 masking_key_ + WebSocketFrameHeader::kMaskingKeyLength, |
42 '\0'); | 43 '\0'); |
43 } | 44 } |
44 | 45 |
45 WebSocketFrameParser::~WebSocketFrameParser() { | 46 WebSocketFrameParser::~WebSocketFrameParser() { |
46 } | 47 } |
47 | 48 |
48 bool WebSocketFrameParser::Decode( | 49 bool WebSocketFrameParser::Decode( |
49 const char* data, | 50 const char* data, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 uint8 second_byte = *current++; | 115 uint8 second_byte = *current++; |
115 | 116 |
116 bool final = (first_byte & kFinalBit) != 0; | 117 bool final = (first_byte & kFinalBit) != 0; |
117 bool reserved1 = (first_byte & kReserved1Bit) != 0; | 118 bool reserved1 = (first_byte & kReserved1Bit) != 0; |
118 bool reserved2 = (first_byte & kReserved2Bit) != 0; | 119 bool reserved2 = (first_byte & kReserved2Bit) != 0; |
119 bool reserved3 = (first_byte & kReserved3Bit) != 0; | 120 bool reserved3 = (first_byte & kReserved3Bit) != 0; |
120 OpCode opcode = first_byte & kOpCodeMask; | 121 OpCode opcode = first_byte & kOpCodeMask; |
121 | 122 |
122 bool masked = (second_byte & kMaskBit) != 0; | 123 bool masked = (second_byte & kMaskBit) != 0; |
123 uint64 payload_length = second_byte & kPayloadLengthMask; | 124 uint64 payload_length = second_byte & kPayloadLengthMask; |
124 bool valid_length_format = true; | |
125 bool message_too_big = false; | |
126 if (payload_length == kPayloadLengthWithTwoByteExtendedLengthField) { | 125 if (payload_length == kPayloadLengthWithTwoByteExtendedLengthField) { |
127 if (end - current < 2) | 126 if (end - current < 2) |
128 return; | 127 return; |
129 uint16 payload_length_16; | 128 uint16 payload_length_16; |
130 ReadBigEndian(current, &payload_length_16); | 129 ReadBigEndian(current, &payload_length_16); |
131 current += 2; | 130 current += 2; |
132 payload_length = payload_length_16; | 131 payload_length = payload_length_16; |
133 if (payload_length <= kMaxPayloadLengthWithoutExtendedLengthField) | 132 if (payload_length <= kMaxPayloadLengthWithoutExtendedLengthField) |
134 valid_length_format = false; | 133 error_code_ = WS_ERR_PROTOCOL_ERROR; |
135 } else if (payload_length == kPayloadLengthWithEightByteExtendedLengthField) { | 134 } else if (payload_length == kPayloadLengthWithEightByteExtendedLengthField) { |
136 if (end - current < 8) | 135 if (end - current < 8) |
137 return; | 136 return; |
138 ReadBigEndian(current, &payload_length); | 137 ReadBigEndian(current, &payload_length); |
139 current += 8; | 138 current += 8; |
140 if (payload_length <= kuint16max || | 139 if (payload_length <= kuint16max || |
141 payload_length > static_cast<uint64>(kint64max)) { | 140 payload_length > static_cast<uint64>(kint64max)) |
mmenke
2012/08/01 14:44:59
nit: Google style (Or maybe Chrome style, can't r
| |
142 valid_length_format = false; | 141 error_code_ = WS_ERR_PROTOCOL_ERROR; |
143 } | 142 else if (payload_length > static_cast<uint64>(kint32max)) |
144 if (payload_length > static_cast<uint64>(kint32max)) | 143 error_code_ = WS_ERR_MESSAGE_TOO_BIG; |
145 message_too_big = true; | |
146 } | 144 } |
147 if (!valid_length_format || message_too_big) { | 145 if (error_code_ != WS_OK) { |
148 failed_ = true; | 146 failed_ = true; |
149 buffer_.clear(); | 147 buffer_.clear(); |
150 current_read_pos_ = 0; | 148 current_read_pos_ = 0; |
151 current_frame_header_.reset(); | 149 current_frame_header_.reset(); |
152 frame_offset_ = 0; | 150 frame_offset_ = 0; |
153 return; | 151 return; |
154 } | 152 } |
155 | 153 |
156 if (masked) { | 154 if (masked) { |
157 if (end - current < kMaskingKeyLength) | 155 if (end - current < kMaskingKeyLength) |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 if (frame_offset_ == current_frame_header_->payload_length) { | 213 if (frame_offset_ == current_frame_header_->payload_length) { |
216 frame_chunk->final_chunk = true; | 214 frame_chunk->final_chunk = true; |
217 current_frame_header_.reset(); | 215 current_frame_header_.reset(); |
218 frame_offset_ = 0; | 216 frame_offset_ = 0; |
219 } | 217 } |
220 | 218 |
221 return frame_chunk.Pass(); | 219 return frame_chunk.Pass(); |
222 } | 220 } |
223 | 221 |
224 } // namespace net | 222 } // namespace net |
OLD | NEW |