fix: keep HTTP response alive during async_write

This commit is contained in:
Andrey Kutin 2026-10-25 20:05:17 +03:00
parent 68c09700ff
commit 7f114ed92b
3 changed files with 8 additions and 6 deletions

View File

@ -66,7 +66,7 @@ install(FILES LICENSE DESTINATION /usr/share/doc/cards)
# ─── Packaging (CPack) ──────────────────────────────────────────────── # ─── Packaging (CPack) ────────────────────────────────────────────────
set(CPACK_PACKAGE_NAME "cards") set(CPACK_PACKAGE_NAME "cards")
set(CPACK_PACKAGE_VERSION "1.0.1") set(CPACK_PACKAGE_VERSION "1.0.4")
set(CPACK_PACKAGE_VENDOR "cards") set(CPACK_PACKAGE_VENDOR "cards")
set(CPACK_PACKAGE_MAINTAINER "adwet") set(CPACK_PACKAGE_MAINTAINER "adwet")
set(CPACK_PACKAGE_CONTACT "adwet@yandex.ru") set(CPACK_PACKAGE_CONTACT "adwet@yandex.ru")

View File

@ -111,16 +111,18 @@ void HttpSession::processRequest(http::request<http::string_body>&& request) {
}); });
} }
void HttpSession::writeResponse(http::response<http::string_body>&& res) { void HttpSession::writeResponse(http::response<http::string_body> res) {
auto self = shared_from_this(); auto self = shared_from_this();
auto response = std::make_shared<http::response<http::string_body>>(std::move(res));
http::async_write( http::async_write(
stream, stream,
res, *response,
[self](beast::error_code ec, std::size_t) { [self, response](beast::error_code ec, std::size_t) {
if (ec) { if (ec) {
self->handleError(ec, "write"); self->handleError(ec, "write");
} }
self->stream.socket().shutdown(net::ip::tcp::socket::shutdown_send, ec); beast::error_code ignored;
self->stream.socket().shutdown(net::ip::tcp::socket::shutdown_send, ignored);
} }
); );
} }

View File

@ -32,7 +32,7 @@ private:
void readRequest(); void readRequest();
void onRead(beast::error_code ec, std::size_t bytes_transferred); void onRead(beast::error_code ec, std::size_t bytes_transferred);
void processRequest(http::request<http::string_body>&& req); void processRequest(http::request<http::string_body>&& req);
void writeResponse(http::response<http::string_body>&& res); void writeResponse(http::response<http::string_body> res);
void handleError(beast::error_code ec, const char* what); void handleError(beast::error_code ec, const char* what);
beast::tcp_stream stream; beast::tcp_stream stream;