2
0
Эх сурвалжийг харах

xcap_server: functions exported to kemi framework

Daniel-Constantin Mierla 7 жил өмнө
parent
commit
982eaff9d5

+ 161 - 92
src/modules/xcap_server/xcap_server.c

@@ -1,6 +1,4 @@
 /*
- * $Id$
- *
  * xcap_server module - builtin XCAP server
  *
  * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
@@ -46,6 +44,7 @@
 #include "../../modules/sl/sl.h"
 #include "../../core/strutils.h"
 #include "../../core/ip_addr.h"
+#include "../../core/kemi.h"
 
 #include "xcap_misc.h"
 
@@ -492,59 +491,39 @@ static str xcaps_str_appdrxml	= {"application/vnd.oma.xcap-directory+xml", 38};
 /**
  *
  */
-static int w_xcaps_put(sip_msg_t* msg, char* puri, char* ppath,
-		char* pbody)
+static int ki_xcaps_put(sip_msg_t* msg, str* uri, str* path,
+		str* pbody)
 {
 	struct sip_uri turi;
-	str uri;
-	str path;
 	str body = {0, 0};
 	str etag = {0, 0};
 	str etag_hdr = {0, 0};
 	str tbuf;
 	str nbuf = {0, 0};
 	str allow = {0, 0};
-	pv_elem_t *xm;
 	xcap_uri_t xuri;
 
-	if(puri==0 || ppath==0 || pbody==0)
-	{
-		LM_ERR("invalid parameters\n");
-		goto error;
-	}
-
-	if(fixup_get_svalue(msg, (gparam_p)puri, &uri)!=0)
-	{
-		LM_ERR("unable to get uri\n");
-		goto error;
-	}
-	if(uri.s==NULL || uri.len == 0)
+	if(uri->s==NULL || uri->len == 0)
 	{
 		LM_ERR("invalid uri parameter\n");
 		goto error;
 	}
 
-	if(fixup_get_svalue(msg, (gparam_p)ppath, &path)!=0)
-	{
-		LM_ERR("unable to get path\n");
-		goto error;
-	}
-	if(path.s==NULL || path.len == 0)
+	if(path->s==NULL || path->len == 0)
 	{
 		LM_ERR("invalid path parameter\n");
 		return -1;
 	}
 
-	if(parse_uri(uri.s, uri.len, &turi)!=0)
+	if(parse_uri(uri->s, uri->len, &turi)!=0)
 	{
-		LM_ERR("parsing uri parameter\n");
+		LM_ERR("parsing uri parameter [%.*s]\n", uri->len, uri->s);
 		goto error;
 	}
 
-	if(xcap_parse_uri(&path, &xcaps_root, &xuri)<0)
+	if(xcap_parse_uri(path, &xcaps_root, &xuri)<0)
 	{
-		LM_ERR("cannot parse xcap uri [%.*s]\n",
-				path.len, path.s);
+		LM_ERR("cannot parse xcap uri [%.*s]\n", path->len, path->s);
 		goto error;
 	}
 
@@ -562,25 +541,19 @@ static int w_xcaps_put(sip_msg_t* msg, char* puri, char* ppath,
 		xcaps_send_reply(msg, 405, &xcaps_str_notallowed, &allow, NULL, NULL);
 		break;
 	default:
-		xm = (pv_elem_t*)pbody;
-		body.len = xcaps_buf.len - 1;
-		if(pv_printf(msg, xm, xcaps_buf.s, &body.len)<0)
-		{
-			LM_ERR("unable to get body\n");
-			goto error;
-		}
-		if(body.len <= 0)
+		if(pbody->len <= 0)
 		{
 			LM_ERR("invalid body parameter\n");
 			goto error;
 		}
-		body.s = (char*)pkg_malloc(body.len+1);
+		body.s = (char*)pkg_malloc(pbody->len+1);
 		if(body.s==NULL)
 		{
 			LM_ERR("no more pkg\n");
 			goto error;
 		}
-		memcpy(body.s, xcaps_buf.s, body.len);
+		memcpy(body.s, pbody->s, pbody->len);
+		body.len = pbody->len;
 		body.s[body.len] = '\0';
 
 		xcaps_get_db_etag(&turi.user, &turi.host, &xuri, &etag);
@@ -657,6 +630,50 @@ error:
 	return -1;
 }
 
+/**
+ *
+ */
+static int w_xcaps_put(sip_msg_t* msg, char* puri, char* ppath,
+		char* pbody)
+{
+	str uri;
+	str path;
+	str body = {0, 0};
+	pv_elem_t *xm;
+
+	if(puri==0 || ppath==0 || pbody==0)
+	{
+		LM_ERR("invalid parameters\n");
+		goto error;
+	}
+
+	if(fixup_get_svalue(msg, (gparam_p)puri, &uri)!=0)
+	{
+		LM_ERR("unable to get uri\n");
+		goto error;
+	}
+
+	if(fixup_get_svalue(msg, (gparam_p)ppath, &path)!=0)
+	{
+		LM_ERR("unable to get path\n");
+		goto error;
+	}
+
+	xm = (pv_elem_t*)pbody;
+	body.len = xcaps_buf.len - 1;
+	if(pv_printf(msg, xm, xcaps_buf.s, &body.len)<0)
+	{
+		LM_ERR("unable to get body\n");
+		goto error;
+	}
+	body.s = xcaps_buf.s;
+
+	return ki_xcaps_put(msg, &uri, &path, &body);
+
+error:
+	return -1;
+}
+
 /**
  *
  */
@@ -1059,11 +1076,9 @@ error:
 /**
  *
  */
-static int w_xcaps_get(sip_msg_t* msg, char* puri, char* ppath)
+static int ki_xcaps_get(sip_msg_t* msg, str* uri, str* path)
 {
 	struct sip_uri turi;
-	str uri;
-	str path;
 	str etag = {0, 0};
 	str body = {0, 0};
 	str new_body = {0, 0};
@@ -1072,44 +1087,27 @@ static int w_xcaps_get(sip_msg_t* msg, char* puri, char* ppath)
 	str *ctype;
 	str allow;
 
-	if(puri==0 || ppath==0)
-	{
-		LM_ERR("invalid parameters\n");
-		return -1;
-	}
-
-	if(fixup_get_svalue(msg, (gparam_p)puri, &uri)!=0)
-	{
-		LM_ERR("unable to get uri\n");
-		return -1;
-	}
-	if(uri.s==NULL || uri.len == 0)
+	if(uri->s==NULL || uri->len == 0)
 	{
 		LM_ERR("invalid uri parameter\n");
 		return -1;
 	}
 
-	if(fixup_get_svalue(msg, (gparam_p)ppath, &path)!=0)
-	{
-		LM_ERR("unable to get path\n");
-		return -1;
-	}
-	if(path.s==NULL || path.len == 0)
+	if(path->s==NULL || path->len == 0)
 	{
 		LM_ERR("invalid path parameter\n");
 		return -1;
 	}
 
-	if(parse_uri(uri.s, uri.len, &turi)!=0)
+	if(parse_uri(uri->s, uri->len, &turi)!=0)
 	{
-		LM_ERR("parsing uri parameter\n");
+		LM_ERR("parsing uri parameter [%.*s]\n", uri->len, uri->s);
 		goto error;
 	}
 
-	if(xcap_parse_uri(&path, &xcaps_root, &xuri)<0)
+	if(xcap_parse_uri(path, &xcaps_root, &xuri)<0)
 	{
-		LM_ERR("cannot parse xcap uri [%.*s]\n",
-				path.len, path.s);
+		LM_ERR("cannot parse xcap uri [%.*s]\n", path->len, path->s);
 		goto error;
 	}
 
@@ -1242,6 +1240,33 @@ error:
 	return -1;
 }
 
+/**
+ *
+ */
+static int w_xcaps_get(sip_msg_t* msg, char* puri, char* ppath)
+{
+	str uri;
+	str path;
+
+	if(puri==0 || ppath==0)
+	{
+		LM_ERR("invalid parameters\n");
+		return -1;
+	}
+
+	if(fixup_get_svalue(msg, (gparam_p)puri, &uri)!=0)
+	{
+		LM_ERR("unable to get uri\n");
+		return -1;
+	}
+
+	if(fixup_get_svalue(msg, (gparam_p)ppath, &path)!=0)
+	{
+		LM_ERR("unable to get path\n");
+		return -1;
+	}
+	return ki_xcaps_get(msg, &uri, &path);
+}
 
 /**
  *
@@ -1293,11 +1318,9 @@ error:
 /**
  *
  */
-static int w_xcaps_del(sip_msg_t* msg, char* puri, char* ppath)
+static int ki_xcaps_del(sip_msg_t* msg, str* uri, str* path)
 {
 	struct sip_uri turi;
-	str uri;
-	str path;
 	xcap_uri_t xuri;
 	str body = {0, 0};
 	str etag_hdr = {0, 0};
@@ -1305,44 +1328,27 @@ static int w_xcaps_del(sip_msg_t* msg, char* puri, char* ppath)
 	str tbuf;
 	str allow = {0, 0};
 
-	if(puri==0 || ppath==0)
-	{
-		LM_ERR("invalid parameters\n");
-		return -1;
-	}
-
-	if(fixup_get_svalue(msg, (gparam_p)puri, &uri)!=0)
-	{
-		LM_ERR("unable to get uri\n");
-		return -1;
-	}
-	if(uri.s==NULL || uri.len == 0)
+	if(uri->s==NULL || uri->len == 0)
 	{
 		LM_ERR("invalid uri parameter\n");
 		return -1;
 	}
 
-	if(fixup_get_svalue(msg, (gparam_p)ppath, &path)!=0)
-	{
-		LM_ERR("unable to get path\n");
-		return -1;
-	}
-	if(path.s==NULL || path.len == 0)
+	if(path->s==NULL || path->len == 0)
 	{
 		LM_ERR("invalid path parameter\n");
 		return -1;
 	}
 
-	if(parse_uri(uri.s, uri.len, &turi)!=0)
+	if(parse_uri(uri->s, uri->len, &turi)!=0)
 	{
-		LM_ERR("parsing uri parameter\n");
+		LM_ERR("parsing uri parameter [%.*s]\n", uri->len, uri->s);
 		goto error;
 	}
 
-	if(xcap_parse_uri(&path, &xcaps_root, &xuri)<0)
+	if(xcap_parse_uri(path, &xcaps_root, &xuri)<0)
 	{
-		LM_ERR("cannot parse xcap uri [%.*s]\n",
-				path.len, path.s);
+		LM_ERR("cannot parse xcap uri [%.*s]\n", path->len, path->s);
 		goto error;
 	}
 
@@ -1442,6 +1448,35 @@ error:
 	return -1;
 }
 
+/**
+ *
+ */
+static int w_xcaps_del(sip_msg_t* msg, char* puri, char* ppath)
+{
+	str uri;
+	str path;
+
+	if(puri==0 || ppath==0)
+	{
+		LM_ERR("invalid parameters\n");
+		return -1;
+	}
+
+	if(fixup_get_svalue(msg, (gparam_p)puri, &uri)!=0)
+	{
+		LM_ERR("unable to get uri\n");
+		return -1;
+	}
+
+	if(fixup_get_svalue(msg, (gparam_p)ppath, &path)!=0)
+	{
+		LM_ERR("unable to get path\n");
+		return -1;
+	}
+
+	return ki_xcaps_del(msg, &uri, &path);
+}
+
 /**
  *
  */
@@ -1671,3 +1706,37 @@ static int check_match_header(str body, str *etag)
 
 	return -1;
 }
+
+/**
+ *
+ */
+/* clang-format off */
+static sr_kemi_t sr_kemi_xcap_server_exports[] = {
+	{ str_init("xcap_server"), str_init("xcaps_put"),
+		SR_KEMIP_INT, ki_xcaps_put,
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_STR,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
+	{ str_init("xcap_server"), str_init("xcaps_get"),
+		SR_KEMIP_INT, ki_xcaps_get,
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
+	{ str_init("xcap_server"), str_init("xcaps_del"),
+		SR_KEMIP_INT, ki_xcaps_del,
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+		},
+
+	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
+};
+/* clang-format on */
+
+/**
+ *
+ */
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
+{
+	sr_kemi_modules_add(sr_kemi_xcap_server_exports);
+	return 0;
+}