| 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" |
| 11 #include "base/big_endian.h" | 11 #include "base/big_endian.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
| 16 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 17 #include "net/websockets/websocket_frame.h" | 17 #include "net/websockets/websocket_frame.h" |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 const uint8 kFinalBit = 0x80; | 21 const uint8_t kFinalBit = 0x80; |
| 22 const uint8 kReserved1Bit = 0x40; | 22 const uint8_t kReserved1Bit = 0x40; |
| 23 const uint8 kReserved2Bit = 0x20; | 23 const uint8_t kReserved2Bit = 0x20; |
| 24 const uint8 kReserved3Bit = 0x10; | 24 const uint8_t kReserved3Bit = 0x10; |
| 25 const uint8 kOpCodeMask = 0xF; | 25 const uint8_t kOpCodeMask = 0xF; |
| 26 const uint8 kMaskBit = 0x80; | 26 const uint8_t kMaskBit = 0x80; |
| 27 const uint8 kPayloadLengthMask = 0x7F; | 27 const uint8_t kPayloadLengthMask = 0x7F; |
| 28 const uint64 kMaxPayloadLengthWithoutExtendedLengthField = 125; | 28 const uint64_t kMaxPayloadLengthWithoutExtendedLengthField = 125; |
| 29 const uint64 kPayloadLengthWithTwoByteExtendedLengthField = 126; | 29 const uint64_t kPayloadLengthWithTwoByteExtendedLengthField = 126; |
| 30 const uint64 kPayloadLengthWithEightByteExtendedLengthField = 127; | 30 const uint64_t 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 websocket_error_(kWebSocketNormalClosure) { | 39 websocket_error_(kWebSocketNormalClosure) { |
| 40 std::fill(masking_key_.key, | 40 std::fill(masking_key_.key, |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 DCHECK(!current_frame_header_.get()); | 102 DCHECK(!current_frame_header_.get()); |
| 103 | 103 |
| 104 const char* start = &buffer_.front() + current_read_pos_; | 104 const char* start = &buffer_.front() + current_read_pos_; |
| 105 const char* current = start; | 105 const char* current = start; |
| 106 const char* end = &buffer_.front() + buffer_.size(); | 106 const char* end = &buffer_.front() + buffer_.size(); |
| 107 | 107 |
| 108 // Header needs 2 bytes at minimum. | 108 // Header needs 2 bytes at minimum. |
| 109 if (end - current < 2) | 109 if (end - current < 2) |
| 110 return; | 110 return; |
| 111 | 111 |
| 112 uint8 first_byte = *current++; | 112 uint8_t first_byte = *current++; |
| 113 uint8 second_byte = *current++; | 113 uint8_t second_byte = *current++; |
| 114 | 114 |
| 115 bool final = (first_byte & kFinalBit) != 0; | 115 bool final = (first_byte & kFinalBit) != 0; |
| 116 bool reserved1 = (first_byte & kReserved1Bit) != 0; | 116 bool reserved1 = (first_byte & kReserved1Bit) != 0; |
| 117 bool reserved2 = (first_byte & kReserved2Bit) != 0; | 117 bool reserved2 = (first_byte & kReserved2Bit) != 0; |
| 118 bool reserved3 = (first_byte & kReserved3Bit) != 0; | 118 bool reserved3 = (first_byte & kReserved3Bit) != 0; |
| 119 OpCode opcode = first_byte & kOpCodeMask; | 119 OpCode opcode = first_byte & kOpCodeMask; |
| 120 | 120 |
| 121 bool masked = (second_byte & kMaskBit) != 0; | 121 bool masked = (second_byte & kMaskBit) != 0; |
| 122 uint64 payload_length = second_byte & kPayloadLengthMask; | 122 uint64_t payload_length = second_byte & kPayloadLengthMask; |
| 123 if (payload_length == kPayloadLengthWithTwoByteExtendedLengthField) { | 123 if (payload_length == kPayloadLengthWithTwoByteExtendedLengthField) { |
| 124 if (end - current < 2) | 124 if (end - current < 2) |
| 125 return; | 125 return; |
| 126 uint16 payload_length_16; | 126 uint16_t payload_length_16; |
| 127 base::ReadBigEndian(current, &payload_length_16); | 127 base::ReadBigEndian(current, &payload_length_16); |
| 128 current += 2; | 128 current += 2; |
| 129 payload_length = payload_length_16; | 129 payload_length = payload_length_16; |
| 130 if (payload_length <= kMaxPayloadLengthWithoutExtendedLengthField) | 130 if (payload_length <= kMaxPayloadLengthWithoutExtendedLengthField) |
| 131 websocket_error_ = kWebSocketErrorProtocolError; | 131 websocket_error_ = kWebSocketErrorProtocolError; |
| 132 } else if (payload_length == kPayloadLengthWithEightByteExtendedLengthField) { | 132 } else if (payload_length == kPayloadLengthWithEightByteExtendedLengthField) { |
| 133 if (end - current < 8) | 133 if (end - current < 8) |
| 134 return; | 134 return; |
| 135 base::ReadBigEndian(current, &payload_length); | 135 base::ReadBigEndian(current, &payload_length); |
| 136 current += 8; | 136 current += 8; |
| 137 if (payload_length <= kuint16max || | 137 if (payload_length <= kuint16max || |
| 138 payload_length > static_cast<uint64>(kint64max)) { | 138 payload_length > static_cast<uint64_t>(kint64max)) { |
| 139 websocket_error_ = kWebSocketErrorProtocolError; | 139 websocket_error_ = kWebSocketErrorProtocolError; |
| 140 } else if (payload_length > static_cast<uint64>(kint32max)) { | 140 } else if (payload_length > static_cast<uint64_t>(kint32max)) { |
| 141 websocket_error_ = kWebSocketErrorMessageTooBig; | 141 websocket_error_ = kWebSocketErrorMessageTooBig; |
| 142 } | 142 } |
| 143 } | 143 } |
| 144 if (websocket_error_ != kWebSocketNormalClosure) { | 144 if (websocket_error_ != kWebSocketNormalClosure) { |
| 145 buffer_.clear(); | 145 buffer_.clear(); |
| 146 current_read_pos_ = 0; | 146 current_read_pos_ = 0; |
| 147 current_frame_header_.reset(); | 147 current_frame_header_.reset(); |
| 148 frame_offset_ = 0; | 148 frame_offset_ = 0; |
| 149 return; | 149 return; |
| 150 } | 150 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 166 current_frame_header_->masked = masked; | 166 current_frame_header_->masked = masked; |
| 167 current_frame_header_->payload_length = payload_length; | 167 current_frame_header_->payload_length = payload_length; |
| 168 current_read_pos_ += current - start; | 168 current_read_pos_ += current - start; |
| 169 DCHECK_EQ(0u, frame_offset_); | 169 DCHECK_EQ(0u, frame_offset_); |
| 170 } | 170 } |
| 171 | 171 |
| 172 scoped_ptr<WebSocketFrameChunk> WebSocketFrameParser::DecodeFramePayload( | 172 scoped_ptr<WebSocketFrameChunk> WebSocketFrameParser::DecodeFramePayload( |
| 173 bool first_chunk) { | 173 bool first_chunk) { |
| 174 // The cast here is safe because |payload_length| is already checked to be | 174 // The cast here is safe because |payload_length| is already checked to be |
| 175 // less than std::numeric_limits<int>::max() when the header is parsed. | 175 // less than std::numeric_limits<int>::max() when the header is parsed. |
| 176 int next_size = static_cast<int>(std::min( | 176 int next_size = static_cast<int>( |
| 177 static_cast<uint64>(buffer_.size() - current_read_pos_), | 177 std::min(static_cast<uint64_t>(buffer_.size() - current_read_pos_), |
| 178 current_frame_header_->payload_length - frame_offset_)); | 178 current_frame_header_->payload_length - frame_offset_)); |
| 179 | 179 |
| 180 scoped_ptr<WebSocketFrameChunk> frame_chunk(new WebSocketFrameChunk); | 180 scoped_ptr<WebSocketFrameChunk> frame_chunk(new WebSocketFrameChunk); |
| 181 if (first_chunk) { | 181 if (first_chunk) { |
| 182 frame_chunk->header = current_frame_header_->Clone(); | 182 frame_chunk->header = current_frame_header_->Clone(); |
| 183 } | 183 } |
| 184 frame_chunk->final_chunk = false; | 184 frame_chunk->final_chunk = false; |
| 185 if (next_size) { | 185 if (next_size) { |
| 186 frame_chunk->data = new IOBufferWithSize(static_cast<int>(next_size)); | 186 frame_chunk->data = new IOBufferWithSize(static_cast<int>(next_size)); |
| 187 char* io_data = frame_chunk->data->data(); | 187 char* io_data = frame_chunk->data->data(); |
| 188 memcpy(io_data, &buffer_.front() + current_read_pos_, next_size); | 188 memcpy(io_data, &buffer_.front() + current_read_pos_, next_size); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 201 if (frame_offset_ == current_frame_header_->payload_length) { | 201 if (frame_offset_ == current_frame_header_->payload_length) { |
| 202 frame_chunk->final_chunk = true; | 202 frame_chunk->final_chunk = true; |
| 203 current_frame_header_.reset(); | 203 current_frame_header_.reset(); |
| 204 frame_offset_ = 0; | 204 frame_offset_ = 0; |
| 205 } | 205 } |
| 206 | 206 |
| 207 return frame_chunk.Pass(); | 207 return frame_chunk.Pass(); |
| 208 } | 208 } |
| 209 | 209 |
| 210 } // namespace net | 210 } // namespace net |
| OLD | NEW |