1 /* GLib testing framework examples and tests
3 * Copyright (C) 2010 Collabora, Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
34 static const gchar *uri = NULL;
35 static GCancellable *cancellable = NULL;
36 static gint return_value = 0;
38 static void G_GNUC_NORETURN
41 fprintf (stderr, "Usage: proxy [-s] uri\n");
42 fprintf (stderr, " Use -s to do synchronous lookups.\n");
43 fprintf (stderr, " Use -c to cancel operation.\n");
44 fprintf (stderr, " Use -e to use enumerator.\n");
49 print_and_free_error (GError *error)
51 fprintf (stderr, "Failed to obtain proxies: %s\n", error->message);
57 print_proxies (const gchar *uri, gchar **proxies)
59 printf ("Proxies for URI '%s' are:\n", uri);
61 if (proxies == NULL || proxies[0] == NULL)
64 for (; proxies[0]; proxies++)
65 printf ("\t%s\n", proxies[0]);
69 _proxy_lookup_cb (GObject *source_object,
75 GMainLoop *loop = user_data;
77 proxies = g_proxy_resolver_lookup_finish (G_PROXY_RESOLVER (source_object),
82 print_and_free_error (error);
86 print_proxies (uri, proxies);
90 g_main_loop_quit (loop);
94 use_resolver (gboolean synchronous)
96 GProxyResolver *resolver;
98 resolver = g_proxy_resolver_get_default ();
102 GError *error = NULL;
105 proxies = g_proxy_resolver_lookup (resolver, uri, cancellable, &error);
108 print_and_free_error (error);
110 print_proxies (uri, proxies);
112 g_strfreev (proxies);
116 GMainLoop *loop = g_main_loop_new (NULL, FALSE);
118 g_proxy_resolver_lookup_async (resolver,
124 g_main_loop_run (loop);
125 g_main_loop_unref (loop);
130 print_proxy_address (GSocketAddress *sockaddr)
132 GProxyAddress *proxy = NULL;
134 if (sockaddr == NULL)
136 printf ("\tdirect://\n");
140 if (G_IS_PROXY_ADDRESS (sockaddr))
142 proxy = G_PROXY_ADDRESS (sockaddr);
143 printf ("\t%s://", g_proxy_address_get_protocol(proxy));
147 printf ("\tdirect://");
150 if (G_IS_INET_SOCKET_ADDRESS (sockaddr))
152 GInetAddress *inetaddr;
156 g_object_get (sockaddr,
157 "address", &inetaddr,
161 addr = g_inet_address_to_string (inetaddr);
163 printf ("%s:%u", addr, port);
168 if (proxy && g_proxy_address_get_username(proxy))
169 printf ("\t(Username: %s Password: %s)",
170 g_proxy_address_get_username(proxy),
171 g_proxy_address_get_password(proxy));
177 _proxy_enumerate_cb (GObject *object,
178 GAsyncResult *result,
181 GError *error = NULL;
182 GMainLoop *loop = user_data;
183 GSocketAddressEnumerator *enumerator = G_SOCKET_ADDRESS_ENUMERATOR (object);
184 GSocketAddress *sockaddr;
186 sockaddr = g_socket_address_enumerator_next_finish (enumerator,
191 print_proxy_address (sockaddr);
192 g_socket_address_enumerator_next_async (enumerator,
196 g_object_unref (sockaddr);
201 print_and_free_error (error);
203 g_main_loop_quit (loop);
208 run_with_enumerator (gboolean synchronous, GSocketAddressEnumerator *enumerator)
210 GError *error = NULL;
214 GSocketAddress *sockaddr;
216 while ((sockaddr = g_socket_address_enumerator_next(enumerator,
220 print_proxy_address (sockaddr);
221 g_object_unref (sockaddr);
225 print_and_free_error (error);
229 GMainLoop *loop = g_main_loop_new (NULL, FALSE);
231 g_socket_address_enumerator_next_async (enumerator,
235 g_main_loop_run (loop);
236 g_main_loop_unref (loop);
241 use_enumerator (gboolean synchronous)
243 GSocketAddressEnumerator *enumerator;
245 enumerator = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
249 printf ("Proxies for URI '%s' are:\n", uri);
250 run_with_enumerator (synchronous, enumerator);
252 g_object_unref (enumerator);
262 main (gint argc, gchar **argv)
264 gboolean synchronous = FALSE;
265 gboolean cancel = FALSE;
266 ProxyTestType type = USE_RESOLVER;
270 while (argc >= 2 && argv[1][0] == '-')
272 if (!strcmp (argv[1], "-s"))
274 else if (!strcmp (argv[1], "-c"))
276 else if (!strcmp (argv[1], "-e"))
277 type = USE_ENUMERATOR;
288 /* Save URI for asyncrhonous callback */
293 cancellable = g_cancellable_new ();
294 g_cancellable_cancel (cancellable);
300 use_resolver (synchronous);
303 use_enumerator (synchronous);