Skip to content

Commit

Permalink
fix: server stop send websocket close message
Browse files Browse the repository at this point in the history
  • Loading branch information
helintongh committed Oct 13, 2023
1 parent 2422dfe commit f18c3ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
14 changes: 13 additions & 1 deletion include/cinatra/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,15 @@ class connection : public base_connection,
return;
}

if (req_.get_content_type() == content_type::websocket) {
req_.set_websocket_state(true);
std::string close_reason = "server close\n";
std::string close_msg = ws_.format_close_payload(
close_code::normal, close_reason.data(), close_reason.size());
auto header = ws_.format_header(close_msg.length(), opcode::close);
send_msg(std::move(header), std::move(close_msg));
}

req_.close_upload_file();
shutdown();
std::error_code ec;
Expand Down Expand Up @@ -1133,7 +1142,9 @@ class connection : public base_connection,
[this, self](const std::error_code &ec, size_t bytes_transferred) {
if (ec) {
cancel_timer();
req_.call_event(data_proc_state::data_error);

if (!req_.get_websocket_state())
req_.call_event(data_proc_state::data_error);

close();
return;
Expand Down Expand Up @@ -1239,6 +1250,7 @@ class connection : public base_connection,
close_code::normal, close_frame.message, len);
auto header = ws_.format_header(close_msg.length(), opcode::close);
send_msg(std::move(header), std::move(close_msg));
req_.set_websocket_state(true);
} break;
case cinatra::ws_frame_type::WS_PING_FRAME: {
auto header = ws_.format_header(payload.length(), opcode::pong);
Expand Down
6 changes: 6 additions & 0 deletions include/cinatra/request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,10 @@ class request {

data_proc_state get_state() const { return state_; }

void set_websocket_state(bool is_closed) { is_websocket_closed_ = is_closed; }

bool get_websocket_state() { return is_websocket_closed_; }

void set_part_data(std::string_view data) {
#ifdef CINATRA_ENABLE_GZIP
if (has_gzip_) {
Expand Down Expand Up @@ -944,5 +948,7 @@ class request {
event_call_backs_ = {};
std::smatch matches_;
std::unordered_map<std::string, int> restful_params_;

bool is_websocket_closed_ = false;
};
} // namespace cinatra

0 comments on commit f18c3ec

Please sign in to comment.