Browse code

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.

Peter Dunkley authored on 23/10/2013 13:53:04
Showing 2 changed files
... ...
@@ -334,15 +334,15 @@ int xcaps_xpath_get(str *inbuf, str *xpaths, str *outbuf)
334 334
 		LM_ERR("unable to evaluate xpath expression [%s]\n", xpaths->s);
335 335
 		goto error;
336 336
 	}
337
-    nodes = xpathObj->nodesetval;
338
-	if(nodes==NULL)
337
+	nodes = xpathObj->nodesetval;
338
+	if(nodes==NULL || nodes->nodeNr==0 || nodes->nodeTab == NULL)
339 339
 	{
340 340
 		outbuf->len = 0;
341 341
 		outbuf->s[outbuf->len] = '\0';
342 342
 		goto done;
343 343
 	}
344 344
 	size = nodes->nodeNr;
345
-    p = outbuf->s;
345
+	p = outbuf->s;
346 346
 	end = outbuf->s + outbuf->len;
347 347
 	for(i = 0; i < size; ++i)
348 348
 	{
... ...
@@ -1070,7 +1070,8 @@ static int w_xcaps_get(sip_msg_t* msg, char* puri, char* ppath)
1070 1070
 	str uri;
1071 1071
 	str path;
1072 1072
 	str etag = {0, 0};
1073
-	str body;
1073
+	str body = {0, 0};
1074
+	str new_body = {0, 0};
1074 1075
 	int ret = 0;
1075 1076
 	xcap_uri_t xuri;
1076 1077
 	str *ctype;
... ...
@@ -1170,37 +1171,77 @@ static int w_xcaps_get(sip_msg_t* msg, char* puri, char* ppath)
1170 1170
 			LM_ERR("could not fetch xcap document\n");
1171 1171
 			goto error;
1172 1172
 		}
1173
-
1174
-		if(ret==0)
1173
+		if(ret!=0)
1175 1174
 		{
1176
-			/* doc found */
1177
-			ctype = &xcaps_str_appxml;
1178
-			if(xuri.type==RESOURCE_LIST)
1179
-				ctype = &xcaps_str_apprlxml;
1180
-			else if(xuri.type==PRES_RULES)
1181
-				ctype = &xcaps_str_appapxml;
1182
-			else if(xuri.type==RLS_SERVICE)
1183
-				ctype = &xcaps_str_apprsxml;
1184
-			else if(xuri.type==USER_PROFILE)
1185
-				ctype = &xcaps_str_appupxml;
1186
-			else if(xuri.type==PRES_CONTENT)
1187
-				ctype = &xcaps_str_apppcxml;
1188
-			else if(xuri.type==PIDF_MANIPULATION)
1189
-				ctype = &xcaps_str_apppdxml;
1190
-			xcaps_send_reply(msg, 200, &xcaps_str_ok, &etag,
1191
-					ctype, &body);
1192
-		} else {
1193 1175
 			/* doc not found */
1194 1176
 			xcaps_send_reply(msg, 404, &xcaps_str_notfound, NULL,
1195 1177
 					NULL, NULL);
1178
+			break;
1179
+		}
1180
+
1181
+		if(xuri.nss!=NULL && xuri.node.len>0)
1182
+		{
1183
+			if((new_body.s = pkg_malloc(body.len))==NULL)
1184
+			{
1185
+				LM_ERR("allocating package memory\n");
1186
+				goto error;
1187
+			}
1188
+			new_body.len = body.len;
1189
+			
1190
+			if(xcaps_xpath_hack(&body, 0)<0)
1191
+			{
1192
+				LM_ERR("could not hack xcap document\n");
1193
+				goto error;
1194
+			}
1195
+			if(xcaps_xpath_get(&body, &xuri.node, &new_body)<0)
1196
+			{
1197
+				LM_ERR("could not retrieve element from xcap document\n");
1198
+				goto error;
1199
+			}
1200
+			if(new_body.len<=0)
1201
+			{
1202
+				/* element not found */
1203
+				xcaps_send_reply(msg, 404, &xcaps_str_notfound, NULL,
1204
+					NULL, NULL);
1205
+				pkg_free(new_body.s);
1206
+				new_body.s = NULL;
1207
+				break;
1208
+			}
1209
+			if(xcaps_xpath_hack(&new_body, 1)<0)
1210
+			{
1211
+				LM_ERR("could not hack xcap document\n");
1212
+				goto error;
1213
+			}
1214
+			memcpy(body.s, new_body.s, new_body.len);
1215
+			body.len = new_body.len;
1216
+			pkg_free(new_body.s);
1217
+			new_body.s = NULL;
1196 1218
 		}
1197 1219
 
1220
+		/* doc or element found */
1221
+		ctype = &xcaps_str_appxml;
1222
+		if(xuri.type==RESOURCE_LIST)
1223
+			ctype = &xcaps_str_apprlxml;
1224
+		else if(xuri.type==PRES_RULES)
1225
+			ctype = &xcaps_str_appapxml;
1226
+		else if(xuri.type==RLS_SERVICE)
1227
+			ctype = &xcaps_str_apprsxml;
1228
+		else if(xuri.type==USER_PROFILE)
1229
+			ctype = &xcaps_str_appupxml;
1230
+		else if(xuri.type==PRES_CONTENT)
1231
+			ctype = &xcaps_str_apppcxml;
1232
+		else if(xuri.type==PIDF_MANIPULATION)
1233
+			ctype = &xcaps_str_apppdxml;
1234
+		xcaps_send_reply(msg, 200, &xcaps_str_ok, &etag,
1235
+				ctype, &body);
1236
+
1198 1237
 		break;
1199 1238
 	}
1200 1239
 
1201 1240
 	return 1;
1202 1241
 
1203 1242
 error:
1243
+	if (new_body.s) pkg_free(new_body.s);
1204 1244
 	xcaps_send_reply(msg, 500, &xcaps_str_srverr, NULL,
1205 1245
 				NULL, NULL);
1206 1246
 	return -1;