[gssdp] browser: Also rewrite AL headers



commit f763a1508807d5cfb83eef41aedb78cdf40846c7
Author: Jens Georg <mail jensge org>
Date:   Sun Nov 4 10:35:58 2018 +0100

    browser: Also rewrite AL headers

 libgssdp/gssdp-resource-browser.c | 78 +++++++++++++++++++++++----------------
 1 file changed, 46 insertions(+), 32 deletions(-)
---
diff --git a/libgssdp/gssdp-resource-browser.c b/libgssdp/gssdp-resource-browser.c
index eae064b..30dadc0 100644
--- a/libgssdp/gssdp-resource-browser.c
+++ b/libgssdp/gssdp-resource-browser.c
@@ -705,6 +705,35 @@ resource_expire (gpointer user_data)
         return FALSE;
 }
 
+static char *
+rewrite_uri (const char *header, int index)
+{
+        const char *host = NULL;
+        SoupURI *uri = NULL;
+        GInetAddress *addr = NULL;
+        char *retval = NULL;
+
+        uri = soup_uri_new (header);
+        host = soup_uri_get_host (uri);
+        addr = g_inet_address_new_from_string (host);
+
+        if (g_inet_address_get_is_link_local (addr)) {
+                char *new_host;
+
+                new_host = g_strdup_printf ("%s%%%d",
+                                host,
+                                index);
+                soup_uri_set_host (uri, new_host);
+                g_free (new_host);
+        }
+
+        g_object_unref (addr);
+        retval = soup_uri_to_string (uri, FALSE);
+        soup_uri_free (uri);
+
+        return retval;
+}
+
 static void
 resource_available (GSSDPResourceBrowser *resource_browser,
                     SoupMessageHeaders   *headers)
@@ -719,47 +748,25 @@ resource_available (GSSDPResourceBrowser *resource_browser,
         gboolean destroyLocations;
         GList *it1, *it2;
         char *canonical_usn;
+        GSocketFamily family = G_SOCKET_FAMILY_INVALID;
+        int index;
 
         priv = gssdp_resource_browser_get_instance_private (resource_browser);
         usn = soup_message_headers_get_one (headers, "USN");
         if (!usn)
                 return; /* No USN specified */
 
+        family = gssdp_client_get_family (priv->client);
+        index = gssdp_client_get_index (priv->client);
+
         /* Build list of locations */
         locations = NULL;
         destroyLocations = TRUE;
 
         header = soup_message_headers_get_one (headers, "Location");
         if (header) {
-                GSocketFamily family;
-                GSSDPClient *client;
-
-                client = priv->client;
-                family = gssdp_client_get_family (client);
-
                 if (family == G_SOCKET_FAMILY_IPV6) {
-                        SoupURI *uri = soup_uri_new (header);
-                        const char *host = NULL;
-                        GInetAddress *addr = NULL;
-
-                        host = soup_uri_get_host (uri);
-                        addr = g_inet_address_new_from_string (host);
-                        if (g_inet_address_get_is_link_local (addr)) {
-                                char *new_host;
-                                int index = 0;
-
-                                index = gssdp_client_get_index (client);
-
-                                new_host = g_strdup_printf ("%s%%%d",
-                                                            host,
-                                                            index);
-                                soup_uri_set_host (uri, new_host);
-                        }
-                        g_object_unref (addr);
-                        locations = g_list_append (locations,
-                                                   soup_uri_to_string (uri,
-                                                                       FALSE));
-                        soup_uri_free (uri);
+                        locations = g_list_append (locations, rewrite_uri (header, index));
                 } else {
                         locations = g_list_append (locations, g_strdup (header));
                 }
@@ -769,8 +776,8 @@ resource_available (GSSDPResourceBrowser *resource_browser,
         if (header) {
                 /* Parse AL header. The format is:
                  * <uri1><uri2>... */
-                const char *start, *end;
-                char *uri;
+                const char *start;
+                char *end;
 
                 start = header;
                 while ((start = strchr (start, '<'))) {
@@ -782,8 +789,15 @@ resource_available (GSSDPResourceBrowser *resource_browser,
                         if (!end || !*end)
                                 break;
 
-                        uri = g_strndup (start, end - start);
-                        locations = g_list_append (locations, uri);
+                        if (family == G_SOCKET_FAMILY_IPV6) {
+                                *end = '\0';
+                                locations = g_list_append (locations, rewrite_uri (start, index));
+                                *end = '>';
+                        } else {
+                                char *uri;
+                                uri = g_strndup (start, end - start);
+                                locations = g_list_append (locations, uri);
+                        }
 
                         start = end;
                 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]