diff --git a/libraries/core_libs/network/include/network/http_server.hpp b/libraries/core_libs/network/include/network/http_server.hpp index bfe7e586ea..615f0cb954 100644 --- a/libraries/core_libs/network/include/network/http_server.hpp +++ b/libraries/core_libs/network/include/network/http_server.hpp @@ -60,10 +60,11 @@ class HttpServer : public std::enable_shared_from_this { class HttpConnection : public std::enable_shared_from_this { public: explicit HttpConnection(const std::shared_ptr& http_server); - virtual ~HttpConnection() = default; + virtual ~HttpConnection() { HttpConnection::stop(); } boost::asio::ip::tcp::socket& getSocket() { return socket_; } virtual std::shared_ptr getShared(); void read(); + void stop(); protected: std::shared_ptr server_; diff --git a/libraries/core_libs/network/src/http_server.cpp b/libraries/core_libs/network/src/http_server.cpp index 4c269a45b9..d89d4ee84b 100644 --- a/libraries/core_libs/network/src/http_server.cpp +++ b/libraries/core_libs/network/src/http_server.cpp @@ -78,6 +78,19 @@ std::shared_ptr HttpConnection::getShared() { HttpConnection::HttpConnection(const std::shared_ptr &http_server) : server_(http_server), socket_(http_server->getIoContext()) {} +void HttpConnection::stop() { + if (socket_.is_open()) { + try { + LOG(server_->log_dg_) << "Closing connection..."; + boost::asio::socket_base::linger option(true, 0); + socket_.set_option(option); + socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both); + socket_.close(); + } catch (...) { + } + } +} + void HttpConnection::read() { boost::beast::http::async_read( socket_, buffer_, request_, [this, this_sp = getShared()](boost::system::error_code const &ec, size_t) { @@ -87,8 +100,11 @@ void HttpConnection::read() { assert(server_->request_processor_); LOG(server_->log_dg_) << "Received: " << request_; response_ = server_->request_processor_->process(request_); - boost::beast::http::async_write(socket_, response_, - [this_sp = getShared()](auto const & /*ec*/, auto /*bytes_transfered*/) {}); + response_.version(request_.version()); + response_.keep_alive(false); + boost::beast::http::async_write( + socket_, response_, + [this_sp = getShared()](auto const & /*ec*/, auto /*bytes_transfered*/) { this_sp->stop(); }); } }); }