Browse code

jansson: exported jansson_get(...) to kemi framework

- jansson.get(...)

Daniel-Constantin Mierla authored on 22/08/2018 14:49:32
Showing 3 changed files
... ...
@@ -31,38 +31,23 @@
31 31
 #include "jansson_funcs.h"
32 32
 #include "jansson_utils.h"
33 33
 
34
-int janssonmod_get(struct sip_msg* msg, char* path_in, char* src_in, char* dst)
34
+int janssonmod_get_helper(sip_msg_t* msg, str *path_s, str *src_s, pv_spec_t *dst_pv)
35 35
 {
36
-	str src_s;
37
-	str path_s;
38
-	pv_spec_t *dst_pv;
39
-	pv_value_t dst_val;
40
-
41
-	if (fixup_get_svalue(msg, (gparam_p)src_in, &src_s) != 0) {
42
-		ERR("cannot get json string value\n");
43
-		return -1;
44
-	}
45
-
46
-	if (fixup_get_svalue(msg, (gparam_p)path_in, &path_s) != 0) {
47
-		ERR("cannot get path string value\n");
48
-		return -1;
49
-	}
50
-
51
-	dst_pv = (pv_spec_t *)dst;
52 36
 
37
+	pv_value_t dst_val;
53 38
 	json_t* json = NULL;
54 39
 	json_error_t parsing_error;
55 40
 
56
-	json = json_loads(src_s.s, JSON_REJECT_DUPLICATES, &parsing_error);
41
+	json = json_loads(src_s->s, JSON_REJECT_DUPLICATES, &parsing_error);
57 42
 
58 43
 	if(!json) {
59
-		ERR("failed to parse: %.*s\n", src_s.len, src_s.s);
44
+		ERR("failed to parse: %.*s\n", src_s->len, src_s->s);
60 45
 		ERR("json error at line %d: %s\n",
61 46
 				parsing_error.line, parsing_error.text);
62 47
 		goto fail;
63 48
 	}
64 49
 
65
-	char* path = path_s.s;
50
+	char* path = path_s->s;
66 51
 
67 52
 	json_t* v = json_path_get(json, path);
68 53
 	if(!v) {
... ...
@@ -87,6 +72,24 @@ fail:
87 72
 	return -1;
88 73
 }
89 74
 
75
+int janssonmod_get(struct sip_msg* msg, char* path_in, char* src_in, char* dst)
76
+{
77
+	str src_s;
78
+	str path_s;
79
+
80
+	if (fixup_get_svalue(msg, (gparam_p)src_in, &src_s) != 0) {
81
+		ERR("cannot get json string value\n");
82
+		return -1;
83
+	}
84
+
85
+	if (fixup_get_svalue(msg, (gparam_p)path_in, &path_s) != 0) {
86
+		ERR("cannot get path string value\n");
87
+		return -1;
88
+	}
89
+
90
+	return janssonmod_get_helper(msg, &path_s, &src_s, (pv_spec_t *)dst);
91
+}
92
+
90 93
 #define STR_EQ_STATIC(a,b) ((a.len == sizeof(b)-1) && (strncmp(a.s, b, sizeof(b)-1)==0))
91 94
 
92 95
 int janssonmod_set(unsigned int append, struct sip_msg* msg, char* type_in,
... ...
@@ -31,5 +31,7 @@ int janssonmod_set(unsigned int append, struct sip_msg* msg, char* type_in,
31 31
 		char* path_in, char* value_in, char* result);
32 32
 int janssonmod_array_size(struct sip_msg* msg, char* json_in,
33 33
 		char* path_in, char* dst);
34
+int janssonmod_get_helper(sip_msg_t* msg, str *path_s, str *src_s,
35
+		pv_spec_t *dst_pv);
34 36
 
35 37
 #endif
... ...
@@ -25,6 +25,7 @@
25 25
 
26 26
 #include "../../core/mod_fix.h"
27 27
 #include "../../core/sr_module.h"
28
+#include "../../core/kemi.h"
28 29
 
29 30
 #include "jansson_funcs.h"
30 31
 #include "jansson_utils.h"
... ...
@@ -147,3 +148,46 @@ static int fixup_set_params_free(void** param, int param_no)
147 148
 static int mod_init(void) {
148 149
 	return 0;
149 150
 }
151
+
152
+/**
153
+ *
154
+ */
155
+static int ki_jansson_get(sip_msg_t *msg, str *spath, str *sdoc, str *spv)
156
+{
157
+	pv_spec_t *pvs = NULL;
158
+
159
+	pvs = pv_cache_get(spv);
160
+	if(pvs==NULL) {
161
+		LM_ERR("cannot get pv spec for [%.*s]\n", spv->len, spv->s);
162
+		return -1;
163
+	}
164
+
165
+	if(pvs->setf==NULL) {
166
+		LM_ERR("read only output var [%.*s]\n", spv->len, spv->s);
167
+		return -1;
168
+	}
169
+
170
+	return janssonmod_get_helper(msg, spath, sdoc, pvs);
171
+}
172
+
173
+/**
174
+ *
175
+ */
176
+static sr_kemi_t sr_kemi_jansson_exports[] = {
177
+	{ str_init("jansson"), str_init("get"),
178
+		SR_KEMIP_INT, ki_jansson_get,
179
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
180
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
181
+	},
182
+
183
+	{{0, 0}, {0, 0}, 0, NULL, {0, 0, 0, 0, 0, 0}}
184
+};
185
+
186
+/**
187
+ *
188
+ */
189
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
190
+{
191
+	sr_kemi_modules_add(sr_kemi_jansson_exports);
192
+	return 0;
193
+}
150 194
\ No newline at end of file