From 7120667ccd2d1341e15a53cc16edab286846f0aa Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Fri, 1 Mar 2019 10:24:57 +0000 Subject: [PATCH] trivial: Move getting the SoupSession to common code --- src/fu-util-common.c | 45 +++++++++++++++++++++++++++++++ src/fu-util-common.h | 2 ++ src/fu-util.c | 63 +++++++------------------------------------- 3 files changed, 57 insertions(+), 53 deletions(-) diff --git a/src/fu-util-common.c b/src/fu-util-common.c index a0154f214..46d228770 100644 --- a/src/fu-util-common.c +++ b/src/fu-util-common.c @@ -415,3 +415,48 @@ fu_util_cmd_array_to_string (GPtrArray *array) return g_string_free (string, FALSE); } + +SoupSession * +fu_util_setup_networking (GError **error) +{ + const gchar *http_proxy; + g_autofree gchar *user_agent = NULL; + g_autoptr(SoupSession) session = NULL; + + /* create the soup session */ + user_agent = fwupd_build_user_agent (PACKAGE_NAME, PACKAGE_VERSION); + session = soup_session_new_with_options (SOUP_SESSION_USER_AGENT, user_agent, + SOUP_SESSION_TIMEOUT, 60, + NULL); + if (session == NULL) { + g_set_error_literal (error, + FWUPD_ERROR, + FWUPD_ERROR_INTERNAL, + "failed to setup networking"); + return NULL; + } + + /* set the proxy */ + http_proxy = g_getenv ("https_proxy"); + if (http_proxy == NULL) + http_proxy = g_getenv ("HTTPS_PROXY"); + if (http_proxy == NULL) + http_proxy = g_getenv ("http_proxy"); + if (http_proxy == NULL) + http_proxy = g_getenv ("HTTP_PROXY"); + if (http_proxy != NULL) { + g_autoptr(SoupURI) proxy_uri = soup_uri_new (http_proxy); + if (proxy_uri == NULL) { + g_set_error (error, + FWUPD_ERROR, + FWUPD_ERROR_INTERNAL, + "invalid proxy URI: %s", http_proxy); + return FALSE; + } + g_object_set (session, SOUP_SESSION_PROXY_URI, proxy_uri, NULL); + } + + /* this disables the double-compression of the firmware.xml.gz file */ + soup_session_remove_feature_by_type (session, SOUP_TYPE_CONTENT_DECODER); + return g_steal_pointer (&session); +} diff --git a/src/fu-util-common.h b/src/fu-util-common.h index bdaef830a..5c3d44caf 100644 --- a/src/fu-util-common.h +++ b/src/fu-util-common.h @@ -8,6 +8,7 @@ #include #include +#include G_BEGIN_DECLS @@ -33,6 +34,7 @@ gboolean fu_util_prompt_for_boolean (gboolean def); gboolean fu_util_print_device_tree (GNode *n, gpointer data); gboolean fu_util_is_interesting_device (FwupdDevice *dev); gchar *fu_util_get_user_cache_path (const gchar *fn); +SoupSession *fu_util_setup_networking (GError **error); gchar *fu_util_get_versions (void); diff --git a/src/fu-util.c b/src/fu-util.c index 647a38d0c..751ec58cb 100644 --- a/src/fu-util.c +++ b/src/fu-util.c @@ -20,7 +20,6 @@ #include #include #include -#include #include #include "fu-history.h" @@ -182,54 +181,6 @@ fu_util_prompt_for_device (FuUtilPrivate *priv, GError **error) return g_object_ref (dev); } -static gboolean -fu_util_setup_networking (FuUtilPrivate *priv, GError **error) -{ - const gchar *http_proxy; - g_autofree gchar *user_agent = NULL; - - /* already done */ - if (priv->soup_session != NULL) - return TRUE; - - /* create the soup session */ - user_agent = fwupd_build_user_agent (PACKAGE_NAME, PACKAGE_VERSION); - priv->soup_session = soup_session_new_with_options (SOUP_SESSION_USER_AGENT, user_agent, - SOUP_SESSION_TIMEOUT, 60, - NULL); - if (priv->soup_session == NULL) { - g_set_error_literal (error, - FWUPD_ERROR, - FWUPD_ERROR_INTERNAL, - "failed to setup networking"); - return FALSE; - } - - /* set the proxy */ - http_proxy = g_getenv ("https_proxy"); - if (http_proxy == NULL) - http_proxy = g_getenv ("HTTPS_PROXY"); - if (http_proxy == NULL) - http_proxy = g_getenv ("http_proxy"); - if (http_proxy == NULL) - http_proxy = g_getenv ("HTTP_PROXY"); - if (http_proxy != NULL) { - g_autoptr(SoupURI) proxy_uri = soup_uri_new (http_proxy); - if (proxy_uri == NULL) { - g_set_error (error, - FWUPD_ERROR, - FWUPD_ERROR_INTERNAL, - "invalid proxy URI: %s", http_proxy); - return FALSE; - } - g_object_set (priv->soup_session, SOUP_SESSION_PROXY_URI, proxy_uri, NULL); - } - - /* this disables the double-compression of the firmware.xml.gz file */ - soup_session_remove_feature_by_type (priv->soup_session, SOUP_TYPE_CONTENT_DECODER); - return TRUE; -} - static gboolean fu_util_perhaps_show_unreported (FuUtilPrivate *priv, GError **error) { @@ -860,8 +811,11 @@ fu_util_report_history (FuUtilPrivate *priv, gchar **values, GError **error) g_autoptr(GPtrArray) remotes = NULL; /* set up networking */ - if (!fu_util_setup_networking (priv, error)) - return FALSE; + if (priv->soup_session == NULL) { + priv->soup_session = fu_util_setup_networking (error); + if (priv->soup_session == NULL) + return FALSE; + } /* create a map of RemoteID to RemoteURI */ remotes = fwupd_client_get_remotes (priv->client, NULL, error); @@ -1143,8 +1097,11 @@ fu_util_download_file (FuUtilPrivate *priv, } /* set up networking */ - if (!fu_util_setup_networking (priv, error)) - return FALSE; + if (priv->soup_session == NULL) { + priv->soup_session = fu_util_setup_networking (error); + if (priv->soup_session == NULL) + return FALSE; + } /* download data */ uri_str = soup_uri_to_string (uri, FALSE);