Module: kamailio
Branch: master
Commit: bf6d926d5d22f99c1a2776aaeb9ec705b58592c5
URL:
https://github.com/kamailio/kamailio/commit/bf6d926d5d22f99c1a2776aaeb9ec70…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2018-08-22T16:50:22+02:00
jansson: exported jansson_get(...) to kemi framework
- jansson.get(...)
---
Modified: src/modules/jansson/jansson_funcs.c
Modified: src/modules/jansson/jansson_funcs.h
Modified: src/modules/jansson/jansson_mod.c
---
Diff:
https://github.com/kamailio/kamailio/commit/bf6d926d5d22f99c1a2776aaeb9ec70…
Patch:
https://github.com/kamailio/kamailio/commit/bf6d926d5d22f99c1a2776aaeb9ec70…
---
diff --git a/src/modules/jansson/jansson_funcs.c b/src/modules/jansson/jansson_funcs.c
index 9f82243114..012f1aba40 100644
--- a/src/modules/jansson/jansson_funcs.c
+++ b/src/modules/jansson/jansson_funcs.c
@@ -31,38 +31,23 @@
#include "jansson_funcs.h"
#include "jansson_utils.h"
-int janssonmod_get(struct sip_msg* msg, char* path_in, char* src_in, char* dst)
+int janssonmod_get_helper(sip_msg_t* msg, str *path_s, str *src_s, pv_spec_t *dst_pv)
{
- str src_s;
- str path_s;
- pv_spec_t *dst_pv;
- pv_value_t dst_val;
-
- if (fixup_get_svalue(msg, (gparam_p)src_in, &src_s) != 0) {
- ERR("cannot get json string value\n");
- return -1;
- }
-
- if (fixup_get_svalue(msg, (gparam_p)path_in, &path_s) != 0) {
- ERR("cannot get path string value\n");
- return -1;
- }
-
- dst_pv = (pv_spec_t *)dst;
+ pv_value_t dst_val;
json_t* json = NULL;
json_error_t parsing_error;
- json = json_loads(src_s.s, JSON_REJECT_DUPLICATES, &parsing_error);
+ json = json_loads(src_s->s, JSON_REJECT_DUPLICATES, &parsing_error);
if(!json) {
- ERR("failed to parse: %.*s\n", src_s.len, src_s.s);
+ ERR("failed to parse: %.*s\n", src_s->len, src_s->s);
ERR("json error at line %d: %s\n",
parsing_error.line, parsing_error.text);
goto fail;
}
- char* path = path_s.s;
+ char* path = path_s->s;
json_t* v = json_path_get(json, path);
if(!v) {
@@ -87,6 +72,24 @@ int janssonmod_get(struct sip_msg* msg, char* path_in, char* src_in,
char* dst)
return -1;
}
+int janssonmod_get(struct sip_msg* msg, char* path_in, char* src_in, char* dst)
+{
+ str src_s;
+ str path_s;
+
+ if (fixup_get_svalue(msg, (gparam_p)src_in, &src_s) != 0) {
+ ERR("cannot get json string value\n");
+ return -1;
+ }
+
+ if (fixup_get_svalue(msg, (gparam_p)path_in, &path_s) != 0) {
+ ERR("cannot get path string value\n");
+ return -1;
+ }
+
+ return janssonmod_get_helper(msg, &path_s, &src_s, (pv_spec_t *)dst);
+}
+
#define STR_EQ_STATIC(a,b) ((a.len == sizeof(b)-1) && (strncmp(a.s, b,
sizeof(b)-1)==0))
int janssonmod_set(unsigned int append, struct sip_msg* msg, char* type_in,
diff --git a/src/modules/jansson/jansson_funcs.h b/src/modules/jansson/jansson_funcs.h
index 9373956520..abf3e15ba3 100644
--- a/src/modules/jansson/jansson_funcs.h
+++ b/src/modules/jansson/jansson_funcs.h
@@ -31,5 +31,7 @@ int janssonmod_set(unsigned int append, struct sip_msg* msg, char*
type_in,
char* path_in, char* value_in, char* result);
int janssonmod_array_size(struct sip_msg* msg, char* json_in,
char* path_in, char* dst);
+int janssonmod_get_helper(sip_msg_t* msg, str *path_s, str *src_s,
+ pv_spec_t *dst_pv);
#endif
diff --git a/src/modules/jansson/jansson_mod.c b/src/modules/jansson/jansson_mod.c
index a8404a241f..50c3d91292 100644
--- a/src/modules/jansson/jansson_mod.c
+++ b/src/modules/jansson/jansson_mod.c
@@ -25,6 +25,7 @@
#include "../../core/mod_fix.h"
#include "../../core/sr_module.h"
+#include "../../core/kemi.h"
#include "jansson_funcs.h"
#include "jansson_utils.h"
@@ -147,3 +148,46 @@ static int fixup_set_params_free(void** param, int param_no)
static int mod_init(void) {
return 0;
}
+
+/**
+ *
+ */
+static int ki_jansson_get(sip_msg_t *msg, str *spath, str *sdoc, str *spv)
+{
+ pv_spec_t *pvs = NULL;
+
+ pvs = pv_cache_get(spv);
+ if(pvs==NULL) {
+ LM_ERR("cannot get pv spec for [%.*s]\n", spv->len, spv->s);
+ return -1;
+ }
+
+ if(pvs->setf==NULL) {
+ LM_ERR("read only output var [%.*s]\n", spv->len, spv->s);
+ return -1;
+ }
+
+ return janssonmod_get_helper(msg, spath, sdoc, pvs);
+}
+
+/**
+ *
+ */
+static sr_kemi_t sr_kemi_jansson_exports[] = {
+ { str_init("jansson"), str_init("get"),
+ SR_KEMIP_INT, ki_jansson_get,
+ { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
+ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+ },
+
+ {{0, 0}, {0, 0}, 0, NULL, {0, 0, 0, 0, 0, 0}}
+};
+
+/**
+ *
+ */
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
+{
+ sr_kemi_modules_add(sr_kemi_jansson_exports);
+ return 0;
+}
\ No newline at end of file