Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: server stop send websocket close message #420

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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