Skip to content

Commit 043b7e0

Browse files
authored
Merge pull request #3311 from vladpaiu/sip2json
Add sip2json pvar
2 parents fa93ebb + 15537c2 commit 043b7e0

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

modules/sipmsgops/doc/sipmsgops_admin.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,37 @@ list_hdr_add_option("Supported", "optionX");
16841684
</example>
16851685
</section>
16861686

1687+
<section id="sip_to_json" xreflabel="sip_to_json()">
1688+
<title>
1689+
<function moreinfo="none">sip_to_json(out_var)</function>
1690+
</title>
1691+
<para>
1692+
Returns a JSON formatted representation of the current SIP message, containing first_line , headers and body json members
1693+
Useful in cases when you want to pass a generic SIP message to a SIP agnostic entity, but still want to provide some layer of SIP parsing before sending the full message further.
1694+
</para>
1695+
<para>Meaning of the parameters is as follows:</para>
1696+
<itemizedlist>
1697+
<listitem>
1698+
<para><emphasis>out_var (string)</emphasis> - the output JSON formatted SIP message variable
1699+
</para>
1700+
</listitem>
1701+
</itemizedlist>
1702+
<para>
1703+
This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
1704+
FAILURE_ROUTE, BRANCH_ROUTE and LOCAL_ROUTE.
1705+
</para>
1706+
<example>
1707+
<title><function>sip_to_json</function> usage</title>
1708+
<programlisting format="linespecific">
1709+
...
1710+
if (sip_to_json($var(out_sip_json))) {
1711+
xlog("The JSON format for the current SIP message is $var(out_sip_json) \n");
1712+
}
1713+
...
1714+
</programlisting>
1715+
</example>
1716+
</section>
1717+
16871718

16881719
</section>
16891720
<section>

modules/sipmsgops/sipmsgops.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "../../msg_translator.h"
5454
#include "../../mod_fix.h"
5555
#include "../../trim.h"
56+
#include "../../lib/cJSON.h"
5657

5758
#include "codecs.h"
5859
#include "list_hdr.h"
@@ -101,6 +102,7 @@ static int add_body_part_f(struct sip_msg *msg, str *body, str *mime,
101102
static int get_updated_body_part_f(struct sip_msg *msg, int *type,pv_spec_t* out);
102103
static int is_audio_on_hold_f(struct sip_msg *msg);
103104
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);
104106

105107
static int fixup_parse_hname(void** param);
106108

@@ -303,6 +305,9 @@ static const cmd_export_t cmds[]={
303305
{CMD_PARAM_VAR, 0, 0},
304306
{0, 0, 0}},
305307
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},
306311

307312
{0,0,{{0,0,0}},0}
308313
};
@@ -2072,3 +2077,100 @@ static int get_glob_headers_values(struct sip_msg* msg, str* pattern,pv_spec_t*
20722077
}
20732078
return cnt==0 ? -1 : 1;
20742079
}
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

Comments
 (0)