From 5b06d90831691d51e665a017a6da983e0d3920c6 Mon Sep 17 00:00:00 2001 From: Hodlinator <172445034+hodlinator@users.noreply.github.com> Date: Tue, 25 Aug 2026 13:21:43 +0200 Subject: [PATCH] refactor: Replace HTTPServer::MaybeDispatchRequestsFromClient() with HTTPRemoteClient::TryReadRequest() --- src/httpserver.cpp | 19 ++++++++++++------- src/httpserver.h | 18 ++++++++---------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 64e8a2748c6..b43bdd1dddd 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -910,7 +910,11 @@ void HTTPServer::SocketHandlerConnected(const IOReadiness& io_readiness) const // This executes for every client whether or not reading or writing // took place because it also (might) parse a request we have already // received and pass it to a worker thread. - MaybeDispatchRequestsFromClient(client); + if (std::unique_ptr request{HTTPRemoteClient::TryReadRequest(client)}) + { + LOCK(m_request_dispatcher_mutex); + m_request_dispatcher(std::move(request)); + } } } @@ -1029,12 +1033,12 @@ void HTTPServer::ThreadSocketHandler() } } -void HTTPServer::MaybeDispatchRequestsFromClient(const std::shared_ptr& client) const +std::unique_ptr HTTPRemoteClient::TryReadRequest(const std::shared_ptr& client) { // If we are already handling a request from // this client, do nothing. We'll check again on the next I/O // loop iteration. - if (client->m_req_busy) return; + if (client->m_req_busy) return nullptr; if (!client->m_req) { client->m_req = std::make_unique(client); @@ -1053,7 +1057,7 @@ void HTTPServer::MaybeDispatchRequestsFromClient(const std::shared_ptrm_req, HTTP_CONTENT_TOO_LARGE); client->m_disconnect = true; - return; + return nullptr; } catch (const std::runtime_error& e) { LogDebug( BCLog::HTTP, @@ -1065,7 +1069,7 @@ void HTTPServer::MaybeDispatchRequestsFromClient(const std::shared_ptrm_req, HTTP_BAD_REQUEST); client->m_disconnect = true; - return; + return nullptr; } // If the request is ready, hand it to a worker. @@ -1078,10 +1082,11 @@ void HTTPServer::MaybeDispatchRequestsFromClient(const std::shared_ptrm_origin, client->m_id); - LOCK(m_request_dispatcher_mutex); client->m_req_busy = true; - m_request_dispatcher(std::move(client->m_req)); + return std::move(client->m_req); } + + return nullptr; } void HTTPServer::DisconnectClients() diff --git a/src/httpserver.h b/src/httpserver.h index 33da812c07e..de4af1d3c7a 100644 --- a/src/httpserver.h +++ b/src/httpserver.h @@ -464,16 +464,6 @@ private: */ void ThreadSocketHandler() EXCLUSIVE_LOCKS_REQUIRED(!m_request_dispatcher_mutex); - /** - * Try to read HTTPRequests from a client's receive buffer. - * Complete requests are dispatched, incomplete requests are - * left in the buffer to wait for more data. Some read errors - * will mark this client for disconnection. - * @param[in] client The HTTPRemoteClient to read requests from - */ - void MaybeDispatchRequestsFromClient(const std::shared_ptr& client) const - EXCLUSIVE_LOCKS_REQUIRED(!m_request_dispatcher_mutex); - /** * Close underlying socket connections for flagged clients * by removing their shared pointer from m_connected. If an HTTPRemoteClient @@ -585,6 +575,14 @@ public: bool MaybeDisconnect(std::chrono::time_point now, std::chrono::seconds rpcservertimeout, bool disconnect_all); + /** + * Try to read an HTTPRequest from a client's receive buffer. + * Only complete requests are returned, incomplete requests are + * left in the buffer to wait for more data. Some read errors + * will mark this client for disconnection. + */ + static std::unique_ptr TryReadRequest(const std::shared_ptr& client); + /** * Try to read an HTTP request from the receive buffer. * Updates HTTPRequest.m_state and drains buffer on error.