Remove redundant nullptr checks before deallocation
[bitcoinplatinum.git] / src / rpc / protocol.cpp
blobdb0626b5e1ca62a95bfcd711440bb8b1d0b68727
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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.
6 #include "rpc/protocol.h"
8 #include "random.h"
9 #include "tinyformat.h"
10 #include "util.h"
11 #include "utilstrencodings.h"
12 #include "utiltime.h"
13 #include "version.h"
15 #include <stdint.h>
16 #include <fstream>
18 /**
19 * JSON-RPC protocol. Bitcoin speaks version 1.0 for maximum compatibility,
20 * but uses JSON-RPC 1.1/2.0 standards for parts of the 1.0 standard that were
21 * unspecified (HTTP errors and contents of 'error').
23 * 1.0 spec: http://json-rpc.org/wiki/specification
24 * 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html
27 UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id)
29 UniValue request(UniValue::VOBJ);
30 request.push_back(Pair("method", strMethod));
31 request.push_back(Pair("params", params));
32 request.push_back(Pair("id", id));
33 return request;
36 UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id)
38 UniValue reply(UniValue::VOBJ);
39 if (!error.isNull())
40 reply.push_back(Pair("result", NullUniValue));
41 else
42 reply.push_back(Pair("result", result));
43 reply.push_back(Pair("error", error));
44 reply.push_back(Pair("id", id));
45 return reply;
48 std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
50 UniValue reply = JSONRPCReplyObj(result, error, id);
51 return reply.write() + "\n";
54 UniValue JSONRPCError(int code, const std::string& message)
56 UniValue error(UniValue::VOBJ);
57 error.push_back(Pair("code", code));
58 error.push_back(Pair("message", message));
59 return error;
62 /** Username used when cookie authentication is in use (arbitrary, only for
63 * recognizability in debugging/logging purposes)
65 static const std::string COOKIEAUTH_USER = "__cookie__";
66 /** Default name for auth cookie file */
67 static const std::string COOKIEAUTH_FILE = ".cookie";
69 fs::path GetAuthCookieFile()
71 fs::path path(gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE));
72 if (!path.is_complete()) path = GetDataDir() / path;
73 return path;
76 bool GenerateAuthCookie(std::string *cookie_out)
78 const size_t COOKIE_SIZE = 32;
79 unsigned char rand_pwd[COOKIE_SIZE];
80 GetRandBytes(rand_pwd, COOKIE_SIZE);
81 std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd, rand_pwd+COOKIE_SIZE);
83 /** the umask determines what permissions are used to create this file -
84 * these are set to 077 in init.cpp unless overridden with -sysperms.
86 std::ofstream file;
87 fs::path filepath = GetAuthCookieFile();
88 file.open(filepath.string().c_str());
89 if (!file.is_open()) {
90 LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath.string());
91 return false;
93 file << cookie;
94 file.close();
95 LogPrintf("Generated RPC authentication cookie %s\n", filepath.string());
97 if (cookie_out)
98 *cookie_out = cookie;
99 return true;
102 bool GetAuthCookie(std::string *cookie_out)
104 std::ifstream file;
105 std::string cookie;
106 fs::path filepath = GetAuthCookieFile();
107 file.open(filepath.string().c_str());
108 if (!file.is_open())
109 return false;
110 std::getline(file, cookie);
111 file.close();
113 if (cookie_out)
114 *cookie_out = cookie;
115 return true;
118 void DeleteAuthCookie()
120 try {
121 fs::remove(GetAuthCookieFile());
122 } catch (const fs::filesystem_error& e) {
123 LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, e.what());