diff --git a/src/httprpc.cpp b/src/httprpc.cpp index 9a6b8eb0b2e..2fc7f093f5c 100644 --- a/src/httprpc.cpp +++ b/src/httprpc.cpp @@ -26,6 +26,8 @@ #include #include +using http_libevent::EventBase; +using http_libevent::HTTPRequest; using util::SplitString; using util::TrimStringView; diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 2e42eb0bd1d..367e29ca639 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -46,6 +46,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; @@ -381,6 +382,7 @@ static void libevent_log_cb(int severity, const char *msg) } } +namespace http_libevent { bool InitHTTPServer(const util::SignalInterrupt& interrupt) { if (!InitHTTPAllowList()) @@ -497,6 +499,7 @@ struct event_base* EventBase() { return eventBase; } +} // namespace http_libevent static void httpevent_callback_fn(evutil_socket_t, short, void* data) { @@ -524,6 +527,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) { @@ -686,6 +691,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 76381a382b3..5d6ea3c8a54 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. @@ -59,6 +62,7 @@ void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPR /** Unregister handler for prefix */ void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch); +namespace http_libevent { /** Return evhttp event base. This can be used by submodules to * queue timers or custom events. */ @@ -158,6 +162,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 class. This can be used either as a cross-thread trigger or as a timer. */ diff --git a/src/init.cpp b/src/init.cpp index d2e2902040f..320940e4e84 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -145,6 +145,10 @@ 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 d2c5a9b4899..5321cbee104 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 74d950ca707..a43bf04deff 100644 --- a/src/rpc/node.cpp +++ b/src/rpc/node.cpp @@ -264,7 +264,7 @@ static RPCMethod 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 9241060b931..f64fb972071 100644 --- a/src/test/fuzz/http_request.cpp +++ b/src/test/fuzz/http_request.cpp @@ -22,11 +22,12 @@ extern "C" int evhttp_parse_firstline_(struct evhttp_request*, struct evbuffer*); extern "C" int evhttp_parse_headers_(struct evhttp_request*, struct evbuffer*); - -std::string RequestMethodString(HTTPRequest::RequestMethod m); - FUZZ_TARGET(http_request) { + using http_libevent::HTTPRequest; + + std::string RequestMethodString(HTTPRequest::RequestMethod m); + FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; evhttp_request* evreq = evhttp_request_new(nullptr, nullptr); assert(evreq != nullptr); diff --git a/src/test/httpserver_tests.cpp b/src/test/httpserver_tests.cpp index 030d48db4ab..75c25cafff2 100644 --- a/src/test/httpserver_tests.cpp +++ b/src/test/httpserver_tests.cpp @@ -12,6 +12,7 @@ BOOST_FIXTURE_TEST_SUITE(httpserver_tests, BasicTestingSetup) BOOST_AUTO_TEST_CASE(test_query_parameters) { + using http_libevent::GetQueryParameterFromUri; std::string uri {}; // No parameters