| 
53 | 53 | #include "../../msg_translator.h"  | 
54 | 54 | #include "../../mod_fix.h"  | 
55 | 55 | #include "../../trim.h"  | 
 | 56 | +#include "../../lib/cJSON.h"  | 
56 | 57 | 
 
  | 
57 | 58 | #include "codecs.h"  | 
58 | 59 | #include "list_hdr.h"  | 
@@ -101,6 +102,7 @@ static int add_body_part_f(struct sip_msg *msg, str *body, str *mime,  | 
101 | 102 | static int get_updated_body_part_f(struct sip_msg *msg, int *type,pv_spec_t* out);  | 
102 | 103 | static int is_audio_on_hold_f(struct sip_msg *msg);  | 
103 | 104 | static int w_sip_validate(struct sip_msg *msg, void *flags, pv_spec_t* err_txt);  | 
 | 105 | +static int w_sip_to_json(struct sip_msg *msg, pv_spec_t* out_json);  | 
104 | 106 | 
 
  | 
105 | 107 | static int fixup_parse_hname(void** param);  | 
106 | 108 | 
 
  | 
@@ -303,6 +305,9 @@ static const cmd_export_t cmds[]={  | 
303 | 305 | 	        {CMD_PARAM_VAR, 0, 0},  | 
304 | 306 | 		{0, 0, 0}},  | 
305 | 307 | 		REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},  | 
 | 308 | +	{"sip_to_json",     (cmd_function)w_sip_to_json, {  | 
 | 309 | +		{CMD_PARAM_VAR, 0, 0}, {0, 0, 0}},  | 
 | 310 | +		REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},  | 
306 | 311 | 
 
  | 
307 | 312 | 	{0,0,{{0,0,0}},0}  | 
308 | 313 | };  | 
@@ -2072,3 +2077,100 @@ static int get_glob_headers_values(struct sip_msg* msg, str* pattern,pv_spec_t*  | 
2072 | 2077 | 	}  | 
2073 | 2078 | 	return cnt==0 ? -1 : 1;  | 
2074 | 2079 | }  | 
 | 2080 | + | 
 | 2081 | +static int w_sip_to_json(struct sip_msg *msg, pv_spec_t* out_json)  | 
 | 2082 | +{  | 
 | 2083 | +	cJSON *ret=NULL, *aux, *aux2, *arr;  | 
 | 2084 | +	struct hdr_field* it;  | 
 | 2085 | +	char hdr_name_buf[255];  | 
 | 2086 | +	str json_ret = {0,0};  | 
 | 2087 | +	pv_value_t pv_val;  | 
 | 2088 | + | 
 | 2089 | +	if (!msg) {  | 
 | 2090 | +		LM_ERR("No SIP msg, can't convert to json\n");  | 
 | 2091 | +		return -1;  | 
 | 2092 | +	}	  | 
 | 2093 | + | 
 | 2094 | +	if(parse_headers(msg, HDR_EOH_F, 0) < 0) {  | 
 | 2095 | +		LM_ERR("Failed to parse all SIP msg \n");  | 
 | 2096 | +		return -1;  | 
 | 2097 | +	}  | 
 | 2098 | + | 
 | 2099 | +	ret = cJSON_CreateObject();  | 
 | 2100 | + | 
 | 2101 | +	/* first line */  | 
 | 2102 | +	aux = cJSON_CreateStr(msg->buf,msg->first_line.len);   | 
 | 2103 | +	if (!aux) {  | 
 | 2104 | +		LM_ERR("Failed to create 1st line json \n");  | 
 | 2105 | +		goto error;  | 
 | 2106 | +	}  | 
 | 2107 | + | 
 | 2108 | +	cJSON_AddItemToObject(ret,"first_line",aux);  | 
 | 2109 | + | 
 | 2110 | +	/* headers */  | 
 | 2111 | +	aux = cJSON_CreateObject();  | 
 | 2112 | +	if (!aux) {  | 
 | 2113 | +		LM_ERR("Failed to create headers json \n");  | 
 | 2114 | +		goto error;  | 
 | 2115 | +	}  | 
 | 2116 | + | 
 | 2117 | +	for (it=msg->headers;it;it=it->next) {  | 
 | 2118 | +		memcpy(hdr_name_buf,it->name.s,it->name.len);  | 
 | 2119 | +		hdr_name_buf[it->name.len] = 0;  | 
 | 2120 | + | 
 | 2121 | +		arr = cJSON_GetObjectItem(aux, hdr_name_buf);  | 
 | 2122 | +		if (!arr) {  | 
 | 2123 | +			arr = cJSON_CreateArray();  | 
 | 2124 | +			cJSON_AddItemToObject(aux,hdr_name_buf,arr);  | 
 | 2125 | +		}  | 
 | 2126 | + | 
 | 2127 | +		aux2 =  cJSON_CreateStr(it->body.s,it->body.len);  | 
 | 2128 | +		if (!aux2) {  | 
 | 2129 | +			LM_ERR("Failed to create individual header json\n");  | 
 | 2130 | +			goto error;  | 
 | 2131 | +		}  | 
 | 2132 | + | 
 | 2133 | +		cJSON_AddItemToArray(arr,aux2);  | 
 | 2134 | +	}  | 
 | 2135 | +	cJSON_AddItemToObject(ret,"headers",aux);  | 
 | 2136 | + | 
 | 2137 | +	/* body */  | 
 | 2138 | +	if (msg->body) {  | 
 | 2139 | +		aux = cJSON_CreateStr(msg->body->body.s,msg->body->body.len);   | 
 | 2140 | +		if (!aux) {  | 
 | 2141 | +			LM_ERR("Failed to create body json\n");  | 
 | 2142 | +			goto error;  | 
 | 2143 | +		}  | 
 | 2144 | +		cJSON_AddItemToObject(ret,"body",aux);  | 
 | 2145 | +	}  | 
 | 2146 | + | 
 | 2147 | +	json_ret.s = cJSON_Print(ret);  | 
 | 2148 | +	if (!json_ret.s) {  | 
 | 2149 | +		LM_ERR("Failed to print json to string obj\n");  | 
 | 2150 | +		goto error;  | 
 | 2151 | +	}  | 
 | 2152 | + | 
 | 2153 | +	cJSON_Minify(json_ret.s);  | 
 | 2154 | +	json_ret.len = strlen(json_ret.s);  | 
 | 2155 | + | 
 | 2156 | +	pv_val.flags = PV_VAL_STR;  | 
 | 2157 | +	pv_val.rs = json_ret;  | 
 | 2158 | + | 
 | 2159 | +	if (pv_set_value(msg,out_json,0,&pv_val) != 0) {  | 
 | 2160 | +		LM_ERR("Failed to set out json pvar \n");  | 
 | 2161 | +		goto error;  | 
 | 2162 | +	}   | 
 | 2163 | + | 
 | 2164 | +	pkg_free(json_ret.s);  | 
 | 2165 | +	cJSON_Delete(ret);  | 
 | 2166 | + | 
 | 2167 | +	return 1;  | 
 | 2168 | + | 
 | 2169 | +error:  | 
 | 2170 | +	if (ret)  | 
 | 2171 | +		cJSON_Delete(ret);  | 
 | 2172 | +	if (json_ret.s)  | 
 | 2173 | +		pkg_free(json_ret.s);  | 
 | 2174 | + | 
 | 2175 | +	return -1;  | 
 | 2176 | +}  | 
0 commit comments