mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-20 19:15:54 +00:00
Begin to bring some sanity to the RPC code.
This commit is contained in:
114
CallRPC.cpp
114
CallRPC.cpp
@@ -1,25 +1,22 @@
|
|||||||
#include "CallRPC.h"
|
|
||||||
#include "RPC.h"
|
|
||||||
|
|
||||||
#include "Config.h"
|
|
||||||
#include "BitcoinUtil.h"
|
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include <boost/iostreams/concepts.hpp>
|
#include <boost/iostreams/concepts.hpp>
|
||||||
#include <boost/iostreams/stream.hpp>
|
#include <boost/iostreams/stream.hpp>
|
||||||
#include <boost/algorithm/string.hpp>
|
#include <boost/algorithm/string.hpp>
|
||||||
#include <iostream>
|
|
||||||
#include "string.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include <openssl/buffer.h>
|
#include <openssl/buffer.h>
|
||||||
//#include <openssl/ecdsa.h>
|
|
||||||
#include <openssl/evp.h>
|
#include <openssl/evp.h>
|
||||||
//#include <openssl/rand.h>
|
|
||||||
//#include <openssl/sha.h>
|
|
||||||
//#include <openssl/ripemd.h>
|
|
||||||
|
|
||||||
using namespace std;
|
#include "json/value.h"
|
||||||
|
#include "json/reader.h"
|
||||||
|
|
||||||
|
#include "CallRPC.h"
|
||||||
|
#include "RPC.h"
|
||||||
|
#include "Config.h"
|
||||||
|
#include "BitcoinUtil.h"
|
||||||
|
|
||||||
using namespace boost::asio;
|
using namespace boost::asio;
|
||||||
|
|
||||||
inline bool isSwitchChar(char c)
|
inline bool isSwitchChar(char c)
|
||||||
@@ -31,8 +28,8 @@ inline bool isSwitchChar(char c)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
string EncodeBase64(string s)
|
std::string EncodeBase64(std::string s)
|
||||||
{
|
{ // FIXME: This performs terribly
|
||||||
BIO *b64, *bmem;
|
BIO *b64, *bmem;
|
||||||
BUF_MEM *bptr;
|
BUF_MEM *bptr;
|
||||||
|
|
||||||
@@ -44,16 +41,15 @@ string EncodeBase64(string s)
|
|||||||
BIO_flush(b64);
|
BIO_flush(b64);
|
||||||
BIO_get_mem_ptr(b64, &bptr);
|
BIO_get_mem_ptr(b64, &bptr);
|
||||||
|
|
||||||
string result(bptr->data, bptr->length);
|
std::string result(bptr->data, bptr->length);
|
||||||
BIO_free_all(b64);
|
BIO_free_all(b64);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
|
||||||
int commandLineRPC(int argc, char *argv[])
|
int commandLineRPC(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
string strPrint;
|
std::string strPrint;
|
||||||
int nRet = 0;
|
int nRet = 0;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -66,106 +62,106 @@ int commandLineRPC(int argc, char *argv[])
|
|||||||
|
|
||||||
if(argc < 2) return(0);
|
if(argc < 2) return(0);
|
||||||
|
|
||||||
string strMethod = argv[1];
|
std::string strMethod = argv[1];
|
||||||
|
|
||||||
// Parameters default to strings
|
// Parameters default to strings
|
||||||
json_spirit::Array params;
|
Json::Value params(Json::arrayValue);
|
||||||
for (int i = 2; i < argc; i++)
|
for (int i = 2; i < argc; i++)
|
||||||
params.push_back(argv[i]);
|
params.append(argv[i]);
|
||||||
|
|
||||||
// Execute
|
// Execute
|
||||||
json_spirit::Object reply = callRPC(strMethod, params);
|
Json::Value reply = callRPC(strMethod, params);
|
||||||
|
|
||||||
// Parse reply
|
// Parse reply
|
||||||
const json_spirit::Value& result = find_value(reply, "result");
|
Json::Value result=reply.get("result", Json::Value());
|
||||||
const json_spirit::Value& error = find_value(reply, "error");
|
Json::Value error=reply.get("error", Json::Value());
|
||||||
|
|
||||||
if(error.type() != json_spirit::null_type)
|
if(!error.isNull())
|
||||||
{
|
{
|
||||||
// Error
|
// Error
|
||||||
strPrint = "error: " + write_string(error, false);
|
strPrint = "error: " + error.toStyledString();
|
||||||
int code = find_value(error.get_obj(), "code").get_int();
|
int code = error["code"].asInt();
|
||||||
nRet = abs(code);
|
nRet = abs(code);
|
||||||
}else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
// Result
|
// Result
|
||||||
if (result.type() == json_spirit::null_type)
|
if (result.isNull())
|
||||||
strPrint = "";
|
strPrint = "";
|
||||||
else if (result.type() == json_spirit::str_type)
|
else if (result.isString())
|
||||||
strPrint = result.get_str();
|
strPrint = result.asString();
|
||||||
else
|
else
|
||||||
strPrint = write_string(result, true);
|
strPrint = result.toStyledString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (std::exception& e)
|
catch (std::exception& e)
|
||||||
{
|
{
|
||||||
strPrint = string("error: ") + e.what();
|
strPrint = std::string("error: ") + e.what();
|
||||||
nRet = 87;
|
nRet = 87;
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
cout << "Exception CommandLineRPC()" << endl;
|
std::cout << "Exception CommandLineRPC()" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(strPrint != "")
|
if(strPrint != "")
|
||||||
{
|
{
|
||||||
cout << strPrint << endl;
|
std::cout << strPrint << std::endl;
|
||||||
}
|
}
|
||||||
return nRet;
|
return nRet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
json_spirit::Object callRPC(const string& strMethod, const json_spirit::Array& params)
|
Json::Value callRPC(const std::string& strMethod, const Json::Value& params)
|
||||||
{
|
{
|
||||||
if(theConfig.RPC_USER == "" && theConfig.RPC_PASSWORD == "")
|
if(theConfig.RPC_USER == "" && theConfig.RPC_PASSWORD == "")
|
||||||
throw runtime_error("You must set rpcpassword=<password> in the configuration file"
|
throw std::runtime_error("You must set rpcpassword=<password> in the configuration file"
|
||||||
"If the file does not exist, create it with owner-readable-only file permissions.");
|
"If the file does not exist, create it with owner-readable-only file permissions.");
|
||||||
|
|
||||||
// Connect to localhost
|
// Connect to localhost
|
||||||
|
|
||||||
cout << "Connecting to port:" << theConfig.RPC_PORT << endl;
|
std::cout << "Connecting to port:" << theConfig.RPC_PORT << std::endl;
|
||||||
ip::tcp::endpoint endpoint( ip::address::from_string("127.0.0.1"), theConfig.RPC_PORT);
|
ip::tcp::endpoint endpoint( ip::address::from_string("127.0.0.1"), theConfig.RPC_PORT);
|
||||||
ip::tcp::iostream stream;
|
ip::tcp::iostream stream;
|
||||||
stream.connect(endpoint);
|
stream.connect(endpoint);
|
||||||
if(stream.fail())
|
if(stream.fail())
|
||||||
throw runtime_error("couldn't connect to server");
|
throw std::runtime_error("couldn't connect to server");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// HTTP basic authentication
|
// HTTP basic authentication
|
||||||
string strUserPass64 = EncodeBase64(theConfig.RPC_USER + ":" + theConfig.RPC_PASSWORD);
|
std::string strUserPass64 = EncodeBase64(theConfig.RPC_USER + ":" + theConfig.RPC_PASSWORD);
|
||||||
map<string, string> mapRequestHeaders;
|
std::map<std::string, std::string> mapRequestHeaders;
|
||||||
mapRequestHeaders["Authorization"] = string("Basic ") + strUserPass64;
|
mapRequestHeaders["Authorization"] = std::string("Basic ") + strUserPass64;
|
||||||
|
|
||||||
|
|
||||||
// Send request
|
// Send request
|
||||||
string strRequest = JSONRPCRequest(strMethod, params, 1);
|
std::string strRequest = JSONRPCRequest(strMethod, params, Json::Value(1));
|
||||||
cout << "send request " << strMethod << " : " << strRequest << endl;
|
std::cout << "send request " << strMethod << " : " << strRequest << std::endl;
|
||||||
string strPost = createHTTPPost(strRequest, mapRequestHeaders);
|
std::string strPost = createHTTPPost(strRequest, mapRequestHeaders);
|
||||||
stream << strPost << std::flush;
|
stream << strPost << std::flush;
|
||||||
|
|
||||||
cout << "post " << strPost << endl;
|
std::cout << "post " << strPost << std::endl;
|
||||||
|
|
||||||
// Receive reply
|
// Receive reply
|
||||||
map<string, string> mapHeaders;
|
std::map<std::string, std::string> mapHeaders;
|
||||||
string strReply;
|
std::string strReply;
|
||||||
int nStatus = ReadHTTP(stream, mapHeaders, strReply);
|
int nStatus = ReadHTTP(stream, mapHeaders, strReply);
|
||||||
if (nStatus == 401)
|
if (nStatus == 401)
|
||||||
throw runtime_error("incorrect rpcuser or rpcpassword (authorization failed)");
|
throw std::runtime_error("incorrect rpcuser or rpcpassword (authorization failed)");
|
||||||
else if (nStatus >= 400 && nStatus != 400 && nStatus != 404 && nStatus != 500)
|
else if (nStatus >= 400 && nStatus != 400 && nStatus != 404 && nStatus != 500)
|
||||||
throw runtime_error(strprintf("server returned HTTP error %d", nStatus));
|
throw std::runtime_error(strprintf("server returned HTTP error %d", nStatus));
|
||||||
else if (strReply.empty())
|
else if (strReply.empty())
|
||||||
throw runtime_error("no response from server");
|
throw std::runtime_error("no response from server");
|
||||||
|
|
||||||
// Parse reply
|
// Parse reply
|
||||||
json_spirit::Value valReply;
|
Json::Reader reader;
|
||||||
if (!json_spirit::read_string(strReply, valReply))
|
Json::Value valReply;
|
||||||
throw runtime_error("couldn't parse reply from server");
|
if (!reader.parse(strReply, valReply))
|
||||||
const json_spirit::Object& reply = valReply.get_obj();
|
throw std::runtime_error("couldn't parse reply from server");
|
||||||
if (reply.empty())
|
if (valReply.isNull())
|
||||||
throw runtime_error("expected reply to have result, error and id properties");
|
throw std::runtime_error("expected reply to have result, error and id properties");
|
||||||
|
|
||||||
return reply;
|
return valReply;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "json/json_spirit_value.h"
|
#include "json/value.h"
|
||||||
|
|
||||||
extern int commandLineRPC(int argc, char *argv[]);
|
extern int commandLineRPC(int argc, char *argv[]);
|
||||||
extern json_spirit::Object callRPC(const std::string& strMethod, const json_spirit::Array& params);
|
extern Json::Value callRPC(const std::string& strMethod, const Json::Value& params);
|
||||||
10
Makefile
10
Makefile
@@ -2,7 +2,7 @@
|
|||||||
# Distributed under the MIT/X11 software license, see the accompanying
|
# Distributed under the MIT/X11 software license, see the accompanying
|
||||||
# file license.txt or http://www.opensource.org/licenses/mit-license.php.
|
# file license.txt or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
CXX=g++ -I/packages/openssl-1.0.0/include
|
CXX=g++ -I/packages/openssl-1.0.0/include -Wall -Wno-sign-compare -Wno-char-subscripts
|
||||||
|
|
||||||
DEFS=
|
DEFS=
|
||||||
|
|
||||||
@@ -72,12 +72,14 @@ SRCS= keystore.cpp BitcoinUtil.cpp \
|
|||||||
Application.cpp TimingService.cpp KnownNodeList.cpp ConnectionPool.cpp Peer.cpp \
|
Application.cpp TimingService.cpp KnownNodeList.cpp ConnectionPool.cpp Peer.cpp \
|
||||||
PeerDoor.cpp RPCDoor.cpp RPCServer.cpp rpc.cpp Conversion.cpp RequestParser.cpp HashedObject.cpp \
|
PeerDoor.cpp RPCDoor.cpp RPCServer.cpp rpc.cpp Conversion.cpp RequestParser.cpp HashedObject.cpp \
|
||||||
UniqueNodeList.cpp PubKeyCache.cpp SHAMapDiff.cpp DeterministicKeys.cpp LedgerMaster.cpp \
|
UniqueNodeList.cpp PubKeyCache.cpp SHAMapDiff.cpp DeterministicKeys.cpp LedgerMaster.cpp \
|
||||||
LedgerHistory.cpp NetworkOPs.cpp
|
LedgerHistory.cpp NetworkOPs.cpp CallRPC.cpp
|
||||||
|
|
||||||
DBSRCS= SqliteDatabase.cpp database.cpp
|
DBSRCS= SqliteDatabase.cpp database.cpp
|
||||||
|
|
||||||
UTILSRCS= pugixml.cpp
|
UTILSRCS= pugixml.cpp
|
||||||
|
|
||||||
|
JSONSRCS= json_reader.cpp json_value.cpp json_writer.cpp
|
||||||
|
|
||||||
# Application.cpp HttpReply.cpp main.cpp RPCCommands.cpp \
|
# Application.cpp HttpReply.cpp main.cpp RPCCommands.cpp \
|
||||||
# BitcoinUtil.cpp keystore.cpp NewcoinAddress.cpp rpc.cpp UniqueNodeList.cpp \
|
# BitcoinUtil.cpp keystore.cpp NewcoinAddress.cpp rpc.cpp UniqueNodeList.cpp \
|
||||||
# CallRPC.cpp KnownNodeList.cpp PackedMessage.cpp RPCDoor.cpp ValidationCollection.cpp \
|
# CallRPC.cpp KnownNodeList.cpp PackedMessage.cpp RPCDoor.cpp ValidationCollection.cpp \
|
||||||
@@ -88,6 +90,7 @@ UTILSRCS= pugixml.cpp
|
|||||||
# database/linux/mysqldatabase.cpp database/database.cpp database/SqliteDatabase.cpp
|
# database/linux/mysqldatabase.cpp database/database.cpp database/SqliteDatabase.cpp
|
||||||
|
|
||||||
OBJS= $(SRCS:%.cpp=%.o) $(DBSRCS:%.cpp=database/%.o) $(UTILSRCS:%.cpp=util/%.o) newcoin.pb.o
|
OBJS= $(SRCS:%.cpp=%.o) $(DBSRCS:%.cpp=database/%.o) $(UTILSRCS:%.cpp=util/%.o) newcoin.pb.o
|
||||||
|
OBJS+= $(JSONSRCS:%.cpp=json/%.o)
|
||||||
#cryptopp/obj/sha.o cryptopp/obj/cpu.o
|
#cryptopp/obj/sha.o cryptopp/obj/cpu.o
|
||||||
|
|
||||||
all: newcoind
|
all: newcoind
|
||||||
@@ -107,8 +110,9 @@ newcoind: $(OBJS)
|
|||||||
|
|
||||||
clean:
|
clean:
|
||||||
-rm -f newcoind
|
-rm -f newcoind
|
||||||
-rm -f *.o
|
-rm -f *.o database/*.o util/*.o
|
||||||
-rm -f headers.h.gch
|
-rm -f headers.h.gch
|
||||||
-rm -f newcoin.pb.*
|
-rm -f newcoin.pb.*
|
||||||
|
-rm -f .dep
|
||||||
|
|
||||||
include .dep
|
include .dep
|
||||||
|
|||||||
24
RPC.h
24
RPC.h
@@ -1,3 +1,7 @@
|
|||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
#include "json/value.h"
|
||||||
|
|
||||||
enum http_status_type
|
enum http_status_type
|
||||||
{
|
{
|
||||||
@@ -19,11 +23,17 @@ enum http_status_type
|
|||||||
service_unavailable = 503
|
service_unavailable = 503
|
||||||
};
|
};
|
||||||
|
|
||||||
#if 0
|
extern std::string JSONRPCRequest(const std::string& strMethod, const Json::Value& params,
|
||||||
extern std::string JSONRPCRequest(const std::string& strMethod, const json_spirit::Array& params, const json_spirit::Value& id);
|
const Json::Value& id);
|
||||||
extern std::string createHTTPPost(const std::string& strMsg, const std::map<std::string,std::string>& mapRequestHeaders);
|
|
||||||
extern int ReadHTTP(std::basic_istream<char>& stream, std::map<std::string, std::string>& mapHeadersRet, std::string& strMessageRet);
|
extern std::string createHTTPPost(const std::string& strMsg,
|
||||||
|
const std::map<std::string,std::string>& mapRequestHeaders);
|
||||||
|
|
||||||
|
extern int ReadHTTP(std::basic_istream<char>& stream,
|
||||||
|
std::map<std::string, std::string>& mapHeadersRet, std::string& strMessageRet);
|
||||||
|
|
||||||
extern std::string HTTPReply(int nStatus, const std::string& strMsg);
|
extern std::string HTTPReply(int nStatus, const std::string& strMsg);
|
||||||
extern std::string JSONRPCReply(const json_spirit::Value& result, const json_spirit::Value& error, const json_spirit::Value& id);
|
|
||||||
extern json_spirit::Object JSONRPCError(int code, const std::string& message);
|
extern std::string JSONRPCReply(const Json::Value& result, const Json::Value& error, const Json::Value& id);
|
||||||
#endif
|
|
||||||
|
extern Json::Value JSONRPCError(int code, const std::string& message);
|
||||||
|
|||||||
151
rpc.cpp
151
rpc.cpp
@@ -1,6 +1,3 @@
|
|||||||
#include "RPC.h"
|
|
||||||
#include "BitcoinUtil.h"
|
|
||||||
#include "Config.h"
|
|
||||||
|
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include <boost/iostreams/concepts.hpp>
|
#include <boost/iostreams/concepts.hpp>
|
||||||
@@ -11,7 +8,13 @@
|
|||||||
#include <openssl/buffer.h>
|
#include <openssl/buffer.h>
|
||||||
#include <openssl/evp.h>
|
#include <openssl/evp.h>
|
||||||
|
|
||||||
using namespace std;
|
#include "json/value.h"
|
||||||
|
#include "json/writer.h"
|
||||||
|
|
||||||
|
#include "RPC.h"
|
||||||
|
#include "BitcoinUtil.h"
|
||||||
|
#include "Config.h"
|
||||||
|
|
||||||
using namespace boost;
|
using namespace boost;
|
||||||
using namespace boost::asio;
|
using namespace boost::asio;
|
||||||
|
|
||||||
@@ -19,12 +22,11 @@ using namespace boost::asio;
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if 0
|
Json::Value JSONRPCError(int code, const std::string& message)
|
||||||
Object JSONRPCError(int code, const string& message)
|
|
||||||
{
|
{
|
||||||
Object error;
|
Json::Value error(Json::arrayValue);
|
||||||
error.push_back(Pair("code", code));
|
error["code"]=Json::Value(code);
|
||||||
error.push_back(Pair("message", message));
|
error["message"]=Json::Value(message);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,9 +38,9 @@ Object JSONRPCError(int code, const string& message)
|
|||||||
// and to be compatible with other JSON-RPC implementations.
|
// and to be compatible with other JSON-RPC implementations.
|
||||||
//
|
//
|
||||||
|
|
||||||
string createHTTPPost(const string& strMsg, const map<string,string>& mapRequestHeaders)
|
std::string createHTTPPost(const std::string& strMsg, const std::map<std::string, std::string>& mapRequestHeaders)
|
||||||
{
|
{
|
||||||
ostringstream s;
|
std::ostringstream s;
|
||||||
s << "POST / HTTP/1.1\r\n"
|
s << "POST / HTTP/1.1\r\n"
|
||||||
<< "User-Agent: coin-json-rpc/" << FormatFullVersion() << "\r\n"
|
<< "User-Agent: coin-json-rpc/" << FormatFullVersion() << "\r\n"
|
||||||
<< "Host: 127.0.0.1\r\n"
|
<< "Host: 127.0.0.1\r\n"
|
||||||
@@ -46,7 +48,7 @@ string createHTTPPost(const string& strMsg, const map<string,string>& mapRequest
|
|||||||
<< "Content-Length: " << strMsg.size() << "\r\n"
|
<< "Content-Length: " << strMsg.size() << "\r\n"
|
||||||
<< "Accept: application/json\r\n";
|
<< "Accept: application/json\r\n";
|
||||||
|
|
||||||
typedef const pair<string, string> HeaderType;
|
typedef const std::pair<std::string, std::string> HeaderType;
|
||||||
BOOST_FOREACH(HeaderType& item, mapRequestHeaders)
|
BOOST_FOREACH(HeaderType& item, mapRequestHeaders)
|
||||||
s << item.first << ": " << item.second << "\r\n";
|
s << item.first << ": " << item.second << "\r\n";
|
||||||
s << "\r\n" << strMsg;
|
s << "\r\n" << strMsg;
|
||||||
@@ -54,27 +56,27 @@ string createHTTPPost(const string& strMsg, const map<string,string>& mapRequest
|
|||||||
return s.str();
|
return s.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
string rfc1123Time()
|
std::string rfc1123Time()
|
||||||
{
|
{
|
||||||
char buffer[64];
|
char buffer[64];
|
||||||
time_t now;
|
time_t now;
|
||||||
time(&now);
|
time(&now);
|
||||||
struct tm* now_gmt = gmtime(&now);
|
struct tm* now_gmt = gmtime(&now);
|
||||||
string locale(setlocale(LC_TIME, NULL));
|
std::string locale(setlocale(LC_TIME, NULL));
|
||||||
setlocale(LC_TIME, "C"); // we want posix (aka "C") weekday/month strings
|
setlocale(LC_TIME, "C"); // we want posix (aka "C") weekday/month strings
|
||||||
strftime(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S +0000", now_gmt);
|
strftime(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S +0000", now_gmt);
|
||||||
setlocale(LC_TIME, locale.c_str());
|
setlocale(LC_TIME, locale.c_str());
|
||||||
return string(buffer);
|
return std::string(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
string HTTPReply(int nStatus, const string& strMsg)
|
std::string HTTPReply(int nStatus, const std::string& strMsg)
|
||||||
{
|
{
|
||||||
cout << "HTTP Reply " << nStatus << " " << strMsg << endl;
|
std::cout << "HTTP Reply " << nStatus << " " << strMsg << std::endl;
|
||||||
|
|
||||||
if (nStatus == 401)
|
if (nStatus == 401)
|
||||||
return strprintf("HTTP/1.0 401 Authorization Required\r\n"
|
return strprintf("HTTP/1.0 401 Authorization Required\r\n"
|
||||||
"Date: %s\r\n"
|
"Date: %s\r\n"
|
||||||
"Server: bitcoin-json-rpc/%s\r\n"
|
"Server: coin-json-rpc/%s\r\n"
|
||||||
"WWW-Authenticate: Basic realm=\"jsonrpc\"\r\n"
|
"WWW-Authenticate: Basic realm=\"jsonrpc\"\r\n"
|
||||||
"Content-Type: text/html\r\n"
|
"Content-Type: text/html\r\n"
|
||||||
"Content-Length: 296\r\n"
|
"Content-Length: 296\r\n"
|
||||||
@@ -88,7 +90,7 @@ string HTTPReply(int nStatus, const string& strMsg)
|
|||||||
"</HEAD>\r\n"
|
"</HEAD>\r\n"
|
||||||
"<BODY><H1>401 Unauthorized.</H1></BODY>\r\n"
|
"<BODY><H1>401 Unauthorized.</H1></BODY>\r\n"
|
||||||
"</HTML>\r\n", rfc1123Time().c_str(), FormatFullVersion().c_str());
|
"</HTML>\r\n", rfc1123Time().c_str(), FormatFullVersion().c_str());
|
||||||
string strStatus;
|
std::string strStatus;
|
||||||
if (nStatus == 200) strStatus = "OK";
|
if (nStatus == 200) strStatus = "OK";
|
||||||
else if (nStatus == 400) strStatus = "Bad Request";
|
else if (nStatus == 400) strStatus = "Bad Request";
|
||||||
else if (nStatus == 403) strStatus = "Forbidden";
|
else if (nStatus == 403) strStatus = "Forbidden";
|
||||||
@@ -100,7 +102,7 @@ string HTTPReply(int nStatus, const string& strMsg)
|
|||||||
"Connection: close\r\n"
|
"Connection: close\r\n"
|
||||||
"Content-Length: %d\r\n"
|
"Content-Length: %d\r\n"
|
||||||
"Content-Type: application/json\r\n"
|
"Content-Type: application/json\r\n"
|
||||||
"Server: bitcoin-json-rpc/%s\r\n"
|
"Server: coin-json-rpc/%s\r\n"
|
||||||
"\r\n"
|
"\r\n"
|
||||||
"%s",
|
"%s",
|
||||||
nStatus,
|
nStatus,
|
||||||
@@ -113,31 +115,31 @@ string HTTPReply(int nStatus, const string& strMsg)
|
|||||||
|
|
||||||
int ReadHTTPStatus(std::basic_istream<char>& stream)
|
int ReadHTTPStatus(std::basic_istream<char>& stream)
|
||||||
{
|
{
|
||||||
string str;
|
std::string str;
|
||||||
getline(stream, str);
|
getline(stream, str);
|
||||||
vector<string> vWords;
|
std::vector<std::string> vWords;
|
||||||
boost::split(vWords, str, boost::is_any_of(" "));
|
boost::split(vWords, str, boost::is_any_of(" "));
|
||||||
if (vWords.size() < 2)
|
if (vWords.size() < 2)
|
||||||
return 500;
|
return 500;
|
||||||
return atoi(vWords[1].c_str());
|
return atoi(vWords[1].c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
int ReadHTTPHeader(std::basic_istream<char>& stream, map<string, string>& mapHeadersRet)
|
int ReadHTTPHeader(std::basic_istream<char>& stream, std::map<std::string, std::string>& mapHeadersRet)
|
||||||
{
|
{
|
||||||
int nLen = 0;
|
int nLen = 0;
|
||||||
loop
|
loop
|
||||||
{
|
{
|
||||||
string str;
|
std::string str;
|
||||||
std::getline(stream, str);
|
std::getline(stream, str);
|
||||||
if (str.empty() || str == "\r")
|
if (str.empty() || str == "\r")
|
||||||
break;
|
break;
|
||||||
string::size_type nColon = str.find(":");
|
std::string::size_type nColon = str.find(":");
|
||||||
if (nColon != string::npos)
|
if (nColon != std::string::npos)
|
||||||
{
|
{
|
||||||
string strHeader = str.substr(0, nColon);
|
std::string strHeader = str.substr(0, nColon);
|
||||||
boost::trim(strHeader);
|
boost::trim(strHeader);
|
||||||
boost::to_lower(strHeader);
|
boost::to_lower(strHeader);
|
||||||
string strValue = str.substr(nColon+1);
|
std::string strValue = str.substr(nColon+1);
|
||||||
boost::trim(strValue);
|
boost::trim(strValue);
|
||||||
mapHeadersRet[strHeader] = strValue;
|
mapHeadersRet[strHeader] = strValue;
|
||||||
if (strHeader == "content-length")
|
if (strHeader == "content-length")
|
||||||
@@ -147,7 +149,8 @@ int ReadHTTPHeader(std::basic_istream<char>& stream, map<string, string>& mapHea
|
|||||||
return nLen;
|
return nLen;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ReadHTTP(std::basic_istream<char>& stream, map<string, string>& mapHeadersRet, string& strMessageRet)
|
int ReadHTTP(std::basic_istream<char>& stream, std::map<std::string, std::string>& mapHeadersRet,
|
||||||
|
std::string& strMessageRet)
|
||||||
{
|
{
|
||||||
mapHeadersRet.clear();
|
mapHeadersRet.clear();
|
||||||
strMessageRet = "";
|
strMessageRet = "";
|
||||||
@@ -163,17 +166,17 @@ int ReadHTTP(std::basic_istream<char>& stream, map<string, string>& mapHeadersRe
|
|||||||
// Read message
|
// Read message
|
||||||
if (nLen > 0)
|
if (nLen > 0)
|
||||||
{
|
{
|
||||||
vector<char> vch(nLen);
|
std::vector<char> vch(nLen);
|
||||||
stream.read(&vch[0], nLen);
|
stream.read(&vch[0], nLen);
|
||||||
strMessageRet = string(vch.begin(), vch.end());
|
strMessageRet = std::string(vch.begin(), vch.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
return nStatus;
|
return nStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
string DecodeBase64(string s)
|
std::string DecodeBase64(std::string s)
|
||||||
{
|
{ // FIXME: This performs badly
|
||||||
BIO *b64, *bmem;
|
BIO *b64, *bmem;
|
||||||
|
|
||||||
char* buffer = static_cast<char*>(calloc(s.size(), sizeof(char)));
|
char* buffer = static_cast<char*>(calloc(s.size(), sizeof(char)));
|
||||||
@@ -185,23 +188,23 @@ string DecodeBase64(string s)
|
|||||||
BIO_read(bmem, buffer, s.size());
|
BIO_read(bmem, buffer, s.size());
|
||||||
BIO_free_all(bmem);
|
BIO_free_all(bmem);
|
||||||
|
|
||||||
string result(buffer);
|
std::string result(buffer);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
bool HTTPAuthorized(map<string, string>& mapHeaders)
|
bool HTTPAuthorized(map<std::string, std::string>& mapHeaders)
|
||||||
{
|
{
|
||||||
string strAuth = mapHeaders["authorization"];
|
std::string strAuth = mapHeaders["authorization"];
|
||||||
if (strAuth.substr(0,6) != "Basic ")
|
if (strAuth.substr(0,6) != "Basic ")
|
||||||
return false;
|
return false;
|
||||||
string strUserPass64 = strAuth.substr(6); boost::trim(strUserPass64);
|
std::string strUserPass64 = strAuth.substr(6); boost::trim(strUserPass64);
|
||||||
string strUserPass = DecodeBase64(strUserPass64);
|
std::string strUserPass = DecodeBase64(strUserPass64);
|
||||||
string::size_type nColon = strUserPass.find(":");
|
std::string::size_type nColon = strUserPass.find(":");
|
||||||
if (nColon == string::npos)
|
if (nColon == std::string::npos)
|
||||||
return false;
|
return false;
|
||||||
string strUser = strUserPass.substr(0, nColon);
|
std::string strUser = strUserPass.substr(0, nColon);
|
||||||
string strPassword = strUserPass.substr(nColon+1);
|
std::string strPassword = strUserPass.substr(nColon+1);
|
||||||
return (strUser == mapArgs["-rpcuser"] && strPassword == mapArgs["-rpcpassword"]);
|
return (strUser == mapArgs["-rpcuser"] && strPassword == mapArgs["-rpcpassword"]);
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
@@ -215,62 +218,36 @@ bool HTTPAuthorized(map<string, string>& mapHeaders)
|
|||||||
// http://www.codeproject.com/KB/recipes/JSON_Spirit.aspx
|
// http://www.codeproject.com/KB/recipes/JSON_Spirit.aspx
|
||||||
//
|
//
|
||||||
|
|
||||||
string JSONRPCRequest(const string& strMethod, const Array& params, const Value& id)
|
std::string JSONRPCRequest(const std::string& strMethod, const Json::Value& params, const Json::Value& id)
|
||||||
{
|
{
|
||||||
Object request;
|
Json::Value request;
|
||||||
request.push_back(Pair("method", strMethod));
|
request["method"]=strMethod;
|
||||||
request.push_back(Pair("params", params));
|
request["params"]=params;
|
||||||
request.push_back(Pair("id", id));
|
request["id"]=id;
|
||||||
return write_string(Value(request), false) + "\n";
|
Json::FastWriter writer;
|
||||||
|
return writer.write(request) + "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
string JSONRPCReply(const Value& result, const Value& error, const Value& id)
|
std::string JSONRPCReply(const Json::Value& result, const Json::Value& error, const Json::Value& id)
|
||||||
{
|
{
|
||||||
Object reply;
|
Json::Value reply;
|
||||||
if (error.type() != null_type)
|
if (!error.isNull())
|
||||||
reply.push_back(Pair("result", Value::null));
|
reply["result"]=Json::Value();
|
||||||
else
|
else
|
||||||
reply.push_back(Pair("result", result));
|
reply["result"]=result;
|
||||||
reply.push_back(Pair("error", error));
|
reply["error"]=error;
|
||||||
reply.push_back(Pair("id", id));
|
reply["id"]=id;
|
||||||
return write_string(Value(reply), false) + "\n";
|
Json::FastWriter writer;
|
||||||
|
return writer.write(reply) + "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void ErrorReply(std::ostream& stream, const Object& objError, const Value& id)
|
void ErrorReply(std::ostream& stream, const Json::Value& objError, const Json::Value& id)
|
||||||
{
|
{
|
||||||
// Send error reply from json-rpc error object
|
// Send error reply from json-rpc error object
|
||||||
int nStatus = 500;
|
int nStatus = 500;
|
||||||
int code = find_value(objError, "code").get_int();
|
int code = objError["code"].asInt();
|
||||||
if (code == -32600) nStatus = 400;
|
if (code == -32600) nStatus = 400;
|
||||||
else if (code == -32601) nStatus = 404;
|
else if (code == -32601) nStatus = 404;
|
||||||
string strReply = JSONRPCReply(Value::null, objError, id);
|
std::string strReply = JSONRPCReply(Json::Value(), objError, id);
|
||||||
stream << HTTPReply(nStatus, strReply) << std::flush;
|
stream << HTTPReply(nStatus, strReply) << std::flush;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void ConvertTo(Value& value)
|
|
||||||
{
|
|
||||||
if (value.type() == str_type)
|
|
||||||
{
|
|
||||||
// reinterpret string as unquoted json value
|
|
||||||
Value value2;
|
|
||||||
if (!read_string(value.get_str(), value2))
|
|
||||||
throw runtime_error("type mismatch");
|
|
||||||
value = value2.get_value<T>();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
value = value.get_value<T>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|||||||
Reference in New Issue
Block a user