43851875b6ae1fcd66c11db1cac8693b817bd8a7
[platform/upstream/glib.git] / gio / tests / proxy.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2010 Collabora, Ltd.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Authors: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
21  */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <gio/gio.h>
30 #include <glib.h>
31
32 #include "glibintl.h"
33
34 static const gchar *uri = NULL;
35 static GCancellable *cancellable = NULL;
36 static gint return_value = 0;
37
38 static void G_GNUC_NORETURN
39 usage (void)
40 {
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");
45   exit (1);
46 }
47
48 static void
49 print_and_free_error (GError *error)
50 {
51   fprintf (stderr, "Failed to obtain proxies: %s\n", error->message);
52   g_error_free (error);
53   return_value = 1;
54 }
55
56 static void
57 print_proxies (const gchar *uri, gchar **proxies)
58 {
59   printf ("Proxies for URI '%s' are:\n", uri);
60
61   if (proxies == NULL || proxies[0] == NULL)
62     printf ("\tnone\n");
63   else
64     for (; proxies[0]; proxies++)
65       printf ("\t%s\n", proxies[0]);
66 }
67
68 static void
69 _proxy_lookup_cb (GObject *source_object,
70                   GAsyncResult *result,
71                   gpointer user_data)
72 {
73   GError *error = NULL;
74   gchar **proxies;
75   GMainLoop *loop = user_data;
76
77   proxies = g_proxy_resolver_lookup_finish (G_PROXY_RESOLVER (source_object),
78                                             result,
79                                             &error);
80   if (error)
81     {
82       print_and_free_error (error);
83     }
84   else
85     {
86       print_proxies (uri, proxies);
87       g_strfreev (proxies);
88     }
89
90   g_main_loop_quit (loop);
91 }
92
93 static void
94 use_resolver (gboolean synchronous)
95 {
96   GProxyResolver *resolver;
97
98   resolver = g_proxy_resolver_get_default ();
99
100   if (synchronous)
101     {
102       GError *error = NULL;
103       gchar **proxies;
104
105       proxies = g_proxy_resolver_lookup (resolver, uri, cancellable, &error);
106
107       if (error)
108           print_and_free_error (error);
109       else
110           print_proxies (uri, proxies);
111
112       g_strfreev (proxies);
113     }
114   else
115     {
116       GMainLoop *loop = g_main_loop_new (NULL, FALSE);
117
118       g_proxy_resolver_lookup_async (resolver,
119                                      uri,
120                                      cancellable,
121                                      _proxy_lookup_cb,
122                                      loop);
123
124       g_main_loop_run (loop);
125       g_main_loop_unref (loop);
126     }
127 }
128
129 static void
130 print_proxy_address (GSocketAddress *sockaddr)
131 {
132   GProxyAddress *proxy = NULL;
133
134   if (sockaddr == NULL)
135     {
136       printf ("\tdirect://\n");
137       return;
138     }
139
140   if (G_IS_PROXY_ADDRESS (sockaddr))
141     {
142       proxy = G_PROXY_ADDRESS (sockaddr);
143       printf ("\t%s://", g_proxy_address_get_protocol(proxy));
144     }
145   else
146     {
147       printf ("\tdirect://");
148     }
149
150   if (G_IS_INET_SOCKET_ADDRESS (sockaddr))
151     {
152       GInetAddress *inetaddr;
153       guint port;
154       gchar *addr;
155
156       g_object_get (sockaddr,
157                     "address", &inetaddr,
158                     "port", &port,
159                     NULL);
160
161       addr = g_inet_address_to_string (inetaddr);
162
163       printf ("%s:%u", addr, port);
164
165       g_free (addr);
166     }
167
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));
172
173   printf ("\n");
174 }
175
176 static void
177 _proxy_enumerate_cb (GObject *object,
178                      GAsyncResult *result,
179                      gpointer user_data)
180 {
181   GError *error = NULL;
182   GMainLoop *loop = user_data;
183   GSocketAddressEnumerator *enumerator = G_SOCKET_ADDRESS_ENUMERATOR (object);
184   GSocketAddress *sockaddr;
185
186   sockaddr = g_socket_address_enumerator_next_finish (enumerator,
187                                                       result,
188                                                       &error);
189   if (sockaddr)
190     {
191       print_proxy_address (sockaddr);
192       g_socket_address_enumerator_next_async (enumerator,
193                                               cancellable,
194                                               _proxy_enumerate_cb,
195                                               loop);
196       g_object_unref (sockaddr);
197     }
198   else
199     {
200       if (error)
201         print_and_free_error (error);
202
203       g_main_loop_quit (loop);
204     }
205 }
206
207 static void
208 run_with_enumerator (gboolean synchronous, GSocketAddressEnumerator *enumerator)
209 {
210   GError *error = NULL;
211
212   if (synchronous)
213     {
214       GSocketAddress *sockaddr;
215
216       while ((sockaddr = g_socket_address_enumerator_next(enumerator,
217                                                           cancellable,
218                                                           &error)))
219         {
220           print_proxy_address (sockaddr);
221           g_object_unref (sockaddr);
222         }
223
224       if (error)
225         print_and_free_error (error);
226     }
227   else
228     {
229       GMainLoop *loop = g_main_loop_new (NULL, FALSE);
230
231       g_socket_address_enumerator_next_async (enumerator,
232                                               cancellable,
233                                               _proxy_enumerate_cb,
234                                               loop);
235       g_main_loop_run (loop);
236       g_main_loop_unref (loop);
237     }
238 }
239
240 static void
241 use_enumerator (gboolean synchronous)
242 {
243   GSocketAddressEnumerator *enumerator;
244
245   enumerator = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
246                              "uri", uri,
247                              NULL);
248
249   printf ("Proxies for URI '%s' are:\n", uri);
250   run_with_enumerator (synchronous, enumerator);
251
252   g_object_unref (enumerator);
253 }
254
255 typedef enum
256 {
257   USE_RESOLVER,
258   USE_ENUMERATOR,
259 } ProxyTestType;
260
261 gint
262 main (gint argc, gchar **argv)
263 {
264   gboolean synchronous = FALSE;
265   gboolean cancel = FALSE;
266   ProxyTestType type = USE_RESOLVER;
267
268   g_type_init ();
269
270   while (argc >= 2 && argv[1][0] == '-')
271     {
272       if (!strcmp (argv[1], "-s"))
273         synchronous = TRUE;
274       else if (!strcmp (argv[1], "-c"))
275         cancel = TRUE;
276       else if (!strcmp (argv[1], "-e"))
277         type = USE_ENUMERATOR;
278       else
279         usage ();
280
281       argv++;
282       argc--;
283     }
284
285   if (argc != 2)
286     usage ();
287
288   /* Save URI for asyncrhonous callback */
289   uri = argv[1];
290
291   if (cancel)
292     {
293       cancellable = g_cancellable_new ();
294       g_cancellable_cancel (cancellable);
295     }
296
297   switch (type)
298     {
299     case USE_RESOLVER:
300       use_resolver (synchronous);
301       break;
302     case USE_ENUMERATOR:
303       use_enumerator (synchronous);
304       break;
305     }
306
307   return return_value;
308 }