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




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

    Dynamically load libsoup to request URIs

 tls/base/gtls-http.c             | 120 +++++++++++++++++++++++++++++++++++++++
 tls/base/gtls-http.h             |  31 ++++++++++
 tls/base/meson.build             |   1 +
 tls/gnutls/gtlsdatabase-gnutls.c |   9 +--
 4 files changed, 155 insertions(+), 6 deletions(-)
---
diff --git a/tls/base/gtls-http.c b/tls/base/gtls-http.c
new file mode 100644
index 0000000..09b3d85
--- /dev/null
+++ b/tls/base/gtls-http.c
@@ -0,0 +1,120 @@
+/* -*- 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"
+
+#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)
+{
+  static const char * const libsoup_sonames[] = { "libsoup-3.0.so.0", "libsoup-2.4.so.1" };
+
+  if (G_UNLIKELY (!g_module_supported ()))
+    {
+      g_debug ("GModule not supported");
+      return;
+    }
+
+  for (guint i = 0; i < G_N_ELEMENTS (libsoup_sonames); 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");
+}
+
+/**
+ * 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.
+ *
+ * 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);
+    }
+
+  g_message ("OPENING %s", uri);
+
+  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);
+          return NULL;
+        }
+
+      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..607e30e 100644
--- a/tls/base/meson.build
+++ b/tls/base/meson.build
@@ -3,6 +3,7 @@ tlsbase_sources = files(
   'gtlsinputstream.c',
   'gtlslog.c',
   'gtlsoutputstream.c',
+  'gtls-http.c',
 )
 
 tlsbase = static_library('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]