ソースを参照

Added json module, providing a simple json parser.

Matthew Williams 14 年 前
コミット
580afbaa92

+ 4 - 0
modules/json/.gitignore

@@ -0,0 +1,4 @@
+*.d
+*.o
+*.so
+*.lst

+ 21 - 0
modules/json/Makefile

@@ -0,0 +1,21 @@
+#
+# json module makefile
+#
+# 
+# WARNING: do not run this directly, it should be run by the master Makefile
+
+include ../../Makefile.defs
+auto_gen=
+NAME=json.so
+LIBS=
+
+DEFS+=-I/usr/include/json -I$(LOCALBASE)/include/json \
+       -I$(LOCALBASE)/include
+LIBS+=-L$(SYSBASE)/include/lib -L$(LOCALBASE)/lib -ljson
+ 
+DEFS+=-DOPENSER_MOD_INTERFACE
+
+SERLIBPATH=../../lib
+# SER_LIBS+=$(SERLIBPATH)/srdb1/srdb1
+# SER_LIBS+=$(SERLIBPATH)/kmi/kmi
+include ../../Makefile.modules

+ 95 - 0
modules/json/README

@@ -0,0 +1,95 @@
+JSON Module
+
+Matthew Williams
+
+   <[email protected]>
+
+Edited by
+
+Matthew Williams
+
+   <[email protected]>
+
+   Copyright (c) 2011 FlowRoute LLC (flowroute.com)
+     __________________________________________________________________
+
+   Table of Contents
+
+   1. Admin Guide
+
+        1. Overview
+        2. Dependencies
+
+              2.1. Kamailio Modules
+              2.2. External Libraries or Applications
+
+        3. Exported Parameters
+
+              3.1.
+
+        4. Exported Functions
+
+              4.1. json_get_field(json_string, field_name, destination)
+
+   List of Examples
+
+   1.1. json_get_field usage
+
+Chapter 1. Admin Guide
+
+   Table of Contents
+
+   1. Overview
+   2. Dependencies
+
+        2.1. Kamailio Modules
+        2.2. External Libraries or Applications
+
+   3. Exported Parameters
+
+        3.1.
+
+   4. Exported Functions
+
+        4.1. json_get_field(json_string, field_name, destination)
+
+1. Overview
+
+   This module provides operations on json strings.
+
+2. Dependencies
+
+   2.1. Kamailio Modules
+   2.2. External Libraries or Applications
+
+2.1. Kamailio Modules
+
+   The following modules must be loaded before this module:
+     * None
+
+2.2. External Libraries or Applications
+
+   The following libraries or applications must be installed before
+   running Kamailio with this module loaded:
+     * libjson (http://metaparadigm.com/json-c/)
+
+3. Exported Parameters
+
+   3.1.
+
+   None
+
+4. Exported Functions
+
+   4.1. json_get_field(json_string, field_name, destination)
+
+4.1.  json_get_field(json_string, field_name, destination)
+
+   Copy field 'field_name' from json object 'json_string' and store it in
+   pvar 'destination'.
+
+   Example 1.1. json_get_field usage
+...
+json_get_field("{'foo':'bar'}", "foo", "$var(foo)");
+xlog("foo is $var(foo)");
+...

+ 4 - 0
modules/json/doc/Makefile

@@ -0,0 +1,4 @@
+docs = json.xml
+
+docbook_dir = ../../../docbook
+include $(docbook_dir)/Makefile.module

+ 37 - 0
modules/json/doc/json.xml

@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding='ISO-8859-1'?>
+<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
+"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
+
+<!-- Include general documentation entities -->
+<!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
+%docentities;
+
+]>
+
+<book xmlns:xi="http://www.w3.org/2001/XInclude">
+    <bookinfo>
+	<title>JSON Module</title>
+	<productname class="trade">sip-router.org</productname>
+	<authorgroup>
+	    <author>
+		<firstname>Matthew</firstname>
+		<surname>Williams</surname>
+		<email>[email protected]</email>
+	    </author>
+	    <editor>
+			<firstname>Matthew</firstname>
+			<surname>Williams</surname>
+			<email>[email protected]</email>
+	    </editor>
+	</authorgroup>
+	<copyright>
+	    <year>2011</year>
+	    <holder>Flowroute LLC (flowroute.com)</holder>
+	</copyright>
+    </bookinfo>
+    <toc></toc>
+    
+    <xi:include href="json_admin.xml"/>
+    
+    
+</book>

+ 83 - 0
modules/json/doc/json_admin.xml

@@ -0,0 +1,83 @@
+<?xml version="1.0" encoding='ISO-8859-1'?>
+<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
+"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
+
+<!-- Include general documentation entities -->
+<!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
+%docentities;
+
+]>
+<!-- Module User's Guide -->
+
+<chapter>
+	
+	<title>&adminguide;</title>
+	
+	<section>
+	<title>Overview</title>
+	<para>
+		This module provides operations on json strings.
+	</para>
+	</section>
+
+	<section>
+	<title>Dependencies</title>
+	<section>
+		<title>&kamailio; Modules</title>
+		<para>
+		The following modules must be loaded before this module:
+			<itemizedlist>
+			<listitem>
+			<para>
+				<emphasis>None</emphasis>
+			</para>
+			</listitem>
+			</itemizedlist>
+		</para>
+	</section>
+	<section>
+		<title>External Libraries or Applications</title>
+		<para>
+		The following libraries or applications must be installed before running
+		&kamailio; with this module loaded:
+			<itemizedlist>
+			<listitem>
+			<para>
+				<emphasis>libjson (http://metaparadigm.com/json-c/)</emphasis>
+			</para>
+			</listitem>
+			</itemizedlist>
+		</para>
+	</section>
+	</section>
+	<section>
+	<title>Exported Parameters</title>
+	<section>
+		<para>
+			<emphasis>None</emphasis>
+		</para>
+	</section>
+	</section>
+
+	<section>
+	<title>Exported Functions</title>
+	<section>
+	    <title>
+		<function moreinfo="none">json_get_field(json_string, field_name, destination)</function>
+	    </title>
+	    <para>
+		Copy field 'field_name' from json object 'json_string' and store it in pvar 'destination'.
+		</para>
+		<example>
+		<title><function>json_get_field</function> usage</title>
+		<programlisting format="linespecific">
+...
+json_get_field("{'foo':'bar'}", "foo", "$var(foo)");
+xlog("foo is $var(foo)");
+...
+		</programlisting>
+	    </example>
+	</section>
+	</section>
+</chapter>
+

+ 62 - 0
modules/json/json_funcs.c

@@ -0,0 +1,62 @@
+/**
+ * $Id$
+ *
+ * Copyright (C) 2011 Flowroute LLC (flowroute.com)
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * This file 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
+ *
+ *
+ * This file 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 <stdio.h>
+#include <string.h>
+#include <json.h>
+
+#include "../../mod_fix.h"
+#include "../../lvalue.h"
+
+#include "json_funcs.h"
+
+int 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;
+	}
+	
+	dst_pv = (pv_spec_t *)dst;
+	
+	struct json_object *j = json_tokener_parse(json_s.s);
+	char *value = (char*)json_object_to_json_string(json_object_object_get(j, field_s.s));
+
+	dst_val.rs.s = value;
+	dst_val.rs.len = strlen(value);
+	dst_val.flags = PV_VAL_STR;
+	dst_pv->setf(msg, &dst_pv->pvp, (int)EQ_T, &dst_val);
+
+	return 1;
+}

+ 32 - 0
modules/json/json_funcs.h

@@ -0,0 +1,32 @@
+/**
+ * $Id$
+ *
+ * Copyright (C) 2011 Flowroute LLC (flowroute.com)
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * This file 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
+ *
+ *
+ * This file 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
+ *
+ */
+
+#ifndef _JSON_FUNCS_H_
+#define _JSON_FUNCS_H_
+
+#include "../../parser/msg_parser.h"
+
+int json_get_field(struct sip_msg* msg, char* json, char* field, char* dst);
+
+#endif

+ 96 - 0
modules/json/json_mod.c

@@ -0,0 +1,96 @@
+/**
+ * $Id$
+ *
+ * Copyright (C) 2011 Flowroute LLC (flowroute.com)
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * This file 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
+ *
+ *
+ * This file 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 <stdio.h>
+#include <string.h>
+
+#include "../../mod_fix.h"
+#include "../../sr_module.h"
+
+#include "json_funcs.h"
+
+MODULE_VERSION
+
+static int fixup_get_field(void** param, int param_no);
+static int fixup_get_field_free(void** param, int param_no);
+
+/* Exported functions */
+static cmd_export_t cmds[]={
+	{"json_get_field", (cmd_function)json_get_field, 3, 
+		fixup_get_field, fixup_get_field_free, ANY_ROUTE},
+	{0, 0, 0, 0, 0, 0}
+};
+
+struct module_exports exports = {
+		"json",
+		DEFAULT_DLFLAGS, /* dlopen flags */
+		cmds,			 /* Exported functions */
+		0,		 /* Exported parameters */
+		0,		 /* exported statistics */
+		0,	             /* exported MI functions */
+		0,				 /* exported pseudo-variables */
+		0,				 /* extra processes */
+		0,        /* module initialization function */
+		0,				 /* response function*/
+		0,	 /* destroy function */
+		0       /* per-child init function */
+};
+
+
+static int fixup_get_field(void** param, int param_no)
+{
+  if (param_no == 1 || param_no == 2) {
+		return fixup_spve_null(param, 1);
+	}
+
+	if (param_no == 3) {
+		if (fixup_pvar_null(param, 1) != 0) {
+		    LM_ERR("failed to fixup result pvar\n");
+		    return -1;
+		}
+		if (((pv_spec_t *)(*param))->setf == NULL) {
+		    LM_ERR("result pvar is not writeble\n");
+		    return -1;
+		}
+		return 0;
+	}
+	
+	LM_ERR("invalid parameter number <%d>\n", param_no);
+	return -1;
+}
+
+static int fixup_get_field_free(void** param, int param_no)
+{
+	if (param_no == 1 || param_no == 2) {
+		LM_WARN("free function has not been defined for spve\n");
+		return 0;
+	}
+
+	if (param_no == 3) {
+		return fixup_free_pvar_null(param, 3);
+	}
+
+	LM_ERR("invalid parameter number <%d>\n", param_no);
+	return -1;
+}