Skip to content

Commit 108b19f

Browse files
committed
Merge pull request #5326
5dc713b [REST] set REST API behind "-rest" option (Jonas Schnelli) 78bdc81 [REST] give an appropriate response in warmup phase (Jonas Schnelli) 210eba9 [REST] fix headersonly flag for BINARY responses (Jonas Schnelli)
2 parents 8ba38ab + 5dc713b commit 108b19f

File tree

5 files changed

+20
-3
lines changed

5 files changed

+20
-3
lines changed

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ std::string HelpMessage(HelpMessageMode mode)
352352

353353
strUsage += "\n" + _("RPC server options:") + "\n";
354354
strUsage += " -server " + _("Accept command line and JSON-RPC commands") + "\n";
355+
strUsage += " -rest " + strprintf(_("Accept public REST requests (default: %u)"), 0) + "\n";
355356
strUsage += " -rpcbind=<addr> " + _("Bind to given address to listen for JSON-RPC connections. Use [host]:port notation for IPv6. This option can be specified multiple times (default: bind to all interfaces)") + "\n";
356357
strUsage += " -rpcuser=<user> " + _("Username for JSON-RPC connections") + "\n";
357358
strUsage += " -rpcpassword=<pw> " + _("Password for JSON-RPC connections") + "\n";

src/rest.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static bool rest_block(AcceptedConnection *conn,
100100
switch (rf) {
101101
case RF_BINARY: {
102102
string binaryBlock = ssBlock.str();
103-
conn->stream() << HTTPReply(HTTP_OK, binaryBlock, fRun, true, "application/octet-stream") << binaryBlock << std::flush;
103+
conn->stream() << HTTPReplyHeader(HTTP_OK, fRun, binaryBlock.size(), "application/octet-stream") << binaryBlock << std::flush;
104104
return true;
105105
}
106106

@@ -148,7 +148,7 @@ static bool rest_tx(AcceptedConnection *conn,
148148
switch (rf) {
149149
case RF_BINARY: {
150150
string binaryTx = ssTx.str();
151-
conn->stream() << HTTPReply(HTTP_OK, binaryTx, fRun, true, "application/octet-stream") << binaryTx << std::flush;
151+
conn->stream() << HTTPReplyHeader(HTTP_OK, fRun, binaryTx.size(), "application/octet-stream") << binaryTx << std::flush;
152152
return true;
153153
}
154154

@@ -188,6 +188,10 @@ bool HTTPReq_REST(AcceptedConnection *conn,
188188
bool fRun)
189189
{
190190
try {
191+
std::string statusmessage;
192+
if(RPCIsInWarmup(&statusmessage))
193+
throw RESTERR(HTTP_SERVICE_UNAVAILABLE, "Service temporarily unavailable: "+statusmessage);
194+
191195
for (unsigned int i = 0; i < ARRAYLEN(uri_prefixes); i++) {
192196
unsigned int plen = strlen(uri_prefixes[i].prefix);
193197
if (strURI.substr(0, plen) == uri_prefixes[i].prefix) {

src/rpcprotocol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum HTTPStatusCode
2828
HTTP_FORBIDDEN = 403,
2929
HTTP_NOT_FOUND = 404,
3030
HTTP_INTERNAL_SERVER_ERROR = 500,
31+
HTTP_SERVICE_UNAVAILABLE = 503,
3132
};
3233

3334
//! Bitcoin RPC error codes

src/rpcserver.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,14 @@ void SetRPCWarmupFinished()
756756
fRPCInWarmup = false;
757757
}
758758

759+
bool RPCIsInWarmup(std::string *outStatus)
760+
{
761+
LOCK(cs_rpcWarmup);
762+
if (outStatus)
763+
*outStatus = rpcWarmupStatus;
764+
return fRPCInWarmup;
765+
}
766+
759767
void RPCRunHandler(const boost::system::error_code& err, boost::function<void(void)> func)
760768
{
761769
if (!err)
@@ -947,7 +955,7 @@ void ServiceConnection(AcceptedConnection *conn)
947955
break;
948956

949957
// Process via HTTP REST API
950-
} else if (strURI.substr(0, 6) == "/rest/") {
958+
} else if (strURI.substr(0, 6) == "/rest/" && GetBoolArg("-rest", false)) {
951959
if (!HTTPReq_REST(conn, strURI, mapHeaders, fRun))
952960
break;
953961

src/rpcserver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ void SetRPCWarmupStatus(const std::string& newStatus);
5353
/* Mark warmup as done. RPC calls will be processed from now on. */
5454
void SetRPCWarmupFinished();
5555

56+
/* returns the current warmup state. */
57+
bool RPCIsInWarmup(std::string *statusOut);
58+
5659
/**
5760
* Type-check arguments; throws JSONRPCError if wrong type given. Does not check that
5861
* the right number of arguments are passed, just that any passed are the correct type.

0 commit comments

Comments
 (0)