[grilo/wip/hadess/soup3: 2/3] net: Add support for libsoup3 through a compile-time option
- From: Bastien Nocera <hadess src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [grilo/wip/hadess/soup3: 2/3] net: Add support for libsoup3 through a compile-time option
- Date: Mon, 4 Jul 2022 15:27:55 +0000 (UTC)
commit 5cf738b4f19b5587cd0a5afbed7ff10388c5163b
Author: Bastien Nocera <hadess hadess net>
Date: Mon Jul 4 16:45:54 2022 +0200
net: Add support for libsoup3 through a compile-time option
Add 'soup3' build option to make it easier to build the grl-net library
against either libsoup 2.x or libsoup 3.x. The API version used is
exported in the soupapiversion variable of the pkg-config file.
Based on work by Daniel Kolesa <dkolesa igalia com>
libs/net/grl-net-wc.c | 119 +++++++++++++++++++++++++++++++++++++++-----------
libs/net/meson.build | 1 +
meson.build | 8 +++-
meson_options.txt | 1 +
tests/lib-net.c | 30 +++++++++++++
5 files changed, 133 insertions(+), 26 deletions(-)
---
diff --git a/libs/net/grl-net-wc.c b/libs/net/grl-net-wc.c
index d992664..1e78a36 100644
--- a/libs/net/grl-net-wc.c
+++ b/libs/net/grl-net-wc.c
@@ -39,13 +39,14 @@
#include "config.h"
#endif
-#define LIBSOUP_USE_UNSTABLE_REQUEST_API
-
#include <errno.h>
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>
#include <libsoup/soup-cache.h>
+#if ! SOUP_CHECK_VERSION (2, 99, 2)
+#define LIBSOUP_USE_UNSTABLE_REQUEST_API
#include <libsoup/soup-request-http.h>
+#endif
#include <libsoup/soup.h>
#include <string.h>
@@ -68,7 +69,11 @@ enum {
};
struct request_res {
+#if SOUP_CHECK_VERSION (2, 99, 2)
+ SoupMessage *message;
+#else
SoupRequest *request;
+#endif
gchar *buffer;
gsize length;
gsize offset;
@@ -195,10 +200,15 @@ free_op_res (void *op)
{
struct request_res *rr = op;
+#if SOUP_CHECK_VERSION (2, 99, 2)
+ g_object_unref (rr->message);
+#else
g_object_unref (rr->request);
+#endif
g_slice_free (struct request_res, rr);
}
+#if ! SOUP_CHECK_VERSION (2, 99, 2)
/*
* use-thread-context is available for libsoup-2.4 >= 2.39.0
* We check in run-time if it's available
@@ -213,6 +223,7 @@ set_thread_context (GrlNetWc *self)
if (spec)
g_object_set (priv->session, "use-thread-context", TRUE, NULL);
}
+#endif
static void
init_dump_directory (void)
@@ -313,11 +324,13 @@ grl_net_wc_init (GrlNetWc *wc)
wc->priv = grl_net_wc_get_instance_private (wc);
+#if ! SOUP_CHECK_VERSION (2, 99, 2)
wc->priv->session = soup_session_async_new ();
g_object_set (G_OBJECT (wc->priv->session), "ssl-use-system-ca-file", TRUE, NULL);
+ set_thread_context (wc);
+#endif
wc->priv->pending = g_queue_new ();
- set_thread_context (wc);
init_mock_requester (wc);
init_requester (wc);
}
@@ -430,6 +443,7 @@ parse_error (guint status,
GSimpleAsyncResult *result)
{
switch (status) {
+#if ! SOUP_CHECK_VERSION (2, 99, 2)
case SOUP_STATUS_CANT_RESOLVE:
case SOUP_STATUS_CANT_CONNECT:
case SOUP_STATUS_SSL_FAILED:
@@ -444,8 +458,11 @@ parse_error (guint status,
G_IO_ERROR_PROXY_FAILED,
_("Cannot connect to the proxy server"));
return;
+#endif
case SOUP_STATUS_INTERNAL_SERVER_ERROR: /* 500 */
+#if ! SOUP_CHECK_VERSION (2, 99, 2)
case SOUP_STATUS_MALFORMED:
+#endif
case SOUP_STATUS_BAD_REQUEST: /* 400 */
g_simple_async_result_set_error (result, GRL_NET_WC_ERROR,
GRL_NET_WC_ERROR_PROTOCOL_ERROR,
@@ -471,11 +488,13 @@ parse_error (guint status,
_("The entry has been modified since it was downloaded: %s"),
reason);
return;
+#if ! SOUP_CHECK_VERSION (2, 99, 2)
case SOUP_STATUS_CANCELLED:
g_simple_async_result_set_error (result, G_IO_ERROR,
G_IO_ERROR_CANCELLED,
_("Operation was cancelled"));
return;
+#endif
default:
GRL_DEBUG ("Unhandled status: %s", soup_status_get_phrase (status));
g_simple_async_result_set_error (result, G_IO_ERROR,
@@ -497,15 +516,13 @@ build_request_filename (const char *uri)
}
static void
-dump_data (SoupURI *uri,
+dump_data (const char *uri_string,
const char *buffer,
const gsize length)
{
if (!capture_dir)
return;
- char *uri_string = soup_uri_to_string (uri, FALSE);
-
/* Write request content to file in capture directory. */
char *request_filename = build_request_filename (uri_string);
char *path = g_build_filename (capture_dir, request_filename, NULL);
@@ -538,7 +555,6 @@ dump_data (SoupURI *uri,
}
g_free (request_filename);
- g_free (uri_string);
}
static void
@@ -601,14 +617,24 @@ read_async_cb (GObject *source,
}
{
- SoupMessage *msg =
- soup_request_http_get_message (SOUP_REQUEST_HTTP (rr->request));
+ g_autoptr(SoupMessage) msg = NULL;
+ guint status_code;
+ const char *reason_phrase;
+
+#if SOUP_CHECK_VERSION (2, 99, 2)
+ msg = g_object_ref (rr->message);
+ status_code = soup_message_get_status (msg);
+ reason_phrase = soup_message_get_reason_phrase (msg);
+#else
+ msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (rr->request));
+ status_code = msg->status_code;
+ reason_phrase = msg->reason_phrase;
+#endif
- if (msg && msg->status_code != SOUP_STATUS_OK) {
- parse_error (msg->status_code,
- msg->reason_phrase,
+ if (status_code != SOUP_STATUS_OK) {
+ parse_error (status_code,
+ reason_phrase,
G_SIMPLE_ASYNC_RESULT (user_data));
- g_object_unref (msg);
}
}
@@ -621,11 +647,20 @@ reply_cb (GObject *source,
GAsyncResult *res,
gpointer user_data)
{
+#if SOUP_CHECK_VERSION (2, 99, 2)
+ SoupSession *session = SOUP_SESSION (source);
+ SoupMessage *message = soup_session_get_async_result_message (session, res);
+ SoupMessageHeaders *response_hdrs = soup_message_get_response_headers (message);
+#endif
GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT (user_data);
struct request_res *rr = g_simple_async_result_get_op_res_gpointer (result);
GError *error = NULL;
+#if SOUP_CHECK_VERSION (2, 99, 2)
+ GInputStream *in = soup_session_send_finish (session, res, &error);
+#else
GInputStream *in = soup_request_send_finish (rr->request, res, &error);
+#endif
if (error) {
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
@@ -642,7 +677,11 @@ reply_cb (GObject *source,
return;
}
+#if SOUP_CHECK_VERSION (2, 99, 2)
+ rr->length = soup_message_headers_get_content_length (response_hdrs) + 1;
+#else
rr->length = soup_request_get_content_length (rr->request) + 1;
+#endif
if (rr->length == 1)
rr->length = 50 * 1024;
@@ -665,22 +704,28 @@ get_url_now (GrlNetWc *self,
GCancellable *cancellable)
{
GrlNetWcPrivate *priv = self->priv;
- SoupURI *uri;
struct request_res *rr = g_slice_new0 (struct request_res);
g_simple_async_result_set_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result),
rr,
NULL);
- uri = soup_uri_new (url);
- if (uri) {
- rr->request = soup_session_request_uri (priv->session, uri, NULL);
- soup_uri_free (uri);
- } else {
- rr->request = NULL;
+#if SOUP_CHECK_VERSION (2, 99, 2)
+ {
+ g_autoptr(GUri) uri = NULL;
+
+ uri = g_uri_parse (url, SOUP_HTTP_URI_FLAGS, NULL);
+ rr->message = soup_message_new_from_uri (SOUP_METHOD_GET, uri);
}
+#else
+ rr->request = soup_session_request (priv->session, url, NULL);
+#endif
+#if SOUP_CHECK_VERSION (2, 99, 2)
+ if (!rr->message) {
+#else
if (!rr->request) {
+#endif
g_simple_async_result_set_error (G_SIMPLE_ASYNC_RESULT (result),
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
@@ -692,22 +737,34 @@ get_url_now (GrlNetWc *self,
}
if (headers != NULL) {
- SoupMessage *message;
+ g_autoptr(SoupMessage) message = NULL;
GHashTableIter iter;
const char *key, *value;
+#if SOUP_CHECK_VERSION (2, 99, 2)
+ message = g_object_ref (rr->message);
+#else
message = soup_request_http_get_message (SOUP_REQUEST_HTTP (rr->request));
+#endif
if (message) {
g_hash_table_iter_init (&iter, headers);
while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *)&value)) {
+#if SOUP_CHECK_VERSION (2, 99, 2)
+ soup_message_headers_append (soup_message_get_request_headers (message), key, value);
+#else
soup_message_headers_append (message->request_headers, key, value);
+#endif
}
- g_object_unref (message);
}
}
+#if SOUP_CHECK_VERSION (2, 99, 2)
+ soup_session_send_async (priv->session, rr->message, G_PRIORITY_DEFAULT,
+ cancellable, reply_cb, result);
+#else
soup_request_send_async (rr->request, cancellable, reply_cb, result);
+#endif
}
static gboolean
@@ -789,7 +846,13 @@ get_content (GrlNetWc *self,
if (is_mocked ()) {
get_content_mocked (self, op, &(priv->previous_data), length);
} else {
- dump_data (soup_request_get_uri (rr->request),
+ g_autofree char *uri = NULL;
+#if SOUP_CHECK_VERSION (2, 99, 2)
+ uri = g_uri_to_string (soup_message_get_uri (rr->message));
+#else
+ uri = soup_uri_to_string (soup_request_get_uri (rr->request), FALSE);
+#endif
+ dump_data (uri,
rr->buffer,
rr->offset);
priv->previous_data = rr->buffer;
@@ -1010,7 +1073,11 @@ grl_net_wc_set_log_level (GrlNetWc *self,
soup_session_remove_feature_by_type (self->priv->session, SOUP_TYPE_LOGGER);
+#if SOUP_CHECK_VERSION (2, 99, 2)
+ logger = soup_logger_new ((SoupLoggerLogLevel) log_level);
+#else
logger = soup_logger_new ((SoupLoggerLogLevel) log_level, -1);
+#endif
soup_session_add_feature (self->priv->session, SOUP_SESSION_FEATURE (logger));
g_object_unref (logger);
@@ -1031,14 +1098,16 @@ grl_net_wc_set_throttling (GrlNetWc *self,
{
g_return_if_fail (GRL_IS_NET_WC (self));
+ //FIXME check if we have pending jobs before replacing it
+
if (throttling > 0) {
/* max conns per host = 1 */
g_object_set (self->priv->session,
- SOUP_SESSION_MAX_CONNS_PER_HOST, 1, NULL);
+ "max-conns-per-host", 1, NULL);
} else {
/* default value */
g_object_set (self->priv->session,
- SOUP_SESSION_MAX_CONNS_PER_HOST, 2, NULL);
+ "max-conns-per-host", 2, NULL);
}
self->priv->throttling = throttling;
diff --git a/libs/net/meson.build b/libs/net/meson.build
index c366adf..c3c7801 100644
--- a/libs/net/meson.build
+++ b/libs/net/meson.build
@@ -56,6 +56,7 @@ grlnet_pc = pkgconfig.generate(libgrlnet,
'datadir=${datarootdir}',
'girdir=' + girdir_for_pc_file,
'typelibdir=' + typelibdir_for_pc_file,
+ 'soupapiversion=' + soup_api_version,
],
)
diff --git a/meson.build b/meson.build
index 2bfc686..478a795 100644
--- a/meson.build
+++ b/meson.build
@@ -48,7 +48,13 @@ libxml_dep = dependency('libxml-2.0', required: true)
enable_grlnet = get_option('enable-grl-net')
if enable_grlnet
- libsoup_dep = dependency('libsoup-2.4', version: '>= 2.41.3', required: true)
+ if get_option('soup3')
+ libsoup_dep = dependency('libsoup-3.0', version: '>= 2.99.2', required: true)
+ soup_api_version = '3.0'
+ else
+ libsoup_dep = dependency('libsoup-2.4', version: '>= 2.41.3', required: true)
+ soup_api_version = '2.0'
+ endif
endif
enable_grlpls = get_option('enable-grl-pls')
diff --git a/meson_options.txt b/meson_options.txt
index a2a6fc4..91418cc 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -4,3 +4,4 @@ option('enable-gtk-doc', type: 'boolean', value: true, description: 'Enable gene
option('enable-introspection', type: 'boolean', value: true, description: 'Enable GObject Introspection')
option('enable-test-ui', type: 'boolean', value: true, description: 'Build Test UI')
option('enable-vala', type: 'boolean', value: true, description: 'Enable Vala (enables GObject
Introspection)')
+option('soup3', type: 'boolean', value: true, description: 'Whether to use libsoup3')
diff --git a/tests/lib-net.c b/tests/lib-net.c
index 2857310..869d7bb 100644
--- a/tests/lib-net.c
+++ b/tests/lib-net.c
@@ -80,6 +80,20 @@ timeout (gpointer user_data)
return G_SOURCE_REMOVE;
}
+#if SOUP_CHECK_VERSION (2, 99, 2)
+static void
+soup_server_throttling_cb (SoupServer *server,
+ SoupServerMessage *message,
+ const char *path,
+ GHashTable *query,
+ gpointer user_data)
+{
+ gchar *response = g_strdup_printf ("%" G_GINT64_FORMAT, g_get_monotonic_time());
+
+ soup_server_message_set_response (message, "text/plain", SOUP_MEMORY_TAKE, response, strlen(response));
+ soup_server_message_set_status (message, SOUP_STATUS_OK, NULL);
+}
+#else
static void
soup_server_throttling_cb (SoupServer *server,
SoupMessage *message,
@@ -93,6 +107,7 @@ soup_server_throttling_cb (SoupServer *server,
soup_message_set_response (message, "text/plain", SOUP_MEMORY_TAKE, response, strlen(response));
soup_message_set_status (message, SOUP_STATUS_OK);
}
+#endif
static void
test_net_wc_throttling_cb (GObject *source_object,
@@ -163,8 +178,13 @@ test_net_wc_small_throttling (Fixture *f,
uris = soup_server_get_uris (f->server);
g_assert_nonnull (uris);
+#if SOUP_CHECK_VERSION (2, 99, 2)
+ request = g_uri_to_string_partial (uris->data, G_URI_HIDE_PASSWORD);
+ g_slist_free_full (uris, (GDestroyNotify) g_uri_unref);
+#else
request = soup_uri_to_string (uris->data, FALSE);
g_slist_free_full (uris, (GDestroyNotify) soup_uri_free);
+#endif
g_assert_nonnull (request);
wc = grl_net_wc_new ();
@@ -203,8 +223,13 @@ test_net_wc_big_throttling (Fixture *f,
uris = soup_server_get_uris (f->server);
g_assert_nonnull (uris);
+#if SOUP_CHECK_VERSION (2, 99, 2)
+ request = g_uri_to_string_partial (uris->data, G_URI_HIDE_PASSWORD);
+ g_slist_free_full (uris, (GDestroyNotify) g_uri_unref);
+#else
request = soup_uri_to_string (uris->data, FALSE);
g_slist_free_full (uris, (GDestroyNotify) soup_uri_free);
+#endif
g_assert_nonnull (request);
wc = grl_net_wc_new ();
@@ -245,8 +270,13 @@ test_net_wc_no_throttling_stress (Fixture *f,
uris = soup_server_get_uris (f->server);
g_assert_nonnull (uris);
+#if SOUP_CHECK_VERSION (2, 99, 2)
+ request = g_uri_to_string_partial (uris->data, G_URI_HIDE_PASSWORD);
+ g_slist_free_full (uris, (GDestroyNotify) g_uri_unref);
+#else
request = soup_uri_to_string (uris->data, FALSE);
g_slist_free_full (uris, (GDestroyNotify) soup_uri_free);
+#endif
g_assert_nonnull (request);
/* Under the same grl-net-wc, create NUM_STRESS_TEST async operations to our
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]