[gupnp] service: Use SoupURI in call-backs
- From: Jens Georg <jensgeorg src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gupnp] service: Use SoupURI in call-backs
- Date: Tue, 16 Jun 2020 21:26:11 +0000 (UTC)
commit 4d887c38d18f4994918bb534fd2dac61775dedc6
Author: Jens Georg <mail jensge org>
Date: Tue Jun 16 21:25:03 2020 +0200
service: Use SoupURI in call-backs
libgupnp/gupnp-context-private.h | 4 +++
libgupnp/gupnp-context.c | 56 +++++++++++++++++++++++++++++++-------
libgupnp/gupnp-service.c | 58 +++++++++++++++++++---------------------
3 files changed, 77 insertions(+), 41 deletions(-)
---
diff --git a/libgupnp/gupnp-context-private.h b/libgupnp/gupnp-context-private.h
index 513d774..ed39300 100644
--- a/libgupnp/gupnp-context-private.h
+++ b/libgupnp/gupnp-context-private.h
@@ -36,6 +36,10 @@ _gupnp_context_add_server_handler_with_data (GUPnPContext *context,
const char *path,
AclServerHandler *data);
+G_GNUC_INTERNAL SoupURI *
+gupnp_context_rewrite_uri_to_uri (GUPnPContext *context,
+ const char *uri);
+
G_END_DECLS
#endif /* GUPNP_CONTEXT_PRIVATE_H */
diff --git a/libgupnp/gupnp-context.c b/libgupnp/gupnp-context.c
index 022910d..00e7abf 100644
--- a/libgupnp/gupnp-context.c
+++ b/libgupnp/gupnp-context.c
@@ -1618,21 +1618,59 @@ gupnp_context_remove_server_handler (GUPnPContext *context, const char *path)
* the zone index appended to the IP address.
*
* Returns: A re-written version of the @uri if the context is on a link-local
- * IPv6 address, a copy of the @uri otherwise.
+ * IPv6 address, a copy of the @uri otherwise or %NULL if @uri was invalid
*
* Since: 1.1.1
*/
char *
-gupnp_context_rewrite_uri (GUPnPContext *context, const char *plain_uri)
+gupnp_context_rewrite_uri (GUPnPContext *context, const char *uri)
+{
+ SoupURI *soup_uri = NULL;
+ char *retval = NULL;
+
+ soup_uri = gupnp_context_rewrite_uri_to_uri (context, uri);
+ if (soup_uri == NULL) {
+ // We already issued a warning above, so just return
+ return NULL;
+ }
+
+ retval = soup_uri_to_string (soup_uri, FALSE);
+ soup_uri_free (soup_uri);
+
+ return retval;
+}
+
+/**
+ * gupnp_context_rewrite_uri_to_uri:
+ * @context: a #GUPnPContext
+ * @uri: an uri to rewrite if necessary
+ *
+ * Utility function to re-write an uri to the IPv6 link-local form which has
+ * the zone index appended to the IP address.
+ *
+ * Returns: A re-written version of the @uri if the context is on a link-local
+ * IPv6 address or @uri converted to a #SoupURI or %NULL if @uri is invalid
+ *
+ * Since: 1.2.3
+ * Stability: Private
+ */
+SoupURI *
+gupnp_context_rewrite_uri_to_uri (GUPnPContext *context, const char *uri)
{
const char *host = NULL;
- SoupURI *uri = NULL;
+ SoupURI *soup_uri = NULL;
GInetAddress *addr = NULL;
- char *retval = NULL;
int index = -1;
- uri = soup_uri_new (plain_uri);
- host = soup_uri_get_host (uri);
+ soup_uri = soup_uri_new (uri);
+
+ if (soup_uri == NULL) {
+ g_warning ("Invalid call-back url: %s", uri);
+
+ return NULL;
+ }
+
+ host = soup_uri_get_host (soup_uri);
addr = g_inet_address_new_from_string (host);
index = gssdp_client_get_index (GSSDP_CLIENT (context));
@@ -1643,13 +1681,11 @@ gupnp_context_rewrite_uri (GUPnPContext *context, const char *plain_uri)
new_host = g_strdup_printf ("%s%%%d",
host,
index);
- soup_uri_set_host (uri, new_host);
+ soup_uri_set_host (soup_uri, new_host);
g_free (new_host);
}
g_object_unref (addr);
- retval = soup_uri_to_string (uri, FALSE);
- soup_uri_free (uri);
- return retval;
+ return soup_uri;
}
diff --git a/libgupnp/gupnp-service.c b/libgupnp/gupnp-service.c
index ce25ca5..a9ac37f 100644
--- a/libgupnp/gupnp-service.c
+++ b/libgupnp/gupnp-service.c
@@ -190,7 +190,7 @@ subscription_data_free (SubscriptionData *data)
}
/* Further cleanup */
- g_list_free_full (data->callbacks, g_free);
+ g_list_free_full (data->callbacks, (GDestroyNotify) soup_uri_free);
g_free (data->sid);
@@ -1217,13 +1217,14 @@ subscribe (GUPnPService *service,
break;
if (strncmp (start, "http://", strlen ("http://")) == 0) {
- char *local_uri;
+ SoupURI *local_uri;
uri = g_strndup (start, end - start);
- local_uri = gupnp_context_rewrite_uri (context, uri);
+ local_uri = gupnp_context_rewrite_uri_to_uri (context, uri);
g_free (uri);
-
- data->callbacks = g_list_append (data->callbacks, local_uri);
+ if (local_uri != NULL) {
+ data->callbacks = g_list_append (data->callbacks, local_uri);
+ }
}
start = end;
@@ -1945,32 +1946,27 @@ notify_got_response (G_GNUC_UNUSED SoupSession *session,
}
/* Send notification @user_data to subscriber @value */
-static void
-notify_subscriber (G_GNUC_UNUSED gpointer key,
- gpointer value,
- gpointer user_data)
-{
- SubscriptionData *data;
- const char *property_set;
- char *tmp;
- SoupMessage *msg;
- SoupSession *session;
-
- data = value;
- property_set = user_data;
-
- /* Subscriber called unsubscribe */
- if (subscription_data_can_delete (data))
- return;
-
- /* Create message */
- msg = soup_message_new (GENA_METHOD_NOTIFY, data->callbacks->data);
- if (!msg) {
- g_warning ("Invalid callback URL: %s",
- (char *) data->callbacks->data);
-
- return;
- }
+ static void
+ notify_subscriber (G_GNUC_UNUSED gpointer key,
+ gpointer value,
+ gpointer user_data)
+ {
+ SubscriptionData *data;
+ const char *property_set;
+ char *tmp;
+ SoupMessage *msg;
+ SoupSession *session;
+
+ data = value;
+ property_set = user_data;
+
+ /* Subscriber called unsubscribe */
+ if (subscription_data_can_delete (data))
+ return;
+
+ /* Create message */
+ msg = soup_message_new_from_uri (GENA_METHOD_NOTIFY,
+ data->callbacks->data);
soup_message_headers_append (msg->request_headers,
"NT",
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]