OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/websockets/websocket_frame_parser.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/scoped_vector.h" |
| 14 #include "net/base/big_endian.h" |
| 15 #include "net/websockets/websocket_frame.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 const uint8 kFinalBit = 0x80; |
| 20 const uint8 kReserved1Bit = 0x40; |
| 21 const uint8 kReserved2Bit = 0x20; |
| 22 const uint8 kReserved3Bit = 0x10; |
| 23 const uint8 kOpCodeMask = 0xF; |
| 24 const uint8 kMaskBit = 0x80; |
| 25 const uint8 kPayloadLengthMask = 0x7F; |
| 26 const uint64 kMaxPayloadLengthWithoutExtendedLengthField = 125; |
| 27 const uint64 kPayloadLengthWithTwoByteExtendedLengthField = 126; |
| 28 const uint64 kPayloadLengthWithEightByteExtendedLengthField = 127; |
| 29 |
| 30 } // Unnamed namespace. |
| 31 |
| 32 namespace net { |
| 33 |
| 34 WebSocketFrameParser::WebSocketFrameParser() |
| 35 : current_read_pos_(0), |
| 36 frame_offset_(0), |
| 37 failed_(false) { |
| 38 std::fill(masking_key_, |
| 39 masking_key_ + WebSocketFrameHeader::kMaskingKeyLength, |
| 40 '\0'); |
| 41 } |
| 42 |
| 43 WebSocketFrameParser::~WebSocketFrameParser() { |
| 44 } |
| 45 |
| 46 bool WebSocketFrameParser::Decode( |
| 47 const char* data, |
| 48 size_t length, |
| 49 ScopedVector<WebSocketFrameChunk>* frame_chunks) { |
| 50 if (failed_) |
| 51 return false; |
| 52 if (!length) |
| 53 return true; |
| 54 |
| 55 // TODO(yutak): Remove copy. |
| 56 buffer_.insert(buffer_.end(), data, data + length); |
| 57 |
| 58 while (current_read_pos_ < buffer_.size()) { |
| 59 bool first_chunk = false; |
| 60 if (!current_frame_header_.get()) { |
| 61 DecodeFrameHeader(); |
| 62 if (failed_) |
| 63 return false; |
| 64 // If frame header is incomplete, then carry over the remaining |
| 65 // data to the next round of Decode(). |
| 66 if (!current_frame_header_.get()) |
| 67 break; |
| 68 first_chunk = true; |
| 69 } |
| 70 |
| 71 scoped_ptr<WebSocketFrameChunk> frame_chunk = |
| 72 DecodeFramePayload(first_chunk); |
| 73 DCHECK(frame_chunk.get()); |
| 74 frame_chunks->push_back(frame_chunk.release()); |
| 75 |
| 76 if (current_frame_header_.get()) { |
| 77 DCHECK(current_read_pos_ == buffer_.size()); |
| 78 break; |
| 79 } |
| 80 } |
| 81 |
| 82 // Drain unnecessary data. TODO(yutak): Remove copy. (but how?) |
| 83 buffer_.erase(buffer_.begin(), buffer_.begin() + current_read_pos_); |
| 84 current_read_pos_ = 0; |
| 85 |
| 86 // Sanity check: the size of carried-over data should not exceed |
| 87 // the maximum possible length of a frame header. |
| 88 static const size_t kMaximumFrameHeaderSize = |
| 89 WebSocketFrameHeader::kBaseHeaderSize + |
| 90 WebSocketFrameHeader::kMaximumExtendedLengthSize + |
| 91 WebSocketFrameHeader::kMaskingKeyLength; |
| 92 DCHECK_LT(buffer_.size(), kMaximumFrameHeaderSize); |
| 93 |
| 94 return true; |
| 95 } |
| 96 |
| 97 void WebSocketFrameParser::DecodeFrameHeader() { |
| 98 typedef WebSocketFrameHeader::OpCode OpCode; |
| 99 static const int kMaskingKeyLength = WebSocketFrameHeader::kMaskingKeyLength; |
| 100 |
| 101 DCHECK(!current_frame_header_.get()); |
| 102 |
| 103 const char* start = &buffer_.front() + current_read_pos_; |
| 104 const char* current = start; |
| 105 const char* end = &buffer_.front() + buffer_.size(); |
| 106 |
| 107 // Header needs 2 bytes at minimum. |
| 108 if (end - current < 2) |
| 109 return; |
| 110 |
| 111 uint8 first_byte = *current++; |
| 112 uint8 second_byte = *current++; |
| 113 |
| 114 bool final = (first_byte & kFinalBit) != 0; |
| 115 bool reserved1 = (first_byte & kReserved1Bit) != 0; |
| 116 bool reserved2 = (first_byte & kReserved2Bit) != 0; |
| 117 bool reserved3 = (first_byte & kReserved3Bit) != 0; |
| 118 OpCode opcode = first_byte & kOpCodeMask; |
| 119 |
| 120 bool masked = (second_byte & kMaskBit) != 0; |
| 121 uint64 payload_length = second_byte & kPayloadLengthMask; |
| 122 bool valid_length_format = true; |
| 123 if (payload_length == kPayloadLengthWithTwoByteExtendedLengthField) { |
| 124 if (end - current < 2) |
| 125 return; |
| 126 uint16 payload_length_16; |
| 127 ReadBigEndian(current, &payload_length_16); |
| 128 current += 2; |
| 129 payload_length = payload_length_16; |
| 130 if (payload_length <= kMaxPayloadLengthWithoutExtendedLengthField) |
| 131 valid_length_format = false; |
| 132 } else if (payload_length == kPayloadLengthWithEightByteExtendedLengthField) { |
| 133 if (end - current < 8) |
| 134 return; |
| 135 ReadBigEndian(current, &payload_length); |
| 136 current += 8; |
| 137 if (payload_length <= kuint16max || |
| 138 payload_length > static_cast<uint64>(kint64max)) { |
| 139 valid_length_format = false; |
| 140 } |
| 141 } |
| 142 if (!valid_length_format) { |
| 143 failed_ = true; |
| 144 buffer_.clear(); |
| 145 current_read_pos_ = 0; |
| 146 current_frame_header_.reset(); |
| 147 frame_offset_ = 0; |
| 148 return; |
| 149 } |
| 150 |
| 151 if (masked) { |
| 152 if (end - current < kMaskingKeyLength) |
| 153 return; |
| 154 std::copy(current, current + kMaskingKeyLength, masking_key_); |
| 155 current += kMaskingKeyLength; |
| 156 } else { |
| 157 std::fill(masking_key_, masking_key_ + kMaskingKeyLength, '\0'); |
| 158 } |
| 159 |
| 160 current_frame_header_.reset(new WebSocketFrameHeader); |
| 161 current_frame_header_->final = final; |
| 162 current_frame_header_->reserved1 = reserved1; |
| 163 current_frame_header_->reserved2 = reserved2; |
| 164 current_frame_header_->reserved3 = reserved3; |
| 165 current_frame_header_->opcode = opcode; |
| 166 current_frame_header_->masked = masked; |
| 167 current_frame_header_->payload_length = payload_length; |
| 168 current_read_pos_ += current - start; |
| 169 DCHECK_EQ(0u, frame_offset_); |
| 170 } |
| 171 |
| 172 scoped_ptr<WebSocketFrameChunk> WebSocketFrameParser::DecodeFramePayload( |
| 173 bool first_chunk) { |
| 174 static const int kMaskingKeyLength = WebSocketFrameHeader::kMaskingKeyLength; |
| 175 |
| 176 const char* current = &buffer_.front() + current_read_pos_; |
| 177 const char* end = &buffer_.front() + buffer_.size(); |
| 178 uint64 next_size = std::min<uint64>( |
| 179 end - current, |
| 180 current_frame_header_->payload_length - frame_offset_); |
| 181 |
| 182 scoped_ptr<WebSocketFrameChunk> frame_chunk(new WebSocketFrameChunk); |
| 183 if (first_chunk) { |
| 184 frame_chunk->header.reset(new WebSocketFrameHeader(*current_frame_header_)); |
| 185 } |
| 186 frame_chunk->final_chunk = false; |
| 187 frame_chunk->data.assign(current, current + next_size); |
| 188 if (current_frame_header_->masked) { |
| 189 // Unmask the payload. |
| 190 // TODO(yutak): This could be faster by doing unmasking for each |
| 191 // machine word (instead of each byte). |
| 192 size_t key_offset = frame_offset_ % kMaskingKeyLength; |
| 193 for (uint64 i = 0; i < next_size; ++i) { |
| 194 frame_chunk->data[i] ^= masking_key_[key_offset]; |
| 195 key_offset = (key_offset + 1) % kMaskingKeyLength; |
| 196 } |
| 197 } |
| 198 |
| 199 current_read_pos_ += next_size; |
| 200 frame_offset_ += next_size; |
| 201 |
| 202 DCHECK_LE(frame_offset_, current_frame_header_->payload_length); |
| 203 if (frame_offset_ == current_frame_header_->payload_length) { |
| 204 frame_chunk->final_chunk = true; |
| 205 current_frame_header_.reset(); |
| 206 frame_offset_ = 0; |
| 207 } |
| 208 |
| 209 return frame_chunk.Pass(); |
| 210 } |
| 211 |
| 212 } // namespace net |
OLD | NEW |