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
|
// Copyright 2024 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_chunk_assembler.h"
#include "base/compiler_specific.h"
#include "base/containers/extend.h"
#include "base/containers/span.h"
#include "base/types/expected.h"
#include "net/base/net_errors.h"
#include "net/websockets/websocket_errors.h"
#include "net/websockets/websocket_frame.h"
namespace net {
namespace {
// This uses type uint64_t to match the definition of
// WebSocketFrameHeader::payload_length in websocket_frame.h.
constexpr uint64_t kMaxControlFramePayload = 125;
// Utility function to create a WebSocketFrame
std::unique_ptr<WebSocketFrame> MakeWebSocketFrame(
const WebSocketFrameHeader& header,
base::span<uint8_t> payload) {
auto frame = std::make_unique<WebSocketFrame>(header.opcode);
frame->header.CopyFrom(header);
if (header.masked) {
MaskWebSocketFramePayload(header.masking_key, 0, payload);
}
frame->payload = payload;
return frame;
}
} // namespace
WebSocketChunkAssembler::WebSocketChunkAssembler() = default;
WebSocketChunkAssembler::~WebSocketChunkAssembler() = default;
void WebSocketChunkAssembler::Reset() {
current_frame_header_.reset();
chunk_buffer_.clear();
state_ = AssemblyState::kInitialFrame;
}
base::expected<std::unique_ptr<WebSocketFrame>, net::Error>
WebSocketChunkAssembler::HandleChunk(
std::unique_ptr<WebSocketFrameChunk> chunk) {
if (state_ == AssemblyState::kMessageFinished) {
Reset();
}
if (chunk->header) {
CHECK_EQ(state_, AssemblyState::kInitialFrame);
CHECK(!current_frame_header_);
current_frame_header_ = std::move(chunk->header);
}
CHECK(current_frame_header_);
const WebSocketFrameHeader::OpCode opcode = current_frame_header_->opcode;
const bool is_control_frame =
WebSocketFrameHeader::IsKnownControlOpCode(opcode) ||
WebSocketFrameHeader::IsReservedControlOpCode(opcode);
const bool is_data_frame = WebSocketFrameHeader::IsKnownDataOpCode(opcode) ||
WebSocketFrameHeader::IsReservedDataOpCode(opcode);
CHECK(is_control_frame || is_data_frame);
if (is_control_frame && !current_frame_header_->final) {
return base::unexpected(ERR_WS_PROTOCOL_ERROR);
}
if (is_control_frame &&
current_frame_header_->payload_length > kMaxControlFramePayload) {
return base::unexpected(ERR_WS_PROTOCOL_ERROR);
}
const bool is_first_chunk = state_ == AssemblyState::kInitialFrame;
const bool is_final_chunk = chunk->final_chunk;
const bool is_empty_middle_chunk =
!is_first_chunk && !is_final_chunk && chunk->payload.empty();
if (is_empty_middle_chunk) {
return base::unexpected(ERR_IO_PENDING);
}
// Handle single-chunk frame without buffering
const bool is_single_chunk_frame = is_first_chunk && is_final_chunk;
if (is_single_chunk_frame) {
CHECK_EQ(current_frame_header_->payload_length, chunk->payload.size());
auto frame = MakeWebSocketFrame(*current_frame_header_,
base::as_writable_bytes(chunk->payload));
state_ = AssemblyState::kMessageFinished;
return frame;
}
// For data frames, process each chunk separately without accumulating all
// in memory (streaming to render process)
if (is_data_frame) {
auto frame = MakeWebSocketFrame(*current_frame_header_,
base::as_writable_bytes(chunk->payload));
// Since we are synthesizing a frame that the origin server didn't send,
// we need to comply with the requirement ourselves.
if (state_ == AssemblyState::kContinuationFrame) {
// This is needed to satisfy the constraint of RFC7692:
//
// An endpoint MUST NOT set the "Per-Message Compressed" bit of control
// frames and non-first fragments of a data message.
frame->header.opcode = WebSocketFrameHeader::kOpCodeContinuation;
frame->header.reserved1 = false;
frame->header.reserved2 = false;
frame->header.reserved3 = false;
}
frame->header.payload_length = chunk->payload.size();
frame->header.final = current_frame_header_->final && chunk->final_chunk;
if (is_final_chunk) {
state_ = AssemblyState::kMessageFinished;
} else {
state_ = AssemblyState::kContinuationFrame;
}
return frame;
}
CHECK(is_control_frame && current_frame_header_->final);
// Control frames should be processed as a unit as they are small in size.
base::Extend(chunk_buffer_, chunk->payload);
if (!chunk->final_chunk) {
state_ = AssemblyState::kControlFrame;
return base::unexpected(ERR_IO_PENDING);
}
state_ = AssemblyState::kMessageFinished;
CHECK_EQ(current_frame_header_->payload_length, chunk_buffer_.size());
auto frame = MakeWebSocketFrame(*current_frame_header_,
base::as_writable_byte_span(chunk_buffer_));
state_ = AssemblyState::kMessageFinished;
return frame;
}
} // namespace net
|