1 /* GIO - GLib Input, Output and Streaming Library
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 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
25 #include "gproxyresolver.h"
30 #include "gasyncresult.h"
31 #include "gcancellable.h"
32 #include "giomodule.h"
33 #include "giomodule-priv.h"
34 #include "gsimpleasyncresult.h"
37 * SECTION:gproxyresolver
38 * @short_description: Asynchronous and cancellable network proxy resolver
41 * #GProxyResolver provides synchronous and asynchronous network proxy
42 * resolution. #GProxyResolver is used within #GSocketClient through
43 * the method g_socket_connectable_proxy_enumerate().
46 G_DEFINE_INTERFACE (GProxyResolver, g_proxy_resolver, G_TYPE_OBJECT)
49 g_proxy_resolver_default_init (GProxyResolverInterface *iface)
54 get_default_proxy_resolver (gpointer arg)
56 const gchar *use_this;
57 GProxyResolver *resolver;
59 GIOExtensionPoint *ep;
60 GIOExtension *extension;
63 use_this = g_getenv ("GIO_USE_PROXY_RESOLVER");
65 /* Ensure proxy-resolver modules loaded */
66 _g_io_modules_ensure_loaded ();
68 ep = g_io_extension_point_lookup (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
72 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
75 resolver = g_object_new (g_io_extension_get_type (extension), NULL);
77 if (g_proxy_resolver_is_supported (resolver))
80 g_object_unref (resolver);
84 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
88 resolver = g_object_new (g_io_extension_get_type (extension), NULL);
90 if (g_proxy_resolver_is_supported (resolver))
93 g_object_unref (resolver);
100 * g_proxy_resolver_get_default:
102 * Gets the default #GProxyResolver for the system.
104 * Return value: (transfer none): the default #GProxyResolver.
109 g_proxy_resolver_get_default (void)
111 static GOnce once_init = G_ONCE_INIT;
113 return g_once (&once_init, get_default_proxy_resolver, NULL);
117 * g_proxy_resolver_is_supported:
118 * @resolver: a #GProxyResolver
120 * Checks if @resolver can be used on this system. (This is used
121 * internally; g_proxy_resolver_get_default() will only return a proxy
122 * resolver that returns %TRUE for this method.)
124 * Return value: %TRUE if @resolver is supported.
129 g_proxy_resolver_is_supported (GProxyResolver *resolver)
131 GProxyResolverInterface *iface;
133 g_return_val_if_fail (G_IS_PROXY_RESOLVER (resolver), FALSE);
135 iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
137 return (* iface->is_supported) (resolver);
141 * g_proxy_resolver_lookup:
142 * @resolver: a #GProxyResolver
143 * @uri: a URI representing the destination to connect to
144 * @cancellable: (allow-none): a #GCancellable, or %NULL
145 * @error: return location for a #GError, or %NULL
147 * Looks into the system proxy configuration to determine what proxy,
148 * if any, to use to connect to @uri. The returned proxy URIs are of the
149 * form <literal><protocol>://[user[:password]@]host:port</literal>
150 * or <literal>direct://</literal>, where <protocol> could be
151 * http, rtsp, socks or other proxying protocol.
153 * If you don't know what network protocol is being used on the
154 * socket, you should use <literal>none</literal> as the URI protocol.
155 * In this case, the resolver might still return a generic proxy type
156 * (such as SOCKS), but would not return protocol-specific proxy types
159 * <literal>direct://</literal> is used when no proxy is needed.
160 * Direct connection should not be attempted unless it is part of the
161 * returned array of proxies.
163 * Return value: (transfer full) (array zero-terminated=1): A
164 * NULL-terminated array of proxy URIs. Must be freed
170 g_proxy_resolver_lookup (GProxyResolver *resolver,
172 GCancellable *cancellable,
175 GProxyResolverInterface *iface;
177 g_return_val_if_fail (G_IS_PROXY_RESOLVER (resolver), NULL);
178 g_return_val_if_fail (uri != NULL, NULL);
180 iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
182 return (* iface->lookup) (resolver, uri, cancellable, error);
186 * g_proxy_resolver_lookup_async:
187 * @resolver: a #GProxyResolver
188 * @uri: a URI representing the destination to connect to
189 * @cancellable: (allow-none): a #GCancellable, or %NULL
190 * @callback: (scope async): callback to call after resolution completes
191 * @user_data: (closure): data for @callback
193 * Asynchronous lookup of proxy. See g_proxy_resolver_lookup() for more
199 g_proxy_resolver_lookup_async (GProxyResolver *resolver,
201 GCancellable *cancellable,
202 GAsyncReadyCallback callback,
205 GProxyResolverInterface *iface;
207 g_return_if_fail (G_IS_PROXY_RESOLVER (resolver));
208 g_return_if_fail (uri != NULL);
210 iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
212 (* iface->lookup_async) (resolver, uri, cancellable, callback, user_data);
216 * g_proxy_resolver_lookup_finish:
217 * @resolver: a #GProxyResolver
218 * @result: the result passed to your #GAsyncReadyCallback
219 * @error: return location for a #GError, or %NULL
221 * Call this function to obtain the array of proxy URIs when
222 * g_proxy_resolver_lookup_async() is complete. See
223 * g_proxy_resolver_lookup() for more details.
225 * Return value: (transfer full) (array zero-terminated=1): A
226 * NULL-terminated array of proxy URIs. Must be freed
232 g_proxy_resolver_lookup_finish (GProxyResolver *resolver,
233 GAsyncResult *result,
236 GProxyResolverInterface *iface;
238 g_return_val_if_fail (G_IS_PROXY_RESOLVER (resolver), NULL);
240 iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
242 return (* iface->lookup_finish) (resolver, result, error);