forked from abdelghafour/kamailio-nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsq_json.c
302 lines (250 loc) · 6.86 KB
/
nsq_json.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "nsq_json.h"
# define json_foreach_key(obj,key) \
char *key;\
struct lh_entry *entry ## key; \
struct lh_entry *entry_next ## key = NULL; \
for(entry ## key = json_object_get_object(obj)->head; \
(entry ## key ? ( \
key = (char*)entry ## key->k, \
entry_next ## key = entry ## key->next, \
entry ## key) : 0); \
entry ## key = entry_next ## key)
static str nsq_pv_str_empty = {"", 0};
char **str_split(char* a_str, const char a_delim)
{
char** result = 0;
size_t count = 0;
char* tmp = a_str;
char* last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
int len = 0;
/* Count how many elements will be extracted. */
while (*tmp) {
if (a_delim == *tmp) {
count++;
last_comma = tmp;
}
tmp++;
}
/* Add space for trailing token. */
count += last_comma < (a_str + strlen(a_str) - 1);
/* Add space for terminating null string so caller
knows where the list of returned strings ends. */
count++;
result = pkg_malloc(sizeof(char*) * count);
if (result) {
size_t idx = 0;
char* token = strtok(a_str, delim);
while (token) {
assert(idx < count);
len = strlen(token);
char* ptr = pkg_malloc( (len+1) * sizeof(char));
*(result + idx) = ptr;
memcpy(ptr, token, len);
ptr[len] = '\0';
int i = 0;
while(i < len) {
if (ptr[i] == nsq_json_escape_char)
ptr[i] = '.';
i++;
}
token = strtok(0, delim);
idx++;
}
assert(idx == count - 1);
*(result + idx) = 0;
}
return result;
}
struct json_object * nsq_json_get_field_object(str* json, str* field)
{
char** tokens;
char* dup;
char f1[25], f2[25];//, f3[25];
int i;
dup = pkg_malloc(json->len+1);
memcpy(dup, json->s, json->len);
dup[json->len] = '\0';
struct json_object *j = json_tokener_parse(dup);
pkg_free(dup);
if (is_error(j)) {
LM_ERR("empty or invalid JSON\n");
return NULL;
}
struct json_object *jtree = NULL;
struct json_object *ret = NULL;
LM_DBG("getting json %.*s\n", field->len, field->s);
dup = pkg_malloc(field->len+1);
memcpy(dup, field->s, field->len);
dup[field->len] = '\0';
tokens = str_split(dup, '.');
pkg_free(dup);
if (tokens) {
jtree = j;
for (i = 0; *(tokens + i); i++) {
if (jtree != NULL) {
str field = str_init(*(tokens + i));
// check for idx []
int sresult = sscanf(field.s, "%[^[][%[^]]]", f1, f2); //, f3);
LM_DBG("CHECK IDX %d - %s , %s, %s\n", sresult, field.s, f1, (sresult > 1? f2 : "(null)"));
jtree = nsq_json_get_object(jtree, f1);
if (jtree != NULL) {
char *value = (char*)json_object_get_string(jtree);
LM_DBG("JTREE OK %s\n", value);
}
if (jtree != NULL && sresult > 1 && json_object_is_type(jtree, json_type_array)) {
int idx = atoi(f2);
jtree = json_object_array_get_idx(jtree, idx);
if (jtree != NULL) {
char *value = (char*)json_object_get_string(jtree);
LM_DBG("JTREE IDX OK %s\n", value);
}
}
}
pkg_free(*(tokens + i));
}
pkg_free(tokens);
}
if (jtree != NULL)
ret = json_object_get(jtree);
json_object_put(j);
return ret;
}
int nsq_json_get_field_ex(str* json, str* field, pv_value_p dst_val)
{
struct json_object *jtree = nsq_json_get_field_object(json, field);
if (jtree != NULL) {
char *value = (char*)json_object_get_string(jtree);
int len = strlen(value);
dst_val->rs.s = pkg_malloc(len+1);
memcpy(dst_val->rs.s, value, len);
dst_val->rs.s[len] = '\0';
dst_val->rs.len = len;
dst_val->flags = PV_VAL_STR | PV_VAL_PKG;
dst_val->ri = 0;
json_object_put(jtree);
} else {
dst_val->flags = PV_VAL_NULL;
dst_val->rs = nsq_pv_str_empty;
dst_val->ri = 0;
}
return 1;
}
int nsq_json_get_field(struct sip_msg* msg, char* json, char* field, char* dst)
{
str json_s;
str field_s;
pv_spec_t *dst_pv;
pv_value_t dst_val;
if (fixup_get_svalue(msg, (gparam_p)json, &json_s) != 0) {
LM_ERR("cannot get json string value\n");
return -1;
}
if (fixup_get_svalue(msg, (gparam_p)field, &field_s) != 0) {
LM_ERR("cannot get field string value\n");
return -1;
}
if (nsq_json_get_field_ex(&json_s, &field_s, &dst_val) != 1)
return -1;
dst_pv = (pv_spec_t *)dst;
dst_pv->setf(msg, &dst_pv->pvp, (int)EQ_T, &dst_val);
if (dst_val.flags & PV_VAL_PKG) {
pkg_free(dst_val.rs.s);
} else if (dst_val.flags & PV_VAL_SHM) {
shm_free(dst_val.rs.s);
}
return 1;
}
struct json_object* nsq_json_parse(const char *str)
{
struct json_tokener* tok;
struct json_object* obj;
tok = json_tokener_new();
if (!tok) {
LM_ERR("Error parsing json: could not allocate tokener\n");
return NULL;
}
obj = json_tokener_parse_ex(tok, str, -1);
if (tok->err != json_tokener_success) {
LM_ERR("Error parsing json: %s\n", json_tokener_error_desc(tok->err));
LM_ERR("%s\n", str);
if (obj != NULL) {
json_object_put(obj);
}
obj = NULL;
}
json_tokener_free(tok);
return obj;
}
struct json_object* nsq_json_get_object(struct json_object* jso, const char *key)
{
struct json_object *result = NULL;
json_object_object_get_ex(jso, key, &result);
return result;
}
int nsq_json_get_keys(struct sip_msg* msg, char* json, char* field, char* dst)
{
str json_s;
str field_s;
int_str keys_avp_name;
unsigned short keys_avp_type;
pv_spec_t *avp_spec;
if (fixup_get_svalue(msg, (gparam_p)json, &json_s) != 0) {
LM_ERR("cannot get json string value\n");
return -1;
}
if (fixup_get_svalue(msg, (gparam_p)field, &field_s) != 0) {
LM_ERR("cannot get field string value\n");
return -1;
}
if (dst == NULL){
LM_ERR("avp spec is null\n");
return -1;
}
avp_spec = (pv_spec_t *)dst;
if (avp_spec->type != PVT_AVP) {
LM_ERR("invalid avp spec\n");
return -1;
}
if (pv_get_avp_name(0, &avp_spec->pvp, &keys_avp_name, &keys_avp_type)!=0) {
LM_ERR("invalid AVP definition\n");
return -1;
}
struct json_object *jtree = nsq_json_get_field_object(&json_s, &field_s);
if (jtree != NULL) {
json_foreach_key(jtree, k) {
LM_DBG("ITERATING KEY %s\n", k);
int_str v1;
v1.s.s = k;
v1.s.len = strlen(k);
if (add_avp(AVP_VAL_STR|keys_avp_type, keys_avp_name, v1) < 0) {
LM_ERR("failed to create AVP\n");
json_object_put(jtree);
return -1;
}
}
json_object_put(jtree);
}
return 1;
}