Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(756)

Unified Diff: net/websockets/websocket_frame_parser.cc

Issue 9956013: Add WebSocketFrameParser. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/websockets/websocket_frame_parser.cc
diff --git a/net/websockets/websocket_frame_parser.cc b/net/websockets/websocket_frame_parser.cc
new file mode 100644
index 0000000000000000000000000000000000000000..72e1f3a82cb340cf4c02b4b66b695777ee865ab2
--- /dev/null
+++ b/net/websockets/websocket_frame_parser.cc
@@ -0,0 +1,182 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/websockets/websocket_frame_parser.h"
+
+#include <algorithm>
+#include "base/basictypes.h"
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/memory/scoped_vector.h"
+#include "base/memory/ref_counted.h"
+#include "net/base/big_endian.h"
+#include "net/websockets/websocket_frame.h"
+
+const uint8 kFinalBit = 0x80;
+const uint8 kReserved1Bit = 0x40;
+const uint8 kReserved2Bit = 0x20;
+const uint8 kReserved3Bit = 0x10;
+const uint8 kOpCodeMask = 0xF;
+const uint8 kMaskBit = 0x80;
+const uint8 kPayloadLengthMask = 0x7F;
+const uint64 kMaxPayloadLengthWithoutExtendedLengthField = 125;
+const uint64 kPayloadLengthWithTwoByteExtendedLengthField = 126;
+const uint64 kPayloadLengthWithEightByteExtendedLengthField = 127;
+
+namespace net {
+
+WebSocketFrameParser::WebSocketFrameParser()
+ : buffer_pos_(0),
+ frame_offset_(0),
+ failed_(false) {
+ std::fill(masking_key_,
+ masking_key_ + WebSocketFrameHeader::kMaskingKeyLength,
+ '\0');
+}
+
+WebSocketFrameParser::~WebSocketFrameParser() {
+}
+
+bool WebSocketFrameParser::Decode(
+ const char* data,
+ size_t length,
+ ScopedVector<WebSocketPartialFrame>* frames) {
+ static const int kMaskingKeyLength = WebSocketFrameHeader::kMaskingKeyLength;
+
+ if (failed_)
+ return false;
+ if (!length)
+ return true;
+
+ // TODO(yutak): Remove copy.
+ buffer_.insert(buffer_.end(), data, data + length);
+
+ while (buffer_pos_ < buffer_.size()) {
+ if (!current_frame_header_.get()) {
+ DecodeFrameHeader();
+ if (failed_)
+ return false;
+ if (!current_frame_header_.get())
+ break;
+ }
+
+ // Reads frame payload. Unmasks it if necessary.
bashi 2012/04/03 09:35:50 How about extracting the following payload-parsing
Yuta Kitamura 2012/04/09 08:17:57 Sounds like a good idea. Will do.
+ const char* current = &buffer_.front() + buffer_pos_;
+ const char* end = &buffer_.front() + buffer_.size();
+ uint64 next_size = std::min<uint64>(
+ end - current,
+ current_frame_header_->payload_length - frame_offset_);
+
+ scoped_ptr<WebSocketPartialFrame> frame(new WebSocketPartialFrame);
+ frame->header = current_frame_header_;
+ frame->final_part = false;
+ frame->data.assign(current, current + next_size);
+ if (current_frame_header_->masked) {
+ size_t key_offset = frame_offset_ % kMaskingKeyLength;
+ for (uint64 i = 0; i < next_size; ++i) {
+ frame->data[i] ^= masking_key_[key_offset];
+ key_offset = (key_offset + 1) % kMaskingKeyLength;
+ }
+ }
+
+ buffer_pos_ += next_size;
+ frame_offset_ += next_size;
+
+ DCHECK_LE(frame_offset_, current_frame_header_->payload_length);
+ if (frame_offset_ == current_frame_header_->payload_length) {
+ frame->final_part = true;
+ current_frame_header_ = NULL;
+ frame_offset_ = 0;
+ std::fill(masking_key_, masking_key_ + kMaskingKeyLength, '\0');
+ }
+
+ frames->push_back(frame.release());
+
+ if (current_frame_header_.get())
+ break;
+ }
+
+ // Drain unnecessary data. TODO(yutak): Remove copy. (but how?)
+ buffer_.erase(buffer_.begin(), buffer_.begin() + buffer_pos_);
+ buffer_pos_ = 0;
+
+ return true;
+}
+
+bool WebSocketFrameParser::failed() const {
+ return failed_;
bashi 2012/04/03 09:35:50 WebKit's implementation tells the reason of the fa
Yuta Kitamura 2012/04/09 08:17:57 I'll leave this as a TODO. It might be better for
+}
+
+void WebSocketFrameParser::DecodeFrameHeader() {
bashi 2012/04/03 09:35:50 Adding a comment that describes |buffer_pos_| only
Takashi Toyoshima 2012/04/04 05:46:21 Maybe this variable name is a little vague. |buffe
Yuta Kitamura 2012/04/09 08:17:57 Maybe |current_read_pos_|? Anyway I'm going to add
Takashi Toyoshima 2012/04/09 23:12:16 sounds good.
+ typedef WebSocketFrameHeader::OpCode OpCode;
+ static const int kMaskingKeyLength = WebSocketFrameHeader::kMaskingKeyLength;
+
+ const char* start = &buffer_.front() + buffer_pos_;
+ const char* current = start;
+ const char* end = &buffer_.front() + buffer_.size();
Takashi Toyoshima 2012/04/04 05:46:21 I guess operator[] or iterator are enough to meet
Yuta Kitamura 2012/04/09 08:17:57 Using operator[] employs index arithmetic, and the
Takashi Toyoshima 2012/04/09 23:12:16 As a first impression, I prefer readability than o
Yuta Kitamura 2012/04/10 05:34:00 A big question: "Does that matter?" All your disc
Takashi Toyoshima 2012/04/11 05:37:54 As I said, I prefer readability. Please remove unr
Yuta Kitamura 2012/04/11 08:48:53 I didn't intend to do any optimization here. I don
+
+ // Header needs 2 bytes at minimum.
+ if (end - current < 2)
+ return;
Takashi Toyoshima 2012/04/04 05:46:21 This check must be done firstly as 'buffer_.size()
Yuta Kitamura 2012/04/09 08:17:57 |end - current| is used idiomatically to get the s
Takashi Toyoshima 2012/04/09 23:12:16 I think it's true that "&buffer_.front() == &buffe
Yuta Kitamura 2012/04/10 05:34:00 I don't get your point, but actually "&buffer_.fro
Takashi Toyoshima 2012/04/12 06:08:50 Sorry, I misunderstand this point. My expectation
+
+ uint8 first_byte = *current++;
+ uint8 second_byte = *current++;
+
+ bool final = (first_byte & kFinalBit) != 0;
+ bool reserved1 = (first_byte & kReserved1Bit) != 0;
+ bool reserved2 = (first_byte & kReserved2Bit) != 0;
+ bool reserved3 = (first_byte & kReserved3Bit) != 0;
+ OpCode opcode = first_byte & kOpCodeMask;
+
+ bool masked = (second_byte & kMaskBit) != 0;
+ uint64 payload_length = second_byte & kPayloadLengthMask;
+ bool valid_length_format = true;
+ if (payload_length == kPayloadLengthWithTwoByteExtendedLengthField) {
+ if (end - current < 2)
+ return;
+ uint16 payload_length_16;
+ ReadBigEndian(current, &payload_length_16);
+ current += 2;
+ payload_length = payload_length_16;
+ if (payload_length <= kMaxPayloadLengthWithoutExtendedLengthField)
+ valid_length_format = false;
+ } else if (payload_length == kPayloadLengthWithEightByteExtendedLengthField) {
+ if (end - current < 8)
+ return;
+ ReadBigEndian(current, &payload_length);
+ current += 8;
+ if (payload_length <= kuint16max ||
+ payload_length > static_cast<uint64>(kint64max)) {
+ valid_length_format = false;
+ }
+ }
+ if (!valid_length_format) {
+ failed_ = true;
+ buffer_.clear();
+ buffer_pos_ = 0;
+ current_frame_header_ = NULL;
+ frame_offset_ = 0;
+ return;
+ }
+
+ if (masked) {
+ if (end - current < kMaskingKeyLength)
+ return;
+ std::copy(current, current + kMaskingKeyLength, masking_key_);
+ current += kMaskingKeyLength;
+ }
+
Takashi Toyoshima 2012/04/04 05:46:21 We should implement continuation opcode consistenc
Yuta Kitamura 2012/04/09 08:17:57 I'm ambivarent about doing a check here. My origi
Takashi Toyoshima 2012/04/09 23:12:16 Sharing semantic checker between parser and builde
+ current_frame_header_ = new WebSocketFrameHeader;
+ current_frame_header_->final = final;
+ current_frame_header_->reserved1 = reserved1;
+ current_frame_header_->reserved2 = reserved2;
+ current_frame_header_->reserved3 = reserved3;
+ current_frame_header_->opcode = opcode;
+ current_frame_header_->masked = masked;
+ current_frame_header_->payload_length = payload_length;
+ buffer_pos_ += current - start;
+ DCHECK_EQ(0u, frame_offset_);
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698