diff --git a/websocketpp/impl/connection_impl.hpp b/websocketpp/impl/connection_impl.hpp index 6c97998cd2..e30720516f 100644 --- a/websocketpp/impl/connection_impl.hpp +++ b/websocketpp/impl/connection_impl.hpp @@ -213,28 +213,17 @@ void connection::close(const close::status::value code, const std::string & reason, lib::error_code & ec) { m_alog.write(log::alevel::devel,"connection close"); - // check state + if (m_state != session::state::OPEN) { ec = error::make_error_code(error::invalid_state); return; } - // check reason length - if (reason.size() > frame::limits::close_reason_size) { - ec = this->send_close_frame( - code, - std::string(reason,0,frame::limits::close_reason_size), - false, - close::status::terminal(code) - ); - } else { - ec = this->send_close_frame( - code, - reason, - false, - close::status::terminal(code) - ); - } + // Truncate reason to maximum size allowable in a close frame. + std::string tr(reason,0,std::min(reason.size(), + frame::limits::close_reason_size)); + + ec = this->send_close_frame(code,tr,false,close::status::terminal(code)); } template