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

Side by Side 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, 8 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 #include "base/basictypes.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/memory/ref_counted.h"
13 #include "net/base/big_endian.h"
14 #include "net/websockets/websocket_frame.h"
15
16 const uint8 kFinalBit = 0x80;
17 const uint8 kReserved1Bit = 0x40;
18 const uint8 kReserved2Bit = 0x20;
19 const uint8 kReserved3Bit = 0x10;
20 const uint8 kOpCodeMask = 0xF;
21 const uint8 kMaskBit = 0x80;
22 const uint8 kPayloadLengthMask = 0x7F;
23 const uint64 kMaxPayloadLengthWithoutExtendedLengthField = 125;
24 const uint64 kPayloadLengthWithTwoByteExtendedLengthField = 126;
25 const uint64 kPayloadLengthWithEightByteExtendedLengthField = 127;
26
27 namespace net {
28
29 WebSocketFrameParser::WebSocketFrameParser()
30 : buffer_pos_(0),
31 frame_offset_(0),
32 failed_(false) {
33 std::fill(masking_key_,
34 masking_key_ + WebSocketFrameHeader::kMaskingKeyLength,
35 '\0');
36 }
37
38 WebSocketFrameParser::~WebSocketFrameParser() {
39 }
40
41 bool WebSocketFrameParser::Decode(
42 const char* data,
43 size_t length,
44 ScopedVector<WebSocketPartialFrame>* frames) {
45 static const int kMaskingKeyLength = WebSocketFrameHeader::kMaskingKeyLength;
46
47 if (failed_)
48 return false;
49 if (!length)
50 return true;
51
52 // TODO(yutak): Remove copy.
53 buffer_.insert(buffer_.end(), data, data + length);
54
55 while (buffer_pos_ < buffer_.size()) {
56 if (!current_frame_header_.get()) {
57 DecodeFrameHeader();
58 if (failed_)
59 return false;
60 if (!current_frame_header_.get())
61 break;
62 }
63
64 // 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.
65 const char* current = &buffer_.front() + buffer_pos_;
66 const char* end = &buffer_.front() + buffer_.size();
67 uint64 next_size = std::min<uint64>(
68 end - current,
69 current_frame_header_->payload_length - frame_offset_);
70
71 scoped_ptr<WebSocketPartialFrame> frame(new WebSocketPartialFrame);
72 frame->header = current_frame_header_;
73 frame->final_part = false;
74 frame->data.assign(current, current + next_size);
75 if (current_frame_header_->masked) {
76 size_t key_offset = frame_offset_ % kMaskingKeyLength;
77 for (uint64 i = 0; i < next_size; ++i) {
78 frame->data[i] ^= masking_key_[key_offset];
79 key_offset = (key_offset + 1) % kMaskingKeyLength;
80 }
81 }
82
83 buffer_pos_ += next_size;
84 frame_offset_ += next_size;
85
86 DCHECK_LE(frame_offset_, current_frame_header_->payload_length);
87 if (frame_offset_ == current_frame_header_->payload_length) {
88 frame->final_part = true;
89 current_frame_header_ = NULL;
90 frame_offset_ = 0;
91 std::fill(masking_key_, masking_key_ + kMaskingKeyLength, '\0');
92 }
93
94 frames->push_back(frame.release());
95
96 if (current_frame_header_.get())
97 break;
98 }
99
100 // Drain unnecessary data. TODO(yutak): Remove copy. (but how?)
101 buffer_.erase(buffer_.begin(), buffer_.begin() + buffer_pos_);
102 buffer_pos_ = 0;
103
104 return true;
105 }
106
107 bool WebSocketFrameParser::failed() const {
108 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
109 }
110
111 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.
112 typedef WebSocketFrameHeader::OpCode OpCode;
113 static const int kMaskingKeyLength = WebSocketFrameHeader::kMaskingKeyLength;
114
115 const char* start = &buffer_.front() + buffer_pos_;
116 const char* current = start;
117 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
118
119 // Header needs 2 bytes at minimum.
120 if (end - current < 2)
121 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
122
123 uint8 first_byte = *current++;
124 uint8 second_byte = *current++;
125
126 bool final = (first_byte & kFinalBit) != 0;
127 bool reserved1 = (first_byte & kReserved1Bit) != 0;
128 bool reserved2 = (first_byte & kReserved2Bit) != 0;
129 bool reserved3 = (first_byte & kReserved3Bit) != 0;
130 OpCode opcode = first_byte & kOpCodeMask;
131
132 bool masked = (second_byte & kMaskBit) != 0;
133 uint64 payload_length = second_byte & kPayloadLengthMask;
134 bool valid_length_format = true;
135 if (payload_length == kPayloadLengthWithTwoByteExtendedLengthField) {
136 if (end - current < 2)
137 return;
138 uint16 payload_length_16;
139 ReadBigEndian(current, &payload_length_16);
140 current += 2;
141 payload_length = payload_length_16;
142 if (payload_length <= kMaxPayloadLengthWithoutExtendedLengthField)
143 valid_length_format = false;
144 } else if (payload_length == kPayloadLengthWithEightByteExtendedLengthField) {
145 if (end - current < 8)
146 return;
147 ReadBigEndian(current, &payload_length);
148 current += 8;
149 if (payload_length <= kuint16max ||
150 payload_length > static_cast<uint64>(kint64max)) {
151 valid_length_format = false;
152 }
153 }
154 if (!valid_length_format) {
155 failed_ = true;
156 buffer_.clear();
157 buffer_pos_ = 0;
158 current_frame_header_ = NULL;
159 frame_offset_ = 0;
160 return;
161 }
162
163 if (masked) {
164 if (end - current < kMaskingKeyLength)
165 return;
166 std::copy(current, current + kMaskingKeyLength, masking_key_);
167 current += kMaskingKeyLength;
168 }
169
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
170 current_frame_header_ = new WebSocketFrameHeader;
171 current_frame_header_->final = final;
172 current_frame_header_->reserved1 = reserved1;
173 current_frame_header_->reserved2 = reserved2;
174 current_frame_header_->reserved3 = reserved3;
175 current_frame_header_->opcode = opcode;
176 current_frame_header_->masked = masked;
177 current_frame_header_->payload_length = payload_length;
178 buffer_pos_ += current - start;
179 DCHECK_EQ(0u, frame_offset_);
180 }
181
182 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698