1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/websockets/websocket_basic_stream.h"
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <limits>
#include <ostream>
#include <utility>
#include "base/check.h"
#include "base/check_op.h"
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/log/net_log_event_type.h"
#include "net/socket/client_socket_handle.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/websockets/websocket_basic_stream_adapters.h"
#include "net/websockets/websocket_errors.h"
#include "net/websockets/websocket_frame.h"
namespace net {
namespace {
// Please refer to the comment in class header if the usage changes.
constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation =
net::DefineNetworkTrafficAnnotation("websocket_basic_stream", R"(
semantics {
sender: "WebSocket Basic Stream"
description:
"Implementation of WebSocket API from web content (a page the user "
"visits)."
trigger: "Website calls the WebSocket API."
data:
"Any data provided by web content, masked and framed in accordance "
"with RFC6455."
destination: OTHER
destination_other:
"The address that the website has chosen to communicate to."
}
policy {
cookies_allowed: YES
cookies_store: "user"
setting: "These requests cannot be disabled."
policy_exception_justification:
"Not implemented. WebSocket is a core web platform API."
}
comments:
"The browser will never add cookies to a WebSocket message. But the "
"handshake that was performed when the WebSocket connection was "
"established may have contained cookies."
)");
// The number of bytes to attempt to read at a time. It's used only for high
// throughput connections.
// TODO(ricea): See if there is a better number or algorithm to fulfill our
// requirements:
// 1. We would like to use minimal memory on low-bandwidth or idle connections
// 2. We would like to read as close to line speed as possible on
// high-bandwidth connections
// 3. We can't afford to cause jank on the IO thread by copying large buffers
// around
// 4. We would like to hit any sweet-spots that might exist in terms of network
// packet sizes / encryption block sizes / IPC alignment issues, etc.
#if BUILDFLAG(IS_ANDROID)
constexpr size_t kLargeReadBufferSize = 32 * 1024;
#else
// |2^n - delta| is better than 2^n on Linux. See crrev.com/c/1792208.
constexpr size_t kLargeReadBufferSize = 131000;
#endif
// The number of bytes to attempt to read at a time. It's set as an initial read
// buffer size and used for low throughput connections.
constexpr size_t kSmallReadBufferSize = 1000;
// The threshold to decide whether to switch the read buffer size.
constexpr double kThresholdInBytesPerSecond = 1200 * 1000;
// Returns the total serialized size of |frames|. This function assumes that
// |frames| will be serialized with mask field. This function forces the
// masked bit of the frames on.
int CalculateSerializedSizeAndTurnOnMaskBit(
std::vector<std::unique_ptr<WebSocketFrame>>* frames) {
constexpr uint64_t kMaximumTotalSize = std::numeric_limits<int>::max();
uint64_t total_size = 0;
for (const auto& frame : *frames) {
// Force the masked bit on.
frame->header.masked = true;
// We enforce flow control so the renderer should never be able to force us
// to cache anywhere near 2GB of frames.
uint64_t frame_size = frame->header.payload_length +
GetWebSocketFrameHeaderSize(frame->header);
CHECK_LE(frame_size, kMaximumTotalSize - total_size)
<< "Aborting to prevent overflow";
total_size += frame_size;
}
return static_cast<int>(total_size);
}
base::Value::Dict NetLogBufferSizeParam(int buffer_size) {
base::Value::Dict dict;
dict.Set("read_buffer_size_in_bytes", buffer_size);
return dict;
}
base::Value::Dict NetLogFrameHeaderParam(const WebSocketFrameHeader* header) {
base::Value::Dict dict;
dict.Set("final", header->final);
dict.Set("reserved1", header->reserved1);
dict.Set("reserved2", header->reserved2);
dict.Set("reserved3", header->reserved3);
dict.Set("opcode", header->opcode);
dict.Set("masked", header->masked);
dict.Set("payload_length", static_cast<double>(header->payload_length));
return dict;
}
} // namespace
WebSocketBasicStream::BufferSizeManager::BufferSizeManager() = default;
WebSocketBasicStream::BufferSizeManager::~BufferSizeManager() = default;
void WebSocketBasicStream::BufferSizeManager::OnRead(base::TimeTicks now) {
read_start_timestamps_.push(now);
}
void WebSocketBasicStream::BufferSizeManager::OnReadComplete(
base::TimeTicks now,
int size) {
DCHECK_GT(size, 0);
// This cannot overflow because the result is at most
// kLargeReadBufferSize*rolling_average_window_.
rolling_byte_total_ += size;
recent_read_sizes_.push(size);
DCHECK_LE(read_start_timestamps_.size(), rolling_average_window_);
if (read_start_timestamps_.size() == rolling_average_window_) {
DCHECK_EQ(read_start_timestamps_.size(), recent_read_sizes_.size());
base::TimeDelta duration = now - read_start_timestamps_.front();
base::TimeDelta threshold_duration =
base::Seconds(rolling_byte_total_ / kThresholdInBytesPerSecond);
read_start_timestamps_.pop();
rolling_byte_total_ -= recent_read_sizes_.front();
recent_read_sizes_.pop();
if (threshold_duration < duration) {
buffer_size_ = BufferSize::kSmall;
} else {
buffer_size_ = BufferSize::kLarge;
}
}
}
WebSocketBasicStream::WebSocketBasicStream(
std::unique_ptr<Adapter> connection,
const scoped_refptr<GrowableIOBuffer>& http_read_buffer,
const std::string& sub_protocol,
const std::string& extensions,
const NetLogWithSource& net_log)
: read_buffer_(
base::MakeRefCounted<IOBufferWithSize>(kSmallReadBufferSize)),
target_read_buffer_size_(read_buffer_->size()),
connection_(std::move(connection)),
http_read_buffer_(http_read_buffer),
sub_protocol_(sub_protocol),
extensions_(extensions),
net_log_(net_log),
generate_websocket_masking_key_(&GenerateWebSocketMaskingKey) {
// http_read_buffer_ should not be set if it contains no data.
if (http_read_buffer_.get() && http_read_buffer_->offset() == 0)
http_read_buffer_ = nullptr;
DCHECK(connection_->is_initialized());
}
WebSocketBasicStream::~WebSocketBasicStream() { Close(); }
int WebSocketBasicStream::ReadFrames(
std::vector<std::unique_ptr<WebSocketFrame>>* frames,
CompletionOnceCallback callback) {
read_callback_ = std::move(callback);
control_frame_payloads_.clear();
if (http_read_buffer_ && is_http_read_buffer_decoded_) {
http_read_buffer_.reset();
}
return ReadEverything(frames);
}
int WebSocketBasicStream::WriteFrames(
std::vector<std::unique_ptr<WebSocketFrame>>* frames,
CompletionOnceCallback callback) {
// This function always concatenates all frames into a single buffer.
// TODO(ricea): Investigate whether it would be better in some cases to
// perform multiple writes with smaller buffers.
write_callback_ = std::move(callback);
// First calculate the size of the buffer we need to allocate.
int total_size = CalculateSerializedSizeAndTurnOnMaskBit(frames);
auto combined_buffer = base::MakeRefCounted<IOBufferWithSize>(total_size);
base::span<uint8_t> dest = combined_buffer->span();
for (const auto& frame : *frames) {
net_log_.AddEvent(net::NetLogEventType::WEBSOCKET_SENT_FRAME_HEADER,
[&] { return NetLogFrameHeaderParam(&frame->header); });
WebSocketMaskingKey mask = generate_websocket_masking_key_();
int result = WriteWebSocketFrameHeader(frame->header, &mask, dest);
DCHECK_NE(ERR_INVALID_ARGUMENT, result)
<< "WriteWebSocketFrameHeader() says that " << dest.size()
<< " is not enough to write the header in. This should not happen.";
dest = dest.subspan(base::checked_cast<size_t>(result));
CHECK_LE(frame->header.payload_length,
base::checked_cast<uint64_t>(dest.size()));
const size_t frame_size = frame->header.payload_length;
if (frame_size > 0) {
dest.copy_prefix_from(frame->payload);
MaskWebSocketFramePayload(mask, 0, dest.first(frame_size));
dest = dest.subspan(frame_size);
}
}
DCHECK(dest.empty()) << "Buffer size calculation was wrong; " << dest.size()
<< " bytes left over.";
auto drainable_buffer = base::MakeRefCounted<DrainableIOBuffer>(
std::move(combined_buffer), total_size);
return WriteEverything(drainable_buffer);
}
void WebSocketBasicStream::Close() {
connection_->Disconnect();
}
std::string WebSocketBasicStream::GetSubProtocol() const {
return sub_protocol_;
}
std::string WebSocketBasicStream::GetExtensions() const { return extensions_; }
const NetLogWithSource& WebSocketBasicStream::GetNetLogWithSource() const {
return net_log_;
}
/*static*/
std::unique_ptr<WebSocketBasicStream>
WebSocketBasicStream::CreateWebSocketBasicStreamForTesting(
std::unique_ptr<ClientSocketHandle> connection,
const scoped_refptr<GrowableIOBuffer>& http_read_buffer,
const std::string& sub_protocol,
const std::string& extensions,
const NetLogWithSource& net_log,
WebSocketMaskingKeyGeneratorFunction key_generator_function) {
auto stream = std::make_unique<WebSocketBasicStream>(
std::make_unique<WebSocketClientSocketHandleAdapter>(
std::move(connection)),
http_read_buffer, sub_protocol, extensions, net_log);
stream->generate_websocket_masking_key_ = key_generator_function;
return stream;
}
int WebSocketBasicStream::ReadEverything(
std::vector<std::unique_ptr<WebSocketFrame>>* frames) {
DCHECK(frames->empty());
// If there is data left over after parsing the HTTP headers, attempt to parse
// it as WebSocket frames.
if (http_read_buffer_.get() && !is_http_read_buffer_decoded_) {
DCHECK_GE(http_read_buffer_->offset(), 0);
is_http_read_buffer_decoded_ = true;
std::vector<std::unique_ptr<WebSocketFrameChunk>> frame_chunks;
if (!parser_.Decode(http_read_buffer_->span_before_offset(),
&frame_chunks)) {
return WebSocketErrorToNetError(parser_.websocket_error());
}
if (!frame_chunks.empty()) {
int result = ConvertChunksToFrames(&frame_chunks, frames);
if (result != ERR_IO_PENDING)
return result;
}
}
// Run until socket stops giving us data or we get some frames.
while (true) {
if (buffer_size_manager_.buffer_size() != buffer_size_) {
read_buffer_ = base::MakeRefCounted<IOBufferWithSize>(
buffer_size_manager_.buffer_size() == BufferSize::kSmall
? kSmallReadBufferSize
: kLargeReadBufferSize);
buffer_size_ = buffer_size_manager_.buffer_size();
net_log_.AddEvent(
net::NetLogEventType::WEBSOCKET_READ_BUFFER_SIZE_CHANGED,
[&] { return NetLogBufferSizeParam(read_buffer_->size()); });
}
buffer_size_manager_.OnRead(base::TimeTicks::Now());
// base::Unretained(this) here is safe because net::Socket guarantees not to
// call any callbacks after Disconnect(), which we call from the destructor.
// The caller of ReadEverything() is required to keep |frames| valid.
int result = connection_->Read(
read_buffer_.get(), read_buffer_->size(),
base::BindOnce(&WebSocketBasicStream::OnReadComplete,
base::Unretained(this), base::Unretained(frames)));
if (result == ERR_IO_PENDING)
return result;
result = HandleReadResult(result, frames);
if (result != ERR_IO_PENDING)
return result;
DCHECK(frames->empty());
}
}
void WebSocketBasicStream::OnReadComplete(
std::vector<std::unique_ptr<WebSocketFrame>>* frames,
int result) {
result = HandleReadResult(result, frames);
if (result == ERR_IO_PENDING)
result = ReadEverything(frames);
if (result != ERR_IO_PENDING)
std::move(read_callback_).Run(result);
}
int WebSocketBasicStream::WriteEverything(
const scoped_refptr<DrainableIOBuffer>& buffer) {
while (buffer->BytesRemaining() > 0) {
// The use of base::Unretained() here is safe because on destruction we
// disconnect the socket, preventing any further callbacks.
int result = connection_->Write(
buffer.get(), buffer->BytesRemaining(),
base::BindOnce(&WebSocketBasicStream::OnWriteComplete,
base::Unretained(this), buffer),
kTrafficAnnotation);
if (result > 0) {
buffer->DidConsume(result);
} else {
return result;
}
}
return OK;
}
void WebSocketBasicStream::OnWriteComplete(
const scoped_refptr<DrainableIOBuffer>& buffer,
int result) {
if (result < 0) {
DCHECK_NE(ERR_IO_PENDING, result);
std::move(write_callback_).Run(result);
return;
}
DCHECK_NE(0, result);
buffer->DidConsume(result);
result = WriteEverything(buffer);
if (result != ERR_IO_PENDING)
std::move(write_callback_).Run(result);
}
int WebSocketBasicStream::HandleReadResult(
int result,
std::vector<std::unique_ptr<WebSocketFrame>>* frames) {
DCHECK_NE(ERR_IO_PENDING, result);
// This CHECK() is critical to prevent data corruption. See
// https://crbug.com/393000981.
CHECK(frames->empty());
if (result < 0)
return result;
if (result == 0)
return ERR_CONNECTION_CLOSED;
buffer_size_manager_.OnReadComplete(base::TimeTicks::Now(), result);
std::vector<std::unique_ptr<WebSocketFrameChunk>> frame_chunks;
if (!parser_.Decode(read_buffer_->first(base::checked_cast<size_t>(result)),
&frame_chunks)) {
return WebSocketErrorToNetError(parser_.websocket_error());
}
if (frame_chunks.empty())
return ERR_IO_PENDING;
return ConvertChunksToFrames(&frame_chunks, frames);
}
int WebSocketBasicStream::ConvertChunksToFrames(
std::vector<std::unique_ptr<WebSocketFrameChunk>>* frame_chunks,
std::vector<std::unique_ptr<WebSocketFrame>>* frames) {
for (auto& chunk : *frame_chunks) {
DCHECK(chunk == frame_chunks->back() || chunk->final_chunk)
<< "Only last chunk can have |final_chunk| set to be false.";
if (chunk->header) {
net_log_.AddEvent(net::NetLogEventType::WEBSOCKET_RECV_FRAME_HEADER, [&] {
return NetLogFrameHeaderParam(chunk->header.get());
});
}
auto frame_result = chunk_assembler_.HandleChunk(std::move(chunk));
if (!frame_result.has_value()) {
net::Error error = frame_result.error();
if (error == ERR_IO_PENDING) {
// We just need more data.
continue;
}
return frame_result.error();
}
auto frame = std::move(frame_result.value());
bool is_control_opcode =
WebSocketFrameHeader::IsKnownControlOpCode(frame->header.opcode) ||
WebSocketFrameHeader::IsReservedControlOpCode(frame->header.opcode);
if (is_control_opcode) {
const size_t length =
base::checked_cast<size_t>(frame->header.payload_length);
if (length > 0) {
auto copied_payload =
base::HeapArray<uint8_t>::CopiedFrom(frame->payload);
frame->payload = copied_payload.as_span();
control_frame_payloads_.emplace_back(std::move(copied_payload));
}
}
frames->emplace_back(std::move(frame));
}
frame_chunks->clear();
return frames->empty() ? ERR_IO_PENDING : OK;
}
} // namespace net
|