Browse code

apps/jsonrpc: bug fixes

- error member cannot exist if there was no error
- id member must be of same type in response as it was in request
- id cannot be bool

Juha Heinanen authored on 24/05/2014 18:33:48
Showing 1 changed files
... ...
@@ -173,14 +173,14 @@ int JsonRpcServer::processMessage(char* msgbuf, unsigned int* msg_size,
173 173
   }
174 174
 
175 175
   string id;
176
+  bool id_is_int = false;
176 177
   if (rpc_params.hasMember("id")) {
177
-    if (isArgCStr(rpc_params["id"]))
178
+    if (isArgCStr(rpc_params["id"])) {
178 179
       id = rpc_params["id"].asCStr();
179
-    else if (isArgInt(rpc_params["id"]))
180
+    } else if (isArgInt(rpc_params["id"])) {
180 181
       id = int2str(rpc_params["id"].asInt());
181
-    else if (isArgBool(rpc_params["id"]))
182
-      id = rpc_params["id"].asBool() ? "True":"False";
183
-    else {
182
+      id_is_int = true;
183
+    } else {
184 184
       ERROR("incorrect type for jsonrpc id <%s>\n", 
185 185
 	    AmArg::print(rpc_params["id"]).c_str());
186 186
     }
... ...
@@ -237,11 +237,18 @@ int JsonRpcServer::processMessage(char* msgbuf, unsigned int* msg_size,
237 237
   }
238 238
 
239 239
   AmArg rpc_res;
240
+  int int_id;
240 241
 
241 242
   execRpc(rpc_params, rpc_res);
242 243
 
243
-  // rpc_res["error"] = AmArg(); // Undef/null
244
-  // rpc_res["id"] = rpc_params["id"];
244
+  if (!id.empty()) {
245
+    if (id_is_int) {
246
+      str2int(id, int_id);
247
+      rpc_res["id"] = int_id;
248
+    } else {
249
+      rpc_res["id"] = id;
250
+    }
251
+  }
245 252
 
246 253
   string res_s = arg2json(rpc_res);
247 254
   if (res_s.length() > MAX_RPC_MSG_SIZE) {
... ...
@@ -287,7 +294,6 @@ void JsonRpcServer::execRpc(const string& method, const string& id, const AmArg&
287 294
       if (factory == "core") {
288 295
 	runCoreMethod(fact_meth, params, rpc_res["result"]);
289 296
 	rpc_res["id"] = id;
290
-	rpc_res["error"] = AmArg(); // Undef/null
291 297
 	rpc_res["jsonrpc"] = "2.0";
292 298
 	return;
293 299
       }