GCC Code Coverage Report


Directory: src/
File: src/libproxy/proxy.c
Date: 2023-01-04 17:35:37
Exec Total Coverage
Lines: 0 37 0.0%
Branches: 0 20 0.0%

Line Branch Exec Source
1 /*******************************************************************************
2 * libproxy - A library for proxy configuration
3 * Copyright (C) 2006 Nathaniel McCallum <nathaniel@natemccallum.com>
4 * Copyright (C) 2022-2023 Jan-Michael Brummer <jan.brummer@tabos.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 ******************************************************************************/
20
21 #include <gio/gio.h>
22
23 #include "proxy.h"
24
25 /**
26 * SECTION:px-proxy
27 * @short_description: A convient helper for using proxy servers
28 *
29 * Test 123
30 */
31
32 struct px_proxy_factory {
33 GDBusProxy *proxy;
34 GCancellable *cancellable;
35 };
36
37 /**
38 * px_proxy_factory_new:
39 * Creates a new proxy factory.
40 *
41 * Returns: pointer to #px_proxy_factory
42 */
43 struct px_proxy_factory *
44 px_proxy_factory_new (void)
45 {
46 g_autoptr (GError) error = NULL;
47 struct px_proxy_factory *self = g_malloc0 (sizeof (struct px_proxy_factory));
48
49 self->cancellable = g_cancellable_new ();
50 self->proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
51 G_DBUS_PROXY_FLAGS_NONE,
52 NULL, /* GDBusInterfaceInfo */
53 "org.libproxy.proxy",
54 "/org/libproxy/proxy",
55 "org.libproxy.proxy",
56 self->cancellable, /* GCancellable */
57 &error);
58 if (!self->proxy)
59 g_warning ("Could not create libproxy dbus proxy: %s", error->message);
60
61 return self;
62 }
63
64 char **
65 px_proxy_factory_get_proxies (struct px_proxy_factory *self,
66 const char *url)
67 {
68 g_autoptr (GVariant) result = NULL;
69 g_autoptr (GError) error = NULL;
70 g_autoptr (GVariantIter) iter = NULL;
71 g_autoptr (GList) list = NULL;
72 GList *tmp;
73 char *str;
74 char **retval;
75 gsize len;
76 gsize idx;
77
78 result = g_dbus_proxy_call_sync (self->proxy,
79 "query",
80 g_variant_new ("(s)", url),
81 G_DBUS_CALL_FLAGS_NONE,
82 -1,
83 self->cancellable,
84 &error);
85 if (!result) {
86 g_warning ("Could not query proxy dbus: %s", error->message);
87 return NULL;
88 }
89
90 g_variant_get (result, "(as)", &iter);
91
92 while (g_variant_iter_loop (iter, "&s", &str)) {
93 list = g_list_prepend (list, g_strdup (str));
94 }
95
96 len = g_list_length (list);
97 if (len == 0) {
98 retval = g_malloc0 (sizeof (char *) * 2);
99 retval[0] = g_strdup ("direct://");
100
101 return retval;
102 }
103
104 retval = g_malloc0 (sizeof (char *) * (len + 1));
105 for (tmp = list, idx = 0; tmp && tmp->data; tmp = tmp->next, idx++) {
106 char *value = tmp->data;
107 retval[idx] = g_strdup (value);
108 }
109
110 return retval;
111 }
112
113 void
114 px_proxy_factory_free_proxies (char **proxies)
115 {
116 g_clear_pointer (&proxies, g_strfreev);
117 }
118
119 /**
120 * px_proxy_factory_free:
121 * @self: a px_proxy_factory
122 *
123 * Free px_proxy_factory
124 */
125 void
126 px_proxy_factory_free (struct px_proxy_factory *self)
127 {
128 g_cancellable_cancel (self->cancellable);
129 g_clear_object (&self->cancellable);
130 g_clear_object (&self->proxy);
131 g_clear_pointer (&self, g_free);
132 }
133