[glib-networking/pgriffis/libsoup-dynamic] Dynamically load libsoup to request URIs




commit 5d57148fb860e441c6e8b8bf01ab5c960677a5a5
Author: Patrick Griffis <pgriffis igalia com>
Date:   Tue Apr 20 11:48:15 2021 -0500

    Dynamically load libsoup to request URIs

 meson.build                      |  13 ++++
 tls/base/gtls-http.c             | 140 +++++++++++++++++++++++++++++++++++++++
 tls/base/gtls-http.h             |  31 +++++++++
 tls/base/meson.build             |   4 +-
 tls/gnutls/gtlsdatabase-gnutls.c |   9 +--
 5 files changed, 190 insertions(+), 7 deletions(-)
---
diff --git a/meson.build b/meson.build
index dc58838..7bba35b 100644
--- a/meson.build
+++ b/meson.build
@@ -75,6 +75,19 @@ gsettings_desktop_schemas_dep = dependency('gsettings-desktop-schemas', required
 
 backends = []
 
+# *** Check for dl          ***
+dl_dep = dependency('dl', required: false)
+dl_link_args = ''
+have_dlopen = cc.has_function('dlopen',
+  prefix: '#include <dlfcn.h>',
+  args: '-ldl',
+  dependencies: dl_dep,
+)
+if have_dlopen and not dl_dep.found()
+  dl_link_args = '-ldl'
+endif
+config_h.set('HAVE_DLOPEN', have_dlopen)
+
 # *** Checks for GnuTLS     ***
 gnutls_dep = dependency('gnutls', version: '>= 3.6.5', required: get_option('gnutls'))
 
diff --git a/tls/base/gtls-http.c b/tls/base/gtls-http.c
new file mode 100644
index 0000000..3c8f268
--- /dev/null
+++ b/tls/base/gtls-http.c
@@ -0,0 +1,140 @@
+/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ * GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright 2021 Igalia S.L.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * In addition, when the library is used with OpenSSL, a special
+ * exception applies. Refer to the LICENSE_EXCEPTION file for details.
+ */
+
+#include "config.h"
+
+#ifdef HAVE_DLOPEN
+#include <dlfcn.h>
+#endif
+
+#include "gtls-http.h"
+
+typedef gpointer SoupSession;
+typedef gpointer SoupMessage;
+
+static SoupSession *(*soup_session_new)(void);
+static SoupMessage *(*soup_message_new)(const char *method, const char *uri);
+static GInputStream *(*soup_session_send)(SoupSession *, SoupMessage *, GCancellable *, GError **);
+
+static gsize libsoup_initialized;
+static GModule *libsoup_module;
+
+static void
+init_libsoup (void)
+{
+#ifdef HAVE_DLOPEN
+  const char * libsoup_sonames[3] = { 0 };
+  gpointer handle = NULL;
+
+  g_assert (g_module_supported ());
+
+  /* In order to avoid causing conflicts we detect if libsoup 2 or 3 is loaded already.
+   * If so use that. Otherwise we will try to load our own version to use preferring 3. */
+
+  if ((handle = dlopen ("libsoup-3.0.so.0", RTLD_NOLOAD)))
+    libsoup_sonames[0] = "libsoup-3.0.so.0";
+  else if ((handle = dlopen ("libsoup-2.4.so.1", RTLD_NOLOAD)))
+    libsoup_sonames[0] = "libsoup-2.4.so.1";
+  else
+    {
+      libsoup_sonames[0] = "libsoup-3.0.so.0";
+      libsoup_sonames[1] = "libsoup-2.4.so.1";
+    }
+
+  if (handle)
+    g_clear_pointer (&handle, dlclose);
+
+  for (guint i = 0; libsoup_sonames[i]; i++)
+    {
+      libsoup_module = g_module_open (libsoup_sonames[i], G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
+      if (libsoup_module)
+        {
+          g_debug ("Loaded %s", g_module_name (libsoup_module));
+          if (!g_module_symbol (libsoup_module, "soup_session_new", (gpointer *)&soup_session_new) ||
+              !g_module_symbol (libsoup_module, "soup_message_new", (gpointer *)&soup_message_new) ||
+              !g_module_symbol (libsoup_module, "soup_session_send", (gpointer *)&soup_session_send))
+            {
+              g_debug ("Failed to find all libsoup symbols");
+              g_clear_pointer (&libsoup_module, g_module_close);
+              continue;
+            }
+          break;
+        }
+    }
+
+  if (!libsoup_module)
+    g_debug ("Failed to load libsoup");
+
+#endif
+}
+
+/**
+ * g_tls_request_uri:
+ * @uri: An HTTP URI to request
+ * @cancellable: (nullable): A #GCancellable
+ * @error: A #GError
+ *
+ * Synchronously requests an HTTP uri using the best available method.
+ *
+ * Note this is thread-safe.
+ *
+ * Returns: A #GInputStream of the response body or %NULL on failure
+ */
+GInputStream *
+g_tls_request_uri (const char    *uri,
+                   GCancellable  *cancellable,
+                   GError       **error)
+{
+  GInputStream *istream = NULL;
+
+  if (g_once_init_enter (&libsoup_initialized))
+    {
+      init_libsoup ();
+      g_once_init_leave (&libsoup_initialized, TRUE);
+    }
+
+  if (libsoup_module)
+    {
+      SoupSession *session = soup_session_new ();
+      SoupMessage *message = soup_message_new ("GET", uri);
+
+      if (!message)
+          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Failed to parse URI \"%s\"", uri);
+      else
+        {
+          istream = soup_session_send (session, message, cancellable, error);
+          g_object_unref (message);
+        }
+
+      g_object_unref (session);
+    }
+  else
+    {
+      GFile *file = g_file_new_for_uri (uri);
+      istream = G_INPUT_STREAM (g_file_read (file, cancellable, error));
+      g_object_unref (file);
+    }
+
+  return istream;
+}
diff --git a/tls/base/gtls-http.h b/tls/base/gtls-http.h
new file mode 100644
index 0000000..9e527ea
--- /dev/null
+++ b/tls/base/gtls-http.h
@@ -0,0 +1,31 @@
+/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ * GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright 2021 Igalia S.L.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * In addition, when the library is used with OpenSSL, a special
+ * exception applies. Refer to the LICENSE_EXCEPTION file for details.
+ */
+
+#pragma once
+
+#include <gio/gio.h>
+
+GInputStream *g_tls_request_uri (const char    *uri,
+                                 GCancellable  *cancellable,
+                                 GError       **error);
diff --git a/tls/base/meson.build b/tls/base/meson.build
index ca7d5a3..506a5e1 100644
--- a/tls/base/meson.build
+++ b/tls/base/meson.build
@@ -3,11 +3,13 @@ tlsbase_sources = files(
   'gtlsinputstream.c',
   'gtlslog.c',
   'gtlsoutputstream.c',
+  'gtls-http.c',
 )
 
 tlsbase = static_library('tlsbase',
                          tlsbase_sources,
-                         dependencies: gio_dep,
+                         dependencies: [gio_dep, dl_dep],
+                         link_args: dl_link_args,
                          include_directories: top_inc)
 
 tlsbase_dep = declare_dependency(link_with: tlsbase,
diff --git a/tls/gnutls/gtlsdatabase-gnutls.c b/tls/gnutls/gtlsdatabase-gnutls.c
index 41fccb2..7b27089 100644
--- a/tls/gnutls/gtlsdatabase-gnutls.c
+++ b/tls/gnutls/gtlsdatabase-gnutls.c
@@ -34,6 +34,7 @@
 #include <gnutls/x509.h>
 
 #include "gtlscertificate-gnutls.h"
+#include "gtls-http.h"
 
 typedef struct
 {
@@ -595,7 +596,6 @@ issuer_missing_cb (gnutls_x509_trust_list_t   tlist,
   GTlsDatabaseGnutls *self = gnutls_x509_trust_list_get_ptr (tlist);
   GTlsDatabaseGnutlsPrivate *priv = g_tls_database_gnutls_get_instance_private (self);
   gnutls_datum_t datum;
-  GFile *file = NULL;
   GFileInputStream *istream = NULL;
   char *aia = NULL;
   char *scheme = NULL;
@@ -659,11 +659,10 @@ issuer_missing_cb (gnutls_x509_trust_list_t   tlist,
       goto out;
     }
 
-  file = g_file_new_for_uri (aia);
-  istream = g_file_read (file, priv->verify_chain_cancellable, &error);
+  istream = g_tls_request_uri (aia, priv->verify_chain_cancellable, &error);
   if (!istream)
     {
-      g_warning ("Failed to download missing issuer certificate from Authority Information Access URI %s: 
failed g_file_read (do you need to install gvfs?): %s",
+      g_warning ("Failed to download missing issuer certificate from Authority Information Access URI %s: 
%s",
                  aia, error->message);
       goto out;
     }
@@ -700,8 +699,6 @@ issuer_missing_cb (gnutls_x509_trust_list_t   tlist,
 out:
   if (error)
     g_error_free (error);
-  if (file)
-    g_object_unref (file);
   if (istream)
     g_object_unref (istream);
   if (der)


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