From dbcc94e02a88974967495303b69e487713d2cfdb Mon Sep 17 00:00:00 2001
From: Michael Catanzaro <mcatanzaro@redhat.com>
Date: Wed, 18 Aug 2021 17:03:34 -0500
Subject: [PATCH] Avoid direct use of libproxy

GProxyResolver has been around since GLib 2.26. There's no reason to use
libproxy directly anymore. GProxyResolver will call into libproxy if
GLib thinks it's a good idea to do so, except in distributions that are
configured to build glib-networking without libproxy support.

Note that the existing code in get_proxy_list() was unsafely using
g_autofree and g_free() to free the memory allocated by libproxy. With
the switch to GProxyResolver, the memory is actually allocated by GLib
now, so this is now actually correct to do.

Let's also initialize l = NULL just in case some weird bug results in a
non-NULL strv of zero length being returned. It should never happen, but
we would wind up using l uninitialize did it did.

Signed-off-by: guojin17 <guoj17@chinatelecom.cn>
---
 configure.ac      |  3 ---
 src/lib/curl.c    |  2 +-
 src/lib/proxies.c | 37 +++++++++++--------------------------
 3 files changed, 12 insertions(+), 30 deletions(-)

diff --git a/configure.ac b/configure.ac
index fa97fa7..3e761e9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -224,9 +224,6 @@ PKG_CHECK_MODULES([GIO], [
 PKG_CHECK_MODULES([GOBJECT], [gobject-2.0])
 PKG_CHECK_MODULES([LIBXML], [libxml-2.0])
 PKG_CHECK_MODULES([CURL], [libcurl])
-PKG_CHECK_MODULES([PROXY], [libproxy-1.0], [
-    AC_DEFINE([HAVE_PROXY], [1], [Use libproxy])
-], [:])
 PKG_CHECK_MODULES([SATYR], [satyr])
 PKG_CHECK_MODULES([JOURNAL], [libsystemd])
 PKG_CHECK_MODULES([AUGEAS], [augeas])
diff --git a/src/lib/curl.c b/src/lib/curl.c
index 9b7920a..78080bc 100644
--- a/src/lib/curl.c
+++ b/src/lib/curl.c
@@ -102,7 +102,7 @@ CURLcode curl_easy_perform_with_proxy(CURL *handle, const char *url)
         curl_err = curl_easy_perform(handle);
     }
 
-    g_list_free_full(proxy_list, free);
+    g_list_free_full(proxy_list, g_free);
 
     return curl_err;
 }
diff --git a/src/lib/proxies.c b/src/lib/proxies.c
index 3b46d9d..e60ef46 100644
--- a/src/lib/proxies.c
+++ b/src/lib/proxies.c
@@ -19,49 +19,34 @@
 #include "internal_libreport.h"
 #include "proxies.h"
 
-#ifdef HAVE_PROXY
-#include <proxy.h>
-
-static pxProxyFactory *px_factory;
+#include <gio/gio.h>
 
 GList *get_proxy_list(const char *url)
 {
     int i;
-    GList *l;
-    g_autofree char **proxies = NULL;
+    GList *l = NULL;
+    GProxyResolver *resolver;
+    g_auto(GStrv) proxies = NULL;
+    g_autoptr(GError) error = NULL;
 
-    if (!px_factory)
-    {
-        px_factory = px_proxy_factory_new();
-        if (!px_factory)
-            return NULL;
-    }
+    resolver = g_proxy_resolver_get_default();
 
-    /* Cast to char * is needed with libproxy versions before 0.4.0 */
-    proxies = px_proxy_factory_get_proxies(px_factory, (char *)url);
+    proxies = g_proxy_resolver_lookup(resolver, url, NULL, &error);
     if (!proxies)
+    {
+        log_warning("Failed to perform proxy lookup for %s: %s", url, error->message);
         return NULL;
+    }
 
     for (i = 0, l = NULL; proxies[i]; i++)
-        l = g_list_append(l, proxies[i]);
+        l = g_list_append(l, g_steal_pointer(&proxies[i]));
 
     /* Don't set proxy if the list contains just "direct://" */
     if (l && !g_list_next(l) && !strcmp(l->data, "direct://"))
     {
-        g_free(l->data);
         g_list_free(l);
         l = NULL;
     }
 
     return l;
 }
-
-#else
-
-GList *get_proxy_list(const char *url)
-{
-    /* Without libproxy just return an empty list */
-    return NULL;
-}
-
-#endif
-- 
1.8.3.1

