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,32 +94,32 @@ _get_monitor_by_action (DispatcherAction action)
|
|||||||
static void
|
static void
|
||||||
dump_proxy_to_props (NMProxyConfig *proxy, GVariantBuilder *builder)
|
dump_proxy_to_props (NMProxyConfig *proxy, GVariantBuilder *builder)
|
||||||
{
|
{
|
||||||
char **proxies = NULL;
|
const char *const*proxies;
|
||||||
const char *pac_url = NULL, *pac_script = NULL;
|
const char *pac_url = NULL, *pac_script = NULL;
|
||||||
|
|
||||||
if (nm_proxy_config_get_method (proxy) == NM_PROXY_CONFIG_METHOD_NONE)
|
if (nm_proxy_config_get_method (proxy) == NM_PROXY_CONFIG_METHOD_NONE)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* Proxies */
|
|
||||||
proxies = nm_proxy_config_get_proxies (proxy);
|
proxies = nm_proxy_config_get_proxies (proxy);
|
||||||
if (proxies && g_strv_length (proxies) > 0)
|
if (proxies && proxies[0]) {
|
||||||
g_variant_builder_add (builder, "{sv}",
|
g_variant_builder_add (builder, "{sv}",
|
||||||
"proxies",
|
"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);
|
pac_url = nm_proxy_config_get_pac_url (proxy);
|
||||||
if (pac_url)
|
if (pac_url) {
|
||||||
g_variant_builder_add (builder, "{sv}",
|
g_variant_builder_add (builder, "{sv}",
|
||||||
"pac-url",
|
"pac-url",
|
||||||
g_variant_new_string (pac_url));
|
g_variant_new_string (pac_url));
|
||||||
|
}
|
||||||
|
|
||||||
/* PAC Script */
|
|
||||||
pac_script = nm_proxy_config_get_pac_script (proxy);
|
pac_script = nm_proxy_config_get_pac_script (proxy);
|
||||||
if (pac_script)
|
if (pac_script) {
|
||||||
g_variant_builder_add (builder, "{sv}",
|
g_variant_builder_add (builder, "{sv}",
|
||||||
"pac-script",
|
"pac-script",
|
||||||
g_variant_new_string (pac_script));
|
g_variant_new_string (pac_script));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@@ -101,53 +101,50 @@ remove_data_destroy (struct remove_data *data)
|
|||||||
static void
|
static void
|
||||||
add_proxy_config (NMPacRunnerManager *self, GVariantBuilder *proxy_data, const NMProxyConfig *proxy_config)
|
add_proxy_config (NMPacRunnerManager *self, GVariantBuilder *proxy_data, const NMProxyConfig *proxy_config)
|
||||||
{
|
{
|
||||||
const char *pac = NULL, *filename = NULL;
|
const char *pac_url, *pac_script;
|
||||||
char **servers = NULL, **excludes = NULL;
|
const char *const*proxies;
|
||||||
char *contents = NULL;
|
const char *const*excludes;
|
||||||
NMProxyConfigMethod method;
|
NMProxyConfigMethod method;
|
||||||
|
|
||||||
method = nm_proxy_config_get_method (proxy_config);
|
method = nm_proxy_config_get_method (proxy_config);
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case NM_PROXY_CONFIG_METHOD_AUTO:
|
case NM_PROXY_CONFIG_METHOD_AUTO:
|
||||||
/* Extract Pac Url */
|
pac_url = nm_proxy_config_get_pac_url (proxy_config);
|
||||||
pac = nm_proxy_config_get_pac_url (proxy_config);
|
if (pac_url) {
|
||||||
if (pac)
|
|
||||||
g_variant_builder_add (proxy_data, "{sv}",
|
g_variant_builder_add (proxy_data, "{sv}",
|
||||||
"URL",
|
"URL",
|
||||||
g_variant_new_string (pac));
|
g_variant_new_string (pac_url));
|
||||||
|
}
|
||||||
|
|
||||||
/* Extract Pac Script */
|
pac_script = nm_proxy_config_get_pac_script (proxy_config);
|
||||||
filename = nm_proxy_config_get_pac_script (proxy_config);
|
if (pac_script) {
|
||||||
if (filename)
|
char *contents;
|
||||||
if (g_file_get_contents (filename, &contents, NULL, NULL))
|
|
||||||
|
if (g_file_get_contents (pac_script, &contents, NULL, NULL)) {
|
||||||
g_variant_builder_add (proxy_data, "{sv}",
|
g_variant_builder_add (proxy_data, "{sv}",
|
||||||
"Script",
|
"Script",
|
||||||
g_variant_new_string (contents));
|
g_variant_new_take_string (contents));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case NM_PROXY_CONFIG_METHOD_MANUAL:
|
case NM_PROXY_CONFIG_METHOD_MANUAL:
|
||||||
/* Extract Proxy servers */
|
proxies = nm_proxy_config_get_proxies (proxy_config);
|
||||||
servers = nm_proxy_config_get_proxies (proxy_config);
|
if (proxies && proxies[0]) {
|
||||||
if (servers && g_strv_length (servers))
|
|
||||||
g_variant_builder_add (proxy_data, "{sv}",
|
g_variant_builder_add (proxy_data, "{sv}",
|
||||||
"Servers",
|
"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);
|
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}",
|
g_variant_builder_add (proxy_data, "{sv}",
|
||||||
"Excludes",
|
"Excludes",
|
||||||
g_variant_new_strv ((const char *const *) excludes, -1));
|
g_variant_new_strv (excludes, -1));
|
||||||
|
}
|
||||||
if (servers)
|
|
||||||
g_strfreev (servers);
|
|
||||||
if (excludes)
|
|
||||||
g_strfreev (excludes);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case NM_PROXY_CONFIG_METHOD_NONE:
|
case NM_PROXY_CONFIG_METHOD_NONE:
|
||||||
/* Do Nothing */
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -28,8 +28,8 @@
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
NMProxyConfigMethod method;
|
NMProxyConfigMethod method;
|
||||||
GPtrArray *proxies;
|
char **proxies;
|
||||||
GPtrArray *excludes;
|
char **excludes;
|
||||||
gboolean browser_only;
|
gboolean browser_only;
|
||||||
char *pac_url;
|
char *pac_url;
|
||||||
char *pac_script;
|
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 *
|
NMProxyConfig *
|
||||||
nm_proxy_config_new (void)
|
nm_proxy_config_new (void)
|
||||||
{
|
{
|
||||||
@@ -90,6 +96,7 @@ nm_proxy_config_merge_setting (NMProxyConfig *config, NMSettingProxy *setting)
|
|||||||
guint32 port = 0;
|
guint32 port = 0;
|
||||||
NMProxyConfigPrivate *priv;
|
NMProxyConfigPrivate *priv;
|
||||||
NMSettingProxyMethod method;
|
NMSettingProxyMethod method;
|
||||||
|
GPtrArray *proxies;
|
||||||
|
|
||||||
if (!setting)
|
if (!setting)
|
||||||
return;
|
return;
|
||||||
@@ -98,13 +105,9 @@ nm_proxy_config_merge_setting (NMProxyConfig *config, NMSettingProxy *setting)
|
|||||||
|
|
||||||
priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
|
priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
|
||||||
|
|
||||||
g_ptr_array_free (priv->proxies, TRUE);
|
g_clear_pointer (&priv->proxies, g_strfreev);
|
||||||
g_ptr_array_free (priv->excludes, TRUE);
|
g_clear_pointer (&priv->excludes, g_strfreev);
|
||||||
g_free (priv->pac_script);
|
g_clear_pointer (&priv->pac_script, g_free);
|
||||||
|
|
||||||
priv->proxies = NULL;
|
|
||||||
priv->excludes = NULL;
|
|
||||||
priv->pac_script = NULL;
|
|
||||||
|
|
||||||
method = nm_setting_proxy_get_method (setting);
|
method = nm_setting_proxy_get_method (setting);
|
||||||
switch (method) {
|
switch (method) {
|
||||||
@@ -127,9 +130,8 @@ nm_proxy_config_merge_setting (NMProxyConfig *config, NMSettingProxy *setting)
|
|||||||
case NM_SETTING_PROXY_METHOD_MANUAL:
|
case NM_SETTING_PROXY_METHOD_MANUAL:
|
||||||
priv->method = NM_PROXY_CONFIG_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);
|
tmp = nm_setting_proxy_get_http_proxy (setting);
|
||||||
port = nm_setting_proxy_get_http_port (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
|
* set up a generic proxy in PacRunner i.e without a
|
||||||
* protocol prefix.
|
* protocol prefix.
|
||||||
*/
|
*/
|
||||||
|
proxies = g_ptr_array_new ();
|
||||||
if (nm_setting_proxy_get_http_default (setting)) {
|
if (nm_setting_proxy_get_http_default (setting)) {
|
||||||
if (tmp && port)
|
if (tmp && port)
|
||||||
g_ptr_array_add (priv->proxies, g_strdup_printf ("%s:%u/", tmp, port));
|
g_ptr_array_add (proxies, g_strdup_printf ("%s:%u/", tmp, port));
|
||||||
break;
|
} else {
|
||||||
|
if (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 (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 (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 (proxies, g_strdup_printf (nm_setting_proxy_get_socks_version_5 (setting) ?
|
||||||
|
"socks5://%s:%u/" : "socks4://%s:%u/", tmp, port));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tmp && port)
|
priv->proxies = (char **) g_ptr_array_free (proxies, proxies->len == 0);
|
||||||
g_ptr_array_add (priv->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));
|
|
||||||
|
|
||||||
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));
|
|
||||||
|
|
||||||
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) ?
|
|
||||||
"socks5://%s:%u/" : "socks4://%s:%u/", tmp, port));
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case NM_SETTING_PROXY_METHOD_NONE:
|
case NM_SETTING_PROXY_METHOD_NONE:
|
||||||
priv->method = NM_PROXY_CONFIG_METHOD_NONE;
|
priv->method = NM_PROXY_CONFIG_METHOD_NONE;
|
||||||
/* Do Nothing */
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
priv->browser_only = nm_setting_proxy_get_browser_only (setting);
|
priv->browser_only = nm_setting_proxy_get_browser_only (setting);
|
||||||
}
|
}
|
||||||
|
|
||||||
char **
|
const char *const*
|
||||||
nm_proxy_config_get_proxies (const NMProxyConfig *config)
|
nm_proxy_config_get_proxies (const NMProxyConfig *config)
|
||||||
{
|
{
|
||||||
const NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (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)
|
nm_proxy_config_get_excludes (const NMProxyConfig *config)
|
||||||
{
|
{
|
||||||
const NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (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
|
gboolean
|
||||||
@@ -236,8 +242,6 @@ nm_proxy_config_init (NMProxyConfig *config)
|
|||||||
NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
|
NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config);
|
||||||
|
|
||||||
priv->method = NM_PROXY_CONFIG_METHOD_NONE;
|
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
|
static void
|
||||||
@@ -246,10 +250,8 @@ finalize (GObject *object)
|
|||||||
NMProxyConfig *self = NM_PROXY_CONFIG (object);
|
NMProxyConfig *self = NM_PROXY_CONFIG (object);
|
||||||
NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (self);
|
NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (self);
|
||||||
|
|
||||||
if (priv->proxies)
|
g_strfreev (priv->proxies);
|
||||||
g_ptr_array_free (priv->proxies, TRUE);
|
g_strfreev (priv->excludes);
|
||||||
if (priv->excludes)
|
|
||||||
g_ptr_array_free (priv->excludes, TRUE);
|
|
||||||
g_free (priv->pac_url);
|
g_free (priv->pac_url);
|
||||||
g_free (priv->pac_script);
|
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);
|
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);
|
gboolean nm_proxy_config_get_browser_only (const NMProxyConfig *config);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user