blob: d7bccc125e2a1abbc45189d69e4f2ba5819372b2 [file] [log] [blame]
[email protected]adb225d2013-08-30 13:14:431// Copyright 2013 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_basic_stream.h"
6
tfarinaea94afc232015-10-20 04:23:367#include <stddef.h>
tfarina8a2c66c22015-10-13 19:14:498#include <stdint.h>
[email protected]adb225d2013-08-30 13:14:439#include <algorithm>
10#include <limits>
11#include <string>
yhirano592ff7f2015-12-07 08:45:1912#include <utility>
[email protected]adb225d2013-08-30 13:14:4313#include <vector>
14
[email protected]adb225d2013-08-30 13:14:4315#include "base/bind.h"
16#include "base/logging.h"
rajendrant75fe34f2017-03-28 05:53:0017#include "base/metrics/histogram_macros.h"
[email protected]cb154062014-01-17 03:32:4018#include "base/numerics/safe_conversions.h"
[email protected]adb225d2013-08-30 13:14:4319#include "net/base/io_buffer.h"
20#include "net/base/net_errors.h"
21#include "net/socket/client_socket_handle.h"
Ramin Halavaticdb199a62018-01-25 12:38:1922#include "net/traffic_annotation/network_traffic_annotation.h"
[email protected]adb225d2013-08-30 13:14:4323#include "net/websockets/websocket_errors.h"
24#include "net/websockets/websocket_frame.h"
25#include "net/websockets/websocket_frame_parser.h"
26
27namespace net {
28
29namespace {
30
Ramin Halavaticdb199a62018-01-25 12:38:1931// Please refer to the comment in class header if the usage changes.
32constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation =
33 net::DefineNetworkTrafficAnnotation("websocket_basic_stream", R"(
34 semantics {
35 sender: "WebSocket Basic Stream"
36 description:
37 "Implementation of WebSocket API from web content (a page the user "
38 "visits)."
39 trigger: "Website calls the WebSocket API."
40 data:
41 "Any data provided by web content, masked and framed in accordance "
42 "with RFC6455."
43 destination: OTHER
44 destination_other:
45 "The address that the website has chosen to communicate to."
46 }
47 policy {
48 cookies_allowed: YES
49 cookies_store: "user"
50 setting: "These requests cannot be disabled."
51 policy_exception_justification:
52 "Not implemented. WebSocket is a core web platform API."
53 }
54 comments:
55 "The browser will never add cookies to a WebSocket message. But the "
56 "handshake that was performed when the WebSocket connection was "
57 "established may have contained cookies."
58 )");
59
tfarina8a2c66c22015-10-13 19:14:4960// This uses type uint64_t to match the definition of
[email protected]2f5d9f62013-09-26 12:14:2861// WebSocketFrameHeader::payload_length in websocket_frame.h.
tfarina8a2c66c22015-10-13 19:14:4962const uint64_t kMaxControlFramePayload = 125;
[email protected]2f5d9f62013-09-26 12:14:2863
[email protected]adb225d2013-08-30 13:14:4364// The number of bytes to attempt to read at a time.
65// TODO(ricea): See if there is a better number or algorithm to fulfill our
66// requirements:
67// 1. We would like to use minimal memory on low-bandwidth or idle connections
68// 2. We would like to read as close to line speed as possible on
69// high-bandwidth connections
70// 3. We can't afford to cause jank on the IO thread by copying large buffers
71// around
72// 4. We would like to hit any sweet-spots that might exist in terms of network
73// packet sizes / encryption block sizes / IPC alignment issues, etc.
74const int kReadBufferSize = 32 * 1024;
75
[email protected]9576544a2013-10-11 08:36:3376// Returns the total serialized size of |frames|. This function assumes that
77// |frames| will be serialized with mask field. This function forces the
78// masked bit of the frames on.
79int CalculateSerializedSizeAndTurnOnMaskBit(
danakj9c5cab52016-04-16 00:54:3380 std::vector<std::unique_ptr<WebSocketFrame>>* frames) {
tfarina8a2c66c22015-10-13 19:14:4981 const uint64_t kMaximumTotalSize = std::numeric_limits<int>::max();
[email protected]9576544a2013-10-11 08:36:3382
tfarina8a2c66c22015-10-13 19:14:4983 uint64_t total_size = 0;
yhirano592ff7f2015-12-07 08:45:1984 for (const auto& frame : *frames) {
[email protected]9576544a2013-10-11 08:36:3385 // Force the masked bit on.
86 frame->header.masked = true;
87 // We enforce flow control so the renderer should never be able to force us
88 // to cache anywhere near 2GB of frames.
tfarina8a2c66c22015-10-13 19:14:4989 uint64_t frame_size = frame->header.payload_length +
90 GetWebSocketFrameHeaderSize(frame->header);
pkasting4bff6be2014-10-15 17:54:3491 CHECK_LE(frame_size, kMaximumTotalSize - total_size)
[email protected]9576544a2013-10-11 08:36:3392 << "Aborting to prevent overflow";
93 total_size += frame_size;
94 }
pkasting4bff6be2014-10-15 17:54:3495 return static_cast<int>(total_size);
[email protected]9576544a2013-10-11 08:36:3396}
97
[email protected]adb225d2013-08-30 13:14:4398} // namespace
99
100WebSocketBasicStream::WebSocketBasicStream(
danakj9c5cab52016-04-16 00:54:33101 std::unique_ptr<ClientSocketHandle> connection,
[email protected]86602d3f2013-11-06 00:08:22102 const scoped_refptr<GrowableIOBuffer>& http_read_buffer,
103 const std::string& sub_protocol,
104 const std::string& extensions)
[email protected]adb225d2013-08-30 13:14:43105 : read_buffer_(new IOBufferWithSize(kReadBufferSize)),
dchengc7eeda422015-12-26 03:56:48106 connection_(std::move(connection)),
[email protected]86602d3f2013-11-06 00:08:22107 http_read_buffer_(http_read_buffer),
108 sub_protocol_(sub_protocol),
109 extensions_(extensions),
[email protected]adb225d2013-08-30 13:14:43110 generate_websocket_masking_key_(&GenerateWebSocketMaskingKey) {
[email protected]50133d7a2013-11-07 13:08:36111 // http_read_buffer_ should not be set if it contains no data.
dchengb206dc412014-08-26 19:46:23112 if (http_read_buffer_.get() && http_read_buffer_->offset() == 0)
[email protected]50133d7a2013-11-07 13:08:36113 http_read_buffer_ = NULL;
[email protected]adb225d2013-08-30 13:14:43114 DCHECK(connection_->is_initialized());
115}
116
117WebSocketBasicStream::~WebSocketBasicStream() { Close(); }
118
yhirano592ff7f2015-12-07 08:45:19119int WebSocketBasicStream::ReadFrames(
danakj9c5cab52016-04-16 00:54:33120 std::vector<std::unique_ptr<WebSocketFrame>>* frames,
yhirano592ff7f2015-12-07 08:45:19121 const CompletionCallback& callback) {
[email protected]2f5d9f62013-09-26 12:14:28122 DCHECK(frames->empty());
[email protected]adb225d2013-08-30 13:14:43123 // If there is data left over after parsing the HTTP headers, attempt to parse
124 // it as WebSocket frames.
dchengb206dc412014-08-26 19:46:23125 if (http_read_buffer_.get()) {
[email protected]adb225d2013-08-30 13:14:43126 DCHECK_GE(http_read_buffer_->offset(), 0);
127 // We cannot simply copy the data into read_buffer_, as it might be too
128 // large.
129 scoped_refptr<GrowableIOBuffer> buffered_data;
130 buffered_data.swap(http_read_buffer_);
melandory1346cde2016-06-11 00:42:12131 DCHECK(!http_read_buffer_);
danakj9c5cab52016-04-16 00:54:33132 std::vector<std::unique_ptr<WebSocketFrameChunk>> frame_chunks;
[email protected]adb225d2013-08-30 13:14:43133 if (!parser_.Decode(buffered_data->StartOfBuffer(),
134 buffered_data->offset(),
[email protected]2f5d9f62013-09-26 12:14:28135 &frame_chunks))
[email protected]adb225d2013-08-30 13:14:43136 return WebSocketErrorToNetError(parser_.websocket_error());
[email protected]2f5d9f62013-09-26 12:14:28137 if (!frame_chunks.empty()) {
138 int result = ConvertChunksToFrames(&frame_chunks, frames);
139 if (result != ERR_IO_PENDING)
140 return result;
141 }
[email protected]adb225d2013-08-30 13:14:43142 }
143
[email protected]2f5d9f62013-09-26 12:14:28144 // Run until socket stops giving us data or we get some frames.
[email protected]adb225d2013-08-30 13:14:43145 while (true) {
146 // base::Unretained(this) here is safe because net::Socket guarantees not to
147 // call any callbacks after Disconnect(), which we call from the
[email protected]2f5d9f62013-09-26 12:14:28148 // destructor. The caller of ReadFrames() is required to keep |frames|
[email protected]adb225d2013-08-30 13:14:43149 // valid.
[email protected]2f5d9f62013-09-26 12:14:28150 int result = connection_->socket()->Read(
151 read_buffer_.get(),
152 read_buffer_->size(),
153 base::Bind(&WebSocketBasicStream::OnReadComplete,
154 base::Unretained(this),
155 base::Unretained(frames),
156 callback));
[email protected]adb225d2013-08-30 13:14:43157 if (result == ERR_IO_PENDING)
158 return result;
[email protected]2f5d9f62013-09-26 12:14:28159 result = HandleReadResult(result, frames);
[email protected]adb225d2013-08-30 13:14:43160 if (result != ERR_IO_PENDING)
161 return result;
[email protected]2f5d9f62013-09-26 12:14:28162 DCHECK(frames->empty());
[email protected]adb225d2013-08-30 13:14:43163 }
164}
165
yhirano592ff7f2015-12-07 08:45:19166int WebSocketBasicStream::WriteFrames(
danakj9c5cab52016-04-16 00:54:33167 std::vector<std::unique_ptr<WebSocketFrame>>* frames,
yhirano592ff7f2015-12-07 08:45:19168 const CompletionCallback& callback) {
[email protected]adb225d2013-08-30 13:14:43169 // This function always concatenates all frames into a single buffer.
170 // TODO(ricea): Investigate whether it would be better in some cases to
171 // perform multiple writes with smaller buffers.
172 //
173 // First calculate the size of the buffer we need to allocate.
[email protected]9576544a2013-10-11 08:36:33174 int total_size = CalculateSerializedSizeAndTurnOnMaskBit(frames);
[email protected]adb225d2013-08-30 13:14:43175 scoped_refptr<IOBufferWithSize> combined_buffer(
176 new IOBufferWithSize(total_size));
[email protected]9576544a2013-10-11 08:36:33177
[email protected]adb225d2013-08-30 13:14:43178 char* dest = combined_buffer->data();
179 int remaining_size = total_size;
yhirano592ff7f2015-12-07 08:45:19180 for (const auto& frame : *frames) {
[email protected]adb225d2013-08-30 13:14:43181 WebSocketMaskingKey mask = generate_websocket_masking_key_();
[email protected]2f5d9f62013-09-26 12:14:28182 int result =
183 WriteWebSocketFrameHeader(frame->header, &mask, dest, remaining_size);
184 DCHECK_NE(ERR_INVALID_ARGUMENT, result)
[email protected]adb225d2013-08-30 13:14:43185 << "WriteWebSocketFrameHeader() says that " << remaining_size
186 << " is not enough to write the header in. This should not happen.";
187 CHECK_GE(result, 0) << "Potentially security-critical check failed";
188 dest += result;
189 remaining_size -= result;
190
tfarina8a2c66c22015-10-13 19:14:49191 CHECK_LE(frame->header.payload_length,
192 static_cast<uint64_t>(remaining_size));
pkasting4bff6be2014-10-15 17:54:34193 const int frame_size = static_cast<int>(frame->header.payload_length);
[email protected]403ee6e2014-01-27 10:10:44194 if (frame_size > 0) {
[email protected]403ee6e2014-01-27 10:10:44195 const char* const frame_data = frame->data->data();
196 std::copy(frame_data, frame_data + frame_size, dest);
197 MaskWebSocketFramePayload(mask, 0, dest, frame_size);
198 dest += frame_size;
199 remaining_size -= frame_size;
200 }
[email protected]adb225d2013-08-30 13:14:43201 }
202 DCHECK_EQ(0, remaining_size) << "Buffer size calculation was wrong; "
203 << remaining_size << " bytes left over.";
204 scoped_refptr<DrainableIOBuffer> drainable_buffer(
dchengb206dc412014-08-26 19:46:23205 new DrainableIOBuffer(combined_buffer.get(), total_size));
[email protected]adb225d2013-08-30 13:14:43206 return WriteEverything(drainable_buffer, callback);
207}
208
209void WebSocketBasicStream::Close() { connection_->socket()->Disconnect(); }
210
211std::string WebSocketBasicStream::GetSubProtocol() const {
212 return sub_protocol_;
213}
214
215std::string WebSocketBasicStream::GetExtensions() const { return extensions_; }
216
[email protected]adb225d2013-08-30 13:14:43217/*static*/
danakj9c5cab52016-04-16 00:54:33218std::unique_ptr<WebSocketBasicStream>
[email protected]adb225d2013-08-30 13:14:43219WebSocketBasicStream::CreateWebSocketBasicStreamForTesting(
danakj9c5cab52016-04-16 00:54:33220 std::unique_ptr<ClientSocketHandle> connection,
[email protected]adb225d2013-08-30 13:14:43221 const scoped_refptr<GrowableIOBuffer>& http_read_buffer,
222 const std::string& sub_protocol,
223 const std::string& extensions,
224 WebSocketMaskingKeyGeneratorFunction key_generator_function) {
danakj9c5cab52016-04-16 00:54:33225 std::unique_ptr<WebSocketBasicStream> stream(new WebSocketBasicStream(
dchengc7eeda422015-12-26 03:56:48226 std::move(connection), http_read_buffer, sub_protocol, extensions));
[email protected]adb225d2013-08-30 13:14:43227 stream->generate_websocket_masking_key_ = key_generator_function;
dchengc7eeda422015-12-26 03:56:48228 return stream;
[email protected]adb225d2013-08-30 13:14:43229}
230
231int WebSocketBasicStream::WriteEverything(
232 const scoped_refptr<DrainableIOBuffer>& buffer,
233 const CompletionCallback& callback) {
234 while (buffer->BytesRemaining() > 0) {
235 // The use of base::Unretained() here is safe because on destruction we
236 // disconnect the socket, preventing any further callbacks.
[email protected]2f5d9f62013-09-26 12:14:28237 int result = connection_->socket()->Write(
Ramin Halavaticdb199a62018-01-25 12:38:19238 buffer.get(), buffer->BytesRemaining(),
[email protected]2f5d9f62013-09-26 12:14:28239 base::Bind(&WebSocketBasicStream::OnWriteComplete,
Ramin Halavaticdb199a62018-01-25 12:38:19240 base::Unretained(this), buffer, callback),
241 kTrafficAnnotation);
[email protected]adb225d2013-08-30 13:14:43242 if (result > 0) {
rajendrant75fe34f2017-03-28 05:53:00243 UMA_HISTOGRAM_COUNTS_100000("Net.WebSocket.DataUse.Upstream", result);
[email protected]adb225d2013-08-30 13:14:43244 buffer->DidConsume(result);
245 } else {
246 return result;
247 }
248 }
249 return OK;
250}
251
252void WebSocketBasicStream::OnWriteComplete(
253 const scoped_refptr<DrainableIOBuffer>& buffer,
254 const CompletionCallback& callback,
255 int result) {
256 if (result < 0) {
[email protected]2f5d9f62013-09-26 12:14:28257 DCHECK_NE(ERR_IO_PENDING, result);
[email protected]adb225d2013-08-30 13:14:43258 callback.Run(result);
259 return;
260 }
261
[email protected]2f5d9f62013-09-26 12:14:28262 DCHECK_NE(0, result);
rajendrant75fe34f2017-03-28 05:53:00263 UMA_HISTOGRAM_COUNTS_100000("Net.WebSocket.DataUse.Upstream", result);
264
[email protected]adb225d2013-08-30 13:14:43265 buffer->DidConsume(result);
266 result = WriteEverything(buffer, callback);
267 if (result != ERR_IO_PENDING)
268 callback.Run(result);
269}
270
271int WebSocketBasicStream::HandleReadResult(
272 int result,
danakj9c5cab52016-04-16 00:54:33273 std::vector<std::unique_ptr<WebSocketFrame>>* frames) {
[email protected]adb225d2013-08-30 13:14:43274 DCHECK_NE(ERR_IO_PENDING, result);
[email protected]2f5d9f62013-09-26 12:14:28275 DCHECK(frames->empty());
[email protected]adb225d2013-08-30 13:14:43276 if (result < 0)
277 return result;
278 if (result == 0)
279 return ERR_CONNECTION_CLOSED;
rajendrant75fe34f2017-03-28 05:53:00280
281 UMA_HISTOGRAM_COUNTS_100000("Net.WebSocket.DataUse.Downstream", result);
282
danakj9c5cab52016-04-16 00:54:33283 std::vector<std::unique_ptr<WebSocketFrameChunk>> frame_chunks;
[email protected]2f5d9f62013-09-26 12:14:28284 if (!parser_.Decode(read_buffer_->data(), result, &frame_chunks))
[email protected]adb225d2013-08-30 13:14:43285 return WebSocketErrorToNetError(parser_.websocket_error());
[email protected]2f5d9f62013-09-26 12:14:28286 if (frame_chunks.empty())
287 return ERR_IO_PENDING;
288 return ConvertChunksToFrames(&frame_chunks, frames);
[email protected]adb225d2013-08-30 13:14:43289}
290
[email protected]2f5d9f62013-09-26 12:14:28291int WebSocketBasicStream::ConvertChunksToFrames(
danakj9c5cab52016-04-16 00:54:33292 std::vector<std::unique_ptr<WebSocketFrameChunk>>* frame_chunks,
293 std::vector<std::unique_ptr<WebSocketFrame>>* frames) {
[email protected]2f5d9f62013-09-26 12:14:28294 for (size_t i = 0; i < frame_chunks->size(); ++i) {
danakj9c5cab52016-04-16 00:54:33295 std::unique_ptr<WebSocketFrame> frame;
yhirano592ff7f2015-12-07 08:45:19296 int result = ConvertChunkToFrame(std::move((*frame_chunks)[i]), &frame);
[email protected]2f5d9f62013-09-26 12:14:28297 if (result != OK)
298 return result;
299 if (frame)
dchengc7eeda422015-12-26 03:56:48300 frames->push_back(std::move(frame));
[email protected]2f5d9f62013-09-26 12:14:28301 }
yhirano592ff7f2015-12-07 08:45:19302 frame_chunks->clear();
[email protected]2f5d9f62013-09-26 12:14:28303 if (frames->empty())
304 return ERR_IO_PENDING;
305 return OK;
306}
307
308int WebSocketBasicStream::ConvertChunkToFrame(
danakj9c5cab52016-04-16 00:54:33309 std::unique_ptr<WebSocketFrameChunk> chunk,
310 std::unique_ptr<WebSocketFrame>* frame) {
[email protected]2f5d9f62013-09-26 12:14:28311 DCHECK(frame->get() == NULL);
312 bool is_first_chunk = false;
313 if (chunk->header) {
314 DCHECK(current_frame_header_ == NULL)
315 << "Received the header for a new frame without notification that "
316 << "the previous frame was complete (bug in WebSocketFrameParser?)";
317 is_first_chunk = true;
318 current_frame_header_.swap(chunk->header);
319 }
dchengb206dc412014-08-26 19:46:23320 const int chunk_size = chunk->data.get() ? chunk->data->size() : 0;
[email protected]2f5d9f62013-09-26 12:14:28321 DCHECK(current_frame_header_) << "Unexpected header-less chunk received "
322 << "(final_chunk = " << chunk->final_chunk
323 << ", data size = " << chunk_size
324 << ") (bug in WebSocketFrameParser?)";
325 scoped_refptr<IOBufferWithSize> data_buffer;
326 data_buffer.swap(chunk->data);
327 const bool is_final_chunk = chunk->final_chunk;
328 const WebSocketFrameHeader::OpCode opcode = current_frame_header_->opcode;
329 if (WebSocketFrameHeader::IsKnownControlOpCode(opcode)) {
330 bool protocol_error = false;
331 if (!current_frame_header_->final) {
332 DVLOG(1) << "WebSocket protocol error. Control frame, opcode=" << opcode
333 << " received with FIN bit unset.";
334 protocol_error = true;
335 }
336 if (current_frame_header_->payload_length > kMaxControlFramePayload) {
337 DVLOG(1) << "WebSocket protocol error. Control frame, opcode=" << opcode
338 << ", payload_length=" << current_frame_header_->payload_length
339 << " exceeds maximum payload length for a control message.";
340 protocol_error = true;
341 }
342 if (protocol_error) {
343 current_frame_header_.reset();
344 return ERR_WS_PROTOCOL_ERROR;
345 }
346 if (!is_final_chunk) {
347 DVLOG(2) << "Encountered a split control frame, opcode " << opcode;
dchengb206dc412014-08-26 19:46:23348 if (incomplete_control_frame_body_.get()) {
[email protected]2f5d9f62013-09-26 12:14:28349 DVLOG(3) << "Appending to an existing split control frame.";
350 AddToIncompleteControlFrameBody(data_buffer);
351 } else {
352 DVLOG(3) << "Creating new storage for an incomplete control frame.";
353 incomplete_control_frame_body_ = new GrowableIOBuffer();
354 // This method checks for oversize control frames above, so as long as
355 // the frame parser is working correctly, this won't overflow. If a bug
356 // does cause it to overflow, it will CHECK() in
357 // AddToIncompleteControlFrameBody() without writing outside the buffer.
358 incomplete_control_frame_body_->SetCapacity(kMaxControlFramePayload);
359 AddToIncompleteControlFrameBody(data_buffer);
360 }
361 return OK;
362 }
dchengb206dc412014-08-26 19:46:23363 if (incomplete_control_frame_body_.get()) {
[email protected]2f5d9f62013-09-26 12:14:28364 DVLOG(2) << "Rejoining a split control frame, opcode " << opcode;
365 AddToIncompleteControlFrameBody(data_buffer);
366 const int body_size = incomplete_control_frame_body_->offset();
367 DCHECK_EQ(body_size,
368 static_cast<int>(current_frame_header_->payload_length));
369 scoped_refptr<IOBufferWithSize> body = new IOBufferWithSize(body_size);
370 memcpy(body->data(),
371 incomplete_control_frame_body_->StartOfBuffer(),
372 body_size);
373 incomplete_control_frame_body_ = NULL; // Frame now complete.
374 DCHECK(is_final_chunk);
375 *frame = CreateFrame(is_final_chunk, body);
376 return OK;
377 }
378 }
379
380 // Apply basic sanity checks to the |payload_length| field from the frame
381 // header. A check for exact equality can only be used when the whole frame
382 // arrives in one chunk.
383 DCHECK_GE(current_frame_header_->payload_length,
tfarina8a2c66c22015-10-13 19:14:49384 base::checked_cast<uint64_t>(chunk_size));
[email protected]2f5d9f62013-09-26 12:14:28385 DCHECK(!is_first_chunk || !is_final_chunk ||
386 current_frame_header_->payload_length ==
tfarina8a2c66c22015-10-13 19:14:49387 base::checked_cast<uint64_t>(chunk_size));
[email protected]2f5d9f62013-09-26 12:14:28388
389 // Convert the chunk to a complete frame.
390 *frame = CreateFrame(is_final_chunk, data_buffer);
391 return OK;
392}
393
danakj9c5cab52016-04-16 00:54:33394std::unique_ptr<WebSocketFrame> WebSocketBasicStream::CreateFrame(
[email protected]2f5d9f62013-09-26 12:14:28395 bool is_final_chunk,
396 const scoped_refptr<IOBufferWithSize>& data) {
danakj9c5cab52016-04-16 00:54:33397 std::unique_ptr<WebSocketFrame> result_frame;
[email protected]2f5d9f62013-09-26 12:14:28398 const bool is_final_chunk_in_message =
399 is_final_chunk && current_frame_header_->final;
dchengb206dc412014-08-26 19:46:23400 const int data_size = data.get() ? data->size() : 0;
[email protected]2f5d9f62013-09-26 12:14:28401 const WebSocketFrameHeader::OpCode opcode = current_frame_header_->opcode;
[email protected]e678d4fe2013-10-11 11:27:55402 // Empty frames convey no useful information unless they are the first frame
403 // (containing the type and flags) or have the "final" bit set.
404 if (is_final_chunk_in_message || data_size > 0 ||
405 current_frame_header_->opcode !=
406 WebSocketFrameHeader::kOpCodeContinuation) {
[email protected]2f5d9f62013-09-26 12:14:28407 result_frame.reset(new WebSocketFrame(opcode));
408 result_frame->header.CopyFrom(*current_frame_header_);
409 result_frame->header.final = is_final_chunk_in_message;
410 result_frame->header.payload_length = data_size;
411 result_frame->data = data;
412 // Ensure that opcodes Text and Binary are only used for the first frame in
[email protected]d52c0c42014-02-19 08:27:47413 // the message. Also clear the reserved bits.
414 // TODO(ricea): If a future extension requires the reserved bits to be
415 // retained on continuation frames, make this behaviour conditional on a
416 // flag set at construction time.
417 if (!is_final_chunk && WebSocketFrameHeader::IsKnownDataOpCode(opcode)) {
[email protected]2f5d9f62013-09-26 12:14:28418 current_frame_header_->opcode = WebSocketFrameHeader::kOpCodeContinuation;
[email protected]d52c0c42014-02-19 08:27:47419 current_frame_header_->reserved1 = false;
420 current_frame_header_->reserved2 = false;
421 current_frame_header_->reserved3 = false;
422 }
[email protected]2f5d9f62013-09-26 12:14:28423 }
424 // Make sure that a frame header is not applied to any chunks that do not
425 // belong to it.
426 if (is_final_chunk)
427 current_frame_header_.reset();
dchengc7eeda422015-12-26 03:56:48428 return result_frame;
[email protected]2f5d9f62013-09-26 12:14:28429}
430
431void WebSocketBasicStream::AddToIncompleteControlFrameBody(
432 const scoped_refptr<IOBufferWithSize>& data_buffer) {
dchengb206dc412014-08-26 19:46:23433 if (!data_buffer.get())
[email protected]2f5d9f62013-09-26 12:14:28434 return;
435 const int new_offset =
436 incomplete_control_frame_body_->offset() + data_buffer->size();
437 CHECK_GE(incomplete_control_frame_body_->capacity(), new_offset)
438 << "Control frame body larger than frame header indicates; frame parser "
439 "bug?";
440 memcpy(incomplete_control_frame_body_->data(),
441 data_buffer->data(),
442 data_buffer->size());
443 incomplete_control_frame_body_->set_offset(new_offset);
444}
445
yhirano592ff7f2015-12-07 08:45:19446void WebSocketBasicStream::OnReadComplete(
danakj9c5cab52016-04-16 00:54:33447 std::vector<std::unique_ptr<WebSocketFrame>>* frames,
yhirano592ff7f2015-12-07 08:45:19448 const CompletionCallback& callback,
449 int result) {
[email protected]2f5d9f62013-09-26 12:14:28450 result = HandleReadResult(result, frames);
[email protected]adb225d2013-08-30 13:14:43451 if (result == ERR_IO_PENDING)
[email protected]2f5d9f62013-09-26 12:14:28452 result = ReadFrames(frames, callback);
[email protected]adb225d2013-08-30 13:14:43453 if (result != ERR_IO_PENDING)
454 callback.Run(result);
455}
456
457} // namespace net