xmlrpc.c 16.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * xmlrpc.c: XML-RPC protocol handler for libvir library
 *
 * Copyright (C) 2006  IBM, Corp.
 *
 * See COPYING.LIB for the License of this software
 *
 * Anthony Liguori <aliguori@us.ibm.com>
 */

#include "xmlrpc.h"
K
Karel Zak 已提交
12
#include "internal.h"
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

#include <libxml/nanohttp.h>

#include <string.h>
#include <errno.h>

/* TODO
   1) Lots of error checking
   2) xmlRpcValueToSexpr
*/

static xmlNodePtr xmlFirstElement(xmlNodePtr node);
static xmlNodePtr xmlNextElement(xmlNodePtr node);

struct _xmlRpcContext
{
    char *uri;
    int faultCode;
    char *faultMessage;
};

K
Karel Zak 已提交
34 35 36 37 38 39 40 41 42 43 44 45
static void xmlRpcError(virErrorNumber error, const char *info, int value)
{
    const char *errmsg;

    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
    __virRaiseError(NULL, NULL, VIR_FROM_RPC, error, VIR_ERR_ERROR,
                    errmsg, info, NULL, value, 0, errmsg, info, value);
}

46 47 48
static xmlRpcValuePtr xmlRpcValueNew(xmlRpcValueType type)
{
    xmlRpcValuePtr ret = malloc(sizeof(*ret));
K
Karel Zak 已提交
49 50 51 52 53
    
    if (!ret)
        xmlRpcError(VIR_ERR_NO_MEMORY, "allocate value", sizeof(*ret));
    else
        ret->kind = type;
54 55 56 57 58 59
    return ret;
}

static char *xmlGetText(xmlNodePtr node)
{
    for (node = node->children; node; node = node->next)
K
Karel Zak 已提交
60 61 62
	if (node->type == XML_TEXT_NODE) {
	    char *x = strdup((const char *)node->content);
	    if (!x)
63 64
                xmlRpcError(VIR_ERR_NO_MEMORY, _("copying node content"),
                            strlen((const char *)node->content));
K
Karel Zak 已提交
65 66
	    return x;
	}
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    return NULL;
}

static xmlNodePtr xmlFirstElement(xmlNodePtr node)
{
    for (node = node->children; node; node = node->next)
	if (node->type == XML_ELEMENT_NODE)
	    break;
    return node;
}

static xmlNodePtr xmlNextElement(xmlNodePtr node)
{
    for (node = node->next; node; node = node->next)
	if (node->type == XML_ELEMENT_NODE)
	    break;
    return node;
}

K
Karel Zak 已提交
86
static xmlRpcValuePtr xmlRpcValueUnmarshalDateTime(xmlNodePtr node ATTRIBUTE_UNUSED)
87 88
{
    /* we don't need this */
K
Karel Zak 已提交
89
    TODO
90 91 92 93 94 95
    return NULL;
}

static xmlRpcValuePtr xmlRpcValueUnmarshalString(xmlNodePtr node)
{
    xmlRpcValuePtr ret = xmlRpcValueNew(XML_RPC_STRING);
K
Karel Zak 已提交
96 97 98
    
    if (ret)
        ret->value.string = xmlGetText(node);
99 100 101
    return ret;
}

K
Karel Zak 已提交
102
static xmlRpcValuePtr xmlRpcValueUnmarshalBase64(xmlNodePtr node ATTRIBUTE_UNUSED)
103 104
{
    /* we don't need this */
K
Karel Zak 已提交
105
    TODO
106 107 108 109 110 111 112
    return NULL;
}

static xmlRpcValuePtr xmlRpcValueUnmarshalInteger(xmlNodePtr node)
{
    xmlRpcValuePtr ret = xmlRpcValueNew(XML_RPC_INTEGER);
    char *value = xmlGetText(node);
K
Karel Zak 已提交
113 114 115 116 117
    
    if (ret && value)
        ret->value.integer = atoi(value);
    if (value)
        free(value);
118 119 120 121 122 123 124 125
    return ret;
}

static xmlRpcValuePtr xmlRpcValueUnmarshalBoolean(xmlNodePtr node)
{
    xmlRpcValuePtr ret = xmlRpcValueNew(XML_RPC_BOOLEAN);
    char *value = xmlGetText(node);

K
Karel Zak 已提交
126 127 128
    if (!ret)
        return NULL;
    if (value && atoi(value))
129 130 131
	ret->value.boolean = true;
    else
	ret->value.boolean = false;
K
Karel Zak 已提交
132 133
    if (value)
        free(value);
134 135 136 137 138 139 140 141
    return ret;
}

static xmlRpcValuePtr xmlRpcValueUnmarshalDouble(xmlNodePtr node)
{
    xmlRpcValuePtr ret = xmlRpcValueNew(XML_RPC_DOUBLE);
    char *value = xmlGetText(node);

K
Karel Zak 已提交
142 143 144 145
    if (ret && value)
        ret->value.real = atof(value);
    if (value)
        free(value);
146 147 148 149 150 151 152 153 154
    return ret;
}

static xmlRpcValuePtr xmlRpcValueUnmarshalArray(xmlNodePtr node)
{
    xmlRpcValuePtr ret = xmlRpcValueNew(XML_RPC_ARRAY);
    xmlNodePtr cur;
    int n_elements = 0;

K
Karel Zak 已提交
155 156
    if (!ret)
        return NULL;
157

158 159 160 161
    for (cur = xmlFirstElement(node); cur; cur = xmlNextElement(cur))
	n_elements += 1;

    ret->value.array.elements = malloc(n_elements * sizeof(xmlRpcValue));
K
Karel Zak 已提交
162
    if (!ret->value.array.elements) {
163 164
        xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate value array"),
                    n_elements * sizeof(xmlRpcValue));
K
Karel Zak 已提交
165 166 167
	free(ret);
	return NULL;
    }
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    n_elements = 0;
    for (cur = xmlFirstElement(node); cur; cur = xmlNextElement(cur)) {
	ret->value.array.elements[n_elements] = xmlRpcValueUnmarshal(cur);
	n_elements += 1;
    }

    ret->value.array.n_elements = n_elements;

    return ret;
}

static xmlRpcValueDictElementPtr xmlRpcValueUnmarshalDictElement(xmlNodePtr node)
{
    xmlRpcValueDictElementPtr ret = malloc(sizeof(*ret));
    xmlNodePtr cur;

K
Karel Zak 已提交
184 185 186 187
    if (!ret) {
        xmlRpcError(VIR_ERR_NO_MEMORY, "allocate dict", sizeof(*ret));
	return NULL;
    }
188 189 190 191 192 193 194 195
    memset(ret, 0, sizeof(*ret));

    for (cur = xmlFirstElement(node); cur; cur = xmlNextElement(cur)) {
	if (xmlStrEqual(cur->name, BAD_CAST "name")) {
	    ret->name = xmlGetText(cur);
	} else if (xmlStrEqual(cur->name, BAD_CAST "value")) {
	    ret->value = xmlRpcValueUnmarshal(cur);
	} else {
196
            xmlRpcError(VIR_ERR_XML_ERROR, _("unexpected dict node"), 0);
K
Karel Zak 已提交
197 198 199 200 201 202
	    if (ret->name)
		free(ret->name);
	    if (ret->value)
		xmlRpcValueFree(ret->value);
	    free(ret);
	    return NULL;
203 204 205 206 207 208 209 210 211 212 213 214 215 216
	}
    }

    ret->next = NULL;

    return ret;
}

static xmlRpcValuePtr xmlRpcValueUnmarshalDict(xmlNodePtr node)
{
    xmlRpcValueDictElementPtr root = NULL, *elem = &root;
    xmlRpcValuePtr ret = xmlRpcValueNew(XML_RPC_STRUCT);
    xmlNodePtr cur;

K
Karel Zak 已提交
217 218 219 220 221
    if (!ret)
	return NULL;
    
    ret->value.dict.root = root;
    
222 223
    for (cur = xmlFirstElement(node); cur; cur = xmlNextElement(cur)) {
	*elem = xmlRpcValueUnmarshalDictElement(cur);
K
Karel Zak 已提交
224 225 226 227
	if (*elem==NULL) {
	    xmlRpcValueFree(ret);
	    return NULL;
	} 
228 229 230 231 232 233 234 235 236
	elem = &(*elem)->next;
    }

    return ret;
}

xmlRpcValuePtr xmlRpcValueUnmarshal(xmlNodePtr node)
{
    xmlNodePtr n;
K
Karel Zak 已提交
237
    xmlRpcValuePtr ret = NULL;
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

    if (xmlStrEqual(node->name, BAD_CAST "value")) {
	n = xmlFirstElement(node);
	if (n == NULL) {
	    ret = xmlRpcValueUnmarshalString(node);
	} else {
	    ret = xmlRpcValueUnmarshal(n);
	}
    } else if (xmlStrEqual(node->name, BAD_CAST "dateTime.iso8601")) {
	ret = xmlRpcValueUnmarshalDateTime(node);
    } else if (xmlStrEqual(node->name, BAD_CAST "string")) {
	ret = xmlRpcValueUnmarshalString(node);
    } else if (xmlStrEqual(node->name, BAD_CAST "base64")) {
	ret = xmlRpcValueUnmarshalBase64(node);
    } else if (xmlStrEqual(node->name, BAD_CAST "i4") ||
	       xmlStrEqual(node->name, BAD_CAST "int")) {
	ret = xmlRpcValueUnmarshalInteger(node);
    } else if (xmlStrEqual(node->name, BAD_CAST "boolean")) {
	ret = xmlRpcValueUnmarshalBoolean(node);
    } else if (xmlStrEqual(node->name, BAD_CAST "double")) {
	ret = xmlRpcValueUnmarshalDouble(node);
    } else if (xmlStrEqual(node->name, BAD_CAST "array")) {
	ret = xmlRpcValueUnmarshal(xmlFirstElement(node));
    } else if (xmlStrEqual(node->name, BAD_CAST "data")) {
	ret = xmlRpcValueUnmarshalArray(node);
    } else if (xmlStrEqual(node->name, BAD_CAST "struct")) {
	ret = xmlRpcValueUnmarshalDict(node);
    } else if (xmlStrEqual(node->name, BAD_CAST "nil")) {
	ret = xmlRpcValueNew(XML_RPC_NIL);
    } else {
268
        xmlRpcError(VIR_ERR_XML_ERROR, _("unexpected value node"), 0);
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
    }

    return ret;
}

void xmlRpcValueFree(xmlRpcValuePtr value)
{
    int i;
    xmlRpcValueDictElementPtr cur, next;

    if (value == NULL)
	return;

    switch (value->kind) {
    case XML_RPC_ARRAY:
	for (i = 0; i < value->value.array.n_elements; i++)
	    xmlRpcValueFree(value->value.array.elements[i]);
	free(value->value.array.elements);
	break;
    case XML_RPC_STRUCT:
	next = value->value.dict.root;
	while (next) {
	    cur = next;
	    next = next->next;
	    free(cur->name);
	    xmlRpcValueFree(cur->value);
	    free(cur);
	}
	break;
    case XML_RPC_STRING:
	free(value->value.string);
	break;
    default:
	break;
    }

    free(value);
}

void xmlRpcValueMarshal(xmlRpcValuePtr value, virBufferPtr buf, int indent)
{
    int i;
    xmlRpcValueDictElement *elem;

    virBufferVSprintf(buf, "%*s<value>", indent, "");
    switch (value->kind) {
    case XML_RPC_ARRAY:
K
Karel Zak 已提交
316
	virBufferStrcat(buf, "<array><data>\n", NULL);
317 318 319 320 321
	for (i = 0; i < value->value.array.n_elements; i++)
	    xmlRpcValueMarshal(value->value.array.elements[i], buf, indent+2);
	virBufferVSprintf(buf, "%*s</data></array>", indent, "");
	break;
    case XML_RPC_STRUCT:
K
Karel Zak 已提交
322
	virBufferStrcat(buf, "<struct>\n", NULL);
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
	indent += 2;
	for (elem = value->value.dict.root; elem; elem = elem->next) {
	    virBufferVSprintf(buf, "%*s<member>\n", indent, "");
	    virBufferVSprintf(buf, "%*s<name>%s</name>\n",
			      indent + 2, "", elem->name);
	    xmlRpcValueMarshal(elem->value, buf, indent + 2);
	    virBufferVSprintf(buf, "%*s</member>\n", indent, "");
	}
	indent -= 2;
	virBufferVSprintf(buf, "%*s</struct>", indent, "");
	break;
    case XML_RPC_INTEGER:
	virBufferVSprintf(buf, "<int>%d</int>", value->value.integer);
	break;
    case XML_RPC_DOUBLE:
	virBufferVSprintf(buf, "<double>%f</double>", value->value.real);
	break;
    case XML_RPC_BOOLEAN:
	if (value->value.boolean)
	    i = 1;
	else
	    i = 0;
	virBufferVSprintf(buf, "<boolean>%d</boolean>", i);
	break;
    case XML_RPC_DATE_TIME:
	/* FIXME */
K
Karel Zak 已提交
349
	TODO
350 351 352
	break;
    case XML_RPC_BASE64:
	/* FIXME */
K
Karel Zak 已提交
353
	TODO
354 355
	break;
    case XML_RPC_STRING:
K
Karel Zak 已提交
356 357
	virBufferStrcat(buf, 
		"<string>", value->value.string, "</string>", NULL);
358 359
	break;
    case XML_RPC_NIL:
K
Karel Zak 已提交
360
	virBufferStrcat(buf, "<nil> </nil>", NULL);
361 362
	break;
    }
K
Karel Zak 已提交
363
    virBufferStrcat(buf, "</value>\n", NULL);
364 365 366 367 368 369 370 371
}

virBufferPtr xmlRpcMarshalRequest(const char *request,
				  int argc, xmlRpcValuePtr *argv)
{
    virBufferPtr buf;
    int i;

K
Karel Zak 已提交
372 373 374 375 376 377 378
    buf = virBufferNew(1024);

    virBufferStrcat(buf,
		    "<?xml version=\"1.0\"?>\n"
		    "<methodCall>\n"
		    "  <methodName>", request, "</methodName>\n"
		    "  <params>\n", NULL);
379
    for (i = 0; i < argc; i++) {
K
Karel Zak 已提交
380 381
	virBufferStrcat(buf,  
                    "    <param>\n", NULL);
382
	xmlRpcValueMarshal(argv[i], buf, 6);
K
Karel Zak 已提交
383 384
	virBufferStrcat(buf,  
                    "    </param>\n", NULL);
385
    }
K
Karel Zak 已提交
386 387 388
    virBufferStrcat(buf,
                    "  </params>\n"
		    "</methodCall>\n", NULL);
389 390 391 392 393
    return buf;
}

xmlRpcValuePtr xmlRpcUnmarshalResponse(xmlNodePtr node, bool *is_fault)
{
K
Karel Zak 已提交
394 395 396
    if (!node)
	return NULL;

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    if (!xmlStrEqual(node->name, BAD_CAST "methodResponse"))
	return NULL;

    node = xmlFirstElement(node);
    if (xmlStrEqual(node->name, BAD_CAST "params")) {
	node = xmlFirstElement(node);

	if (!xmlStrEqual(node->name, BAD_CAST "param"))
	    return NULL;

	*is_fault = false;
	return xmlRpcValueUnmarshal(xmlFirstElement(node));
    } else if (xmlStrEqual(node->name, BAD_CAST "fault")) {
	*is_fault = true;
	return xmlRpcValueUnmarshal(xmlFirstElement(node));
    } else
	return NULL;
}

static char *xmlRpcCallRaw(const char *url, const char *request)
{
	void *cxt;
K
Karel Zak 已提交
419
	char *contentType = (char *) "text/xml";
420 421 422 423 424 425 426 427 428
	int len, ret, serrno;
	char *response = NULL;

	cxt = xmlNanoHTTPMethod(url,
				"POST",
				request,
				&contentType,
				NULL,
				strlen(request));
429

K
Karel Zak 已提交
430
	if (cxt == NULL) {
431
                xmlRpcError(VIR_ERR_POST_FAILED, _("send request"), 0);
432
		goto error;
K
Karel Zak 已提交
433
	}
434 435 436

	if (contentType && strcmp(contentType, "text/xml") != 0) {
		errno = EINVAL;
437
		xmlRpcError(VIR_ERR_POST_FAILED, _("unexpected mime type"), 0);
438 439 440 441 442
		goto error;
	}

	len = xmlNanoHTTPContentLength(cxt);
	response = malloc(len + 1);
K
Karel Zak 已提交
443
	if (response == NULL) {
444
		xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate response"), len);
445
		goto error;
K
Karel Zak 已提交
446
	}
447 448 449 450 451
	ret = xmlNanoHTTPRead(cxt, response, len);
	if (ret != len) {
		errno = EINVAL;
		free(response);
		response = NULL;
452
		xmlRpcError(VIR_ERR_POST_FAILED, _("read response"), 0);
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
	}

	response[len] = 0;

 error:
	serrno = errno;
	if (cxt) {
		xmlNanoHTTPClose(cxt);
		free(contentType);
	}
	errno = serrno;

	return response;
}

static char **xmlRpcStringArray(xmlRpcValuePtr value)
{
    char **ret, *ptr;
    int i;
    size_t size = 0;

    if (value->kind != XML_RPC_ARRAY)
	return NULL;

    size = sizeof(char *) * (value->value.array.n_elements + 1);

    for (i = 0; i < value->value.array.n_elements; i++)
	if (value->value.array.elements[i]->kind == XML_RPC_STRING)
	    size += strlen(value->value.array.elements[i]->value.string) + 1;

K
Karel Zak 已提交
483
    if (!(ptr = malloc(size))) {
484
	xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate string array"), size);
K
Karel Zak 已提交
485 486
	return NULL;
    }
487 488 489 490 491 492 493 494 495 496
    ret = (char **)ptr;
    ptr += sizeof(char *) * (value->value.array.n_elements + 1);

    for (i = 0; i < value->value.array.n_elements; i++) {
	if (value->value.array.elements[i]->kind == XML_RPC_STRING) {
	    char *s = value->value.array.elements[i]->value.string;
	    strcpy(ptr, s);
	    ret[i] = ptr;
	    ptr += strlen(s) + 1;
	} else
K
Karel Zak 已提交
497
	    ret[i] = (char *) "";
498 499 500 501 502 503 504
    }

    ret[i] = NULL;

    return ret;
}

K
Karel Zak 已提交
505 506
xmlRpcValuePtr *
xmlRpcArgvNew(const char *fmt, va_list ap, int *argc)
507 508
{
    xmlRpcValuePtr *argv;
K
Karel Zak 已提交
509
    const char *ptr;
510
    int i;
K
Karel Zak 已提交
511 512
    
    *argc = strlen(fmt);
K
Karel Zak 已提交
513
    if (!(argv = malloc(sizeof(*argv) * *argc))) {
514
        xmlRpcError(VIR_ERR_NO_MEMORY, _("read response"), sizeof(*argv) * *argc);
K
Karel Zak 已提交
515 516
        return NULL;
    }
517 518 519 520
    i = 0;
    for (ptr = fmt; *ptr; ptr++) {
	switch (*ptr) {
	case 'i':
K
Karel Zak 已提交
521 522
	    if ((argv[i] = xmlRpcValueNew(XML_RPC_INTEGER)))
		argv[i]->value.integer = va_arg(ap, int32_t);
523 524
	    break;
	case 'f':
K
Karel Zak 已提交
525 526
	    if ((argv[i] = xmlRpcValueNew(XML_RPC_DOUBLE)))
		argv[i]->value.real = va_arg(ap, double);
527 528
	    break;
	case 'b':
K
Karel Zak 已提交
529 530
	    if ((argv[i] = xmlRpcValueNew(XML_RPC_BOOLEAN)))
	        argv[i]->value.boolean = va_arg(ap, int);
531 532
	    break;
	case 's':
K
Karel Zak 已提交
533 534
	    if ((argv[i] = xmlRpcValueNew(XML_RPC_STRING)))
  	        argv[i]->value.string = strdup(va_arg(ap, const char *));
535 536
	    break;
	default:
K
Karel Zak 已提交
537 538 539 540 541
	    argv[i] = NULL;
	    break;
	}
	if (argv[i]==NULL) {
	    xmlRpcArgvFree(i, argv);
K
Karel Zak 已提交
542
	    return NULL;
543 544 545
	}
	i++;
    }
K
Karel Zak 已提交
546 547
    return argv;
}
548

K
Karel Zak 已提交
549 550 551 552
void
xmlRpcArgvFree(int argc, xmlRpcValuePtr *argv)
{
    int i;
K
Karel Zak 已提交
553 554
    if (!argv)
	return;
555 556 557 558
    for (i = 0; i < argc; i++)
	xmlRpcValueFree(argv[i]);

    free(argv);
K
Karel Zak 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
}

int xmlRpcCall(xmlRpcContextPtr context, const char *method,
	       const char *retfmt, const char *fmt, ...)
{
    va_list ap;
    int argc;
    xmlRpcValuePtr *argv;
    virBufferPtr buf;
    char *ret;
    xmlDocPtr xml;
    xmlNodePtr node;
    bool fault;
    xmlRpcValuePtr value;
    void *retval = NULL;

    va_start(ap, fmt);
    
    if (retfmt && *retfmt)
	retval = va_arg(ap, void *);
 
K
Karel Zak 已提交
580 581
    if (!(argv = xmlRpcArgvNew(fmt, ap, &argc)))
	return -1;
K
Karel Zak 已提交
582 583 584 585 586 587
    
    va_end(ap);

    buf = xmlRpcMarshalRequest(method, argc, argv);

    xmlRpcArgvFree(argc, argv);
588
	
K
Karel Zak 已提交
589 590 591
    if (!buf)
	return -1;
    
592 593
    ret = xmlRpcCallRaw(context->uri, buf->content);

K
Karel Zak 已提交
594
    virBufferFree(buf);
595

K
Karel Zak 已提交
596 597 598
    if (!ret)
	return -1;

599 600 601 602 603 604 605
    xml = xmlReadDoc((const xmlChar *)ret, "response.xml", NULL,
		     XML_PARSE_NOENT | XML_PARSE_NONET |
		     XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
    free(ret);

    if (xml == NULL) {
	errno = EINVAL;
606
	xmlRpcError(VIR_ERR_XML_ERROR, _("parse server response failed"), 0);
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
	return -1;
    }

    node = xmlDocGetRootElement(xml);

    value = xmlRpcUnmarshalResponse(node, &fault);

    if (!fault) {
	switch (*retfmt) {
	case 'i':
	    if (value->kind == XML_RPC_INTEGER)
		*(int32_t *)retval = value->value.integer;
	    break;
	case 'b':
	    if (value->kind == XML_RPC_BOOLEAN)
		*(bool *)retval = value->value.boolean;
	    break;
	case 'f':
	    if (value->kind == XML_RPC_DOUBLE)
		*(double *)retval = value->value.real;
	    break;
	case 's':
	    if (value->kind == XML_RPC_STRING)
		*(char **)retval = strdup(value->value.string);
	    break;
	case 'S':
	    *(char ***)retval = xmlRpcStringArray(value);
	    break;
	case 'V':
	    *(xmlRpcValuePtr *)retval = value;
	    value = NULL;
	    break;
	default:
	    printf("not supported yet\n");
	    break;
	}
    }

    xmlFreeDoc(xml);

K
Karel Zak 已提交
647 648 649
    if (fault) { 
	/* FIXME we need generic dict routines */
	/* FIXME we need faultMessage propagate to libvirt error API */
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
	context->faultCode = value->value.dict.root->value->value.integer;
	context->faultMessage = strdup(value->value.dict.root->next->value->value.string);
	xmlRpcValueFree(value);
	errno = EFAULT;
	return -1;
    }

    xmlRpcValueFree(value);

    return 0;
}

xmlRpcContextPtr xmlRpcContextNew(const char *uri)
{
    xmlRpcContextPtr ret = malloc(sizeof(*ret));

    if (ret) {
	ret->uri = strdup(uri);
	ret->faultMessage = NULL;
K
Karel Zak 已提交
669
    } else
670
        xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate new context"), sizeof(*ret));
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

    return ret;
}

void xmlRpcContextFree(xmlRpcContextPtr context)
{
    if (context) {
	if (context->uri)
	    free(context->uri);

	if (context->faultMessage)
	    free(context->faultMessage);

	free(context);
    }
}

int xmlRpcContextFaultCode(xmlRpcContextPtr context)
{
    return context->faultCode;
}

const char *xmlRpcContextFaultMessage(xmlRpcContextPtr context)
{
    return context->faultMessage;
}