Skip to content
This repository was archived by the owner on Apr 24, 2022. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 010f9c3

Browse files
gianlucastella1AndreaLanfranchi
authored andcommittedJul 4, 2018
Add support for API request id as per JSONRPC v2 specs (#1332)
Member "id" in API requests can be an integer or a string
1 parent ccf8722 commit 010f9c3

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed
 

‎libapicore/ApiServer.cpp

+37-9
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,41 @@ static bool checkApiWriteAccess(bool is_read_only, Json::Value& jResponse)
153153
return !is_read_only;
154154
}
155155

156+
static bool parseRequestId(Json::Value& jRequest, Json::Value& jResponse)
157+
{
158+
const char *membername = "id";
159+
160+
// NOTE: all errors have the same code (-32600) indicating this is an invalid request
161+
162+
// be sure id is there and it's not empty, otherwise raise an error
163+
if (!jRequest.isMember(membername) || jRequest[membername].empty())
164+
{
165+
jResponse[membername] = Json::nullValue;
166+
jResponse["error"]["code"] = -32600;
167+
jResponse["error"]["message"] = "Invalid Request (missing or empty id)";
168+
return false;
169+
}
170+
171+
// try to parse id as Uint
172+
if(jRequest[membername].isUInt())
173+
{
174+
jResponse[membername] = jRequest[membername].asUInt();
175+
return true;
176+
}
177+
178+
// try to parse id as String
179+
if (jRequest[membername].isString())
180+
{
181+
jResponse[membername] = jRequest[membername].asString();
182+
return true;
183+
}
184+
185+
// id has invalid type
186+
jResponse[membername] = Json::nullValue;
187+
jResponse["error"]["code"] = -32600;
188+
jResponse["error"]["message"] = "Invalid Request (id has invalid type)";
189+
return false;
190+
}
156191

157192
ApiServer::ApiServer(
158193
boost::asio::io_service& io_service, int portnum, bool readonly, string password, Farm& f, PoolManager& mgr)
@@ -288,15 +323,8 @@ void ApiConnection::processRequest(Json::Value& jRequest, Json::Value& jResponse
288323
jResponse["jsonrpc"] = "2.0";
289324

290325
// Strict sanity checks over jsonrpc v2
291-
unsigned id;
292-
if (!getRequestValue("id", id, jRequest, false, jResponse))
293-
{
294-
jResponse["id"] = Json::nullValue;
295-
jResponse["error"]["code"] = -32600;
296-
jResponse["error"]["message"] = "Invalid Request";
297-
return;
298-
}
299-
jResponse["id"] = id;
326+
if (!parseRequestId(jRequest, jResponse))
327+
return;
300328

301329
std::string jsonrpc;
302330
std::string _method;

0 commit comments

Comments
 (0)
This repository has been archived.