Commit 0a9ae8a2 authored by Samir Halilcevic's avatar Samir Halilcevic

Integrate review feedback

parent ef0d2b4c
......@@ -144,7 +144,12 @@ private:
// Signal abort to the upper layer and shutdown to the lower layer,
// with closing message
void abort_and_close_connection(sec reason, std::string_view msg);
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 -------------------------------------------------------
......
......@@ -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");
abort_and_close_connection(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,8 +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");
abort_and_close_connection(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.
......@@ -61,27 +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) {
// TODO
abort_and_close_connection(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");
abort_and_close_connection(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");
abort_and_close_connection(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) {
// TODO
abort_and_close_connection(sec::connection_closed, "");
abort_and_shutdown(sec::connection_closed);
return -1;
} else {
// End of fragmented input.
......@@ -100,23 +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");
abort_and_close_connection(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");
abort_and_close_connection(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");
abort_and_close_connection(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());
......@@ -247,8 +241,8 @@ void framing::ship_closing_message(status code, std::string_view msg) {
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));
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);
......@@ -259,18 +253,4 @@ void framing::ship_closing_message(status code, std::string_view msg) {
down_->end_output();
}
void framing::abort_and_close_connection(sec reason, std::string_view msg) {
if (msg.empty())
up_->abort(make_error(reason));
else
up_->abort(make_error(reason, std::string(msg)));
status code = status::unexpected_condition;
if (reason == sec::connection_closed)
code = status::normal_close;
else if (reason == sec::protocol_error)
code = status::protocol_error;
ship_closing_message(code, to_string(reason));
down_->shutdown();
}
} // namespace caf::net::web_socket
......@@ -144,7 +144,7 @@ SCENARIO("the client closes the connection with a closing handshake") {
make_test_data(0), handshake);
transport->push(handshake);
}
THEN("the server closes the connection with a closing handshake") {
THEN("the server closes the connection after sending a close frame") {
transport->handle_input();
detail::rfc6455::header hdr;
auto hdr_length
......
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