Skip to content

Commit

Permalink
Fix WebSocket implementation in Cobalt 25 (#3328)
Browse files Browse the repository at this point in the history
Fixed the following bugs to make it work:
- Deleted the whole concept of flow control;
- The data frame received from net was not populated;
- net now sends only a string view which does not promise validaty
later. So we store a copy of the data between consecutive reads of the
same frame.

b/339672080

(cherry picked from commit a7b54ab)
  • Loading branch information
johnxwork authored and anonymous1-me committed May 23, 2024
1 parent 93eb084 commit 8ce430e
Show file tree
Hide file tree
Showing 21 changed files with 21 additions and 3,050 deletions.
4 changes: 0 additions & 4 deletions cobalt/websocket/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ static_library("websocket") {
has_pedantic_warnings = true

sources = [
"buffered_amount_tracker.cc",
"buffered_amount_tracker.h",
"close_event.h",
"cobalt_web_socket_event_handler.cc",
"cobalt_web_socket_event_handler.h",
"sec_web_socket_key.h",
"web_socket.cc",
"web_socket.h",
"web_socket_impl.cc",
Expand All @@ -47,7 +44,6 @@ target(gtest_target_type, "websocket_test") {
sources = [
"mock_websocket_channel.cc",
"mock_websocket_channel.h",
"web_socket_impl_test.cc",
"web_socket_test.cc",
]
deps = [
Expand Down
62 changes: 0 additions & 62 deletions cobalt/websocket/buffered_amount_tracker.cc

This file was deleted.

64 changes: 0 additions & 64 deletions cobalt/websocket/buffered_amount_tracker.h

This file was deleted.

144 changes: 0 additions & 144 deletions cobalt/websocket/buffered_amount_tracker_test.cc

This file was deleted.

19 changes: 8 additions & 11 deletions cobalt/websocket/cobalt_web_socket_event_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@
namespace cobalt {
namespace websocket {
namespace {
typedef std::vector<std::pair<scoped_refptr<net::IOBuffer>, size_t>>
FrameDataVector;
typedef std::vector<std::string> FrameDataVector;
std::size_t GetMessageLength(const FrameDataVector& frame_data) {
std::size_t total_length = 0;
for (const auto& i : frame_data) {
total_length += i.second;
total_length += i.size();
}
return total_length;
}
Expand All @@ -42,12 +41,10 @@ std::size_t CombineFramesChunks(FrameDataVector::const_iterator begin,
std::size_t bytes_available = buffer_length;
for (FrameDataVector::const_iterator iterator = begin; iterator != end;
++iterator) {
const scoped_refptr<net::IOBuffer>& data = iterator->first;

std::size_t frame_chunk_size = iterator->second;

const auto& data = iterator->data();
std::size_t frame_chunk_size = iterator->size();
if (bytes_available >= frame_chunk_size) {
memcpy(out_destination, data->data(), frame_chunk_size);
memcpy(out_destination, data, frame_chunk_size);
out_destination += frame_chunk_size;
bytes_written += frame_chunk_size;
bytes_available -= frame_chunk_size;
Expand All @@ -64,18 +61,18 @@ void CobaltWebSocketEventHandler::OnAddChannelResponse(
const std::string& selected_subprotocol, const std::string& extensions) {
creator_->OnHandshakeComplete(selected_subprotocol);
}

void CobaltWebSocketEventHandler::OnDataFrame(bool fin,
WebSocketMessageType type,
base::span<const char> payload) {
std::string message(payload.data(), payload.size());
if (message_type_ == net::WebSocketFrameHeader::kOpCodeControlUnused) {
message_type_ = type;
}
if (type != net::WebSocketFrameHeader::kOpCodeContinuation) {
DCHECK_EQ(message_type_, type);
}
#ifndef COBALT_PENDING_CLEAN_UP
frame_data_.push_back(std::make_pair(std::move(payload), buffer_size));
#endif
frame_data_.push_back(std::move(message));
if (fin) {
std::size_t message_length = GetMessageLength(frame_data_);
scoped_refptr<net::IOBufferWithSize> buf =
Expand Down
5 changes: 1 addition & 4 deletions cobalt/websocket/cobalt_web_socket_event_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
#include <vector>

#include "base/basictypes.h"
#include "cobalt/websocket/web_socket_frame_container.h"
#include "cobalt/websocket/web_socket_handshake_helper.h"
#include "net/base/io_buffer.h"
#include "net/websockets/websocket_event_interface.h"
#include "net/websockets/websocket_frame_parser.h"
Expand Down Expand Up @@ -134,8 +132,7 @@ class CobaltWebSocketEventHandler : public net::WebSocketEventInterface {
// move more thing
WebSocketImpl* creator_;
// This vector should store data of data frames from the same message.
typedef std::vector<std::pair<scoped_refptr<net::IOBuffer>, size_t>>
FrameDataVector;
typedef std::vector<std::string> FrameDataVector;
FrameDataVector frame_data_;
WebSocketMessageType message_type_ =
net::WebSocketFrameHeader::kOpCodeControlUnused;
Expand Down
Loading

0 comments on commit 8ce430e

Please sign in to comment.