diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2014-11-26 14:20:17 +0100 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2014-11-26 14:30:11 +0100 |
commit | 108b19f7ef7f9d0bf6c6794bf2be9a12b4cb6400 (patch) | |
tree | b52b1176ec950b5f798c2f6e0f31b86bd832c4ff | |
parent | 8ba38aba42c3b65f1ad61699197566d99d2b761a (diff) | |
parent | 5dc713bfc7bf98c8e1d80fec9c5f5e0417a2bdcd (diff) |
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)
-rw-r--r-- | src/init.cpp | 1 | ||||
-rw-r--r-- | src/rest.cpp | 8 | ||||
-rw-r--r-- | src/rpcprotocol.h | 1 | ||||
-rw-r--r-- | src/rpcserver.cpp | 10 | ||||
-rw-r--r-- | src/rpcserver.h | 3 |
5 files changed, 20 insertions, 3 deletions
diff --git a/src/init.cpp b/src/init.cpp index b73c6e8722..63e72c66d2 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -352,6 +352,7 @@ std::string HelpMessage(HelpMessageMode mode) strUsage += "\n" + _("RPC server options:") + "\n"; strUsage += " -server " + _("Accept command line and JSON-RPC commands") + "\n"; + strUsage += " -rest " + strprintf(_("Accept public REST requests (default: %u)"), 0) + "\n"; 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"; strUsage += " -rpcuser=<user> " + _("Username for JSON-RPC connections") + "\n"; strUsage += " -rpcpassword=<pw> " + _("Password for JSON-RPC connections") + "\n"; diff --git a/src/rest.cpp b/src/rest.cpp index 122b361719..4953d7e717 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -100,7 +100,7 @@ static bool rest_block(AcceptedConnection *conn, switch (rf) { case RF_BINARY: { string binaryBlock = ssBlock.str(); - conn->stream() << HTTPReply(HTTP_OK, binaryBlock, fRun, true, "application/octet-stream") << binaryBlock << std::flush; + conn->stream() << HTTPReplyHeader(HTTP_OK, fRun, binaryBlock.size(), "application/octet-stream") << binaryBlock << std::flush; return true; } @@ -148,7 +148,7 @@ static bool rest_tx(AcceptedConnection *conn, switch (rf) { case RF_BINARY: { string binaryTx = ssTx.str(); - conn->stream() << HTTPReply(HTTP_OK, binaryTx, fRun, true, "application/octet-stream") << binaryTx << std::flush; + conn->stream() << HTTPReplyHeader(HTTP_OK, fRun, binaryTx.size(), "application/octet-stream") << binaryTx << std::flush; return true; } @@ -188,6 +188,10 @@ bool HTTPReq_REST(AcceptedConnection *conn, bool fRun) { try { + std::string statusmessage; + if(RPCIsInWarmup(&statusmessage)) + throw RESTERR(HTTP_SERVICE_UNAVAILABLE, "Service temporarily unavailable: "+statusmessage); + for (unsigned int i = 0; i < ARRAYLEN(uri_prefixes); i++) { unsigned int plen = strlen(uri_prefixes[i].prefix); if (strURI.substr(0, plen) == uri_prefixes[i].prefix) { diff --git a/src/rpcprotocol.h b/src/rpcprotocol.h index a321338176..f7cd50f9f6 100644 --- a/src/rpcprotocol.h +++ b/src/rpcprotocol.h @@ -28,6 +28,7 @@ enum HTTPStatusCode HTTP_FORBIDDEN = 403, HTTP_NOT_FOUND = 404, HTTP_INTERNAL_SERVER_ERROR = 500, + HTTP_SERVICE_UNAVAILABLE = 503, }; //! Bitcoin RPC error codes diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 7022c50375..b03016a508 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -756,6 +756,14 @@ void SetRPCWarmupFinished() fRPCInWarmup = false; } +bool RPCIsInWarmup(std::string *outStatus) +{ + LOCK(cs_rpcWarmup); + if (outStatus) + *outStatus = rpcWarmupStatus; + return fRPCInWarmup; +} + void RPCRunHandler(const boost::system::error_code& err, boost::function<void(void)> func) { if (!err) @@ -947,7 +955,7 @@ void ServiceConnection(AcceptedConnection *conn) break; // Process via HTTP REST API - } else if (strURI.substr(0, 6) == "/rest/") { + } else if (strURI.substr(0, 6) == "/rest/" && GetBoolArg("-rest", false)) { if (!HTTPReq_REST(conn, strURI, mapHeaders, fRun)) break; diff --git a/src/rpcserver.h b/src/rpcserver.h index 7395fc23c6..b0e437057b 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -53,6 +53,9 @@ void SetRPCWarmupStatus(const std::string& newStatus); /* Mark warmup as done. RPC calls will be processed from now on. */ void SetRPCWarmupFinished(); +/* returns the current warmup state. */ +bool RPCIsInWarmup(std::string *statusOut); + /** * Type-check arguments; throws JSONRPCError if wrong type given. Does not check that * the right number of arguments are passed, just that any passed are the correct type. |