Debian Codegraph @

libgupnp/gupnp-service-action.c

raw symbols SCIP
1 // SPDX-License-Identifier: LGPL-2.1-or-later
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 * gupnp_service_action_get_type:
52 *
53 * Get the gtype for GUPnPServiceActon
54 *
55 * Return value: The gtype of GUPnPServiceAction
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 /* Embed action->response_str in a SOAP document */
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 // Fixme: Probably easier to use an output stream converter
103 // instead
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 /* Server header on response */
120 soup_message_headers_append (
121 headers,
122 "Server",
123 gssdp_client_get_server_id (GSSDP_CLIENT (action->context)));
124
125 /* Tell soup server that response is now ready */
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 /* Cleanup */
134 gupnp_service_action_unref (action);
135 }
136
137 /**
138 * gupnp_service_action_get_name:
139 * @action: A #GUPnPServiceAction
140 *
141 * Get the name of @action.
142 *
143 * Return value: The name of @action
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 * gupnp_service_action_get_locales:
155 * @action: A #GUPnPServiceAction
156 *
157 * Get an ordered (preferred first) #GList of locales preferred by
158 * the client. Free list and elements after use.
159 *
160 * Return value: (element-type utf8) (transfer full): A #GList of #char*
161 * locale names.
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 * gupnp_service_action_get:
174 * @action: A #GUPnPServiceAction
175 * @...: tuples of argument name, argument type, and argument value
176 * location, terminated with %NULL.
177 *
178 * Retrieves the specified action arguments.
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, &copy_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 * gupnp_service_action_get_values:
222 * @action: A #GUPnPServiceAction
223 * @arg_names: (element-type utf8) : A #GList of argument names as string
224 * @arg_types: (element-type GType): A #GList of argument types as #GType
225 *
226 * A variant of #gupnp_service_action_get that uses #GList instead of varargs.
227 *
228 * Return value: (element-type GValue) (transfer full): The values as #GList of
229 * #GValue. g_list_free() the returned list and g_value_unset() and g_slice_free()
230 * each element.
231 *
232 * Since: 0.14.0
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 * gupnp_service_action_get_value: (skip)
267 * @action: A #GUPnPServiceAction
268 * @argument: The name of the argument to retrieve
269 * @value: (inout):The #GValue to store the value of the argument, initialized
270 * to the correct type.
271 *
272 * Retrieves the value of @argument into @value.
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 * gupnp_service_action_get_gvalue: (rename-to gupnp_service_action_get_value)
304 * @action: A #GUPnPServiceAction
305 * @argument: The name of the argument to retrieve
306 * @type: The type of argument to retrieve
307 *
308 * Retrieves the value of @argument into a GValue of type @type and returns it.
309 * The method exists only and only to satify PyGI, please use
310 * gupnp_service_action_get_value() and ignore this if possible.
311 *
312 * Return value: (transfer full): Value as #GValue associated with @action.
313 * g_value_unset() and g_slice_free() it after usage.
314 *
315 * Since: 0.14.0
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 * gupnp_service_action_get_argument_count:
334 * @action: A #GUPnPServiceAction
335 *
336 * Get the number of IN arguments from the @action and return it.
337 *
338 * Return value: The number of IN arguments from the @action.
339 *
340 * Since: 0.18.0
341 */
342 guint
343 gupnp_service_action_get_argument_count (GUPnPServiceAction *action)
344 {
345 return action->argument_count;
346 }
347
348 /**
349 * gupnp_service_action_set:
350 * @action: A #GUPnPServiceAction
351 * @...: tuples of return value name, return value type, and
352 * actual return value, terminated with %NULL.
353 *
354 * Sets the specified action return values.
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 * gupnp_service_action_set_values:
403 * @action: A #GUPnPServiceAction
404 * @arg_names: (element-type utf8) (transfer none): A #GList of argument names
405 * @arg_values: (element-type GValue) (transfer none): The #GList of values (as
406 * #GValue<!-- -->s) that line up with @arg_names.
407 *
408 * Sets the specified action return values.
409 *
410 * Since: 0.14.0
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 /* Append to response */
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 * gupnp_service_action_set_value:
451 * @action: A #GUPnPServiceAction
452 * @argument: The name of the return value to retrieve
453 * @value: The #GValue to store the return value
454 *
455 * Sets the value of @argument to @value.
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 /* Append to response */
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 * gupnp_service_action_return_success:
483 * @action: A #GUPnPServiceAction
484 *
485 * Return successfully.
486 *
487 * Since: 1.4.2
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 * gupnp_service_action_return_error:
501 * @action: A #GUPnPServiceAction
502 * @error_code: The error code
503 * @error_description: The error description, or %NULL if @error_code is
504 * one of #GUPNP_CONTROL_ERROR_INVALID_ACTION,
505 * #GUPNP_CONTROL_ERROR_INVALID_ARGS, #GUPNP_CONTROL_ERROR_OUT_OF_SYNC or
506 * #GUPNP_CONTROL_ERROR_ACTION_FAILED, in which case a description is
507 * provided automatically.
508 *
509 * Return @error_code.
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 /* Replace response_str with a SOAP Fault */
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 * gupnp_service_action_get_message:
585 * @action: A #GUPnPServiceAction
586 *
587 * Get the #SoupMessage associated with @action. Mainly intended for
588 * applications to be able to read HTTP headers received from clients.
589 *
590 * Return value: (transfer full): #SoupServerMessage associated with @action.
591 *Unref after using it.
592 *
593 * Since: 0.14.0
594 **/
595 SoupServerMessage *
596 gupnp_service_action_get_message (GUPnPServiceAction *action)
597 {
598 return g_object_ref (action->msg);
599 }