MCPcopy Create free account
hub / github.com/OpenSIPS/opensips / jsonrpc_build_cmd

Function jsonrpc_build_cmd

modules/jsonrpc/jsonrpc.c:464–510  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

462}
463
464static char *jsonrpc_build_cmd(str *method, str *params, int *id)
465{
466 char *s;
467 cJSON *param_obj;
468 cJSON *ret_obj;
469 char *params_buf;
470
471 /* first thing - try to parse the parameters - need it NULL terminated */
472 params_buf = pkg_malloc(params->len + 1);
473 if (!params_buf) {
474 LM_ERR("cannot allocate memory for params!\n");
475 return NULL;
476 }
477 memcpy(params_buf, params->s, params->len);
478 params_buf[params->len] = 0;
479
480 param_obj = cJSON_Parse(params_buf);
481 pkg_free(params_buf);
482 if (!param_obj) {
483 LM_ERR("cannot parse json param: %.*s\n", params->len, params->s);
484 return NULL;
485 }
486
487 if (param_obj->type != cJSON_Array && param_obj->type != cJSON_Object) {
488 LM_ERR("invalid cJSON type %d - must be array or object!\n", param_obj->type);
489 cJSON_Delete(param_obj);
490 return NULL;
491 }
492
493 ret_obj = cJSON_CreateObject();
494 if (id)
495 cJSON_AddNumberToObject(ret_obj, "id", *id);
496 else
497 cJSON_AddNullToObject(ret_obj, "id");
498 cJSON_AddItemToObject(ret_obj, "jsonrpc",
499 cJSON_CreateString(JSONRPC_VERSION));
500 cJSON_AddItemToObject(ret_obj, "method",
501 cJSON_CreateStr(method->s, method->len));
502 cJSON_AddItemToObject(ret_obj, "params", param_obj);
503
504 s = cJSON_PrintUnformatted(ret_obj);
505 if (!s)
506 LM_ERR("cannot print json object!\n");
507
508 cJSON_Delete(ret_obj);
509 return s;
510}

Callers 2

jrpc_notifyFunction · 0.85
jrpc_requestFunction · 0.85

Calls 7

cJSON_CreateStrFunction · 0.85
cJSON_ParseFunction · 0.50
cJSON_DeleteFunction · 0.50
cJSON_CreateObjectFunction · 0.50
cJSON_AddItemToObjectFunction · 0.50
cJSON_CreateStringFunction · 0.50
cJSON_PrintUnformattedFunction · 0.50

Tested by

no test coverage detected