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