From d72f67fd6c9db5f0e23c12dde2ac6bf3887a2e17 Mon Sep 17 00:00:00 2001 From: Hodlinator <172445034+hodlinator@users.noreply.github.com> Date: Mon, 24 Aug 2026 11:36:47 +0200 Subject: [PATCH] refactor: Expose additional HTTPRemoteClient fields through accessors --- src/httpserver.cpp | 7 +++---- src/httpserver.h | 7 ++++++- src/test/httpserver_tests.cpp | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/httpserver.cpp b/src/httpserver.cpp index b43bdd1dddd..04839f1406c 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -659,7 +659,7 @@ void HTTPRemoteClient::Send(const HTTPResponse& res, std::span CService HTTPRequest::GetPeer() const { if (std::shared_ptr c{m_client.lock()}) { - return c->m_addr; + return c->GetPeer(); } else { return {}; } @@ -989,7 +989,7 @@ HTTPServer::IOReadiness HTTPServer::GenerateWaitSockets() const for (const auto& http_client : m_connected) { // Safely copy the shared pointer to the socket - std::shared_ptr sock{WITH_LOCK(http_client->m_sock_mutex, return http_client->m_sock;)}; + std::shared_ptr sock{http_client->GetSock()}; // Check if client is ready to send data. Don't try to receive again // until the send buffer is cleared (all data sent to client). @@ -997,8 +997,7 @@ HTTPServer::IOReadiness HTTPServer::GenerateWaitSockets() const // never hold m_sock_mutex and m_send_mutex at the same time here. // MaybeSendBytesFromBuffer() locks m_send_mutex then m_sock_mutex, so nesting // them in the opposite order here would risk a lock-order inversion deadlock. - const bool send_ready{WITH_LOCK(http_client->m_send_mutex, return http_client->m_send_ready;)}; - Sock::Event event = (send_ready ? Sock::SendEvent : Sock::RecvEvent); + Sock::Event event = (http_client->ReadyToSend() ? Sock::SendEvent : Sock::RecvEvent); io_readiness.events_per_sock.emplace(sock, Sock::Events{event}); io_readiness.httpclients_per_sock.emplace(sock, http_client); } diff --git a/src/httpserver.h b/src/httpserver.h index f06d300c08a..83e6882a4a8 100644 --- a/src/httpserver.h +++ b/src/httpserver.h @@ -508,7 +508,7 @@ public: * Written to by http worker threads, read and erased by HTTPServer I/O thread */ /// @{ - Mutex m_send_mutex; + mutable Mutex m_send_mutex; std::vector m_send_buffer GUARDED_BY(m_send_mutex); /// @} @@ -570,6 +570,11 @@ public: HTTPRemoteClient(const HTTPRemoteClient&) = delete; HTTPRemoteClient& operator=(const HTTPRemoteClient&) = delete; + const std::string& GetOrigin() const { return m_origin; } + const CService& GetPeer() const { return m_addr; } + std::shared_ptr GetSock() EXCLUSIVE_LOCKS_REQUIRED(!m_sock_mutex) { return WITH_LOCK(m_sock_mutex, return m_sock;); } + bool ReadyToSend() const EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex) { return WITH_LOCK(m_send_mutex, return m_send_ready;); } + void Send(const HTTPResponse& res, std::span reply_body, bool keep_alive) EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex, !m_sock_mutex); void Receive() EXCLUSIVE_LOCKS_REQUIRED(!m_sock_mutex); diff --git a/src/test/httpserver_tests.cpp b/src/test/httpserver_tests.cpp index 6d79f0dd32e..c5c5c218c0e 100644 --- a/src/test/httpserver_tests.cpp +++ b/src/test/httpserver_tests.cpp @@ -857,7 +857,7 @@ BOOST_AUTO_TEST_CASE(http_server_socket_tests) // Inspect the connection pointed to from the request client = requests.front()->GetClient(); BOOST_REQUIRE(client); - BOOST_CHECK_EQUAL(client->m_origin, "5.5.5.5:6789"); + BOOST_CHECK_EQUAL(client->GetOrigin(), "5.5.5.5:6789"); // Respond to request requests.front()->WriteReply(HTTP_OK, "874140\n");