From a31b62f92644e2d89966dc06767c88c4ac64afc8 Mon Sep 17 00:00:00 2001 From: Matthew Zipkin Date: Mon, 30 Sep 2024 12:28:54 -0400 Subject: [PATCH] http: enclose libevent-dependent code in a namespace This commit is a no-op to isolate HTTP methods and objects that depend on libevent. Following commits will add replacement objects and methods in a new namespace for testing and review before switching over the server. --- src/httprpc.cpp | 1 + src/httpserver.cpp | 6 ++++++ src/httpserver.h | 8 +++++++- src/init.cpp | 4 ++++ src/rest.cpp | 1 + src/rpc/node.cpp | 2 +- src/test/fuzz/http_request.cpp | 2 ++ src/test/httpserver_tests.cpp | 1 + 8 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/httprpc.cpp b/src/httprpc.cpp index 57893702b8b..159f97966e6 100644 --- a/src/httprpc.cpp +++ b/src/httprpc.cpp @@ -26,6 +26,7 @@ #include #include +using http_libevent::HTTPRequest; using util::SplitString; using util::TrimStringView; diff --git a/src/httpserver.cpp b/src/httpserver.cpp index bd2dec19b97..6fba353527d 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -43,6 +43,7 @@ #include using common::InvalidPortErrMsg; +using http_libevent::HTTPRequest; /** Maximum size of http request (request line + headers) */ static const size_t MAX_HEADERS_SIZE = 8192; @@ -438,6 +439,7 @@ static void libevent_log_cb(int severity, const char *msg) LogPrintLevel(BCLog::LIBEVENT, level, "%s\n", msg); } +namespace http_libevent { bool InitHTTPServer(const util::SignalInterrupt& interrupt) { if (!InitHTTPAllowList()) @@ -559,6 +561,7 @@ void StopHTTPServer() g_work_queue.reset(); LogDebug(BCLog::HTTP, "Stopped HTTP server\n"); } +} // namespace http_libevent struct event_base* EventBase() { @@ -591,6 +594,8 @@ void HTTPEvent::trigger(struct timeval* tv) else evtimer_add(ev, tv); // trigger after timeval passed } + +namespace http_libevent { HTTPRequest::HTTPRequest(struct evhttp_request* _req, const util::SignalInterrupt& interrupt, bool _replySent) : req(_req), m_interrupt(interrupt), replySent(_replySent) { @@ -753,6 +758,7 @@ std::optional GetQueryParameterFromUri(const char* uri, const std:: return result; } +} // namespace http_libevent void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler) { diff --git a/src/httpserver.h b/src/httpserver.h index 6535dc6086c..cd8d0724a6f 100644 --- a/src/httpserver.h +++ b/src/httpserver.h @@ -30,6 +30,8 @@ static const int DEFAULT_HTTP_SERVER_TIMEOUT=30; struct evhttp_request; struct event_base; class CService; + +namespace http_libevent { class HTTPRequest; /** Initialize HTTP server. @@ -48,9 +50,10 @@ void StopHTTPServer(); /** Change logging level for libevent. */ void UpdateHTTPServerLogging(bool enable); +} // namespace http_libevent /** Handler for requests to a certain HTTP path */ -typedef std::function HTTPRequestHandler; +typedef std::function HTTPRequestHandler; /** Register handler for prefix. * If multiple handlers match a prefix, the first-registered one will * be invoked. @@ -64,6 +67,7 @@ void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch); */ struct event_base* EventBase(); +namespace http_libevent { /** In-flight HTTP request. * Thin C++ wrapper around evhttp_request. */ @@ -145,6 +149,7 @@ public: void WriteReply(int nStatus, std::span reply); }; + /** Get the query parameter value from request uri for a specified key, or std::nullopt if the key * is not found. * @@ -158,6 +163,7 @@ public: * @param[in] key represents the query parameter of which the value is returned */ std::optional GetQueryParameterFromUri(const char* uri, const std::string& key); +} // namespace http_libevent /** Event handler closure. */ diff --git a/src/init.cpp b/src/init.cpp index 3cfd301fbab..5ecb3e61c6c 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -121,6 +121,10 @@ using common::AmountErrMsg; using common::InvalidPortErrMsg; using common::ResolveErrMsg; +using http_libevent::InitHTTPServer; +using http_libevent::InterruptHTTPServer; +using http_libevent::StartHTTPServer; +using http_libevent::StopHTTPServer; using node::ApplyArgsManOptions; using node::BlockManager; using node::CalculateCacheSizes; diff --git a/src/rest.cpp b/src/rest.cpp index 44984b360ff..1b2e1e14e18 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -37,6 +37,7 @@ #include +using http_libevent::HTTPRequest; using node::GetTransaction; using node::NodeContext; using util::SplitString; diff --git a/src/rpc/node.cpp b/src/rpc/node.cpp index 5e36273cf49..fb851ee9a07 100644 --- a/src/rpc/node.cpp +++ b/src/rpc/node.cpp @@ -256,7 +256,7 @@ static RPCHelpMan logging() // Update libevent logging if BCLog::LIBEVENT has changed. if (changed_log_categories & BCLog::LIBEVENT) { - UpdateHTTPServerLogging(LogInstance().WillLogCategory(BCLog::LIBEVENT)); + http_libevent::UpdateHTTPServerLogging(LogInstance().WillLogCategory(BCLog::LIBEVENT)); } UniValue result(UniValue::VOBJ); diff --git a/src/test/fuzz/http_request.cpp b/src/test/fuzz/http_request.cpp index f13f1c72a51..712c020eeac 100644 --- a/src/test/fuzz/http_request.cpp +++ b/src/test/fuzz/http_request.cpp @@ -20,6 +20,8 @@ #include #include +using http_libevent::HTTPRequest; + extern "C" int evhttp_parse_firstline_(struct evhttp_request*, struct evbuffer*); extern "C" int evhttp_parse_headers_(struct evhttp_request*, struct evbuffer*); diff --git a/src/test/httpserver_tests.cpp b/src/test/httpserver_tests.cpp index c95a777e80c..6edaf1a7747 100644 --- a/src/test/httpserver_tests.cpp +++ b/src/test/httpserver_tests.cpp @@ -11,6 +11,7 @@ BOOST_FIXTURE_TEST_SUITE(httpserver_tests, BasicTestingSetup) BOOST_AUTO_TEST_CASE(test_query_parameters) { + using http_libevent::GetQueryParameterFromUri; std::string uri {}; // No parameters