Skip to content

Commit

Permalink
acc_json: param to accept pre-encoded json values (#2699)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchavanton committed Apr 1, 2021
1 parent 2f1067f commit c45d65b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 7 deletions.
55 changes: 49 additions & 6 deletions src/modules/acc_json/acc_json_mod.c
Expand Up @@ -64,6 +64,8 @@ int acc_output_syslog = -1;
char *acc_output_mqueue_str = 0; /* see mqueue module queue name */
str acc_q_name = {0, 0};
static char *acc_log_facility_str = 0;
static char *acc_json_pre_encoded_prefix_str = 0;
str acc_json_pre_encoded_prefix = {0, 0};

int cdr_enable = 0;
static char *cdr_extra_str = 0;
Expand All @@ -73,20 +75,24 @@ int cdr_output_syslog = -1;
char *cdr_output_mqueue_str = 0; /* see mqueue module queue name */
str cdr_q_name = {0, 0};
static char *cdr_log_facility_str = 0;
static char *cdr_json_pre_encoded_prefix_str = 0;
str cdr_json_pre_encoded_prefix = {0, 0};

static cmd_export_t cmds[] = {{0, 0, 0, 0, 0, 0}};


static param_export_t params[] = {{"acc_flag", INT_PARAM, &acc_flag},
{"acc_missed_flag", INT_PARAM, &acc_missed_flag},
{"acc_extra", PARAM_STRING, &acc_extra_str},
{"acc_pre_encoded_prefix", PARAM_STRING, &acc_json_pre_encoded_prefix_str},
{"acc_time_mode", INT_PARAM, &acc_time_mode},
{"acc_time_format", PARAM_STRING, &acc_time_format},
{"acc_log_level", INT_PARAM, &acc_log_level},
{"acc_log_facility", PARAM_STRING, &acc_log_facility_str},
{"acc_output_mqueue", PARAM_STRING, &acc_output_mqueue_str},
{"acc_output_syslog", INT_PARAM, &acc_output_syslog},
{"cdr_extra", PARAM_STRING, &cdr_extra_str},
{"cdr_pre_encoded_prefix", PARAM_STRING, &cdr_json_pre_encoded_prefix_str},
{"cdr_enable", INT_PARAM, &cdr_enable},
{"cdr_expired_dlg_enable", INT_PARAM, &cdr_expired_dlg_enable},
{"cdr_log_level", INT_PARAM, &cdr_log_level},
Expand Down Expand Up @@ -150,6 +156,15 @@ static int mod_init(void)
return -1;
}
}
/* prefix to handle pre-encoded json fields */
if (acc_json_pre_encoded_prefix_str) {
acc_json_pre_encoded_prefix.s = acc_json_pre_encoded_prefix_str;
acc_json_pre_encoded_prefix.len = strlen(acc_json_pre_encoded_prefix_str);
}
if (cdr_json_pre_encoded_prefix_str) {
cdr_json_pre_encoded_prefix.s = cdr_json_pre_encoded_prefix_str;
cdr_json_pre_encoded_prefix.len = strlen(cdr_json_pre_encoded_prefix_str);
}

/* load the MQUEUE API */
if((acc_output_mqueue_str || cdr_output_mqueue_str) && (load_mq_api(&mq_api) != 0)) {
Expand Down Expand Up @@ -315,9 +330,23 @@ int acc_json_send_request(struct sip_msg *req, acc_info_t *inf)
LM_DBG("[%d][%s][%.*s]\n", i, extra->name.s, inf->varr[i].len,
inf->varr[i].s);
char *tmp = strndup(inf->varr[i].s, inf->varr[i].len);
json_t *value = json_string(tmp);
if(!value)
value = json_string("NON-UTF8");
json_t *value;
if (acc_json_pre_encoded_prefix.len && inf->varr[i].len
&& extra->name.len>=acc_json_pre_encoded_prefix.len
&& strncmp(extra->name.s,acc_json_pre_encoded_prefix.s, sizeof(char)*acc_json_pre_encoded_prefix.len) == 0) {
LM_DBG("[%d][%s] json content \n", i, extra->name.s);
json_error_t jerr;
value = json_loads(tmp, 0, &jerr);
if (!value) {
LM_ERR("error loading json in[%s]: %s:%d:%d: %s\n", extra->name.s,
jerr.source, jerr.line, jerr.column, jerr.text);
value = json_string("INVALID_JSON");
}
} else {
value = json_string(tmp);
if(!value)
value = json_string("NON-UTF8");
}
json_object_set_new(object, extra->name.s, value);
free(tmp);
extra = extra->next;
Expand Down Expand Up @@ -439,9 +468,23 @@ int cdr_json_write(struct dlg_cell *dlg, struct sip_msg *req, cdr_info_t *inf)
LM_DBG("[%d][%s][%.*s]\n", i, extra->name.s, inf->varr[i].len,
inf->varr[i].s);
char *tmp = strndup(inf->varr[i].s, inf->varr[i].len);
json_t *value = json_string(tmp);
if(!value)
value = json_string("NON-UTF8");
json_t *value;
if (cdr_json_pre_encoded_prefix.len && inf->varr[i].len
&& extra->name.len>=cdr_json_pre_encoded_prefix.len
&& strncmp(extra->name.s,cdr_json_pre_encoded_prefix.s, sizeof(char)*cdr_json_pre_encoded_prefix.len) == 0) {
LM_DBG("[%d][%s] json content \n", i, extra->name.s);
json_error_t jerr;
value = json_loads(tmp, 0, &jerr);
if (!value) {
LM_ERR("error loading json in[%s]: %s:%d:%d: %s\n", extra->name.s,
jerr.source, jerr.line, jerr.column, jerr.text);
value = json_string("INVALID_JSON");
}
} else {
value = json_string(tmp);
if(!value)
value = json_string("NON-UTF8");
}
json_object_set_new(object, extra->name.s, value);
free(tmp);
extra = extra->next;
Expand Down
43 changes: 42 additions & 1 deletion src/modules/acc_json/doc/acc_json_admin.xml
Expand Up @@ -121,6 +121,27 @@ modparam("acc_json", "acc_extra", "via=$hdr(Via[*]); email=$avp(s:email)")
</example>
</section>

<section id="acc_json.p.acc_json_pre_encoded_prefix">
<title><varname>acc_json_pre_encoded_prefix</varname> (string)</title>
<para>
Prefix to identify values that will be considered to be already json encoded.
</para>
<para>
Default value is NULL.
</para>
<example>
<title>acc_json_pre_encoded_prefix example</title>
<programlisting format="linespecific">
...
modparam("acc_json", "acc_extra", "json_data=$avp(json_data);")
modparam("acc_json", "acc_json_pre_encoded_prefix", "json_")
...
$avp(json_data) = '{"b":2, "c":3}';
...
</programlisting>
</example>
</section>

<section id="acc_json.p.acc_time_mode">
<title><varname>acc_time_mode</varname> (integer)</title>
<para>
Expand Down Expand Up @@ -338,7 +359,27 @@ modparam("acc_json", "cdr_extra", "ci=$dlg_var(call_id);ft=$dlg_var(from_tag)")
</example>
</section>


<section id="acc.p.cdr_json_pre_encoded_prefix">
<title><varname>cdr_json_pre_encoded_prefix</varname> (string)</title>
<para>
Prefix to identify values that will be considered to be already json encoded.
</para>
<para>
Default value is NULL.
</para>
<example>
<title>cdr_json_pre_encoded_prefix example</title>
<programlisting format="linespecific">
...
modparam("acc_json", "cdr_extra", "json_data=$avp(json_data);")
modparam("acc_json", "cdr_json_pre_encoded_prefix", "json_")
...
$avp(json_data) = '{"b":2, "c":3}';
...
</programlisting>
</example>
</section>

<section id="acc.p.cdr_expired_dlg_enable">
<title><varname>cdr_expired_dlg_enable</varname> (str)</title>
<para>
Expand Down

0 comments on commit c45d65b

Please sign in to comment.