Skip to content

Commit 5c5e32b

Browse files
committed
rpc: migrate JSONRPCRequest functionality into request.cpp
1 parent 0ab8ba1 commit 5c5e32b

File tree

10 files changed

+89
-79
lines changed

10 files changed

+89
-79
lines changed

src/Makefile.am

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,10 @@ BITCOIN_CORE_H = \
175175
rpc/blockchain.h \
176176
rpc/client.h \
177177
rpc/protocol.h \
178-
rpc/server.h \
179178
rpc/rawtransaction_util.h \
180179
rpc/register.h \
180+
rpc/request.h \
181+
rpc/server.h \
181182
rpc/util.h \
182183
scheduler.h \
183184
script/descriptor.h \
@@ -481,7 +482,7 @@ libbitcoin_util_a_SOURCES = \
481482
interfaces/handler.cpp \
482483
logging.cpp \
483484
random.cpp \
484-
rpc/protocol.cpp \
485+
rpc/request.cpp \
485486
support/cleanse.cpp \
486487
sync.cpp \
487488
threadinterrupt.cpp \

src/bitcoin-cli.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <fs.h>
1313
#include <rpc/client.h>
1414
#include <rpc/protocol.h>
15+
#include <rpc/request.h>
1516
#include <util/system.h>
1617
#include <util/strencodings.h>
1718

src/rest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <primitives/block.h>
1313
#include <primitives/transaction.h>
1414
#include <rpc/blockchain.h>
15+
#include <rpc/protocol.h>
1516
#include <rpc/server.h>
1617
#include <streams.h>
1718
#include <sync.h>

src/rpc/protocol.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@
66
#ifndef BITCOIN_RPC_PROTOCOL_H
77
#define BITCOIN_RPC_PROTOCOL_H
88

9-
#include <fs.h>
10-
11-
#include <list>
12-
#include <map>
13-
#include <stdint.h>
14-
#include <string>
15-
16-
#include <univalue.h>
17-
189
//! HTTP status codes
1910
enum HTTPStatusCode
2011
{
@@ -92,18 +83,4 @@ enum RPCErrorCode
9283
RPC_FORBIDDEN_BY_SAFE_MODE = -2, //!< Server is in safe mode, and command is not allowed in safe mode
9384
};
9485

95-
UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id);
96-
UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id);
97-
std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id);
98-
UniValue JSONRPCError(int code, const std::string& message);
99-
100-
/** Generate a new RPC authentication cookie and write it to disk */
101-
bool GenerateAuthCookie(std::string *cookie_out);
102-
/** Read the RPC authentication cookie from disk */
103-
bool GetAuthCookie(std::string *cookie_out);
104-
/** Delete RPC authentication cookie from disk */
105-
void DeleteAuthCookie();
106-
/** Parse JSON-RPC batch reply into a vector */
107-
std::vector<UniValue> JSONRPCProcessBatchReply(const UniValue &in, size_t num);
108-
10986
#endif // BITCOIN_RPC_PROTOCOL_H

src/rpc/rawtransaction_util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <keystore.h>
1212
#include <policy/policy.h>
1313
#include <primitives/transaction.h>
14-
#include <rpc/protocol.h>
14+
#include <rpc/request.h>
1515
#include <rpc/util.h>
1616
#include <tinyformat.h>
1717
#include <univalue.h>

src/rpc/protocol.cpp renamed to src/rpc/request.cpp

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// Copyright (c) 2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2018 The Bitcoin Core developers
2+
// Copyright (c) 2009-2019 The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6-
#include <rpc/protocol.h>
6+
#include <rpc/request.h>
7+
8+
#include <fs.h>
79

810
#include <random.h>
9-
#include <tinyformat.h>
11+
#include <rpc/protocol.h>
1012
#include <util/system.h>
1113
#include <util/strencodings.h>
12-
#include <util/time.h>
1314

1415
/**
1516
* JSON-RPC protocol. Bitcoin speaks version 1.0 for maximum compatibility,
@@ -148,3 +149,36 @@ std::vector<UniValue> JSONRPCProcessBatchReply(const UniValue &in, size_t num)
148149
}
149150
return batch;
150151
}
152+
153+
void JSONRPCRequest::parse(const UniValue& valRequest)
154+
{
155+
// Parse request
156+
if (!valRequest.isObject())
157+
throw JSONRPCError(RPC_INVALID_REQUEST, "Invalid Request object");
158+
const UniValue& request = valRequest.get_obj();
159+
160+
// Parse id now so errors from here on will have the id
161+
id = find_value(request, "id");
162+
163+
// Parse method
164+
UniValue valMethod = find_value(request, "method");
165+
if (valMethod.isNull())
166+
throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method");
167+
if (!valMethod.isStr())
168+
throw JSONRPCError(RPC_INVALID_REQUEST, "Method must be a string");
169+
strMethod = valMethod.get_str();
170+
if (fLogIPs)
171+
LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s peeraddr=%s\n", SanitizeString(strMethod),
172+
this->authUser, this->peerAddr);
173+
else
174+
LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s\n", SanitizeString(strMethod), this->authUser);
175+
176+
// Parse params
177+
UniValue valParams = find_value(request, "params");
178+
if (valParams.isArray() || valParams.isObject())
179+
params = valParams;
180+
else if (valParams.isNull())
181+
params = UniValue(UniValue::VARR);
182+
else
183+
throw JSONRPCError(RPC_INVALID_REQUEST, "Params must be an array or object");
184+
}

src/rpc/request.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2019 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#ifndef BITCOIN_RPC_REQUEST_H
7+
#define BITCOIN_RPC_REQUEST_H
8+
9+
#include <string>
10+
11+
#include <univalue.h>
12+
13+
UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id);
14+
UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id);
15+
std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id);
16+
UniValue JSONRPCError(int code, const std::string& message);
17+
18+
/** Generate a new RPC authentication cookie and write it to disk */
19+
bool GenerateAuthCookie(std::string *cookie_out);
20+
/** Read the RPC authentication cookie from disk */
21+
bool GetAuthCookie(std::string *cookie_out);
22+
/** Delete RPC authentication cookie from disk */
23+
void DeleteAuthCookie();
24+
/** Parse JSON-RPC batch reply into a vector */
25+
std::vector<UniValue> JSONRPCProcessBatchReply(const UniValue &in, size_t num);
26+
27+
class JSONRPCRequest
28+
{
29+
public:
30+
UniValue id;
31+
std::string strMethod;
32+
UniValue params;
33+
bool fHelp;
34+
std::string URI;
35+
std::string authUser;
36+
std::string peerAddr;
37+
38+
JSONRPCRequest() : id(NullUniValue), params(NullUniValue), fHelp(false) {}
39+
void parse(const UniValue& valRequest);
40+
};
41+
42+
#endif // BITCOIN_RPC_REQUEST_H

src/rpc/server.cpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -329,39 +329,6 @@ bool RPCIsInWarmup(std::string *outStatus)
329329
return fRPCInWarmup;
330330
}
331331

332-
void JSONRPCRequest::parse(const UniValue& valRequest)
333-
{
334-
// Parse request
335-
if (!valRequest.isObject())
336-
throw JSONRPCError(RPC_INVALID_REQUEST, "Invalid Request object");
337-
const UniValue& request = valRequest.get_obj();
338-
339-
// Parse id now so errors from here on will have the id
340-
id = find_value(request, "id");
341-
342-
// Parse method
343-
UniValue valMethod = find_value(request, "method");
344-
if (valMethod.isNull())
345-
throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method");
346-
if (!valMethod.isStr())
347-
throw JSONRPCError(RPC_INVALID_REQUEST, "Method must be a string");
348-
strMethod = valMethod.get_str();
349-
if (fLogIPs)
350-
LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s peeraddr=%s\n", SanitizeString(strMethod),
351-
this->authUser, this->peerAddr);
352-
else
353-
LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s\n", SanitizeString(strMethod), this->authUser);
354-
355-
// Parse params
356-
UniValue valParams = find_value(request, "params");
357-
if (valParams.isArray() || valParams.isObject())
358-
params = valParams;
359-
else if (valParams.isNull())
360-
params = UniValue(UniValue::VARR);
361-
else
362-
throw JSONRPCError(RPC_INVALID_REQUEST, "Params must be an array or object");
363-
}
364-
365332
bool IsDeprecatedRPCEnabled(const std::string& method)
366333
{
367334
const std::vector<std::string> enabled_methods = gArgs.GetArgs("-deprecatedrpc");

src/rpc/server.h

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
#define BITCOIN_RPC_SERVER_H
88

99
#include <amount.h>
10-
#include <rpc/protocol.h>
10+
#include <rpc/request.h>
1111
#include <uint256.h>
1212

1313
#include <list>
1414
#include <map>
1515
#include <stdint.h>
1616
#include <string>
17+
#include <functional>
1718

1819
#include <univalue.h>
1920

@@ -27,21 +28,6 @@ namespace RPCServer
2728
void OnStopped(std::function<void ()> slot);
2829
}
2930

30-
class JSONRPCRequest
31-
{
32-
public:
33-
UniValue id;
34-
std::string strMethod;
35-
UniValue params;
36-
bool fHelp;
37-
std::string URI;
38-
std::string authUser;
39-
std::string peerAddr;
40-
41-
JSONRPCRequest() : id(NullUniValue), params(NullUniValue), fHelp(false) {}
42-
void parse(const UniValue& valRequest);
43-
};
44-
4531
/** Query whether RPC is running */
4632
bool IsRPCRunning();
4733

src/rpc/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <outputtype.h>
1010
#include <pubkey.h>
1111
#include <rpc/protocol.h>
12+
#include <rpc/request.h>
1213
#include <script/script.h>
1314
#include <script/sign.h>
1415
#include <script/standard.h>

0 commit comments

Comments
 (0)