[sr-dev] git:master: modules/xcap_server: fixed fetching of an element ( GET with XPath)

Peter Dunkley peter.dunkley at crocodile-rcs.com
Wed Oct 23 15:55:02 CEST 2013


Module: sip-router
Branch: master
Commit: 6b8b8a050e4490302d1f1f940a7fda95784cce46
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=6b8b8a050e4490302d1f1f940a7fda95784cce46

Author: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Committer: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Date:   Wed Oct 23 14:53:04 2013 +0100

modules/xcap_server: fixed fetching of an element (GET with XPath)

- This is a mandatory part of XCAP and DELETE/PUT of elements (using XPath)
  is supported in xcap_server.
- The code to get a node from a document was all in there but never called.

---

 modules/xcap_server/xcap_misc.c   |    6 +-
 modules/xcap_server/xcap_server.c |   81 ++++++++++++++++++++++++++++---------
 2 files changed, 64 insertions(+), 23 deletions(-)

diff --git a/modules/xcap_server/xcap_misc.c b/modules/xcap_server/xcap_misc.c
index 7ca891e..da8bb59 100644
--- a/modules/xcap_server/xcap_misc.c
+++ b/modules/xcap_server/xcap_misc.c
@@ -334,15 +334,15 @@ int xcaps_xpath_get(str *inbuf, str *xpaths, str *outbuf)
 		LM_ERR("unable to evaluate xpath expression [%s]\n", xpaths->s);
 		goto error;
 	}
-    nodes = xpathObj->nodesetval;
-	if(nodes==NULL)
+	nodes = xpathObj->nodesetval;
+	if(nodes==NULL || nodes->nodeNr==0 || nodes->nodeTab == NULL)
 	{
 		outbuf->len = 0;
 		outbuf->s[outbuf->len] = '\0';
 		goto done;
 	}
 	size = nodes->nodeNr;
-    p = outbuf->s;
+	p = outbuf->s;
 	end = outbuf->s + outbuf->len;
 	for(i = 0; i < size; ++i)
 	{
diff --git a/modules/xcap_server/xcap_server.c b/modules/xcap_server/xcap_server.c
index 8bc14eb..372fc39 100644
--- a/modules/xcap_server/xcap_server.c
+++ b/modules/xcap_server/xcap_server.c
@@ -1070,7 +1070,8 @@ static int w_xcaps_get(sip_msg_t* msg, char* puri, char* ppath)
 	str uri;
 	str path;
 	str etag = {0, 0};
-	str body;
+	str body = {0, 0};
+	str new_body = {0, 0};
 	int ret = 0;
 	xcap_uri_t xuri;
 	str *ctype;
@@ -1170,37 +1171,77 @@ static int w_xcaps_get(sip_msg_t* msg, char* puri, char* ppath)
 			LM_ERR("could not fetch xcap document\n");
 			goto error;
 		}
-
-		if(ret==0)
+		if(ret!=0)
 		{
-			/* doc found */
-			ctype = &xcaps_str_appxml;
-			if(xuri.type==RESOURCE_LIST)
-				ctype = &xcaps_str_apprlxml;
-			else if(xuri.type==PRES_RULES)
-				ctype = &xcaps_str_appapxml;
-			else if(xuri.type==RLS_SERVICE)
-				ctype = &xcaps_str_apprsxml;
-			else if(xuri.type==USER_PROFILE)
-				ctype = &xcaps_str_appupxml;
-			else if(xuri.type==PRES_CONTENT)
-				ctype = &xcaps_str_apppcxml;
-			else if(xuri.type==PIDF_MANIPULATION)
-				ctype = &xcaps_str_apppdxml;
-			xcaps_send_reply(msg, 200, &xcaps_str_ok, &etag,
-					ctype, &body);
-		} else {
 			/* doc not found */
 			xcaps_send_reply(msg, 404, &xcaps_str_notfound, NULL,
 					NULL, NULL);
+			break;
+		}
+
+		if(xuri.nss!=NULL && xuri.node.len>0)
+		{
+			if((new_body.s = pkg_malloc(body.len))==NULL)
+			{
+				LM_ERR("allocating package memory\n");
+				goto error;
+			}
+			new_body.len = body.len;
+			
+			if(xcaps_xpath_hack(&body, 0)<0)
+			{
+				LM_ERR("could not hack xcap document\n");
+				goto error;
+			}
+			if(xcaps_xpath_get(&body, &xuri.node, &new_body)<0)
+			{
+				LM_ERR("could not retrieve element from xcap document\n");
+				goto error;
+			}
+			if(new_body.len<=0)
+			{
+				/* element not found */
+				xcaps_send_reply(msg, 404, &xcaps_str_notfound, NULL,
+					NULL, NULL);
+				pkg_free(new_body.s);
+				new_body.s = NULL;
+				break;
+			}
+			if(xcaps_xpath_hack(&new_body, 1)<0)
+			{
+				LM_ERR("could not hack xcap document\n");
+				goto error;
+			}
+			memcpy(body.s, new_body.s, new_body.len);
+			body.len = new_body.len;
+			pkg_free(new_body.s);
+			new_body.s = NULL;
 		}
 
+		/* doc or element found */
+		ctype = &xcaps_str_appxml;
+		if(xuri.type==RESOURCE_LIST)
+			ctype = &xcaps_str_apprlxml;
+		else if(xuri.type==PRES_RULES)
+			ctype = &xcaps_str_appapxml;
+		else if(xuri.type==RLS_SERVICE)
+			ctype = &xcaps_str_apprsxml;
+		else if(xuri.type==USER_PROFILE)
+			ctype = &xcaps_str_appupxml;
+		else if(xuri.type==PRES_CONTENT)
+			ctype = &xcaps_str_apppcxml;
+		else if(xuri.type==PIDF_MANIPULATION)
+			ctype = &xcaps_str_apppdxml;
+		xcaps_send_reply(msg, 200, &xcaps_str_ok, &etag,
+				ctype, &body);
+
 		break;
 	}
 
 	return 1;
 
 error:
+	if (new_body.s) pkg_free(new_body.s);
 	xcaps_send_reply(msg, 500, &xcaps_str_srverr, NULL,
 				NULL, NULL);
 	return -1;




More information about the sr-dev mailing list