fix the shared_from_this lifetime in for ServerConnection and SecureServerConnection;

pull/121/merge
Bryan Biedenkapp 2 months ago
parent 90ca8f1a6a
commit 616b6c730a

@ -106,11 +106,9 @@ namespace restapi
*/ */
void handshake() void handshake()
{ {
if (!m_persistent) { auto self = this->shared_from_this();
auto self(this->shared_from_this());
}
m_socket.async_handshake(asio::ssl::stream_base::server, [this](asio::error_code ec) { m_socket.async_handshake(asio::ssl::stream_base::server, [this, self](asio::error_code ec) {
if (!ec) { if (!ec) {
read(); read();
} }
@ -122,11 +120,8 @@ namespace restapi
*/ */
void read() void read()
{ {
if (!m_persistent) { auto self = this->shared_from_this();
auto self(this->shared_from_this()); m_socket.async_read_some(asio::buffer(m_buffer), [this, self](asio::error_code ec, std::size_t recvLength) {
}
m_socket.async_read_some(asio::buffer(m_buffer), [=](asio::error_code ec, std::size_t recvLength) {
if (!ec) { if (!ec) {
HTTPLexer::ResultType result = HTTPLexer::GOOD; HTTPLexer::ResultType result = HTTPLexer::GOOD;
char* content; char* content;
@ -211,7 +206,7 @@ namespace restapi
if (ec) { if (ec) {
::LogError(LOG_REST, "SecureServerConnection::read(), %s, code = %u", ec.message().c_str(), ec.value()); ::LogError(LOG_REST, "SecureServerConnection::read(), %s, code = %u", ec.message().c_str(), ec.value());
} }
m_connectionManager.stop(this->shared_from_this()); m_connectionManager.stop(self);
m_continue = false; m_continue = false;
} }
}); });
@ -222,14 +217,14 @@ namespace restapi
*/ */
void write() void write()
{ {
if (!m_persistent) { auto self = this->shared_from_this();
auto self(this->shared_from_this());
} else { if (m_persistent) {
m_reply.headers.add("Connection", "keep-alive"); m_reply.headers.add("Connection", "keep-alive");
} }
auto buffers = m_reply.toBuffers(); auto buffers = m_reply.toBuffers();
asio::async_write(m_socket, buffers, [=](asio::error_code ec, std::size_t) { asio::async_write(m_socket, buffers, [this, self](asio::error_code ec, std::size_t) {
if (m_persistent) { if (m_persistent) {
m_lexer.reset(); m_lexer.reset();
m_reply.headers = HTTPHeaders(); m_reply.headers = HTTPHeaders();
@ -253,7 +248,7 @@ namespace restapi
if (ec) { if (ec) {
::LogError(LOG_REST, "SecureServerConnection::write(), %s, code = %u", ec.message().c_str(), ec.value()); ::LogError(LOG_REST, "SecureServerConnection::write(), %s, code = %u", ec.message().c_str(), ec.value());
} }
m_connectionManager.stop(this->shared_from_this()); m_connectionManager.stop(self);
} }
} }
}); });

@ -103,11 +103,8 @@ namespace restapi
*/ */
void read() void read()
{ {
if (!m_persistent) { auto self = this->shared_from_this();
auto self(this->shared_from_this()); m_socket.async_read_some(asio::buffer(m_buffer), [this, self](asio::error_code ec, std::size_t recvLength) {
}
m_socket.async_read_some(asio::buffer(m_buffer), [=](asio::error_code ec, std::size_t recvLength) {
if (!ec) { if (!ec) {
HTTPLexer::ResultType result = HTTPLexer::GOOD; HTTPLexer::ResultType result = HTTPLexer::GOOD;
char* content; char* content;
@ -192,7 +189,7 @@ namespace restapi
if (ec) { if (ec) {
::LogError(LOG_REST, "ServerConnection::read(), %s, code = %u", ec.message().c_str(), ec.value()); ::LogError(LOG_REST, "ServerConnection::read(), %s, code = %u", ec.message().c_str(), ec.value());
} }
m_connectionManager.stop(this->shared_from_this()); m_connectionManager.stop(self);
m_continue = false; m_continue = false;
m_contResult = HTTPLexer::INDETERMINATE; m_contResult = HTTPLexer::INDETERMINATE;
} }
@ -204,14 +201,14 @@ namespace restapi
*/ */
void write() void write()
{ {
if (!m_persistent) { auto self = this->shared_from_this();
auto self(this->shared_from_this());
} else { if (m_persistent) {
m_reply.headers.add("Connection", "keep-alive"); m_reply.headers.add("Connection", "keep-alive");
} }
auto buffers = m_reply.toBuffers(); auto buffers = m_reply.toBuffers();
asio::async_write(m_socket, buffers, [=](asio::error_code ec, std::size_t) { asio::async_write(m_socket, buffers, [this, self](asio::error_code ec, std::size_t) {
if (m_persistent) { if (m_persistent) {
m_lexer.reset(); m_lexer.reset();
m_reply.headers = HTTPHeaders(); m_reply.headers = HTTPHeaders();
@ -235,7 +232,7 @@ namespace restapi
if (ec) { if (ec) {
::LogError(LOG_REST, "ServerConnection::write(), %s, code = %u", ec.message().c_str(), ec.value()); ::LogError(LOG_REST, "ServerConnection::write(), %s, code = %u", ec.message().c_str(), ec.value());
} }
m_connectionManager.stop(this->shared_from_this()); m_connectionManager.stop(self);
} }
} }
}); });

Loading…
Cancel
Save

Powered by TurnKey Linux.