From 5e0d7a286a49d14018068bd413833ffaef6af37e Mon Sep 17 00:00:00 2001 From: Hodlinator <172445034+hodlinator@users.noreply.github.com> Date: Mon, 10 Aug 2026 12:30:39 +0200 Subject: [PATCH] refactor: Drastically narrow scope of http_bitcoin namespace and rename it to bitcoin_http http_bitcoin was mostly used during #35182 to distinguish from http_libevent counterpart: - The http_libevent namespace was introduced around the legacy code in 89c54ae4cbc8e58921551d5f1a90eb4683106ccb. - The http_bitcoin namespace was introduced in 68b5d289d19c42de9bebf54a0555053d29721111 and extended in subsequent commits. - The http_libevent namespace together with code it contained was removed in 8c1eea0777c586ce58a500bbea509cd39e4f3507. bitcoin_http is a better name as it is Bitcoin Core's implementation of the HTTP protocol, not HTTP protocol's implementation of bitcoin 402 payment required codes or anything like that. The namespace only remains for a few constants and a type which don't have HTTP in their names. --- src/httprpc.cpp | 1 - src/httpserver.cpp | 11 ++++------- src/httpserver.h | 19 ++++++++----------- src/init.cpp | 4 ---- src/rest.cpp | 1 - src/test/fuzz/http_request.cpp | 7 +++---- src/test/httpserver_tests.cpp | 13 +++---------- 7 files changed, 18 insertions(+), 38 deletions(-) diff --git a/src/httprpc.cpp b/src/httprpc.cpp index f9b95dd8361..0981de5f97b 100644 --- a/src/httprpc.cpp +++ b/src/httprpc.cpp @@ -26,7 +26,6 @@ #include #include -using http_bitcoin::HTTPRequest; using util::SplitString; using util::TrimStringView; diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 04839f1406c..0d93fd34cb4 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -50,7 +50,8 @@ static constexpr auto SELECT_TIMEOUT{50ms}; static constexpr int SOCKET_OPTION_TRUE{1}; using common::InvalidPortErrMsg; -using http_bitcoin::HTTPRequest; +using util::LineReader; +using namespace bitcoin_http; struct HTTPPathHandler { @@ -65,7 +66,7 @@ struct HTTPPathHandler /** HTTP module state */ -static std::unique_ptr g_http_server{nullptr}; +static std::unique_ptr g_http_server{nullptr}; //! Handlers for (sub)paths static GlobalMutex g_httppathhandlers_mutex; static std::vector pathHandlers GUARDED_BY(g_httppathhandlers_mutex); @@ -74,7 +75,6 @@ static std::vector pathHandlers GUARDED_BY(g_httppathhandlers_m static ThreadPool g_threadpool_http("http"); static int g_max_queue_depth{100}; -namespace http_bitcoin { /** Check if a network address is allowed to access the HTTP server */ bool HTTPServer::ClientAllowed(const CNetAddr& netaddr) const { @@ -112,7 +112,6 @@ bool HTTPServer::InitHTTPAllowList() LogDebug(BCLog::HTTP, "Allowing HTTP connections from: %s\n", strAllowed); return true; } -} // namespace http_bitcoin /** HTTP request method as string - use for logging only */ std::string_view RequestMethodString(HTTPRequestMethod m) @@ -200,7 +199,7 @@ static void MaybeDispatchRequestToWorker(std::shared_ptr hreq) } } -static void RejectRequest(std::unique_ptr hreq) +static void RejectRequest(std::unique_ptr hreq) { LogDebug(BCLog::HTTP, "Rejecting request while shutting down"); WriteNoStoreErrorReply(*hreq, HTTP_SERVICE_UNAVAILABLE); @@ -261,7 +260,6 @@ void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch) } } -namespace http_bitcoin { using util::Split; std::optional HTTPHeaders::FindFirst(const std::string_view key) const @@ -1388,4 +1386,3 @@ void StopHTTPServer() } LogDebug(BCLog::HTTP, "Stopped HTTP server"); } -} // namespace http_bitcoin diff --git a/src/httpserver.h b/src/httpserver.h index 79e530dc914..9a41838102f 100644 --- a/src/httpserver.h +++ b/src/httpserver.h @@ -49,11 +49,10 @@ enum class HTTPRequestMethod { PUT }; -namespace http_bitcoin { - class HTTPRequest; -} +class HTTPRequest; + /** Handler for requests to a certain HTTP path */ -using HTTPRequestHandler = std::function; +using HTTPRequestHandler = std::function; /** Register handler for prefix. * If multiple handlers match a prefix, the first-registered one will @@ -63,9 +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_bitcoin { -using util::LineReader; - +namespace bitcoin_http { //! Shortest valid request line, used by libevent in evhttp_parse_request_line() inline constexpr size_t MIN_REQUEST_LINE_LENGTH = std::string_view("GET / HTTP/1.0").size(); @@ -83,6 +80,7 @@ inline constexpr uint64_t MAX_BODY_SIZE{32_MiB}; struct ContentTooLargeError : std::runtime_error { using std::runtime_error::runtime_error; }; +} // namespace bitcoin_http class HTTPHeaders { @@ -163,9 +161,9 @@ public: * @throws std::runtime_error if data is invalid. */ /// @{ - bool LoadControlData(LineReader& reader); - bool LoadHeaders(LineReader& reader); - bool LoadBody(LineReader& reader); + bool LoadControlData(util::LineReader& reader); + bool LoadHeaders(util::LineReader& reader); + bool LoadBody(util::LineReader& reader); /// @} void WriteReply(HTTPStatusCode status, std::span reply_body = {}); @@ -630,6 +628,5 @@ void InterruptHTTPServer(); /** Stop HTTP server */ void StopHTTPServer(); -} // namespace http_bitcoin #endif // BITCOIN_HTTPSERVER_H diff --git a/src/init.cpp b/src/init.cpp index c25e07bf65f..576131c478d 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -145,10 +145,6 @@ using common::InvalidPortErrMsg; using common::ResolveErrMsg; -using http_bitcoin::InitHTTPServer; -using http_bitcoin::InterruptHTTPServer; -using http_bitcoin::StartHTTPServer; -using http_bitcoin::StopHTTPServer; using node::ApplyArgsManOptions; using node::BlockManager; using node::CalculateCacheSizes; diff --git a/src/rest.cpp b/src/rest.cpp index fd40d25b9d2..8004b44d062 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -37,7 +37,6 @@ #include -using http_bitcoin::HTTPRequest; using node::GetTransaction; using node::NodeContext; using util::SplitString; diff --git a/src/test/fuzz/http_request.cpp b/src/test/fuzz/http_request.cpp index 3ae5355c5ef..c24413add07 100644 --- a/src/test/fuzz/http_request.cpp +++ b/src/test/fuzz/http_request.cpp @@ -20,9 +20,8 @@ std::string_view RequestMethodString(HTTPRequestMethod m); FUZZ_TARGET(http_request) { - using http_bitcoin::HTTPRequest; - using http_bitcoin::MAX_HEADERS_SIZE; using util::LineReader; + using namespace bitcoin_http; FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; const std::string http_buffer{fuzzed_data_provider.ConsumeRandomLengthString(4096)}; @@ -47,7 +46,7 @@ FUZZ_TARGET(http_request) (void)http_request.GetHeader(header); // Reaching here means LoadControlData/LoadHeaders/LoadBody all succeeded, so the // parsed body must be consistent with the message framing. Before libevent was - // replaced with http_bitcoin::HTTPRequest (#35182), ReadBody() always returned an + // replaced with HTTPRequest (#35182), ReadBody() always returned an // empty string here; LoadBody now populates the body per RFC 9112 framing, so mirror // its branch logic to assert the body matches the framing that produced it. const std::string body = http_request.ReadBody(); @@ -55,7 +54,7 @@ FUZZ_TARGET(http_request) const auto content_length = http_request.GetHeader("Content-Length"); if (transfer_encoding && ToLower(*transfer_encoding) == "chunked") { // A chunked body is the concatenation of the decoded chunks, bounded by MAX_BODY_SIZE. - assert(body.size() <= http_bitcoin::MAX_BODY_SIZE); + assert(body.size() <= MAX_BODY_SIZE); } else if (content_length) { // A Content-Length body is exactly that many bytes. const auto parsed_length{ToIntegral(*content_length)}; diff --git a/src/test/httpserver_tests.cpp b/src/test/httpserver_tests.cpp index c5c5c218c0e..f853a2d01c6 100644 --- a/src/test/httpserver_tests.cpp +++ b/src/test/httpserver_tests.cpp @@ -12,15 +12,8 @@ #include -using http_bitcoin::GetQueryParameterFromUri; -using http_bitcoin::HTTPHeaders; -using http_bitcoin::HTTPRemoteClient; -using http_bitcoin::HTTPRequest; -using http_bitcoin::HTTPResponse; -using http_bitcoin::HTTPServer; -using http_bitcoin::MAX_BODY_SIZE; -using http_bitcoin::MAX_HEADERS_SIZE; using util::LineReader; +using namespace bitcoin_http; // HTTP request captured from bitcoin-cli constexpr std::string_view full_request = "POST / HTTP/1.1\r\n" @@ -369,7 +362,7 @@ BOOST_AUTO_TEST_CASE(http_request_tests) LineReader reader(request, MAX_HEADERS_SIZE); BOOST_CHECK(req.LoadControlData(reader)); BOOST_CHECK(req.LoadHeaders(reader)); - BOOST_CHECK_EXCEPTION(req.LoadBody(reader), http_bitcoin::ContentTooLargeError, HasReason{"Max body size exceeded"}); + BOOST_CHECK_EXCEPTION(req.LoadBody(reader), ContentTooLargeError, HasReason{"Max body size exceeded"}); } { // Content-Length exactly on the limit @@ -423,7 +416,7 @@ BOOST_AUTO_TEST_CASE(http_request_tests) LineReader reader(excessive_chunk_size, MAX_HEADERS_SIZE); BOOST_CHECK(req.LoadControlData(reader)); BOOST_CHECK(req.LoadHeaders(reader)); - BOOST_CHECK_EXCEPTION(req.LoadBody(reader), http_bitcoin::ContentTooLargeError, HasReason{"Chunk will exceed max body size"}); + BOOST_CHECK_EXCEPTION(req.LoadBody(reader), ContentTooLargeError, HasReason{"Chunk will exceed max body size"}); } { // Allow (but ignore) Chunk Extensions