Skip to content

Commit 42e5829

Browse files
committed
refactor: Remove call to ShutdownRequested from HTTPRequest
Pass HTTP server an interrupt object instead of having it depend on shutdown.h and global shutdown state. There is no change in behavior in this commit.
1 parent 73133c3 commit 42e5829

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

src/httpserver.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
#include <netbase.h>
1616
#include <node/interface_ui.h>
1717
#include <rpc/protocol.h> // For HTTP status codes
18-
#include <shutdown.h>
1918
#include <sync.h>
2019
#include <util/check.h>
20+
#include <util/signalinterrupt.h>
2121
#include <util/strencodings.h>
2222
#include <util/threadnames.h>
2323
#include <util/translation.h>
@@ -284,7 +284,7 @@ static void http_request_cb(struct evhttp_request* req, void* arg)
284284
}
285285
}
286286
}
287-
std::unique_ptr<HTTPRequest> hreq(new HTTPRequest(req));
287+
auto hreq{std::make_unique<HTTPRequest>(req, *static_cast<const util::SignalInterrupt*>(arg))};
288288

289289
// Early address-based allow check
290290
if (!ClientAllowed(hreq->GetPeer())) {
@@ -425,7 +425,7 @@ static void libevent_log_cb(int severity, const char *msg)
425425
LogPrintLevel(BCLog::LIBEVENT, level, "%s\n", msg);
426426
}
427427

428-
bool InitHTTPServer()
428+
bool InitHTTPServer(const util::SignalInterrupt& interrupt)
429429
{
430430
if (!InitHTTPAllowList())
431431
return false;
@@ -454,7 +454,7 @@ bool InitHTTPServer()
454454
evhttp_set_timeout(http, gArgs.GetIntArg("-rpcservertimeout", DEFAULT_HTTP_SERVER_TIMEOUT));
455455
evhttp_set_max_headers_size(http, MAX_HEADERS_SIZE);
456456
evhttp_set_max_body_size(http, MAX_SIZE);
457-
evhttp_set_gencb(http, http_request_cb, nullptr);
457+
evhttp_set_gencb(http, http_request_cb, (void*)&interrupt);
458458

459459
if (!HTTPBindAddresses(http)) {
460460
LogPrintf("Unable to bind any endpoint for RPC server\n");
@@ -579,7 +579,8 @@ void HTTPEvent::trigger(struct timeval* tv)
579579
else
580580
evtimer_add(ev, tv); // trigger after timeval passed
581581
}
582-
HTTPRequest::HTTPRequest(struct evhttp_request* _req, bool _replySent) : req(_req), replySent(_replySent)
582+
HTTPRequest::HTTPRequest(struct evhttp_request* _req, const util::SignalInterrupt& interrupt, bool _replySent)
583+
: req(_req), m_interrupt(interrupt), replySent(_replySent)
583584
{
584585
}
585586

@@ -639,7 +640,7 @@ void HTTPRequest::WriteHeader(const std::string& hdr, const std::string& value)
639640
void HTTPRequest::WriteReply(int nStatus, const std::string& strReply)
640641
{
641642
assert(!replySent && req);
642-
if (ShutdownRequested()) {
643+
if (m_interrupt) {
643644
WriteHeader("Connection", "close");
644645
}
645646
// Send event to main http thread to send reply message

src/httpserver.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <optional>
1010
#include <string>
1111

12+
namespace util {
13+
class SignalInterrupt;
14+
} // namespace util
15+
1216
static const int DEFAULT_HTTP_THREADS=4;
1317
static const int DEFAULT_HTTP_WORKQUEUE=16;
1418
static const int DEFAULT_HTTP_SERVER_TIMEOUT=30;
@@ -21,7 +25,7 @@ class HTTPRequest;
2125
/** Initialize HTTP server.
2226
* Call this before RegisterHTTPHandler or EventBase().
2327
*/
24-
bool InitHTTPServer();
28+
bool InitHTTPServer(const util::SignalInterrupt& interrupt);
2529
/** Start HTTP server.
2630
* This is separate from InitHTTPServer to give users race-condition-free time
2731
* to register their handlers between InitHTTPServer and StartHTTPServer.
@@ -57,10 +61,11 @@ class HTTPRequest
5761
{
5862
private:
5963
struct evhttp_request* req;
64+
const util::SignalInterrupt& m_interrupt;
6065
bool replySent;
6166

6267
public:
63-
explicit HTTPRequest(struct evhttp_request* req, bool replySent = false);
68+
explicit HTTPRequest(struct evhttp_request* req, const util::SignalInterrupt& interrupt, bool replySent = false);
6469
~HTTPRequest();
6570

6671
enum RequestMethod {

src/init.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,9 @@ static bool AppInitServers(NodeContext& node)
690690
const ArgsManager& args = *Assert(node.args);
691691
RPCServer::OnStarted(&OnRPCStarted);
692692
RPCServer::OnStopped(&OnRPCStopped);
693-
if (!InitHTTPServer())
693+
if (!InitHTTPServer(*Assert(node.shutdown))) {
694694
return false;
695+
}
695696
StartRPC();
696697
node.rpc_interruption_point = RpcInterruptionPoint;
697698
if (!StartHTTPRPC(&node))

src/test/fuzz/http_request.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <test/fuzz/FuzzedDataProvider.h>
88
#include <test/fuzz/fuzz.h>
99
#include <test/fuzz/util.h>
10+
#include <util/signalinterrupt.h>
1011
#include <util/strencodings.h>
1112

1213
#include <event2/buffer.h>
@@ -47,7 +48,8 @@ FUZZ_TARGET(http_request)
4748
return;
4849
}
4950

50-
HTTPRequest http_request{evreq, true};
51+
util::SignalInterrupt interrupt;
52+
HTTPRequest http_request{evreq, interrupt, true};
5153
const HTTPRequest::RequestMethod request_method = http_request.GetRequestMethod();
5254
(void)RequestMethodString(request_method);
5355
(void)http_request.GetURI();

0 commit comments

Comments
 (0)