| 1 |
|
| 2 |
|
| 3 |
#define G_LOG_DOMAIN "gupnp-service-proxy" |
| 4 |
|
| 5 |
#include <config.h> |
| 6 |
|
| 7 |
#include "gupnp-error.h" |
| 8 |
#include "gupnp-service-private.h" |
| 9 |
#include "gupnp-service.h" |
| 10 |
#include "gupnp-xml-doc.h" |
| 11 |
#include "gvalue-util.h" |
| 12 |
#include "http-headers.h" |
| 13 |
#include "xml-util.h" |
| 14 |
|
| 15 |
#include <gobject/gvaluecollector.h> |
| 16 |
|
| 17 |
GUPnPServiceAction * |
| 18 |
gupnp_service_action_new () |
| 19 |
{ |
| 20 |
return g_atomic_rc_box_new0 (GUPnPServiceAction); |
| 21 |
} |
| 22 |
|
| 23 |
GUPnPServiceAction * |
| 24 |
gupnp_service_action_ref (GUPnPServiceAction *action) |
| 25 |
{ |
| 26 |
g_return_val_if_fail (action, NULL); |
| 27 |
|
| 28 |
return g_atomic_rc_box_acquire (action); |
| 29 |
} |
| 30 |
|
| 31 |
static void |
| 32 |
action_dispose (GUPnPServiceAction *action) |
| 33 |
{ |
| 34 |
g_free (action->name); |
| 35 |
g_object_unref (action->msg); |
| 36 |
g_object_unref (action->context); |
| 37 |
g_object_unref (action->doc); |
| 38 |
if (action->response_str) |
| 39 |
g_string_free (action->response_str, TRUE); |
| 40 |
} |
| 41 |
|
| 42 |
void |
| 43 |
gupnp_service_action_unref (struct _GUPnPServiceAction *action) |
| 44 |
{ |
| 45 |
g_return_if_fail (action); |
| 46 |
|
| 47 |
g_atomic_rc_box_release_full (action, (GDestroyNotify) action_dispose); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
GType |
| 58 |
gupnp_service_action_get_type (void) |
| 59 |
{ |
| 60 |
static GType our_type = 0; |
| 61 |
|
| 62 |
if (our_type == 0) |
| 63 |
our_type = g_boxed_type_register_static ( |
| 64 |
"GUPnPServiceAction", |
| 65 |
(GBoxedCopyFunc) gupnp_service_action_ref, |
| 66 |
(GBoxedFreeFunc) gupnp_service_action_unref); |
| 67 |
|
| 68 |
return our_type; |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
finalize_action (GUPnPServiceAction *action) |
| 73 |
{ |
| 74 |
|
| 75 |
g_string_prepend (action->response_str, |
| 76 |
"<?xml version=\"1.0\"?>" |
| 77 |
"<s:Envelope xmlns:s=" |
| 78 |
"\"http://schemas.xmlsoap.org/soap/envelope/\" " |
| 79 |
"s:encodingStyle=" |
| 80 |
"\"http://schemas.xmlsoap.org/soap/encoding/\">" |
| 81 |
"<s:Body>"); |
| 82 |
|
| 83 |
if (soup_server_message_get_status (action->msg) != |
| 84 |
SOUP_STATUS_INTERNAL_SERVER_ERROR) { |
| 85 |
g_string_append (action->response_str, "</u:"); |
| 86 |
g_string_append (action->response_str, action->name); |
| 87 |
g_string_append (action->response_str, "Response>"); |
| 88 |
} |
| 89 |
|
| 90 |
g_string_append (action->response_str, |
| 91 |
"</s:Body>" |
| 92 |
"</s:Envelope>"); |
| 93 |
|
| 94 |
SoupMessageHeaders *headers = |
| 95 |
soup_server_message_get_response_headers (action->msg); |
| 96 |
|
| 97 |
soup_message_headers_replace (headers, |
| 98 |
"Content-Type", |
| 99 |
"text/xml; charset=\"utf-8\""); |
| 100 |
|
| 101 |
if (action->accept_gzip && action->response_str->len > 1024) { |
| 102 |
|
| 103 |
|
| 104 |
http_response_set_body_gzip (action->msg, |
| 105 |
action->response_str->str, |
| 106 |
action->response_str->len); |
| 107 |
g_string_free (action->response_str, TRUE); |
| 108 |
} else { |
| 109 |
SoupMessageBody *msg_body = |
| 110 |
soup_server_message_get_response_body (action->msg); |
| 111 |
GBytes *bytes = g_string_free_to_bytes (action->response_str); |
| 112 |
soup_message_body_append_bytes (msg_body, bytes); |
| 113 |
g_bytes_unref (bytes); |
| 114 |
} |
| 115 |
action->response_str = NULL; |
| 116 |
|
| 117 |
soup_message_headers_append (headers, "Ext", ""); |
| 118 |
|
| 119 |
|
| 120 |
soup_message_headers_append ( |
| 121 |
headers, |
| 122 |
"Server", |
| 123 |
gssdp_client_get_server_id (GSSDP_CLIENT (action->context))); |
| 124 |
|
| 125 |
|
| 126 |
#if SOUP_CHECK_VERSION(3,1,2) |
| 127 |
soup_server_message_unpause (action->msg); |
| 128 |
#else |
| 129 |
SoupServer *server = gupnp_context_get_server (action->context); |
| 130 |
soup_server_unpause_message (server, action->msg); |
| 131 |
#endif |
| 132 |
|
| 133 |
|
| 134 |
gupnp_service_action_unref (action); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
const char * |
| 146 |
gupnp_service_action_get_name (GUPnPServiceAction *action) |
| 147 |
{ |
| 148 |
g_return_val_if_fail (action != NULL, NULL); |
| 149 |
|
| 150 |
return action->name; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
GList * |
| 164 |
gupnp_service_action_get_locales (GUPnPServiceAction *action) |
| 165 |
{ |
| 166 |
g_return_val_if_fail (action != NULL, NULL); |
| 167 |
|
| 168 |
return http_request_get_accept_locales ( |
| 169 |
soup_server_message_get_request_headers (action->msg)); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
void |
| 181 |
gupnp_service_action_get (GUPnPServiceAction *action, ...) |
| 182 |
{ |
| 183 |
va_list var_args; |
| 184 |
|
| 185 |
g_return_if_fail (action != NULL); |
| 186 |
|
| 187 |
va_start (var_args, action); |
| 188 |
const char *arg_name; |
| 189 |
GType arg_type; |
| 190 |
GValue value = G_VALUE_INIT; |
| 191 |
char *copy_error; |
| 192 |
|
| 193 |
g_return_if_fail (action != NULL); |
| 194 |
|
| 195 |
copy_error = NULL; |
| 196 |
|
| 197 |
arg_name = va_arg (var_args, const char *); |
| 198 |
while (arg_name) { |
| 199 |
arg_type = va_arg (var_args, GType); |
| 200 |
g_value_init (&value, arg_type); |
| 201 |
|
| 202 |
gupnp_service_action_get_value (action, arg_name, &value); |
| 203 |
|
| 204 |
G_VALUE_LCOPY (&value, var_args, 0, ©_error); |
| 205 |
|
| 206 |
g_value_unset (&value); |
| 207 |
|
| 208 |
if (copy_error) { |
| 209 |
g_warning ("Error lcopying value: %s\n", copy_error); |
| 210 |
|
| 211 |
g_free (copy_error); |
| 212 |
} |
| 213 |
|
| 214 |
arg_name = va_arg (var_args, const char *); |
| 215 |
} |
| 216 |
va_end (var_args); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
GList * |
| 235 |
gupnp_service_action_get_values (GUPnPServiceAction *action, |
| 236 |
GList *arg_names, |
| 237 |
GList *arg_types) |
| 238 |
{ |
| 239 |
GList *arg_values; |
| 240 |
guint i; |
| 241 |
|
| 242 |
g_return_val_if_fail (action != NULL, NULL); |
| 243 |
|
| 244 |
arg_values = NULL; |
| 245 |
|
| 246 |
for (i = 0; i < g_list_length (arg_names); i++) { |
| 247 |
const char *arg_name; |
| 248 |
GType arg_type; |
| 249 |
GValue *arg_value; |
| 250 |
|
| 251 |
arg_name = (const char *) g_list_nth_data (arg_names, i); |
| 252 |
arg_type = (GType) g_list_nth_data (arg_types, i); |
| 253 |
|
| 254 |
arg_value = g_slice_new0 (GValue); |
| 255 |
g_value_init (arg_value, arg_type); |
| 256 |
|
| 257 |
gupnp_service_action_get_value (action, arg_name, arg_value); |
| 258 |
|
| 259 |
arg_values = g_list_append (arg_values, arg_value); |
| 260 |
} |
| 261 |
|
| 262 |
return arg_values; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
void |
| 275 |
gupnp_service_action_get_value (GUPnPServiceAction *action, |
| 276 |
const char *argument, |
| 277 |
GValue *value) |
| 278 |
{ |
| 279 |
xmlNode *node; |
| 280 |
gboolean found; |
| 281 |
|
| 282 |
g_return_if_fail (action != NULL); |
| 283 |
g_return_if_fail (argument != NULL); |
| 284 |
g_return_if_fail (value != NULL); |
| 285 |
|
| 286 |
found = FALSE; |
| 287 |
for (node = action->node->children; node; node = node->next) { |
| 288 |
if (strcmp ((char *) node->name, argument) != 0) |
| 289 |
continue; |
| 290 |
|
| 291 |
found = gvalue_util_set_value_from_xml_node (value, node); |
| 292 |
|
| 293 |
break; |
| 294 |
} |
| 295 |
|
| 296 |
if (!found) |
| 297 |
g_warning ("Failed to retrieve '%s' argument of '%s' action", |
| 298 |
argument, |
| 299 |
action->name); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
GValue * |
| 318 |
gupnp_service_action_get_gvalue (GUPnPServiceAction *action, |
| 319 |
const char *argument, |
| 320 |
GType type) |
| 321 |
{ |
| 322 |
GValue *val; |
| 323 |
|
| 324 |
val = g_slice_new0 (GValue); |
| 325 |
g_value_init (val, type); |
| 326 |
|
| 327 |
gupnp_service_action_get_value (action, argument, val); |
| 328 |
|
| 329 |
return val; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
guint |
| 343 |
gupnp_service_action_get_argument_count (GUPnPServiceAction *action) |
| 344 |
{ |
| 345 |
return action->argument_count; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
void |
| 357 |
gupnp_service_action_set (GUPnPServiceAction *action, ...) |
| 358 |
{ |
| 359 |
va_list var_args; |
| 360 |
|
| 361 |
g_return_if_fail (action != NULL); |
| 362 |
|
| 363 |
va_start (var_args, action); |
| 364 |
const char *arg_name; |
| 365 |
GType arg_type; |
| 366 |
GValue value = G_VALUE_INIT; |
| 367 |
char *collect_error; |
| 368 |
|
| 369 |
g_return_if_fail (action != NULL); |
| 370 |
|
| 371 |
collect_error = NULL; |
| 372 |
|
| 373 |
arg_name = va_arg (var_args, const char *); |
| 374 |
while (arg_name) { |
| 375 |
arg_type = va_arg (var_args, GType); |
| 376 |
g_value_init (&value, arg_type); |
| 377 |
|
| 378 |
G_VALUE_COLLECT (&value, |
| 379 |
var_args, |
| 380 |
G_VALUE_NOCOPY_CONTENTS, |
| 381 |
&collect_error); |
| 382 |
if (!collect_error) { |
| 383 |
gupnp_service_action_set_value (action, |
| 384 |
arg_name, |
| 385 |
&value); |
| 386 |
|
| 387 |
g_value_unset (&value); |
| 388 |
|
| 389 |
} else { |
| 390 |
g_warning ("Error collecting value: %s\n", |
| 391 |
collect_error); |
| 392 |
|
| 393 |
g_free (collect_error); |
| 394 |
} |
| 395 |
|
| 396 |
arg_name = va_arg (var_args, const char *); |
| 397 |
} |
| 398 |
va_end (var_args); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
void |
| 413 |
gupnp_service_action_set_values (GUPnPServiceAction *action, |
| 414 |
GList *arg_names, |
| 415 |
GList *arg_values) |
| 416 |
{ |
| 417 |
g_return_if_fail (action != NULL); |
| 418 |
g_return_if_fail (arg_names != NULL); |
| 419 |
g_return_if_fail (arg_values != NULL); |
| 420 |
g_return_if_fail (g_list_length (arg_names) == |
| 421 |
g_list_length (arg_values)); |
| 422 |
|
| 423 |
if (soup_server_message_get_status (action->msg) == |
| 424 |
SOUP_STATUS_INTERNAL_SERVER_ERROR) { |
| 425 |
g_warning ("Calling gupnp_service_action_set_value() after " |
| 426 |
"having called gupnp_service_action_return_error() " |
| 427 |
"is not allowed."); |
| 428 |
|
| 429 |
return; |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
for (; arg_names; arg_names = arg_names->next) { |
| 434 |
const char *arg_name; |
| 435 |
GValue *value; |
| 436 |
|
| 437 |
arg_name = arg_names->data; |
| 438 |
value = arg_values->data; |
| 439 |
|
| 440 |
xml_util_start_element (action->response_str, arg_name); |
| 441 |
gvalue_util_value_append_to_xml_string (value, |
| 442 |
action->response_str); |
| 443 |
xml_util_end_element (action->response_str, arg_name); |
| 444 |
|
| 445 |
arg_values = arg_values->next; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
void |
| 458 |
gupnp_service_action_set_value (GUPnPServiceAction *action, |
| 459 |
const char *argument, |
| 460 |
const GValue *value) |
| 461 |
{ |
| 462 |
g_return_if_fail (action != NULL); |
| 463 |
g_return_if_fail (argument != NULL); |
| 464 |
g_return_if_fail (value != NULL); |
| 465 |
|
| 466 |
if (soup_server_message_get_status (action->msg) == |
| 467 |
SOUP_STATUS_INTERNAL_SERVER_ERROR) { |
| 468 |
g_warning ("Calling gupnp_service_action_set_value() after " |
| 469 |
"having called gupnp_service_action_return_error() " |
| 470 |
"is not allowed."); |
| 471 |
|
| 472 |
return; |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
xml_util_start_element (action->response_str, argument); |
| 477 |
gvalue_util_value_append_to_xml_string (value, action->response_str); |
| 478 |
xml_util_end_element (action->response_str, argument); |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
void |
| 490 |
gupnp_service_action_return_success (GUPnPServiceAction *action) |
| 491 |
{ |
| 492 |
g_return_if_fail (action != NULL); |
| 493 |
|
| 494 |
soup_server_message_set_status (action->msg, SOUP_STATUS_OK, NULL); |
| 495 |
|
| 496 |
finalize_action (action); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
void |
| 512 |
gupnp_service_action_return_error (GUPnPServiceAction *action, |
| 513 |
guint error_code, |
| 514 |
const char *error_description) |
| 515 |
{ |
| 516 |
g_return_if_fail (action != NULL); |
| 517 |
|
| 518 |
switch (error_code) { |
| 519 |
case GUPNP_CONTROL_ERROR_INVALID_ACTION: |
| 520 |
if (error_description == NULL) |
| 521 |
error_description = "Invalid Action"; |
| 522 |
|
| 523 |
break; |
| 524 |
case GUPNP_CONTROL_ERROR_INVALID_ARGS: |
| 525 |
if (error_description == NULL) |
| 526 |
error_description = "Invalid Args"; |
| 527 |
|
| 528 |
break; |
| 529 |
case GUPNP_CONTROL_ERROR_OUT_OF_SYNC: |
| 530 |
if (error_description == NULL) |
| 531 |
error_description = "Out of Sync"; |
| 532 |
|
| 533 |
break; |
| 534 |
case GUPNP_CONTROL_ERROR_ACTION_FAILED: |
| 535 |
if (error_description == NULL) |
| 536 |
error_description = "Action Failed"; |
| 537 |
|
| 538 |
break; |
| 539 |
default: |
| 540 |
g_return_if_fail (error_description != NULL); |
| 541 |
break; |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
g_string_erase (action->response_str, 0, -1); |
| 546 |
|
| 547 |
xml_util_start_element (action->response_str, "s:Fault"); |
| 548 |
|
| 549 |
xml_util_start_element (action->response_str, "faultcode"); |
| 550 |
g_string_append (action->response_str, "s:Client"); |
| 551 |
xml_util_end_element (action->response_str, "faultcode"); |
| 552 |
|
| 553 |
xml_util_start_element (action->response_str, "faultstring"); |
| 554 |
g_string_append (action->response_str, "UPnPError"); |
| 555 |
xml_util_end_element (action->response_str, "faultstring"); |
| 556 |
|
| 557 |
xml_util_start_element (action->response_str, "detail"); |
| 558 |
|
| 559 |
xml_util_start_element (action->response_str, |
| 560 |
"UPnPError " |
| 561 |
"xmlns=\"urn:schemas-upnp-org:control-1-0\""); |
| 562 |
|
| 563 |
xml_util_start_element (action->response_str, "errorCode"); |
| 564 |
g_string_append_printf (action->response_str, "%u", error_code); |
| 565 |
xml_util_end_element (action->response_str, "errorCode"); |
| 566 |
|
| 567 |
xml_util_start_element (action->response_str, "errorDescription"); |
| 568 |
xml_util_add_content (action->response_str, error_description); |
| 569 |
xml_util_end_element (action->response_str, "errorDescription"); |
| 570 |
|
| 571 |
xml_util_end_element (action->response_str, "UPnPError"); |
| 572 |
xml_util_end_element (action->response_str, "detail"); |
| 573 |
|
| 574 |
xml_util_end_element (action->response_str, "s:Fault"); |
| 575 |
|
| 576 |
soup_server_message_set_status (action->msg, |
| 577 |
SOUP_STATUS_INTERNAL_SERVER_ERROR, |
| 578 |
"Internal server error"); |
| 579 |
|
| 580 |
finalize_action (action); |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
SoupServerMessage * |
| 596 |
gupnp_service_action_get_message (GUPnPServiceAction *action) |
| 597 |
{ |
| 598 |
return g_object_ref (action->msg); |
| 599 |
} |