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

Function w_sip_to_json

modules/sipmsgops/sipmsgops.c:2089–2188  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2087}
2088
2089static int w_sip_to_json(struct sip_msg *msg, pv_spec_t* out_json)
2090{
2091 cJSON *ret=NULL, *aux, *aux2, *arr;
2092 struct hdr_field* it;
2093 char hdr_name_buf[255];
2094 str json_ret = {0,0};
2095 pv_value_t pv_val;
2096
2097 if (!msg) {
2098 LM_ERR("No SIP msg, can't convert to json\n");
2099 return -1;
2100 }
2101
2102 if(parse_headers(msg, HDR_EOH_F, 0) < 0) {
2103 LM_ERR("Failed to parse all SIP msg \n");
2104 return -1;
2105 }
2106
2107 ret = cJSON_CreateObject();
2108
2109 /* first line */
2110 aux = cJSON_CreateStr(msg->buf,msg->first_line.len);
2111 if (!aux) {
2112 LM_ERR("Failed to create 1st line json \n");
2113 goto error;
2114 }
2115
2116 cJSON_AddItemToObject(ret,"first_line",aux);
2117
2118 /* headers */
2119 aux = cJSON_CreateObject();
2120 if (!aux) {
2121 LM_ERR("Failed to create headers json \n");
2122 goto error;
2123 }
2124
2125 for (it=msg->headers;it;it=it->next) {
2126 if (it->name.len >= sizeof(hdr_name_buf)) {
2127 LM_WARN("header name too long (%d), skipping\n", it->name.len);
2128 continue;
2129 }
2130 memcpy(hdr_name_buf,it->name.s,it->name.len);
2131 hdr_name_buf[it->name.len] = 0;
2132
2133 arr = cJSON_GetObjectItem(aux, hdr_name_buf);
2134 if (!arr) {
2135 arr = cJSON_CreateArray();
2136 cJSON_AddItemToObject(aux,hdr_name_buf,arr);
2137 }
2138
2139 aux2 = cJSON_CreateStr(it->body.s,it->body.len);
2140 if (!aux2) {
2141 LM_ERR("Failed to create individual header json\n");
2142 goto error;
2143 }
2144
2145 cJSON_AddItemToArray(arr,aux2);
2146 }

Callers

nothing calls this directly

Calls 10

cJSON_CreateStrFunction · 0.85
cJSON_MinifyFunction · 0.85
pv_set_valueFunction · 0.85
cJSON_CreateObjectFunction · 0.50
cJSON_AddItemToObjectFunction · 0.50
cJSON_GetObjectItemFunction · 0.50
cJSON_CreateArrayFunction · 0.50
cJSON_AddItemToArrayFunction · 0.50
cJSON_PrintFunction · 0.50
cJSON_DeleteFunction · 0.50

Tested by

no test coverage detected