Browse code

acc_json: param to accept pre-encoded json values

Julien Chavanton authored on 29/03/2021 23:12:34 • Julien Chavanton committed on 31/03/2021 14:54:58
Showing 2 changed files
... ...
@@ -64,6 +64,8 @@ int acc_output_syslog = -1;
64 64
 char *acc_output_mqueue_str = 0; /* see mqueue module queue name */
65 65
 str acc_q_name = {0, 0};
66 66
 static char *acc_log_facility_str = 0;
67
+static char *acc_json_pre_encoded_prefix_str = 0;
68
+str acc_json_pre_encoded_prefix = {0, 0};
67 69
 
68 70
 int cdr_enable  = 0;
69 71
 static char *cdr_extra_str = 0;
... ...
@@ -73,6 +75,8 @@ int cdr_output_syslog = -1;
73 75
 char *cdr_output_mqueue_str = 0; /* see mqueue module queue name */
74 76
 str cdr_q_name = {0, 0};
75 77
 static char *cdr_log_facility_str = 0;
78
+static char *cdr_json_pre_encoded_prefix_str = 0;
79
+str cdr_json_pre_encoded_prefix = {0, 0};
76 80
 
77 81
 static cmd_export_t cmds[] = {{0, 0, 0, 0, 0, 0}};
78 82
 
... ...
@@ -80,6 +84,7 @@ static cmd_export_t cmds[] = {{0, 0, 0, 0, 0, 0}};
80 84
 static param_export_t params[] = {{"acc_flag", INT_PARAM, &acc_flag},
81 85
 		{"acc_missed_flag", INT_PARAM, &acc_missed_flag},
82 86
 		{"acc_extra", PARAM_STRING, &acc_extra_str},
87
+		{"acc_pre_encoded_prefix", PARAM_STRING, &acc_json_pre_encoded_prefix_str},
83 88
 		{"acc_time_mode", INT_PARAM, &acc_time_mode},
84 89
 		{"acc_time_format", PARAM_STRING, &acc_time_format},
85 90
 		{"acc_log_level", INT_PARAM, &acc_log_level},
... ...
@@ -87,6 +92,7 @@ static param_export_t params[] = {{"acc_flag", INT_PARAM, &acc_flag},
87 92
 		{"acc_output_mqueue", PARAM_STRING, &acc_output_mqueue_str},
88 93
 		{"acc_output_syslog", INT_PARAM, &acc_output_syslog},
89 94
 		{"cdr_extra", PARAM_STRING, &cdr_extra_str},
95
+		{"cdr_pre_encoded_prefix", PARAM_STRING, &cdr_json_pre_encoded_prefix_str},
90 96
 		{"cdr_enable", INT_PARAM, &cdr_enable},
91 97
 		{"cdr_expired_dlg_enable", INT_PARAM, &cdr_expired_dlg_enable},
92 98
 		{"cdr_log_level", INT_PARAM, &cdr_log_level},
... ...
@@ -150,6 +156,15 @@ static int mod_init(void)
150 156
 			return -1;
151 157
 		}
152 158
 	}
159
+	/* prefix to handle pre-encoded json fields */
160
+	if (acc_json_pre_encoded_prefix_str) {
161
+		acc_json_pre_encoded_prefix.s = acc_json_pre_encoded_prefix_str;
162
+		acc_json_pre_encoded_prefix.len = strlen(acc_json_pre_encoded_prefix_str);
163
+	}
164
+	if (cdr_json_pre_encoded_prefix_str) {
165
+		cdr_json_pre_encoded_prefix.s = cdr_json_pre_encoded_prefix_str;
166
+		cdr_json_pre_encoded_prefix.len = strlen(cdr_json_pre_encoded_prefix_str);
167
+	}
153 168
 
154 169
 	/* load the MQUEUE API */
155 170
 	if((acc_output_mqueue_str || cdr_output_mqueue_str) && (load_mq_api(&mq_api) != 0)) {
... ...
@@ -315,9 +330,23 @@ int acc_json_send_request(struct sip_msg *req, acc_info_t *inf)
315 330
 		LM_DBG("[%d][%s][%.*s]\n", i, extra->name.s, inf->varr[i].len,
316 331
 				inf->varr[i].s);
317 332
 		char *tmp = strndup(inf->varr[i].s, inf->varr[i].len);
318
-		json_t *value = json_string(tmp);
319
-		if(!value)
320
-			value = json_string("NON-UTF8");
333
+		json_t *value;
334
+		if (acc_json_pre_encoded_prefix.len && inf->varr[i].len
335
+				&& extra->name.len>=acc_json_pre_encoded_prefix.len
336
+				&& strncmp(extra->name.s,acc_json_pre_encoded_prefix.s, sizeof(char)*acc_json_pre_encoded_prefix.len) == 0) {
337
+			LM_DBG("[%d][%s] json content \n", i, extra->name.s);
338
+			json_error_t jerr;
339
+			value = json_loads(tmp, 0, &jerr);
340
+			if (!value) {
341
+				LM_ERR("error loading json in[%s]: %s:%d:%d: %s\n", extra->name.s,
342
+				jerr.source, jerr.line, jerr.column, jerr.text);
343
+				value = json_string("INVALID_JSON");
344
+			}
345
+		} else {
346
+			value = json_string(tmp);
347
+			if(!value)
348
+				value = json_string("NON-UTF8");
349
+		}
321 350
 		json_object_set_new(object, extra->name.s, value);
322 351
 		free(tmp);
323 352
 		extra = extra->next;
... ...
@@ -439,9 +468,23 @@ int cdr_json_write(struct dlg_cell *dlg, struct sip_msg *req, cdr_info_t *inf)
439 468
 		LM_DBG("[%d][%s][%.*s]\n", i, extra->name.s, inf->varr[i].len,
440 469
 						inf->varr[i].s);
441 470
 		char *tmp = strndup(inf->varr[i].s, inf->varr[i].len);
442
-		json_t *value = json_string(tmp);
443
-		if(!value)
444
-			value = json_string("NON-UTF8");
471
+		json_t *value;
472
+		if (cdr_json_pre_encoded_prefix.len && inf->varr[i].len
473
+				&& extra->name.len>=cdr_json_pre_encoded_prefix.len
474
+				&& strncmp(extra->name.s,cdr_json_pre_encoded_prefix.s, sizeof(char)*cdr_json_pre_encoded_prefix.len) == 0) {
475
+			LM_DBG("[%d][%s] json content \n", i, extra->name.s);
476
+			json_error_t jerr;
477
+			value = json_loads(tmp, 0, &jerr);
478
+			if (!value) {
479
+				LM_ERR("error loading json in[%s]: %s:%d:%d: %s\n", extra->name.s,
480
+				jerr.source, jerr.line, jerr.column, jerr.text);
481
+				value = json_string("INVALID_JSON");
482
+			}
483
+		} else {
484
+			value = json_string(tmp);
485
+			if(!value)
486
+				value = json_string("NON-UTF8");
487
+		}
445 488
 		json_object_set_new(object, extra->name.s, value);
446 489
 		free(tmp);
447 490
 		extra = extra->next;
... ...
@@ -121,6 +121,27 @@ modparam("acc_json", "acc_extra", "via=$hdr(Via[*]); email=$avp(s:email)")
121 121
 		</example>
122 122
 	</section>
123 123
 
124
+	<section id="acc_json.p.acc_json_pre_encoded_prefix">
125
+		<title><varname>acc_json_pre_encoded_prefix</varname> (string)</title>
126
+		<para>
127
+		Prefix to identify values that will be considered to be already json encoded.
128
+		</para>
129
+		<para>
130
+		Default value is NULL.
131
+		</para>
132
+		<example>
133
+		<title>acc_json_pre_encoded_prefix example</title>
134
+		<programlisting format="linespecific">
135
+...
136
+modparam("acc_json", "acc_extra", "json_data=$avp(json_data);")
137
+modparam("acc_json", "acc_json_pre_encoded_prefix", "json_")
138
+...
139
+$avp(json_data) = '{"b":2, "c":3}';
140
+...
141
+</programlisting>
142
+		</example>
143
+	</section>
144
+
124 145
 	<section id="acc_json.p.acc_time_mode">
125 146
 	<title><varname>acc_time_mode</varname> (integer)</title>
126 147
 		<para>
... ...
@@ -338,7 +359,27 @@ modparam("acc_json", "cdr_extra", "ci=$dlg_var(call_id);ft=$dlg_var(from_tag)")
338 359
 		</example>
339 360
 	</section>
340 361
 
341
-	
362
+	<section id="acc.p.cdr_json_pre_encoded_prefix">
363
+		<title><varname>cdr_json_pre_encoded_prefix</varname> (string)</title>
364
+		<para>
365
+		Prefix to identify values that will be considered to be already json encoded.
366
+		</para>
367
+		<para>
368
+		Default value is NULL.
369
+		</para>
370
+		<example>
371
+		<title>cdr_json_pre_encoded_prefix example</title>
372
+		<programlisting format="linespecific">
373
+...
374
+modparam("acc_json", "cdr_extra", "json_data=$avp(json_data);")
375
+modparam("acc_json", "cdr_json_pre_encoded_prefix", "json_")
376
+...
377
+$avp(json_data) = '{"b":2, "c":3}';
378
+...
379
+</programlisting>
380
+		</example>
381
+	</section>
382
+
342 383
 	<section id="acc.p.cdr_expired_dlg_enable">
343 384
 		<title><varname>cdr_expired_dlg_enable</varname> (str)</title>
344 385
 		<para>