proxy: refactor NMProxyConfig to keep internal variables as strv
The API of NMProxyConfig exposes @proxies and @excludes as strv values. There is no need to track those values internally as a GPtrArray and then clone them in the getters (especially, since the entire NMProxyConfig API is internal to core. Thereby, fix a few memory leaks in add_proxy_config() and some style fixes for { }.
This commit is contained in:
@@ -94,33 +94,33 @@ _get_monitor_by_action (DispatcherAction action)
|
||||
static void
|
||||
dump_proxy_to_props (NMProxyConfig *proxy, GVariantBuilder *builder)
|
||||
{
|
||||
char **proxies = NULL;
|
||||
const char *const*proxies;
|
||||
const char *pac_url = NULL, *pac_script = NULL;
|
||||
|
||||
if (nm_proxy_config_get_method (proxy) == NM_PROXY_CONFIG_METHOD_NONE)
|
||||
return;
|
||||
|
||||
/* Proxies */
|
||||
proxies = nm_proxy_config_get_proxies (proxy);
|
||||
if (proxies && g_strv_length (proxies) > 0)
|
||||
if (proxies && proxies[0]) {
|
||||
g_variant_builder_add (builder, "{sv}",
|
||||
"proxies",
|
||||
g_variant_new_strv ((const char *const *) proxies, -1));
|
||||
g_variant_new_strv (proxies, -1));
|
||||
}
|
||||
|
||||
/* PAC Url */
|
||||
pac_url = nm_proxy_config_get_pac_url (proxy);
|
||||
if (pac_url)
|
||||
if (pac_url) {
|
||||
g_variant_builder_add (builder, "{sv}",
|
||||
"pac-url",
|
||||
g_variant_new_string (pac_url));
|
||||
}
|
||||
|
||||
/* PAC Script */
|
||||
pac_script = nm_proxy_config_get_pac_script (proxy);
|
||||
if (pac_script)
|
||||
if (pac_script) {
|
||||
g_variant_builder_add (builder, "{sv}",
|
||||
"pac-script",
|
||||
g_variant_new_string (pac_script));
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
dump_ip4_to_props (NMIP4Config *ip4, GVariantBuilder *builder)
|
||||
|
@@ -101,53 +101,50 @@ remove_data_destroy (struct remove_data *data)
|
||||
static void
|
||||
add_proxy_config (NMPacRunnerManager *self, GVariantBuilder *proxy_data, const NMProxyConfig *proxy_config)
|
||||
{
|
||||
const char *pac = NULL, *filename = NULL;
|
||||
char **servers = NULL, **excludes = NULL;
|
||||
char *contents = NULL;
|
||||
const char *pac_url, *pac_script;
|
||||
const char *const*proxies;
|
||||
const char *const*excludes;
|
||||
NMProxyConfigMethod method;
|
||||
|
||||
method = nm_proxy_config_get_method (proxy_config);
|
||||
switch (method) {
|
||||
case NM_PROXY_CONFIG_METHOD_AUTO:
|
||||
/* Extract Pac Url */
|
||||
pac = nm_proxy_config_get_pac_url (proxy_config);
|
||||
if (pac)
|
||||
pac_url = nm_proxy_config_get_pac_url (proxy_config);
|
||||
if (pac_url) {
|
||||
g_variant_builder_add (proxy_data, "{sv}",
|
||||
"URL",
|
||||
g_variant_new_string (pac));
|
||||
g_variant_new_string (pac_url));
|
||||
}
|
||||
|
||||
/* Extract Pac Script */
|
||||
filename = nm_proxy_config_get_pac_script (proxy_config);
|
||||
if (filename)
|
||||
if (g_file_get_contents (filename, &contents, NULL, NULL))
|
||||
pac_script = nm_proxy_config_get_pac_script (proxy_config);
|
||||
if (pac_script) {
|
||||
char *contents;
|
||||
|
||||
if (g_file_get_contents (pac_script, &contents, NULL, NULL)) {
|
||||
g_variant_builder_add (proxy_data, "{sv}",
|
||||
"Script",
|
||||
g_variant_new_string (contents));
|
||||
g_variant_new_take_string (contents));
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case NM_PROXY_CONFIG_METHOD_MANUAL:
|
||||
/* Extract Proxy servers */
|
||||
servers = nm_proxy_config_get_proxies (proxy_config);
|
||||
if (servers && g_strv_length (servers))
|
||||
proxies = nm_proxy_config_get_proxies (proxy_config);
|
||||
if (proxies && proxies[0]) {
|
||||
g_variant_builder_add (proxy_data, "{sv}",
|
||||
"Servers",
|
||||
g_variant_new_strv ((const char *const *) servers, -1));
|
||||
g_variant_new_strv (proxies, -1));
|
||||
}
|
||||
|
||||
/* Extract Excludes */
|
||||
excludes = nm_proxy_config_get_excludes (proxy_config);
|
||||
if (excludes && g_strv_length (excludes))
|
||||
if (excludes && excludes[0]) {
|
||||
g_variant_builder_add (proxy_data, "{sv}",
|
||||
"Excludes",
|
||||
g_variant_new_strv ((const char *const *) excludes, -1));
|
||||
|
||||
if (servers)
|
||||
g_strfreev (servers);
|
||||
if (excludes)
|
||||
g_strfreev (excludes);
|
||||
g_variant_new_strv (excludes, -1));
|
||||
}
|
||||
|
||||
break;
|
||||
case NM_PROXY_CONFIG_METHOD_NONE:
|
||||
/* Do Nothing */
|
||||
break;
|
||||
}
|
||||
|
||||
|
@@ -28,8 +28,8 @@
|
||||
|
||||
typedef struct {
|
||||
NMProxyConfigMethod method;
|
||||
GPtrArray *proxies;
|
||||
GPtrArray *excludes;
|
||||
char **proxies;
|
||||
char **excludes;
|
||||
gboolean browser_only;
|
||||
char *pac_url;
|
||||
char *pac_script;
|
||||
@@ -61,6 +61,12 @@ G_DEFINE_TYPE (NMProxyConfig, nm_proxy_config, G_TYPE_OBJECT)
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
static char **
|
||||
_strdupv_nonempty (const char *const* strv)
|
||||
{
|
||||
return (!strv || !strv[0]) ? NULL : g_strdupv ((char **) strv);
|
||||
}
|
||||
|
||||
NMProxyConfig *
|
||||
nm_proxy_config_new (void)
|
||||
{
|
||||
@@ -90,6 +96,7 @@ nm_proxy_config_merge_setting (NMProxyConfig *config, NMSettingProxy *setting)
|
||||
guint32 port = 0;
|
||||
NMProxyConfigPrivate *priv;
|
||||
NMSettingProxyMethod method;
|
||||
GPtrArray *proxies;
|
||||
|
||||
if (!setting)
|
||||
return;
|
||||
@@ -98,13 +105,9 @@ nm_proxy_config_merge_setting (NMProxyConfig *config, NMSettingProxy *setting)
|
||||
|
||||
priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
|
||||
|
||||
g_ptr_array_free (priv->proxies, TRUE);
|
||||
g_ptr_array_free (priv->excludes, TRUE);
|
||||
g_free (priv->pac_script);
|
||||
|
||||
priv->proxies = NULL;
|
||||
priv->excludes = NULL;
|
||||
priv->pac_script = NULL;
|
||||
g_clear_pointer (&priv->proxies, g_strfreev);
|
||||
g_clear_pointer (&priv->excludes, g_strfreev);
|
||||
g_clear_pointer (&priv->pac_script, g_free);
|
||||
|
||||
method = nm_setting_proxy_get_method (setting);
|
||||
switch (method) {
|
||||
@@ -127,9 +130,8 @@ nm_proxy_config_merge_setting (NMProxyConfig *config, NMSettingProxy *setting)
|
||||
case NM_SETTING_PROXY_METHOD_MANUAL:
|
||||
priv->method = NM_PROXY_CONFIG_METHOD_MANUAL;
|
||||
|
||||
priv->excludes = _nm_utils_strv_to_ptrarray ((char **) nm_setting_proxy_get_no_proxy_for (setting));
|
||||
priv->excludes = _strdupv_nonempty (nm_setting_proxy_get_no_proxy_for (setting));
|
||||
|
||||
priv->proxies = g_ptr_array_new_with_free_func (g_free);
|
||||
|
||||
tmp = nm_setting_proxy_get_http_proxy (setting);
|
||||
port = nm_setting_proxy_get_http_port (setting);
|
||||
@@ -138,54 +140,58 @@ nm_proxy_config_merge_setting (NMProxyConfig *config, NMSettingProxy *setting)
|
||||
* set up a generic proxy in PacRunner i.e without a
|
||||
* protocol prefix.
|
||||
*/
|
||||
proxies = g_ptr_array_new ();
|
||||
if (nm_setting_proxy_get_http_default (setting)) {
|
||||
if (tmp && port)
|
||||
g_ptr_array_add (priv->proxies, g_strdup_printf ("%s:%u/", tmp, port));
|
||||
break;
|
||||
}
|
||||
|
||||
g_ptr_array_add (proxies, g_strdup_printf ("%s:%u/", tmp, port));
|
||||
} else {
|
||||
if (tmp && port)
|
||||
g_ptr_array_add (priv->proxies, g_strdup_printf ("http://%s:%u/", tmp, port));
|
||||
g_ptr_array_add (proxies, g_strdup_printf ("http://%s:%u/", tmp, port));
|
||||
|
||||
tmp = nm_setting_proxy_get_ssl_proxy (setting);
|
||||
port = nm_setting_proxy_get_ssl_port (setting);
|
||||
if (tmp && port)
|
||||
g_ptr_array_add (priv->proxies, g_strdup_printf ("https://%s:%u/", tmp, port));
|
||||
g_ptr_array_add (proxies, g_strdup_printf ("https://%s:%u/", tmp, port));
|
||||
|
||||
tmp = nm_setting_proxy_get_ftp_proxy (setting);
|
||||
port = nm_setting_proxy_get_ftp_port (setting);
|
||||
if (tmp && port)
|
||||
g_ptr_array_add (priv->proxies, g_strdup_printf ("ftp://%s:%u/", tmp, port));
|
||||
g_ptr_array_add (proxies, g_strdup_printf ("ftp://%s:%u/", tmp, port));
|
||||
|
||||
tmp = nm_setting_proxy_get_socks_proxy (setting);
|
||||
port = nm_setting_proxy_get_socks_port (setting);
|
||||
if (tmp && port)
|
||||
g_ptr_array_add (priv->proxies, g_strdup_printf (nm_setting_proxy_get_socks_version_5 (setting) ?
|
||||
if (tmp && port) {
|
||||
g_ptr_array_add (proxies, g_strdup_printf (nm_setting_proxy_get_socks_version_5 (setting) ?
|
||||
"socks5://%s:%u/" : "socks4://%s:%u/", tmp, port));
|
||||
}
|
||||
}
|
||||
|
||||
priv->proxies = (char **) g_ptr_array_free (proxies, proxies->len == 0);
|
||||
break;
|
||||
case NM_SETTING_PROXY_METHOD_NONE:
|
||||
priv->method = NM_PROXY_CONFIG_METHOD_NONE;
|
||||
/* Do Nothing */
|
||||
break;
|
||||
}
|
||||
|
||||
priv->browser_only = nm_setting_proxy_get_browser_only (setting);
|
||||
}
|
||||
|
||||
char **
|
||||
const char *const*
|
||||
nm_proxy_config_get_proxies (const NMProxyConfig *config)
|
||||
{
|
||||
const NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
|
||||
|
||||
return _nm_utils_ptrarray_to_strv (priv->proxies);
|
||||
/* don't return NULL */
|
||||
return priv->proxies ? ((const char *const*) priv->proxies) : ((const char *const*) &priv->proxies);
|
||||
}
|
||||
|
||||
char **
|
||||
const char *const*
|
||||
nm_proxy_config_get_excludes (const NMProxyConfig *config)
|
||||
{
|
||||
const NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
|
||||
|
||||
return _nm_utils_ptrarray_to_strv (priv->excludes);
|
||||
/* don't return NULL */
|
||||
return priv->excludes ? ((const char *const*) priv->excludes) : ((const char *const*) &priv->excludes);
|
||||
}
|
||||
|
||||
gboolean
|
||||
@@ -236,8 +242,6 @@ nm_proxy_config_init (NMProxyConfig *config)
|
||||
NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
|
||||
|
||||
priv->method = NM_PROXY_CONFIG_METHOD_NONE;
|
||||
priv->proxies = g_ptr_array_new_with_free_func (g_free);
|
||||
priv->excludes = g_ptr_array_new_with_free_func (g_free);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -246,10 +250,8 @@ finalize (GObject *object)
|
||||
NMProxyConfig *self = NM_PROXY_CONFIG (object);
|
||||
NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (self);
|
||||
|
||||
if (priv->proxies)
|
||||
g_ptr_array_free (priv->proxies, TRUE);
|
||||
if (priv->excludes)
|
||||
g_ptr_array_free (priv->excludes, TRUE);
|
||||
g_strfreev (priv->proxies);
|
||||
g_strfreev (priv->excludes);
|
||||
g_free (priv->pac_url);
|
||||
g_free (priv->pac_script);
|
||||
|
||||
|
@@ -47,9 +47,9 @@ NMProxyConfigMethod nm_proxy_config_get_method (const NMProxyConfig *config);
|
||||
|
||||
void nm_proxy_config_merge_setting (NMProxyConfig *config, NMSettingProxy *setting);
|
||||
|
||||
char ** nm_proxy_config_get_proxies (const NMProxyConfig *config);
|
||||
const char *const*nm_proxy_config_get_proxies (const NMProxyConfig *config);
|
||||
|
||||
char ** nm_proxy_config_get_excludes (const NMProxyConfig *config);
|
||||
const char *const*nm_proxy_config_get_excludes (const NMProxyConfig *config);
|
||||
|
||||
gboolean nm_proxy_config_get_browser_only (const NMProxyConfig *config);
|
||||
|
||||
|
Reference in New Issue
Block a user