aboutsummaryrefslogtreecommitdiff
path: root/src/rpc_server.cpp
blob: 20b55cf43d8d8b3c7d4675dfc360b6a8d00c18af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>

#include "rpc_server.h"

#define RPC_PREFIX "rpc."

RPCRequest::RPCRequest(HTTPRequest* request):
    request(request)
{
    if (!request)
        return;

    this->body.Parse(request->get_body());

    if (!this->body.HasMember("jsonrpc"))
    {
        spdlog::error("Request is missing 'jsonrpc' field");
        this->error(rapidjson::Value("Request is missing 'jsonrpc' field"));
        return;
    }
    else if (this->body["jsonrpc"] != "2.0")
    {
        spdlog::error("Request has invalid value for 'jsonrpc' field");
        this->error(rapidjson::Value("Request has invalid value for 'jsonrpc' field"));
        return;
    }

    if (this->body.HasMember("id"))
    {
        this->id = this->body["id"];

        if (!this->id.IsInt() && !this->id.IsString() && !this->id.IsNull())
        {
            spdlog::error("Request has invalid type for 'id' field");
            this->error(rapidjson::Value("Request has invalid type for 'id' field"));
            return;
        }
    }

    if (!this->body.HasMember("method"))
    {
        spdlog::error("Request is missing 'method' field");
        this->error(rapidjson::Value("Request is missing 'method' field"));
        return;
    }
    else if (!this->body["method"].IsString())
    {
        spdlog::error("Request has invalid type for 'method' field");
        this->error(rapidjson::Value("Request has invalid type for 'method' field"));
        return;
    }

    this->method = this->body["method"].GetString();

    if (!strncmp(this->method.c_str(), RPC_PREFIX, strlen(RPC_PREFIX)))
    {
        spdlog::error("Request uses reserved value for 'method' field");
        this->error(rapidjson::Value("Request uses reserved value for 'method' field"));
        return;
    }

    if (this->body.HasMember("params"))
    {
        this->params = this->body["params"];

        if (!this->params.IsArray() && !this->params.IsObject())
        {
            spdlog::error("Request has invalid type for 'params' field");
            this->error(rapidjson::Value("Request has invalid type for 'params' field"));
            return;
        }
    }

    spdlog::debug("Valid request");
}

RPCRequest::~RPCRequest()
{
    delete this->request;
}

void RPCRequest::response(rapidjson::Value& response)
{
    // a request without an ID is a Notification.
    // Nothing we can do about it
    if (!this->body.HasMember("id"))
        return;

    rapidjson::StringBuffer buffer;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);

    response.Accept(writer);

    this->request->respond(HTTP_OK, {
        {"Content-Type", "application/json"}
    }, buffer.GetString());
}

void RPCRequest::result(rapidjson::Value result)
{
    if (!this->body.HasMember("id"))
        return;

    rapidjson::MemoryPoolAllocator<>& allocator = this->body.GetAllocator();

    rapidjson::Value error_resp;
    error_resp.SetObject();

    error_resp.AddMember("jsonrpc", "2.0", allocator);
    error_resp.AddMember("id", this->id, allocator);
    error_resp.AddMember("result", result, allocator);

    this->response(error_resp);
}

void RPCRequest::error(rapidjson::Value error)
{
    if (!this->body.HasMember("id"))
        return;

    rapidjson::MemoryPoolAllocator<>& allocator = this->body.GetAllocator();

    rapidjson::Value error_resp;
    error_resp.SetObject();

    error_resp.AddMember("jsonrpc", "2.0", allocator);
    error_resp.AddMember("id", this->id, allocator);
    error_resp.AddMember("error", error, allocator);

    this->response(error_resp);
}

void RPCRequest::close()
{
    if (this->request)
    {
        this->request->close();
    }
}

RPCServer::RPCServer(unsigned long addr, unsigned short port):
    http_server(addr, port)
{
}

RPCRequest* RPCServer::receive_request()
{
    spdlog::debug("awaiting JSON-RPC request");

    HTTPRequest* http_req = this->http_server.receive_request();
    if (!http_req)
        return nullptr;

    RPCRequest* rpc_req = new RPCRequest(http_req);

    return rpc_req;
}