| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include <config.h> |
| 11 |
|
| 12 |
#include <libxml/parser.h> |
| 13 |
|
| 14 |
#include "gupnp-error.h" |
| 15 |
#include "gupnp-service-proxy.h" |
| 16 |
#include "gvalue-util.h" |
| 17 |
#include "xml-util.h" |
| 18 |
|
| 19 |
#include "gupnp-error-private.h" |
| 20 |
#include "gupnp-service-info-private.h" |
| 21 |
#include "gupnp-service-proxy-action-private.h" |
| 22 |
|
| 23 |
struct _GUPnPServiceProxyActionIter { |
| 24 |
GObject parent; |
| 25 |
|
| 26 |
GUPnPServiceProxyAction *action; |
| 27 |
xmlNode *current; |
| 28 |
GUPnPServiceIntrospection *introspection; |
| 29 |
gboolean iterating; |
| 30 |
}; |
| 31 |
|
| 32 |
G_DEFINE_TYPE (GUPnPServiceProxyActionIter, |
| 33 |
gupnp_service_proxy_action_iter, |
| 34 |
G_TYPE_OBJECT) |
| 35 |
|
| 36 |
static void |
| 37 |
gupnp_service_proxy_action_iter_init (GUPnPServiceProxyActionIter *self) |
| 38 |
{ |
| 39 |
} |
| 40 |
|
| 41 |
static void |
| 42 |
gupnp_service_proxy_action_iter_dispose (GObject *object) |
| 43 |
{ |
| 44 |
GUPnPServiceProxyActionIter *self = |
| 45 |
GUPNP_SERVICE_PROXY_ACTION_ITER (object); |
| 46 |
g_clear_pointer (&self->action, gupnp_service_proxy_action_unref); |
| 47 |
g_clear_object (&self->introspection); |
| 48 |
|
| 49 |
G_OBJECT_CLASS (gupnp_service_proxy_action_iter_parent_class) |
| 50 |
->dispose (object); |
| 51 |
} |
| 52 |
|
| 53 |
static void |
| 54 |
gupnp_service_proxy_action_iter_class_init ( |
| 55 |
GUPnPServiceProxyActionIterClass *klass) |
| 56 |
{ |
| 57 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 58 |
object_class->dispose = gupnp_service_proxy_action_iter_dispose; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
GUPnPServiceProxyActionIter * |
| 72 |
gupnp_service_proxy_action_iterate (GUPnPServiceProxyAction *action, |
| 73 |
GError **error) |
| 74 |
{ |
| 75 |
g_return_val_if_fail (action != NULL, NULL); |
| 76 |
g_return_val_if_fail (!action->pending, NULL); |
| 77 |
|
| 78 |
|
| 79 |
if (action->error) { |
| 80 |
g_propagate_error (error, g_error_copy (action->error)); |
| 81 |
|
| 82 |
return NULL; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
gupnp_service_proxy_action_check_response (action); |
| 87 |
if (action->error != NULL) { |
| 88 |
g_propagate_error (error, g_error_copy (action->error)); |
| 89 |
|
| 90 |
return NULL; |
| 91 |
} |
| 92 |
|
| 93 |
g_type_ensure (gupnp_service_proxy_action_iter_get_type ()); |
| 94 |
|
| 95 |
GUPnPServiceProxyActionIter *iter = |
| 96 |
g_object_new (gupnp_service_proxy_action_iter_get_type (), |
| 97 |
NULL); |
| 98 |
iter->action = gupnp_service_proxy_action_ref (action); |
| 99 |
|
| 100 |
if (action->proxy != NULL) { |
| 101 |
iter->introspection = gupnp_service_info_get_introspection ( |
| 102 |
GUPNP_SERVICE_INFO (action->proxy)); |
| 103 |
if (iter->introspection != NULL) { |
| 104 |
g_object_ref (iter->introspection); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return iter; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
gboolean |
| 121 |
gupnp_service_proxy_action_iter_next (GUPnPServiceProxyActionIter *self) |
| 122 |
{ |
| 123 |
if (self->iterating) { |
| 124 |
self->current = self->current->next; |
| 125 |
} else { |
| 126 |
self->current = self->action->params->children; |
| 127 |
self->iterating = TRUE; |
| 128 |
} |
| 129 |
|
| 130 |
return self->current != NULL; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
const char * |
| 143 |
gupnp_service_proxy_action_iter_get_name (GUPnPServiceProxyActionIter *self) |
| 144 |
{ |
| 145 |
g_return_val_if_fail (self->current != NULL, NULL); |
| 146 |
|
| 147 |
return (const char *) self->current->name; |
| 148 |
} |
| 149 |
|
| 150 |
static int |
| 151 |
find_argument (gconstpointer a, gconstpointer b) |
| 152 |
{ |
| 153 |
const GUPnPServiceActionArgInfo *arg = a; |
| 154 |
const char *needle = b; |
| 155 |
|
| 156 |
return g_strcmp0 (arg->name, needle); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
gboolean |
| 174 |
gupnp_service_proxy_action_iter_get_value (GUPnPServiceProxyActionIter *self, |
| 175 |
GValue *value) |
| 176 |
{ |
| 177 |
if (self->introspection != NULL) { |
| 178 |
const GUPnPServiceActionInfo *action_info = |
| 179 |
gupnp_service_introspection_get_action ( |
| 180 |
self->introspection, |
| 181 |
self->action->name); |
| 182 |
GList *result = g_list_find_custom (action_info->arguments, |
| 183 |
self->current->name, |
| 184 |
find_argument); |
| 185 |
if (result == NULL) { |
| 186 |
g_debug ("No argument %s\n", self->current->name); |
| 187 |
return FALSE; |
| 188 |
} |
| 189 |
|
| 190 |
const GUPnPServiceActionArgInfo *arg = result->data; |
| 191 |
const char *var = arg->related_state_variable; |
| 192 |
const GUPnPServiceStateVariableInfo *info = |
| 193 |
gupnp_service_introspection_get_state_variable ( |
| 194 |
self->introspection, |
| 195 |
var); |
| 196 |
|
| 197 |
if (info == NULL) { |
| 198 |
g_debug ("No state variable for %s\n", |
| 199 |
self->current->name); |
| 200 |
return FALSE; |
| 201 |
} |
| 202 |
|
| 203 |
g_value_init (value, info->type); |
| 204 |
} else { |
| 205 |
|
| 206 |
|
| 207 |
g_value_init (value, G_TYPE_STRING); |
| 208 |
} |
| 209 |
|
| 210 |
gvalue_util_set_value_from_xml_node (value, self->current); |
| 211 |
return TRUE; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
gboolean |
| 230 |
gupnp_service_proxy_action_iter_get_value_as (GUPnPServiceProxyActionIter *self, |
| 231 |
GType type, |
| 232 |
GValue *value) |
| 233 |
{ |
| 234 |
g_value_init (value, type); |
| 235 |
|
| 236 |
gvalue_util_set_value_from_xml_node (value, self->current); |
| 237 |
return TRUE; |
| 238 |
} |
| 239 |
|
| 240 |
struct _ActionArgument { |
| 241 |
char *name; |
| 242 |
GValue value; |
| 243 |
}; |
| 244 |
typedef struct _ActionArgument ActionArgument; |
| 245 |
|
| 246 |
void |
| 247 |
action_argument_free (ActionArgument* arg) |
| 248 |
{ |
| 249 |
g_free (arg->name); |
| 250 |
g_value_unset (&arg->value); |
| 251 |
g_free (arg); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
static void |
| 257 |
read_out_parameter (const char *arg_name, |
| 258 |
GValue *value, |
| 259 |
xmlNode *params) |
| 260 |
{ |
| 261 |
xmlNode *param; |
| 262 |
|
| 263 |
|
| 264 |
param = xml_util_get_element (params, |
| 265 |
arg_name, |
| 266 |
NULL); |
| 267 |
if (!param) { |
| 268 |
g_warning ("Could not find variable \"%s\" in response", |
| 269 |
arg_name); |
| 270 |
|
| 271 |
return; |
| 272 |
} |
| 273 |
|
| 274 |
gvalue_util_set_value_from_xml_node (value, param); |
| 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 |
GUPnPServiceProxyAction * |
| 303 |
gupnp_service_proxy_action_new_plain (const char *action) |
| 304 |
{ |
| 305 |
GUPnPServiceProxyAction *ret; |
| 306 |
|
| 307 |
g_return_val_if_fail (action != NULL, NULL); |
| 308 |
|
| 309 |
ret = g_atomic_rc_box_new0 (GUPnPServiceProxyAction); |
| 310 |
ret->name = g_strdup (action); |
| 311 |
ret->args = g_ptr_array_new_with_free_func((GDestroyNotify)action_argument_free); |
| 312 |
ret->arg_map = g_hash_table_new (g_str_hash, g_str_equal); |
| 313 |
|
| 314 |
return ret; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
GUPnPServiceProxyAction * |
| 327 |
gupnp_service_proxy_action_ref (GUPnPServiceProxyAction *action) |
| 328 |
{ |
| 329 |
g_return_val_if_fail (action, NULL); |
| 330 |
|
| 331 |
return g_atomic_rc_box_acquire (action); |
| 332 |
} |
| 333 |
|
| 334 |
void |
| 335 |
gupnp_service_proxy_action_reset (GUPnPServiceProxyAction *action) |
| 336 |
{ |
| 337 |
g_clear_weak_pointer (&action->proxy); |
| 338 |
g_clear_error (&action->error); |
| 339 |
g_clear_object (&action->msg); |
| 340 |
|
| 341 |
if (action->msg_str != NULL) { |
| 342 |
g_string_free (action->msg_str, TRUE); |
| 343 |
action->msg_str = NULL; |
| 344 |
} |
| 345 |
g_clear_pointer (&action->response, g_bytes_unref); |
| 346 |
action->params = NULL; |
| 347 |
g_clear_pointer (&action->doc, xmlFreeDoc); |
| 348 |
} |
| 349 |
|
| 350 |
static void |
| 351 |
action_dispose (GUPnPServiceProxyAction *action) |
| 352 |
{ |
| 353 |
gupnp_service_proxy_action_reset (action); |
| 354 |
g_hash_table_destroy (action->arg_map); |
| 355 |
g_ptr_array_unref (action->args); |
| 356 |
|
| 357 |
g_free (action->name); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
void |
| 370 |
gupnp_service_proxy_action_unref (GUPnPServiceProxyAction *action) |
| 371 |
{ |
| 372 |
g_return_if_fail (action); |
| 373 |
|
| 374 |
g_atomic_rc_box_release_full (action, (GDestroyNotify) action_dispose); |
| 375 |
} |
| 376 |
|
| 377 |
G_DEFINE_BOXED_TYPE (GUPnPServiceProxyAction, |
| 378 |
gupnp_service_proxy_action, |
| 379 |
gupnp_service_proxy_action_ref, |
| 380 |
gupnp_service_proxy_action_unref) |
| 381 |
|
| 382 |
|
| 383 |
static void |
| 384 |
write_in_parameter (ActionArgument *arg, |
| 385 |
GString *msg_str) |
| 386 |
{ |
| 387 |
|
| 388 |
xml_util_start_element (msg_str, arg->name); |
| 389 |
gvalue_util_value_append_to_xml_string (&arg->value, msg_str); |
| 390 |
xml_util_end_element (msg_str, arg->name); |
| 391 |
} |
| 392 |
|
| 393 |
static void |
| 394 |
write_footer (GUPnPServiceProxyAction *action) |
| 395 |
{ |
| 396 |
|
| 397 |
g_string_append (action->msg_str, "</u:"); |
| 398 |
g_string_append (action->msg_str, action->name); |
| 399 |
g_string_append_c (action->msg_str, '>'); |
| 400 |
|
| 401 |
g_string_append (action->msg_str, |
| 402 |
"</s:Body>" |
| 403 |
"</s:Envelope>"); |
| 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 |
GUPnPServiceProxyAction * |
| 453 |
gupnp_service_proxy_action_new (const char *action, |
| 454 |
...) |
| 455 |
{ |
| 456 |
GUPnPServiceProxyAction *result = NULL; |
| 457 |
va_list var_args; |
| 458 |
|
| 459 |
g_return_val_if_fail (action != NULL, NULL); |
| 460 |
|
| 461 |
va_start (var_args, action); |
| 462 |
const char *arg_name = va_arg (var_args, const char *); |
| 463 |
|
| 464 |
result = gupnp_service_proxy_action_new_plain (action); |
| 465 |
|
| 466 |
while (arg_name != NULL) { |
| 467 |
GValue value = G_VALUE_INIT; |
| 468 |
|
| 469 |
GType type = va_arg (var_args, GType); |
| 470 |
char *error = NULL; |
| 471 |
|
| 472 |
G_VALUE_COLLECT_INIT (&value, type, var_args, 0, &error); |
| 473 |
if (error == NULL) { |
| 474 |
gupnp_service_proxy_action_add_argument (result, |
| 475 |
arg_name, |
| 476 |
&value); |
| 477 |
g_value_unset (&value); |
| 478 |
} else { |
| 479 |
g_warning ( |
| 480 |
"Failed to collect value of type %s for %s: %s", |
| 481 |
g_type_name (type), |
| 482 |
arg_name, |
| 483 |
error); |
| 484 |
g_free (error); |
| 485 |
} |
| 486 |
arg_name = va_arg (var_args, const char *); |
| 487 |
} |
| 488 |
va_end (var_args); |
| 489 |
|
| 490 |
return result; |
| 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 |
GUPnPServiceProxyAction * |
| 540 |
gupnp_service_proxy_action_new_from_list (const char *action_name, |
| 541 |
GList *in_names, |
| 542 |
GList *in_values) |
| 543 |
{ |
| 544 |
GUPnPServiceProxyAction *action; |
| 545 |
GList *names = NULL; |
| 546 |
GList *values = NULL; |
| 547 |
guint position; |
| 548 |
|
| 549 |
action = gupnp_service_proxy_action_new_plain (action_name); |
| 550 |
|
| 551 |
|
| 552 |
for (names = in_names, values = in_values, position = 0; |
| 553 |
names && values; |
| 554 |
names = names->next, values = values->next, position++) { |
| 555 |
GValue *val = values->data; |
| 556 |
gupnp_service_proxy_action_add_argument ( |
| 557 |
action, |
| 558 |
(const char *) names->data, |
| 559 |
val); |
| 560 |
} |
| 561 |
|
| 562 |
return action; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
GUPnPServiceProxyAction * |
| 577 |
gupnp_service_proxy_action_add_argument (GUPnPServiceProxyAction *action, |
| 578 |
const char *name, |
| 579 |
const GValue *value) |
| 580 |
{ |
| 581 |
g_return_val_if_fail (g_hash_table_lookup_extended (action->arg_map, |
| 582 |
name, |
| 583 |
NULL, |
| 584 |
NULL) == FALSE, |
| 585 |
NULL); |
| 586 |
|
| 587 |
ActionArgument *arg = g_new0 (ActionArgument, 1); |
| 588 |
arg->name = g_strdup (name); |
| 589 |
g_value_init (&arg->value, G_VALUE_TYPE (value)); |
| 590 |
g_value_copy (value, &arg->value); |
| 591 |
g_hash_table_insert (action->arg_map, |
| 592 |
arg->name, |
| 593 |
GUINT_TO_POINTER (action->args->len)); |
| 594 |
g_ptr_array_add (action->args, arg); |
| 595 |
|
| 596 |
return action; |
| 597 |
} |
| 598 |
|
| 599 |
void |
| 600 |
gupnp_service_proxy_action_serialize (GUPnPServiceProxyAction *action, |
| 601 |
const char *service_type) |
| 602 |
{ |
| 603 |
if (action->msg_str != NULL) { |
| 604 |
g_string_free (action->msg_str, TRUE); |
| 605 |
} |
| 606 |
action->msg_str = xml_util_new_string (); |
| 607 |
|
| 608 |
g_string_append (action->msg_str, |
| 609 |
"<?xml version=\"1.0\"?>" |
| 610 |
"<s:Envelope xmlns:s=" |
| 611 |
"\"http://schemas.xmlsoap.org/soap/envelope/\" " |
| 612 |
"s:encodingStyle=" |
| 613 |
"\"http://schemas.xmlsoap.org/soap/encoding/\">" |
| 614 |
"<s:Body>"); |
| 615 |
action->header_pos = action->msg_str->len; |
| 616 |
|
| 617 |
g_ptr_array_foreach (action->args, |
| 618 |
(GFunc) write_in_parameter, |
| 619 |
action->msg_str); |
| 620 |
|
| 621 |
write_footer (action); |
| 622 |
|
| 623 |
g_string_insert (action->msg_str, action->header_pos, "<u:"); |
| 624 |
action->header_pos += strlen ("<u:"); |
| 625 |
g_string_insert (action->msg_str, action->header_pos, action->name); |
| 626 |
action->header_pos += strlen (action->name); |
| 627 |
g_string_insert (action->msg_str, action->header_pos, " xmlns:u=\""); |
| 628 |
action->header_pos += strlen (" xmlns:u=\""); |
| 629 |
g_string_insert (action->msg_str, action->header_pos, service_type); |
| 630 |
action->header_pos += strlen (service_type); |
| 631 |
g_string_insert (action->msg_str, action->header_pos, "\">"); |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
void |
| 637 |
gupnp_service_proxy_action_check_response (GUPnPServiceProxyAction *action) |
| 638 |
{ |
| 639 |
xmlDoc *response; |
| 640 |
int code; |
| 641 |
|
| 642 |
if (action->doc != NULL) { |
| 643 |
return; |
| 644 |
} |
| 645 |
|
| 646 |
if (action->error != NULL) { |
| 647 |
return; |
| 648 |
} |
| 649 |
|
| 650 |
if (action->msg == NULL || action->response == NULL) { |
| 651 |
g_set_error_literal (&action->error, |
| 652 |
GUPNP_SERVER_ERROR, |
| 653 |
GUPNP_SERVER_ERROR_INVALID_RESPONSE, |
| 654 |
"No message, the action was not sent?"); |
| 655 |
|
| 656 |
return; |
| 657 |
} |
| 658 |
|
| 659 |
SoupStatus status = soup_message_get_status (action->msg); |
| 660 |
|
| 661 |
if (status != SOUP_STATUS_OK && |
| 662 |
status != SOUP_STATUS_INTERNAL_SERVER_ERROR) { |
| 663 |
_gupnp_error_set_server_error (&action->error, action->msg); |
| 664 |
return; |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
gconstpointer data; |
| 669 |
gsize length; |
| 670 |
data = g_bytes_get_data (action->response, &length); |
| 671 |
response = xmlReadMemory (data, |
| 672 |
length, |
| 673 |
NULL, |
| 674 |
NULL, |
| 675 |
XML_PARSE_NONET | XML_PARSE_RECOVER); |
| 676 |
|
| 677 |
g_clear_pointer (&action->response, g_bytes_unref); |
| 678 |
|
| 679 |
if (!response) { |
| 680 |
if (status == SOUP_STATUS_OK) { |
| 681 |
g_set_error_literal ( |
| 682 |
&action->error, |
| 683 |
GUPNP_SERVER_ERROR, |
| 684 |
GUPNP_SERVER_ERROR_INVALID_RESPONSE, |
| 685 |
"Could not parse SOAP response"); |
| 686 |
} else { |
| 687 |
g_set_error_literal ( |
| 688 |
&action->error, |
| 689 |
GUPNP_SERVER_ERROR, |
| 690 |
GUPNP_SERVER_ERROR_INTERNAL_SERVER_ERROR, |
| 691 |
soup_message_get_reason_phrase (action->msg)); |
| 692 |
} |
| 693 |
|
| 694 |
return; |
| 695 |
} |
| 696 |
|
| 697 |
xmlNodePtr params = NULL; |
| 698 |
|
| 699 |
params = xml_util_get_element ((xmlNode *) response, "Envelope", NULL); |
| 700 |
if (params != NULL) |
| 701 |
params = xml_util_real_node (params->children); |
| 702 |
|
| 703 |
if (params != NULL) { |
| 704 |
if (strcmp ((const char *) params->name, "Header") == 0) |
| 705 |
params = xml_util_real_node (params->next); |
| 706 |
|
| 707 |
if (params != NULL) |
| 708 |
if (strcmp ((const char *) params->name, "Body") != 0) |
| 709 |
params = NULL; |
| 710 |
} |
| 711 |
|
| 712 |
if (params != NULL) |
| 713 |
params = xml_util_real_node (params->children); |
| 714 |
|
| 715 |
if (params == NULL) { |
| 716 |
g_set_error (&action->error, |
| 717 |
GUPNP_SERVER_ERROR, |
| 718 |
GUPNP_SERVER_ERROR_INVALID_RESPONSE, |
| 719 |
"Invalid Envelope"); |
| 720 |
|
| 721 |
xmlFreeDoc (response); |
| 722 |
|
| 723 |
return; |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
if (status == SOUP_STATUS_INTERNAL_SERVER_ERROR) { |
| 728 |
xmlNode *param; |
| 729 |
char *desc; |
| 730 |
|
| 731 |
param = xml_util_get_element (params, |
| 732 |
"detail", |
| 733 |
"UPnPError", |
| 734 |
NULL); |
| 735 |
|
| 736 |
if (!param) { |
| 737 |
g_set_error (&action->error, |
| 738 |
GUPNP_SERVER_ERROR, |
| 739 |
GUPNP_SERVER_ERROR_INVALID_RESPONSE, |
| 740 |
"Invalid Fault"); |
| 741 |
|
| 742 |
xmlFreeDoc (response); |
| 743 |
|
| 744 |
return; |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
code = xml_util_get_child_element_content_int (param, |
| 749 |
"errorCode"); |
| 750 |
if (code == -1) { |
| 751 |
g_set_error (&action->error, |
| 752 |
GUPNP_SERVER_ERROR, |
| 753 |
GUPNP_SERVER_ERROR_INVALID_RESPONSE, |
| 754 |
"Invalid Fault"); |
| 755 |
|
| 756 |
xmlFreeDoc (response); |
| 757 |
|
| 758 |
return; |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
desc = xml_util_get_child_element_content_glib ( |
| 763 |
param, |
| 764 |
"errorDescription"); |
| 765 |
if (desc == NULL) |
| 766 |
desc = g_strdup ( |
| 767 |
soup_message_get_reason_phrase (action->msg)); |
| 768 |
|
| 769 |
g_set_error_literal (&action->error, |
| 770 |
GUPNP_CONTROL_ERROR, |
| 771 |
code, |
| 772 |
desc); |
| 773 |
|
| 774 |
g_free (desc); |
| 775 |
|
| 776 |
xmlFreeDoc (response); |
| 777 |
|
| 778 |
return; |
| 779 |
} |
| 780 |
|
| 781 |
action->params = params; |
| 782 |
action->doc = response; |
| 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 |
gboolean |
| 853 |
gupnp_service_proxy_action_get_result_list (GUPnPServiceProxyAction *action, |
| 854 |
GList *out_names, |
| 855 |
GList *out_types, |
| 856 |
GList **out_values, |
| 857 |
GError **error) |
| 858 |
{ |
| 859 |
GList *names; |
| 860 |
GList *types; |
| 861 |
GList *out_values_list; |
| 862 |
|
| 863 |
out_values_list = NULL; |
| 864 |
|
| 865 |
g_return_val_if_fail (action, FALSE); |
| 866 |
|
| 867 |
|
| 868 |
if (action->error) { |
| 869 |
g_propagate_error (error, g_error_copy (action->error)); |
| 870 |
|
| 871 |
return FALSE; |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
gupnp_service_proxy_action_check_response (action); |
| 876 |
if (action->error != NULL) { |
| 877 |
g_propagate_error (error, g_error_copy (action->error)); |
| 878 |
|
| 879 |
return FALSE; |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
types = out_types; |
| 884 |
for (names = out_names; names; names=names->next) { |
| 885 |
GValue *val; |
| 886 |
|
| 887 |
val = g_new0 (GValue, 1); |
| 888 |
g_value_init (val, (GType) types->data); |
| 889 |
|
| 890 |
read_out_parameter (names->data, val, action->params); |
| 891 |
|
| 892 |
out_values_list = g_list_append (out_values_list, val); |
| 893 |
|
| 894 |
types = types->next; |
| 895 |
} |
| 896 |
|
| 897 |
*out_values = out_values_list; |
| 898 |
|
| 899 |
return TRUE; |
| 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 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
gboolean |
| 961 |
gupnp_service_proxy_action_get_result_hash (GUPnPServiceProxyAction *action, |
| 962 |
GHashTable *hash, |
| 963 |
GError **error) |
| 964 |
{ |
| 965 |
g_return_val_if_fail (action, FALSE); |
| 966 |
|
| 967 |
|
| 968 |
if (action->error) { |
| 969 |
g_propagate_error (error, g_error_copy (action->error)); |
| 970 |
|
| 971 |
return FALSE; |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
gupnp_service_proxy_action_check_response (action); |
| 976 |
if (action->error != NULL) { |
| 977 |
g_propagate_error (error, g_error_copy (action->error)); |
| 978 |
|
| 979 |
return FALSE; |
| 980 |
} |
| 981 |
|
| 982 |
|
| 983 |
g_hash_table_foreach (hash, |
| 984 |
(GHFunc) read_out_parameter, |
| 985 |
action->params); |
| 986 |
|
| 987 |
return TRUE; |
| 988 |
|
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
gboolean |
| 1006 |
gupnp_service_proxy_action_get_result (GUPnPServiceProxyAction *action, |
| 1007 |
GError **error, |
| 1008 |
...) |
| 1009 |
{ |
| 1010 |
g_return_val_if_fail (action, FALSE); |
| 1011 |
|
| 1012 |
va_list var_args; |
| 1013 |
|
| 1014 |
va_start (var_args, error); |
| 1015 |
GHashTable *out_hash; |
| 1016 |
va_list var_args_copy; |
| 1017 |
gboolean result; |
| 1018 |
GError *local_error; |
| 1019 |
|
| 1020 |
|
| 1021 |
out_hash = g_hash_table_new_full (g_str_hash, |
| 1022 |
g_str_equal, |
| 1023 |
g_free, |
| 1024 |
gvalue_free); |
| 1025 |
G_VA_COPY (var_args_copy, var_args); |
| 1026 |
VAR_ARGS_TO_OUT_HASH_TABLE (var_args, out_hash); |
| 1027 |
local_error = NULL; |
| 1028 |
result = gupnp_service_proxy_action_get_result_hash (action, |
| 1029 |
out_hash, |
| 1030 |
&local_error); |
| 1031 |
|
| 1032 |
if (local_error == NULL) { |
| 1033 |
OUT_HASH_TABLE_TO_VAR_ARGS (out_hash, var_args_copy); |
| 1034 |
} else { |
| 1035 |
g_propagate_error (error, local_error); |
| 1036 |
} |
| 1037 |
va_end (var_args_copy); |
| 1038 |
g_hash_table_unref (out_hash); |
| 1039 |
va_end (var_args); |
| 1040 |
|
| 1041 |
return result; |
| 1042 |
} |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
gboolean |
| 1059 |
gupnp_service_proxy_action_set (GUPnPServiceProxyAction *action, |
| 1060 |
const char *key, |
| 1061 |
const GValue *value, |
| 1062 |
GError **error) |
| 1063 |
{ |
| 1064 |
g_return_val_if_fail (key != NULL, FALSE); |
| 1065 |
g_return_val_if_fail (value != NULL, FALSE); |
| 1066 |
g_return_val_if_fail (error != NULL && *error == NULL, FALSE); |
| 1067 |
gpointer position; |
| 1068 |
|
| 1069 |
if (!g_hash_table_lookup_extended (action->arg_map, |
| 1070 |
key, |
| 1071 |
NULL, |
| 1072 |
&position)) { |
| 1073 |
g_propagate_error (error, |
| 1074 |
g_error_new (GUPNP_SERVER_ERROR, |
| 1075 |
GUPNP_SERVER_ERROR_OTHER, |
| 1076 |
"Unknown argument: %s", |
| 1077 |
key)); |
| 1078 |
|
| 1079 |
return FALSE; |
| 1080 |
} |
| 1081 |
|
| 1082 |
ActionArgument *arg = |
| 1083 |
g_ptr_array_index (action->args, GPOINTER_TO_UINT (position)); |
| 1084 |
|
| 1085 |
if (G_VALUE_TYPE (value) != G_VALUE_TYPE (&arg->value)) { |
| 1086 |
g_propagate_error ( |
| 1087 |
error, |
| 1088 |
g_error_new ( |
| 1089 |
GUPNP_SERVER_ERROR, |
| 1090 |
GUPNP_SERVER_ERROR_OTHER, |
| 1091 |
"Type mismatch for %s. Expected %s, got %s", |
| 1092 |
key, |
| 1093 |
G_VALUE_TYPE_NAME (&arg->value), |
| 1094 |
G_VALUE_TYPE_NAME (value))); |
| 1095 |
|
| 1096 |
return FALSE; |
| 1097 |
} |
| 1098 |
|
| 1099 |
g_value_copy (value, &arg->value); |
| 1100 |
|
| 1101 |
return TRUE; |
| 1102 |
} |