Unverified Commit 6493c42a authored by Dominik Charousset's avatar Dominik Charousset Committed by GitHub

Merge pull request #1461

Isolate closing handshake
parents 493e1077 0a9ae8a2
......@@ -139,6 +139,18 @@ private:
template <class T>
void ship_frame(std::vector<T>& buf);
// Sends closing message, can be error status, or closing handshake
void ship_closing_message(status code, std::string_view desc);
// Signal abort to the upper layer and shutdown to the lower layer,
// with closing message
template <class... Ts>
void abort_and_shutdown(sec reason, Ts&&... xs) {
auto err = make_error(reason, std::forward<Ts>(xs)...);
up_->abort(err);
shutdown(err);
}
// -- member variables -------------------------------------------------------
/// Points to the transport layer below.
......
......@@ -30,8 +30,8 @@ ptrdiff_t framing::consume(byte_span buffer, byte_span) {
auto hdr_bytes = detail::rfc6455::decode_header(buffer, hdr);
if (hdr_bytes < 0) {
CAF_LOG_DEBUG("decoded malformed data: hdr_bytes < 0");
up_->abort(make_error(sec::protocol_error,
"negative header size on WebSocket connection"));
abort_and_shutdown(sec::protocol_error,
"negative header size on WebSocket connection");
return -1;
}
if (hdr_bytes == 0) {
......@@ -41,7 +41,7 @@ ptrdiff_t framing::consume(byte_span buffer, byte_span) {
// Make sure the entire frame (including header) fits into max_frame_size.
if (hdr.payload_len >= (max_frame_size - static_cast<size_t>(hdr_bytes))) {
CAF_LOG_DEBUG("WebSocket frame too large");
up_->abort(make_error(sec::protocol_error, "WebSocket frame too large"));
abort_and_shutdown(sec::protocol_error, "WebSocket frame too large");
return -1;
}
// Wait for more data if necessary.
......@@ -60,24 +60,24 @@ ptrdiff_t framing::consume(byte_span buffer, byte_span) {
if (opcode_ == nil_code) {
// Call upper layer.
if (hdr.opcode == detail::rfc6455::connection_close) {
up_->abort(make_error(sec::connection_closed));
abort_and_shutdown(sec::connection_closed);
return -1;
} else if (!handle(hdr.opcode, payload)) {
return -1;
}
} else if (hdr.opcode != detail::rfc6455::continuation_frame) {
CAF_LOG_DEBUG("expected a WebSocket continuation_frame");
up_->abort(make_error(sec::protocol_error,
"expected a WebSocket continuation_frame"));
abort_and_shutdown(sec::protocol_error,
"expected a WebSocket continuation_frame");
return -1;
} else if (payload_buf_.size() + payload_len > max_frame_size) {
CAF_LOG_DEBUG("fragmented WebSocket payload exceeds maximum size");
up_->abort(make_error(sec::protocol_error, "fragmented WebSocket payload "
"exceeds maximum size"));
abort_and_shutdown(sec::protocol_error, "fragmented WebSocket payload "
"exceeds maximum size");
return -1;
} else {
if (hdr.opcode == detail::rfc6455::connection_close) {
up_->abort(make_error(sec::connection_closed));
abort_and_shutdown(sec::connection_closed);
return -1;
} else {
// End of fragmented input.
......@@ -96,22 +96,21 @@ ptrdiff_t framing::consume(byte_span buffer, byte_span) {
if (hdr.opcode == detail::rfc6455::continuation_frame) {
CAF_LOG_DEBUG("received WebSocket continuation "
"frame without prior opcode");
up_->abort(make_error(sec::protocol_error,
"received WebSocket continuation "
"frame without prior opcode"));
abort_and_shutdown(sec::protocol_error,
"received WebSocket continuation "
"frame without prior opcode");
return -1;
}
opcode_ = hdr.opcode;
} else if (hdr.opcode != detail::rfc6455::continuation_frame) {
CAF_LOG_DEBUG("expected a continuation frame");
up_->abort(make_error(sec::protocol_error, //
"expected a continuation frame"));
abort_and_shutdown(sec::protocol_error, "expected a continuation frame");
return -1;
} else if (payload_buf_.size() + payload_len > max_frame_size) {
// Reject assembled payloads that exceed max_frame_size.
CAF_LOG_DEBUG("fragmented WebSocket payload exceeds maximum size");
up_->abort(make_error(sec::protocol_error, "fragmented WebSocket payload "
"exceeds maximum size"));
abort_and_shutdown(sec::protocol_error, "fragmented WebSocket payload "
"exceeds maximum size");
return -1;
}
payload_buf_.insert(payload_buf_.end(), payload.begin(), payload.end());
......@@ -150,22 +149,7 @@ void framing::write_later() {
}
void framing::shutdown(status code, std::string_view msg) {
auto code_val = static_cast<uint16_t>(code);
uint32_t mask_key = 0;
byte_buffer payload;
payload.reserve(msg.size() + 2);
payload.push_back(static_cast<std::byte>((code_val & 0xFF00) >> 8));
payload.push_back(static_cast<std::byte>(code_val & 0x00FF));
for (auto c : msg)
payload.push_back(static_cast<std::byte>(c));
if (mask_outgoing_frames) {
mask_key = static_cast<uint32_t>(rng_());
detail::rfc6455::mask_data(mask_key, payload);
}
down_->begin_output();
detail::rfc6455::assemble_frame(detail::rfc6455::connection_close, mask_key,
payload, down_->output_buffer());
down_->end_output();
ship_closing_message(code, msg);
down_->shutdown();
}
......@@ -250,4 +234,23 @@ void framing::ship_frame(std::vector<T>& buf) {
buf.clear();
}
void framing::ship_closing_message(status code, std::string_view msg) {
auto code_val = static_cast<uint16_t>(code);
uint32_t mask_key = 0;
byte_buffer payload;
payload.reserve(msg.size() + 2);
payload.push_back(static_cast<std::byte>((code_val & 0xFF00) >> 8));
payload.push_back(static_cast<std::byte>(code_val & 0x00FF));
auto* first = reinterpret_cast<const std::byte*>(msg.data());
payload.insert(payload.end(), first, first + msg.size());
if (mask_outgoing_frames) {
mask_key = static_cast<uint32_t>(rng_());
detail::rfc6455::mask_data(mask_key, payload);
}
down_->begin_output();
detail::rfc6455::assemble_frame(detail::rfc6455::connection_close, mask_key,
payload, down_->output_buffer());
down_->end_output();
}
} // namespace caf::net::web_socket
......@@ -20,7 +20,9 @@ void lower_layer::shutdown() {
}
void lower_layer::shutdown(const error& reason) {
if (reason.code() == static_cast<uint8_t>(sec::protocol_error)) {
if (reason.code() == static_cast<uint8_t>(sec::connection_closed)) {
shutdown(status::normal_close, to_string(reason));
} else if (reason.code() == static_cast<uint8_t>(sec::protocol_error)) {
shutdown(status::protocol_error, to_string(reason));
} else {
shutdown(status::unexpected_condition, to_string(reason));
......
......@@ -136,4 +136,30 @@ SCENARIO("the client sends an invalid ping that closes the connection") {
}
}
SCENARIO("the client closes the connection with a closing handshake") {
GIVEN("a valid WebSocket connection") {
WHEN("the client sends a closing handshake") {
std::vector<std::byte> handshake;
detail::rfc6455::assemble_frame(detail::rfc6455::connection_close, 0x0,
make_test_data(0), handshake);
transport->push(handshake);
}
THEN("the server closes the connection after sending a close frame") {
transport->handle_input();
detail::rfc6455::header hdr;
auto hdr_length
= detail::rfc6455::decode_header(transport->output_buffer(), hdr);
CHECK(app->has_aborted());
CHECK_EQ(app->abort_reason, sec::connection_closed);
CHECK_EQ(hdr_length, 2);
CHECK_EQ(hdr.opcode, detail::rfc6455::connection_close);
CHECK(hdr.fin);
CHECK(hdr.payload_len >= 2);
auto status = (std::to_integer<int>(transport->output_buffer()[2]) << 8)
+ std::to_integer<int>(transport->output_buffer()[3]);
CHECK_EQ(status, static_cast<int>(net::web_socket::status::normal_close));
}
}
}
END_FIXTURE_SCOPE()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment