1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
|
/*
* $Id$
*
* xcap_server module - builtin XCAP server
*
* Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio 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
*
* Kamailio 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include "../../dprint.h"
#include "../../mem/mem.h"
#include "../../parser/parse_param.h"
#include "../../modules/xcap_client/xcap_callbacks.h"
#include "xcap_misc.h"
extern str xcaps_root;
static param_t *_xcaps_xpath_ns_root = NULL;
/* list of supported auid - ordered ascending by type */
xcaps_auid_list_t xcaps_auid_list[] = {
{ { "pres-rules", 10 },
'/', PRES_RULES },
{ { "org.openmobilealliance.pres-rules", 33 },
'/', PRES_RULES },
{ { "resource-lists", 14 },
'/', RESOURCE_LIST },
{ { "rls-services", 12 },
'/', RLS_SERVICE },
{ { "pidf-manipulation", 17 },
'/', PIDF_MANIPULATION },
{ { "xcap-caps", 9 },
'/', XCAP_CAPS },
{ { "org.openmobilealliance.user-profile", 35},
'/', USER_PROFILE },
{ { "org.openmobilealliance.pres-content", 35},
'/', PRES_CONTENT },
{ { "org.openmobilealliance.search", 29},
'?', SEARCH },
{ { "org.openmobilealliance.xcap-directory", 37},
'/', DIRECTORY },
{ { 0, 0 }, 0, 0 }
};
static int xcaps_find_auid(str *s, xcap_uri_t *xuri)
{
int i;
for(i=0; xcaps_auid_list[i].auid.s!=NULL; i++)
{
if(s->len > xcaps_auid_list[i].auid.len
&& s->s[xcaps_auid_list[i].auid.len] == xcaps_auid_list[i].term
&& strncmp(s->s, xcaps_auid_list[i].auid.s,
xcaps_auid_list[i].auid.len) == 0)
{
LM_DBG("matched %.*s\n", xcaps_auid_list[i].auid.len,
xcaps_auid_list[i].auid.s);
xuri->type = xcaps_auid_list[i].type;
xuri->auid.s = s->s;
xuri->auid.len = xcaps_auid_list[i].auid.len;
return 0;
}
}
LM_ERR("unsupported auid in [%.*s]\n", xuri->uri.len,
xuri->uri.s);
return -1;
}
/**
* parse xcap uri
*/
int xcap_parse_uri(str *huri, str *xroot, xcap_uri_t *xuri)
{
str s;
char *p;
int i;
if(huri==NULL || xuri==NULL || huri->s==NULL)
{
LM_ERR("invalid parameters\n");
return -1;
}
if(huri->len>XCAP_MAX_URI_SIZE)
{
LM_ERR("http uri too long\n");
return -1;
}
memset(xuri, 0, sizeof(xcap_uri_t));
/* copy and url decode */
for(p=huri->s, i=0; p<huri->s+huri->len; p++)
{
if(*p=='%') {
if(p > huri->s+huri->len-2)
{
LM_ERR("invalid http uri - hexa value too short\n");
return -1;
}
p++;
if(*p>='0' && *p<='9')
xuri->buf[i] = (*p - '0') << 4;
else if(*p>='a'&&*p<='f')
xuri->buf[i] = (*p-'a'+10) << 4;
else if(*p>='A'&&*p<='F')
xuri->buf[i] = (*p-'A'+10) << 4;
else return -1;
p++;
if(*p>='0'&&*p<='9')
xuri->buf[i] += *p-'0';
else if(*p>='a'&&*p<='f')
xuri->buf[i] += *p-'a'+10;
else if(*p>='A'&&*p<='F')
xuri->buf[i] += *p-'A'+10;
else return -1;
} else {
xuri->buf[i] = *p;
}
i++;
}
xuri->uri.s = xuri->buf;
xuri->uri.len = i;
xuri->uri.s[xuri->uri.len] = '\0';
xuri->nss = strstr(xuri->uri.s, XCAP_NSS);
xuri->root.s = xuri->uri.s;
s = xuri->uri;
if(xroot!=NULL)
{
if(xroot->len >= xuri->uri.len)
{
LM_ERR("invalid http uri - shorter than xcap-root\n");
return -1;
}
if(strncmp(xuri->uri.s, xroot->s, xroot->len)!=0)
{
LM_ERR("missing xcap-root in [%.*s]\n", xuri->uri.len,
xuri->uri.s);
return -1;
}
s.s = xuri->uri.s + xroot->len;
s.len = xuri->uri.len - xroot->len;
xuri->root.len = xroot->len;
}
if(*s.s == '/')
{
s.s++;
s.len--;
}
/* auid */
if(xcaps_find_auid(&s, xuri)<0)
return -1;
/* handling special auids */
if(xuri->type == SEARCH) {
s.s += xuri->auid.len + 1;
s.len -= xuri->auid.len + 1;
/* target */
if (s.len>7 && strncmp(s.s, "target=", 7)==0) {
LM_DBG("matched target=\n");
s.s += 7;
s.len -= 7;
xuri->target.s = s.s;
p = strchr(s.s, '&');
if (p==NULL) {
xuri->target.len = s.len;
} else {
xuri->target.len = p - xuri->target.s;
}
s.s += xuri->target.len + 1;
s.len -= xuri->target.len+1;
LM_DBG("target=%.*s\n", xuri->target.len, xuri->target.s);
}
/* domain */
if (s.len>7 && strncmp(s.s, "domain=", 7)==0) {
LM_DBG("matched domain=\n");
s.s += 7;
s.len -= 7;
xuri->domain.s = s.s;
xuri->domain.len = s.len;
LM_DBG("domain=%.*s\n", xuri->domain.len, xuri->domain.s);
}
return 0;
}
s.s += xuri->auid.len + 1;
s.len -= xuri->auid.len + 1;
/* tree: users or global */
xuri->tree.s = s.s;
if(s.len>6 && strncmp(s.s, "users/", 6)==0) {
LM_DBG("matched users\n");
xuri->tree.len = 5;
} else if(s.len>7 && strncmp(s.s, "global/", 7)==0) {
LM_DBG("matched global\n");
xuri->tree.len = 6;
} else {
LM_ERR("unsupported sub-tree in [%.*s]\n", xuri->uri.len,
xuri->uri.s);
return -1;
}
s.s += xuri->tree.len + 1;
s.len -= xuri->tree.len + 1;
/* xuid */
if(xuri->tree.s[0]=='u') {
xuri->xuid.s = s.s;
p = strchr(s.s, '/');
if(p==NULL) {
LM_ERR("no xuid in [%.*s]\n", xuri->uri.len,
xuri->uri.s);
return -1;
}
xuri->xuid.len = p - xuri->xuid.s;
s.s += xuri->xuid.len + 1;
s.len -= xuri->xuid.len + 1;
}
/* file */
xuri->file.s = s.s;
if(xuri->nss==NULL) {
/* without node selector in http uri */
if(s.s[s.len-1]=='/')
xuri->file.len = s.len - 1;
else
xuri->file.len = s.len;
} else {
/* with node selector in http uri */
if(xuri->nss <= s.s) {
LM_ERR("no file in [%.*s]\n", xuri->uri.len,
xuri->uri.s);
return -1;
}
xuri->file.len = xuri->nss - s.s;
if(xuri->file.s[xuri->file.len-1]=='/')
xuri->file.len--;
}
/* doc: aboslute and relative */
xuri->adoc.s = xuri->uri.s;
xuri->adoc.len = xuri->file.s + xuri->file.len - xuri->adoc.s;
xuri->rdoc.s = xuri->auid.s;
xuri->rdoc.len = xuri->file.s + xuri->file.len - xuri->rdoc.s;
/* node */
if(xuri->nss!=NULL) {
xuri->node.s = xuri->nss + 2;
xuri->node.len = xuri->uri.s + xuri->uri.len - xuri->node.s;
}
#if 0
LM_DBG("----- uri: [%.*s]\n", xuri->uri.len, xuri->uri.s);
LM_DBG("----- root: [%.*s]\n", xuri->root.len, xuri->root.s);
LM_DBG("----- auid: [%.*s] (%d)\n", xuri->auid.len, xuri->auid.s,
xuri->type);
LM_DBG("----- tree: [%.*s]\n", xuri->tree.len, xuri->tree.s);
if(xuri->tree.s[0]=='u')
LM_DBG("----- xuid: [%.*s]\n", xuri->xuid.len, xuri->xuid.s);
LM_DBG("----- file: [%.*s]\n", xuri->file.len, xuri->file.s);
LM_DBG("----- adoc: [%.*s]\n", xuri->adoc.len, xuri->adoc.s);
LM_DBG("----- rdoc: [%.*s]\n", xuri->rdoc.len, xuri->rdoc.s);
if(xuri->nss!=NULL)
LM_DBG("----- node: [%.*s]\n", xuri->node.len, xuri->node.s);
#endif
return 0;
}
/**
* get content of xpath pointer
*/
int xcaps_xpath_get(str *inbuf, str *xpaths, str *outbuf)
{
xmlDocPtr doc = NULL;
xmlXPathContextPtr xpathCtx = NULL;
xmlXPathObjectPtr xpathObj = NULL;
xmlNodeSetPtr nodes;
xmlChar *keyword;
xmlBufferPtr psBuf;
int size;
int i;
char *p;
char *end;
char *pos;
doc = xmlParseMemory(inbuf->s, inbuf->len);
if(doc == NULL)
return -1;
xpathCtx = xmlXPathNewContext(doc);
if(xpathCtx == NULL)
{
LM_ERR("unable to create new XPath context\n");
goto error;
}
/* Evaluate xpath expression */
// xcaps_xpath_register_ns(xpathCtx);
xpathObj = xmlXPathEvalExpression(
(const xmlChar*)xpaths->s, xpathCtx);
if(xpathObj == NULL)
{
LM_ERR("unable to evaluate xpath expression [%s]\n", xpaths->s);
goto error;
}
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;
end = outbuf->s + outbuf->len;
for(i = 0; i < size; ++i)
{
if(nodes->nodeTab[i]==NULL)
continue;
if(i!=0)
{
if(p>=end)
{
LM_ERR("output buffer overflow\n");
goto error;
}
*p = ',';
p++;
}
if(nodes->nodeTab[i]->type == XML_ATTRIBUTE_NODE)
{
keyword = xmlNodeListGetString(doc,
nodes->nodeTab[i]->children, 0);
if(keyword != NULL)
{
pos = p + strlen((char*)keyword);
if(pos>=end)
{
LM_ERR("output buffer overflow\n");
goto error;
}
strcpy(p, (char*)keyword);
p = pos;
xmlFree(keyword);
keyword = NULL;
}
} else {
if(nodes->nodeTab[i]->content!=NULL)
{
pos = p + strlen((char*)nodes->nodeTab[i]->content);
if(pos>=end)
{
LM_ERR("output buffer overflow\n");
goto error;
}
strcpy(p, (char*)nodes->nodeTab[i]->content);
p = pos;
} else {
psBuf = xmlBufferCreate();
if(psBuf != NULL && xmlNodeDump(psBuf, doc,
nodes->nodeTab[i], 0, 0)>0)
{
pos = p + strlen((char*)xmlBufferContent(psBuf));
if(pos>=end)
{
LM_ERR("output buffer overflow\n");
goto error;
}
strcpy(p, (char*)xmlBufferContent(psBuf));
p = pos;
}
if(psBuf != NULL) xmlBufferFree(psBuf);
psBuf = NULL;
}
}
}
outbuf->len = p - outbuf->s;
outbuf->s[outbuf->len] = '\0';
done:
if(xpathObj!=NULL) xmlXPathFreeObject(xpathObj);
if(xpathCtx!=NULL) xmlXPathFreeContext(xpathCtx);
if(doc!=NULL) xmlFreeDoc(doc);
xpathObj = NULL;
xpathCtx = NULL;
doc = NULL;
return 0;
error:
if(xpathObj!=NULL) xmlXPathFreeObject(xpathObj);
if(xpathCtx!=NULL) xmlXPathFreeContext(xpathCtx);
if(doc!=NULL) xmlFreeDoc(doc);
xpathObj = NULL;
xpathCtx = NULL;
doc = NULL;
outbuf->len = 0;
outbuf->s[outbuf->len] = '\0';
return -1;
}
/**
* set content of xpath pointer
*/
int xcaps_xpath_set(str *inbuf, str *xpaths, str *val, str *outbuf)
{
xmlDocPtr doc = NULL;
xmlDocPtr newnode = NULL;
xmlXPathContextPtr xpathCtx = NULL;
xmlXPathObjectPtr xpathObj = NULL;
xmlNodeSetPtr nodes;
const xmlChar* value = NULL;
xmlChar *xmem = NULL;
xmlNodePtr parent = NULL;
int size;
int i;
char *p;
doc = xmlParseMemory(inbuf->s, inbuf->len);
if(doc == NULL)
return -1;
if(val!=NULL)
{
newnode = xmlParseMemory(val->s, val->len);
if(newnode==NULL)
goto error;
}
outbuf->s = NULL;
outbuf->len = 0;
xpathCtx = xmlXPathNewContext(doc);
if(xpathCtx == NULL)
{
LM_ERR("unable to create new XPath context\n");
goto error;
}
/* Evaluate xpath expression */
xpathObj = xmlXPathEvalExpression(
(const xmlChar*)xpaths->s, xpathCtx);
if(xpathObj == NULL)
{
LM_ERR("unable to evaluate xpath expression [%s]\n", xpaths->s);
goto error;
}
nodes = xpathObj->nodesetval;
if(nodes==NULL || nodes->nodeNr==0 || nodes->nodeTab == NULL)
{
/* no selection for xpath expression */
LM_DBG("no selection for xpath expression [%s]\n", xpaths->s);
if(val==NULL)
goto done;
/* could be an insert - locate the selection of parent node */
p = strrchr(xpaths->s, '/');
if(p==NULL)
goto done;
/* evaluate xpath expression for parrent node */
*p = 0;
xpathObj = xmlXPathEvalExpression(
(const xmlChar*)xpaths->s, xpathCtx);
if(xpathObj == NULL)
{
LM_DBG("unable to evaluate xpath parent expression [%s]\n",
xpaths->s);
*p = '/';
goto done;
}
*p = '/';
nodes = xpathObj->nodesetval;
if(nodes==NULL || nodes->nodeNr==0 || nodes->nodeTab == NULL)
{
LM_DBG("no selection for xpath parent expression [%s]\n",
xpaths->s);
goto done;
}
/* add the new content as child to first selected element node */
if(nodes->nodeTab[0]==NULL)
{
LM_DBG("selection for xpath parent expression has first child"
" NULL [%s]\n", xpaths->s);
goto done;
}
if(nodes->nodeTab[0]->type==XML_ELEMENT_NODE)
{
xmlAddChild(nodes->nodeTab[0], xmlCopyNode(newnode->children, 1));
} else {
LM_DBG("selection for xpath parent expression is not element"
" node [%s]\n", xpaths->s);
goto done;
}
} else {
/* selection for xpath expression */
size = nodes->nodeNr;
if(val!=NULL)
value = (const xmlChar*)val->s;
/*
* NOTE: the nodes are processed in reverse order, i.e. reverse document
* order because xmlNodeSetContent can actually free up descendant
* of the node and such nodes may have been selected too ! Handling
* in reverse order ensure that descendant are accessed first, before
* they get removed. Mixing XPath and modifications on a tree must be
* done carefully !
*/
for(i = size - 1; i >= 0; i--) {
if(nodes->nodeTab[i]==NULL)
continue;
if(nodes->nodeTab[i]->type==XML_ELEMENT_NODE)
{
parent = nodes->nodeTab[i]->parent;
xmlUnlinkNode(nodes->nodeTab[i]);
if(val!=NULL && newnode!=NULL)
xmlAddChild(parent, xmlCopyNode(newnode->children, 1));
} else {
if(val!=NULL)
xmlNodeSetContent(nodes->nodeTab[i], value);
else
xmlNodeSetContent(nodes->nodeTab[i], (const xmlChar*)"");
}
/*
* All the elements returned by an XPath query are pointers to
* elements from the tree *except* namespace nodes where the XPath
* semantic is different from the implementation in libxml2 tree.
* As a result when a returned node set is freed when
* xmlXPathFreeObject() is called, that routine must check the
* element type. But node from the returned set may have been removed
* by xmlNodeSetContent() resulting in access to freed data.
* This can be exercised by running
* valgrind xpath2 test3.xml '//discarded' discarded
* There is 2 ways around it:
* - make a copy of the pointers to the nodes from the result set
* then call xmlXPathFreeObject() and then modify the nodes
* or
* - remove the reference to the modified nodes from the node set
* as they are processed, if they are not namespace nodes.
*/
if (nodes->nodeTab[i]->type != XML_NAMESPACE_DECL)
nodes->nodeTab[i] = NULL;
}
}
xmlDocDumpMemory(doc, &xmem, &size);
if(xmem==NULL)
{
LM_ERR("error printing output\n");
goto error;
}
if(size<=0)
{
LM_ERR("invalid output size\n");
xmlFree(xmem);
goto error;
}
outbuf->s = (char*)pkg_malloc(size+1);
if(outbuf->s==NULL)
{
LM_ERR("no pkg for output\n");
xmlFree(xmem);
goto error;
}
memcpy(outbuf->s, xmem, size);
outbuf->s[size] = '\0';
outbuf->len = size;
xmlFree(xmem);
done:
if(xpathObj!=NULL) xmlXPathFreeObject(xpathObj);
if(xpathCtx!=NULL) xmlXPathFreeContext(xpathCtx);
if(doc!=NULL) xmlFreeDoc(doc);
if(newnode!=NULL) xmlFreeDoc(newnode);
xpathObj = NULL;
xpathCtx = NULL;
doc = NULL;
return 0;
error:
if(xpathObj!=NULL) xmlXPathFreeObject(xpathObj);
if(xpathCtx!=NULL) xmlXPathFreeContext(xpathCtx);
if(doc!=NULL) xmlFreeDoc(doc);
if(newnode!=NULL) xmlFreeDoc(newnode);
xpathObj = NULL;
xpathCtx = NULL;
doc = NULL;
outbuf->s = NULL;
outbuf->len = 0;
return -1;
}
/**
* register extra xml name spaces
*/
void xcaps_xpath_register_ns(xmlXPathContextPtr xpathCtx)
{
param_t *ns;
ns = _xcaps_xpath_ns_root;
while(ns) {
xmlXPathRegisterNs(xpathCtx, (xmlChar*)ns->name.s,
(xmlChar*)ns->body.s);
ns = ns->next;
}
}
/**
* parse xml ns parameter
*/
int xcaps_xpath_ns_param(modparam_t type, void *val)
{
char *p;
param_t *ns;
if(val==NULL)
goto error;
ns = (param_t*)pkg_malloc(sizeof(param_t));
if(ns==NULL)
{
LM_ERR("no more pkg\n");
goto error;
}
memset(ns, 0, sizeof(param_t));
p = strchr((const char*)val, '=');
if(p==NULL)
{
ns->name.s = "";
ns->body.s = (char*)val;
ns->body.len = strlen(ns->body.s);
} else {
*p = 0;
p++;
ns->name.s = (char*)val;
ns->name.len = strlen(ns->name.s);
ns->body.s = p;
ns->body.len = strlen(ns->body.s);
}
ns->next = _xcaps_xpath_ns_root;
_xcaps_xpath_ns_root = ns;
return 0;
error:
return -1;
}
/**
* check if provided XML doc is valid
* - return -1 if document is invalid or 0 if document is valid
*/
int xcaps_check_doc_validity(str *doc)
{
xmlDocPtr docxml = NULL;
if(doc==NULL || doc->s==NULL || doc->len<0)
return -1;
docxml = xmlParseMemory(doc->s, doc->len);
if(docxml==NULL)
return -1;
xmlFreeDoc(docxml);
return 0;
}
/**
* xcapuri PV export
*/
typedef struct _pv_xcap_uri {
str name;
unsigned int id;
xcap_uri_t xuri;
struct _pv_xcap_uri *next;
} pv_xcap_uri_t;
typedef struct _pv_xcap_uri_spec {
str name;
str key;
int ktype;
pv_xcap_uri_t *xus;
} pv_xcap_uri_spec_t;
pv_xcap_uri_t *_pv_xcap_uri_root = NULL;
/**
*
*/
pv_xcap_uri_t *pv_xcap_uri_get_struct(str *name)
{
unsigned int id;
pv_xcap_uri_t *it;
id = get_hash1_raw(name->s, name->len);
it = _pv_xcap_uri_root;
while(it!=NULL)
{
if(id == it->id && name->len==it->name.len
&& strncmp(name->s, it->name.s, name->len)==0)
{
LM_DBG("uri found [%.*s]\n", name->len, name->s);
return it;
}
it = it->next;
}
it = (pv_xcap_uri_t*)pkg_malloc(sizeof(pv_xcap_uri_t));
if(it==NULL)
{
LM_ERR("no more pkg\n");
return NULL;
}
memset(it, 0, sizeof(pv_xcap_uri_t));
it->id = id;
it->name = *name;
it->next = _pv_xcap_uri_root;
_pv_xcap_uri_root = it;
return it;
}
/**
*
*/
int pv_parse_xcap_uri_name(pv_spec_p sp, str *in)
{
pv_xcap_uri_spec_t *pxs = NULL;
char *p;
if(in->s==NULL || in->len<=0)
return -1;
pxs = (pv_xcap_uri_spec_t*)pkg_malloc(sizeof(pv_xcap_uri_spec_t));
if(pxs==NULL)
return -1;
memset(pxs, 0, sizeof(pv_xcap_uri_spec_t));
p = in->s;
while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
p++;
if(p>in->s+in->len || *p=='\0')
goto error;
pxs->name.s = p;
while(p < in->s + in->len)
{
if(*p=='=' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
break;
p++;
}
if(p>in->s+in->len || *p=='\0')
goto error;
pxs->name.len = p - pxs->name.s;
if(*p!='=')
{
while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
p++;
if(p>in->s+in->len || *p=='\0' || *p!='=')
goto error;
}
p++;
if(*p!='>')
goto error;
p++;
pxs->key.len = in->len - (int)(p - in->s);
pxs->key.s = p;
LM_DBG("uri name [%.*s] - key [%.*s]\n", pxs->name.len, pxs->name.s,
pxs->key.len, pxs->key.s);
if(pxs->key.len==4 && strncmp(pxs->key.s, "data", 4)==0) {
pxs->ktype = 0;
} else if(pxs->key.len==3 && strncmp(pxs->key.s, "uri", 3)==0) {
pxs->ktype = 1;
} else if(pxs->key.len==4 && strncmp(pxs->key.s, "root", 4)==0) {
pxs->ktype = 2;
} else if(pxs->key.len==4 && strncmp(pxs->key.s, "auid", 4)==0) {
pxs->ktype = 3;
} else if(pxs->key.len==4 && strncmp(pxs->key.s, "type", 4)==0) {
pxs->ktype = 4;
} else if(pxs->key.len==4 && strncmp(pxs->key.s, "tree", 4)==0) {
pxs->ktype = 5;
} else if(pxs->key.len==4 && strncmp(pxs->key.s, "xuid", 4)==0) {
pxs->ktype = 6;
} else if(pxs->key.len==4 && strncmp(pxs->key.s, "file", 4)==0) {
pxs->ktype = 7;
} else if(pxs->key.len==4 && strncmp(pxs->key.s, "node", 4)==0) {
pxs->ktype = 8;
} else if(pxs->key.len==6 && strncmp(pxs->key.s, "target", 6)==0) {
pxs->ktype = 9;
} else if(pxs->key.len==6 && strncmp(pxs->key.s, "domain", 6)==0) {
pxs->ktype = 10;
} else if(pxs->key.len== 8 && strncmp(pxs->key.s, "uri_adoc", 8)==0) {
pxs->ktype = 11;
} else {
LM_ERR("unknown key type [%.*s]\n", in->len, in->s);
goto error;
}
pxs->xus = pv_xcap_uri_get_struct(&pxs->name);
sp->pvp.pvn.u.dname = (void*)pxs;
sp->pvp.pvn.type = PV_NAME_OTHER;
return 0;
error:
if(pxs!=NULL)
pkg_free(pxs);
return -1;
}
/**
*
*/
int pv_set_xcap_uri(struct sip_msg* msg, pv_param_t *param,
int op, pv_value_t *val)
{
pv_xcap_uri_spec_t *pxs = NULL;
pxs = (pv_xcap_uri_spec_t*)param->pvn.u.dname;
if(pxs->xus==NULL)
return -1;
if(!(val->flags&PV_VAL_STR))
return -1;
if(pxs->ktype!=0)
return -1;
/* set uri data */
if(xcap_parse_uri(&val->rs, &xcaps_root, &pxs->xus->xuri)<0)
{
LM_ERR("error setting xcap uri data [%.*s]\n",
val->rs.len, val->rs.s);
return -1;
}
return 0;
}
/**
*
*/
int pv_get_xcap_uri(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
pv_xcap_uri_spec_t *pxs = NULL;
pxs = (pv_xcap_uri_spec_t*)param->pvn.u.dname;
if(pxs->xus==NULL)
return -1;
switch(pxs->ktype) {
case 0:
case 1:
/* get uri */
if(pxs->xus->xuri.uri.len>0)
return pv_get_strval(msg, param, res, &pxs->xus->xuri.uri);
break;
case 2:
/* get root */
if(pxs->xus->xuri.root.len>0)
return pv_get_strval(msg, param, res, &pxs->xus->xuri.root);
break;
case 3:
/* get auid */
if(pxs->xus->xuri.auid.len>0)
return pv_get_strval(msg, param, res, &pxs->xus->xuri.auid);
break;
case 4:
/* get type */
return pv_get_sintval(msg, param, res, pxs->xus->xuri.type);
break;
case 5:
/* get tree */
if(pxs->xus->xuri.tree.len>0)
return pv_get_strval(msg, param, res, &pxs->xus->xuri.tree);
break;
case 6:
/* get xuid */
if(pxs->xus->xuri.xuid.len>0)
return pv_get_strval(msg, param, res, &pxs->xus->xuri.xuid);
break;
case 7:
/* get file */
if(pxs->xus->xuri.file.len>0)
return pv_get_strval(msg, param, res, &pxs->xus->xuri.file);
break;
case 8:
/* get node */
if(pxs->xus->xuri.node.len>0)
return pv_get_strval(msg, param, res, &pxs->xus->xuri.node);
break;
case 9:
/* get target */
if(pxs->xus->xuri.target.len>0)
return pv_get_strval(msg, param, res, &pxs->xus->xuri.target);
break;
case 10:
/* get domain */
if(pxs->xus->xuri.domain.len>0)
return pv_get_strval(msg, param, res, &pxs->xus->xuri.domain);
case 11:
/* get xuri->adoc */
if(pxs->xus->xuri.adoc.len>0)
return pv_get_strval(msg, param, res, &pxs->xus->xuri.adoc);
break;
break;
default:
return pv_get_null(msg, param, res);
}
return pv_get_null(msg, param, res);
}
|