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




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

    Dynamically load libsoup to request URIs

 meson.build                      |   4 ++
 tls/base/gtls-http.c             | 143 +++++++++++++++++++++++++++++++++++++++
 tls/base/gtls-http.h             |  31 +++++++++
 tls/base/meson.build             |   3 +-
 tls/gnutls/gtlsdatabase-gnutls.c |  13 ++--
 5 files changed, 185 insertions(+), 9 deletions(-)
---
diff --git a/meson.build b/meson.build
index dc58838..768cdf2 100644
--- a/meson.build
+++ b/meson.build
@@ -75,6 +75,10 @@ gsettings_desktop_schemas_dep = dependency('gsettings-desktop-schemas', required
 
 backends = []
 
+# *** Check for dl          ***
+have_rtld_noload = cc.has_header_symbol('dlfcn.h', 'RTLD_NOLOAD')
+config_h.set('HAVE_RTLD_NOLOAD', have_rtld_noload)
+
 # *** 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..9f87dba
--- /dev/null
+++ b/tls/base/gtls-http.c
@@ -0,0 +1,143 @@
+/* -*- 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_RTLD_NOLOAD
+#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;
+
+#define LIBSOUP_3_SONAME "libsoup-3.0.so.0"
+#define LIBSOUP_2_SONAME "libsoup-2.4.so.1"
+
+static void
+init_libsoup (void)
+{
+#ifdef HAVE_RTLD_NOLOAD
+  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_SONAME, RTLD_NOLOAD)))
+    libsoup_sonames[0] = LIBSOUP_3_SONAME;
+  else if ((handle = dlopen (LIBSOUP_2_SONAME, RTLD_NOLOAD)))
+    libsoup_sonames[0] = LIBSOUP_2_SONAME;
+  else
+    {
+      libsoup_sonames[0] = LIBSOUP_3_SONAME;
+      libsoup_sonames[1] = LIBSOUP_2_SONAME;
+    }
+
+  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..1ce1cf4 100644
--- a/tls/base/meson.build
+++ b/tls/base/meson.build
@@ -3,11 +3,12 @@ tlsbase_sources = files(
   'gtlsinputstream.c',
   'gtlslog.c',
   'gtlsoutputstream.c',
+  'gtls-http.c',
 )
 
 tlsbase = static_library('tlsbase',
                          tlsbase_sources,
-                         dependencies: gio_dep,
+                         dependencies: [gio_dep, gmodule_dep],
                          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..7340615 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,8 +596,7 @@ 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;
+  GInputStream *istream = NULL;
   char *aia = NULL;
   char *scheme = NULL;
   int gerr;
@@ -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;
     }
@@ -671,7 +670,7 @@ issuer_missing_cb (gnutls_x509_trust_list_t   tlist,
   der = g_byte_array_sized_new (sizeof (buffer));
   do
     {
-      n_read = g_input_stream_read (G_INPUT_STREAM (istream), buffer, sizeof (buffer),
+      n_read = g_input_stream_read (istream, buffer, sizeof (buffer),
                                     priv->verify_chain_cancellable, &error);
       if (n_read == -1)
         {
@@ -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]